1 // Code to load disk image and start system boot.
3 // Copyright (C) 2008-2010 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));
32 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 = "DVD/CD";
51 if (CONFIG_COREBOOT && 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;
71 char *f = romfile_loadfile("bootorder", NULL);
76 IPL.fw_bootorder_count = 1;
79 IPL.fw_bootorder_count++;
82 IPL.fw_bootorder = malloc_tmphigh(IPL.fw_bootorder_count*sizeof(char*));
83 if (!IPL.fw_bootorder) {
89 dprintf(3, "boot order:\n");
92 IPL.fw_bootorder[i] = f;
97 dprintf(3, "%d: %s\n", i, IPL.fw_bootorder[i]);
103 // Add a BEV vector for a given pnp compatible option rom.
105 add_bev(u16 seg, u16 bev, u16 desc)
109 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
112 struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++];
113 ie->type = IPL_TYPE_BEV;
114 ie->vector = (seg << 16) | bev;
115 const char *d = "Unknown";
117 d = MAKE_FLATPTR(seg, desc);
121 // Add a IPL entry for BAID cdrom.
123 add_baid_cdrom(struct drive_s *drive_g)
125 if (! CONFIG_CDROM_BOOT)
128 /* put first cdrom into ipl 3 for compatability with qemu */
129 struct ipl_entry_s *ie = &IPL.bev[2];
130 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev) && ie->vector)
134 ie = &IPL.bev[IPL.bevcount++];
135 ie->type = IPL_TYPE_CDROM;
136 ie->vector = (u32)drive_g;
137 ie->description = "DVD/CD";
140 // Add a bcv entry for an expansion card harddrive or legacy option rom
142 add_bcv(u16 seg, u16 ip, u16 desc)
146 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
149 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
150 ie->type = BCV_TYPE_EXTERNAL;
151 ie->vector = (seg << 16) | ip;
152 const char *d = "Legacy option rom";
154 d = MAKE_FLATPTR(seg, desc);
158 // Add a bcv entry for an internal harddrive
160 add_bcv_internal(struct drive_s *drive_g)
164 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
167 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
168 if (CONFIG_THREADS) {
169 // Add to bcv list with assured drive order.
170 struct ipl_entry_s *end = ie;
172 struct ipl_entry_s *prev = ie - 1;
173 if (prev < IPL.bcv || prev->type != BCV_TYPE_INTERNAL)
175 struct drive_s *prevdrive = (void*)prev->vector;
176 if (prevdrive->type < drive_g->type
177 || (prevdrive->type == drive_g->type
178 && prevdrive->cntl_id < drive_g->cntl_id))
183 memmove(ie+1, ie, (void*)end-(void*)ie);
185 ie->type = BCV_TYPE_INTERNAL;
186 ie->vector = (u32)drive_g;
187 ie->description = "";
191 /****************************************************************
192 * Boot menu and BCV execution
193 ****************************************************************/
195 // Show a generic menu item
197 menu_show_default(struct ipl_entry_s *ie, int menupos)
200 printf("%d. %s\n", menupos
201 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
205 // Show floppy menu item - but only if there exists a floppy drive.
207 menu_show_floppy(struct ipl_entry_s *ie, int menupos)
210 for (i = 0; i < Drives.floppycount; i++) {
211 struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, i);
212 printf("%d. Floppy [%s]\n", menupos + i, drive_g->desc);
214 return Drives.floppycount;
217 // Show menu items from BCV list.
219 menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
222 for (i = 0; i < IPL.bcvcount; i++) {
223 struct ipl_entry_s *ie = &IPL.bcv[i];
224 struct drive_s *drive_g = (void*)ie->vector;
226 case BCV_TYPE_INTERNAL:
227 printf("%d. %s\n", menupos + i, drive_g->desc);
230 menu_show_default(ie, menupos+i);
237 // Show cdrom menu item - but only if there exists a cdrom drive.
239 menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
241 struct drive_s *drive_g = (void*)ie->vector;
244 printf("%d. DVD/CD [%s]\n", menupos, drive_g->desc);
248 // Show coreboot-fs menu item.
250 menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
253 struct cbfs_file *file = NULL;
255 file = cbfs_findprefix("img/", file);
258 const char *filename = cbfs_filename(file);
259 printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
267 // Show IPL option menu.
269 interactive_bootmenu(void)
271 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
274 while (get_keystroke(0) >= 0)
277 printf("Press F12 for boot menu.\n\n");
280 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
281 disable_bootsplash();
282 if (scan_code != 0x86)
286 while (get_keystroke(0) >= 0)
289 printf("Select boot device:\n\n");
292 int subcount[ARRAY_SIZE(IPL.bev)];
295 for (i = 0; i < IPL.bevcount; i++) {
296 struct ipl_entry_s *ie = &IPL.bev[i];
299 case IPL_TYPE_FLOPPY:
300 sc = menu_show_floppy(ie, menupos);
302 case IPL_TYPE_HARDDISK:
303 sc = menu_show_harddisk(ie, menupos);
306 sc = menu_show_cdrom(ie, menupos);
309 sc = menu_show_cbfs(ie, menupos);
312 sc = menu_show_default(ie, menupos);
320 scan_code = get_keystroke(1000);
321 if (scan_code == 0x01)
324 if (scan_code < 1 || scan_code > menupos)
326 int choice = scan_code - 1;
328 // Find out which IPL this was for.
330 while (choice > subcount[bev]) {
331 choice -= subcount[bev];
334 IPL.bev[bev].subchoice = choice-1;
336 // Add user choice to the boot order.
337 IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
343 // Run the specified bcv.
345 run_bcv(struct ipl_entry_s *ie)
348 case BCV_TYPE_INTERNAL:
349 map_hd_drive((void*)ie->vector);
351 case BCV_TYPE_EXTERNAL:
352 call_bcv(ie->vector >> 16, ie->vector & 0xffff);
357 // Prepare for boot - show menu and run bcvs.
366 // XXX - show available drives?
368 // Allow user to modify BCV/IPL order.
369 interactive_bootmenu();
372 // Setup floppy boot order
373 int override = IPL.bev[0].subchoice;
374 struct drive_s *tmp = Drives.idmap[EXTTYPE_FLOPPY][0];
375 Drives.idmap[EXTTYPE_FLOPPY][0] = Drives.idmap[EXTTYPE_FLOPPY][override];
376 Drives.idmap[EXTTYPE_FLOPPY][override] = tmp;
379 override = IPL.bev[1].subchoice;
380 if (override < IPL.bcvcount)
381 run_bcv(&IPL.bcv[override]);
383 for (i=0; i<IPL.bcvcount; i++)
385 run_bcv(&IPL.bcv[i]);
389 /****************************************************************
390 * Boot code (int 18/19)
391 ****************************************************************/
393 // Jump to a bootup entry point.
395 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
397 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
399 memset(&br, 0, sizeof(br));
401 br.code = SEGOFF(bootseg, bootip);
402 // Set the magic number in ax and the boot drive in dl.
408 // Boot from a disk (either floppy or harddrive)
410 boot_disk(u8 bootdrv, int checksig)
412 u16 bootseg = 0x07c0;
416 memset(&br, 0, sizeof(br));
423 call16_int(0x13, &br);
425 if (br.flags & F_CF) {
426 printf("Boot failed: could not read the boot disk\n\n");
431 struct mbr_s *mbr = (void*)0;
432 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
433 printf("Boot failed: not a bootable disk\n\n");
438 /* Canonicalize bootseg:bootip */
439 u16 bootip = (bootseg & 0x0fff) << 4;
442 call_boot_entry(bootseg, bootip, bootdrv);
445 // Boot from a CD-ROM
447 boot_cdrom(struct ipl_entry_s *ie)
449 if (! CONFIG_CDROM_BOOT)
455 struct drive_s *drive_g = (void*)ie->vector;
456 int status = cdrom_boot(drive_g);
458 printf("Boot failed: Could not read from CDROM %s (code %04x)\n", drive_g->desc, status);
462 u16 ebda_seg = get_ebda_seg();
463 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
464 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
465 /* Canonicalize bootseg:bootip */
466 u16 bootip = (bootseg & 0x0fff) << 4;
469 call_boot_entry(bootseg, bootip, bootdrv);
472 // Boot from a CBFS payload
474 boot_cbfs(struct ipl_entry_s *ie)
476 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
478 int count = ie->subchoice;
479 struct cbfs_file *file = NULL;
481 file = cbfs_findprefix("img/", file);
486 cbfs_run_payload(file);
494 panic("Boot support not compiled in.\n");
496 u32 bootdev = IPL.bootorder;
497 bootdev >>= 4 * seq_nr;
500 /* Translate bootdev to an IPL table offset by subtracting 1 */
503 if (bootdev >= IPL.bevcount) {
504 printf("No bootable device.\n");
505 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
510 /* Do the loading, and set up vector as a far pointer to the boot
511 * address, and bootdrv as the boot drive */
512 struct ipl_entry_s *ie = &IPL.bev[bootdev];
514 printf("Booting from %s...\n"
515 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
518 case IPL_TYPE_FLOPPY:
519 boot_disk(0x00, IPL.checkfloppysig);
521 case IPL_TYPE_HARDDISK:
531 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
535 // Boot failed: invoke the boot recovery function
537 memset(&br, 0, sizeof(br));
539 call16_int(0x18, &br);
542 // Boot Failure recovery: try the next device.
546 debug_serial_setup();
547 debug_enter(NULL, DEBUG_HDL_18);
548 u16 ebda_seg = get_ebda_seg();
549 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
550 SET_EBDA2(ebda_seg, boot_sequence, seq);
554 // INT 19h Boot Load Service Entry Point
558 debug_serial_setup();
559 debug_enter(NULL, DEBUG_HDL_19);
560 SET_EBDA(boot_sequence, 0);