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, size_t size)
167 const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus;
172 if (*offp + n > zp->zp_size)
173 n = zp->zp_size - *offp;
175 rc = dnode_read(spa, dnode, *offp, start, n);
187 static spa_t *primary_spa;
188 static vdev_t *primary_vdev;
191 * A wrapper for dskread that doesn't have to worry about whether the
192 * buffer pointer crosses a 64k boundary.
195 vdev_read(void *xvdev, void *priv, off_t off, void *buf, size_t bytes)
198 daddr_t lba, alignlba;
200 unsigned int nb, alignnb;
201 struct zfsdsk *zdsk = (struct zfsdsk *) priv;
203 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
207 lba = off / DEV_BSIZE;
208 lba += zdsk->dsk.start;
210 * Align reads to 4k else 4k sector GELIs will not decrypt.
211 * Round LBA down to nearest multiple of DEV_GELIBOOT_BSIZE bytes.
213 alignlba = rounddown2(off, DEV_GELIBOOT_BSIZE) / DEV_BSIZE;
215 * The read must be aligned to DEV_GELIBOOT_BSIZE bytes relative to the
216 * start of the GELI partition, not the start of the actual disk.
218 alignlba += zdsk->dsk.start;
219 diff = (lba - alignlba) * DEV_BSIZE;
222 nb = bytes / DEV_BSIZE;
224 * Ensure that the read size plus the leading offset does not
225 * exceed the size of the read buffer.
227 if (nb > (READ_BUF_SIZE - diff) / DEV_BSIZE)
228 nb = (READ_BUF_SIZE - diff) / DEV_BSIZE;
230 * Round the number of blocks to read up to the nearest multiple
231 * of DEV_GELIBOOT_BSIZE.
233 alignnb = roundup2(nb * DEV_BSIZE + diff, DEV_GELIBOOT_BSIZE)
236 if (zdsk->dsk.size > 0 && alignlba + alignnb >
237 zdsk->dsk.size + zdsk->dsk.start) {
238 printf("Shortening read at %lld from %d to %lld\n",
240 (zdsk->dsk.size + zdsk->dsk.start) - alignlba);
241 alignnb = (zdsk->dsk.size + zdsk->dsk.start) - alignlba;
244 if (drvread(&zdsk->dsk, dmadat->rdbuf, alignlba, alignnb))
246 #ifdef LOADER_GELI_SUPPORT
248 if (zdsk->gdev != NULL) {
249 if (geli_read(zdsk->gdev, ((alignlba - zdsk->dsk.start) *
250 DEV_BSIZE), dmadat->rdbuf, alignnb * DEV_BSIZE))
254 memcpy(p, dmadat->rdbuf + diff, nb * DEV_BSIZE);
258 bytes -= nb * DEV_BSIZE;
259 /* Don't need the leading offset after the first block. */
265 /* Match the signature exactly due to signature madness */
267 vdev_read2(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
269 return vdev_read(vdev, priv, off, buf, bytes);
274 vdev_write(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
279 struct zfsdsk *zdsk = (struct zfsdsk *) priv;
281 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
285 lba = off / DEV_BSIZE;
286 lba += zdsk->dsk.start;
288 nb = bytes / DEV_BSIZE;
289 if (nb > READ_BUF_SIZE / DEV_BSIZE)
290 nb = READ_BUF_SIZE / DEV_BSIZE;
291 memcpy(dmadat->rdbuf, p, nb * DEV_BSIZE);
292 if (drvwrite(&zdsk->dsk, dmadat->rdbuf, lba, nb))
296 bytes -= nb * DEV_BSIZE;
303 xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte)
305 if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) {
306 printf("Invalid format\n");
313 * Read Pad2 (formerly "Boot Block Header") area of the first
314 * vdev label of the given vdev.
317 vdev_read_pad2(vdev_t *vdev, char *buf, size_t size)
320 char *tmp = zap_scratch;
321 off_t off = offsetof(vdev_label_t, vl_pad2);
323 if (size > VDEV_PAD_SIZE)
324 size = VDEV_PAD_SIZE;
327 BP_SET_LSIZE(&bp, VDEV_PAD_SIZE);
328 BP_SET_PSIZE(&bp, VDEV_PAD_SIZE);
329 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
330 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
331 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
332 if (vdev_read_phys(vdev, &bp, tmp, off, 0))
334 memcpy(buf, tmp, size);
339 vdev_clear_pad2(vdev_t *vdev)
341 char *zeroes = zap_scratch;
343 off_t off = offsetof(vdev_label_t, vl_pad2);
345 memset(zeroes, 0, VDEV_PAD_SIZE);
346 end = (uint64_t *)(zeroes + VDEV_PAD_SIZE);
347 /* ZIO_CHECKSUM_LABEL magic and pre-calcualted checksum for all zeros */
348 end[-5] = 0x0210da7ab10c7a11;
349 end[-4] = 0x97f48f807f6e2a3f;
350 end[-3] = 0xaf909f1658aacefc;
351 end[-2] = 0xcbd1ea57ff6db48b;
352 end[-1] = 0x6ec692db0d465fab;
353 if (vdev_write(vdev, vdev->v_read_priv, off, zeroes, VDEV_PAD_SIZE))
363 /* Parse system memory map */
367 v86.addr = 0x15; /* int 0x15 function 0xe820*/
369 v86.ecx = sizeof(struct bios_smap);
371 v86.es = VTOPSEG(&smap);
372 v86.edi = VTOPOFF(&smap);
374 if (V86_CY(v86.efl) || (v86.eax != SMAP_SIG))
376 /* look for a low-memory segment that's large enough */
377 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
378 (smap.length >= (512 * 1024)))
379 bios_basemem = smap.length;
380 /* look for the first segment in 'extended' memory */
381 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000)) {
382 bios_extmem = smap.length;
386 * Look for the largest segment in 'extended' memory beyond
389 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
390 (smap.base < 0x100000000ull)) {
394 * If this segment crosses the 4GB boundary, truncate it.
396 if (smap.base + size > 0x100000000ull)
397 size = 0x100000000ull - smap.base;
399 if (size > high_heap_size) {
400 high_heap_size = size;
401 high_heap_base = smap.base;
404 } while (v86.ebx != 0);
406 /* Fall back to the old compatibility function for base memory */
407 if (bios_basemem == 0) {
409 v86.addr = 0x12; /* int 0x12 */
412 bios_basemem = (v86.eax & 0xffff) * 1024;
415 /* Fall back through several compatibility functions for extended memory */
416 if (bios_extmem == 0) {
418 v86.addr = 0x15; /* int 0x15 function 0xe801*/
421 if (!V86_CY(v86.efl)) {
422 bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
425 if (bios_extmem == 0) {
427 v86.addr = 0x15; /* int 0x15 function 0x88*/
430 bios_extmem = (v86.eax & 0xffff) * 1024;
434 * If we have extended memory and did not find a suitable heap
435 * region in the SMAP, use the last 3MB of 'extended' memory as a
436 * high heap candidate.
438 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
439 high_heap_size = HEAP_MIN;
440 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
445 * Try to detect a device supported by the legacy int13 BIOS
448 int13probe(int drive)
456 if (!V86_CY(v86.efl) && /* carry clear */
457 ((v86.edx & 0xff) != (drive & DRV_MASK))) { /* unit # OK */
458 if ((v86.ecx & 0x3f) == 0) { /* absurd sector size */
459 return(0); /* skip device */
467 * We call this when we find a ZFS vdev - ZFS consumes the dsk
468 * structure so we must make a new one.
470 static struct zfsdsk *
471 copy_dsk(struct zfsdsk *zdsk)
473 struct zfsdsk *newdsk;
475 newdsk = malloc(sizeof(struct zfsdsk));
481 * Get disk size from eax=0x800 and 0x4800. We need to probe both
482 * because 0x4800 may not be available and we would like to get more
483 * or less correct disk size - if it is possible at all.
484 * Note we do not really want to touch drv.c because that code is shared
485 * with boot2 and we can not afford to grow that code.
488 drvsize_ext(struct zfsdsk *zdsk)
499 v86.edx = dskp->drive;
502 /* Don't error out if we get bad sector number, try EDD as well */
503 if (V86_CY(v86.efl) || /* carry set */
504 (v86.edx & 0xff) <= (unsigned)(dskp->drive & 0x7f)) /* unit # bad */
506 cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
507 /* Convert max head # -> # of heads */
508 hds = ((v86.edx & 0xff00) >> 8) + 1;
509 sec = v86.ecx & 0x3f;
511 size = (uint64_t)cyl * hds * sec;
513 /* Determine if we can use EDD with this device. */
517 v86.edx = dskp->drive;
520 if (V86_CY(v86.efl) || /* carry set */
521 (v86.ebx & 0xffff) != 0xaa55 || /* signature */
522 (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
533 * The "layered" ioctl to read disk/partition size. Unfortunately
534 * the zfsboot case is hardest, because we do not have full software
535 * stack available, so we need to do some manual work here.
538 ldi_get_size(void *priv)
540 struct zfsdsk *zdsk = priv;
541 uint64_t size = zdsk->dsk.size;
543 if (zdsk->dsk.start == 0)
544 size = drvsize_ext(zdsk);
546 return (size * DEV_BSIZE);
550 probe_drive(struct zfsdsk *zdsk)
555 unsigned part, entries_per_sec;
558 #if defined(GPT) || defined(LOADER_GELI_SUPPORT)
562 struct dos_partition *dp;
567 * If we find a vdev on the whole disk, stop here.
569 if (vdev_probe(vdev_read2, zdsk, NULL) == 0)
572 #ifdef LOADER_GELI_SUPPORT
574 * Taste the disk, if it is GELI encrypted, decrypt it and check to see if
575 * it is a usable vdev then. Otherwise dig
576 * out the partition table and probe each slice/partition
577 * in turn for a vdev or GELI encrypted vdev.
579 elba = drvsize_ext(zdsk);
583 zdsk->gdev = geli_taste(vdev_read, zdsk, elba, "disk%u:0:");
584 if (zdsk->gdev != NULL) {
585 if (geli_havekey(zdsk->gdev) == 0 ||
586 geli_passphrase(zdsk->gdev, gelipw) == 0) {
587 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
592 #endif /* LOADER_GELI_SUPPORT */
594 sec = dmadat->secbuf;
599 * First check for GPT.
601 if (drvread(&zdsk->dsk, sec, 1, 1)) {
604 memcpy(&hdr, sec, sizeof(hdr));
605 if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
606 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
607 hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
612 * Probe all GPT partitions for the presence of ZFS pools. We
613 * return the spa_t for the first we find (if requested). This
614 * will have the effect of booting from the first pool on the
617 * If no vdev is found, GELI decrypting the device and try again
619 entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
620 slba = hdr.hdr_lba_table;
621 elba = slba + hdr.hdr_entries / entries_per_sec;
622 while (slba < elba) {
624 if (drvread(&zdsk->dsk, sec, slba, 1))
626 for (part = 0; part < entries_per_sec; part++) {
627 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
628 if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
629 sizeof(uuid_t)) == 0) {
630 zdsk->dsk.start = ent->ent_lba_start;
631 zdsk->dsk.size = ent->ent_lba_end - ent->ent_lba_start + 1;
632 zdsk->dsk.slice = part + 1;
633 zdsk->dsk.part = 255;
634 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
636 * This slice had a vdev. We need a new dsk
637 * structure now since the vdev now owns this one.
639 zdsk = copy_dsk(zdsk);
641 #ifdef LOADER_GELI_SUPPORT
642 else if ((zdsk->gdev = geli_taste(vdev_read, zdsk,
643 ent->ent_lba_end - ent->ent_lba_start, "disk%up%u:",
644 zdsk->dsk.unit, zdsk->dsk.slice)) != NULL) {
645 if (geli_havekey(zdsk->gdev) == 0 ||
646 geli_passphrase(zdsk->gdev, gelipw) == 0) {
648 * This slice has GELI, check it for ZFS.
650 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
652 * This slice had a vdev. We need a new dsk
653 * structure now since the vdev now owns this one.
655 zdsk = copy_dsk(zdsk);
660 #endif /* LOADER_GELI_SUPPORT */
669 if (drvread(&zdsk->dsk, sec, DOSBBSECTOR, 1))
671 dp = (void *)(sec + DOSPARTOFF);
673 for (i = 0; i < NDOSPART; i++) {
676 zdsk->dsk.start = dp[i].dp_start;
677 zdsk->dsk.size = dp[i].dp_size;
678 zdsk->dsk.slice = i + 1;
679 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
680 zdsk = copy_dsk(zdsk);
682 #ifdef LOADER_GELI_SUPPORT
683 else if ((zdsk->gdev = geli_taste(vdev_read, zdsk, dp[i].dp_size -
684 dp[i].dp_start, "disk%us%u:")) != NULL) {
685 if (geli_havekey(zdsk->gdev) == 0 ||
686 geli_passphrase(zdsk->gdev, gelipw) == 0) {
688 * This slice has GELI, check it for ZFS.
690 if (vdev_probe(vdev_read2, zdsk, NULL) == 0) {
692 * This slice had a vdev. We need a new dsk
693 * structure now since the vdev now owns this one.
695 zdsk = copy_dsk(zdsk);
700 #endif /* LOADER_GELI_SUPPORT */
714 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
718 if (high_heap_size > 0) {
719 heap_end = PTOV(high_heap_base + high_heap_size);
720 heap_next = PTOV(high_heap_base);
722 heap_next = (char *)dmadat + sizeof(*dmadat);
723 heap_end = (char *)PTOV(bios_basemem);
725 setheap(heap_next, heap_end);
727 zdsk = calloc(1, sizeof(struct zfsdsk));
728 zdsk->dsk.drive = *(uint8_t *)PTOV(ARGS);
729 zdsk->dsk.type = zdsk->dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
730 zdsk->dsk.unit = zdsk->dsk.drive & DRV_MASK;
731 zdsk->dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
734 zdsk->dsk.size = drvsize_ext(zdsk);
736 bootinfo.bi_version = BOOTINFO_VERSION;
737 bootinfo.bi_size = sizeof(bootinfo);
738 bootinfo.bi_basemem = bios_basemem / 1024;
739 bootinfo.bi_extmem = bios_extmem / 1024;
740 bootinfo.bi_memsizes_valid++;
741 bootinfo.bi_bios_dev = zdsk->dsk.drive;
743 bootdev = MAKEBOOTDEV(dev_maj[zdsk->dsk.type],
744 zdsk->dsk.slice, zdsk->dsk.unit, zdsk->dsk.part);
746 /* Process configuration file */
753 * Probe the boot drive first - we will try to boot from whatever
754 * pool we find on that drive.
759 * Probe the rest of the drives that the bios knows about. This
760 * will find any other available pools and it may fill in missing
761 * vdevs for the boot pool.
764 for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++)
766 for (i = 0; i < MAXBDDEV; i++)
769 if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
772 if (!int13probe(i | DRV_HARD))
775 zdsk = calloc(1, sizeof(struct zfsdsk));
776 zdsk->dsk.drive = i | DRV_HARD;
777 zdsk->dsk.type = zdsk->dsk.drive & TYPE_AD;
782 zdsk->dsk.size = drvsize_ext(zdsk);
787 * The first discovered pool, if any, is the pool.
789 spa = spa_get_primary();
791 printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
797 primary_vdev = spa_get_primary_vdev(spa);
800 rc = vdev_read_pad2(primary_vdev, cmd, sizeof(cmd));
801 if (vdev_clear_pad2(primary_vdev))
802 printf("failed to clear pad2 area of primary vdev\n");
806 * We could find an old-style ZFS Boot Block header here.
809 if (*(uint64_t *)cmd != 0x2f5b007b10c) {
811 * Note that parse() is destructive to cmd[] and we also want
812 * to honor RBX_QUIET option that could be present in cmd[].
815 memcpy(cmddup, cmd, sizeof(cmd));
817 printf("failed to parse pad2 area of primary vdev\n");
820 if (!OPT_CHECK(RBX_QUIET))
821 printf("zfs nextboot: %s\n", cmddup);
823 /* Do not process this command twice */
827 printf("failed to read pad2 area of primary vdev\n");
829 /* Mount ZFS only if it's not already mounted via nextboot parsing. */
830 if (zfsmount.spa == NULL &&
831 (zfs_spa_init(spa) != 0 || zfs_mount(spa, 0, &zfsmount) != 0)) {
832 printf("%s: failed to mount default pool %s\n",
833 BOOTPROG, spa->spa_name);
835 } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 ||
836 zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) {
838 zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
843 * Note that parse_cmd() is destructive to cmd[] and we also want
844 * to honor RBX_QUIET option that could be present in cmd[].
846 memcpy(cmddup, cmd, sizeof(cmd));
849 if (!OPT_CHECK(RBX_QUIET))
850 printf("%s: %s\n", PATH_CONFIG, cmddup);
851 /* Do not process this command twice */
855 /* Do not risk waiting at the prompt forever. */
856 if (nextboot && !autoboot)
859 if (autoboot && !*kname) {
861 * Iterate through the list of loader and kernel paths, trying to load.
862 * If interrupted by a keypress, or in case of failure, drop the user
863 * to the boot2 prompt.
865 for (i = 0; i < nitems(loadpath); i++) {
866 memcpy(kname, loadpath[i].p, loadpath[i].len);
873 /* Present the user with the boot2 prompt. */
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"
881 spa->spa_name, zfsmount.rootobj, kname);
882 else if (rootname[0] != '\0')
883 printf("Default: %s/%s:%s\n"
885 spa->spa_name, rootname, kname);
887 printf("Default: %s:%s\n"
889 spa->spa_name, kname);
891 if (ioctrl & IO_SERIAL)
893 if (!autoboot || keyhit(5))
894 getstr(cmd, sizeof(cmd));
895 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
905 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
925 static Elf32_Phdr ep[2];
926 static Elf32_Shdr es[2];
933 if (zfs_lookup(&zfsmount, kname, &dn)) {
934 printf("\nCan't find %s\n", kname);
938 if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
940 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
942 else if (IS_ELF(hdr.eh))
945 printf("Invalid %s\n", "format");
949 addr = hdr.ex.a_entry & 0xffffff;
952 if (xfsread(&dn, &off, p, hdr.ex.a_text))
954 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
955 if (xfsread(&dn, &off, p, hdr.ex.a_data))
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);
962 if (xfsread(&dn, &off, p, hdr.ex.a_syms))
965 if (xfsread(&dn, &off, p, sizeof(int)))
970 if (xfsread(&dn, &off, p, x))
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])))
979 if (ep[j].p_type == PT_LOAD)
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))
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)))
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))
1004 addr = hdr.eh.e_entry & 0xffffff;
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);
1016 if (primary_vdev != NULL)
1017 zfsargs.primary_vdev = primary_vdev->v_guid;
1019 printf("failed to detect primary vdev\n");
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.
1025 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
1027 KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
1028 (uint32_t) spa->spa_guid,
1029 (uint32_t) (spa->spa_guid >> 32),
1035 zfs_mount_ds(char *dsname)
1041 q = strchr(dsname, '/');
1044 newspa = spa_find_by_name(dsname);
1045 if (newspa == NULL) {
1046 printf("\nCan't find ZFS pool %s\n", dsname);
1050 if (zfs_spa_init(newspa))
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);
1061 if (zfs_mount(newspa, newroot, &zfsmount)) {
1062 printf("\nCan't mount ZFS dataset\n");
1077 while ((c = *arg++)) {
1078 if (c == ' ' || c == '\t' || c == '\n')
1080 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
1085 while ((c = *arg++)) {
1087 if (*(uint8_t *)PTOV(0x496) & 0x10) {
1090 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
1093 printf("Keyboard: %s\n", cp);
1095 } else if (c == 'S') {
1097 while ((unsigned int)(i = *arg++ - '0') <= 9)
1099 if (j > 0 && i == -'0') {
1103 /* Fall through to error below ('S' not in optstr[]). */
1105 for (i = 0; c != optstr[i]; i++)
1108 opts ^= OPT_SET(flags[i]);
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;
1119 if (zfs_lookup(&zfsmount, arg, &dn) == 0) {
1127 * Report pool status if the comment is 'status'. Lets
1128 * hope no-one wants to load /status as a kernel.
1130 if (!strcmp(arg, "status")) {
1136 * If there is "zfs:" prefix simply ignore it.
1138 if (strncmp(arg, "zfs:", 4) == 0)
1142 * If there is a colon, switch pools.
1144 q = strchr(arg, ':');
1147 if (zfs_mount_ds(arg) != 0)
1151 if ((i = ep - arg)) {
1152 if ((size_t)i >= sizeof(kname))
1154 memcpy(kname, arg, i + 1);