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" // dprintf
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
20 /****************************************************************
21 * IPL and BCV handlers
22 ****************************************************************/
29 dprintf(3, "init boot device ordering\n");
31 memset(&IPL, 0, sizeof(IPL));
34 struct ipl_entry_s *ie = &IPL.bev[0];
35 ie->type = IPL_TYPE_FLOPPY;
36 ie->description = "Floppy";
40 ie->type = IPL_TYPE_HARDDISK;
41 ie->description = "Hard Disk";
45 if (CONFIG_CDROM_BOOT) {
46 ie->type = IPL_TYPE_CDROM;
47 ie->description = "CD-Rom";
51 if (CONFIG_COREBOOT_FLASH) {
52 ie->type = IPL_TYPE_CBFS;
53 ie->description = "CBFS";
57 IPL.bevcount = ie - IPL.bev;
58 SET_EBDA(boot_sequence, 0xffff);
59 if (CONFIG_COREBOOT) {
60 // XXX - hardcode defaults for coreboot.
61 IPL.bootorder = 0x87654231;
62 IPL.checkfloppysig = 1;
64 // On emulators, get boot order from nvram.
65 IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
66 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
67 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
68 IPL.checkfloppysig = 1;
72 // Add a BEV vector for a given pnp compatible option rom.
74 add_bev(u16 seg, u16 bev, u16 desc)
78 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
81 struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++];
82 ie->type = IPL_TYPE_BEV;
83 ie->vector = (seg << 16) | bev;
84 const char *d = "Unknown";
86 d = MAKE_FLATPTR(seg, desc);
90 // Add a bcv entry for an expansion card harddrive or legacy option rom
92 add_bcv(u16 seg, u16 ip, u16 desc)
96 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
99 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
100 ie->type = BCV_TYPE_EXTERNAL;
101 ie->vector = (seg << 16) | ip;
102 const char *d = "Legacy option rom";
104 d = MAKE_FLATPTR(seg, desc);
108 // Add a bcv entry for an internal harddrive
110 add_bcv_internal(struct drive_s *drive_g)
114 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
117 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
118 ie->type = BCV_TYPE_INTERNAL;
119 ie->vector = (u32)drive_g;
120 ie->description = "";
124 /****************************************************************
125 * Boot menu and BCV execution
126 ****************************************************************/
128 // Show a generic menu item
130 menu_show_default(struct ipl_entry_s *ie, int menupos)
133 printf("%d. %s\n", menupos
134 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
138 // Show floppy menu item - but only if there exists a floppy drive.
140 menu_show_floppy(struct ipl_entry_s *ie, int menupos)
143 for (i = 0; i < Drives.floppycount; i++) {
144 struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, i);
145 printf("%d. Floppy [", menupos + i);
146 describe_drive(drive_g);
149 return Drives.floppycount;
152 // Show menu items from BCV list.
154 menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
157 for (i = 0; i < IPL.bcvcount; i++) {
158 struct ipl_entry_s *ie = &IPL.bcv[i];
160 case BCV_TYPE_INTERNAL:
161 printf("%d. ", menupos + i);
162 describe_drive((void*)ie->vector);
166 menu_show_default(ie, menupos+i);
173 // Show cdrom menu item - but only if there exists a cdrom drive.
175 menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
178 for (i = 0; i < Drives.cdcount; i++) {
179 struct drive_s *drive_g = getDrive(EXTTYPE_CD, i);
180 printf("%d. CD-Rom [", menupos + i);
181 describe_drive(drive_g);
184 return Drives.cdcount;
187 // Show coreboot-fs menu item.
189 menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
192 struct cbfs_file *file = NULL;
194 file = cbfs_findprefix("img/", file);
197 const char *filename = cbfs_filename(file);
198 printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
206 // Show IPL option menu.
208 interactive_bootmenu()
210 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
213 while (get_keystroke(0) >= 0)
216 printf("Press F12 for boot menu.\n\n");
218 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
219 if (scan_code != 0x86)
223 while (get_keystroke(0) >= 0)
226 printf("Select boot device:\n\n");
228 int subcount[ARRAY_SIZE(IPL.bev)];
231 for (i = 0; i < IPL.bevcount; i++) {
232 struct ipl_entry_s *ie = &IPL.bev[i];
235 case IPL_TYPE_FLOPPY:
236 sc = menu_show_floppy(ie, menupos);
238 case IPL_TYPE_HARDDISK:
239 sc = menu_show_harddisk(ie, menupos);
242 sc = menu_show_cdrom(ie, menupos);
245 sc = menu_show_cbfs(ie, menupos);
248 sc = menu_show_default(ie, menupos);
256 scan_code = get_keystroke(1000);
257 if (scan_code == 0x01)
260 if (scan_code < 1 || scan_code > menupos)
262 int choice = scan_code - 1;
264 // Find out which IPL this was for.
266 while (choice > subcount[bev]) {
267 choice -= subcount[bev];
270 IPL.bev[bev].subchoice = choice-1;
272 // Add user choice to the boot order.
273 IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
279 // Run the specified bcv.
281 run_bcv(struct ipl_entry_s *ie)
284 case BCV_TYPE_INTERNAL:
285 map_hd_drive((void*)ie->vector);
287 case BCV_TYPE_EXTERNAL:
288 call_bcv(ie->vector >> 16, ie->vector & 0xffff);
293 // Prepare for boot - show menu and run bcvs.
300 // Allow user to modify BCV/IPL order.
301 interactive_bootmenu();
303 // Setup floppy boot order
304 int override = IPL.bev[0].subchoice;
305 int tmp = Drives.idmap[EXTTYPE_FLOPPY][0];
306 Drives.idmap[EXTTYPE_FLOPPY][0] = Drives.idmap[EXTTYPE_FLOPPY][override];
307 Drives.idmap[EXTTYPE_FLOPPY][override] = tmp;
310 override = IPL.bev[1].subchoice;
311 if (override < IPL.bcvcount)
312 run_bcv(&IPL.bcv[override]);
314 for (i=0; i<IPL.bcvcount; i++)
316 run_bcv(&IPL.bcv[i]);
320 /****************************************************************
321 * Boot code (int 18/19)
322 ****************************************************************/
324 // Jump to a bootup entry point.
326 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
328 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
331 memset(&br, 0, sizeof(br));
333 br.code = SEGOFF(bootseg, bootip);
334 // Set the magic number in ax and the boot drive in dl.
340 // Boot from a disk (either floppy or harddrive)
342 boot_disk(u8 bootdrv, int checksig)
344 u16 bootseg = 0x07c0;
348 memset(&br, 0, sizeof(br));
355 call16_int(0x13, &br);
357 if (br.flags & F_CF) {
358 printf("Boot failed: could not read the boot disk\n\n");
363 struct mbr_s *mbr = (void*)0;
364 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
365 printf("Boot failed: not a bootable disk\n\n");
370 /* Canonicalize bootseg:bootip */
371 u16 bootip = (bootseg & 0x0fff) << 4;
374 call_boot_entry(bootseg, bootip, bootdrv);
377 // Boot from a CD-ROM
379 boot_cdrom(struct ipl_entry_s *ie)
381 if (! CONFIG_CDROM_BOOT)
383 int status = cdrom_boot(ie->subchoice);
385 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
389 u16 ebda_seg = get_ebda_seg();
390 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
391 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
392 /* Canonicalize bootseg:bootip */
393 u16 bootip = (bootseg & 0x0fff) << 4;
396 call_boot_entry(bootseg, bootip, bootdrv);
399 // Boot from a CBFS payload
401 boot_cbfs(struct ipl_entry_s *ie)
403 if (! CONFIG_COREBOOT_FLASH)
405 int count = ie->subchoice;
406 struct cbfs_file *file;
408 file = cbfs_findprefix("img/", file);
413 cbfs_run_payload(file);
421 panic("Boot support not compiled in.\n");
423 u32 bootdev = IPL.bootorder;
424 bootdev >>= 4 * seq_nr;
427 /* Translate bootdev to an IPL table offset by subtracting 1 */
430 if (bootdev >= IPL.bevcount) {
431 printf("No bootable device.\n");
432 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
437 /* Do the loading, and set up vector as a far pointer to the boot
438 * address, and bootdrv as the boot drive */
439 struct ipl_entry_s *ie = &IPL.bev[bootdev];
441 printf("Booting from %s...\n"
442 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
445 case IPL_TYPE_FLOPPY:
446 boot_disk(0x00, IPL.checkfloppysig);
448 case IPL_TYPE_HARDDISK:
458 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
462 // Boot failed: invoke the boot recovery function
464 memset(&br, 0, sizeof(br));
466 call16_int(0x18, &br);
469 // Boot Failure recovery: try the next device.
473 debug_serial_setup();
474 debug_enter(NULL, DEBUG_HDL_18);
475 u16 ebda_seg = get_ebda_seg();
476 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
477 SET_EBDA2(ebda_seg, boot_sequence, seq);
481 // INT 19h Boot Load Service Entry Point
485 debug_serial_setup();
486 debug_enter(NULL, DEBUG_HDL_19);
487 SET_EBDA(boot_sequence, 0);