blob: 687be346dc5d2c6be9ca26ee8fcee70cdd01c771 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 16bit code to load disk image and start system boot.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
6// This file may be distributed under the terms of the GNU GPLv3 license.
7
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05008#include "util.h" // irq_enable
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "biosvar.h" // GET_EBDA
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050010#include "config.h" // CONFIG_*
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050011#include "ata.h" // ata_detect
Kevin O'Connor180a9592008-03-04 22:50:53 -050012#include "disk.h" // cdrom_boot
Kevin O'Connor9521e262008-07-04 13:04:29 -040013#include "bregs.h" // struct bregs
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050014
Kevin O'Connor44c631d2008-03-02 11:24:36 -050015// We need a copy of this string, but we are not actually a PnP BIOS,
16// so make sure it is *not* aligned, so OSes will not see it if they
17// scan.
Kevin O'Connor19786762008-03-05 21:09:59 -050018char pnp_string[] VISIBLE16 __attribute__((aligned (2))) = " $PnP";
Kevin O'Connor44c631d2008-03-02 11:24:36 -050019
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050020//--------------------------------------------------------------------------
21// print_boot_device
22// displays the boot device
23//--------------------------------------------------------------------------
24
25static const char drivetypes[][10]={
Kevin O'Connorabc75972008-05-18 00:20:53 -040026 "", "Floppy", "Hard Disk", "CD-Rom", "Network"
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050027};
28
Kevin O'Connore0113c92008-04-05 15:51:12 -040029void
30printf_bootdev(u16 bootdev)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050031{
Kevin O'Connore0113c92008-04-05 15:51:12 -040032 u16 type = GET_EBDA(ipl.table[bootdev].type);
33
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050034 /* NIC appears as type 0x80 */
35 if (type == IPL_TYPE_BEV)
36 type = 0x4;
37 if (type == 0 || type > 0x4)
38 BX_PANIC("Bad drive type\n");
Kevin O'Connore0113c92008-04-05 15:51:12 -040039 printf("%s", drivetypes[type]);
Kevin O'Connora2e73802008-02-27 10:27:00 -050040
Kevin O'Connore0113c92008-04-05 15:51:12 -040041 /* print product string if BEV */
42 void *far_description = (void*)GET_EBDA(ipl.table[bootdev].description);
43 if (type == 4 && far_description != 0) {
44 char description[33];
45 /* first 32 bytes are significant */
46 memcpy(MAKE_FARPTR(GET_SEG(SS), &description), far_description, 32);
47 /* terminate string */
48 description[32] = 0;
49 printf(" [%.s]", description);
50 }
51}
52
53static void
54print_boot_device(u16 bootdev)
55{
56 printf("Booting from ");
57 printf_bootdev(bootdev);
58 printf("...\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050059}
60
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050061//--------------------------------------------------------------------------
62// print_boot_failure
63// displays the reason why boot failed
64//--------------------------------------------------------------------------
65static void
66print_boot_failure(u16 type, u8 reason)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050067{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050068 if (type == 0 || type > 0x3)
69 BX_PANIC("Bad drive type\n");
70
Kevin O'Connora2e73802008-02-27 10:27:00 -050071 printf("Boot failed");
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050072 if (type < 4) {
73 /* Report the reason too */
74 if (reason==0)
75 printf(": not a bootable disk");
76 else
77 printf(": could not read the boot disk");
78 }
Kevin O'Connora2e73802008-02-27 10:27:00 -050079 printf("\n\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050080}
81
82static void
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050083try_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050084{
Kevin O'Connor7a558e42008-03-11 20:38:33 -040085 irq_enable();
86
Kevin O'Connor56a506d2008-03-29 13:15:36 -040087 SET_EBDA(ipl.sequence, seq_nr);
Kevin O'Connor7a558e42008-03-11 20:38:33 -040088
Kevin O'Connorabc75972008-05-18 00:20:53 -040089 u32 bootdev = GET_EBDA(ipl.bootorder);
Kevin O'Connor840c5342008-03-29 14:04:34 -040090 bootdev >>= 4 * seq_nr;
91 bootdev &= 0xf;
Kevin O'Connore0113c92008-04-05 15:51:12 -040092
Kevin O'Connor840c5342008-03-29 14:04:34 -040093 if (bootdev == 0)
94 BX_PANIC("No bootable device.\n");
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050095
Kevin O'Connorabc75972008-05-18 00:20:53 -040096 /* Translate bootdev to an IPL table offset by subtracting 1 */
Kevin O'Connor840c5342008-03-29 14:04:34 -040097 bootdev -= 1;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050098
Kevin O'Connor56a506d2008-03-29 13:15:36 -040099 if (bootdev >= GET_EBDA(ipl.count)) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400100 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500101 return;
102 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500103
104 /* Do the loading, and set up vector as a far pointer to the boot
105 * address, and bootdrv as the boot drive */
Kevin O'Connore0113c92008-04-05 15:51:12 -0400106 print_boot_device(bootdev);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500107
Kevin O'Connore0113c92008-04-05 15:51:12 -0400108 u16 type = GET_EBDA(ipl.table[bootdev].type);
109
110 u16 bootseg, bootip;
111 u8 bootdrv = 0;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500112 struct bregs cr;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500113 switch(type) {
114 case IPL_TYPE_FLOPPY: /* FDD */
115 case IPL_TYPE_HARDDISK: /* HDD */
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500116
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500117 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
118 bootseg = 0x07c0;
119
120 // Read sector
121 memset(&cr, 0, sizeof(cr));
122 cr.dl = bootdrv;
123 cr.es = bootseg;
124 cr.ah = 2;
125 cr.al = 1;
126 cr.cl = 1;
127 call16_int(0x13, &cr);
128
129 if (cr.flags & F_CF) {
130 print_boot_failure(type, 1);
131 return;
132 }
133
134 /* Always check the signature on a HDD boot sector; on FDD,
Kevin O'Connorabc75972008-05-18 00:20:53 -0400135 * only do the check if configured for it */
136 if (type != IPL_TYPE_FLOPPY || GET_EBDA(ipl.checkfloppysig)) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500137 if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
138 print_boot_failure(type, 0);
139 return;
140 }
141 }
142
143 /* Canonicalize bootseg:bootip */
144 bootip = (bootseg & 0x0fff) << 4;
145 bootseg &= 0xf000;
146 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500147 case IPL_TYPE_CDROM: {
148 /* CD-ROM */
149 if (! CONFIG_CDROM_BOOT)
150 break;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400151 int status = cdrom_boot();
Kevin O'Connor180a9592008-03-04 22:50:53 -0500152 if (status) {
153 printf("CDROM boot failure code : %04x\n", status);
154 print_boot_failure(type, 1);
155 return;
156 }
157
158 bootdrv = GET_EBDA(cdemu.emulated_drive);
159 bootseg = GET_EBDA(cdemu.load_segment);
160 /* Canonicalize bootseg:bootip */
161 bootip = (bootseg & 0x0fff) << 4;
162 bootseg &= 0xf000;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500163 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500164 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500165 case IPL_TYPE_BEV: {
166 /* Expansion ROM with a Bootstrap Entry Vector (a far
167 * pointer) */
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400168 u32 vector = GET_EBDA(ipl.table[bootdev].vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500169 bootseg = vector >> 16;
170 bootip = vector & 0xffff;
171 break;
172 }
173 default:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500174 return;
175 }
176
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500177 /* Debugging info */
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400178 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500179
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500180 memset(&cr, 0, sizeof(cr));
181 cr.ip = bootip;
182 cr.cs = bootseg;
183 // Set the magic number in ax and the boot drive in dl.
184 cr.dl = bootdrv;
185 cr.ax = 0xaa55;
186 call16(&cr);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400187}
188
189static void
190do_boot(u16 seq_nr)
191{
192 try_boot(seq_nr);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500193
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500194 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400195 struct bregs br;
196 memset(&br, 0, sizeof(br));
197 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500198}
199
200// Boot Failure recovery: try the next device.
Kevin O'Connor19786762008-03-05 21:09:59 -0500201void VISIBLE16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500202handle_18()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500203{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400204 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400205 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400206 u16 seq = GET_EBDA(ipl.sequence) + 1;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400207 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500208}
209
210// INT 19h Boot Load Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500211void VISIBLE16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500212handle_19()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500213{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400214 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400215 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400216 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500217}