1 // 16bit code to load disk image and start system boot.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
8 #include "util.h" // irq_enable
9 #include "biosvar.h" // GET_EBDA
10 #include "config.h" // CONFIG_*
11 #include "ata.h" // ata_detect
12 #include "disk.h" // cdrom_boot
13 #include "bregs.h" // struct bregs
15 // 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
18 char pnp_string[] VISIBLE16 __attribute__((aligned (2))) = " $PnP";
20 //--------------------------------------------------------------------------
22 // displays the boot device
23 //--------------------------------------------------------------------------
25 static const char drivetypes[][10]={
26 "", "Floppy", "Hard Disk", "CD-Rom", "Network"
30 printf_bootdev(u16 bootdev)
32 u16 type = GET_EBDA(ipl.table[bootdev].type);
34 /* NIC appears as type 0x80 */
35 if (type == IPL_TYPE_BEV)
37 if (type == 0 || type > 0x4)
38 BX_PANIC("Bad drive type\n");
39 printf("%s", drivetypes[type]);
41 /* print product string if BEV */
42 void *far_description = (void*)GET_EBDA(ipl.table[bootdev].description);
43 if (type == 4 && far_description != 0) {
45 /* first 32 bytes are significant */
46 memcpy(MAKE_FARPTR(GET_SEG(SS), &description), far_description, 32);
47 /* terminate string */
49 printf(" [%.s]", description);
54 print_boot_device(u16 bootdev)
56 printf("Booting from ");
57 printf_bootdev(bootdev);
61 //--------------------------------------------------------------------------
63 // displays the reason why boot failed
64 //--------------------------------------------------------------------------
66 print_boot_failure(u16 type, u8 reason)
68 if (type == 0 || type > 0x3)
69 BX_PANIC("Bad drive type\n");
71 printf("Boot failed");
73 /* Report the reason too */
75 printf(": not a bootable disk");
77 printf(": could not read the boot disk");
87 SET_EBDA(ipl.sequence, seq_nr);
89 u32 bootdev = GET_EBDA(ipl.bootorder);
90 bootdev >>= 4 * seq_nr;
94 BX_PANIC("No bootable device.\n");
96 /* Translate bootdev to an IPL table offset by subtracting 1 */
99 if (bootdev >= GET_EBDA(ipl.count)) {
100 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
104 /* Do the loading, and set up vector as a far pointer to the boot
105 * address, and bootdrv as the boot drive */
106 print_boot_device(bootdev);
108 u16 type = GET_EBDA(ipl.table[bootdev].type);
114 case IPL_TYPE_FLOPPY: /* FDD */
115 case IPL_TYPE_HARDDISK: /* HDD */
117 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
121 memset(&cr, 0, sizeof(cr));
127 call16_int(0x13, &cr);
129 if (cr.flags & F_CF) {
130 print_boot_failure(type, 1);
134 /* Always check the signature on a HDD boot sector; on FDD,
135 * only do the check if configured for it */
136 if (type != IPL_TYPE_FLOPPY || GET_EBDA(ipl.checkfloppysig)) {
137 if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
138 print_boot_failure(type, 0);
143 /* Canonicalize bootseg:bootip */
144 bootip = (bootseg & 0x0fff) << 4;
147 case IPL_TYPE_CDROM: {
149 if (! CONFIG_CDROM_BOOT)
151 int status = cdrom_boot();
153 printf("CDROM boot failure code : %04x\n", status);
154 print_boot_failure(type, 1);
158 bootdrv = GET_EBDA(cdemu.emulated_drive);
159 bootseg = GET_EBDA(cdemu.load_segment);
160 /* Canonicalize bootseg:bootip */
161 bootip = (bootseg & 0x0fff) << 4;
166 /* Expansion ROM with a Bootstrap Entry Vector (a far
168 u32 vector = GET_EBDA(ipl.table[bootdev].vector);
169 bootseg = vector >> 16;
170 bootip = vector & 0xffff;
178 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
180 memset(&cr, 0, sizeof(cr));
183 // Set the magic number in ax and the boot drive in dl.
194 // Boot failed: invoke the boot recovery function
196 memset(&br, 0, sizeof(br));
197 call16_int(0x18, &br);
200 // Boot Failure recovery: try the next device.
204 debug_serial_setup();
205 debug_enter(NULL, DEBUG_HDL_18);
206 u16 seq = GET_EBDA(ipl.sequence) + 1;
210 // INT 19h Boot Load Service Entry Point
214 debug_serial_setup();
215 debug_enter(NULL, DEBUG_HDL_19);