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*));
48 dprintf(3, "boot order:\n");
55 nullTrailingSpace(Bootorder[i]);
56 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
61 // See if 'str' starts with 'glob' - if glob contains an '*' character
62 // it will match any number of characters in str that aren't a '/' or
63 // the next glob character.
65 glob_prefix(const char *glob, const char *str)
68 if (!*glob && (!*str || *str == '/'))
71 if (!*str || *str == '/' || *str == glob[1])
84 // Search the bootorder list for the given glob pattern.
86 find_prio(const char *glob)
88 dprintf(1, "Searching bootorder for: %s\n", glob);
90 for (i = 0; i < BootorderCount; i++)
91 if (glob_prefix(glob, Bootorder[i]))
96 #define FW_PCI_DOMAIN "/pci@i0cf8"
99 build_pci_path(char *buf, int max, const char *devname, int bdf)
101 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
103 int parent = pci_bdf_to_bus(bdf);
105 parent = PCIpaths[parent];
106 int parentdev = parent & 0xffff;
107 if (parent & PP_PCIBRIDGE) {
108 p = build_pci_path(p, max, "pci-bridge", parentdev);
111 p += snprintf(p, max, "/pci-root@%x", parentdev);
112 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
115 int dev = pci_bdf_to_dev(bdf), fn = pci_bdf_to_fn(bdf);
116 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
118 p += snprintf(p, buf+max-p, ",%x", fn);
122 int bootprio_find_pci_device(int bdf)
124 // Find pci device - for example: /pci@i0cf8/ethernet@5
126 build_pci_path(desc, sizeof(desc), "*", bdf);
127 return find_prio(desc);
130 int bootprio_find_ata_device(int bdf, int chanid, int slave)
133 // support only pci machine for now
135 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
137 p = build_pci_path(desc, sizeof(desc), "*", bdf);
138 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
139 return find_prio(desc);
142 int bootprio_find_fdc_device(int bdf, int port, int fdid)
145 // support only pci machine for now
147 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
149 p = build_pci_path(desc, sizeof(desc), "isa", bdf);
150 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
151 return find_prio(desc);
154 int bootprio_find_pci_rom(int bdf, int instance)
156 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
158 p = build_pci_path(desc, sizeof(desc), "*", bdf);
160 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
161 return find_prio(desc);
164 int bootprio_find_named_rom(const char *name, int instance)
166 // Find named rom - for example: /rom@genroms/linuxboot.bin
168 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
170 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
171 return find_prio(desc);
174 int bootprio_find_usb(int bdf, u64 path)
176 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
179 p = build_pci_path(desc, sizeof(desc), "usb", bdf);
180 for (i=56; i>0; i-=8) {
181 int port = (path >> i) & 0xff;
183 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
185 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
186 return find_prio(desc);
190 /****************************************************************
192 ****************************************************************/
194 static int CheckFloppySig = 1;
196 #define DEFAULT_PRIO 9999
198 static int DefaultFloppyPrio = 101;
199 static int DefaultCDPrio = 102;
200 static int DefaultHDPrio = 103;
201 static int DefaultBEVPrio = 104;
209 SET_EBDA(boot_sequence, 0xffff);
211 if (!CONFIG_COREBOOT) {
212 // On emulators, get boot order from nvram.
213 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
215 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
216 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
217 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
218 = DefaultBEVPrio = DEFAULT_PRIO;
220 for (i=101; i<104; i++) {
221 u32 val = bootorder & 0x0f;
224 case 1: DefaultFloppyPrio = i; break;
225 case 2: DefaultHDPrio = i; break;
226 case 3: DefaultCDPrio = i; break;
227 case 4: DefaultBEVPrio = i; break;
236 /****************************************************************
238 ****************************************************************/
244 struct segoff_s vector;
245 struct drive_s *drive;
248 const char *description;
249 struct bootentry_s *next;
251 static struct bootentry_s *BootList;
253 #define IPL_TYPE_FLOPPY 0x01
254 #define IPL_TYPE_HARDDISK 0x02
255 #define IPL_TYPE_CDROM 0x03
256 #define IPL_TYPE_CBFS 0x20
257 #define IPL_TYPE_BEV 0x80
258 #define IPL_TYPE_BCV 0x81
261 bootentry_add(int type, int prio, u32 data, const char *desc)
265 struct bootentry_s *be = malloc_tmp(sizeof(*be));
273 be->description = desc ?: "?";
274 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
275 , be->description, type, prio, data);
277 // Add entry in sorted order.
278 struct bootentry_s **pprev;
279 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
280 struct bootentry_s *pos = *pprev;
281 if (be->priority < pos->priority)
283 if (be->priority > pos->priority)
285 if (be->type < pos->type)
287 if (be->type > pos->type)
289 if (be->type <= IPL_TYPE_CDROM
290 && (be->drive->type < pos->drive->type
291 || (be->drive->type == pos->drive->type
292 && be->drive->cntl_id < pos->drive->cntl_id)))
299 // Return the given priority if it's set - defaultprio otherwise.
300 static inline int defPrio(int priority, int defaultprio) {
301 return (priority < 0) ? defaultprio : priority;
304 // Add a BEV vector for a given pnp compatible option rom.
306 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
308 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
309 , SEGOFF(seg, bev).segoff
310 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
311 DefaultBEVPrio = DEFAULT_PRIO;
314 // Add a bcv entry for an expansion card harddrive or legacy option rom
316 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
318 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
319 , SEGOFF(seg, ip).segoff
320 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
324 boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
326 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
327 , (u32)drive_g, desc);
331 boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
333 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
334 , (u32)drive_g, desc);
338 boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
340 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
341 , (u32)drive_g, desc);
344 // Add a CBFS payload entry
346 boot_add_cbfs(void *data, const char *desc, int prio)
348 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
352 /****************************************************************
353 * Boot menu and BCV execution
354 ****************************************************************/
356 // Show IPL option menu.
358 interactive_bootmenu(void)
360 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
363 while (get_keystroke(0) >= 0)
366 printf("Press F12 for boot menu.\n\n");
369 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
370 disable_bootsplash();
371 if (scan_code != 0x86)
375 while (get_keystroke(0) >= 0)
378 printf("Select boot device:\n\n");
382 struct bootentry_s *pos = BootList;
387 printf("%d. %s\n", maxmenu
388 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
394 scan_code = get_keystroke(1000);
395 if (scan_code >= 1 && scan_code <= maxmenu+1)
399 if (scan_code == 0x01)
403 // Find entry and make top priority.
404 int choice = scan_code - 1;
405 struct bootentry_s **pprev = &BootList;
407 pprev = &(*pprev)->next;
410 pos->next = BootList;
415 // BEV (Boot Execution Vector) list
420 static struct bev_s BEV[20];
422 static int HaveHDBoot, HaveFDBoot;
425 add_bev(int type, u32 vector)
427 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
429 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
431 if (BEVCount >= ARRAY_SIZE(BEV))
433 struct bev_s *bev = &BEV[BEVCount++];
435 bev->vector = vector;
438 // Prepare for boot - show menu and run bcvs.
447 // XXX - show available drives?
449 // Allow user to modify BCV/IPL order.
450 interactive_bootmenu();
453 // Map drives and populate BEV list
454 struct bootentry_s *pos = BootList;
458 call_bcv(pos->vector.seg, pos->vector.offset);
459 add_bev(IPL_TYPE_HARDDISK, 0);
461 case IPL_TYPE_FLOPPY:
462 map_floppy_drive(pos->drive);
463 add_bev(IPL_TYPE_FLOPPY, 0);
465 case IPL_TYPE_HARDDISK:
466 map_hd_drive(pos->drive);
467 add_bev(IPL_TYPE_HARDDISK, 0);
470 map_cd_drive(pos->drive);
473 add_bev(pos->type, pos->data);
479 // If nothing added a floppy/hd boot - add it manually.
480 add_bev(IPL_TYPE_FLOPPY, 0);
481 add_bev(IPL_TYPE_HARDDISK, 0);
485 /****************************************************************
486 * Boot code (int 18/19)
487 ****************************************************************/
489 // Jump to a bootup entry point.
491 call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
493 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
495 memset(&br, 0, sizeof(br));
498 // Set the magic number in ax and the boot drive in dl.
504 // Boot from a disk (either floppy or harddrive)
506 boot_disk(u8 bootdrv, int checksig)
508 u16 bootseg = 0x07c0;
512 memset(&br, 0, sizeof(br));
519 call16_int(0x13, &br);
521 if (br.flags & F_CF) {
522 printf("Boot failed: could not read the boot disk\n\n");
527 struct mbr_s *mbr = (void*)0;
528 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
529 printf("Boot failed: not a bootable disk\n\n");
534 /* Canonicalize bootseg:bootip */
535 u16 bootip = (bootseg & 0x0fff) << 4;
538 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
541 // Boot from a CD-ROM
543 boot_cdrom(struct drive_s *drive_g)
545 if (! CONFIG_CDROM_BOOT)
547 printf("Booting from DVD/CD...\n");
549 int status = cdrom_boot(drive_g);
551 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
555 u16 ebda_seg = get_ebda_seg();
556 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
557 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
558 /* Canonicalize bootseg:bootip */
559 u16 bootip = (bootseg & 0x0fff) << 4;
562 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
565 // Boot from a CBFS payload
567 boot_cbfs(struct cbfs_file *file)
569 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
571 printf("Booting from CBFS...\n");
572 cbfs_run_payload(file);
575 // Boot from a BEV entry on an optionrom.
579 printf("Booting from ROM...\n");
582 call_boot_entry(so, 0);
585 // Determine next boot method and attempt a boot using it.
590 panic("Boot support not compiled in.\n");
592 if (seq_nr >= BEVCount) {
593 printf("No bootable device.\n");
594 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
599 // Boot the given BEV type.
600 struct bev_s *ie = &BEV[seq_nr];
602 case IPL_TYPE_FLOPPY:
603 printf("Booting from Floppy...\n");
604 boot_disk(0x00, CheckFloppySig);
606 case IPL_TYPE_HARDDISK:
607 printf("Booting from Hard Disk...\n");
611 boot_cdrom((void*)ie->vector);
614 boot_cbfs((void*)ie->vector);
617 boot_rom(ie->vector);
621 // Boot failed: invoke the boot recovery function
623 memset(&br, 0, sizeof(br));
625 call16_int(0x18, &br);
628 // Boot Failure recovery: try the next device.
632 debug_serial_setup();
633 debug_enter(NULL, DEBUG_HDL_18);
634 u16 ebda_seg = get_ebda_seg();
635 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
636 SET_EBDA2(ebda_seg, boot_sequence, seq);
640 // INT 19h Boot Load Service Entry Point
644 debug_serial_setup();
645 debug_enter(NULL, DEBUG_HDL_19);
646 SET_EBDA(boot_sequence, 0);