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
14 #include "cmos.h" // inb_cmos
19 /****************************************************************
20 * IPL and BCV handlers
21 ****************************************************************/
28 dprintf(3, "init boot device ordering\n");
30 memset(&IPL, 0, sizeof(IPL));
33 struct ipl_entry_s *ie = &IPL.bev[0];
34 ie->type = IPL_TYPE_FLOPPY;
35 ie->description = "Floppy";
39 ie->type = IPL_TYPE_HARDDISK;
40 ie->description = "Hard Disk";
44 if (CONFIG_CDROM_BOOT) {
45 ie->type = IPL_TYPE_CDROM;
46 ie->description = "CD-Rom";
50 IPL.bevcount = ie - IPL.bev;
51 IPL.bcv_override = -1;
52 SET_EBDA(boot_sequence, 0xffff);
53 if (CONFIG_COREBOOT) {
54 // XXX - hardcode defaults for coreboot.
55 IPL.bootorder = 0x00000231;
56 IPL.checkfloppysig = 1;
58 // On emulators, get boot order from nvram.
59 IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
60 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
61 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
62 IPL.checkfloppysig = 1;
66 // Add a BEV vector for a given pnp compatible option rom.
68 add_bev(u16 seg, u16 bev, u16 desc)
72 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
75 struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++];
76 ie->type = IPL_TYPE_BEV;
77 ie->vector = (seg << 16) | bev;
78 const char *d = "Unknown";
80 d = MAKE_FLATPTR(seg, desc);
84 // Add a bcv entry for an expansion card harddrive or legacy option rom
86 add_bcv(u16 seg, u16 ip, u16 desc)
90 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
93 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
94 ie->type = IPL_TYPE_BEV;
95 ie->vector = (seg << 16) | ip;
96 const char *d = "Legacy option rom";
98 d = MAKE_FLATPTR(seg, desc);
102 // Add a bcv entry for an ata harddrive
104 add_bcv_hd(int driveid, const char *desc)
108 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
111 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
112 ie->type = IPL_TYPE_HARDDISK;
113 ie->vector = driveid;
114 ie->description = desc;
118 /****************************************************************
119 * Boot menu and BCV execution
120 ****************************************************************/
122 // Show a generic menu item
124 menu_show_default(struct ipl_entry_s *ie, int menupos)
127 printf("%d. %s\n", menupos
128 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
132 // Show floppy menu item - but only if there exists a floppy drive.
134 menu_show_floppy(struct ipl_entry_s *ie, int menupos)
138 return menu_show_default(ie, menupos);
141 // Show menu items from BCV list.
143 menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
146 for (i = 0; i < IPL.bcvcount; i++) {
147 struct ipl_entry_s *ie = &IPL.bcv[i];
149 case IPL_TYPE_HARDDISK:
150 printf("%d. ata%d-%d %s\n", menupos + i
151 , ie->vector / 2, ie->vector %2, ie->description);
154 menu_show_default(ie, menupos+i);
161 // Show cdrom menu item - but only if there exists a cdrom drive.
163 menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
167 return menu_show_default(ie, menupos);
170 // Show IPL option menu.
172 interactive_bootmenu()
174 if (! CONFIG_BOOTMENU)
177 while (get_keystroke(0) >= 0)
180 printf("Press F12 for boot menu.\n\n");
182 int scan_code = get_keystroke(2500);
183 if (scan_code != 0x86)
187 while (get_keystroke(0) >= 0)
190 printf("Select boot device:\n\n");
192 int subcount[ARRAY_SIZE(IPL.bev)];
195 for (i = 0; i < IPL.bevcount; i++) {
196 struct ipl_entry_s *ie = &IPL.bev[i];
199 case IPL_TYPE_FLOPPY:
200 sc = menu_show_floppy(ie, menupos);
202 case IPL_TYPE_HARDDISK:
203 sc = menu_show_harddisk(ie, menupos);
206 sc = menu_show_cdrom(ie, menupos);
209 sc = menu_show_default(ie, menupos);
217 scan_code = get_keystroke(1000);
218 if (scan_code == 0x01)
221 if (scan_code < 1 || scan_code > menupos)
223 int choice = scan_code - 1;
225 // Find out which IPL this was for.
227 while (choice > subcount[bev]) {
228 choice -= subcount[bev];
232 // A harddrive request enables a BCV order.
233 if (IPL.bev[bev].type == IPL_TYPE_HARDDISK)
234 IPL.bcv_override = choice-1;
236 // Add user choice to the boot order.
237 IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
243 // Run the specified bcv.
245 run_bcv(struct ipl_entry_s *ie)
248 case IPL_TYPE_HARDDISK:
249 map_drive(ie->vector);
252 call_bcv(ie->vector >> 16, ie->vector & 0xffff);
257 // Prepare for boot - show menu and run bcvs.
264 // Allow user to modify BCV/IPL order.
265 interactive_bootmenu();
268 int override = IPL.bcv_override;
270 run_bcv(&IPL.bcv[override]);
272 for (i=0; i<IPL.bcvcount; i++)
274 run_bcv(&IPL.bcv[i]);
278 /****************************************************************
279 * Boot code (int 18/19)
280 ****************************************************************/
282 // Jump to a bootup entry point.
284 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
286 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
289 memset(&br, 0, sizeof(br));
292 // Set the magic number in ax and the boot drive in dl.
298 // Boot from a disk (either floppy or harddrive)
300 boot_disk(u8 bootdrv, int checksig)
302 u16 bootseg = 0x07c0;
306 memset(&br, 0, sizeof(br));
312 call16_int(0x13, &br);
314 if (br.flags & F_CF) {
315 printf("Boot failed: could not read the boot disk\n\n");
320 struct mbr_s *mbr = (void*)0;
321 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
322 printf("Boot failed: not a bootable disk\n\n");
327 /* Canonicalize bootseg:bootip */
328 u16 bootip = (bootseg & 0x0fff) << 4;
331 call_boot_entry(bootseg, bootip, bootdrv);
334 // Boot from a CD-ROM
338 if (! CONFIG_CDROM_BOOT)
340 int status = cdrom_boot();
342 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
346 u16 ebda_seg = get_ebda_seg();
347 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
348 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
349 /* Canonicalize bootseg:bootip */
350 u16 bootip = (bootseg & 0x0fff) << 4;
353 call_boot_entry(bootseg, bootip, bootdrv);
360 panic("Boot support not compiled in.\n");
362 u32 bootdev = IPL.bootorder;
363 bootdev >>= 4 * seq_nr;
367 panic("No bootable device.\n");
369 /* Translate bootdev to an IPL table offset by subtracting 1 */
372 if (bootdev >= IPL.bevcount) {
373 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
377 /* Do the loading, and set up vector as a far pointer to the boot
378 * address, and bootdrv as the boot drive */
379 struct ipl_entry_s *ie = &IPL.bev[bootdev];
381 printf("Booting from %s...\n"
382 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
385 case IPL_TYPE_FLOPPY:
386 boot_disk(0x00, IPL.checkfloppysig);
388 case IPL_TYPE_HARDDISK:
395 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
399 // Boot failed: invoke the boot recovery function
402 memset(&br, 0, sizeof(br));
403 call16_int(0x18, &br);
406 // Boot Failure recovery: try the next device.
410 debug_serial_setup();
411 debug_enter(NULL, DEBUG_HDL_18);
412 u16 ebda_seg = get_ebda_seg();
413 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
414 SET_EBDA2(ebda_seg, boot_sequence, seq);
418 // INT 19h Boot Load Service Entry Point
422 debug_serial_setup();
423 debug_enter(NULL, DEBUG_HDL_19);
424 SET_EBDA(boot_sequence, 0);
428 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
429 // functions to not be exported as global variables.
430 asm(".global handle_18, handle_19");