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 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
59 // See if 'str' starts with 'glob' - if glob contains an '*' character
60 // it will match any number of characters in str that aren't a '/' or
61 // the next glob character.
63 glob_prefix(const char *glob, const char *str)
66 if (!*glob && (!*str || *str == '/'))
69 if (!*str || *str == '/' || *str == glob[1])
82 // Search the bootorder list for the given glob pattern.
84 find_prio(const char *glob)
86 dprintf(1, "Searching bootorder for: %s\n", glob);
88 for (i = 0; i < BootorderCount; i++)
89 if (glob_prefix(glob, Bootorder[i]))
94 #define FW_PCI_DOMAIN "/pci@i0cf8"
97 build_pci_path(char *buf, int max, const char *devname, int bdf)
99 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
101 int bus = pci_bdf_to_bus(bdf);
103 // XXX - this isn't the correct path syntax
104 p += snprintf(p, max, "/bus%x", bus);
106 int dev = pci_bdf_to_dev(bdf), fn = pci_bdf_to_fn(bdf);
107 p += snprintf(p, buf+max-p, "%s/%s@%x", FW_PCI_DOMAIN, devname, dev);
109 p += snprintf(p, buf+max-p, ",%x", fn);
113 int bootprio_find_pci_device(int bdf)
115 // Find pci device - for example: /pci@i0cf8/ethernet@5
117 build_pci_path(desc, sizeof(desc), "*", bdf);
118 return find_prio(desc);
121 int bootprio_find_ata_device(int bdf, int chanid, int slave)
124 // support only pci machine for now
126 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
128 p = build_pci_path(desc, sizeof(desc), "*", bdf);
129 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
130 return find_prio(desc);
133 int bootprio_find_fdc_device(int bdf, int port, int fdid)
136 // support only pci machine for now
138 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
140 p = build_pci_path(desc, sizeof(desc), "isa", bdf);
141 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
142 return find_prio(desc);
145 int bootprio_find_pci_rom(int bdf, int instance)
147 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
149 p = build_pci_path(desc, sizeof(desc), "*", bdf);
151 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
152 return find_prio(desc);
155 int bootprio_find_named_rom(const char *name, int instance)
157 // Find named rom - for example: /rom@genroms/linuxboot.bin
159 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
161 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
162 return find_prio(desc);
165 int bootprio_find_usb(int bdf, u64 path)
167 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
170 p = build_pci_path(desc, sizeof(desc), "usb", bdf);
171 for (i=56; i>0; i-=8) {
172 int port = (path >> i) & 0xff;
174 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
176 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
177 return find_prio(desc);
181 /****************************************************************
183 ****************************************************************/
185 static int CheckFloppySig = 1;
187 #define DEFAULT_PRIO 9999
189 static int DefaultFloppyPrio = 101;
190 static int DefaultCDPrio = 102;
191 static int DefaultHDPrio = 103;
192 static int DefaultBEVPrio = 104;
200 SET_EBDA(boot_sequence, 0xffff);
202 if (!CONFIG_COREBOOT) {
203 // On emulators, get boot order from nvram.
204 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
206 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
207 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
208 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
209 = DefaultBEVPrio = DEFAULT_PRIO;
211 for (i=101; i<104; i++) {
212 u32 val = bootorder & 0x0f;
215 case 1: DefaultFloppyPrio = i; break;
216 case 2: DefaultHDPrio = i; break;
217 case 3: DefaultCDPrio = i; break;
218 case 4: DefaultBEVPrio = i; break;
227 /****************************************************************
229 ****************************************************************/
235 struct segoff_s vector;
236 struct drive_s *drive;
239 const char *description;
240 struct bootentry_s *next;
242 static struct bootentry_s *BootList;
244 #define IPL_TYPE_FLOPPY 0x01
245 #define IPL_TYPE_HARDDISK 0x02
246 #define IPL_TYPE_CDROM 0x03
247 #define IPL_TYPE_CBFS 0x20
248 #define IPL_TYPE_BEV 0x80
249 #define IPL_TYPE_BCV 0x81
252 bootentry_add(int type, int prio, u32 data, const char *desc)
256 struct bootentry_s *be = malloc_tmp(sizeof(*be));
264 be->description = desc ?: "?";
265 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
266 , be->description, type, prio, data);
268 // Add entry in sorted order.
269 struct bootentry_s **pprev;
270 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
271 struct bootentry_s *pos = *pprev;
272 if (be->priority < pos->priority)
274 if (be->priority > pos->priority)
276 if (be->type < pos->type)
278 if (be->type > pos->type)
280 if (be->type <= IPL_TYPE_CDROM
281 && (be->drive->type < pos->drive->type
282 || (be->drive->type == pos->drive->type
283 && be->drive->cntl_id < pos->drive->cntl_id)))
290 // Return the given priority if it's set - defaultprio otherwise.
291 static inline int defPrio(int priority, int defaultprio) {
292 return (priority < 0) ? defaultprio : priority;
295 // Add a BEV vector for a given pnp compatible option rom.
297 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
299 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
300 , SEGOFF(seg, bev).segoff
301 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
302 DefaultBEVPrio = DEFAULT_PRIO;
305 // Add a bcv entry for an expansion card harddrive or legacy option rom
307 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
309 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
310 , SEGOFF(seg, ip).segoff
311 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
315 boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
317 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
318 , (u32)drive_g, desc);
322 boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
324 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
325 , (u32)drive_g, desc);
329 boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
331 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
332 , (u32)drive_g, desc);
335 // Add a CBFS payload entry
337 boot_add_cbfs(void *data, const char *desc, int prio)
339 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
343 /****************************************************************
344 * Boot menu and BCV execution
345 ****************************************************************/
347 // Show IPL option menu.
349 interactive_bootmenu(void)
351 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
354 while (get_keystroke(0) >= 0)
357 printf("Press F12 for boot menu.\n\n");
360 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
361 disable_bootsplash();
362 if (scan_code != 0x86)
366 while (get_keystroke(0) >= 0)
369 printf("Select boot device:\n\n");
373 struct bootentry_s *pos = BootList;
378 printf("%d. %s\n", maxmenu
379 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
385 scan_code = get_keystroke(1000);
386 if (scan_code >= 1 && scan_code <= maxmenu+1)
390 if (scan_code == 0x01)
394 // Find entry and make top priority.
395 int choice = scan_code - 1;
396 struct bootentry_s **pprev = &BootList;
398 pprev = &(*pprev)->next;
401 pos->next = BootList;
406 // BEV (Boot Execution Vector) list
411 static struct bev_s BEV[20];
413 static int HaveHDBoot, HaveFDBoot;
416 add_bev(int type, u32 vector)
418 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
420 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
422 if (BEVCount >= ARRAY_SIZE(BEV))
424 struct bev_s *bev = &BEV[BEVCount++];
426 bev->vector = vector;
429 // Prepare for boot - show menu and run bcvs.
438 // XXX - show available drives?
440 // Allow user to modify BCV/IPL order.
441 interactive_bootmenu();
444 // Map drives and populate BEV list
445 struct bootentry_s *pos = BootList;
449 call_bcv(pos->vector.seg, pos->vector.offset);
450 add_bev(IPL_TYPE_HARDDISK, 0);
452 case IPL_TYPE_FLOPPY:
453 map_floppy_drive(pos->drive);
454 add_bev(IPL_TYPE_FLOPPY, 0);
456 case IPL_TYPE_HARDDISK:
457 map_hd_drive(pos->drive);
458 add_bev(IPL_TYPE_HARDDISK, 0);
461 map_cd_drive(pos->drive);
464 add_bev(pos->type, pos->data);
470 // If nothing added a floppy/hd boot - add it manually.
471 add_bev(IPL_TYPE_FLOPPY, 0);
472 add_bev(IPL_TYPE_HARDDISK, 0);
476 /****************************************************************
477 * Boot code (int 18/19)
478 ****************************************************************/
480 // Jump to a bootup entry point.
482 call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
484 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
486 memset(&br, 0, sizeof(br));
489 // Set the magic number in ax and the boot drive in dl.
495 // Boot from a disk (either floppy or harddrive)
497 boot_disk(u8 bootdrv, int checksig)
499 u16 bootseg = 0x07c0;
503 memset(&br, 0, sizeof(br));
510 call16_int(0x13, &br);
512 if (br.flags & F_CF) {
513 printf("Boot failed: could not read the boot disk\n\n");
518 struct mbr_s *mbr = (void*)0;
519 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
520 printf("Boot failed: not a bootable disk\n\n");
525 /* Canonicalize bootseg:bootip */
526 u16 bootip = (bootseg & 0x0fff) << 4;
529 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
532 // Boot from a CD-ROM
534 boot_cdrom(struct drive_s *drive_g)
536 if (! CONFIG_CDROM_BOOT)
538 printf("Booting from DVD/CD...\n");
540 int status = cdrom_boot(drive_g);
542 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
546 u16 ebda_seg = get_ebda_seg();
547 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
548 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
549 /* Canonicalize bootseg:bootip */
550 u16 bootip = (bootseg & 0x0fff) << 4;
553 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
556 // Boot from a CBFS payload
558 boot_cbfs(struct cbfs_file *file)
560 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
562 printf("Booting from CBFS...\n");
563 cbfs_run_payload(file);
566 // Boot from a BEV entry on an optionrom.
570 printf("Booting from ROM...\n");
573 call_boot_entry(so, 0);
576 // Determine next boot method and attempt a boot using it.
581 panic("Boot support not compiled in.\n");
583 if (seq_nr >= BEVCount) {
584 printf("No bootable device.\n");
585 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
590 // Boot the given BEV type.
591 struct bev_s *ie = &BEV[seq_nr];
593 case IPL_TYPE_FLOPPY:
594 printf("Booting from Floppy...\n");
595 boot_disk(0x00, CheckFloppySig);
597 case IPL_TYPE_HARDDISK:
598 printf("Booting from Hard Disk...\n");
602 boot_cdrom((void*)ie->vector);
605 boot_cbfs((void*)ie->vector);
608 boot_rom(ie->vector);
612 // Boot failed: invoke the boot recovery function
614 memset(&br, 0, sizeof(br));
616 call16_int(0x18, &br);
619 // Boot Failure recovery: try the next device.
623 debug_serial_setup();
624 debug_enter(NULL, DEBUG_HDL_18);
625 u16 ebda_seg = get_ebda_seg();
626 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
627 SET_EBDA2(ebda_seg, boot_sequence, seq);
631 // INT 19h Boot Load Service Entry Point
635 debug_serial_setup();
636 debug_enter(NULL, DEBUG_HDL_19);
637 SET_EBDA(boot_sequence, 0);