2 * Copyright (c) 1998 Robert Nordier
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
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
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
21 #include <sys/param.h>
22 #include <sys/errno.h>
23 #include <sys/diskmbr.h>
27 #include <sys/reboot.h>
28 #include <sys/queue.h>
30 #include <machine/bootinfo.h>
31 #include <machine/elf.h>
32 #include <machine/pc/bios.h>
55 #define BIOS_NUMDRIVES 0x475
61 #define TYPE_MAXHARD TYPE_DA
64 #define DEV_GELIBOOT_BSIZE 4096
69 static const uuid_t freebsd_zfs_uuid = GPT_ENT_TYPE_FREEBSD_ZFS;
71 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
72 static const unsigned char flags[NOPT] = {
91 * Paths to try loading before falling back to the boot2 prompt.
93 * /boot/zfsloader must be tried before /boot/loader in order to remain
94 * backward compatible with ZFS boot environments where /boot/loader exists
95 * but does not have ZFS support, which was the case before FreeBSD 12.
97 * If no loader is found, try to load a kernel directly instead.
99 static const struct string {
103 { PATH_LOADER_ZFS, sizeof(PATH_LOADER_ZFS) },
104 { PATH_LOADER, sizeof(PATH_LOADER) },
105 { PATH_KERNEL, sizeof(PATH_KERNEL) },
108 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
110 static char cmd[512];
111 static char cmddup[512];
112 static char kname[1024];
113 static char rootname[256];
114 static int comspeed = SIOSPD;
115 static struct bootinfo bootinfo;
116 static uint32_t bootdev;
117 static struct zfs_boot_args zfsargs;
119 vm_offset_t high_heap_base;
120 uint32_t bios_basemem, bios_extmem, high_heap_size;
122 static struct bios_smap smap;
125 * The minimum amount of memory to reserve in bios_extmem for the heap.
127 #define HEAP_MIN (64 * 1024 * 1024)
129 static char *heap_next;
130 static char *heap_end;
132 /* Buffers that must not span a 64k boundary. */
133 #define READ_BUF_SIZE 8192
135 char rdbuf[READ_BUF_SIZE]; /* for reading large things */
136 char secbuf[READ_BUF_SIZE]; /* for MBR/disklabel */
138 static struct dmadat *dmadat;
142 static void load(void);
143 static int parse_cmd(void);
144 static void bios_getmem(void);
147 #ifdef LOADER_GELI_SUPPORT
148 #include "geliboot.h"
149 static char gelipw[GELI_PW_MAXLEN];
154 #ifdef LOADER_GELI_SUPPORT
155 struct geli_dev *gdev;
162 * Read from a dnode (which must be from a ZPL filesystem).
165 zfs_read(spa_t *spa, const dnode_phys_t *dnode, off_t *offp, void *start,
168 const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus;
173 if (*offp + n > zp->zp_size)
174 n = zp->zp_size - *offp;
176 rc = dnode_read(spa, dnode, *offp, start, n);
188 static spa_t *primary_spa;
189 static vdev_t *primary_vdev;
192 * A wrapper for dskread that doesn't have to worry about whether the
193 * buffer pointer crosses a 64k boundary.
196 vdev_read(void *xvdev, void *priv, off_t off, void *buf, size_t bytes)
199 daddr_t lba, alignlba;
201 unsigned int nb, alignnb;
202 struct zfsdsk *zdsk = priv;
204 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
208 lba = off / DEV_BSIZE;
209 lba += zdsk->dsk.start;
211 * Align reads to 4k else 4k sector GELIs will not decrypt.
212 * Round LBA down to nearest multiple of DEV_GELIBOOT_BSIZE bytes.
214 alignlba = rounddown2(off, DEV_GELIBOOT_BSIZE) / DEV_BSIZE;
216 * The read must be aligned to DEV_GELIBOOT_BSIZE bytes relative to the
217 * start of the GELI partition, not the start of the actual disk.
219 alignlba += zdsk->dsk.start;
220 diff = (lba - alignlba) * DEV_BSIZE;
223 nb = bytes / DEV_BSIZE;
225 * Ensure that the read size plus the leading offset does not
226 * exceed the size of the read buffer.
228 if (nb > (READ_BUF_SIZE - diff) / DEV_BSIZE)
229 nb = (READ_BUF_SIZE - diff) / DEV_BSIZE;
231 * Round the number of blocks to read up to the nearest multiple
232 * of DEV_GELIBOOT_BSIZE.
234 alignnb = roundup2(nb * DEV_BSIZE + diff, DEV_GELIBOOT_BSIZE)
237 if (zdsk->dsk.size > 0 && alignlba + alignnb >
238 zdsk->dsk.size + zdsk->dsk.start) {
239 printf("Shortening read at %lld from %d to %lld\n",
241 (zdsk->dsk.size + zdsk->dsk.start) - alignlba);
242 alignnb = (zdsk->dsk.size + zdsk->dsk.start) - alignlba;
245 if (drvread(&zdsk->dsk, dmadat->rdbuf, alignlba, alignnb))
247 #ifdef LOADER_GELI_SUPPORT
249 if (zdsk->gdev != NULL) {
250 if (geli_read(zdsk->gdev,
251 ((alignlba - zdsk->dsk.start) * DEV_BSIZE),
252 dmadat->rdbuf, alignnb * DEV_BSIZE))
256 memcpy(p, dmadat->rdbuf + diff, nb * DEV_BSIZE);
260 bytes -= nb * DEV_BSIZE;
261 /* Don't need the leading offset after the first block. */
267 /* Match the signature exactly due to signature madness */
269 vdev_read2(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
271 return (vdev_read(vdev, priv, off, buf, bytes));
276 vdev_write(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
281 struct zfsdsk *zdsk = priv;
283 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
287 lba = off / DEV_BSIZE;
288 lba += zdsk->dsk.start;
290 nb = bytes / DEV_BSIZE;
291 if (nb > READ_BUF_SIZE / DEV_BSIZE)
292 nb = READ_BUF_SIZE / DEV_BSIZE;
293 memcpy(dmadat->rdbuf, p, nb * DEV_BSIZE);
294 if (drvwrite(&zdsk->dsk, dmadat->rdbuf, lba, nb))
298 bytes -= nb * DEV_BSIZE;
305 xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte)
307 if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) {
308 printf("Invalid format\n");
315 * Read Pad2 (formerly "Boot Block Header") area of the first
316 * vdev label of the given vdev.
319 vdev_read_pad2(vdev_t *vdev, char *buf, size_t size)
323 off_t off = offsetof(vdev_label_t, vl_pad2);
326 if (size > VDEV_PAD_SIZE)
327 size = VDEV_PAD_SIZE;
329 tmp = malloc(VDEV_PAD_SIZE);
334 BP_SET_LSIZE(&bp, VDEV_PAD_SIZE);
335 BP_SET_PSIZE(&bp, VDEV_PAD_SIZE);
336 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
337 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
338 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
339 rc = vdev_read_phys(vdev, &bp, tmp, off, 0);
341 memcpy(buf, tmp, size);
347 vdev_clear_pad2(vdev_t *vdev)
351 off_t off = offsetof(vdev_label_t, vl_pad2);
354 zeroes = malloc(VDEV_PAD_SIZE);
358 memset(zeroes, 0, VDEV_PAD_SIZE);
359 end = (uint64_t *)(zeroes + VDEV_PAD_SIZE);
360 /* ZIO_CHECKSUM_LABEL magic and pre-calcualted checksum for all zeros */
361 end[-5] = 0x0210da7ab10c7a11;
362 end[-4] = 0x97f48f807f6e2a3f;
363 end[-3] = 0xaf909f1658aacefc;
364 end[-2] = 0xcbd1ea57ff6db48b;
365 end[-1] = 0x6ec692db0d465fab;
366 rc = vdev_write(vdev, vdev->v_read_priv, off, zeroes, VDEV_PAD_SIZE);
376 /* Parse system memory map */
380 v86.addr = 0x15; /* int 0x15 function 0xe820 */
382 v86.ecx = sizeof(struct bios_smap);
384 v86.es = VTOPSEG(&smap);
385 v86.edi = VTOPOFF(&smap);
387 if (V86_CY(v86.efl) || (v86.eax != SMAP_SIG))
389 /* look for a low-memory segment that's large enough */
390 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
391 (smap.length >= (512 * 1024)))
392 bios_basemem = smap.length;
393 /* look for the first segment in 'extended' memory */
394 if ((smap.type == SMAP_TYPE_MEMORY) &&
395 (smap.base == 0x100000)) {
396 bios_extmem = smap.length;
400 * Look for the largest segment in 'extended' memory beyond
403 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
404 (smap.base < 0x100000000ull)) {
408 * If this segment crosses the 4GB boundary,
411 if (smap.base + size > 0x100000000ull)
412 size = 0x100000000ull - smap.base;
414 if (size > high_heap_size) {
415 high_heap_size = size;
416 high_heap_base = smap.base;
419 } while (v86.ebx != 0);
421 /* Fall back to the old compatibility function for base memory */
422 if (bios_basemem == 0) {
424 v86.addr = 0x12; /* int 0x12 */
427 bios_basemem = (v86.eax & 0xffff) * 1024;
431 * Fall back through several compatibility functions for extended
434 if (bios_extmem == 0) {
436 v86.addr = 0x15; /* int 0x15 function 0xe801 */
439 if (!V86_CY(v86.efl)) {
440 bios_extmem = ((v86.ecx & 0xffff) +
441 ((v86.edx & 0xffff) * 64)) * 1024;
444 if (bios_extmem == 0) {
446 v86.addr = 0x15; /* int 0x15 function 0x88 */
449 bios_extmem = (v86.eax & 0xffff) * 1024;
453 * If we have extended memory and did not find a suitable heap
454 * region in the SMAP, use the last 3MB of 'extended' memory as a
455 * high heap candidate.
457 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
458 high_heap_size = HEAP_MIN;
459 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
464 * Try to detect a device supported by the legacy int13 BIOS
467 int13probe(int drive)
475 if (!V86_CY(v86.efl) && /* carry clear */
476 ((v86.edx & 0xff) != (drive & DRV_MASK))) { /* unit # OK */
477 if ((v86.ecx & 0x3f) == 0) { /* absurd sector size */
478 return (0); /* skip device */
486 * We call this when we find a ZFS vdev - ZFS consumes the dsk
487 * structure so we must make a new one.
489 static struct zfsdsk *
490 copy_dsk(struct zfsdsk *zdsk)
492 struct zfsdsk *newdsk;
494 newdsk = malloc(sizeof(struct zfsdsk));
500 * Get disk size from GPT.
503 drvsize_gpt(struct dsk *dskp)
509 sec = dmadat->secbuf;
510 if (drvread(dskp, sec, 1, 1))
513 memcpy(&hdr, sec, sizeof(hdr));
514 if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
515 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
516 hdr.hdr_entsz < sizeof(struct gpt_ent) ||
517 DEV_BSIZE % hdr.hdr_entsz != 0) {
520 return (hdr.hdr_lba_alt + 1);
527 * Get disk size from eax=0x800 and 0x4800. We need to probe both
528 * because 0x4800 may not be available and we would like to get more
529 * or less correct disk size - if it is possible at all.
530 * Note we do not really want to touch drv.c because that code is shared
531 * with boot2 and we can not afford to grow that code.
534 drvsize_ext(struct zfsdsk *zdsk)
542 /* Try to read disk size from GPT */
543 size = drvsize_gpt(dskp);
550 v86.edx = dskp->drive;
553 /* Don't error out if we get bad sector number, try EDD as well */
554 if (V86_CY(v86.efl) || /* carry set */
555 (v86.edx & 0xff) <= (unsigned)(dskp->drive & 0x7f)) /* unit # bad */
557 cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
558 /* Convert max head # -> # of heads */
559 hds = ((v86.edx & 0xff00) >> 8) + 1;
560 sec = v86.ecx & 0x3f;
562 size = (uint64_t)cyl * hds * sec;
564 /* Determine if we can use EDD with this device. */
568 v86.edx = dskp->drive;
571 if (V86_CY(v86.efl) || /* carry set */
572 (v86.ebx & 0xffff) != 0xaa55 || /* signature */
573 (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
584 * The "layered" ioctl to read disk/partition size. Unfortunately
585 * the zfsboot case is hardest, because we do not have full software
586 * stack available, so we need to do some manual work here.
589 ldi_get_size(void *priv)
591 struct zfsdsk *zdsk = priv;
592 uint64_t size = zdsk->dsk.size;
594 if (zdsk->dsk.start == 0)
595 size = drvsize_ext(zdsk);
597 return (size * DEV_BSIZE);
601 probe_drive(struct zfsdsk *zdsk)
606 unsigned part, entries_per_sec;
609 #if defined(GPT) || defined(LOADER_GELI_SUPPORT)
613 struct dos_partition *dp;
617 #ifdef LOADER_GELI_SUPPORT
619 * Taste the disk, if it is GELI encrypted, decrypt it then dig out the
620 * partition table and probe each slice/partition in turn for a vdev or
621 * GELI encrypted vdev.
623 elba = drvsize_ext(zdsk);
627 zdsk->gdev = geli_taste(vdev_read, zdsk, elba, "disk%u:0:");
628 if ((zdsk->gdev != NULL) && (geli_havekey(zdsk->gdev) == 0))
629 geli_passphrase(zdsk->gdev, gelipw);
630 #endif /* LOADER_GELI_SUPPORT */
632 sec = dmadat->secbuf;
637 * First check for GPT.
639 if (drvread(&zdsk->dsk, sec, 1, 1)) {
642 memcpy(&hdr, sec, sizeof(hdr));
643 if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
644 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
645 hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
650 * Probe all GPT partitions for the presence of ZFS pools. We
651 * return the spa_t for the first we find (if requested). This
652 * will have the effect of booting from the first pool on the
655 * If no vdev is found, GELI decrypting the device and try again
657 entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
658 slba = hdr.hdr_lba_table;
659 elba = slba + hdr.hdr_entries / entries_per_sec;
660 while (slba < elba) {
662 if (drvread(&zdsk->dsk, sec, slba, 1))
664 for (part = 0; part < entries_per_sec; part++) {
665 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
666 if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
667 sizeof(uuid_t)) == 0) {
668 zdsk->dsk.start = ent->ent_lba_start;
670 ent->ent_lba_end - ent->ent_lba_start + 1;
671 zdsk->dsk.slice = part + 1;
672 zdsk->dsk.part = 255;
673 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
675 * This slice had a vdev. We need a new
676 * dsk structure now since the vdev now
679 zdsk = copy_dsk(zdsk);
681 #ifdef LOADER_GELI_SUPPORT
682 else if ((zdsk->gdev = geli_taste(vdev_read,
683 zdsk, ent->ent_lba_end - ent->ent_lba_start,
684 "disk%up%u:", zdsk->dsk.unit,
685 zdsk->dsk.slice)) != NULL) {
686 if (geli_havekey(zdsk->gdev) == 0 ||
687 geli_passphrase(zdsk->gdev, gelipw)
690 * This slice has GELI,
693 if (vdev_probe(vdev_read2,
697 * vdev. We need a new
702 zdsk = copy_dsk(zdsk);
707 #endif /* LOADER_GELI_SUPPORT */
716 if (drvread(&zdsk->dsk, sec, DOSBBSECTOR, 1))
718 dp = (void *)(sec + DOSPARTOFF);
720 for (i = 0; i < NDOSPART; i++) {
723 zdsk->dsk.start = dp[i].dp_start;
724 zdsk->dsk.size = dp[i].dp_size;
725 zdsk->dsk.slice = i + 1;
726 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
727 zdsk = copy_dsk(zdsk);
729 #ifdef LOADER_GELI_SUPPORT
730 else if ((zdsk->gdev = geli_taste(vdev_read, zdsk,
731 dp[i].dp_size - dp[i].dp_start, "disk%us%u:")) != NULL) {
732 if (geli_havekey(zdsk->gdev) == 0 ||
733 geli_passphrase(zdsk->gdev, gelipw) == 0) {
735 * This slice has GELI, check it for ZFS.
737 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
739 * This slice had a vdev. We need a new
740 * dsk structure now since the vdev now
743 zdsk = copy_dsk(zdsk);
748 #endif /* LOADER_GELI_SUPPORT */
762 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
766 if (high_heap_size > 0) {
767 heap_end = PTOV(high_heap_base + high_heap_size);
768 heap_next = PTOV(high_heap_base);
770 heap_next = (char *)dmadat + sizeof(*dmadat);
771 heap_end = (char *)PTOV(bios_basemem);
773 setheap(heap_next, heap_end);
775 zdsk = calloc(1, sizeof(struct zfsdsk));
776 zdsk->dsk.drive = *(uint8_t *)PTOV(ARGS);
777 zdsk->dsk.type = zdsk->dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
778 zdsk->dsk.unit = zdsk->dsk.drive & DRV_MASK;
779 zdsk->dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
782 zdsk->dsk.size = drvsize_ext(zdsk);
784 bootinfo.bi_version = BOOTINFO_VERSION;
785 bootinfo.bi_size = sizeof(bootinfo);
786 bootinfo.bi_basemem = bios_basemem / 1024;
787 bootinfo.bi_extmem = bios_extmem / 1024;
788 bootinfo.bi_memsizes_valid++;
789 bootinfo.bi_bios_dev = zdsk->dsk.drive;
791 bootdev = MAKEBOOTDEV(dev_maj[zdsk->dsk.type],
792 zdsk->dsk.slice, zdsk->dsk.unit, zdsk->dsk.part);
794 /* Process configuration file */
801 * Probe the boot drive first - we will try to boot from whatever
802 * pool we find on that drive.
807 * Probe the rest of the drives that the bios knows about. This
808 * will find any other available pools and it may fill in missing
809 * vdevs for the boot pool.
812 for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++)
814 for (i = 0; i < MAXBDDEV; i++)
817 if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
820 if (!int13probe(i | DRV_HARD))
823 zdsk = calloc(1, sizeof(struct zfsdsk));
824 zdsk->dsk.drive = i | DRV_HARD;
825 zdsk->dsk.type = zdsk->dsk.drive & TYPE_AD;
830 zdsk->dsk.size = drvsize_ext(zdsk);
835 * The first discovered pool, if any, is the pool.
837 spa = spa_get_primary();
839 printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
845 primary_vdev = spa_get_primary_vdev(spa);
848 rc = vdev_read_pad2(primary_vdev, cmd, sizeof(cmd));
849 if (vdev_clear_pad2(primary_vdev))
850 printf("failed to clear pad2 area of primary vdev\n");
854 * We could find an old-style ZFS Boot Block header
855 * here. Simply ignore it.
857 if (*(uint64_t *)cmd != 0x2f5b007b10c) {
859 * Note that parse() is destructive to cmd[]
860 * and we also want to honor RBX_QUIET option
861 * that could be present in cmd[].
864 memcpy(cmddup, cmd, sizeof(cmd));
866 printf("failed to parse pad2 area of "
870 if (!OPT_CHECK(RBX_QUIET))
871 printf("zfs nextboot: %s\n", cmddup);
873 /* Do not process this command twice */
877 printf("failed to read pad2 area of primary vdev\n");
879 /* Mount ZFS only if it's not already mounted via nextboot parsing. */
880 if (zfsmount.spa == NULL &&
881 (zfs_spa_init(spa) != 0 || zfs_mount(spa, 0, &zfsmount) != 0)) {
882 printf("%s: failed to mount default pool %s\n",
883 BOOTPROG, spa->spa_name);
885 } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 ||
886 zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) {
888 zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
893 * Note that parse_cmd() is destructive to cmd[] and we also
894 * want to honor RBX_QUIET option that could be present in
897 memcpy(cmddup, cmd, sizeof(cmd));
900 if (!OPT_CHECK(RBX_QUIET))
901 printf("%s: %s\n", PATH_CONFIG, cmddup);
902 /* Do not process this command twice */
906 /* Do not risk waiting at the prompt forever. */
907 if (nextboot && !autoboot)
910 if (autoboot && !*kname) {
912 * Iterate through the list of loader and kernel paths,
913 * trying to load. If interrupted by a keypress, or in case of
914 * failure, drop the user to the boot2 prompt.
916 for (i = 0; i < nitems(loadpath); i++) {
917 memcpy(kname, loadpath[i].p, loadpath[i].len);
924 /* Present the user with the boot2 prompt. */
927 if (!autoboot || !OPT_CHECK(RBX_QUIET)) {
928 printf("\nFreeBSD/x86 boot\n");
929 if (zfs_rlookup(spa, zfsmount.rootobj, rootname) != 0)
930 printf("Default: %s/<0x%llx>:%s\n"
932 spa->spa_name, zfsmount.rootobj, kname);
933 else if (rootname[0] != '\0')
934 printf("Default: %s/%s:%s\n"
936 spa->spa_name, rootname, kname);
938 printf("Default: %s:%s\n"
940 spa->spa_name, kname);
942 if (ioctrl & IO_SERIAL)
944 if (!autoboot || keyhit(5))
945 getstr(cmd, sizeof(cmd));
946 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
956 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
976 static Elf32_Phdr ep[2];
977 static Elf32_Shdr es[2];
984 if (zfs_lookup(&zfsmount, kname, &dn)) {
985 printf("\nCan't find %s\n", kname);
989 if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
991 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
993 else if (IS_ELF(hdr.eh))
996 printf("Invalid %s\n", "format");
1000 addr = hdr.ex.a_entry & 0xffffff;
1003 if (xfsread(&dn, &off, p, hdr.ex.a_text))
1005 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
1006 if (xfsread(&dn, &off, p, hdr.ex.a_data))
1008 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
1009 bootinfo.bi_symtab = VTOP(p);
1010 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
1011 p += sizeof(hdr.ex.a_syms);
1012 if (hdr.ex.a_syms) {
1013 if (xfsread(&dn, &off, p, hdr.ex.a_syms))
1016 if (xfsread(&dn, &off, p, sizeof(int)))
1021 if (xfsread(&dn, &off, p, x))
1026 off = hdr.eh.e_phoff;
1027 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
1028 if (xfsread(&dn, &off, ep + j, sizeof(ep[0])))
1030 if (ep[j].p_type == PT_LOAD)
1033 for (i = 0; i < 2; i++) {
1034 p = PTOV(ep[i].p_paddr & 0xffffff);
1035 off = ep[i].p_offset;
1036 if (xfsread(&dn, &off, p, ep[i].p_filesz))
1039 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
1040 bootinfo.bi_symtab = VTOP(p);
1041 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
1042 off = hdr.eh.e_shoff + sizeof(es[0]) *
1043 (hdr.eh.e_shstrndx + 1);
1044 if (xfsread(&dn, &off, &es, sizeof(es)))
1046 for (i = 0; i < 2; i++) {
1047 memcpy(p, &es[i].sh_size,
1048 sizeof(es[i].sh_size));
1049 p += sizeof(es[i].sh_size);
1050 off = es[i].sh_offset;
1051 if (xfsread(&dn, &off, p, es[i].sh_size))
1056 addr = hdr.eh.e_entry & 0xffffff;
1058 bootinfo.bi_esymtab = VTOP(p);
1059 bootinfo.bi_kernelname = VTOP(kname);
1060 zfsargs.size = sizeof(zfsargs);
1061 zfsargs.pool = zfsmount.spa->spa_guid;
1062 zfsargs.root = zfsmount.rootobj;
1063 zfsargs.primary_pool = primary_spa->spa_guid;
1064 #ifdef LOADER_GELI_SUPPORT
1065 explicit_bzero(gelipw, sizeof(gelipw));
1066 export_geli_boot_data(&zfsargs.gelidata);
1068 if (primary_vdev != NULL)
1069 zfsargs.primary_vdev = primary_vdev->v_guid;
1071 printf("failed to detect primary vdev\n");
1073 * Note that the zfsargs struct is passed by value, not by pointer.
1074 * Code in btxldr.S copies the values from the entry stack to a fixed
1075 * location within loader(8) at startup due to the presence of
1076 * KARGS_FLAGS_EXTARG.
1078 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
1080 KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
1081 (uint32_t)spa->spa_guid,
1082 (uint32_t)(spa->spa_guid >> 32),
1088 zfs_mount_ds(char *dsname)
1094 q = strchr(dsname, '/');
1097 newspa = spa_find_by_name(dsname);
1098 if (newspa == NULL) {
1099 printf("\nCan't find ZFS pool %s\n", dsname);
1103 if (zfs_spa_init(newspa))
1108 if (zfs_lookup_dataset(newspa, q, &newroot)) {
1109 printf("\nCan't find dataset %s in ZFS pool %s\n",
1110 q, newspa->spa_name);
1114 if (zfs_mount(newspa, newroot, &zfsmount)) {
1115 printf("\nCan't mount ZFS dataset\n");
1130 while ((c = *arg++)) {
1131 if (c == ' ' || c == '\t' || c == '\n')
1133 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
1139 while ((c = *arg++)) {
1141 if (*(uint8_t *)PTOV(0x496) & 0x10) {
1144 opts |= OPT_SET(RBX_DUAL);
1145 opts |= OPT_SET(RBX_SERIAL);
1148 printf("Keyboard: %s\n", cp);
1150 } else if (c == 'S') {
1152 while ((unsigned int)
1153 (i = *arg++ - '0') <= 9)
1155 if (j > 0 && i == -'0') {
1160 * Fall through to error below
1161 * ('S' not in optstr[]).
1164 for (i = 0; c != optstr[i]; i++)
1167 opts ^= OPT_SET(flags[i]);
1169 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
1170 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
1171 if (ioctrl & IO_SERIAL) {
1172 if (sio_init(115200 / comspeed) != 0)
1173 ioctrl &= ~IO_SERIAL;
1178 if (zfs_lookup(&zfsmount, arg, &dn) == 0) {
1186 * Report pool status if the comment is 'status'. Lets
1187 * hope no-one wants to load /status as a kernel.
1189 if (strcmp(arg, "status") == 0) {
1195 * If there is "zfs:" prefix simply ignore it.
1197 if (strncmp(arg, "zfs:", 4) == 0)
1201 * If there is a colon, switch pools.
1203 q = strchr(arg, ':');
1206 if (zfs_mount_ds(arg) != 0)
1210 if ((i = ep - arg)) {
1211 if ((size_t)i >= sizeof(kname))
1213 memcpy(kname, arg, i + 1);