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