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" // func defs
14 #include "cmos.h" // inb_cmos
15 #include "paravirt.h" // romfile_loadfile
16 #include "pci.h" //pci_bdf_to_*
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, int bdf)
104 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
106 int parent = pci_bdf_to_bus(bdf);
108 parent = PCIpaths[parent];
109 int parentdev = parent & 0xffff;
110 if (parent & PP_PCIBRIDGE) {
111 p = build_pci_path(p, max, "pci-bridge", parentdev);
114 p += snprintf(p, max, "/pci-root@%x", parentdev);
115 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
118 int dev = pci_bdf_to_dev(bdf), fn = pci_bdf_to_fn(bdf);
119 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
121 p += snprintf(p, buf+max-p, ",%x", fn);
125 int bootprio_find_pci_device(int bdf)
127 if (!CONFIG_BOOTORDER)
129 // Find pci device - for example: /pci@i0cf8/ethernet@5
131 build_pci_path(desc, sizeof(desc), "*", bdf);
132 return find_prio(desc);
135 int bootprio_find_ata_device(int bdf, int chanid, int slave)
137 if (!CONFIG_BOOTORDER)
140 // support only pci machine for now
142 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
144 p = build_pci_path(desc, sizeof(desc), "*", bdf);
145 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
146 return find_prio(desc);
149 int bootprio_find_fdc_device(int bdf, int port, int fdid)
151 if (!CONFIG_BOOTORDER)
154 // support only pci machine for now
156 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
158 p = build_pci_path(desc, sizeof(desc), "isa", bdf);
159 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
160 return find_prio(desc);
163 int bootprio_find_pci_rom(int bdf, int instance)
165 if (!CONFIG_BOOTORDER)
167 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
169 p = build_pci_path(desc, sizeof(desc), "*", bdf);
171 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
172 return find_prio(desc);
175 int bootprio_find_named_rom(const char *name, int instance)
177 if (!CONFIG_BOOTORDER)
179 // Find named rom - for example: /rom@genroms/linuxboot.bin
181 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
183 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
184 return find_prio(desc);
187 int bootprio_find_usb(int bdf, u64 path)
189 if (!CONFIG_BOOTORDER)
191 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
194 p = build_pci_path(desc, sizeof(desc), "usb", bdf);
195 for (i=56; i>0; i-=8) {
196 int port = (path >> i) & 0xff;
198 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
200 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
201 return find_prio(desc);
205 /****************************************************************
207 ****************************************************************/
209 static int CheckFloppySig = 1;
211 #define DEFAULT_PRIO 9999
213 static int DefaultFloppyPrio = 101;
214 static int DefaultCDPrio = 102;
215 static int DefaultHDPrio = 103;
216 static int DefaultBEVPrio = 104;
224 SET_EBDA(boot_sequence, 0xffff);
226 if (!CONFIG_COREBOOT) {
227 // On emulators, get boot order from nvram.
228 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
230 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
231 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
232 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
233 = DefaultBEVPrio = DEFAULT_PRIO;
235 for (i=101; i<104; i++) {
236 u32 val = bootorder & 0x0f;
239 case 1: DefaultFloppyPrio = i; break;
240 case 2: DefaultHDPrio = i; break;
241 case 3: DefaultCDPrio = i; break;
242 case 4: DefaultBEVPrio = i; break;
251 /****************************************************************
253 ****************************************************************/
259 struct segoff_s vector;
260 struct drive_s *drive;
263 const char *description;
264 struct bootentry_s *next;
266 static struct bootentry_s *BootList;
268 #define IPL_TYPE_FLOPPY 0x01
269 #define IPL_TYPE_HARDDISK 0x02
270 #define IPL_TYPE_CDROM 0x03
271 #define IPL_TYPE_CBFS 0x20
272 #define IPL_TYPE_BEV 0x80
273 #define IPL_TYPE_BCV 0x81
276 bootentry_add(int type, int prio, u32 data, const char *desc)
280 struct bootentry_s *be = malloc_tmp(sizeof(*be));
288 be->description = desc ?: "?";
289 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
290 , be->description, type, prio, data);
292 // Add entry in sorted order.
293 struct bootentry_s **pprev;
294 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
295 struct bootentry_s *pos = *pprev;
296 if (be->priority < pos->priority)
298 if (be->priority > pos->priority)
300 if (be->type < pos->type)
302 if (be->type > pos->type)
304 if (be->type <= IPL_TYPE_CDROM
305 && (be->drive->type < pos->drive->type
306 || (be->drive->type == pos->drive->type
307 && be->drive->cntl_id < pos->drive->cntl_id)))
314 // Return the given priority if it's set - defaultprio otherwise.
315 static inline int defPrio(int priority, int defaultprio) {
316 return (priority < 0) ? defaultprio : priority;
319 // Add a BEV vector for a given pnp compatible option rom.
321 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
323 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
324 , SEGOFF(seg, bev).segoff
325 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
326 DefaultBEVPrio = DEFAULT_PRIO;
329 // Add a bcv entry for an expansion card harddrive or legacy option rom
331 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
333 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
334 , SEGOFF(seg, ip).segoff
335 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
339 boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
341 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
342 , (u32)drive_g, desc);
346 boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
348 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
349 , (u32)drive_g, desc);
353 boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
355 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
356 , (u32)drive_g, desc);
359 // Add a CBFS payload entry
361 boot_add_cbfs(void *data, const char *desc, int prio)
363 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
367 /****************************************************************
368 * Boot menu and BCV execution
369 ****************************************************************/
371 // Show IPL option menu.
373 interactive_bootmenu(void)
375 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
378 while (get_keystroke(0) >= 0)
381 printf("Press F12 for boot menu.\n\n");
384 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
385 disable_bootsplash();
386 if (scan_code != 0x86)
390 while (get_keystroke(0) >= 0)
393 printf("Select boot device:\n\n");
397 struct bootentry_s *pos = BootList;
402 printf("%d. %s\n", maxmenu
403 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
409 scan_code = get_keystroke(1000);
410 if (scan_code >= 1 && scan_code <= maxmenu+1)
414 if (scan_code == 0x01)
418 // Find entry and make top priority.
419 int choice = scan_code - 1;
420 struct bootentry_s **pprev = &BootList;
422 pprev = &(*pprev)->next;
425 pos->next = BootList;
430 // BEV (Boot Execution Vector) list
435 static struct bev_s BEV[20];
437 static int HaveHDBoot, HaveFDBoot;
440 add_bev(int type, u32 vector)
442 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
444 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
446 if (BEVCount >= ARRAY_SIZE(BEV))
448 struct bev_s *bev = &BEV[BEVCount++];
450 bev->vector = vector;
453 // Prepare for boot - show menu and run bcvs.
462 // XXX - show available drives?
464 // Allow user to modify BCV/IPL order.
465 interactive_bootmenu();
468 // Map drives and populate BEV list
469 struct bootentry_s *pos = BootList;
473 call_bcv(pos->vector.seg, pos->vector.offset);
474 add_bev(IPL_TYPE_HARDDISK, 0);
476 case IPL_TYPE_FLOPPY:
477 map_floppy_drive(pos->drive);
478 add_bev(IPL_TYPE_FLOPPY, 0);
480 case IPL_TYPE_HARDDISK:
481 map_hd_drive(pos->drive);
482 add_bev(IPL_TYPE_HARDDISK, 0);
485 map_cd_drive(pos->drive);
488 add_bev(pos->type, pos->data);
494 // If nothing added a floppy/hd boot - add it manually.
495 add_bev(IPL_TYPE_FLOPPY, 0);
496 add_bev(IPL_TYPE_HARDDISK, 0);
500 /****************************************************************
501 * Boot code (int 18/19)
502 ****************************************************************/
504 // Jump to a bootup entry point.
506 call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
508 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
510 memset(&br, 0, sizeof(br));
513 // Set the magic number in ax and the boot drive in dl.
519 // Boot from a disk (either floppy or harddrive)
521 boot_disk(u8 bootdrv, int checksig)
523 u16 bootseg = 0x07c0;
527 memset(&br, 0, sizeof(br));
534 call16_int(0x13, &br);
536 if (br.flags & F_CF) {
537 printf("Boot failed: could not read the boot disk\n\n");
542 struct mbr_s *mbr = (void*)0;
543 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
544 printf("Boot failed: not a bootable disk\n\n");
549 /* Canonicalize bootseg:bootip */
550 u16 bootip = (bootseg & 0x0fff) << 4;
553 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
556 // Boot from a CD-ROM
558 boot_cdrom(struct drive_s *drive_g)
560 if (! CONFIG_CDROM_BOOT)
562 printf("Booting from DVD/CD...\n");
564 int status = cdrom_boot(drive_g);
566 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
570 u16 ebda_seg = get_ebda_seg();
571 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
572 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
573 /* Canonicalize bootseg:bootip */
574 u16 bootip = (bootseg & 0x0fff) << 4;
577 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
580 // Boot from a CBFS payload
582 boot_cbfs(struct cbfs_file *file)
584 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
586 printf("Booting from CBFS...\n");
587 cbfs_run_payload(file);
590 // Boot from a BEV entry on an optionrom.
594 printf("Booting from ROM...\n");
597 call_boot_entry(so, 0);
600 // Determine next boot method and attempt a boot using it.
605 panic("Boot support not compiled in.\n");
607 if (seq_nr >= BEVCount) {
608 printf("No bootable device.\n");
609 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
614 // Boot the given BEV type.
615 struct bev_s *ie = &BEV[seq_nr];
617 case IPL_TYPE_FLOPPY:
618 printf("Booting from Floppy...\n");
619 boot_disk(0x00, CheckFloppySig);
621 case IPL_TYPE_HARDDISK:
622 printf("Booting from Hard Disk...\n");
626 boot_cdrom((void*)ie->vector);
629 boot_cbfs((void*)ie->vector);
632 boot_rom(ie->vector);
636 // Boot failed: invoke the boot recovery function
638 memset(&br, 0, sizeof(br));
640 call16_int(0x18, &br);
643 // Boot Failure recovery: try the next device.
647 debug_serial_setup();
648 debug_enter(NULL, DEBUG_HDL_18);
649 u16 ebda_seg = get_ebda_seg();
650 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
651 SET_EBDA2(ebda_seg, boot_sequence, seq);
655 // INT 19h Boot Load Service Entry Point
659 debug_serial_setup();
660 debug_enter(NULL, DEBUG_HDL_19);
661 SET_EBDA(boot_sequence, 0);