1 /*- 2 * Copyright (c) 1998 Robert Nordier 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are freely 6 * permitted provided that the above copyright notice and this 7 * paragraph and the following disclaimer are duplicated in all 8 * such forms. 9 * 10 * This software is provided "AS IS" and without any express or 11 * implied warranties, including, without limitation, the implied 12 * warranties of merchantability and fitness for a particular 13 * purpose. 14 */ 15 16 #include <sys/cdefs.h> 17 __FBSDID("$FreeBSD$"); 18 19 #include "stand.h" 20 21 #include <sys/param.h> 22 #include <sys/errno.h> 23 #include <sys/diskmbr.h> 24 #ifdef GPT 25 #include <sys/gpt.h> 26 #endif 27 #include <sys/reboot.h> 28 #include <sys/queue.h> 29 30 #include <machine/bootinfo.h> 31 #include <machine/elf.h> 32 #include <machine/pc/bios.h> 33 34 #include <stdarg.h> 35 #include <stddef.h> 36 37 #include <a.out.h> 38 39 #include <btxv86.h> 40 41 #include "lib.h" 42 #include "rbx.h" 43 #include "drv.h" 44 #include "edd.h" 45 #include "cons.h" 46 #include "bootargs.h" 47 #include "paths.h" 48 49 #include "libzfs.h" 50 51 #define ARGS 0x900 52 #define NOPT 14 53 #define NDEV 3 54 55 #define BIOS_NUMDRIVES 0x475 56 #define DRV_HARD 0x80 57 #define DRV_MASK 0x7f 58 59 #define TYPE_AD 0 60 #define TYPE_DA 1 61 #define TYPE_MAXHARD TYPE_DA 62 #define TYPE_FD 2 63 64 #define DEV_GELIBOOT_BSIZE 4096 65 66 extern uint32_t _end; 67 68 #ifdef GPT 69 static const uuid_t freebsd_zfs_uuid = GPT_ENT_TYPE_FREEBSD_ZFS; 70 #endif 71 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */ 72 static const unsigned char flags[NOPT] = { 73 RBX_DUAL, 74 RBX_SERIAL, 75 RBX_ASKNAME, 76 RBX_CDROM, 77 RBX_CONFIG, 78 RBX_KDB, 79 RBX_GDB, 80 RBX_MUTE, 81 RBX_NOINTR, 82 RBX_PAUSE, 83 RBX_QUIET, 84 RBX_DFLTROOT, 85 RBX_SINGLE, 86 RBX_VERBOSE 87 }; 88 uint32_t opts; 89 90 static const unsigned char dev_maj[NDEV] = {30, 4, 2}; 91 92 static char cmd[512]; 93 static char cmddup[512]; 94 static char kname[1024]; 95 static char rootname[256]; 96 static int comspeed = SIOSPD; 97 static struct bootinfo bootinfo; 98 static uint32_t bootdev; 99 static struct zfs_boot_args zfsargs; 100 101 vm_offset_t high_heap_base; 102 uint32_t bios_basemem, bios_extmem, high_heap_size; 103 104 static struct bios_smap smap; 105 106 /* 107 * The minimum amount of memory to reserve in bios_extmem for the heap. 108 */ 109 #define HEAP_MIN (64 * 1024 * 1024) 110 111 static char *heap_next; 112 static char *heap_end; 113 114 /* Buffers that must not span a 64k boundary. */ 115 #define READ_BUF_SIZE 8192 116 struct dmadat { 117 char rdbuf[READ_BUF_SIZE]; /* for reading large things */ 118 char secbuf[READ_BUF_SIZE]; /* for MBR/disklabel */ 119 }; 120 static struct dmadat *dmadat; 121 122 void exit(int); 123 void reboot(void); 124 static void load(void); 125 static int parse_cmd(void); 126 static void bios_getmem(void); 127 int main(void); 128 129 #ifdef LOADER_GELI_SUPPORT 130 #include "geliboot.h" 131 static char gelipw[GELI_PW_MAXLEN]; 132 static struct keybuf *gelibuf; 133 #endif 134 135 struct zfsdsk { 136 struct dsk dsk; 137 #ifdef LOADER_GELI_SUPPORT 138 struct geli_dev *gdev; 139 #endif 140 }; 141 142 #include "zfsimpl.c" 143 144 /* 145 * Read from a dnode (which must be from a ZPL filesystem). 146 */ 147 static int 148 zfs_read(spa_t *spa, const dnode_phys_t *dnode, off_t *offp, void *start, size_t size) 149 { 150 const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus; 151 size_t n; 152 int rc; 153 154 n = size; 155 if (*offp + n > zp->zp_size) 156 n = zp->zp_size - *offp; 157 158 rc = dnode_read(spa, dnode, *offp, start, n); 159 if (rc) 160 return (-1); 161 *offp += n; 162 163 return (n); 164 } 165 166 /* 167 * Current ZFS pool 168 */ 169 static spa_t *spa; 170 static spa_t *primary_spa; 171 static vdev_t *primary_vdev; 172 173 /* 174 * A wrapper for dskread that doesn't have to worry about whether the 175 * buffer pointer crosses a 64k boundary. 176 */ 177 static int 178 vdev_read(void *xvdev, void *priv, off_t off, void *buf, size_t bytes) 179 { 180 char *p; 181 daddr_t lba, alignlba; 182 off_t diff; 183 unsigned int nb, alignnb; 184 struct zfsdsk *zdsk = (struct zfsdsk *) priv; 185 186 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1))) 187 return -1; 188 189 p = buf; 190 lba = off / DEV_BSIZE; 191 lba += zdsk->dsk.start; 192 /* 193 * Align reads to 4k else 4k sector GELIs will not decrypt. 194 * Round LBA down to nearest multiple of DEV_GELIBOOT_BSIZE bytes. 195 */ 196 alignlba = rounddown2(off, DEV_GELIBOOT_BSIZE) / DEV_BSIZE; 197 /* 198 * The read must be aligned to DEV_GELIBOOT_BSIZE bytes relative to the 199 * start of the GELI partition, not the start of the actual disk. 200 */ 201 alignlba += zdsk->dsk.start; 202 diff = (lba - alignlba) * DEV_BSIZE; 203 204 while (bytes > 0) { 205 nb = bytes / DEV_BSIZE; 206 /* 207 * Ensure that the read size plus the leading offset does not 208 * exceed the size of the read buffer. 209 */ 210 if (nb > (READ_BUF_SIZE - diff) / DEV_BSIZE) 211 nb = (READ_BUF_SIZE - diff) / DEV_BSIZE; 212 /* 213 * Round the number of blocks to read up to the nearest multiple 214 * of DEV_GELIBOOT_BSIZE. 215 */ 216 alignnb = roundup2(nb * DEV_BSIZE + diff, DEV_GELIBOOT_BSIZE) 217 / DEV_BSIZE; 218 219 if (zdsk->dsk.size > 0 && alignlba + alignnb > 220 zdsk->dsk.size + zdsk->dsk.start) { 221 printf("Shortening read at %lld from %d to %lld\n", 222 alignlba, alignnb, 223 (zdsk->dsk.size + zdsk->dsk.start) - alignlba); 224 alignnb = (zdsk->dsk.size + zdsk->dsk.start) - alignlba; 225 } 226 227 if (drvread(&zdsk->dsk, dmadat->rdbuf, alignlba, alignnb)) 228 return -1; 229 #ifdef LOADER_GELI_SUPPORT 230 /* decrypt */ 231 if (zdsk->gdev != NULL) { 232 if (geli_read(zdsk->gdev, ((alignlba - zdsk->dsk.start) * 233 DEV_BSIZE), dmadat->rdbuf, alignnb * DEV_BSIZE)) 234 return (-1); 235 } 236 #endif 237 memcpy(p, dmadat->rdbuf + diff, nb * DEV_BSIZE); 238 p += nb * DEV_BSIZE; 239 lba += nb; 240 alignlba += alignnb; 241 bytes -= nb * DEV_BSIZE; 242 /* Don't need the leading offset after the first block. */ 243 diff = 0; 244 } 245 246 return 0; 247 } 248 /* Match the signature exactly due to signature madness */ 249 static int 250 vdev_read2(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes) 251 { 252 return vdev_read(vdev, priv, off, buf, bytes); 253 } 254 255 256 static int 257 vdev_write(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes) 258 { 259 char *p; 260 daddr_t lba; 261 unsigned int nb; 262 struct zfsdsk *zdsk = (struct zfsdsk *) priv; 263 264 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1))) 265 return -1; 266 267 p = buf; 268 lba = off / DEV_BSIZE; 269 lba += zdsk->dsk.start; 270 while (bytes > 0) { 271 nb = bytes / DEV_BSIZE; 272 if (nb > READ_BUF_SIZE / DEV_BSIZE) 273 nb = READ_BUF_SIZE / DEV_BSIZE; 274 memcpy(dmadat->rdbuf, p, nb * DEV_BSIZE); 275 if (drvwrite(&zdsk->dsk, dmadat->rdbuf, lba, nb)) 276 return -1; 277 p += nb * DEV_BSIZE; 278 lba += nb; 279 bytes -= nb * DEV_BSIZE; 280 } 281 282 return 0; 283 } 284 285 static int 286 xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte) 287 { 288 if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) { 289 printf("Invalid format\n"); 290 return -1; 291 } 292 return 0; 293 } 294 295 /* 296 * Read Pad2 (formerly "Boot Block Header") area of the first 297 * vdev label of the given vdev. 298 */ 299 static int 300 vdev_read_pad2(vdev_t *vdev, char *buf, size_t size) 301 { 302 blkptr_t bp; 303 char *tmp = zap_scratch; 304 off_t off = offsetof(vdev_label_t, vl_pad2); 305 306 if (size > VDEV_PAD_SIZE) 307 size = VDEV_PAD_SIZE; 308 309 BP_ZERO(&bp); 310 BP_SET_LSIZE(&bp, VDEV_PAD_SIZE); 311 BP_SET_PSIZE(&bp, VDEV_PAD_SIZE); 312 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL); 313 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF); 314 DVA_SET_OFFSET(BP_IDENTITY(&bp), off); 315 if (vdev_read_phys(vdev, &bp, tmp, off, 0)) 316 return (EIO); 317 memcpy(buf, tmp, size); 318 return (0); 319 } 320 321 static int 322 vdev_clear_pad2(vdev_t *vdev) 323 { 324 char *zeroes = zap_scratch; 325 uint64_t *end; 326 off_t off = offsetof(vdev_label_t, vl_pad2); 327 328 memset(zeroes, 0, VDEV_PAD_SIZE); 329 end = (uint64_t *)(zeroes + VDEV_PAD_SIZE); 330 /* ZIO_CHECKSUM_LABEL magic and pre-calcualted checksum for all zeros */ 331 end[-5] = 0x0210da7ab10c7a11; 332 end[-4] = 0x97f48f807f6e2a3f; 333 end[-3] = 0xaf909f1658aacefc; 334 end[-2] = 0xcbd1ea57ff6db48b; 335 end[-1] = 0x6ec692db0d465fab; 336 if (vdev_write(vdev, vdev->v_read_priv, off, zeroes, VDEV_PAD_SIZE)) 337 return (EIO); 338 return (0); 339 } 340 341 static void 342 bios_getmem(void) 343 { 344 uint64_t size; 345 346 /* Parse system memory map */ 347 v86.ebx = 0; 348 do { 349 v86.ctl = V86_FLAGS; 350 v86.addr = 0x15; /* int 0x15 function 0xe820*/ 351 v86.eax = 0xe820; 352 v86.ecx = sizeof(struct bios_smap); 353 v86.edx = SMAP_SIG; 354 v86.es = VTOPSEG(&smap); 355 v86.edi = VTOPOFF(&smap); 356 v86int(); 357 if (V86_CY(v86.efl) || (v86.eax != SMAP_SIG)) 358 break; 359 /* look for a low-memory segment that's large enough */ 360 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) && 361 (smap.length >= (512 * 1024))) 362 bios_basemem = smap.length; 363 /* look for the first segment in 'extended' memory */ 364 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000)) { 365 bios_extmem = smap.length; 366 } 367 368 /* 369 * Look for the largest segment in 'extended' memory beyond 370 * 1MB but below 4GB. 371 */ 372 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) && 373 (smap.base < 0x100000000ull)) { 374 size = smap.length; 375 376 /* 377 * If this segment crosses the 4GB boundary, truncate it. 378 */ 379 if (smap.base + size > 0x100000000ull) 380 size = 0x100000000ull - smap.base; 381 382 if (size > high_heap_size) { 383 high_heap_size = size; 384 high_heap_base = smap.base; 385 } 386 } 387 } while (v86.ebx != 0); 388 389 /* Fall back to the old compatibility function for base memory */ 390 if (bios_basemem == 0) { 391 v86.ctl = 0; 392 v86.addr = 0x12; /* int 0x12 */ 393 v86int(); 394 395 bios_basemem = (v86.eax & 0xffff) * 1024; 396 } 397 398 /* Fall back through several compatibility functions for extended memory */ 399 if (bios_extmem == 0) { 400 v86.ctl = V86_FLAGS; 401 v86.addr = 0x15; /* int 0x15 function 0xe801*/ 402 v86.eax = 0xe801; 403 v86int(); 404 if (!V86_CY(v86.efl)) { 405 bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024; 406 } 407 } 408 if (bios_extmem == 0) { 409 v86.ctl = 0; 410 v86.addr = 0x15; /* int 0x15 function 0x88*/ 411 v86.eax = 0x8800; 412 v86int(); 413 bios_extmem = (v86.eax & 0xffff) * 1024; 414 } 415 416 /* 417 * If we have extended memory and did not find a suitable heap 418 * region in the SMAP, use the last 3MB of 'extended' memory as a 419 * high heap candidate. 420 */ 421 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) { 422 high_heap_size = HEAP_MIN; 423 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN; 424 } 425 } 426 427 /* 428 * Try to detect a device supported by the legacy int13 BIOS 429 */ 430 static int 431 int13probe(int drive) 432 { 433 v86.ctl = V86_FLAGS; 434 v86.addr = 0x13; 435 v86.eax = 0x800; 436 v86.edx = drive; 437 v86int(); 438 439 if (!V86_CY(v86.efl) && /* carry clear */ 440 ((v86.edx & 0xff) != (drive & DRV_MASK))) { /* unit # OK */ 441 if ((v86.ecx & 0x3f) == 0) { /* absurd sector size */ 442 return(0); /* skip device */ 443 } 444 return (1); 445 } 446 return(0); 447 } 448 449 /* 450 * We call this when we find a ZFS vdev - ZFS consumes the dsk 451 * structure so we must make a new one. 452 */ 453 static struct zfsdsk * 454 copy_dsk(struct zfsdsk *zdsk) 455 { 456 struct zfsdsk *newdsk; 457 458 newdsk = malloc(sizeof(struct zfsdsk)); 459 *newdsk = *zdsk; 460 return (newdsk); 461 } 462 463 /* 464 * Get disk size from eax=0x800 and 0x4800. We need to probe both 465 * because 0x4800 may not be available and we would like to get more 466 * or less correct disk size - if it is possible at all. 467 * Note we do not really want to touch drv.c because that code is shared 468 * with boot2 and we can not afford to grow that code. 469 */ 470 static uint64_t 471 drvsize_ext(struct zfsdsk *zdsk) 472 { 473 struct dsk *dskp; 474 uint64_t size, tmp; 475 int cyl, hds, sec; 476 477 dskp = &zdsk->dsk; 478 479 v86.ctl = V86_FLAGS; 480 v86.addr = 0x13; 481 v86.eax = 0x800; 482 v86.edx = dskp->drive; 483 v86int(); 484 485 /* Don't error out if we get bad sector number, try EDD as well */ 486 if (V86_CY(v86.efl) || /* carry set */ 487 (v86.edx & 0xff) <= (unsigned)(dskp->drive & 0x7f)) /* unit # bad */ 488 return (0); 489 cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1; 490 /* Convert max head # -> # of heads */ 491 hds = ((v86.edx & 0xff00) >> 8) + 1; 492 sec = v86.ecx & 0x3f; 493 494 size = (uint64_t)cyl * hds * sec; 495 496 /* Determine if we can use EDD with this device. */ 497 v86.ctl = V86_FLAGS; 498 v86.addr = 0x13; 499 v86.eax = 0x4100; 500 v86.edx = dskp->drive; 501 v86.ebx = 0x55aa; 502 v86int(); 503 if (V86_CY(v86.efl) || /* carry set */ 504 (v86.ebx & 0xffff) != 0xaa55 || /* signature */ 505 (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0) 506 return (size); 507 508 tmp = drvsize(dskp); 509 if (tmp > size) 510 size = tmp; 511 512 return (size); 513 } 514 515 /* 516 * The "layered" ioctl to read disk/partition size. Unfortunately 517 * the zfsboot case is hardest, because we do not have full software 518 * stack available, so we need to do some manual work here. 519 */ 520 uint64_t 521 ldi_get_size(void *priv) 522 { 523 struct zfsdsk *zdsk = priv; 524 uint64_t size = zdsk->dsk.size; 525 526 if (zdsk->dsk.start == 0) 527 size = drvsize_ext(zdsk); 528 529 return (size * DEV_BSIZE); 530 } 531 532 static void 533 probe_drive(struct zfsdsk *zdsk) 534 { 535 #ifdef GPT 536 struct gpt_hdr hdr; 537 struct gpt_ent *ent; 538 unsigned part, entries_per_sec; 539 daddr_t slba; 540 #endif 541 #if defined(GPT) || defined(LOADER_GELI_SUPPORT) 542 daddr_t elba; 543 #endif 544 545 struct dos_partition *dp; 546 char *sec; 547 unsigned i; 548 549 /* 550 * If we find a vdev on the whole disk, stop here. 551 */ 552 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) 553 return; 554 555 #ifdef LOADER_GELI_SUPPORT 556 /* 557 * Taste the disk, if it is GELI encrypted, decrypt it and check to see if 558 * it is a usable vdev then. Otherwise dig 559 * out the partition table and probe each slice/partition 560 * in turn for a vdev or GELI encrypted vdev. 561 */ 562 elba = drvsize_ext(zdsk); 563 if (elba > 0) { 564 elba--; 565 } 566 zdsk->gdev = geli_taste(vdev_read, zdsk, elba, "disk%u:0:"); 567 if (zdsk->gdev != NULL) { 568 if (geli_havekey(zdsk->gdev) == 0 || 569 geli_passphrase(zdsk->gdev, gelipw) == 0) { 570 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) { 571 return; 572 } 573 } 574 } 575 #endif /* LOADER_GELI_SUPPORT */ 576 577 sec = dmadat->secbuf; 578 zdsk->dsk.start = 0; 579 580 #ifdef GPT 581 /* 582 * First check for GPT. 583 */ 584 if (drvread(&zdsk->dsk, sec, 1, 1)) { 585 return; 586 } 587 memcpy(&hdr, sec, sizeof(hdr)); 588 if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 || 589 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 || 590 hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) { 591 goto trymbr; 592 } 593 594 /* 595 * Probe all GPT partitions for the presence of ZFS pools. We 596 * return the spa_t for the first we find (if requested). This 597 * will have the effect of booting from the first pool on the 598 * disk. 599 * 600 * If no vdev is found, GELI decrypting the device and try again 601 */ 602 entries_per_sec = DEV_BSIZE / hdr.hdr_entsz; 603 slba = hdr.hdr_lba_table; 604 elba = slba + hdr.hdr_entries / entries_per_sec; 605 while (slba < elba) { 606 zdsk->dsk.start = 0; 607 if (drvread(&zdsk->dsk, sec, slba, 1)) 608 return; 609 for (part = 0; part < entries_per_sec; part++) { 610 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz); 611 if (memcmp(&ent->ent_type, &freebsd_zfs_uuid, 612 sizeof(uuid_t)) == 0) { 613 zdsk->dsk.start = ent->ent_lba_start; 614 zdsk->dsk.size = ent->ent_lba_end - ent->ent_lba_start + 1; 615 zdsk->dsk.slice = part + 1; 616 zdsk->dsk.part = 255; 617 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) { 618 /* 619 * This slice had a vdev. We need a new dsk 620 * structure now since the vdev now owns this one. 621 */ 622 zdsk = copy_dsk(zdsk); 623 } 624 #ifdef LOADER_GELI_SUPPORT 625 else if ((zdsk->gdev = geli_taste(vdev_read, zdsk, 626 ent->ent_lba_end - ent->ent_lba_start, "disk%up%u:", 627 zdsk->dsk.unit, zdsk->dsk.slice)) != NULL) { 628 if (geli_havekey(zdsk->gdev) == 0 || 629 geli_passphrase(zdsk->gdev, gelipw) == 0) { 630 /* 631 * This slice has GELI, check it for ZFS. 632 */ 633 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) { 634 /* 635 * This slice had a vdev. We need a new dsk 636 * structure now since the vdev now owns this one. 637 */ 638 zdsk = copy_dsk(zdsk); 639 } 640 break; 641 } 642 } 643 #endif /* LOADER_GELI_SUPPORT */ 644 } 645 } 646 slba++; 647 } 648 return; 649 trymbr: 650 #endif /* GPT */ 651 652 if (drvread(&zdsk->dsk, sec, DOSBBSECTOR, 1)) 653 return; 654 dp = (void *)(sec + DOSPARTOFF); 655 656 for (i = 0; i < NDOSPART; i++) { 657 if (!dp[i].dp_typ) 658 continue; 659 zdsk->dsk.start = dp[i].dp_start; 660 zdsk->dsk.size = dp[i].dp_size; 661 zdsk->dsk.slice = i + 1; 662 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) { 663 zdsk = copy_dsk(zdsk); 664 } 665 #ifdef LOADER_GELI_SUPPORT 666 else if ((zdsk->gdev = geli_taste(vdev_read, zdsk, dp[i].dp_size - 667 dp[i].dp_start, "disk%us%u:")) != NULL) { 668 if (geli_havekey(zdsk->gdev) == 0 || 669 geli_passphrase(zdsk->gdev, gelipw) == 0) { 670 /* 671 * This slice has GELI, check it for ZFS. 672 */ 673 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) { 674 /* 675 * This slice had a vdev. We need a new dsk 676 * structure now since the vdev now owns this one. 677 */ 678 zdsk = copy_dsk(zdsk); 679 } 680 break; 681 } 682 } 683 #endif /* LOADER_GELI_SUPPORT */ 684 } 685 } 686 687 int 688 main(void) 689 { 690 dnode_phys_t dn; 691 off_t off; 692 struct zfsdsk *zdsk; 693 int autoboot, i; 694 int nextboot; 695 int rc; 696 697 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base); 698 699 bios_getmem(); 700 701 if (high_heap_size > 0) { 702 heap_end = PTOV(high_heap_base + high_heap_size); 703 heap_next = PTOV(high_heap_base); 704 } else { 705 heap_next = (char *)dmadat + sizeof(*dmadat); 706 heap_end = (char *)PTOV(bios_basemem); 707 } 708 setheap(heap_next, heap_end); 709 710 zdsk = calloc(1, sizeof(struct zfsdsk)); 711 zdsk->dsk.drive = *(uint8_t *)PTOV(ARGS); 712 zdsk->dsk.type = zdsk->dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD; 713 zdsk->dsk.unit = zdsk->dsk.drive & DRV_MASK; 714 zdsk->dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1; 715 zdsk->dsk.part = 0; 716 zdsk->dsk.start = 0; 717 zdsk->dsk.size = drvsize_ext(zdsk); 718 719 bootinfo.bi_version = BOOTINFO_VERSION; 720 bootinfo.bi_size = sizeof(bootinfo); 721 bootinfo.bi_basemem = bios_basemem / 1024; 722 bootinfo.bi_extmem = bios_extmem / 1024; 723 bootinfo.bi_memsizes_valid++; 724 bootinfo.bi_bios_dev = zdsk->dsk.drive; 725 726 bootdev = MAKEBOOTDEV(dev_maj[zdsk->dsk.type], 727 zdsk->dsk.slice, zdsk->dsk.unit, zdsk->dsk.part); 728 729 /* Process configuration file */ 730 731 autoboot = 1; 732 733 zfs_init(); 734 735 /* 736 * Probe the boot drive first - we will try to boot from whatever 737 * pool we find on that drive. 738 */ 739 probe_drive(zdsk); 740 741 /* 742 * Probe the rest of the drives that the bios knows about. This 743 * will find any other available pools and it may fill in missing 744 * vdevs for the boot pool. 745 */ 746 #ifndef VIRTUALBOX 747 for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++) 748 #else 749 for (i = 0; i < MAXBDDEV; i++) 750 #endif 751 { 752 if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS)) 753 continue; 754 755 if (!int13probe(i | DRV_HARD)) 756 break; 757 758 zdsk = calloc(1, sizeof(struct zfsdsk)); 759 zdsk->dsk.drive = i | DRV_HARD; 760 zdsk->dsk.type = zdsk->dsk.drive & TYPE_AD; 761 zdsk->dsk.unit = i; 762 zdsk->dsk.slice = 0; 763 zdsk->dsk.part = 0; 764 zdsk->dsk.start = 0; 765 zdsk->dsk.size = drvsize_ext(zdsk); 766 probe_drive(zdsk); 767 } 768 769 /* 770 * The first discovered pool, if any, is the pool. 771 */ 772 spa = spa_get_primary(); 773 if (!spa) { 774 printf("%s: No ZFS pools located, can't boot\n", BOOTPROG); 775 for (;;) 776 ; 777 } 778 779 primary_spa = spa; 780 primary_vdev = spa_get_primary_vdev(spa); 781 782 nextboot = 0; 783 rc = vdev_read_pad2(primary_vdev, cmd, sizeof(cmd)); 784 if (vdev_clear_pad2(primary_vdev)) 785 printf("failed to clear pad2 area of primary vdev\n"); 786 if (rc == 0) { 787 if (*cmd) { 788 /* 789 * We could find an old-style ZFS Boot Block header here. 790 * Simply ignore it. 791 */ 792 if (*(uint64_t *)cmd != 0x2f5b007b10c) { 793 /* 794 * Note that parse() is destructive to cmd[] and we also want 795 * to honor RBX_QUIET option that could be present in cmd[]. 796 */ 797 nextboot = 1; 798 memcpy(cmddup, cmd, sizeof(cmd)); 799 if (parse_cmd()) { 800 printf("failed to parse pad2 area of primary vdev\n"); 801 reboot(); 802 } 803 if (!OPT_CHECK(RBX_QUIET)) 804 printf("zfs nextboot: %s\n", cmddup); 805 } 806 /* Do not process this command twice */ 807 *cmd = 0; 808 } 809 } else 810 printf("failed to read pad2 area of primary vdev\n"); 811 812 /* Mount ZFS only if it's not already mounted via nextboot parsing. */ 813 if (zfsmount.spa == NULL && 814 (zfs_spa_init(spa) != 0 || zfs_mount(spa, 0, &zfsmount) != 0)) { 815 printf("%s: failed to mount default pool %s\n", 816 BOOTPROG, spa->spa_name); 817 autoboot = 0; 818 } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 || 819 zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) { 820 off = 0; 821 zfs_read(spa, &dn, &off, cmd, sizeof(cmd)); 822 } 823 824 if (*cmd) { 825 /* 826 * Note that parse_cmd() is destructive to cmd[] and we also want 827 * to honor RBX_QUIET option that could be present in cmd[]. 828 */ 829 memcpy(cmddup, cmd, sizeof(cmd)); 830 if (parse_cmd()) 831 autoboot = 0; 832 if (!OPT_CHECK(RBX_QUIET)) 833 printf("%s: %s\n", PATH_CONFIG, cmddup); 834 /* Do not process this command twice */ 835 *cmd = 0; 836 } 837 838 /* Do not risk waiting at the prompt forever. */ 839 if (nextboot && !autoboot) 840 reboot(); 841 842 /* 843 * Try to exec /boot/loader. If interrupted by a keypress, 844 * or in case of failure, try to load a kernel directly instead. 845 */ 846 847 if (autoboot && !*kname) { 848 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER)); 849 if (!keyhit(3)) { 850 load(); 851 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL)); 852 } 853 } 854 855 /* Present the user with the boot2 prompt. */ 856 857 for (;;) { 858 if (!autoboot || !OPT_CHECK(RBX_QUIET)) { 859 printf("\nFreeBSD/x86 boot\n"); 860 if (zfs_rlookup(spa, zfsmount.rootobj, rootname) != 0) 861 printf("Default: %s/<0x%llx>:%s\n" 862 "boot: ", 863 spa->spa_name, zfsmount.rootobj, kname); 864 else if (rootname[0] != '\0') 865 printf("Default: %s/%s:%s\n" 866 "boot: ", 867 spa->spa_name, rootname, kname); 868 else 869 printf("Default: %s:%s\n" 870 "boot: ", 871 spa->spa_name, kname); 872 } 873 if (ioctrl & IO_SERIAL) 874 sio_flush(); 875 if (!autoboot || keyhit(5)) 876 getstr(cmd, sizeof(cmd)); 877 else if (!autoboot || !OPT_CHECK(RBX_QUIET)) 878 putchar('\n'); 879 autoboot = 0; 880 if (parse_cmd()) 881 putchar('\a'); 882 else 883 load(); 884 } 885 } 886 887 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */ 888 void 889 exit(int x) 890 { 891 __exit(x); 892 } 893 894 void 895 reboot(void) 896 { 897 __exit(0); 898 } 899 900 static void 901 load(void) 902 { 903 union { 904 struct exec ex; 905 Elf32_Ehdr eh; 906 } hdr; 907 static Elf32_Phdr ep[2]; 908 static Elf32_Shdr es[2]; 909 caddr_t p; 910 dnode_phys_t dn; 911 off_t off; 912 uint32_t addr, x; 913 int fmt, i, j; 914 915 if (zfs_lookup(&zfsmount, kname, &dn)) { 916 printf("\nCan't find %s\n", kname); 917 return; 918 } 919 off = 0; 920 if (xfsread(&dn, &off, &hdr, sizeof(hdr))) 921 return; 922 if (N_GETMAGIC(hdr.ex) == ZMAGIC) 923 fmt = 0; 924 else if (IS_ELF(hdr.eh)) 925 fmt = 1; 926 else { 927 printf("Invalid %s\n", "format"); 928 return; 929 } 930 if (fmt == 0) { 931 addr = hdr.ex.a_entry & 0xffffff; 932 p = PTOV(addr); 933 off = PAGE_SIZE; 934 if (xfsread(&dn, &off, p, hdr.ex.a_text)) 935 return; 936 p += roundup2(hdr.ex.a_text, PAGE_SIZE); 937 if (xfsread(&dn, &off, p, hdr.ex.a_data)) 938 return; 939 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE); 940 bootinfo.bi_symtab = VTOP(p); 941 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms)); 942 p += sizeof(hdr.ex.a_syms); 943 if (hdr.ex.a_syms) { 944 if (xfsread(&dn, &off, p, hdr.ex.a_syms)) 945 return; 946 p += hdr.ex.a_syms; 947 if (xfsread(&dn, &off, p, sizeof(int))) 948 return; 949 x = *(uint32_t *)p; 950 p += sizeof(int); 951 x -= sizeof(int); 952 if (xfsread(&dn, &off, p, x)) 953 return; 954 p += x; 955 } 956 } else { 957 off = hdr.eh.e_phoff; 958 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) { 959 if (xfsread(&dn, &off, ep + j, sizeof(ep[0]))) 960 return; 961 if (ep[j].p_type == PT_LOAD) 962 j++; 963 } 964 for (i = 0; i < 2; i++) { 965 p = PTOV(ep[i].p_paddr & 0xffffff); 966 off = ep[i].p_offset; 967 if (xfsread(&dn, &off, p, ep[i].p_filesz)) 968 return; 969 } 970 p += roundup2(ep[1].p_memsz, PAGE_SIZE); 971 bootinfo.bi_symtab = VTOP(p); 972 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) { 973 off = hdr.eh.e_shoff + sizeof(es[0]) * 974 (hdr.eh.e_shstrndx + 1); 975 if (xfsread(&dn, &off, &es, sizeof(es))) 976 return; 977 for (i = 0; i < 2; i++) { 978 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size)); 979 p += sizeof(es[i].sh_size); 980 off = es[i].sh_offset; 981 if (xfsread(&dn, &off, p, es[i].sh_size)) 982 return; 983 p += es[i].sh_size; 984 } 985 } 986 addr = hdr.eh.e_entry & 0xffffff; 987 } 988 bootinfo.bi_esymtab = VTOP(p); 989 bootinfo.bi_kernelname = VTOP(kname); 990 zfsargs.size = sizeof(zfsargs); 991 zfsargs.pool = zfsmount.spa->spa_guid; 992 zfsargs.root = zfsmount.rootobj; 993 zfsargs.primary_pool = primary_spa->spa_guid; 994 #ifdef LOADER_GELI_SUPPORT 995 explicit_bzero(gelipw, sizeof(gelipw)); 996 gelibuf = malloc(sizeof(struct keybuf) + (GELI_MAX_KEYS * sizeof(struct keybuf_ent))); 997 geli_export_key_buffer(gelibuf); 998 zfsargs.notapw = '\0'; 999 zfsargs.keybuf_sentinel = KEYBUF_SENTINEL; 1000 zfsargs.keybuf = gelibuf; 1001 #else 1002 zfsargs.gelipw[0] = '\0'; 1003 #endif 1004 if (primary_vdev != NULL) 1005 zfsargs.primary_vdev = primary_vdev->v_guid; 1006 else 1007 printf("failed to detect primary vdev\n"); 1008 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK), 1009 bootdev, 1010 KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG, 1011 (uint32_t) spa->spa_guid, 1012 (uint32_t) (spa->spa_guid >> 32), 1013 VTOP(&bootinfo), 1014 zfsargs); 1015 } 1016 1017 static int 1018 zfs_mount_ds(char *dsname) 1019 { 1020 uint64_t newroot; 1021 spa_t *newspa; 1022 char *q; 1023 1024 q = strchr(dsname, '/'); 1025 if (q) 1026 *q++ = '\0'; 1027 newspa = spa_find_by_name(dsname); 1028 if (newspa == NULL) { 1029 printf("\nCan't find ZFS pool %s\n", dsname); 1030 return -1; 1031 } 1032 1033 if (zfs_spa_init(newspa)) 1034 return -1; 1035 1036 newroot = 0; 1037 if (q) { 1038 if (zfs_lookup_dataset(newspa, q, &newroot)) { 1039 printf("\nCan't find dataset %s in ZFS pool %s\n", 1040 q, newspa->spa_name); 1041 return -1; 1042 } 1043 } 1044 if (zfs_mount(newspa, newroot, &zfsmount)) { 1045 printf("\nCan't mount ZFS dataset\n"); 1046 return -1; 1047 } 1048 spa = newspa; 1049 return (0); 1050 } 1051 1052 static int 1053 parse_cmd(void) 1054 { 1055 char *arg = cmd; 1056 char *ep, *p, *q; 1057 const char *cp; 1058 int c, i, j; 1059 1060 while ((c = *arg++)) { 1061 if (c == ' ' || c == '\t' || c == '\n') 1062 continue; 1063 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++); 1064 ep = p; 1065 if (*p) 1066 *p++ = 0; 1067 if (c == '-') { 1068 while ((c = *arg++)) { 1069 if (c == 'P') { 1070 if (*(uint8_t *)PTOV(0x496) & 0x10) { 1071 cp = "yes"; 1072 } else { 1073 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL); 1074 cp = "no"; 1075 } 1076 printf("Keyboard: %s\n", cp); 1077 continue; 1078 } else if (c == 'S') { 1079 j = 0; 1080 while ((unsigned int)(i = *arg++ - '0') <= 9) 1081 j = j * 10 + i; 1082 if (j > 0 && i == -'0') { 1083 comspeed = j; 1084 break; 1085 } 1086 /* Fall through to error below ('S' not in optstr[]). */ 1087 } 1088 for (i = 0; c != optstr[i]; i++) 1089 if (i == NOPT - 1) 1090 return -1; 1091 opts ^= OPT_SET(flags[i]); 1092 } 1093 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) : 1094 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD; 1095 if (ioctrl & IO_SERIAL) { 1096 if (sio_init(115200 / comspeed) != 0) 1097 ioctrl &= ~IO_SERIAL; 1098 } 1099 } if (c == '?') { 1100 dnode_phys_t dn; 1101 1102 if (zfs_lookup(&zfsmount, arg, &dn) == 0) { 1103 zap_list(spa, &dn); 1104 } 1105 return -1; 1106 } else { 1107 arg--; 1108 1109 /* 1110 * Report pool status if the comment is 'status'. Lets 1111 * hope no-one wants to load /status as a kernel. 1112 */ 1113 if (!strcmp(arg, "status")) { 1114 spa_all_status(); 1115 return -1; 1116 } 1117 1118 /* 1119 * If there is "zfs:" prefix simply ignore it. 1120 */ 1121 if (strncmp(arg, "zfs:", 4) == 0) 1122 arg += 4; 1123 1124 /* 1125 * If there is a colon, switch pools. 1126 */ 1127 q = strchr(arg, ':'); 1128 if (q) { 1129 *q++ = '\0'; 1130 if (zfs_mount_ds(arg) != 0) 1131 return -1; 1132 arg = q; 1133 } 1134 if ((i = ep - arg)) { 1135 if ((size_t)i >= sizeof(kname)) 1136 return -1; 1137 memcpy(kname, arg, i + 1); 1138 } 1139 } 1140 arg = p; 1141 } 1142 return 0; 1143 } 1144