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 "config.h" // CONFIG_*
10
#include "disk.h" // cdrom_boot
11
#include "bregs.h" // struct bregs
12
#include "boot.h" // func defs
13
#include "cmos.h" // inb_cmos
14
#include "paravirt.h" // qemu_cfg_show_boot_menu
15
#include "pci.h" // pci_bdf_to_*
16
#include "usb.h" // struct usbdevice_s
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_scsi_device(struct pci_device *pci, int target, int lun)
133
if (!CONFIG_BOOTORDER)
136
// support only pci machine for now
138
// Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
140
p = build_pci_path(desc, sizeof(desc), "*", pci);
141
snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%d,%d", target, lun);
142
return find_prio(desc);
145
int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
147
if (!CONFIG_BOOTORDER)
150
// support only pci machine for now
152
// Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
154
p = build_pci_path(desc, sizeof(desc), "*", pci);
155
snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
156
return find_prio(desc);
159
int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
161
if (!CONFIG_BOOTORDER)
164
// support only pci machine for now
166
// Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
168
p = build_pci_path(desc, sizeof(desc), "isa", pci);
169
snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
170
return find_prio(desc);
173
int bootprio_find_pci_rom(struct pci_device *pci, int instance)
175
if (!CONFIG_BOOTORDER)
177
// Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
179
p = build_pci_path(desc, sizeof(desc), "*", pci);
181
snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
182
return find_prio(desc);
185
int bootprio_find_named_rom(const char *name, int instance)
187
if (!CONFIG_BOOTORDER)
189
// Find named rom - for example: /rom@genroms/linuxboot.bin
191
p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
193
snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
194
return find_prio(desc);
198
build_usb_path(char *buf, int max, struct usbhub_s *hub)
201
// Root hub - nothing to add.
203
char *p = build_usb_path(buf, max, hub->usbdev->hub);
204
p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
208
int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
210
if (!CONFIG_BOOTORDER)
212
// Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
214
p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
215
p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
216
snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%d"
217
, usbdev->port+1, lun);
218
int ret = find_prio(desc);
221
// Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
222
snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
223
return find_prio(desc);
227
/****************************************************************
229
****************************************************************/
231
static int CheckFloppySig = 1;
233
#define DEFAULT_PRIO 9999
235
static int DefaultFloppyPrio = 101;
236
static int DefaultCDPrio = 102;
237
static int DefaultHDPrio = 103;
238
static int DefaultBEVPrio = 104;
246
if (!CONFIG_COREBOOT) {
247
// On emulators, get boot order from nvram.
248
if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
250
u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
251
| ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
252
DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
253
= DefaultBEVPrio = DEFAULT_PRIO;
255
for (i=101; i<104; i++) {
256
u32 val = bootorder & 0x0f;
259
case 1: DefaultFloppyPrio = i; break;
260
case 2: DefaultHDPrio = i; break;
261
case 3: DefaultCDPrio = i; break;
262
case 4: DefaultBEVPrio = i; break;
271
/****************************************************************
273
****************************************************************/
279
struct segoff_s vector;
280
struct drive_s *drive;
283
const char *description;
284
struct bootentry_s *next;
286
static struct bootentry_s *BootList;
288
#define IPL_TYPE_FLOPPY 0x01
289
#define IPL_TYPE_HARDDISK 0x02
290
#define IPL_TYPE_CDROM 0x03
291
#define IPL_TYPE_CBFS 0x20
292
#define IPL_TYPE_BEV 0x80
293
#define IPL_TYPE_BCV 0x81
294
#define IPL_TYPE_HALT 0xf0
297
bootentry_add(int type, int prio, u32 data, const char *desc)
301
struct bootentry_s *be = malloc_tmp(sizeof(*be));
309
be->description = desc ?: "?";
310
dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
311
, be->description, type, prio, data);
313
// Add entry in sorted order.
314
struct bootentry_s **pprev;
315
for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
316
struct bootentry_s *pos = *pprev;
317
if (be->priority < pos->priority)
319
if (be->priority > pos->priority)
321
if (be->type < pos->type)
323
if (be->type > pos->type)
325
if (be->type <= IPL_TYPE_CDROM
326
&& (be->drive->type < pos->drive->type
327
|| (be->drive->type == pos->drive->type
328
&& be->drive->cntl_id < pos->drive->cntl_id)))
335
// Return the given priority if it's set - defaultprio otherwise.
336
static inline int defPrio(int priority, int defaultprio) {
337
return (priority < 0) ? defaultprio : priority;
340
// Add a BEV vector for a given pnp compatible option rom.
342
boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
344
bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
345
, SEGOFF(seg, bev).segoff
346
, desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
347
DefaultBEVPrio = DEFAULT_PRIO;
350
// Add a bcv entry for an expansion card harddrive or legacy option rom
352
boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
354
bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
355
, SEGOFF(seg, ip).segoff
356
, desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
360
boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
362
bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
363
, (u32)drive_g, desc);
367
boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
369
bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
370
, (u32)drive_g, desc);
374
boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
376
bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
377
, (u32)drive_g, desc);
380
// Add a CBFS payload entry
382
boot_add_cbfs(void *data, const char *desc, int prio)
384
bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
388
/****************************************************************
389
* Boot menu and BCV execution
390
****************************************************************/
392
#define DEFAULT_BOOTMENU_WAIT 2500
394
// Show IPL option menu.
396
interactive_bootmenu(void)
398
if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
401
while (get_keystroke(0) >= 0)
404
printf("\nPress F12 for boot menu.\n\n");
406
u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
408
int scan_code = get_keystroke(menutime);
409
disable_bootsplash();
410
if (scan_code != 0x86)
414
while (get_keystroke(0) >= 0)
417
printf("Select boot device:\n\n");
421
struct bootentry_s *pos = BootList;
426
printf("%d. %s\n", maxmenu
427
, strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
433
scan_code = get_keystroke(1000);
434
if (scan_code >= 1 && scan_code <= maxmenu+1)
438
if (scan_code == 0x01)
442
// Find entry and make top priority.
443
int choice = scan_code - 1;
444
struct bootentry_s **pprev = &BootList;
446
pprev = &(*pprev)->next;
449
pos->next = BootList;
454
// BEV (Boot Execution Vector) list
459
static struct bev_s BEV[20];
461
static int HaveHDBoot, HaveFDBoot;
464
add_bev(int type, u32 vector)
466
if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
468
if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
470
if (BEVCount >= ARRAY_SIZE(BEV))
472
struct bev_s *bev = &BEV[BEVCount++];
474
bev->vector = vector;
477
// Prepare for boot - show menu and run bcvs.
486
// XXX - show available drives?
488
// Allow user to modify BCV/IPL order.
489
interactive_bootmenu();
492
int haltprio = find_prio("HALT");
494
bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
496
// Map drives and populate BEV list
497
struct bootentry_s *pos = BootList;
501
call_bcv(pos->vector.seg, pos->vector.offset);
502
add_bev(IPL_TYPE_HARDDISK, 0);
504
case IPL_TYPE_FLOPPY:
505
map_floppy_drive(pos->drive);
506
add_bev(IPL_TYPE_FLOPPY, 0);
508
case IPL_TYPE_HARDDISK:
509
map_hd_drive(pos->drive);
510
add_bev(IPL_TYPE_HARDDISK, 0);
513
map_cd_drive(pos->drive);
516
add_bev(pos->type, pos->data);
522
// If nothing added a floppy/hd boot - add it manually.
523
add_bev(IPL_TYPE_FLOPPY, 0);
524
add_bev(IPL_TYPE_HARDDISK, 0);
528
/****************************************************************
529
* Boot code (int 18/19)
530
****************************************************************/
532
// Jump to a bootup entry point.
534
call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
536
dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
538
memset(&br, 0, sizeof(br));
541
// Set the magic number in ax and the boot drive in dl.
547
// Boot from a disk (either floppy or harddrive)
549
boot_disk(u8 bootdrv, int checksig)
551
u16 bootseg = 0x07c0;
555
memset(&br, 0, sizeof(br));
562
call16_int(0x13, &br);
564
if (br.flags & F_CF) {
565
printf("Boot failed: could not read the boot disk\n\n");
570
struct mbr_s *mbr = (void*)0;
571
if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
572
printf("Boot failed: not a bootable disk\n\n");
577
/* Canonicalize bootseg:bootip */
578
u16 bootip = (bootseg & 0x0fff) << 4;
581
call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
584
// Boot from a CD-ROM
586
boot_cdrom(struct drive_s *drive_g)
588
if (! CONFIG_CDROM_BOOT)
590
printf("Booting from DVD/CD...\n");
592
int status = cdrom_boot(drive_g);
594
printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
598
u8 bootdrv = CDEmu.emulated_extdrive;
599
u16 bootseg = CDEmu.load_segment;
600
/* Canonicalize bootseg:bootip */
601
u16 bootip = (bootseg & 0x0fff) << 4;
604
call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
607
// Boot from a CBFS payload
609
boot_cbfs(struct cbfs_file *file)
611
if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
613
printf("Booting from CBFS...\n");
614
cbfs_run_payload(file);
617
// Boot from a BEV entry on an optionrom.
621
printf("Booting from ROM...\n");
624
call_boot_entry(so, 0);
627
// Unable to find bootable device - warn user and eventually retry.
631
u32 retrytime = romfile_loadint("etc/boot-fail-wait", 60*1000);
632
if (retrytime == (u32)-1)
633
printf("No bootable device.\n");
635
printf("No bootable device. Retrying in %d seconds.\n", retrytime/1000);
636
// Wait for 'retrytime' milliseconds and then reboot.
637
u32 end = calc_future_timer(retrytime);
639
if (retrytime != (u32)-1 && check_timer(end))
643
printf("Rebooting.\n");
645
memset(&br, 0, sizeof(br));
646
br.code = SEGOFF(SEG_BIOS, (u32)reset_vector);
650
// Determine next boot method and attempt a boot using it.
655
panic("Boot support not compiled in.\n");
657
if (seq_nr >= BEVCount)
660
// Boot the given BEV type.
661
struct bev_s *ie = &BEV[seq_nr];
663
case IPL_TYPE_FLOPPY:
664
printf("Booting from Floppy...\n");
665
boot_disk(0x00, CheckFloppySig);
667
case IPL_TYPE_HARDDISK:
668
printf("Booting from Hard Disk...\n");
672
boot_cdrom((void*)ie->vector);
675
boot_cbfs((void*)ie->vector);
678
boot_rom(ie->vector);
685
// Boot failed: invoke the boot recovery function
687
memset(&br, 0, sizeof(br));
689
call16_int(0x18, &br);
692
int BootSequence VARLOW = -1;
694
// Boot Failure recovery: try the next device.
698
debug_serial_setup();
699
debug_enter(NULL, DEBUG_HDL_18);
700
int seq = BootSequence + 1;
705
// INT 19h Boot Load Service Entry Point
709
debug_serial_setup();
710
debug_enter(NULL, DEBUG_HDL_19);