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 //--------------------------------------------------------------------------
17 // displays the boot device
18 //--------------------------------------------------------------------------
20 static const char drivetypes[][10]={
21 "", "Floppy", "Hard Disk", "CD-Rom", "Network"
25 printf_bootdev(u16 bootdev)
27 u16 type = GET_EBDA(ipl.table[bootdev].type);
29 /* NIC appears as type 0x80 */
30 if (type == IPL_TYPE_BEV)
32 if (type == 0 || type > 0x4)
33 BX_PANIC("Bad drive type\n");
34 printf("%s", drivetypes[type]);
36 /* print product string if BEV */
37 void *far_description = (void*)GET_EBDA(ipl.table[bootdev].description);
38 if (type == 4 && far_description != 0) {
40 /* first 32 bytes are significant */
41 memcpy(MAKE_FARPTR(GET_SEG(SS), &description), far_description, 32);
42 /* terminate string */
44 printf(" [%.s]", description);
49 print_boot_device(u16 bootdev)
51 printf("Booting from ");
52 printf_bootdev(bootdev);
56 //--------------------------------------------------------------------------
58 // displays the reason why boot failed
59 //--------------------------------------------------------------------------
61 print_boot_failure(u16 type, u8 reason)
63 if (type == 0 || type > 0x3)
64 BX_PANIC("Bad drive type\n");
66 printf("Boot failed");
68 /* Report the reason too */
70 printf(": not a bootable disk");
72 printf(": could not read the boot disk");
80 SET_EBDA(ipl.sequence, seq_nr);
82 u32 bootdev = GET_EBDA(ipl.bootorder);
83 bootdev >>= 4 * seq_nr;
87 BX_PANIC("No bootable device.\n");
89 /* Translate bootdev to an IPL table offset by subtracting 1 */
92 if (bootdev >= GET_EBDA(ipl.count)) {
93 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
97 /* Do the loading, and set up vector as a far pointer to the boot
98 * address, and bootdrv as the boot drive */
99 print_boot_device(bootdev);
101 u16 type = GET_EBDA(ipl.table[bootdev].type);
107 case IPL_TYPE_FLOPPY: /* FDD */
108 case IPL_TYPE_HARDDISK: /* HDD */
110 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
114 memset(&cr, 0, sizeof(cr));
120 call16_int(0x13, &cr);
122 if (cr.flags & F_CF) {
123 print_boot_failure(type, 1);
127 /* Always check the signature on a HDD boot sector; on FDD,
128 * only do the check if configured for it */
129 if (type != IPL_TYPE_FLOPPY || GET_EBDA(ipl.checkfloppysig)) {
130 if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
131 print_boot_failure(type, 0);
136 /* Canonicalize bootseg:bootip */
137 bootip = (bootseg & 0x0fff) << 4;
140 case IPL_TYPE_CDROM: {
142 if (! CONFIG_CDROM_BOOT)
144 int status = cdrom_boot();
146 printf("CDROM boot failure code : %04x\n", status);
147 print_boot_failure(type, 1);
151 bootdrv = GET_EBDA(cdemu.emulated_drive);
152 bootseg = GET_EBDA(cdemu.load_segment);
153 /* Canonicalize bootseg:bootip */
154 bootip = (bootseg & 0x0fff) << 4;
159 /* Expansion ROM with a Bootstrap Entry Vector (a far
161 u32 vector = GET_EBDA(ipl.table[bootdev].vector);
162 bootseg = vector >> 16;
163 bootip = vector & 0xffff;
171 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
173 memset(&cr, 0, sizeof(cr));
176 // Set the magic number in ax and the boot drive in dl.
187 // Boot failed: invoke the boot recovery function
189 memset(&br, 0, sizeof(br));
190 call16_int(0x18, &br);
193 // Boot Failure recovery: try the next device.
197 debug_serial_setup();
198 debug_enter(NULL, DEBUG_HDL_18);
199 u16 seq = GET_EBDA(ipl.sequence) + 1;
203 // INT 19h Boot Load Service Entry Point
207 debug_serial_setup();
208 debug_enter(NULL, DEBUG_HDL_19);