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, 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_ata_device(struct pci_device *pci, int chanid, int slave)
133 if (!CONFIG_BOOTORDER)
136 // support only pci machine for now
138 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
140 p = build_pci_path(desc, sizeof(desc), "*", pci);
141 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
142 return find_prio(desc);
145 int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
147 if (!CONFIG_BOOTORDER)
150 // support only pci machine for now
152 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
154 p = build_pci_path(desc, sizeof(desc), "isa", pci);
155 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
156 return find_prio(desc);
159 int bootprio_find_pci_rom(struct pci_device *pci, int instance)
161 if (!CONFIG_BOOTORDER)
163 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
165 p = build_pci_path(desc, sizeof(desc), "*", pci);
167 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
168 return find_prio(desc);
171 int bootprio_find_named_rom(const char *name, int instance)
173 if (!CONFIG_BOOTORDER)
175 // Find named rom - for example: /rom@genroms/linuxboot.bin
177 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
179 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
180 return find_prio(desc);
183 int bootprio_find_usb(struct pci_device *pci, u64 path)
185 if (!CONFIG_BOOTORDER)
187 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
190 p = build_pci_path(desc, sizeof(desc), "usb", pci);
191 for (i=56; i>0; i-=8) {
192 int port = (path >> i) & 0xff;
194 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
196 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
197 return find_prio(desc);
201 /****************************************************************
203 ****************************************************************/
205 static int CheckFloppySig = 1;
207 #define DEFAULT_PRIO 9999
209 static int DefaultFloppyPrio = 101;
210 static int DefaultCDPrio = 102;
211 static int DefaultHDPrio = 103;
212 static int DefaultBEVPrio = 104;
220 SET_EBDA(boot_sequence, 0xffff);
222 if (!CONFIG_COREBOOT) {
223 // On emulators, get boot order from nvram.
224 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
226 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
227 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
228 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
229 = DefaultBEVPrio = DEFAULT_PRIO;
231 for (i=101; i<104; i++) {
232 u32 val = bootorder & 0x0f;
235 case 1: DefaultFloppyPrio = i; break;
236 case 2: DefaultHDPrio = i; break;
237 case 3: DefaultCDPrio = i; break;
238 case 4: DefaultBEVPrio = i; break;
247 /****************************************************************
249 ****************************************************************/
255 struct segoff_s vector;
256 struct drive_s *drive;
259 const char *description;
260 struct bootentry_s *next;
262 static struct bootentry_s *BootList;
264 #define IPL_TYPE_FLOPPY 0x01
265 #define IPL_TYPE_HARDDISK 0x02
266 #define IPL_TYPE_CDROM 0x03
267 #define IPL_TYPE_CBFS 0x20
268 #define IPL_TYPE_BEV 0x80
269 #define IPL_TYPE_BCV 0x81
272 bootentry_add(int type, int prio, u32 data, const char *desc)
276 struct bootentry_s *be = malloc_tmp(sizeof(*be));
284 be->description = desc ?: "?";
285 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
286 , be->description, type, prio, data);
288 // Add entry in sorted order.
289 struct bootentry_s **pprev;
290 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
291 struct bootentry_s *pos = *pprev;
292 if (be->priority < pos->priority)
294 if (be->priority > pos->priority)
296 if (be->type < pos->type)
298 if (be->type > pos->type)
300 if (be->type <= IPL_TYPE_CDROM
301 && (be->drive->type < pos->drive->type
302 || (be->drive->type == pos->drive->type
303 && be->drive->cntl_id < pos->drive->cntl_id)))
310 // Return the given priority if it's set - defaultprio otherwise.
311 static inline int defPrio(int priority, int defaultprio) {
312 return (priority < 0) ? defaultprio : priority;
315 // Add a BEV vector for a given pnp compatible option rom.
317 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
319 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
320 , SEGOFF(seg, bev).segoff
321 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
322 DefaultBEVPrio = DEFAULT_PRIO;
325 // Add a bcv entry for an expansion card harddrive or legacy option rom
327 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
329 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
330 , SEGOFF(seg, ip).segoff
331 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
335 boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
337 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
338 , (u32)drive_g, desc);
342 boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
344 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
345 , (u32)drive_g, desc);
349 boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
351 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
352 , (u32)drive_g, desc);
355 // Add a CBFS payload entry
357 boot_add_cbfs(void *data, const char *desc, int prio)
359 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
363 /****************************************************************
364 * Boot menu and BCV execution
365 ****************************************************************/
367 #define DEFAULT_BOOTMENU_WAIT 2500
369 // Show IPL option menu.
371 interactive_bootmenu(void)
373 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
376 while (get_keystroke(0) >= 0)
379 printf("Press F12 for boot menu.\n\n");
381 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
383 int scan_code = get_keystroke(menutime);
384 disable_bootsplash();
385 if (scan_code != 0x86)
389 while (get_keystroke(0) >= 0)
392 printf("Select boot device:\n\n");
396 struct bootentry_s *pos = BootList;
401 printf("%d. %s\n", maxmenu
402 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
408 scan_code = get_keystroke(1000);
409 if (scan_code >= 1 && scan_code <= maxmenu+1)
413 if (scan_code == 0x01)
417 // Find entry and make top priority.
418 int choice = scan_code - 1;
419 struct bootentry_s **pprev = &BootList;
421 pprev = &(*pprev)->next;
424 pos->next = BootList;
429 // BEV (Boot Execution Vector) list
434 static struct bev_s BEV[20];
436 static int HaveHDBoot, HaveFDBoot;
439 add_bev(int type, u32 vector)
441 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
443 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
445 if (BEVCount >= ARRAY_SIZE(BEV))
447 struct bev_s *bev = &BEV[BEVCount++];
449 bev->vector = vector;
452 // Prepare for boot - show menu and run bcvs.
461 // XXX - show available drives?
463 // Allow user to modify BCV/IPL order.
464 interactive_bootmenu();
467 // Map drives and populate BEV list
468 struct bootentry_s *pos = BootList;
472 call_bcv(pos->vector.seg, pos->vector.offset);
473 add_bev(IPL_TYPE_HARDDISK, 0);
475 case IPL_TYPE_FLOPPY:
476 map_floppy_drive(pos->drive);
477 add_bev(IPL_TYPE_FLOPPY, 0);
479 case IPL_TYPE_HARDDISK:
480 map_hd_drive(pos->drive);
481 add_bev(IPL_TYPE_HARDDISK, 0);
484 map_cd_drive(pos->drive);
487 add_bev(pos->type, pos->data);
493 // If nothing added a floppy/hd boot - add it manually.
494 add_bev(IPL_TYPE_FLOPPY, 0);
495 add_bev(IPL_TYPE_HARDDISK, 0);
499 /****************************************************************
500 * Boot code (int 18/19)
501 ****************************************************************/
503 // Jump to a bootup entry point.
505 call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
507 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
509 memset(&br, 0, sizeof(br));
512 // Set the magic number in ax and the boot drive in dl.
518 // Boot from a disk (either floppy or harddrive)
520 boot_disk(u8 bootdrv, int checksig)
522 u16 bootseg = 0x07c0;
526 memset(&br, 0, sizeof(br));
533 call16_int(0x13, &br);
535 if (br.flags & F_CF) {
536 printf("Boot failed: could not read the boot disk\n\n");
541 struct mbr_s *mbr = (void*)0;
542 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
543 printf("Boot failed: not a bootable disk\n\n");
548 /* Canonicalize bootseg:bootip */
549 u16 bootip = (bootseg & 0x0fff) << 4;
552 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
555 // Boot from a CD-ROM
557 boot_cdrom(struct drive_s *drive_g)
559 if (! CONFIG_CDROM_BOOT)
561 printf("Booting from DVD/CD...\n");
563 int status = cdrom_boot(drive_g);
565 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
569 u16 ebda_seg = get_ebda_seg();
570 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
571 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
572 /* Canonicalize bootseg:bootip */
573 u16 bootip = (bootseg & 0x0fff) << 4;
576 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
579 // Boot from a CBFS payload
581 boot_cbfs(struct cbfs_file *file)
583 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
585 printf("Booting from CBFS...\n");
586 cbfs_run_payload(file);
589 // Boot from a BEV entry on an optionrom.
593 printf("Booting from ROM...\n");
596 call_boot_entry(so, 0);
599 // Determine next boot method and attempt a boot using it.
604 panic("Boot support not compiled in.\n");
606 if (seq_nr >= BEVCount) {
607 printf("No bootable device.\n");
608 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
613 // Boot the given BEV type.
614 struct bev_s *ie = &BEV[seq_nr];
616 case IPL_TYPE_FLOPPY:
617 printf("Booting from Floppy...\n");
618 boot_disk(0x00, CheckFloppySig);
620 case IPL_TYPE_HARDDISK:
621 printf("Booting from Hard Disk...\n");
625 boot_cdrom((void*)ie->vector);
628 boot_cbfs((void*)ie->vector);
631 boot_rom(ie->vector);
635 // Boot failed: invoke the boot recovery function
637 memset(&br, 0, sizeof(br));
639 call16_int(0x18, &br);
642 // Boot Failure recovery: try the next device.
646 debug_serial_setup();
647 debug_enter(NULL, DEBUG_HDL_18);
648 u16 ebda_seg = get_ebda_seg();
649 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
650 SET_EBDA2(ebda_seg, boot_sequence, seq);
654 // INT 19h Boot Load Service Entry Point
658 debug_serial_setup();
659 debug_enter(NULL, DEBUG_HDL_19);
660 SET_EBDA(boot_sequence, 0);