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 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
14 #include "boot.h" // struct ipl_s
18 //--------------------------------------------------------------------------
20 // displays the boot device
21 //--------------------------------------------------------------------------
23 static const char drivetypes[][10]={
24 "", "Floppy", "Hard Disk", "CD-Rom", "Network"
28 printf_bootdev(u16 bootdev)
30 u16 type = IPL.table[bootdev].type;
32 /* NIC appears as type 0x80 */
33 if (type == IPL_TYPE_BEV)
35 if (type == 0 || type > 0x4)
36 BX_PANIC("Bad drive type\n");
37 printf("%s", drivetypes[type]);
39 /* print product string if BEV */
40 char *far_description = IPL.table[bootdev].description;
41 if (type == 4 && far_description != 0) {
43 /* first 32 bytes are significant */
44 memcpy_far(MAKE_FARPTR(GET_SEG(SS), description), far_description, 32);
45 /* terminate string */
47 printf(" [%.s]", description);
52 print_boot_device(u16 bootdev)
54 printf("Booting from ");
55 printf_bootdev(bootdev);
59 //--------------------------------------------------------------------------
61 // displays the reason why boot failed
62 //--------------------------------------------------------------------------
64 print_boot_failure(u16 type, u8 reason)
66 if (type == 0 || type > 0x3)
67 BX_PANIC("Bad drive type\n");
69 printf("Boot failed");
71 /* Report the reason too */
73 printf(": not a bootable disk");
75 printf(": could not read the boot disk");
84 BX_PANIC("Boot support not compiled in.\n");
86 u32 bootdev = IPL.bootorder;
87 bootdev >>= 4 * seq_nr;
91 BX_PANIC("No bootable device.\n");
93 /* Translate bootdev to an IPL table offset by subtracting 1 */
96 if (bootdev >= IPL.count) {
97 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
101 /* Do the loading, and set up vector as a far pointer to the boot
102 * address, and bootdrv as the boot drive */
103 print_boot_device(bootdev);
105 u16 type = IPL.table[bootdev].type;
111 case IPL_TYPE_FLOPPY:
112 case IPL_TYPE_HARDDISK:
114 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
118 memset(&cr, 0, sizeof(cr));
124 call16_int(0x13, &cr);
126 if (cr.flags & F_CF) {
127 print_boot_failure(type, 1);
131 /* Always check the signature on a HDD boot sector; on FDD,
132 * only do the check if configured for it */
133 if (type != IPL_TYPE_FLOPPY || IPL.checkfloppysig) {
134 if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
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 bootdrv = GET_GLOBAL(CDEMU.emulated_drive);
156 bootseg = GET_GLOBAL(CDEMU.load_segment);
157 /* Canonicalize bootseg:bootip */
158 bootip = (bootseg & 0x0fff) << 4;
163 /* Expansion ROM with a Bootstrap Entry Vector (a far
165 u32 vector = IPL.table[bootdev].vector;
166 bootseg = vector >> 16;
167 bootip = vector & 0xffff;
175 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
177 memset(&cr, 0, sizeof(cr));
180 // Set the magic number in ax and the boot drive in dl.
191 // Boot failed: invoke the boot recovery function
193 memset(&br, 0, sizeof(br));
194 call16_int(0x18, &br);
197 // Boot Failure recovery: try the next device.
201 debug_serial_setup();
202 debug_enter(NULL, DEBUG_HDL_18);
203 u16 ebda_seg = get_ebda_seg();
204 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
205 SET_EBDA2(ebda_seg, boot_sequence, seq);
209 // INT 19h Boot Load Service Entry Point
213 debug_serial_setup();
214 debug_enter(NULL, DEBUG_HDL_19);
215 SET_EBDA(boot_sequence, 0);
219 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
220 // functions to not be exported as global variables.
221 asm(".global handle_18, handle_19");