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 char *f = romfile_loadfile("bootorder", NULL);
40 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
47 dprintf(3, "boot order:\n");
54 nullTrailingSpace(Bootorder[i]);
55 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
60 // See if 'str' starts with 'glob' - if glob contains an '*' character
61 // it will match any number of characters in str that aren't a '/' or
62 // the next glob character.
64 glob_prefix(const char *glob, const char *str)
67 if (!*glob && (!*str || *str == '/'))
70 if (!*str || *str == '/' || *str == glob[1])
83 // Search the bootorder list for the given glob pattern.
85 find_prio(const char *glob)
87 dprintf(1, "Searching bootorder for: %s\n", glob);
89 for (i = 0; i < BootorderCount; i++)
90 if (glob_prefix(glob, Bootorder[i]))
95 #define FW_PCI_DOMAIN "/pci@i0cf8"
98 build_pci_path(char *buf, int max, const char *devname, int bdf)
100 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
102 int parent = pci_bdf_to_bus(bdf);
104 parent = PCIpaths[parent];
105 int parentdev = parent & 0xffff;
106 if (parent & PP_PCIBRIDGE) {
107 p = build_pci_path(p, max, "pci-bridge", parentdev);
110 p += snprintf(p, max, "/pci-root@%x", parentdev);
111 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
114 int dev = pci_bdf_to_dev(bdf), fn = pci_bdf_to_fn(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(int bdf)
123 // Find pci device - for example: /pci@i0cf8/ethernet@5
125 build_pci_path(desc, sizeof(desc), "*", bdf);
126 return find_prio(desc);
129 int bootprio_find_ata_device(int bdf, int chanid, int slave)
132 // support only pci machine for now
134 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
136 p = build_pci_path(desc, sizeof(desc), "*", bdf);
137 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
138 return find_prio(desc);
141 int bootprio_find_fdc_device(int bdf, int port, int fdid)
144 // support only pci machine for now
146 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
148 p = build_pci_path(desc, sizeof(desc), "isa", bdf);
149 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
150 return find_prio(desc);
153 int bootprio_find_pci_rom(int bdf, int instance)
155 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
157 p = build_pci_path(desc, sizeof(desc), "*", bdf);
159 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
160 return find_prio(desc);
163 int bootprio_find_named_rom(const char *name, int instance)
165 // Find named rom - for example: /rom@genroms/linuxboot.bin
167 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
169 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
170 return find_prio(desc);
173 int bootprio_find_usb(int bdf, u64 path)
175 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
178 p = build_pci_path(desc, sizeof(desc), "usb", bdf);
179 for (i=56; i>0; i-=8) {
180 int port = (path >> i) & 0xff;
182 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
184 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
185 return find_prio(desc);
189 /****************************************************************
191 ****************************************************************/
193 static int CheckFloppySig = 1;
195 #define DEFAULT_PRIO 9999
197 static int DefaultFloppyPrio = 101;
198 static int DefaultCDPrio = 102;
199 static int DefaultHDPrio = 103;
200 static int DefaultBEVPrio = 104;
208 SET_EBDA(boot_sequence, 0xffff);
210 if (!CONFIG_COREBOOT) {
211 // On emulators, get boot order from nvram.
212 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
214 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
215 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
216 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
217 = DefaultBEVPrio = DEFAULT_PRIO;
219 for (i=101; i<104; i++) {
220 u32 val = bootorder & 0x0f;
223 case 1: DefaultFloppyPrio = i; break;
224 case 2: DefaultHDPrio = i; break;
225 case 3: DefaultCDPrio = i; break;
226 case 4: DefaultBEVPrio = i; break;
235 /****************************************************************
237 ****************************************************************/
243 struct segoff_s vector;
244 struct drive_s *drive;
247 const char *description;
248 struct bootentry_s *next;
250 static struct bootentry_s *BootList;
252 #define IPL_TYPE_FLOPPY 0x01
253 #define IPL_TYPE_HARDDISK 0x02
254 #define IPL_TYPE_CDROM 0x03
255 #define IPL_TYPE_CBFS 0x20
256 #define IPL_TYPE_BEV 0x80
257 #define IPL_TYPE_BCV 0x81
260 bootentry_add(int type, int prio, u32 data, const char *desc)
264 struct bootentry_s *be = malloc_tmp(sizeof(*be));
272 be->description = desc ?: "?";
273 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
274 , be->description, type, prio, data);
276 // Add entry in sorted order.
277 struct bootentry_s **pprev;
278 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
279 struct bootentry_s *pos = *pprev;
280 if (be->priority < pos->priority)
282 if (be->priority > pos->priority)
284 if (be->type < pos->type)
286 if (be->type > pos->type)
288 if (be->type <= IPL_TYPE_CDROM
289 && (be->drive->type < pos->drive->type
290 || (be->drive->type == pos->drive->type
291 && be->drive->cntl_id < pos->drive->cntl_id)))
298 // Return the given priority if it's set - defaultprio otherwise.
299 static inline int defPrio(int priority, int defaultprio) {
300 return (priority < 0) ? defaultprio : priority;
303 // Add a BEV vector for a given pnp compatible option rom.
305 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
307 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
308 , SEGOFF(seg, bev).segoff
309 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
310 DefaultBEVPrio = DEFAULT_PRIO;
313 // Add a bcv entry for an expansion card harddrive or legacy option rom
315 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
317 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
318 , SEGOFF(seg, ip).segoff
319 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
323 boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
325 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
326 , (u32)drive_g, desc);
330 boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
332 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
333 , (u32)drive_g, desc);
337 boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
339 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
340 , (u32)drive_g, desc);
343 // Add a CBFS payload entry
345 boot_add_cbfs(void *data, const char *desc, int prio)
347 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
351 /****************************************************************
352 * Boot menu and BCV execution
353 ****************************************************************/
355 // Show IPL option menu.
357 interactive_bootmenu(void)
359 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
362 while (get_keystroke(0) >= 0)
365 printf("Press F12 for boot menu.\n\n");
368 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
369 disable_bootsplash();
370 if (scan_code != 0x86)
374 while (get_keystroke(0) >= 0)
377 printf("Select boot device:\n\n");
381 struct bootentry_s *pos = BootList;
386 printf("%d. %s\n", maxmenu
387 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
393 scan_code = get_keystroke(1000);
394 if (scan_code >= 1 && scan_code <= maxmenu+1)
398 if (scan_code == 0x01)
402 // Find entry and make top priority.
403 int choice = scan_code - 1;
404 struct bootentry_s **pprev = &BootList;
406 pprev = &(*pprev)->next;
409 pos->next = BootList;
414 // BEV (Boot Execution Vector) list
419 static struct bev_s BEV[20];
421 static int HaveHDBoot, HaveFDBoot;
424 add_bev(int type, u32 vector)
426 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
428 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
430 if (BEVCount >= ARRAY_SIZE(BEV))
432 struct bev_s *bev = &BEV[BEVCount++];
434 bev->vector = vector;
437 // Prepare for boot - show menu and run bcvs.
446 // XXX - show available drives?
448 // Allow user to modify BCV/IPL order.
449 interactive_bootmenu();
452 // Map drives and populate BEV list
453 struct bootentry_s *pos = BootList;
457 call_bcv(pos->vector.seg, pos->vector.offset);
458 add_bev(IPL_TYPE_HARDDISK, 0);
460 case IPL_TYPE_FLOPPY:
461 map_floppy_drive(pos->drive);
462 add_bev(IPL_TYPE_FLOPPY, 0);
464 case IPL_TYPE_HARDDISK:
465 map_hd_drive(pos->drive);
466 add_bev(IPL_TYPE_HARDDISK, 0);
469 map_cd_drive(pos->drive);
472 add_bev(pos->type, pos->data);
478 // If nothing added a floppy/hd boot - add it manually.
479 add_bev(IPL_TYPE_FLOPPY, 0);
480 add_bev(IPL_TYPE_HARDDISK, 0);
484 /****************************************************************
485 * Boot code (int 18/19)
486 ****************************************************************/
488 // Jump to a bootup entry point.
490 call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
492 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
494 memset(&br, 0, sizeof(br));
497 // Set the magic number in ax and the boot drive in dl.
503 // Boot from a disk (either floppy or harddrive)
505 boot_disk(u8 bootdrv, int checksig)
507 u16 bootseg = 0x07c0;
511 memset(&br, 0, sizeof(br));
518 call16_int(0x13, &br);
520 if (br.flags & F_CF) {
521 printf("Boot failed: could not read the boot disk\n\n");
526 struct mbr_s *mbr = (void*)0;
527 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
528 printf("Boot failed: not a bootable disk\n\n");
533 /* Canonicalize bootseg:bootip */
534 u16 bootip = (bootseg & 0x0fff) << 4;
537 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
540 // Boot from a CD-ROM
542 boot_cdrom(struct drive_s *drive_g)
544 if (! CONFIG_CDROM_BOOT)
546 printf("Booting from DVD/CD...\n");
548 int status = cdrom_boot(drive_g);
550 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
554 u16 ebda_seg = get_ebda_seg();
555 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
556 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
557 /* Canonicalize bootseg:bootip */
558 u16 bootip = (bootseg & 0x0fff) << 4;
561 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
564 // Boot from a CBFS payload
566 boot_cbfs(struct cbfs_file *file)
568 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
570 printf("Booting from CBFS...\n");
571 cbfs_run_payload(file);
574 // Boot from a BEV entry on an optionrom.
578 printf("Booting from ROM...\n");
581 call_boot_entry(so, 0);
584 // Determine next boot method and attempt a boot using it.
589 panic("Boot support not compiled in.\n");
591 if (seq_nr >= BEVCount) {
592 printf("No bootable device.\n");
593 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
598 // Boot the given BEV type.
599 struct bev_s *ie = &BEV[seq_nr];
601 case IPL_TYPE_FLOPPY:
602 printf("Booting from Floppy...\n");
603 boot_disk(0x00, CheckFloppySig);
605 case IPL_TYPE_HARDDISK:
606 printf("Booting from Hard Disk...\n");
610 boot_cdrom((void*)ie->vector);
613 boot_cbfs((void*)ie->vector);
616 boot_rom(ie->vector);
620 // Boot failed: invoke the boot recovery function
622 memset(&br, 0, sizeof(br));
624 call16_int(0x18, &br);
627 // Boot Failure recovery: try the next device.
631 debug_serial_setup();
632 debug_enter(NULL, DEBUG_HDL_18);
633 u16 ebda_seg = get_ebda_seg();
634 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
635 SET_EBDA2(ebda_seg, boot_sequence, seq);
639 // INT 19h Boot Load Service Entry Point
643 debug_serial_setup();
644 debug_enter(NULL, DEBUG_HDL_19);
645 SET_EBDA(boot_sequence, 0);