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];
136 #ifdef LOADER_GELI_SUPPORT
137 struct geli_dev *gdev;
144 * Read from a dnode (which must be from a ZPL filesystem).
147 zfs_read(spa_t *spa, const dnode_phys_t *dnode, off_t *offp, void *start, size_t size)
149 const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus;
154 if (*offp + n > zp->zp_size)
155 n = zp->zp_size - *offp;
157 rc = dnode_read(spa, dnode, *offp, start, n);
169 static spa_t *primary_spa;
170 static vdev_t *primary_vdev;
173 * A wrapper for dskread that doesn't have to worry about whether the
174 * buffer pointer crosses a 64k boundary.
177 vdev_read(void *xvdev, void *priv, off_t off, void *buf, size_t bytes)
180 daddr_t lba, alignlba;
182 unsigned int nb, alignnb;
183 struct zfsdsk *zdsk = (struct zfsdsk *) priv;
185 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
189 lba = off / DEV_BSIZE;
190 lba += zdsk->dsk.start;
192 * Align reads to 4k else 4k sector GELIs will not decrypt.
193 * Round LBA down to nearest multiple of DEV_GELIBOOT_BSIZE bytes.
195 alignlba = rounddown2(off, DEV_GELIBOOT_BSIZE) / DEV_BSIZE;
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.
200 alignlba += zdsk->dsk.start;
201 diff = (lba - alignlba) * DEV_BSIZE;
204 nb = bytes / DEV_BSIZE;
206 * Ensure that the read size plus the leading offset does not
207 * exceed the size of the read buffer.
209 if (nb > (READ_BUF_SIZE - diff) / DEV_BSIZE)
210 nb = (READ_BUF_SIZE - diff) / DEV_BSIZE;
212 * Round the number of blocks to read up to the nearest multiple
213 * of DEV_GELIBOOT_BSIZE.
215 alignnb = roundup2(nb * DEV_BSIZE + diff, DEV_GELIBOOT_BSIZE)
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",
222 (zdsk->dsk.size + zdsk->dsk.start) - alignlba);
223 alignnb = (zdsk->dsk.size + zdsk->dsk.start) - alignlba;
226 if (drvread(&zdsk->dsk, dmadat->rdbuf, alignlba, alignnb))
228 #ifdef LOADER_GELI_SUPPORT
230 if (zdsk->gdev != NULL) {
231 if (geli_read(zdsk->gdev, ((alignlba - zdsk->dsk.start) *
232 DEV_BSIZE), dmadat->rdbuf, alignnb * DEV_BSIZE))
236 memcpy(p, dmadat->rdbuf + diff, nb * DEV_BSIZE);
240 bytes -= nb * DEV_BSIZE;
241 /* Don't need the leading offset after the first block. */
247 /* Match the signature exactly due to signature madness */
249 vdev_read2(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
251 return vdev_read(vdev, priv, off, buf, bytes);
256 vdev_write(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
261 struct zfsdsk *zdsk = (struct zfsdsk *) priv;
263 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
267 lba = off / DEV_BSIZE;
268 lba += zdsk->dsk.start;
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))
278 bytes -= nb * DEV_BSIZE;
285 xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte)
287 if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) {
288 printf("Invalid format\n");
295 * Read Pad2 (formerly "Boot Block Header") area of the first
296 * vdev label of the given vdev.
299 vdev_read_pad2(vdev_t *vdev, char *buf, size_t size)
302 char *tmp = zap_scratch;
303 off_t off = offsetof(vdev_label_t, vl_pad2);
305 if (size > VDEV_PAD_SIZE)
306 size = VDEV_PAD_SIZE;
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))
316 memcpy(buf, tmp, size);
321 vdev_clear_pad2(vdev_t *vdev)
323 char *zeroes = zap_scratch;
325 off_t off = offsetof(vdev_label_t, vl_pad2);
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))
345 /* Parse system memory map */
349 v86.addr = 0x15; /* int 0x15 function 0xe820*/
351 v86.ecx = sizeof(struct bios_smap);
353 v86.es = VTOPSEG(&smap);
354 v86.edi = VTOPOFF(&smap);
356 if (V86_CY(v86.efl) || (v86.eax != SMAP_SIG))
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;
368 * Look for the largest segment in 'extended' memory beyond
371 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
372 (smap.base < 0x100000000ull)) {
376 * If this segment crosses the 4GB boundary, truncate it.
378 if (smap.base + size > 0x100000000ull)
379 size = 0x100000000ull - smap.base;
381 if (size > high_heap_size) {
382 high_heap_size = size;
383 high_heap_base = smap.base;
386 } while (v86.ebx != 0);
388 /* Fall back to the old compatibility function for base memory */
389 if (bios_basemem == 0) {
391 v86.addr = 0x12; /* int 0x12 */
394 bios_basemem = (v86.eax & 0xffff) * 1024;
397 /* Fall back through several compatibility functions for extended memory */
398 if (bios_extmem == 0) {
400 v86.addr = 0x15; /* int 0x15 function 0xe801*/
403 if (!V86_CY(v86.efl)) {
404 bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
407 if (bios_extmem == 0) {
409 v86.addr = 0x15; /* int 0x15 function 0x88*/
412 bios_extmem = (v86.eax & 0xffff) * 1024;
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.
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;
427 * Try to detect a device supported by the legacy int13 BIOS
430 int13probe(int drive)
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 */
449 * We call this when we find a ZFS vdev - ZFS consumes the dsk
450 * structure so we must make a new one.
452 static struct zfsdsk *
453 copy_dsk(struct zfsdsk *zdsk)
455 struct zfsdsk *newdsk;
457 newdsk = malloc(sizeof(struct zfsdsk));
463 * Get disk size from eax=0x800 and 0x4800. We need to probe both
464 * because 0x4800 may not be available and we would like to get more
465 * or less correct disk size - if it is possible at all.
466 * Note we do not really want to touch drv.c because that code is shared
467 * with boot2 and we can not afford to grow that code.
470 drvsize_ext(struct zfsdsk *zdsk)
481 v86.edx = dskp->drive;
484 /* Don't error out if we get bad sector number, try EDD as well */
485 if (V86_CY(v86.efl) || /* carry set */
486 (v86.edx & 0xff) <= (unsigned)(dskp->drive & 0x7f)) /* unit # bad */
488 cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
489 /* Convert max head # -> # of heads */
490 hds = ((v86.edx & 0xff00) >> 8) + 1;
491 sec = v86.ecx & 0x3f;
493 size = (uint64_t)cyl * hds * sec;
495 /* Determine if we can use EDD with this device. */
499 v86.edx = dskp->drive;
502 if (V86_CY(v86.efl) || /* carry set */
503 (v86.ebx & 0xffff) != 0xaa55 || /* signature */
504 (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
515 * The "layered" ioctl to read disk/partition size. Unfortunately
516 * the zfsboot case is hardest, because we do not have full software
517 * stack available, so we need to do some manual work here.
520 ldi_get_size(void *priv)
522 struct zfsdsk *zdsk = priv;
523 uint64_t size = zdsk->dsk.size;
525 if (zdsk->dsk.start == 0)
526 size = drvsize_ext(zdsk);
528 return (size * DEV_BSIZE);
532 probe_drive(struct zfsdsk *zdsk)
537 unsigned part, entries_per_sec;
540 #if defined(GPT) || defined(LOADER_GELI_SUPPORT)
544 struct dos_partition *dp;
549 * If we find a vdev on the whole disk, stop here.
551 if (vdev_probe(vdev_read2, zdsk, NULL) == 0)
554 #ifdef LOADER_GELI_SUPPORT
556 * Taste the disk, if it is GELI encrypted, decrypt it and check to see if
557 * it is a usable vdev then. Otherwise dig
558 * out the partition table and probe each slice/partition
559 * in turn for a vdev or GELI encrypted vdev.
561 elba = drvsize_ext(zdsk);
565 zdsk->gdev = geli_taste(vdev_read, zdsk, elba, "disk%u:0:");
566 if (zdsk->gdev != NULL) {
567 if (geli_havekey(zdsk->gdev) == 0 ||
568 geli_passphrase(zdsk->gdev, gelipw) == 0) {
569 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
574 #endif /* LOADER_GELI_SUPPORT */
576 sec = dmadat->secbuf;
581 * First check for GPT.
583 if (drvread(&zdsk->dsk, sec, 1, 1)) {
586 memcpy(&hdr, sec, sizeof(hdr));
587 if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
588 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
589 hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
594 * Probe all GPT partitions for the presence of ZFS pools. We
595 * return the spa_t for the first we find (if requested). This
596 * will have the effect of booting from the first pool on the
599 * If no vdev is found, GELI decrypting the device and try again
601 entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
602 slba = hdr.hdr_lba_table;
603 elba = slba + hdr.hdr_entries / entries_per_sec;
604 while (slba < elba) {
606 if (drvread(&zdsk->dsk, sec, slba, 1))
608 for (part = 0; part < entries_per_sec; part++) {
609 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
610 if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
611 sizeof(uuid_t)) == 0) {
612 zdsk->dsk.start = ent->ent_lba_start;
613 zdsk->dsk.size = ent->ent_lba_end - ent->ent_lba_start + 1;
614 zdsk->dsk.slice = part + 1;
615 zdsk->dsk.part = 255;
616 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
618 * This slice had a vdev. We need a new dsk
619 * structure now since the vdev now owns this one.
621 zdsk = copy_dsk(zdsk);
623 #ifdef LOADER_GELI_SUPPORT
624 else if ((zdsk->gdev = geli_taste(vdev_read, zdsk,
625 ent->ent_lba_end - ent->ent_lba_start, "disk%up%u:",
626 zdsk->dsk.unit, zdsk->dsk.slice)) != NULL) {
627 if (geli_havekey(zdsk->gdev) == 0 ||
628 geli_passphrase(zdsk->gdev, gelipw) == 0) {
630 * This slice has GELI, check it for ZFS.
632 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
634 * This slice had a vdev. We need a new dsk
635 * structure now since the vdev now owns this one.
637 zdsk = copy_dsk(zdsk);
642 #endif /* LOADER_GELI_SUPPORT */
651 if (drvread(&zdsk->dsk, sec, DOSBBSECTOR, 1))
653 dp = (void *)(sec + DOSPARTOFF);
655 for (i = 0; i < NDOSPART; i++) {
658 zdsk->dsk.start = dp[i].dp_start;
659 zdsk->dsk.size = dp[i].dp_size;
660 zdsk->dsk.slice = i + 1;
661 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
662 zdsk = copy_dsk(zdsk);
664 #ifdef LOADER_GELI_SUPPORT
665 else if ((zdsk->gdev = geli_taste(vdev_read, zdsk, dp[i].dp_size -
666 dp[i].dp_start, "disk%us%u:")) != NULL) {
667 if (geli_havekey(zdsk->gdev) == 0 ||
668 geli_passphrase(zdsk->gdev, gelipw) == 0) {
670 * This slice has GELI, check it for ZFS.
672 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
674 * This slice had a vdev. We need a new dsk
675 * structure now since the vdev now owns this one.
677 zdsk = copy_dsk(zdsk);
682 #endif /* LOADER_GELI_SUPPORT */
696 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
700 if (high_heap_size > 0) {
701 heap_end = PTOV(high_heap_base + high_heap_size);
702 heap_next = PTOV(high_heap_base);
704 heap_next = (char *)dmadat + sizeof(*dmadat);
705 heap_end = (char *)PTOV(bios_basemem);
707 setheap(heap_next, heap_end);
709 zdsk = calloc(1, sizeof(struct zfsdsk));
710 zdsk->dsk.drive = *(uint8_t *)PTOV(ARGS);
711 zdsk->dsk.type = zdsk->dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
712 zdsk->dsk.unit = zdsk->dsk.drive & DRV_MASK;
713 zdsk->dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
716 zdsk->dsk.size = drvsize_ext(zdsk);
718 bootinfo.bi_version = BOOTINFO_VERSION;
719 bootinfo.bi_size = sizeof(bootinfo);
720 bootinfo.bi_basemem = bios_basemem / 1024;
721 bootinfo.bi_extmem = bios_extmem / 1024;
722 bootinfo.bi_memsizes_valid++;
723 bootinfo.bi_bios_dev = zdsk->dsk.drive;
725 bootdev = MAKEBOOTDEV(dev_maj[zdsk->dsk.type],
726 zdsk->dsk.slice, zdsk->dsk.unit, zdsk->dsk.part);
728 /* Process configuration file */
735 * Probe the boot drive first - we will try to boot from whatever
736 * pool we find on that drive.
741 * Probe the rest of the drives that the bios knows about. This
742 * will find any other available pools and it may fill in missing
743 * vdevs for the boot pool.
746 for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++)
748 for (i = 0; i < MAXBDDEV; i++)
751 if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
754 if (!int13probe(i | DRV_HARD))
757 zdsk = calloc(1, sizeof(struct zfsdsk));
758 zdsk->dsk.drive = i | DRV_HARD;
759 zdsk->dsk.type = zdsk->dsk.drive & TYPE_AD;
764 zdsk->dsk.size = drvsize_ext(zdsk);
769 * The first discovered pool, if any, is the pool.
771 spa = spa_get_primary();
773 printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
779 primary_vdev = spa_get_primary_vdev(spa);
782 rc = vdev_read_pad2(primary_vdev, cmd, sizeof(cmd));
783 if (vdev_clear_pad2(primary_vdev))
784 printf("failed to clear pad2 area of primary vdev\n");
788 * We could find an old-style ZFS Boot Block header here.
791 if (*(uint64_t *)cmd != 0x2f5b007b10c) {
793 * Note that parse() is destructive to cmd[] and we also want
794 * to honor RBX_QUIET option that could be present in cmd[].
797 memcpy(cmddup, cmd, sizeof(cmd));
799 printf("failed to parse pad2 area of primary vdev\n");
802 if (!OPT_CHECK(RBX_QUIET))
803 printf("zfs nextboot: %s\n", cmddup);
805 /* Do not process this command twice */
809 printf("failed to read pad2 area of primary vdev\n");
811 /* Mount ZFS only if it's not already mounted via nextboot parsing. */
812 if (zfsmount.spa == NULL &&
813 (zfs_spa_init(spa) != 0 || zfs_mount(spa, 0, &zfsmount) != 0)) {
814 printf("%s: failed to mount default pool %s\n",
815 BOOTPROG, spa->spa_name);
817 } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 ||
818 zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) {
820 zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
825 * Note that parse_cmd() is destructive to cmd[] and we also want
826 * to honor RBX_QUIET option that could be present in cmd[].
828 memcpy(cmddup, cmd, sizeof(cmd));
831 if (!OPT_CHECK(RBX_QUIET))
832 printf("%s: %s\n", PATH_CONFIG, cmddup);
833 /* Do not process this command twice */
837 /* Do not risk waiting at the prompt forever. */
838 if (nextboot && !autoboot)
842 * Try to exec /boot/loader. If interrupted by a keypress,
843 * or in case of failure, try to load a kernel directly instead.
846 if (autoboot && !*kname) {
847 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
850 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
854 /* Present the user with the boot2 prompt. */
857 if (!autoboot || !OPT_CHECK(RBX_QUIET)) {
858 printf("\nFreeBSD/x86 boot\n");
859 if (zfs_rlookup(spa, zfsmount.rootobj, rootname) != 0)
860 printf("Default: %s/<0x%llx>:%s\n"
862 spa->spa_name, zfsmount.rootobj, kname);
863 else if (rootname[0] != '\0')
864 printf("Default: %s/%s:%s\n"
866 spa->spa_name, rootname, kname);
868 printf("Default: %s:%s\n"
870 spa->spa_name, kname);
872 if (ioctrl & IO_SERIAL)
874 if (!autoboot || keyhit(5))
875 getstr(cmd, sizeof(cmd));
876 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
886 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
906 static Elf32_Phdr ep[2];
907 static Elf32_Shdr es[2];
914 if (zfs_lookup(&zfsmount, kname, &dn)) {
915 printf("\nCan't find %s\n", kname);
919 if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
921 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
923 else if (IS_ELF(hdr.eh))
926 printf("Invalid %s\n", "format");
930 addr = hdr.ex.a_entry & 0xffffff;
933 if (xfsread(&dn, &off, p, hdr.ex.a_text))
935 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
936 if (xfsread(&dn, &off, p, hdr.ex.a_data))
938 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
939 bootinfo.bi_symtab = VTOP(p);
940 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
941 p += sizeof(hdr.ex.a_syms);
943 if (xfsread(&dn, &off, p, hdr.ex.a_syms))
946 if (xfsread(&dn, &off, p, sizeof(int)))
951 if (xfsread(&dn, &off, p, x))
956 off = hdr.eh.e_phoff;
957 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
958 if (xfsread(&dn, &off, ep + j, sizeof(ep[0])))
960 if (ep[j].p_type == PT_LOAD)
963 for (i = 0; i < 2; i++) {
964 p = PTOV(ep[i].p_paddr & 0xffffff);
965 off = ep[i].p_offset;
966 if (xfsread(&dn, &off, p, ep[i].p_filesz))
969 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
970 bootinfo.bi_symtab = VTOP(p);
971 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
972 off = hdr.eh.e_shoff + sizeof(es[0]) *
973 (hdr.eh.e_shstrndx + 1);
974 if (xfsread(&dn, &off, &es, sizeof(es)))
976 for (i = 0; i < 2; i++) {
977 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
978 p += sizeof(es[i].sh_size);
979 off = es[i].sh_offset;
980 if (xfsread(&dn, &off, p, es[i].sh_size))
985 addr = hdr.eh.e_entry & 0xffffff;
987 bootinfo.bi_esymtab = VTOP(p);
988 bootinfo.bi_kernelname = VTOP(kname);
989 zfsargs.size = sizeof(zfsargs);
990 zfsargs.pool = zfsmount.spa->spa_guid;
991 zfsargs.root = zfsmount.rootobj;
992 zfsargs.primary_pool = primary_spa->spa_guid;
993 #ifdef LOADER_GELI_SUPPORT
994 explicit_bzero(gelipw, sizeof(gelipw));
995 export_geli_boot_data(&zfsargs.gelidata);
997 if (primary_vdev != NULL)
998 zfsargs.primary_vdev = primary_vdev->v_guid;
1000 printf("failed to detect primary vdev\n");
1002 * Note that the zfsargs struct is passed by value, not by pointer. Code in
1003 * btxldr.S copies the values from the entry stack to a fixed location
1004 * within loader(8) at startup due to the presence of KARGS_FLAGS_EXTARG.
1006 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
1008 KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
1009 (uint32_t) spa->spa_guid,
1010 (uint32_t) (spa->spa_guid >> 32),
1016 zfs_mount_ds(char *dsname)
1022 q = strchr(dsname, '/');
1025 newspa = spa_find_by_name(dsname);
1026 if (newspa == NULL) {
1027 printf("\nCan't find ZFS pool %s\n", dsname);
1031 if (zfs_spa_init(newspa))
1036 if (zfs_lookup_dataset(newspa, q, &newroot)) {
1037 printf("\nCan't find dataset %s in ZFS pool %s\n",
1038 q, newspa->spa_name);
1042 if (zfs_mount(newspa, newroot, &zfsmount)) {
1043 printf("\nCan't mount ZFS dataset\n");
1058 while ((c = *arg++)) {
1059 if (c == ' ' || c == '\t' || c == '\n')
1061 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
1066 while ((c = *arg++)) {
1068 if (*(uint8_t *)PTOV(0x496) & 0x10) {
1071 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
1074 printf("Keyboard: %s\n", cp);
1076 } else if (c == 'S') {
1078 while ((unsigned int)(i = *arg++ - '0') <= 9)
1080 if (j > 0 && i == -'0') {
1084 /* Fall through to error below ('S' not in optstr[]). */
1086 for (i = 0; c != optstr[i]; i++)
1089 opts ^= OPT_SET(flags[i]);
1091 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
1092 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
1093 if (ioctrl & IO_SERIAL) {
1094 if (sio_init(115200 / comspeed) != 0)
1095 ioctrl &= ~IO_SERIAL;
1100 if (zfs_lookup(&zfsmount, arg, &dn) == 0) {
1108 * Report pool status if the comment is 'status'. Lets
1109 * hope no-one wants to load /status as a kernel.
1111 if (!strcmp(arg, "status")) {
1117 * If there is "zfs:" prefix simply ignore it.
1119 if (strncmp(arg, "zfs:", 4) == 0)
1123 * If there is a colon, switch pools.
1125 q = strchr(arg, ':');
1128 if (zfs_mount_ds(arg) != 0)
1132 if ((i = ep - arg)) {
1133 if ((size_t)i >= sizeof(kname))
1135 memcpy(kname, arg, i + 1);