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 "config.h" // CONFIG_*
10 #include "disk.h" // cdrom_boot
11 #include "bregs.h" // struct bregs
12 #include "boot.h" // func defs
13 #include "cmos.h" // inb_cmos
14 #include "paravirt.h" // qemu_cfg_show_boot_menu
15 #include "pci.h" // pci_bdf_to_*
16 #include "usb.h" // struct usbdevice_s
19 /****************************************************************
20 * Boot priority ordering
21 ****************************************************************/
23 static char **Bootorder;
24 static int BootorderCount;
29 if (!CONFIG_BOOTORDER)
32 char *f = romfile_loadfile("bootorder", NULL);
43 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
51 dprintf(3, "boot order:\n");
58 nullTrailingSpace(Bootorder[i]);
59 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
64 // See if 'str' starts with 'glob' - if glob contains an '*' character
65 // it will match any number of characters in str that aren't a '/' or
66 // the next glob character.
68 glob_prefix(const char *glob, const char *str)
71 if (!*glob && (!*str || *str == '/'))
74 if (!*str || *str == '/' || *str == glob[1])
87 // Search the bootorder list for the given glob pattern.
89 find_prio(const char *glob)
91 dprintf(1, "Searching bootorder for: %s\n", glob);
93 for (i = 0; i < BootorderCount; i++)
94 if (glob_prefix(glob, Bootorder[i]))
99 #define FW_PCI_DOMAIN "/pci@i0cf8"
102 build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
104 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
107 p = build_pci_path(p, max, "pci-bridge", pci->parent);
110 p += snprintf(p, max, "/pci-root@%x", pci->rootbus);
111 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
114 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
115 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
117 p += snprintf(p, buf+max-p, ",%x", fn);
121 int bootprio_find_pci_device(struct pci_device *pci)
123 if (!CONFIG_BOOTORDER)
125 // Find pci device - for example: /pci@i0cf8/ethernet@5
127 build_pci_path(desc, sizeof(desc), "*", pci);
128 return find_prio(desc);
131 int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
133 if (!CONFIG_BOOTORDER)
136 // support only pci machine for now
138 // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
140 p = build_pci_path(desc, sizeof(desc), "*", pci);
141 snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%d,%d", target, lun);
142 return find_prio(desc);
145 int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
147 if (!CONFIG_BOOTORDER)
150 // support only pci machine for now
152 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
154 p = build_pci_path(desc, sizeof(desc), "*", pci);
155 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
156 return find_prio(desc);
159 int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
161 if (!CONFIG_BOOTORDER)
164 // support only pci machine for now
166 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
168 p = build_pci_path(desc, sizeof(desc), "isa", pci);
169 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
170 return find_prio(desc);
173 int bootprio_find_pci_rom(struct pci_device *pci, int instance)
175 if (!CONFIG_BOOTORDER)
177 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
179 p = build_pci_path(desc, sizeof(desc), "*", pci);
181 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
182 return find_prio(desc);
185 int bootprio_find_named_rom(const char *name, int instance)
187 if (!CONFIG_BOOTORDER)
189 // Find named rom - for example: /rom@genroms/linuxboot.bin
191 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
193 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
194 return find_prio(desc);
198 build_usb_path(char *buf, int max, struct usbhub_s *hub)
201 // Root hub - nothing to add.
203 char *p = build_usb_path(buf, max, hub->usbdev->hub);
204 p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
208 int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
210 if (!CONFIG_BOOTORDER)
212 // Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
214 p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
215 p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
216 snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%d"
217 , usbdev->port+1, lun);
218 int ret = find_prio(desc);
221 // Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
222 snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
223 return find_prio(desc);
227 /****************************************************************
229 ****************************************************************/
231 static int BootRetryTime;
232 static int CheckFloppySig = 1;
234 #define DEFAULT_PRIO 9999
236 static int DefaultFloppyPrio = 101;
237 static int DefaultCDPrio = 102;
238 static int DefaultHDPrio = 103;
239 static int DefaultBEVPrio = 104;
247 if (!CONFIG_COREBOOT) {
248 // On emulators, get boot order from nvram.
249 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
251 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
252 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
253 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
254 = DefaultBEVPrio = DEFAULT_PRIO;
256 for (i=101; i<104; i++) {
257 u32 val = bootorder & 0x0f;
260 case 1: DefaultFloppyPrio = i; break;
261 case 2: DefaultHDPrio = i; break;
262 case 3: DefaultCDPrio = i; break;
263 case 4: DefaultBEVPrio = i; break;
268 BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000);
274 /****************************************************************
276 ****************************************************************/
282 struct segoff_s vector;
283 struct drive_s *drive;
286 const char *description;
287 struct bootentry_s *next;
289 static struct bootentry_s *BootList;
291 #define IPL_TYPE_FLOPPY 0x01
292 #define IPL_TYPE_HARDDISK 0x02
293 #define IPL_TYPE_CDROM 0x03
294 #define IPL_TYPE_CBFS 0x20
295 #define IPL_TYPE_BEV 0x80
296 #define IPL_TYPE_BCV 0x81
297 #define IPL_TYPE_HALT 0xf0
300 bootentry_add(int type, int prio, u32 data, const char *desc)
304 struct bootentry_s *be = malloc_tmp(sizeof(*be));
312 be->description = desc ?: "?";
313 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
314 , be->description, type, prio, data);
316 // Add entry in sorted order.
317 struct bootentry_s **pprev;
318 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
319 struct bootentry_s *pos = *pprev;
320 if (be->priority < pos->priority)
322 if (be->priority > pos->priority)
324 if (be->type < pos->type)
326 if (be->type > pos->type)
328 if (be->type <= IPL_TYPE_CDROM
329 && (be->drive->type < pos->drive->type
330 || (be->drive->type == pos->drive->type
331 && be->drive->cntl_id < pos->drive->cntl_id)))
338 // Return the given priority if it's set - defaultprio otherwise.
339 static inline int defPrio(int priority, int defaultprio) {
340 return (priority < 0) ? defaultprio : priority;
343 // Add a BEV vector for a given pnp compatible option rom.
345 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
347 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
348 , SEGOFF(seg, bev).segoff
349 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
350 DefaultBEVPrio = DEFAULT_PRIO;
353 // Add a bcv entry for an expansion card harddrive or legacy option rom
355 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
357 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
358 , SEGOFF(seg, ip).segoff
359 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
363 boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
365 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
366 , (u32)drive_g, desc);
370 boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
372 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
373 , (u32)drive_g, desc);
377 boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
379 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
380 , (u32)drive_g, desc);
383 // Add a CBFS payload entry
385 boot_add_cbfs(void *data, const char *desc, int prio)
387 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
391 /****************************************************************
392 * Boot menu and BCV execution
393 ****************************************************************/
395 #define DEFAULT_BOOTMENU_WAIT 2500
397 // Show IPL option menu.
399 interactive_bootmenu(void)
401 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
404 while (get_keystroke(0) >= 0)
407 printf("\nPress F12 for boot menu.\n\n");
409 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
411 int scan_code = get_keystroke(menutime);
412 disable_bootsplash();
413 if (scan_code != 0x86)
417 while (get_keystroke(0) >= 0)
420 printf("Select boot device:\n\n");
424 struct bootentry_s *pos = BootList;
429 printf("%d. %s\n", maxmenu
430 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
436 scan_code = get_keystroke(1000);
437 if (scan_code >= 1 && scan_code <= maxmenu+1)
441 if (scan_code == 0x01)
445 // Find entry and make top priority.
446 int choice = scan_code - 1;
447 struct bootentry_s **pprev = &BootList;
449 pprev = &(*pprev)->next;
452 pos->next = BootList;
457 // BEV (Boot Execution Vector) list
462 static struct bev_s BEV[20];
464 static int HaveHDBoot, HaveFDBoot;
467 add_bev(int type, u32 vector)
469 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
471 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
473 if (BEVCount >= ARRAY_SIZE(BEV))
475 struct bev_s *bev = &BEV[BEVCount++];
477 bev->vector = vector;
480 // Prepare for boot - show menu and run bcvs.
489 // XXX - show available drives?
491 // Allow user to modify BCV/IPL order.
492 interactive_bootmenu();
495 int haltprio = find_prio("HALT");
497 bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
499 // Map drives and populate BEV list
500 struct bootentry_s *pos = BootList;
504 call_bcv(pos->vector.seg, pos->vector.offset);
505 add_bev(IPL_TYPE_HARDDISK, 0);
507 case IPL_TYPE_FLOPPY:
508 map_floppy_drive(pos->drive);
509 add_bev(IPL_TYPE_FLOPPY, 0);
511 case IPL_TYPE_HARDDISK:
512 map_hd_drive(pos->drive);
513 add_bev(IPL_TYPE_HARDDISK, 0);
516 map_cd_drive(pos->drive);
519 add_bev(pos->type, pos->data);
525 // If nothing added a floppy/hd boot - add it manually.
526 add_bev(IPL_TYPE_FLOPPY, 0);
527 add_bev(IPL_TYPE_HARDDISK, 0);
531 /****************************************************************
532 * Boot code (int 18/19)
533 ****************************************************************/
535 // Jump to a bootup entry point.
537 call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
539 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
541 memset(&br, 0, sizeof(br));
544 // Set the magic number in ax and the boot drive in dl.
550 // Boot from a disk (either floppy or harddrive)
552 boot_disk(u8 bootdrv, int checksig)
554 u16 bootseg = 0x07c0;
558 memset(&br, 0, sizeof(br));
565 call16_int(0x13, &br);
567 if (br.flags & F_CF) {
568 printf("Boot failed: could not read the boot disk\n\n");
573 struct mbr_s *mbr = (void*)0;
574 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
575 printf("Boot failed: not a bootable disk\n\n");
580 /* Canonicalize bootseg:bootip */
581 u16 bootip = (bootseg & 0x0fff) << 4;
584 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
587 // Boot from a CD-ROM
589 boot_cdrom(struct drive_s *drive_g)
591 if (! CONFIG_CDROM_BOOT)
593 printf("Booting from DVD/CD...\n");
595 int status = cdrom_boot(drive_g);
597 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
601 u8 bootdrv = CDEmu.emulated_extdrive;
602 u16 bootseg = CDEmu.load_segment;
603 /* Canonicalize bootseg:bootip */
604 u16 bootip = (bootseg & 0x0fff) << 4;
607 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
610 // Boot from a CBFS payload
612 boot_cbfs(struct cbfs_file *file)
614 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
616 printf("Booting from CBFS...\n");
617 cbfs_run_payload(file);
620 // Boot from a BEV entry on an optionrom.
624 printf("Booting from ROM...\n");
627 call_boot_entry(so, 0);
630 // Unable to find bootable device - warn user and eventually retry.
634 if (BootRetryTime == (u32)-1)
635 printf("No bootable device.\n");
637 printf("No bootable device. Retrying in %d seconds.\n"
638 , BootRetryTime/1000);
639 // Wait for 'BootRetryTime' milliseconds and then reboot.
640 u32 end = calc_future_timer(BootRetryTime);
642 if (BootRetryTime != (u32)-1 && check_timer(end))
646 printf("Rebooting.\n");
648 memset(&br, 0, sizeof(br));
649 br.code = SEGOFF(SEG_BIOS, (u32)reset_vector);
653 // Determine next boot method and attempt a boot using it.
658 panic("Boot support not compiled in.\n");
660 if (seq_nr >= BEVCount)
663 // Boot the given BEV type.
664 struct bev_s *ie = &BEV[seq_nr];
666 case IPL_TYPE_FLOPPY:
667 printf("Booting from Floppy...\n");
668 boot_disk(0x00, CheckFloppySig);
670 case IPL_TYPE_HARDDISK:
671 printf("Booting from Hard Disk...\n");
675 boot_cdrom((void*)ie->vector);
678 boot_cbfs((void*)ie->vector);
681 boot_rom(ie->vector);
688 // Boot failed: invoke the boot recovery function
690 memset(&br, 0, sizeof(br));
692 call16_int(0x18, &br);
695 int BootSequence VARLOW = -1;
697 // Boot Failure recovery: try the next device.
701 debug_serial_setup();
702 debug_enter(NULL, DEBUG_HDL_18);
703 int seq = BootSequence + 1;
708 // INT 19h Boot Load Service Entry Point
712 debug_serial_setup();
713 debug_enter(NULL, DEBUG_HDL_19);