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] = {
90 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
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;
101 vm_offset_t high_heap_base;
102 uint32_t bios_basemem, bios_extmem, high_heap_size;
104 static struct bios_smap smap;
107 * The minimum amount of memory to reserve in bios_extmem for the heap.
109 #define HEAP_MIN (64 * 1024 * 1024)
111 static char *heap_next;
112 static char *heap_end;
114 /* Buffers that must not span a 64k boundary. */
115 #define READ_BUF_SIZE 8192
117 char rdbuf[READ_BUF_SIZE]; /* for reading large things */
118 char secbuf[READ_BUF_SIZE]; /* for MBR/disklabel */
120 static struct dmadat *dmadat;
124 static void load(void);
125 static int parse_cmd(void);
126 static void bios_getmem(void);
129 #ifdef LOADER_GELI_SUPPORT
130 #include "geliboot.h"
131 static char gelipw[GELI_PW_MAXLEN];
132 static struct keybuf *gelibuf;
137 #ifdef LOADER_GELI_SUPPORT
138 struct geli_dev *gdev;
145 * Read from a dnode (which must be from a ZPL filesystem).
148 zfs_read(spa_t *spa, const dnode_phys_t *dnode, off_t *offp, void *start, size_t size)
150 const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus;
155 if (*offp + n > zp->zp_size)
156 n = zp->zp_size - *offp;
158 rc = dnode_read(spa, dnode, *offp, start, n);
170 static spa_t *primary_spa;
171 static vdev_t *primary_vdev;
174 * A wrapper for dskread that doesn't have to worry about whether the
175 * buffer pointer crosses a 64k boundary.
178 vdev_read(void *xvdev, void *priv, off_t off, void *buf, size_t bytes)
181 daddr_t lba, alignlba;
183 unsigned int nb, alignnb;
184 struct zfsdsk *zdsk = (struct zfsdsk *) priv;
186 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
190 lba = off / DEV_BSIZE;
191 lba += zdsk->dsk.start;
193 * Align reads to 4k else 4k sector GELIs will not decrypt.
194 * Round LBA down to nearest multiple of DEV_GELIBOOT_BSIZE bytes.
196 alignlba = rounddown2(off, DEV_GELIBOOT_BSIZE) / DEV_BSIZE;
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.
201 alignlba += zdsk->dsk.start;
202 diff = (lba - alignlba) * DEV_BSIZE;
205 nb = bytes / DEV_BSIZE;
207 * Ensure that the read size plus the leading offset does not
208 * exceed the size of the read buffer.
210 if (nb > (READ_BUF_SIZE - diff) / DEV_BSIZE)
211 nb = (READ_BUF_SIZE - diff) / DEV_BSIZE;
213 * Round the number of blocks to read up to the nearest multiple
214 * of DEV_GELIBOOT_BSIZE.
216 alignnb = roundup2(nb * DEV_BSIZE + diff, DEV_GELIBOOT_BSIZE)
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",
223 (zdsk->dsk.size + zdsk->dsk.start) - alignlba);
224 alignnb = (zdsk->dsk.size + zdsk->dsk.start) - alignlba;
227 if (drvread(&zdsk->dsk, dmadat->rdbuf, alignlba, alignnb))
229 #ifdef LOADER_GELI_SUPPORT
231 if (zdsk->gdev != NULL) {
232 if (geli_read(zdsk->gdev, ((alignlba - zdsk->dsk.start) *
233 DEV_BSIZE), dmadat->rdbuf, alignnb * DEV_BSIZE))
237 memcpy(p, dmadat->rdbuf + diff, nb * DEV_BSIZE);
241 bytes -= nb * DEV_BSIZE;
242 /* Don't need the leading offset after the first block. */
248 /* Match the signature exactly due to signature madness */
250 vdev_read2(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
252 return vdev_read(vdev, priv, off, buf, bytes);
257 vdev_write(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
262 struct zfsdsk *zdsk = (struct zfsdsk *) priv;
264 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
268 lba = off / DEV_BSIZE;
269 lba += zdsk->dsk.start;
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))
279 bytes -= nb * DEV_BSIZE;
286 xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte)
288 if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) {
289 printf("Invalid format\n");
296 * Read Pad2 (formerly "Boot Block Header") area of the first
297 * vdev label of the given vdev.
300 vdev_read_pad2(vdev_t *vdev, char *buf, size_t size)
303 char *tmp = zap_scratch;
304 off_t off = offsetof(vdev_label_t, vl_pad2);
306 if (size > VDEV_PAD_SIZE)
307 size = VDEV_PAD_SIZE;
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))
317 memcpy(buf, tmp, size);
322 vdev_clear_pad2(vdev_t *vdev)
324 char *zeroes = zap_scratch;
326 off_t off = offsetof(vdev_label_t, vl_pad2);
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))
346 /* Parse system memory map */
350 v86.addr = 0x15; /* int 0x15 function 0xe820*/
352 v86.ecx = sizeof(struct bios_smap);
354 v86.es = VTOPSEG(&smap);
355 v86.edi = VTOPOFF(&smap);
357 if (V86_CY(v86.efl) || (v86.eax != SMAP_SIG))
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;
369 * Look for the largest segment in 'extended' memory beyond
372 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
373 (smap.base < 0x100000000ull)) {
377 * If this segment crosses the 4GB boundary, truncate it.
379 if (smap.base + size > 0x100000000ull)
380 size = 0x100000000ull - smap.base;
382 if (size > high_heap_size) {
383 high_heap_size = size;
384 high_heap_base = smap.base;
387 } while (v86.ebx != 0);
389 /* Fall back to the old compatibility function for base memory */
390 if (bios_basemem == 0) {
392 v86.addr = 0x12; /* int 0x12 */
395 bios_basemem = (v86.eax & 0xffff) * 1024;
398 /* Fall back through several compatibility functions for extended memory */
399 if (bios_extmem == 0) {
401 v86.addr = 0x15; /* int 0x15 function 0xe801*/
404 if (!V86_CY(v86.efl)) {
405 bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
408 if (bios_extmem == 0) {
410 v86.addr = 0x15; /* int 0x15 function 0x88*/
413 bios_extmem = (v86.eax & 0xffff) * 1024;
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.
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;
428 * Try to detect a device supported by the legacy int13 BIOS
431 int13probe(int drive)
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 */
450 * We call this when we find a ZFS vdev - ZFS consumes the dsk
451 * structure so we must make a new one.
453 static struct zfsdsk *
454 copy_dsk(struct zfsdsk *zdsk)
456 struct zfsdsk *newdsk;
458 newdsk = malloc(sizeof(struct zfsdsk));
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.
471 drvsize_ext(struct zfsdsk *zdsk)
482 v86.edx = dskp->drive;
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 */
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;
494 size = (uint64_t)cyl * hds * sec;
496 /* Determine if we can use EDD with this device. */
500 v86.edx = dskp->drive;
503 if (V86_CY(v86.efl) || /* carry set */
504 (v86.ebx & 0xffff) != 0xaa55 || /* signature */
505 (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
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.
521 ldi_get_size(void *priv)
523 struct zfsdsk *zdsk = priv;
524 uint64_t size = zdsk->dsk.size;
526 if (zdsk->dsk.start == 0)
527 size = drvsize_ext(zdsk);
529 return (size * DEV_BSIZE);
533 probe_drive(struct zfsdsk *zdsk)
538 unsigned part, entries_per_sec;
541 #if defined(GPT) || defined(LOADER_GELI_SUPPORT)
545 struct dos_partition *dp;
550 * If we find a vdev on the whole disk, stop here.
552 if (vdev_probe(vdev_read2, zdsk, NULL) == 0)
555 #ifdef LOADER_GELI_SUPPORT
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.
562 elba = drvsize_ext(zdsk);
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) {
575 #endif /* LOADER_GELI_SUPPORT */
577 sec = dmadat->secbuf;
582 * First check for GPT.
584 if (drvread(&zdsk->dsk, sec, 1, 1)) {
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) {
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
600 * If no vdev is found, GELI decrypting the device and try again
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) {
607 if (drvread(&zdsk->dsk, sec, slba, 1))
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) {
619 * This slice had a vdev. We need a new dsk
620 * structure now since the vdev now owns this one.
622 zdsk = copy_dsk(zdsk);
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) {
631 * This slice has GELI, check it for ZFS.
633 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
635 * This slice had a vdev. We need a new dsk
636 * structure now since the vdev now owns this one.
638 zdsk = copy_dsk(zdsk);
643 #endif /* LOADER_GELI_SUPPORT */
652 if (drvread(&zdsk->dsk, sec, DOSBBSECTOR, 1))
654 dp = (void *)(sec + DOSPARTOFF);
656 for (i = 0; i < NDOSPART; i++) {
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);
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) {
671 * This slice has GELI, check it for ZFS.
673 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
675 * This slice had a vdev. We need a new dsk
676 * structure now since the vdev now owns this one.
678 zdsk = copy_dsk(zdsk);
683 #endif /* LOADER_GELI_SUPPORT */
697 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
701 if (high_heap_size > 0) {
702 heap_end = PTOV(high_heap_base + high_heap_size);
703 heap_next = PTOV(high_heap_base);
705 heap_next = (char *)dmadat + sizeof(*dmadat);
706 heap_end = (char *)PTOV(bios_basemem);
708 setheap(heap_next, heap_end);
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;
717 zdsk->dsk.size = drvsize_ext(zdsk);
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;
726 bootdev = MAKEBOOTDEV(dev_maj[zdsk->dsk.type],
727 zdsk->dsk.slice, zdsk->dsk.unit, zdsk->dsk.part);
729 /* Process configuration file */
736 * Probe the boot drive first - we will try to boot from whatever
737 * pool we find on that drive.
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.
747 for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++)
749 for (i = 0; i < MAXBDDEV; i++)
752 if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
755 if (!int13probe(i | DRV_HARD))
758 zdsk = calloc(1, sizeof(struct zfsdsk));
759 zdsk->dsk.drive = i | DRV_HARD;
760 zdsk->dsk.type = zdsk->dsk.drive & TYPE_AD;
765 zdsk->dsk.size = drvsize_ext(zdsk);
770 * The first discovered pool, if any, is the pool.
772 spa = spa_get_primary();
774 printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
780 primary_vdev = spa_get_primary_vdev(spa);
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");
789 * We could find an old-style ZFS Boot Block header here.
792 if (*(uint64_t *)cmd != 0x2f5b007b10c) {
794 * Note that parse() is destructive to cmd[] and we also want
795 * to honor RBX_QUIET option that could be present in cmd[].
798 memcpy(cmddup, cmd, sizeof(cmd));
800 printf("failed to parse pad2 area of primary vdev\n");
803 if (!OPT_CHECK(RBX_QUIET))
804 printf("zfs nextboot: %s\n", cmddup);
806 /* Do not process this command twice */
810 printf("failed to read pad2 area of primary vdev\n");
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);
818 } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 ||
819 zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) {
821 zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
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[].
829 memcpy(cmddup, cmd, sizeof(cmd));
832 if (!OPT_CHECK(RBX_QUIET))
833 printf("%s: %s\n", PATH_CONFIG, cmddup);
834 /* Do not process this command twice */
838 /* Do not risk waiting at the prompt forever. */
839 if (nextboot && !autoboot)
843 * Try to exec /boot/loader. If interrupted by a keypress,
844 * or in case of failure, try to load a kernel directly instead.
847 if (autoboot && !*kname) {
848 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
851 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
855 /* Present the user with the boot2 prompt. */
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"
863 spa->spa_name, zfsmount.rootobj, kname);
864 else if (rootname[0] != '\0')
865 printf("Default: %s/%s:%s\n"
867 spa->spa_name, rootname, kname);
869 printf("Default: %s:%s\n"
871 spa->spa_name, kname);
873 if (ioctrl & IO_SERIAL)
875 if (!autoboot || keyhit(5))
876 getstr(cmd, sizeof(cmd));
877 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
887 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
907 static Elf32_Phdr ep[2];
908 static Elf32_Shdr es[2];
915 if (zfs_lookup(&zfsmount, kname, &dn)) {
916 printf("\nCan't find %s\n", kname);
920 if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
922 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
924 else if (IS_ELF(hdr.eh))
927 printf("Invalid %s\n", "format");
931 addr = hdr.ex.a_entry & 0xffffff;
934 if (xfsread(&dn, &off, p, hdr.ex.a_text))
936 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
937 if (xfsread(&dn, &off, p, hdr.ex.a_data))
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);
944 if (xfsread(&dn, &off, p, hdr.ex.a_syms))
947 if (xfsread(&dn, &off, p, sizeof(int)))
952 if (xfsread(&dn, &off, p, x))
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])))
961 if (ep[j].p_type == PT_LOAD)
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))
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)))
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))
986 addr = hdr.eh.e_entry & 0xffffff;
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;
1002 zfsargs.gelipw[0] = '\0';
1004 if (primary_vdev != NULL)
1005 zfsargs.primary_vdev = primary_vdev->v_guid;
1007 printf("failed to detect primary vdev\n");
1008 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
1010 KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
1011 (uint32_t) spa->spa_guid,
1012 (uint32_t) (spa->spa_guid >> 32),
1018 zfs_mount_ds(char *dsname)
1024 q = strchr(dsname, '/');
1027 newspa = spa_find_by_name(dsname);
1028 if (newspa == NULL) {
1029 printf("\nCan't find ZFS pool %s\n", dsname);
1033 if (zfs_spa_init(newspa))
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);
1044 if (zfs_mount(newspa, newroot, &zfsmount)) {
1045 printf("\nCan't mount ZFS dataset\n");
1060 while ((c = *arg++)) {
1061 if (c == ' ' || c == '\t' || c == '\n')
1063 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
1068 while ((c = *arg++)) {
1070 if (*(uint8_t *)PTOV(0x496) & 0x10) {
1073 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
1076 printf("Keyboard: %s\n", cp);
1078 } else if (c == 'S') {
1080 while ((unsigned int)(i = *arg++ - '0') <= 9)
1082 if (j > 0 && i == -'0') {
1086 /* Fall through to error below ('S' not in optstr[]). */
1088 for (i = 0; c != optstr[i]; i++)
1091 opts ^= OPT_SET(flags[i]);
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;
1102 if (zfs_lookup(&zfsmount, arg, &dn) == 0) {
1110 * Report pool status if the comment is 'status'. Lets
1111 * hope no-one wants to load /status as a kernel.
1113 if (!strcmp(arg, "status")) {
1119 * If there is "zfs:" prefix simply ignore it.
1121 if (strncmp(arg, "zfs:", 4) == 0)
1125 * If there is a colon, switch pools.
1127 q = strchr(arg, ':');
1130 if (zfs_mount_ds(arg) != 0)
1134 if ((i = ep - arg)) {
1135 if ((size_t)i >= sizeof(kname))
1137 memcpy(kname, arg, i + 1);