1 // Code to load disk image and start system boot.
3 // Copyright (C) 2008-2013 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 "block.h" // struct drive_s
9 #include "bregs.h" // struct bregs
10 #include "config.h" // CONFIG_*
11 #include "fw/paravirt.h" // qemu_cfg_show_boot_menu
12 #include "hw/pci.h" // pci_bdf_to_*
13 #include "hw/rtc.h" // rtc_read
14 #include "hw/usb.h" // struct usbdevice_s
15 #include "list.h" // hlist_node
16 #include "malloc.h" // free
17 #include "output.h" // dprintf
18 #include "romfile.h" // romfile_loadint
19 #include "std/disk.h" // struct mbr_s
20 #include "string.h" // memset
21 #include "util.h" // irqtimer_calc
22 #include "tcgbios.h" // tpm_*
25 /****************************************************************
26 * Boot priority ordering
27 ****************************************************************/
29 static char **Bootorder VARVERIFY32INIT;
30 static int BootorderCount;
35 if (!CONFIG_BOOTORDER)
38 char *f = romfile_loadfile("bootorder", NULL);
49 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
57 dprintf(1, "boot order:\n");
64 Bootorder[i] = nullTrailingSpace(Bootorder[i]);
65 dprintf(1, "%d: %s\n", i+1, Bootorder[i]);
70 // See if 'str' starts with 'glob' - if glob contains an '*' character
71 // it will match any number of characters in str that aren't a '/' or
72 // the next glob character.
74 glob_prefix(const char *glob, const char *str)
77 if (!*glob && (!*str || *str == '/'))
80 if (!*str || *str == '/' || *str == glob[1])
93 // Search the bootorder list for the given glob pattern.
95 find_prio(const char *glob)
97 dprintf(1, "Searching bootorder for: %s\n", glob);
99 for (i = 0; i < BootorderCount; i++)
100 if (glob_prefix(glob, Bootorder[i]))
105 #define FW_PCI_DOMAIN "/pci@i0cf8"
108 build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
110 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
113 p = build_pci_path(p, max, "pci-bridge", pci->parent);
115 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
117 p += snprintf(p, buf+max-p, ",%x", pci->rootbus);
120 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
121 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
123 p += snprintf(p, buf+max-p, ",%x", fn);
127 int bootprio_find_pci_device(struct pci_device *pci)
130 return csm_bootprio_pci(pci);
131 if (!CONFIG_BOOTORDER)
133 // Find pci device - for example: /pci@i0cf8/ethernet@5
135 build_pci_path(desc, sizeof(desc), "*", pci);
136 return find_prio(desc);
139 int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
141 if (!CONFIG_BOOTORDER)
144 // support only pci machine for now
146 // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
148 p = build_pci_path(desc, sizeof(desc), "*", pci);
149 snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%x,%x", target, lun);
150 return find_prio(desc);
153 int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
156 return csm_bootprio_ata(pci, chanid, slave);
157 if (!CONFIG_BOOTORDER)
160 // support only pci machine for now
162 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
164 p = build_pci_path(desc, sizeof(desc), "*", pci);
165 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
166 return find_prio(desc);
169 int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
172 return csm_bootprio_fdc(pci, port, fdid);
173 if (!CONFIG_BOOTORDER)
176 // support only pci machine for now
178 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
180 p = build_pci_path(desc, sizeof(desc), "isa", pci);
181 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
182 return find_prio(desc);
185 int bootprio_find_pci_rom(struct pci_device *pci, int instance)
187 if (!CONFIG_BOOTORDER)
189 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
191 p = build_pci_path(desc, sizeof(desc), "*", pci);
193 snprintf(p, desc+sizeof(desc)-p, ":rom%x", instance);
194 return find_prio(desc);
197 int bootprio_find_named_rom(const char *name, int instance)
199 if (!CONFIG_BOOTORDER)
201 // Find named rom - for example: /rom@genroms/linuxboot.bin
203 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
205 snprintf(p, desc+sizeof(desc)-p, ":rom%x", instance);
206 return find_prio(desc);
210 build_usb_path(char *buf, int max, struct usbhub_s *hub)
213 // Root hub - nothing to add.
215 char *p = build_usb_path(buf, max, hub->usbdev->hub);
216 p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
220 int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
222 if (!CONFIG_BOOTORDER)
224 // Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
226 p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
227 p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
228 snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%x"
229 , usbdev->port+1, lun);
230 int ret = find_prio(desc);
233 // Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
234 snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
235 return find_prio(desc);
239 /****************************************************************
241 ****************************************************************/
243 static int BootRetryTime;
244 static int CheckFloppySig = 1;
246 #define DEFAULT_PRIO 9999
248 static int DefaultFloppyPrio = 101;
249 static int DefaultCDPrio = 102;
250 static int DefaultHDPrio = 103;
251 static int DefaultBEVPrio = 104;
260 // On emulators, get boot order from nvram.
261 if (rtc_read(CMOS_BIOS_BOOTFLAG1) & 1)
263 u32 bootorder = (rtc_read(CMOS_BIOS_BOOTFLAG2)
264 | ((rtc_read(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
265 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
266 = DefaultBEVPrio = DEFAULT_PRIO;
268 for (i=101; i<104; i++) {
269 u32 val = bootorder & 0x0f;
272 case 1: DefaultFloppyPrio = i; break;
273 case 2: DefaultHDPrio = i; break;
274 case 3: DefaultCDPrio = i; break;
275 case 4: DefaultBEVPrio = i; break;
280 BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000);
286 /****************************************************************
288 ****************************************************************/
294 struct segoff_s vector;
295 struct drive_s *drive;
298 const char *description;
299 struct hlist_node node;
301 static struct hlist_head BootList VARVERIFY32INIT;
303 #define IPL_TYPE_FLOPPY 0x01
304 #define IPL_TYPE_HARDDISK 0x02
305 #define IPL_TYPE_CDROM 0x03
306 #define IPL_TYPE_CBFS 0x20
307 #define IPL_TYPE_BEV 0x80
308 #define IPL_TYPE_BCV 0x81
309 #define IPL_TYPE_HALT 0xf0
312 bootentry_add(int type, int prio, u32 data, const char *desc)
316 struct bootentry_s *be = malloc_tmp(sizeof(*be));
324 be->description = desc ?: "?";
325 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
326 , be->description, type, prio, data);
328 // Add entry in sorted order.
329 struct hlist_node **pprev;
330 struct bootentry_s *pos;
331 hlist_for_each_entry_pprev(pos, pprev, &BootList, node) {
332 if (be->priority < pos->priority)
334 if (be->priority > pos->priority)
336 if (be->type < pos->type)
338 if (be->type > pos->type)
340 if (be->type <= IPL_TYPE_CDROM
341 && (be->drive->type < pos->drive->type
342 || (be->drive->type == pos->drive->type
343 && be->drive->cntl_id < pos->drive->cntl_id)))
346 hlist_add(&be->node, pprev);
349 // Return the given priority if it's set - defaultprio otherwise.
350 static inline int defPrio(int priority, int defaultprio) {
351 return (priority < 0) ? defaultprio : priority;
354 // Add a BEV vector for a given pnp compatible option rom.
356 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
358 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
359 , SEGOFF(seg, bev).segoff
360 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
361 DefaultBEVPrio = DEFAULT_PRIO;
364 // Add a bcv entry for an expansion card harddrive or legacy option rom
366 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
368 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
369 , SEGOFF(seg, ip).segoff
370 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
374 boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
376 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
377 , (u32)drive_g, desc);
381 boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
383 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
384 , (u32)drive_g, desc);
388 boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
390 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
391 , (u32)drive_g, desc);
394 // Add a CBFS payload entry
396 boot_add_cbfs(void *data, const char *desc, int prio)
398 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
402 /****************************************************************
404 ****************************************************************/
406 // See if a keystroke is pending in the keyboard buffer.
408 check_for_keystroke(void)
411 memset(&br, 0, sizeof(br));
412 br.flags = F_IF|F_ZF;
414 call16_int(0x16, &br);
415 return !(br.flags & F_ZF);
418 // Return a keystroke - waiting forever if necessary.
420 get_raw_keystroke(void)
423 memset(&br, 0, sizeof(br));
425 call16_int(0x16, &br);
429 // Read a keystroke - waiting up to 'msec' milliseconds.
431 get_keystroke(int msec)
433 u32 end = irqtimer_calc(msec);
435 if (check_for_keystroke())
436 return get_raw_keystroke();
437 if (irqtimer_check(end))
444 /****************************************************************
445 * Boot menu and BCV execution
446 ****************************************************************/
448 #define DEFAULT_BOOTMENU_WAIT 2500
450 // Show IPL option menu.
452 interactive_bootmenu(void)
454 // XXX - show available drives?
456 if (! CONFIG_BOOTMENU || !romfile_loadint("etc/show-boot-menu", 1))
459 while (get_keystroke(0) >= 0)
462 char *bootmsg = romfile_loadfile("etc/boot-menu-message", NULL);
463 int menukey = romfile_loadint("etc/boot-menu-key", 1);
464 printf("%s", bootmsg ?: "\nPress ESC for boot menu.\n\n");
467 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
469 int scan_code = get_keystroke(menutime);
470 disable_bootsplash();
471 if (scan_code != menukey)
474 while (get_keystroke(0) >= 0)
477 printf("Select boot device:\n\n");
482 struct bootentry_s *pos;
483 hlist_for_each_entry(pos, &BootList, node) {
486 printf("%d. %s\n", maxmenu
487 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
490 // Get key press. If the menu key is ESC, do not restart boot unless
491 // 1.5 seconds have passed. Otherwise users (trained by years of
492 // repeatedly hitting keys to enter the BIOS) will end up hitting ESC
493 // multiple times and immediately booting the primary boot device.
494 int esc_accepted_time = irqtimer_calc(menukey == 1 ? 1500 : 0);
496 scan_code = get_keystroke(1000);
497 if (scan_code == 1 && !irqtimer_check(esc_accepted_time))
499 if (scan_code >= 1 && scan_code <= maxmenu+1)
503 if (scan_code == 0x01)
507 // Find entry and make top priority.
508 int choice = scan_code - 1;
509 hlist_for_each_entry(pos, &BootList, node) {
513 hlist_del(&pos->node);
515 hlist_add_head(&pos->node, &BootList);
518 // BEV (Boot Execution Vector) list
523 static struct bev_s BEV[20];
525 static int HaveHDBoot, HaveFDBoot;
528 add_bev(int type, u32 vector)
530 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
532 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
534 if (BEVCount >= ARRAY_SIZE(BEV))
536 struct bev_s *bev = &BEV[BEVCount++];
538 bev->vector = vector;
541 // Prepare for boot - show menu and run bcvs.
548 int haltprio = find_prio("HALT");
550 bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
552 // Map drives and populate BEV list
553 struct bootentry_s *pos;
554 hlist_for_each_entry(pos, &BootList, node) {
557 call_bcv(pos->vector.seg, pos->vector.offset);
558 add_bev(IPL_TYPE_HARDDISK, 0);
560 case IPL_TYPE_FLOPPY:
561 map_floppy_drive(pos->drive);
562 add_bev(IPL_TYPE_FLOPPY, 0);
564 case IPL_TYPE_HARDDISK:
565 map_hd_drive(pos->drive);
566 add_bev(IPL_TYPE_HARDDISK, 0);
569 map_cd_drive(pos->drive);
572 add_bev(pos->type, pos->data);
577 // If nothing added a floppy/hd boot - add it manually.
578 add_bev(IPL_TYPE_FLOPPY, 0);
579 add_bev(IPL_TYPE_HARDDISK, 0);
583 /****************************************************************
584 * Boot code (int 18/19)
585 ****************************************************************/
587 // Jump to a bootup entry point.
589 call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
591 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
593 memset(&br, 0, sizeof(br));
596 // Set the magic number in ax and the boot drive in dl.
602 // Boot from a disk (either floppy or harddrive)
604 boot_disk(u8 bootdrv, int checksig)
606 u16 bootseg = 0x07c0;
610 memset(&br, 0, sizeof(br));
617 call16_int(0x13, &br);
619 if (br.flags & F_CF) {
620 printf("Boot failed: could not read the boot disk\n\n");
625 struct mbr_s *mbr = (void*)0;
626 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
627 printf("Boot failed: not a bootable disk\n\n");
632 tpm_add_bcv(bootdrv, MAKE_FLATPTR(bootseg, 0), 512);
634 /* Canonicalize bootseg:bootip */
635 u16 bootip = (bootseg & 0x0fff) << 4;
638 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
641 // Boot from a CD-ROM
643 boot_cdrom(struct drive_s *drive_g)
645 if (! CONFIG_CDROM_BOOT)
647 printf("Booting from DVD/CD...\n");
649 int status = cdrom_boot(drive_g);
651 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
655 u8 bootdrv = CDEmu.emulated_drive;
656 u16 bootseg = CDEmu.load_segment;
658 tpm_add_cdrom(bootdrv, MAKE_FLATPTR(bootseg, 0), 512);
660 /* Canonicalize bootseg:bootip */
661 u16 bootip = (bootseg & 0x0fff) << 4;
664 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
667 // Boot from a CBFS payload
669 boot_cbfs(struct cbfs_file *file)
671 if (!CONFIG_COREBOOT_FLASH)
673 printf("Booting from CBFS...\n");
674 cbfs_run_payload(file);
677 // Boot from a BEV entry on an optionrom.
681 printf("Booting from ROM...\n");
684 call_boot_entry(so, 0);
687 // Unable to find bootable device - warn user and eventually retry.
691 if (BootRetryTime == (u32)-1)
692 printf("No bootable device.\n");
694 printf("No bootable device. Retrying in %d seconds.\n"
695 , BootRetryTime/1000);
696 // Wait for 'BootRetryTime' milliseconds and then reboot.
697 u32 end = irqtimer_calc(BootRetryTime);
699 if (BootRetryTime != (u32)-1 && irqtimer_check(end))
703 printf("Rebooting.\n");
707 // Determine next boot method and attempt a boot using it.
712 panic("Boot support not compiled in.\n");
714 if (seq_nr >= BEVCount)
717 // Boot the given BEV type.
718 struct bev_s *ie = &BEV[seq_nr];
720 case IPL_TYPE_FLOPPY:
721 printf("Booting from Floppy...\n");
722 boot_disk(0x00, CheckFloppySig);
724 case IPL_TYPE_HARDDISK:
725 printf("Booting from Hard Disk...\n");
729 boot_cdrom((void*)ie->vector);
732 boot_cbfs((void*)ie->vector);
735 boot_rom(ie->vector);
742 // Boot failed: invoke the boot recovery function
744 memset(&br, 0, sizeof(br));
746 call16_int(0x18, &br);
749 int BootSequence VARLOW = -1;
751 // Boot Failure recovery: try the next device.
755 debug_enter(NULL, DEBUG_HDL_18);
756 int seq = BootSequence + 1;
761 // INT 19h Boot Load Service Entry Point
765 debug_enter(NULL, DEBUG_HDL_19);