1
// Code to load disk image and start system boot.
3
// Copyright (C) 2008-2013 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 "block.h" // struct drive_s
9
#include "bregs.h" // struct bregs
10
#include "config.h" // CONFIG_*
11
#include "fw/paravirt.h" // qemu_cfg_show_boot_menu
12
#include "hw/pci.h" // pci_bdf_to_*
13
#include "hw/pcidevice.h" // struct pci_device
14
#include "hw/rtc.h" // rtc_read
15
#include "hw/usb.h" // struct usbdevice_s
16
#include "list.h" // hlist_node
17
#include "malloc.h" // free
18
#include "output.h" // dprintf
19
#include "romfile.h" // romfile_loadint
20
#include "std/disk.h" // struct mbr_s
21
#include "string.h" // memset
22
#include "util.h" // irqtimer_calc
23
#include "tcgbios.h" // tpm_*
26
/****************************************************************
27
* Boot priority ordering
28
****************************************************************/
30
static char **Bootorder VARVERIFY32INIT;
31
static int BootorderCount;
36
if (!CONFIG_BOOTORDER)
39
char *f = romfile_loadfile("bootorder", NULL);
50
Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
58
dprintf(1, "boot order:\n");
65
Bootorder[i] = nullTrailingSpace(Bootorder[i]);
66
dprintf(1, "%d: %s\n", i+1, Bootorder[i]);
71
// See if 'str' starts with 'glob' - if glob contains an '*' character
72
// it will match any number of characters in str that aren't a '/' or
73
// the next glob character.
75
glob_prefix(const char *glob, const char *str)
78
if (!*glob && (!*str || *str == '/'))
81
if (!*str || *str == '/' || *str == glob[1])
94
// Search the bootorder list for the given glob pattern.
96
find_prio(const char *glob)
98
dprintf(1, "Searching bootorder for: %s\n", glob);
100
for (i = 0; i < BootorderCount; i++)
101
if (glob_prefix(glob, Bootorder[i]))
106
#define FW_PCI_DOMAIN "/pci@i0cf8"
109
build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
111
// Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
114
p = build_pci_path(p, max, "pci-bridge", pci->parent);
116
p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
118
p += snprintf(p, buf+max-p, ",%x", pci->rootbus);
121
int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
122
p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
124
p += snprintf(p, buf+max-p, ",%x", fn);
128
int bootprio_find_pci_device(struct pci_device *pci)
131
return csm_bootprio_pci(pci);
132
if (!CONFIG_BOOTORDER)
134
// Find pci device - for example: /pci@i0cf8/ethernet@5
136
build_pci_path(desc, sizeof(desc), "*", pci);
137
return find_prio(desc);
140
int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
142
if (!CONFIG_BOOTORDER)
145
// support only pci machine for now
147
// Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
149
p = build_pci_path(desc, sizeof(desc), "*", pci);
150
snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%x,%x", target, lun);
151
return find_prio(desc);
154
int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
157
return csm_bootprio_ata(pci, chanid, slave);
158
if (!CONFIG_BOOTORDER)
161
// support only pci machine for now
163
// Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
165
p = build_pci_path(desc, sizeof(desc), "*", pci);
166
snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
167
return find_prio(desc);
170
int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
173
return csm_bootprio_fdc(pci, port, fdid);
174
if (!CONFIG_BOOTORDER)
177
// support only pci machine for now
179
// Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
181
p = build_pci_path(desc, sizeof(desc), "isa", pci);
182
snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
183
return find_prio(desc);
186
int bootprio_find_pci_rom(struct pci_device *pci, int instance)
188
if (!CONFIG_BOOTORDER)
190
// Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
192
p = build_pci_path(desc, sizeof(desc), "*", pci);
194
snprintf(p, desc+sizeof(desc)-p, ":rom%x", instance);
195
return find_prio(desc);
198
int bootprio_find_named_rom(const char *name, int instance)
200
if (!CONFIG_BOOTORDER)
202
// Find named rom - for example: /rom@genroms/linuxboot.bin
204
p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
206
snprintf(p, desc+sizeof(desc)-p, ":rom%x", instance);
207
return find_prio(desc);
211
build_usb_path(char *buf, int max, struct usbhub_s *hub)
214
// Root hub - nothing to add.
216
char *p = build_usb_path(buf, max, hub->usbdev->hub);
217
p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
221
int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
223
if (!CONFIG_BOOTORDER)
225
// Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
227
p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
228
p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
229
snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%x"
230
, usbdev->port+1, lun);
231
int ret = find_prio(desc);
234
// Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
235
snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
236
return find_prio(desc);
240
/****************************************************************
242
****************************************************************/
244
static int BootRetryTime;
245
static int CheckFloppySig = 1;
247
#define DEFAULT_PRIO 9999
249
static int DefaultFloppyPrio = 101;
250
static int DefaultCDPrio = 102;
251
static int DefaultHDPrio = 103;
252
static int DefaultBEVPrio = 104;
261
// On emulators, get boot order from nvram.
262
if (rtc_read(CMOS_BIOS_BOOTFLAG1) & 1)
264
u32 bootorder = (rtc_read(CMOS_BIOS_BOOTFLAG2)
265
| ((rtc_read(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
266
DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
267
= DefaultBEVPrio = DEFAULT_PRIO;
269
for (i=101; i<104; i++) {
270
u32 val = bootorder & 0x0f;
273
case 1: DefaultFloppyPrio = i; break;
274
case 2: DefaultHDPrio = i; break;
275
case 3: DefaultCDPrio = i; break;
276
case 4: DefaultBEVPrio = i; break;
281
BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000);
287
/****************************************************************
289
****************************************************************/
295
struct segoff_s vector;
296
struct drive_s *drive;
299
const char *description;
300
struct hlist_node node;
302
static struct hlist_head BootList VARVERIFY32INIT;
304
#define IPL_TYPE_FLOPPY 0x01
305
#define IPL_TYPE_HARDDISK 0x02
306
#define IPL_TYPE_CDROM 0x03
307
#define IPL_TYPE_CBFS 0x20
308
#define IPL_TYPE_BEV 0x80
309
#define IPL_TYPE_BCV 0x81
310
#define IPL_TYPE_HALT 0xf0
313
bootentry_add(int type, int prio, u32 data, const char *desc)
317
struct bootentry_s *be = malloc_tmp(sizeof(*be));
325
be->description = desc ?: "?";
326
dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
327
, be->description, type, prio, data);
329
// Add entry in sorted order.
330
struct hlist_node **pprev;
331
struct bootentry_s *pos;
332
hlist_for_each_entry_pprev(pos, pprev, &BootList, node) {
333
if (be->priority < pos->priority)
335
if (be->priority > pos->priority)
337
if (be->type < pos->type)
339
if (be->type > pos->type)
341
if (be->type <= IPL_TYPE_CDROM
342
&& (be->drive->type < pos->drive->type
343
|| (be->drive->type == pos->drive->type
344
&& be->drive->cntl_id < pos->drive->cntl_id)))
347
hlist_add(&be->node, pprev);
350
// Return the given priority if it's set - defaultprio otherwise.
351
static inline int defPrio(int priority, int defaultprio) {
352
return (priority < 0) ? defaultprio : priority;
355
// Add a BEV vector for a given pnp compatible option rom.
357
boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
359
bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
360
, SEGOFF(seg, bev).segoff
361
, desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
362
DefaultBEVPrio = DEFAULT_PRIO;
365
// Add a bcv entry for an expansion card harddrive or legacy option rom
367
boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
369
bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
370
, SEGOFF(seg, ip).segoff
371
, desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
375
boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
377
bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
378
, (u32)drive_g, desc);
382
boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
384
bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
385
, (u32)drive_g, desc);
389
boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
391
bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
392
, (u32)drive_g, desc);
395
// Add a CBFS payload entry
397
boot_add_cbfs(void *data, const char *desc, int prio)
399
bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
403
/****************************************************************
405
****************************************************************/
407
// See if a keystroke is pending in the keyboard buffer.
409
check_for_keystroke(void)
412
memset(&br, 0, sizeof(br));
413
br.flags = F_IF|F_ZF;
415
call16_int(0x16, &br);
416
return !(br.flags & F_ZF);
419
// Return a keystroke - waiting forever if necessary.
421
get_raw_keystroke(void)
424
memset(&br, 0, sizeof(br));
426
call16_int(0x16, &br);
430
// Read a keystroke - waiting up to 'msec' milliseconds.
432
get_keystroke(int msec)
434
u32 end = irqtimer_calc(msec);
436
if (check_for_keystroke())
437
return get_raw_keystroke();
438
if (irqtimer_check(end))
445
/****************************************************************
446
* Boot menu and BCV execution
447
****************************************************************/
449
#define DEFAULT_BOOTMENU_WAIT 2500
451
// Show IPL option menu.
453
interactive_bootmenu(void)
455
// XXX - show available drives?
457
if (! CONFIG_BOOTMENU || !romfile_loadint("etc/show-boot-menu", 1))
460
while (get_keystroke(0) >= 0)
463
char *bootmsg = romfile_loadfile("etc/boot-menu-message", NULL);
464
int menukey = romfile_loadint("etc/boot-menu-key", 1);
465
printf("%s", bootmsg ?: "\nPress ESC for boot menu.\n\n");
468
u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
470
int scan_code = get_keystroke(menutime);
471
disable_bootsplash();
472
if (scan_code != menukey)
475
while (get_keystroke(0) >= 0)
478
printf("Select boot device:\n\n");
483
struct bootentry_s *pos;
484
hlist_for_each_entry(pos, &BootList, node) {
487
printf("%d. %s\n", maxmenu
488
, strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
490
if (tpm_can_show_menu()) {
491
printf("\nt. TPM Configuration\n");
494
// Get key press. If the menu key is ESC, do not restart boot unless
495
// 1.5 seconds have passed. Otherwise users (trained by years of
496
// repeatedly hitting keys to enter the BIOS) will end up hitting ESC
497
// multiple times and immediately booting the primary boot device.
498
int esc_accepted_time = irqtimer_calc(menukey == 1 ? 1500 : 0);
500
scan_code = get_keystroke(1000);
501
if (scan_code == 1 && !irqtimer_check(esc_accepted_time))
503
if (tpm_can_show_menu() && scan_code == 20 /* t */) {
507
if (scan_code >= 1 && scan_code <= maxmenu+1)
511
if (scan_code == 0x01)
515
// Find entry and make top priority.
516
int choice = scan_code - 1;
517
hlist_for_each_entry(pos, &BootList, node) {
521
hlist_del(&pos->node);
523
hlist_add_head(&pos->node, &BootList);
526
// BEV (Boot Execution Vector) list
531
static struct bev_s BEV[20];
533
static int HaveHDBoot, HaveFDBoot;
536
add_bev(int type, u32 vector)
538
if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
540
if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
542
if (BEVCount >= ARRAY_SIZE(BEV))
544
struct bev_s *bev = &BEV[BEVCount++];
546
bev->vector = vector;
549
// Prepare for boot - show menu and run bcvs.
556
int haltprio = find_prio("HALT");
558
bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
560
// Map drives and populate BEV list
561
struct bootentry_s *pos;
562
hlist_for_each_entry(pos, &BootList, node) {
565
call_bcv(pos->vector.seg, pos->vector.offset);
566
add_bev(IPL_TYPE_HARDDISK, 0);
568
case IPL_TYPE_FLOPPY:
569
map_floppy_drive(pos->drive);
570
add_bev(IPL_TYPE_FLOPPY, 0);
572
case IPL_TYPE_HARDDISK:
573
map_hd_drive(pos->drive);
574
add_bev(IPL_TYPE_HARDDISK, 0);
577
map_cd_drive(pos->drive);
580
add_bev(pos->type, pos->data);
585
// If nothing added a floppy/hd boot - add it manually.
586
add_bev(IPL_TYPE_FLOPPY, 0);
587
add_bev(IPL_TYPE_HARDDISK, 0);
591
/****************************************************************
592
* Boot code (int 18/19)
593
****************************************************************/
595
// Jump to a bootup entry point.
597
call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
599
dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
601
memset(&br, 0, sizeof(br));
604
// Set the magic number in ax and the boot drive in dl.
610
// Boot from a disk (either floppy or harddrive)
612
boot_disk(u8 bootdrv, int checksig)
614
u16 bootseg = 0x07c0;
618
memset(&br, 0, sizeof(br));
625
call16_int(0x13, &br);
627
if (br.flags & F_CF) {
628
printf("Boot failed: could not read the boot disk\n\n");
633
struct mbr_s *mbr = (void*)0;
634
if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
635
printf("Boot failed: not a bootable disk\n\n");
640
tpm_add_bcv(bootdrv, MAKE_FLATPTR(bootseg, 0), 512);
642
/* Canonicalize bootseg:bootip */
643
u16 bootip = (bootseg & 0x0fff) << 4;
646
call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
649
// Boot from a CD-ROM
651
boot_cdrom(struct drive_s *drive_g)
653
if (! CONFIG_CDROM_BOOT)
655
printf("Booting from DVD/CD...\n");
657
int status = cdrom_boot(drive_g);
659
printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
663
u8 bootdrv = CDEmu.emulated_drive;
664
u16 bootseg = CDEmu.load_segment;
666
tpm_add_cdrom(bootdrv, MAKE_FLATPTR(bootseg, 0), 512);
668
/* Canonicalize bootseg:bootip */
669
u16 bootip = (bootseg & 0x0fff) << 4;
672
call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
675
// Boot from a CBFS payload
677
boot_cbfs(struct cbfs_file *file)
679
if (!CONFIG_COREBOOT_FLASH)
681
printf("Booting from CBFS...\n");
682
cbfs_run_payload(file);
685
// Boot from a BEV entry on an optionrom.
689
printf("Booting from ROM...\n");
692
call_boot_entry(so, 0);
695
// Unable to find bootable device - warn user and eventually retry.
699
if (BootRetryTime == (u32)-1)
700
printf("No bootable device.\n");
702
printf("No bootable device. Retrying in %d seconds.\n"
703
, BootRetryTime/1000);
704
// Wait for 'BootRetryTime' milliseconds and then reboot.
705
u32 end = irqtimer_calc(BootRetryTime);
707
if (BootRetryTime != (u32)-1 && irqtimer_check(end))
711
printf("Rebooting.\n");
715
// Determine next boot method and attempt a boot using it.
720
panic("Boot support not compiled in.\n");
722
if (seq_nr >= BEVCount)
725
// Boot the given BEV type.
726
struct bev_s *ie = &BEV[seq_nr];
728
case IPL_TYPE_FLOPPY:
729
printf("Booting from Floppy...\n");
730
boot_disk(0x00, CheckFloppySig);
732
case IPL_TYPE_HARDDISK:
733
printf("Booting from Hard Disk...\n");
737
boot_cdrom((void*)ie->vector);
740
boot_cbfs((void*)ie->vector);
743
boot_rom(ie->vector);
750
// Boot failed: invoke the boot recovery function
752
memset(&br, 0, sizeof(br));
754
call16_int(0x18, &br);
757
int BootSequence VARLOW = -1;
759
// Boot Failure recovery: try the next device.
763
debug_enter(NULL, DEBUG_HDL_18);
764
int seq = BootSequence + 1;
769
// INT 19h Boot Load Service Entry Point
773
debug_enter(NULL, DEBUG_HDL_19);