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 bus = pci_bdf_to_bus(bdf);
104 // XXX - this isn't the correct path syntax
105 p += snprintf(p, max, "/bus%x", bus);
107 int dev = pci_bdf_to_dev(bdf), fn = pci_bdf_to_fn(bdf);
108 p += snprintf(p, buf+max-p, "%s/%s@%x", FW_PCI_DOMAIN, devname, dev);
110 p += snprintf(p, buf+max-p, ",%x", fn);
114 int bootprio_find_pci_device(int bdf)
116 // Find pci device - for example: /pci@i0cf8/ethernet@5
118 build_pci_path(desc, sizeof(desc), "*", bdf);
119 return find_prio(desc);
122 int bootprio_find_ata_device(int bdf, int chanid, int slave)
125 // support only pci machine for now
127 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
129 p = build_pci_path(desc, sizeof(desc), "*", bdf);
130 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
131 return find_prio(desc);
134 int bootprio_find_fdc_device(int bdf, int port, int fdid)
137 // support only pci machine for now
139 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
141 p = build_pci_path(desc, sizeof(desc), "isa", bdf);
142 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
143 return find_prio(desc);
146 int bootprio_find_pci_rom(int bdf, int instance)
148 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
150 p = build_pci_path(desc, sizeof(desc), "*", bdf);
152 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
153 return find_prio(desc);
156 int bootprio_find_named_rom(const char *name, int instance)
158 // Find named rom - for example: /rom@genroms/linuxboot.bin
160 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
162 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
163 return find_prio(desc);
166 int bootprio_find_usb(int bdf, u64 path)
168 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
171 p = build_pci_path(desc, sizeof(desc), "usb", bdf);
172 for (i=56; i>0; i-=8) {
173 int port = (path >> i) & 0xff;
175 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
177 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
178 return find_prio(desc);
182 /****************************************************************
184 ****************************************************************/
186 static int CheckFloppySig = 1;
188 #define DEFAULT_PRIO 9999
190 static int DefaultFloppyPrio = 101;
191 static int DefaultCDPrio = 102;
192 static int DefaultHDPrio = 103;
193 static int DefaultBEVPrio = 104;
201 SET_EBDA(boot_sequence, 0xffff);
203 if (!CONFIG_COREBOOT) {
204 // On emulators, get boot order from nvram.
205 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
207 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
208 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
209 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
210 = DefaultBEVPrio = DEFAULT_PRIO;
212 for (i=101; i<104; i++) {
213 u32 val = bootorder & 0x0f;
216 case 1: DefaultFloppyPrio = i; break;
217 case 2: DefaultHDPrio = i; break;
218 case 3: DefaultCDPrio = i; break;
219 case 4: DefaultBEVPrio = i; break;
228 /****************************************************************
230 ****************************************************************/
236 struct segoff_s vector;
237 struct drive_s *drive;
240 const char *description;
241 struct bootentry_s *next;
243 static struct bootentry_s *BootList;
245 #define IPL_TYPE_FLOPPY 0x01
246 #define IPL_TYPE_HARDDISK 0x02
247 #define IPL_TYPE_CDROM 0x03
248 #define IPL_TYPE_CBFS 0x20
249 #define IPL_TYPE_BEV 0x80
250 #define IPL_TYPE_BCV 0x81
253 bootentry_add(int type, int prio, u32 data, const char *desc)
257 struct bootentry_s *be = malloc_tmp(sizeof(*be));
265 be->description = desc ?: "?";
266 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
267 , be->description, type, prio, data);
269 // Add entry in sorted order.
270 struct bootentry_s **pprev;
271 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
272 struct bootentry_s *pos = *pprev;
273 if (be->priority < pos->priority)
275 if (be->priority > pos->priority)
277 if (be->type < pos->type)
279 if (be->type > pos->type)
281 if (be->type <= IPL_TYPE_CDROM
282 && (be->drive->type < pos->drive->type
283 || (be->drive->type == pos->drive->type
284 && be->drive->cntl_id < pos->drive->cntl_id)))
291 // Return the given priority if it's set - defaultprio otherwise.
292 static inline int defPrio(int priority, int defaultprio) {
293 return (priority < 0) ? defaultprio : priority;
296 // Add a BEV vector for a given pnp compatible option rom.
298 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
300 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
301 , SEGOFF(seg, bev).segoff
302 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
303 DefaultBEVPrio = DEFAULT_PRIO;
306 // Add a bcv entry for an expansion card harddrive or legacy option rom
308 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
310 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
311 , SEGOFF(seg, ip).segoff
312 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
316 boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
318 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
319 , (u32)drive_g, desc);
323 boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
325 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
326 , (u32)drive_g, desc);
330 boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
332 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
333 , (u32)drive_g, desc);
336 // Add a CBFS payload entry
338 boot_add_cbfs(void *data, const char *desc, int prio)
340 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
344 /****************************************************************
345 * Boot menu and BCV execution
346 ****************************************************************/
348 // Show IPL option menu.
350 interactive_bootmenu(void)
352 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
355 while (get_keystroke(0) >= 0)
358 printf("Press F12 for boot menu.\n\n");
361 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
362 disable_bootsplash();
363 if (scan_code != 0x86)
367 while (get_keystroke(0) >= 0)
370 printf("Select boot device:\n\n");
374 struct bootentry_s *pos = BootList;
379 printf("%d. %s\n", maxmenu
380 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
386 scan_code = get_keystroke(1000);
387 if (scan_code >= 1 && scan_code <= maxmenu+1)
391 if (scan_code == 0x01)
395 // Find entry and make top priority.
396 int choice = scan_code - 1;
397 struct bootentry_s **pprev = &BootList;
399 pprev = &(*pprev)->next;
402 pos->next = BootList;
407 // BEV (Boot Execution Vector) list
412 static struct bev_s BEV[20];
414 static int HaveHDBoot, HaveFDBoot;
417 add_bev(int type, u32 vector)
419 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
421 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
423 if (BEVCount >= ARRAY_SIZE(BEV))
425 struct bev_s *bev = &BEV[BEVCount++];
427 bev->vector = vector;
430 // Prepare for boot - show menu and run bcvs.
439 // XXX - show available drives?
441 // Allow user to modify BCV/IPL order.
442 interactive_bootmenu();
445 // Map drives and populate BEV list
446 struct bootentry_s *pos = BootList;
450 call_bcv(pos->vector.seg, pos->vector.offset);
451 add_bev(IPL_TYPE_HARDDISK, 0);
453 case IPL_TYPE_FLOPPY:
454 map_floppy_drive(pos->drive);
455 add_bev(IPL_TYPE_FLOPPY, 0);
457 case IPL_TYPE_HARDDISK:
458 map_hd_drive(pos->drive);
459 add_bev(IPL_TYPE_HARDDISK, 0);
462 map_cd_drive(pos->drive);
465 add_bev(pos->type, pos->data);
471 // If nothing added a floppy/hd boot - add it manually.
472 add_bev(IPL_TYPE_FLOPPY, 0);
473 add_bev(IPL_TYPE_HARDDISK, 0);
477 /****************************************************************
478 * Boot code (int 18/19)
479 ****************************************************************/
481 // Jump to a bootup entry point.
483 call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
485 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
487 memset(&br, 0, sizeof(br));
490 // Set the magic number in ax and the boot drive in dl.
496 // Boot from a disk (either floppy or harddrive)
498 boot_disk(u8 bootdrv, int checksig)
500 u16 bootseg = 0x07c0;
504 memset(&br, 0, sizeof(br));
511 call16_int(0x13, &br);
513 if (br.flags & F_CF) {
514 printf("Boot failed: could not read the boot disk\n\n");
519 struct mbr_s *mbr = (void*)0;
520 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
521 printf("Boot failed: not a bootable disk\n\n");
526 /* Canonicalize bootseg:bootip */
527 u16 bootip = (bootseg & 0x0fff) << 4;
530 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
533 // Boot from a CD-ROM
535 boot_cdrom(struct drive_s *drive_g)
537 if (! CONFIG_CDROM_BOOT)
539 printf("Booting from DVD/CD...\n");
541 int status = cdrom_boot(drive_g);
543 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
547 u16 ebda_seg = get_ebda_seg();
548 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
549 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
550 /* Canonicalize bootseg:bootip */
551 u16 bootip = (bootseg & 0x0fff) << 4;
554 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
557 // Boot from a CBFS payload
559 boot_cbfs(struct cbfs_file *file)
561 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
563 printf("Booting from CBFS...\n");
564 cbfs_run_payload(file);
567 // Boot from a BEV entry on an optionrom.
571 printf("Booting from ROM...\n");
574 call_boot_entry(so, 0);
577 // Determine next boot method and attempt a boot using it.
582 panic("Boot support not compiled in.\n");
584 if (seq_nr >= BEVCount) {
585 printf("No bootable device.\n");
586 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
591 // Boot the given BEV type.
592 struct bev_s *ie = &BEV[seq_nr];
594 case IPL_TYPE_FLOPPY:
595 printf("Booting from Floppy...\n");
596 boot_disk(0x00, CheckFloppySig);
598 case IPL_TYPE_HARDDISK:
599 printf("Booting from Hard Disk...\n");
603 boot_cdrom((void*)ie->vector);
606 boot_cbfs((void*)ie->vector);
609 boot_rom(ie->vector);
613 // Boot failed: invoke the boot recovery function
615 memset(&br, 0, sizeof(br));
617 call16_int(0x18, &br);
620 // Boot Failure recovery: try the next device.
624 debug_serial_setup();
625 debug_enter(NULL, DEBUG_HDL_18);
626 u16 ebda_seg = get_ebda_seg();
627 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
628 SET_EBDA2(ebda_seg, boot_sequence, seq);
632 // INT 19h Boot Load Service Entry Point
636 debug_serial_setup();
637 debug_enter(NULL, DEBUG_HDL_19);
638 SET_EBDA(boot_sequence, 0);