1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
|
// Code to load disk image and start system boot.
//
// Copyright (C) 2008-2013 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2002 MandrakeSoft S.A.
//
// This file may be distributed under the terms of the GNU LGPLv3 license.
#include "block.h" // struct drive_s
#include "bregs.h" // struct bregs
#include "config.h" // CONFIG_*
#include "fw/paravirt.h" // qemu_cfg_show_boot_menu
#include "hw/pci.h" // pci_bdf_to_*
#include "hw/pcidevice.h" // struct pci_device
#include "hw/rtc.h" // rtc_read
#include "hw/usb.h" // struct usbdevice_s
#include "list.h" // hlist_node
#include "malloc.h" // free
#include "output.h" // dprintf
#include "romfile.h" // romfile_loadint
#include "std/disk.h" // struct mbr_s
#include "string.h" // memset
#include "util.h" // irqtimer_calc
#include "tcgbios.h" // tpm_*
/****************************************************************
* Helper search functions
****************************************************************/
// See if 'str' starts with 'glob' - if glob contains an '*' character
// it will match any number of characters in str that aren't a '/' or
// the next glob character.
static char *
glob_prefix(const char *glob, const char *str)
{
for (;;) {
if (!*glob && (!*str || *str == '/'))
return (char*)str;
if (*glob == '*') {
if (!*str || *str == '/' || *str == glob[1])
glob++;
else
str++;
continue;
}
if (*glob != *str)
return NULL;
glob++;
str++;
}
}
#define FW_PCI_DOMAIN "/pci@i0cf8"
static char *
build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
{
// Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
char *p = buf;
if (pci->parent) {
p = build_pci_path(p, max, "pci-bridge", pci->parent);
} else {
p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
if (pci->rootbus)
p += snprintf(p, buf+max-p, ",%x", pci->rootbus);
}
int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
if (fn)
p += snprintf(p, buf+max-p, ",%x", fn);
return p;
}
static char *
build_scsi_path(char *buf, int max,
struct pci_device *pci, int target, int lun)
{
// Build the string path of a scsi drive - for example:
// /pci@i0cf8/scsi@5/channel@0/disk@1,0
char *p;
p = build_pci_path(buf, max, "*", pci);
p += snprintf(p, buf+max-p, "/*@0/*@%x,%x", target, lun);
return p;
}
static char *
build_ata_path(char *buf, int max,
struct pci_device *pci, int chanid, int slave)
{
// Build the string path of an ata drive - for example:
// /pci@i0cf8/ide@1,1/drive@1/disk@0
char *p;
p = build_pci_path(buf, max, "*", pci);
p += snprintf(p, buf+max-p, "/drive@%x/disk@%x", chanid, slave);
return p;
}
/****************************************************************
* Boot device logical geometry
****************************************************************/
typedef struct BootDeviceLCHS {
char *name;
u32 lcyls;
u32 lheads;
u32 lsecs;
} BootDeviceLCHS;
static BootDeviceLCHS *BiosGeometry VARVERIFY32INIT;
static int BiosGeometryCount;
static char *
parse_u32(char *cur, u32 *n)
{
u32 m = 0;
if (cur) {
while ('0' <= *cur && *cur <= '9') {
m = 10 * m + (*cur - '0');
cur++;
}
if (*cur != '\0')
cur++;
}
*n = m;
return cur;
}
static void
loadBiosGeometry(void)
{
if (!CONFIG_HOST_BIOS_GEOMETRY)
return;
char *f = romfile_loadfile("bios-geometry", NULL);
if (!f)
return;
int i = 0;
BiosGeometryCount = 1;
while (f[i]) {
if (f[i] == '\n')
BiosGeometryCount++;
i++;
}
BiosGeometry = malloc_tmphigh(BiosGeometryCount * sizeof(BootDeviceLCHS));
if (!BiosGeometry) {
warn_noalloc();
free(f);
BiosGeometryCount = 0;
return;
}
dprintf(1, "bios geometry:\n");
i = 0;
do {
BootDeviceLCHS *d = &BiosGeometry[i];
d->name = f;
f = strchr(f, '\n');
if (f)
*(f++) = '\0';
char *chs_values = strchr(d->name, ' ');
if (chs_values)
*(chs_values++) = '\0';
chs_values = parse_u32(chs_values, &d->lcyls);
chs_values = parse_u32(chs_values, &d->lheads);
chs_values = parse_u32(chs_values, &d->lsecs);
dprintf(1, "%s: (%u, %u, %u)\n",
d->name, d->lcyls, d->lheads, d->lsecs);
i++;
} while (f);
}
// Search the bios-geometry list for the given glob pattern.
static BootDeviceLCHS *
boot_lchs_find(const char *glob)
{
dprintf(1, "Searching bios-geometry for: %s\n", glob);
int i;
for (i = 0; i < BiosGeometryCount; i++)
if (glob_prefix(glob, BiosGeometry[i].name))
return &BiosGeometry[i];
return NULL;
}
int boot_lchs_find_pci_device(struct pci_device *pci, struct chs_s *chs)
{
if (!CONFIG_HOST_BIOS_GEOMETRY)
return -1;
char desc[256];
build_pci_path(desc, sizeof(desc), "*", pci);
BootDeviceLCHS *b = boot_lchs_find(desc);
if (!b)
return -1;
chs->cylinder = (u16)b->lcyls;
chs->head = (u16)b->lheads;
chs->sector = (u16)b->lsecs;
return 0;
}
int boot_lchs_find_scsi_device(struct pci_device *pci, int target, int lun,
struct chs_s *chs)
{
if (!CONFIG_HOST_BIOS_GEOMETRY)
return -1;
if (!pci)
// support only pci machine for now
return -1;
// Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
char desc[256];
build_scsi_path(desc, sizeof(desc), pci, target, lun);
BootDeviceLCHS *b = boot_lchs_find(desc);
if (!b)
return -1;
chs->cylinder = (u16)b->lcyls;
chs->head = (u16)b->lheads;
chs->sector = (u16)b->lsecs;
return 0;
}
int boot_lchs_find_ata_device(struct pci_device *pci, int chanid, int slave,
struct chs_s *chs)
{
if (!CONFIG_HOST_BIOS_GEOMETRY)
return -1;
if (!pci)
// support only pci machine for now
return -1;
// Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
char desc[256];
build_ata_path(desc, sizeof(desc), pci, chanid, slave);
BootDeviceLCHS *b = boot_lchs_find(desc);
if (!b)
return -1;
chs->cylinder = (u16)b->lcyls;
chs->head = (u16)b->lheads;
chs->sector = (u16)b->lsecs;
return 0;
}
/****************************************************************
* Boot priority ordering
****************************************************************/
static char **Bootorder VARVERIFY32INIT;
static int BootorderCount;
static void
loadBootOrder(void)
{
if (!CONFIG_BOOTORDER)
return;
char *f = romfile_loadfile("bootorder", NULL);
if (!f)
return;
int i = 0;
BootorderCount = 1;
while (f[i]) {
if (f[i] == '\n')
BootorderCount++;
i++;
}
Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
if (!Bootorder) {
warn_noalloc();
free(f);
BootorderCount = 0;
return;
}
dprintf(1, "boot order:\n");
i = 0;
do {
Bootorder[i] = f;
f = strchr(f, '\n');
if (f)
*(f++) = '\0';
Bootorder[i] = nullTrailingSpace(Bootorder[i]);
dprintf(1, "%d: %s\n", i+1, Bootorder[i]);
i++;
} while (f);
}
// Search the bootorder list for the given glob pattern.
static int
find_prio(const char *glob)
{
dprintf(1, "Searching bootorder for: %s\n", glob);
int i;
for (i = 0; i < BootorderCount; i++)
if (glob_prefix(glob, Bootorder[i]))
return i+1;
return -1;
}
u8 is_bootprio_strict(void)
{
static int prio_halt = -2;
if (prio_halt == -2)
prio_halt = find_prio("HALT");
return prio_halt >= 0;
}
int bootprio_find_pci_device(struct pci_device *pci)
{
if (CONFIG_CSM)
return csm_bootprio_pci(pci);
if (!CONFIG_BOOTORDER)
return -1;
// Find pci device - for example: /pci@i0cf8/ethernet@5
char desc[256];
build_pci_path(desc, sizeof(desc), "*", pci);
return find_prio(desc);
}
int bootprio_find_mmio_device(void *mmio)
{
if (!CONFIG_BOOTORDER)
return -1;
char desc[256];
snprintf(desc, sizeof(desc), "/virtio-mmio@%016x/*", (u32)mmio);
return find_prio(desc);
}
int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
{
if (!CONFIG_BOOTORDER)
return -1;
if (!pci)
// support only pci machine for now
return -1;
char desc[256];
build_scsi_path(desc, sizeof(desc), pci, target, lun);
return find_prio(desc);
}
int bootprio_find_scsi_mmio_device(void *mmio, int target, int lun)
{
if (!CONFIG_BOOTORDER)
return -1;
char desc[256];
snprintf(desc, sizeof(desc), "/virtio-mmio@%016x/*@0/*@%x,%x",
(u32)mmio, target, lun);
return find_prio(desc);
}
int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
{
if (CONFIG_CSM)
return csm_bootprio_ata(pci, chanid, slave);
if (!CONFIG_BOOTORDER)
return -1;
if (!pci)
// support only pci machine for now
return -1;
char desc[256];
build_ata_path(desc, sizeof(desc), pci, chanid, slave);
return find_prio(desc);
}
int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
{
if (CONFIG_CSM)
return csm_bootprio_fdc(pci, port, fdid);
if (!CONFIG_BOOTORDER)
return -1;
if (!pci)
// support only pci machine for now
return -1;
// Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
char desc[256], *p;
p = build_pci_path(desc, sizeof(desc), "isa", pci);
snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
return find_prio(desc);
}
int bootprio_find_pci_rom(struct pci_device *pci, int instance)
{
if (!CONFIG_BOOTORDER)
return -1;
// Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
char desc[256], *p;
p = build_pci_path(desc, sizeof(desc), "*", pci);
if (instance)
snprintf(p, desc+sizeof(desc)-p, ":rom%x", instance);
return find_prio(desc);
}
int bootprio_find_named_rom(const char *name, int instance)
{
if (!CONFIG_BOOTORDER)
return -1;
// Find named rom - for example: /rom@genroms/linuxboot.bin
char desc[256], *p;
p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
if (instance)
snprintf(p, desc+sizeof(desc)-p, ":rom%x", instance);
return find_prio(desc);
}
static int usb_portmap(struct usbdevice_s *usbdev)
{
if (usbdev->hub->op->portmap)
return usbdev->hub->op->portmap(usbdev->hub, usbdev->port);
return usbdev->port + 1;
}
static char *
build_usb_path(char *buf, int max, struct usbhub_s *hub)
{
if (!hub->usbdev)
// Root hub - nothing to add.
return buf;
char *p = build_usb_path(buf, max, hub->usbdev->hub);
p += snprintf(p, buf+max-p, "/hub@%x", usb_portmap(hub->usbdev));
return p;
}
int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
{
if (!CONFIG_BOOTORDER)
return -1;
// Find usb - examples:
// pci: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
// mmio: /sysbus-xhci@00000000fe900000/storage@1/channel@0/disk@0,0
char desc[256], *p;
if (usbdev->hub->cntl->pci)
p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
else if (usbdev->hub->cntl->mmio)
p = desc + snprintf(desc, sizeof(desc), "/*@%016x"
, (u32)usbdev->hub->cntl->mmio);
else
return -1;
p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%x"
, usb_portmap(usbdev), lun);
int ret = find_prio(desc);
if (ret >= 0)
return ret;
// Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usb_portmap(usbdev));
return find_prio(desc);
}
/****************************************************************
* Boot setup
****************************************************************/
static int BootRetryTime;
static int CheckFloppySig = 1;
#define DEFAULT_PRIO 9999
static int DefaultFloppyPrio = 101;
static int DefaultCDPrio = 102;
static int DefaultHDPrio = 103;
static int DefaultBEVPrio = 104;
void
boot_init(void)
{
if (! CONFIG_BOOT)
return;
if (CONFIG_QEMU) {
// On emulators, get boot order from nvram.
if (rtc_read(CMOS_BIOS_BOOTFLAG1) & 1)
CheckFloppySig = 0;
u32 bootorder = (rtc_read(CMOS_BIOS_BOOTFLAG2)
| ((rtc_read(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
= DefaultBEVPrio = DEFAULT_PRIO;
int i;
for (i=101; i<104; i++) {
u32 val = bootorder & 0x0f;
bootorder >>= 4;
switch (val) {
case 1: DefaultFloppyPrio = i; break;
case 2: DefaultHDPrio = i; break;
case 3: DefaultCDPrio = i; break;
case 4: DefaultBEVPrio = i; break;
}
}
}
BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000);
loadBootOrder();
loadBiosGeometry();
}
/****************************************************************
* BootList handling
****************************************************************/
struct bootentry_s {
int type;
union {
u32 data;
struct segoff_s vector;
struct drive_s *drive;
};
int priority;
const char *description;
struct hlist_node node;
};
static struct hlist_head BootList VARVERIFY32INIT;
#define IPL_TYPE_FLOPPY 0x01
#define IPL_TYPE_HARDDISK 0x02
#define IPL_TYPE_CDROM 0x03
#define IPL_TYPE_CBFS 0x20
#define IPL_TYPE_BEV 0x80
#define IPL_TYPE_BCV 0x81
#define IPL_TYPE_HALT 0xf0
static void
bootentry_add(int type, int prio, u32 data, const char *desc)
{
if (! CONFIG_BOOT)
return;
struct bootentry_s *be = malloc_tmp(sizeof(*be));
if (!be) {
warn_noalloc();
return;
}
be->type = type;
be->priority = prio;
be->data = data;
be->description = desc ?: "?";
dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
, be->description, type, prio, data);
// Add entry in sorted order.
struct hlist_node **pprev;
struct bootentry_s *pos;
hlist_for_each_entry_pprev(pos, pprev, &BootList, node) {
if (be->priority < pos->priority)
break;
if (be->priority > pos->priority)
continue;
if (be->type < pos->type)
break;
if (be->type > pos->type)
continue;
if (be->type <= IPL_TYPE_CDROM
&& (be->drive->type < pos->drive->type
|| (be->drive->type == pos->drive->type
&& be->drive->cntl_id < pos->drive->cntl_id)))
break;
}
hlist_add(&be->node, pprev);
}
// Return the given priority if it's set - defaultprio otherwise.
static inline int defPrio(int priority, int defaultprio) {
return (priority < 0) ? defaultprio : priority;
}
// Add a BEV vector for a given pnp compatible option rom.
void
boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
{
bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
, SEGOFF(seg, bev).segoff
, desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
DefaultBEVPrio = DEFAULT_PRIO;
}
// Add a bcv entry for an expansion card harddrive or legacy option rom
void
boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
{
bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
, SEGOFF(seg, ip).segoff
, desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
}
void
boot_add_floppy(struct drive_s *drive, const char *desc, int prio)
{
bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
, (u32)drive, desc);
}
void
boot_add_hd(struct drive_s *drive, const char *desc, int prio)
{
bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
, (u32)drive, desc);
}
void
boot_add_cd(struct drive_s *drive, const char *desc, int prio)
{
if (GET_GLOBAL(PlatformRunningOn) & PF_QEMU) {
// We want short boot times. But on physical hardware even
// the test unit ready can take several seconds. So do media
// access on qemu only, where we know it will be fast.
char *extra = cdrom_media_info(drive);
if (extra) {
desc = znprintf(MAXDESCSIZE, "%s (%s)", desc, extra);
free(extra);
}
}
bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
, (u32)drive, desc);
}
// Add a CBFS payload entry
void
boot_add_cbfs(void *data, const char *desc, int prio)
{
bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
}
/****************************************************************
* Keyboard calls
****************************************************************/
// See if a keystroke is pending in the keyboard buffer.
static int
check_for_keystroke(void)
{
struct bregs br;
memset(&br, 0, sizeof(br));
br.flags = F_IF|F_ZF;
br.ah = 1;
call16_int(0x16, &br);
return !(br.flags & F_ZF);
}
// Return a keystroke - waiting forever if necessary.
static int
get_raw_keystroke(void)
{
struct bregs br;
memset(&br, 0, sizeof(br));
br.flags = F_IF;
call16_int(0x16, &br);
return br.ax;
}
// Read a keystroke - waiting up to 'msec' milliseconds.
// returns both scancode and ascii code.
int
get_keystroke_full(int msec)
{
u32 end = irqtimer_calc(msec);
for (;;) {
if (check_for_keystroke())
return get_raw_keystroke();
if (irqtimer_check(end))
return -1;
yield_toirq();
}
}
// Read a keystroke - waiting up to 'msec' milliseconds.
// returns scancode only.
int
get_keystroke(int msec)
{
int keystroke = get_keystroke_full(msec);
if (keystroke < 0)
return keystroke;
return keystroke >> 8;
}
/****************************************************************
* Boot menu and BCV execution
****************************************************************/
#define DEFAULT_BOOTMENU_WAIT 2500
static const char menuchars[] = {
'1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', /* skip t (tpm menu) */
'u', 'v', 'w', 'x', 'y', 'z'
};
// Show IPL option menu.
void
interactive_bootmenu(void)
{
if (! CONFIG_BOOTMENU)
return;
int show_boot_menu = romfile_loadint("etc/show-boot-menu", 1);
if (!show_boot_menu)
return;
// skip menu if only one boot device and no TPM
if (show_boot_menu == 2 && !tpm_can_show_menu()
&& !hlist_empty(&BootList) && !BootList.first->next) {
dprintf(1, "Only one boot device present. Skip boot menu.\n");
printf("\n");
return;
}
while (get_keystroke(0) >= 0)
;
char *bootmsg = romfile_loadfile("etc/boot-menu-message", NULL);
int menukey = romfile_loadint("etc/boot-menu-key", 1);
printf("%s", bootmsg ?: "\nPress ESC for boot menu.\n\n");
free(bootmsg);
u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
enable_bootsplash();
int scan_code = get_keystroke(menutime);
disable_bootsplash();
if (scan_code != menukey)
return;
while (get_keystroke(0) >= 0)
;
printf("Select boot device:\n\n");
wait_threads();
// Show menu items
int maxmenu = 0;
struct bootentry_s *pos, *boot = NULL;
hlist_for_each_entry(pos, &BootList, node) {
char desc[77];
if (maxmenu >= ARRAY_SIZE(menuchars)) {
break;
}
printf("%c. %s\n", menuchars[maxmenu]
, strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
maxmenu++;
}
if (tpm_can_show_menu()) {
printf("\nt. TPM Configuration\n");
}
// Get key press. If the menu key is ESC, do not restart boot unless
// 1.5 seconds have passed. Otherwise users (trained by years of
// repeatedly hitting keys to enter the BIOS) will end up hitting ESC
// multiple times and immediately booting the primary boot device.
int esc_accepted_time = irqtimer_calc(menukey == 1 ? 1500 : 0);
for (;;) {
int keystroke = get_keystroke_full(1000);
if (keystroke == 0x011b && !irqtimer_check(esc_accepted_time))
continue;
if (keystroke < 0) // timeout
continue;
scan_code = keystroke >> 8;
int key_ascii = keystroke & 0xff;
if (tpm_can_show_menu() && key_ascii == 't') {
printf("\n");
tpm_menu();
}
if (scan_code == 1) {
// ESC
printf("\n");
return;
}
maxmenu = 0;
hlist_for_each_entry(pos, &BootList, node) {
if (maxmenu >= ARRAY_SIZE(menuchars))
break;
if (key_ascii == menuchars[maxmenu]) {
boot = pos;
break;
}
maxmenu++;
}
if (boot)
break;
}
printf("\n");
// Find entry and make top priority.
hlist_del(&boot->node);
boot->priority = 0;
hlist_add_head(&boot->node, &BootList);
}
// BEV (Boot Execution Vector) list
struct bev_s {
int type;
u32 vector;
};
static struct bev_s BEV[20];
static int BEVCount;
static int HaveHDBoot, HaveFDBoot;
static void
add_bev(int type, u32 vector)
{
if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
return;
if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
return;
if (BEVCount >= ARRAY_SIZE(BEV))
return;
struct bev_s *bev = &BEV[BEVCount++];
bev->type = type;
bev->vector = vector;
}
// Prepare for boot - show menu and run bcvs.
void
bcv_prepboot(void)
{
if (! CONFIG_BOOT)
return;
int haltprio = find_prio("HALT");
if (haltprio >= 0)
bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
// Map drives and populate BEV list
struct bootentry_s *pos;
hlist_for_each_entry(pos, &BootList, node) {
switch (pos->type) {
case IPL_TYPE_BCV:
call_bcv(pos->vector.seg, pos->vector.offset);
add_bev(IPL_TYPE_HARDDISK, 0);
break;
case IPL_TYPE_FLOPPY:
map_floppy_drive(pos->drive);
add_bev(IPL_TYPE_FLOPPY, 0);
break;
case IPL_TYPE_HARDDISK:
map_hd_drive(pos->drive);
add_bev(IPL_TYPE_HARDDISK, 0);
break;
case IPL_TYPE_CDROM:
map_cd_drive(pos->drive);
// NO BREAK
default:
add_bev(pos->type, pos->data);
break;
}
}
// If nothing added a floppy/hd boot - add it manually.
add_bev(IPL_TYPE_FLOPPY, 0);
add_bev(IPL_TYPE_HARDDISK, 0);
}
/****************************************************************
* Boot code (int 18/19)
****************************************************************/
// Jump to a bootup entry point.
static void
call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
{
dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
struct bregs br;
memset(&br, 0, sizeof(br));
br.flags = F_IF;
br.code = bootsegip;
// Set the magic number in ax and the boot drive in dl.
br.dl = bootdrv;
br.ax = 0xaa55;
farcall16(&br);
}
// Boot from a disk (either floppy or harddrive)
static void
boot_disk(u8 bootdrv, int checksig)
{
u16 bootseg = 0x07c0;
// Read sector
struct bregs br;
memset(&br, 0, sizeof(br));
br.flags = F_IF;
br.dl = bootdrv;
br.es = bootseg;
br.ah = 2;
br.al = 1;
br.cl = 1;
call16_int(0x13, &br);
if (br.flags & F_CF) {
printf("Boot failed: could not read the boot disk\n\n");
return;
}
if (checksig) {
struct mbr_s *mbr = (void*)0;
if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
printf("Boot failed: not a bootable disk\n\n");
return;
}
}
tpm_add_bcv(bootdrv, MAKE_FLATPTR(bootseg, 0), 512);
/* Canonicalize bootseg:bootip */
u16 bootip = (bootseg & 0x0fff) << 4;
bootseg &= 0xf000;
call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
}
// Boot from a CD-ROM
static void
boot_cdrom(struct drive_s *drive)
{
if (! CONFIG_CDROM_BOOT)
return;
printf("Booting from DVD/CD...\n");
int status = cdrom_boot(drive);
if (status) {
printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
return;
}
u8 bootdrv = CDEmu.emulated_drive;
u16 bootseg = CDEmu.load_segment;
tpm_add_cdrom(bootdrv, MAKE_FLATPTR(bootseg, 0), 512);
/* Canonicalize bootseg:bootip */
u16 bootip = (bootseg & 0x0fff) << 4;
bootseg &= 0xf000;
call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
}
// Boot from a CBFS payload
static void
boot_cbfs(struct cbfs_file *file)
{
if (!CONFIG_COREBOOT_FLASH)
return;
printf("Booting from CBFS...\n");
cbfs_run_payload(file);
}
// Boot from a BEV entry on an optionrom.
static void
boot_rom(u32 vector)
{
printf("Booting from ROM...\n");
struct segoff_s so;
so.segoff = vector;
call_boot_entry(so, 0);
}
// Unable to find bootable device - warn user and eventually retry.
static void
boot_fail(void)
{
if (BootRetryTime == (u32)-1)
printf("No bootable device.\n");
else
printf("No bootable device. Retrying in %d seconds.\n"
, BootRetryTime/1000);
// Wait for 'BootRetryTime' milliseconds and then reboot.
u32 end = irqtimer_calc(BootRetryTime);
for (;;) {
if (BootRetryTime != (u32)-1 && irqtimer_check(end))
break;
yield_toirq();
}
printf("Rebooting.\n");
reset();
}
// Determine next boot method and attempt a boot using it.
static void
do_boot(int seq_nr)
{
if (! CONFIG_BOOT)
panic("Boot support not compiled in.\n");
if (seq_nr >= BEVCount)
boot_fail();
// Boot the given BEV type.
struct bev_s *ie = &BEV[seq_nr];
switch (ie->type) {
case IPL_TYPE_FLOPPY:
printf("Booting from Floppy...\n");
boot_disk(0x00, CheckFloppySig);
break;
case IPL_TYPE_HARDDISK:
printf("Booting from Hard Disk...\n");
boot_disk(0x80, 1);
break;
case IPL_TYPE_CDROM:
boot_cdrom((void*)ie->vector);
break;
case IPL_TYPE_CBFS:
boot_cbfs((void*)ie->vector);
break;
case IPL_TYPE_BEV:
boot_rom(ie->vector);
break;
case IPL_TYPE_HALT:
boot_fail();
break;
}
// Boot failed: invoke the boot recovery function
struct bregs br;
memset(&br, 0, sizeof(br));
br.flags = F_IF;
call16_int(0x18, &br);
}
int BootSequence VARLOW = -1;
// Boot Failure recovery: try the next device.
void VISIBLE32FLAT
handle_18(void)
{
debug_enter(NULL, DEBUG_HDL_18);
int seq = BootSequence + 1;
BootSequence = seq;
do_boot(seq);
}
// INT 19h Boot Load Service Entry Point
void VISIBLE32FLAT
handle_19(void)
{
debug_enter(NULL, DEBUG_HDL_19);
BootSequence = 0;
do_boot(0);
}
|