1 // 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 LGPLv3 license.
8 #include "util.h" // irq_enable
9 #include "biosvar.h" // GET_EBDA
10 #include "config.h" // CONFIG_*
11 #include "disk.h" // cdrom_boot
12 #include "bregs.h" // struct bregs
13 #include "boot.h" // struct ipl_s
17 //--------------------------------------------------------------------------
19 // displays the boot device
20 //--------------------------------------------------------------------------
22 static const char drivetypes[][10]={
23 "", "Floppy", "Hard Disk", "CD-Rom", "Network"
27 printf_bootdev(u16 bootdev)
29 u16 type = IPL.table[bootdev].type;
31 /* NIC appears as type 0x80 */
32 if (type == IPL_TYPE_BEV)
34 if (type == 0 || type > 0x4)
35 BX_PANIC("Bad drive type\n");
36 printf("%s", drivetypes[type]);
38 /* print product string if BEV */
39 char *description_fl = IPL.table[bootdev].description;
40 if (type == 4 && description_fl != 0) {
42 /* first 32 bytes are significant */
43 memcpy(description, description_fl, 32);
44 /* terminate string */
46 printf(" [%.s]", description);
51 print_boot_device(u16 bootdev)
53 printf("Booting from ");
54 printf_bootdev(bootdev);
58 //--------------------------------------------------------------------------
60 // displays the reason why boot failed
61 //--------------------------------------------------------------------------
63 print_boot_failure(u16 type, u8 reason)
65 if (type == 0 || type > 0x3)
66 BX_PANIC("Bad drive type\n");
68 printf("Boot failed");
70 /* Report the reason too */
72 printf(": not a bootable disk");
74 printf(": could not read the boot disk");
83 BX_PANIC("Boot support not compiled in.\n");
85 u32 bootdev = IPL.bootorder;
86 bootdev >>= 4 * seq_nr;
90 BX_PANIC("No bootable device.\n");
92 /* Translate bootdev to an IPL table offset by subtracting 1 */
95 if (bootdev >= IPL.count) {
96 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
100 /* Do the loading, and set up vector as a far pointer to the boot
101 * address, and bootdrv as the boot drive */
102 print_boot_device(bootdev);
104 u16 type = IPL.table[bootdev].type;
110 case IPL_TYPE_FLOPPY:
111 case IPL_TYPE_HARDDISK:
113 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
117 memset(&cr, 0, sizeof(cr));
123 call16_int(0x13, &cr);
125 if (cr.flags & F_CF) {
126 print_boot_failure(type, 1);
130 /* Always check the signature on a HDD boot sector; on FDD,
131 * only do the check if configured for it */
132 if (type != IPL_TYPE_FLOPPY || IPL.checkfloppysig) {
133 struct mbr_s *mbr = (void*)0;
134 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
135 print_boot_failure(type, 0);
140 /* Canonicalize bootseg:bootip */
141 bootip = (bootseg & 0x0fff) << 4;
144 case IPL_TYPE_CDROM: {
146 if (! CONFIG_CDROM_BOOT)
148 int status = cdrom_boot();
150 printf("CDROM boot failure code : %04x\n", status);
151 print_boot_failure(type, 1);
155 u16 ebda_seg = get_ebda_seg();
156 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
157 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
158 /* Canonicalize bootseg:bootip */
159 bootip = (bootseg & 0x0fff) << 4;
164 /* Expansion ROM with a Bootstrap Entry Vector (a far
166 u32 vector = IPL.table[bootdev].vector;
167 bootseg = vector >> 16;
168 bootip = vector & 0xffff;
176 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
178 memset(&cr, 0, sizeof(cr));
181 // Set the magic number in ax and the boot drive in dl.
192 // Boot failed: invoke the boot recovery function
194 memset(&br, 0, sizeof(br));
195 call16_int(0x18, &br);
198 // Boot Failure recovery: try the next device.
202 debug_serial_setup();
203 debug_enter(NULL, DEBUG_HDL_18);
204 u16 ebda_seg = get_ebda_seg();
205 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
206 SET_EBDA2(ebda_seg, boot_sequence, seq);
210 // INT 19h Boot Load Service Entry Point
214 debug_serial_setup();
215 debug_enter(NULL, DEBUG_HDL_19);
216 SET_EBDA(boot_sequence, 0);
220 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
221 // functions to not be exported as global variables.
222 asm(".global handle_18, handle_19");