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
24 /****************************************************************
25 * Boot priority ordering
26 ****************************************************************/
28 static char **Bootorder VARVERIFY32INIT;
29 static int BootorderCount;
34 if (!CONFIG_BOOTORDER)
37 char *f = romfile_loadfile("bootorder", NULL);
48 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
56 dprintf(1, "boot order:\n");
63 Bootorder[i] = nullTrailingSpace(Bootorder[i]);
64 dprintf(1, "%d: %s\n", i+1, Bootorder[i]);
69 // See if 'str' starts with 'glob' - if glob contains an '*' character
70 // it will match any number of characters in str that aren't a '/' or
71 // the next glob character.
73 glob_prefix(const char *glob, const char *str)
76 if (!*glob && (!*str || *str == '/'))
79 if (!*str || *str == '/' || *str == glob[1])
92 // Search the bootorder list for the given glob pattern.
94 find_prio(const char *glob)
96 dprintf(1, "Searching bootorder for: %s\n", glob);
98 for (i = 0; i < BootorderCount; i++)
99 if (glob_prefix(glob, Bootorder[i]))
104 #define FW_PCI_DOMAIN "/pci@i0cf8"
107 build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
109 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
112 p = build_pci_path(p, max, "pci-bridge", pci->parent);
115 p += snprintf(p, max, "/pci-root@%x", pci->rootbus);
116 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
119 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
120 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
122 p += snprintf(p, buf+max-p, ",%x", fn);
126 int bootprio_find_pci_device(struct pci_device *pci)
129 return csm_bootprio_pci(pci);
130 if (!CONFIG_BOOTORDER)
132 // Find pci device - for example: /pci@i0cf8/ethernet@5
134 build_pci_path(desc, sizeof(desc), "*", pci);
135 return find_prio(desc);
138 int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
140 if (!CONFIG_BOOTORDER)
143 // support only pci machine for now
145 // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
147 p = build_pci_path(desc, sizeof(desc), "*", pci);
148 snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%x,%x", target, lun);
149 return find_prio(desc);
152 int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
155 return csm_bootprio_ata(pci, chanid, slave);
156 if (!CONFIG_BOOTORDER)
159 // support only pci machine for now
161 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
163 p = build_pci_path(desc, sizeof(desc), "*", pci);
164 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
165 return find_prio(desc);
168 int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
171 return csm_bootprio_fdc(pci, port, fdid);
172 if (!CONFIG_BOOTORDER)
175 // support only pci machine for now
177 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
179 p = build_pci_path(desc, sizeof(desc), "isa", pci);
180 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
181 return find_prio(desc);
184 int bootprio_find_pci_rom(struct pci_device *pci, int instance)
186 if (!CONFIG_BOOTORDER)
188 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
190 p = build_pci_path(desc, sizeof(desc), "*", pci);
192 snprintf(p, desc+sizeof(desc)-p, ":rom%x", instance);
193 return find_prio(desc);
196 int bootprio_find_named_rom(const char *name, int instance)
198 if (!CONFIG_BOOTORDER)
200 // Find named rom - for example: /rom@genroms/linuxboot.bin
202 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
204 snprintf(p, desc+sizeof(desc)-p, ":rom%x", instance);
205 return find_prio(desc);
209 build_usb_path(char *buf, int max, struct usbhub_s *hub)
212 // Root hub - nothing to add.
214 char *p = build_usb_path(buf, max, hub->usbdev->hub);
215 p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
219 int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
221 if (!CONFIG_BOOTORDER)
223 // Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
225 p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
226 p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
227 snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%x"
228 , usbdev->port+1, lun);
229 int ret = find_prio(desc);
232 // Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
233 snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
234 return find_prio(desc);
238 /****************************************************************
240 ****************************************************************/
242 static int BootRetryTime;
243 static int CheckFloppySig = 1;
245 #define DEFAULT_PRIO 9999
247 static int DefaultFloppyPrio = 101;
248 static int DefaultCDPrio = 102;
249 static int DefaultHDPrio = 103;
250 static int DefaultBEVPrio = 104;
259 // On emulators, get boot order from nvram.
260 if (rtc_read(CMOS_BIOS_BOOTFLAG1) & 1)
262 u32 bootorder = (rtc_read(CMOS_BIOS_BOOTFLAG2)
263 | ((rtc_read(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
264 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
265 = DefaultBEVPrio = DEFAULT_PRIO;
267 for (i=101; i<104; i++) {
268 u32 val = bootorder & 0x0f;
271 case 1: DefaultFloppyPrio = i; break;
272 case 2: DefaultHDPrio = i; break;
273 case 3: DefaultCDPrio = i; break;
274 case 4: DefaultBEVPrio = i; break;
279 BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000);
285 /****************************************************************
287 ****************************************************************/
293 struct segoff_s vector;
294 struct drive_s *drive;
297 const char *description;
298 struct hlist_node node;
300 static struct hlist_head BootList VARVERIFY32INIT;
302 #define IPL_TYPE_FLOPPY 0x01
303 #define IPL_TYPE_HARDDISK 0x02
304 #define IPL_TYPE_CDROM 0x03
305 #define IPL_TYPE_CBFS 0x20
306 #define IPL_TYPE_BEV 0x80
307 #define IPL_TYPE_BCV 0x81
308 #define IPL_TYPE_HALT 0xf0
311 bootentry_add(int type, int prio, u32 data, const char *desc)
315 struct bootentry_s *be = malloc_tmp(sizeof(*be));
323 be->description = desc ?: "?";
324 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
325 , be->description, type, prio, data);
327 // Add entry in sorted order.
328 struct hlist_node **pprev;
329 struct bootentry_s *pos;
330 hlist_for_each_entry_pprev(pos, pprev, &BootList, node) {
331 if (be->priority < pos->priority)
333 if (be->priority > pos->priority)
335 if (be->type < pos->type)
337 if (be->type > pos->type)
339 if (be->type <= IPL_TYPE_CDROM
340 && (be->drive->type < pos->drive->type
341 || (be->drive->type == pos->drive->type
342 && be->drive->cntl_id < pos->drive->cntl_id)))
345 hlist_add(&be->node, pprev);
348 // Return the given priority if it's set - defaultprio otherwise.
349 static inline int defPrio(int priority, int defaultprio) {
350 return (priority < 0) ? defaultprio : priority;
353 // Add a BEV vector for a given pnp compatible option rom.
355 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
357 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
358 , SEGOFF(seg, bev).segoff
359 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
360 DefaultBEVPrio = DEFAULT_PRIO;
363 // Add a bcv entry for an expansion card harddrive or legacy option rom
365 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
367 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
368 , SEGOFF(seg, ip).segoff
369 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
373 boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
375 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
376 , (u32)drive_g, desc);
380 boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
382 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
383 , (u32)drive_g, desc);
387 boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
389 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
390 , (u32)drive_g, desc);
393 // Add a CBFS payload entry
395 boot_add_cbfs(void *data, const char *desc, int prio)
397 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
401 /****************************************************************
403 ****************************************************************/
405 // See if a keystroke is pending in the keyboard buffer.
407 check_for_keystroke(void)
410 memset(&br, 0, sizeof(br));
411 br.flags = F_IF|F_ZF;
413 call16_int(0x16, &br);
414 return !(br.flags & F_ZF);
417 // Return a keystroke - waiting forever if necessary.
419 get_raw_keystroke(void)
422 memset(&br, 0, sizeof(br));
424 call16_int(0x16, &br);
428 // Read a keystroke - waiting up to 'msec' milliseconds.
430 get_keystroke(int msec)
432 u32 end = irqtimer_calc(msec);
434 if (check_for_keystroke())
435 return get_raw_keystroke();
436 if (irqtimer_check(end))
443 /****************************************************************
444 * Boot menu and BCV execution
445 ****************************************************************/
447 #define DEFAULT_BOOTMENU_WAIT 2500
449 // Show IPL option menu.
451 interactive_bootmenu(void)
453 // XXX - show available drives?
455 if (! CONFIG_BOOTMENU || !romfile_loadint("etc/show-boot-menu", 1))
458 while (get_keystroke(0) >= 0)
461 char *bootmsg = romfile_loadfile("etc/boot-menu-message", NULL);
462 int menukey = romfile_loadint("etc/boot-menu-key", 0x86);
463 printf("%s", bootmsg ?: "\nPress F12 for boot menu.\n\n");
466 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
468 int scan_code = get_keystroke(menutime);
469 disable_bootsplash();
470 if (scan_code != menukey)
473 while (get_keystroke(0) >= 0)
476 printf("Select boot device:\n\n");
481 struct bootentry_s *pos;
482 hlist_for_each_entry(pos, &BootList, node) {
485 printf("%d. %s\n", maxmenu
486 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
491 scan_code = get_keystroke(1000);
492 if (scan_code >= 1 && scan_code <= maxmenu+1)
496 if (scan_code == 0x01)
500 // Find entry and make top priority.
501 int choice = scan_code - 1;
502 hlist_for_each_entry(pos, &BootList, node) {
506 hlist_del(&pos->node);
508 hlist_add_head(&pos->node, &BootList);
511 // BEV (Boot Execution Vector) list
516 static struct bev_s BEV[20];
518 static int HaveHDBoot, HaveFDBoot;
521 add_bev(int type, u32 vector)
523 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
525 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
527 if (BEVCount >= ARRAY_SIZE(BEV))
529 struct bev_s *bev = &BEV[BEVCount++];
531 bev->vector = vector;
534 // Prepare for boot - show menu and run bcvs.
541 int haltprio = find_prio("HALT");
543 bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
545 // Map drives and populate BEV list
546 struct bootentry_s *pos;
547 hlist_for_each_entry(pos, &BootList, node) {
550 call_bcv(pos->vector.seg, pos->vector.offset);
551 add_bev(IPL_TYPE_HARDDISK, 0);
553 case IPL_TYPE_FLOPPY:
554 map_floppy_drive(pos->drive);
555 add_bev(IPL_TYPE_FLOPPY, 0);
557 case IPL_TYPE_HARDDISK:
558 map_hd_drive(pos->drive);
559 add_bev(IPL_TYPE_HARDDISK, 0);
562 map_cd_drive(pos->drive);
565 add_bev(pos->type, pos->data);
570 // If nothing added a floppy/hd boot - add it manually.
571 add_bev(IPL_TYPE_FLOPPY, 0);
572 add_bev(IPL_TYPE_HARDDISK, 0);
576 /****************************************************************
577 * Boot code (int 18/19)
578 ****************************************************************/
580 // Jump to a bootup entry point.
582 call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
584 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
586 memset(&br, 0, sizeof(br));
589 // Set the magic number in ax and the boot drive in dl.
595 // Boot from a disk (either floppy or harddrive)
597 boot_disk(u8 bootdrv, int checksig)
599 u16 bootseg = 0x07c0;
603 memset(&br, 0, sizeof(br));
610 call16_int(0x13, &br);
612 if (br.flags & F_CF) {
613 printf("Boot failed: could not read the boot disk\n\n");
618 struct mbr_s *mbr = (void*)0;
619 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
620 printf("Boot failed: not a bootable disk\n\n");
625 /* Canonicalize bootseg:bootip */
626 u16 bootip = (bootseg & 0x0fff) << 4;
629 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
632 // Boot from a CD-ROM
634 boot_cdrom(struct drive_s *drive_g)
636 if (! CONFIG_CDROM_BOOT)
638 printf("Booting from DVD/CD...\n");
640 int status = cdrom_boot(drive_g);
642 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
646 u8 bootdrv = CDEmu.emulated_drive;
647 u16 bootseg = CDEmu.load_segment;
648 /* Canonicalize bootseg:bootip */
649 u16 bootip = (bootseg & 0x0fff) << 4;
652 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
655 // Boot from a CBFS payload
657 boot_cbfs(struct cbfs_file *file)
659 if (!CONFIG_COREBOOT_FLASH)
661 printf("Booting from CBFS...\n");
662 cbfs_run_payload(file);
665 // Boot from a BEV entry on an optionrom.
669 printf("Booting from ROM...\n");
672 call_boot_entry(so, 0);
675 // Unable to find bootable device - warn user and eventually retry.
679 if (BootRetryTime == (u32)-1)
680 printf("No bootable device.\n");
682 printf("No bootable device. Retrying in %d seconds.\n"
683 , BootRetryTime/1000);
684 // Wait for 'BootRetryTime' milliseconds and then reboot.
685 u32 end = irqtimer_calc(BootRetryTime);
687 if (BootRetryTime != (u32)-1 && irqtimer_check(end))
691 printf("Rebooting.\n");
695 // Determine next boot method and attempt a boot using it.
700 panic("Boot support not compiled in.\n");
702 if (seq_nr >= BEVCount)
705 // Boot the given BEV type.
706 struct bev_s *ie = &BEV[seq_nr];
708 case IPL_TYPE_FLOPPY:
709 printf("Booting from Floppy...\n");
710 boot_disk(0x00, CheckFloppySig);
712 case IPL_TYPE_HARDDISK:
713 printf("Booting from Hard Disk...\n");
717 boot_cdrom((void*)ie->vector);
720 boot_cbfs((void*)ie->vector);
723 boot_rom(ie->vector);
730 // Boot failed: invoke the boot recovery function
732 memset(&br, 0, sizeof(br));
734 call16_int(0x18, &br);
737 int BootSequence VARLOW = -1;
739 // Boot Failure recovery: try the next device.
743 debug_enter(NULL, DEBUG_HDL_18);
744 int seq = BootSequence + 1;
749 // INT 19h Boot Load Service Entry Point
753 debug_enter(NULL, DEBUG_HDL_19);