zfsboot.c revision 308915
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: stable/10/sys/boot/i386/zfsboot/zfsboot.c 308915 2016-11-21 10:14:36Z avg $");
18
19#include <sys/param.h>
20#include <sys/errno.h>
21#include <sys/diskmbr.h>
22#ifdef GPT
23#include <sys/gpt.h>
24#endif
25#include <sys/reboot.h>
26#include <sys/queue.h>
27
28#include <machine/bootinfo.h>
29#include <machine/elf.h>
30#include <machine/pc/bios.h>
31
32#include <stdarg.h>
33#include <stddef.h>
34
35#include <a.out.h>
36
37#include <btxv86.h>
38
39#include "lib.h"
40#include "rbx.h"
41#include "drv.h"
42#include "util.h"
43#include "cons.h"
44#include "bootargs.h"
45#include "paths.h"
46
47#include "libzfs.h"
48
49#define ARGS		0x900
50#define NOPT		14
51#define NDEV		3
52
53#define BIOS_NUMDRIVES	0x475
54#define DRV_HARD	0x80
55#define DRV_MASK	0x7f
56
57#define TYPE_AD		0
58#define TYPE_DA		1
59#define TYPE_MAXHARD	TYPE_DA
60#define TYPE_FD		2
61
62extern uint32_t _end;
63
64#ifdef GPT
65static const uuid_t freebsd_zfs_uuid = GPT_ENT_TYPE_FREEBSD_ZFS;
66#endif
67static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
68static const unsigned char flags[NOPT] = {
69    RBX_DUAL,
70    RBX_SERIAL,
71    RBX_ASKNAME,
72    RBX_CDROM,
73    RBX_CONFIG,
74    RBX_KDB,
75    RBX_GDB,
76    RBX_MUTE,
77    RBX_NOINTR,
78    RBX_PAUSE,
79    RBX_QUIET,
80    RBX_DFLTROOT,
81    RBX_SINGLE,
82    RBX_VERBOSE
83};
84uint32_t opts;
85
86static const unsigned char dev_maj[NDEV] = {30, 4, 2};
87
88static char cmd[512];
89static char cmddup[512];
90static char kname[1024];
91static char rootname[256];
92static int comspeed = SIOSPD;
93static struct bootinfo bootinfo;
94static uint32_t bootdev;
95static struct zfs_boot_args zfsargs;
96static struct zfsmount zfsmount;
97
98vm_offset_t	high_heap_base;
99uint32_t	bios_basemem, bios_extmem, high_heap_size;
100
101static struct bios_smap smap;
102
103/*
104 * The minimum amount of memory to reserve in bios_extmem for the heap.
105 */
106#define	HEAP_MIN	(3 * 1024 * 1024)
107
108static char *heap_next;
109static char *heap_end;
110
111/* Buffers that must not span a 64k boundary. */
112#define READ_BUF_SIZE	8192
113struct dmadat {
114	char rdbuf[READ_BUF_SIZE];	/* for reading large things */
115	char secbuf[READ_BUF_SIZE];	/* for MBR/disklabel */
116};
117static struct dmadat *dmadat;
118
119void exit(int);
120void reboot(void);
121static void load(void);
122static int parse(void);
123static void bios_getmem(void);
124
125static void *
126malloc(size_t n)
127{
128	char *p = heap_next;
129	if (p + n > heap_end) {
130		printf("malloc failure\n");
131		for (;;)
132		    ;
133		return 0;
134	}
135	heap_next += n;
136	return p;
137}
138
139static char *
140strdup(const char *s)
141{
142	char *p = malloc(strlen(s) + 1);
143	strcpy(p, s);
144	return p;
145}
146
147#include "zfsimpl.c"
148
149/*
150 * Read from a dnode (which must be from a ZPL filesystem).
151 */
152static int
153zfs_read(spa_t *spa, const dnode_phys_t *dnode, off_t *offp, void *start, size_t size)
154{
155	const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus;
156	size_t n;
157	int rc;
158
159	n = size;
160	if (*offp + n > zp->zp_size)
161		n = zp->zp_size - *offp;
162
163	rc = dnode_read(spa, dnode, *offp, start, n);
164	if (rc)
165		return (-1);
166	*offp += n;
167
168	return (n);
169}
170
171/*
172 * Current ZFS pool
173 */
174static spa_t *spa;
175static spa_t *primary_spa;
176static vdev_t *primary_vdev;
177
178/*
179 * A wrapper for dskread that doesn't have to worry about whether the
180 * buffer pointer crosses a 64k boundary.
181 */
182static int
183vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
184{
185	char *p;
186	daddr_t lba;
187	unsigned int nb;
188	struct dsk *dsk = (struct dsk *) priv;
189
190	if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
191		return -1;
192
193	p = buf;
194	lba = off / DEV_BSIZE;
195	lba += dsk->start;
196	while (bytes > 0) {
197		nb = bytes / DEV_BSIZE;
198		if (nb > READ_BUF_SIZE / DEV_BSIZE)
199			nb = READ_BUF_SIZE / DEV_BSIZE;
200		if (drvread(dsk, dmadat->rdbuf, lba, nb))
201			return -1;
202		memcpy(p, dmadat->rdbuf, nb * DEV_BSIZE);
203		p += nb * DEV_BSIZE;
204		lba += nb;
205		bytes -= nb * DEV_BSIZE;
206	}
207
208	return 0;
209}
210
211static int
212vdev_write(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
213{
214	char *p;
215	daddr_t lba;
216	unsigned int nb;
217	struct dsk *dsk = (struct dsk *) priv;
218
219	if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
220		return -1;
221
222	p = buf;
223	lba = off / DEV_BSIZE;
224	lba += dsk->start;
225	while (bytes > 0) {
226		nb = bytes / DEV_BSIZE;
227		if (nb > READ_BUF_SIZE / DEV_BSIZE)
228			nb = READ_BUF_SIZE / DEV_BSIZE;
229		memcpy(dmadat->rdbuf, p, nb * DEV_BSIZE);
230		if (drvwrite(dsk, dmadat->rdbuf, lba, nb))
231			return -1;
232		p += nb * DEV_BSIZE;
233		lba += nb;
234		bytes -= nb * DEV_BSIZE;
235	}
236
237	return 0;
238}
239
240static int
241xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte)
242{
243    if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) {
244	printf("Invalid format\n");
245	return -1;
246    }
247    return 0;
248}
249
250/*
251 * Read Pad2 (formerly "Boot Block Header") area of the first
252 * vdev label of the given vdev.
253 */
254static int
255vdev_read_pad2(vdev_t *vdev, char *buf, size_t size)
256{
257	blkptr_t bp;
258	char *tmp = zap_scratch;
259	off_t off = offsetof(vdev_label_t, vl_pad2);
260
261	if (size > VDEV_PAD_SIZE)
262		size = VDEV_PAD_SIZE;
263
264	BP_ZERO(&bp);
265	BP_SET_LSIZE(&bp, VDEV_PAD_SIZE);
266	BP_SET_PSIZE(&bp, VDEV_PAD_SIZE);
267	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
268	BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
269	DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
270	if (vdev_read_phys(vdev, &bp, tmp, off, 0))
271		return (EIO);
272	memcpy(buf, tmp, size);
273	return (0);
274}
275
276static int
277vdev_clear_pad2(vdev_t *vdev)
278{
279	char *zeroes = zap_scratch;
280	uint64_t *end;
281	off_t off = offsetof(vdev_label_t, vl_pad2);
282
283	memset(zeroes, 0, VDEV_PAD_SIZE);
284	end = (uint64_t *)(zeroes + VDEV_PAD_SIZE);
285	/* ZIO_CHECKSUM_LABEL magic and pre-calcualted checksum for all zeros */
286	end[-5] = 0x0210da7ab10c7a11;
287	end[-4] = 0x97f48f807f6e2a3f;
288	end[-3] = 0xaf909f1658aacefc;
289	end[-2] = 0xcbd1ea57ff6db48b;
290	end[-1] = 0x6ec692db0d465fab;
291	if (vdev_write(vdev, vdev->v_read_priv, off, zeroes, VDEV_PAD_SIZE))
292		return (EIO);
293	return (0);
294}
295
296static void
297bios_getmem(void)
298{
299    uint64_t size;
300
301    /* Parse system memory map */
302    v86.ebx = 0;
303    do {
304	v86.ctl = V86_FLAGS;
305	v86.addr = 0x15;		/* int 0x15 function 0xe820*/
306	v86.eax = 0xe820;
307	v86.ecx = sizeof(struct bios_smap);
308	v86.edx = SMAP_SIG;
309	v86.es = VTOPSEG(&smap);
310	v86.edi = VTOPOFF(&smap);
311	v86int();
312	if (V86_CY(v86.efl) || (v86.eax != SMAP_SIG))
313	    break;
314	/* look for a low-memory segment that's large enough */
315	if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
316	    (smap.length >= (512 * 1024)))
317	    bios_basemem = smap.length;
318	/* look for the first segment in 'extended' memory */
319	if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000)) {
320	    bios_extmem = smap.length;
321	}
322
323	/*
324	 * Look for the largest segment in 'extended' memory beyond
325	 * 1MB but below 4GB.
326	 */
327	if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
328	    (smap.base < 0x100000000ull)) {
329	    size = smap.length;
330
331	    /*
332	     * If this segment crosses the 4GB boundary, truncate it.
333	     */
334	    if (smap.base + size > 0x100000000ull)
335		size = 0x100000000ull - smap.base;
336
337	    if (size > high_heap_size) {
338		high_heap_size = size;
339		high_heap_base = smap.base;
340	    }
341	}
342    } while (v86.ebx != 0);
343
344    /* Fall back to the old compatibility function for base memory */
345    if (bios_basemem == 0) {
346	v86.ctl = 0;
347	v86.addr = 0x12;		/* int 0x12 */
348	v86int();
349
350	bios_basemem = (v86.eax & 0xffff) * 1024;
351    }
352
353    /* Fall back through several compatibility functions for extended memory */
354    if (bios_extmem == 0) {
355	v86.ctl = V86_FLAGS;
356	v86.addr = 0x15;		/* int 0x15 function 0xe801*/
357	v86.eax = 0xe801;
358	v86int();
359	if (!V86_CY(v86.efl)) {
360	    bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
361	}
362    }
363    if (bios_extmem == 0) {
364	v86.ctl = 0;
365	v86.addr = 0x15;		/* int 0x15 function 0x88*/
366	v86.eax = 0x8800;
367	v86int();
368	bios_extmem = (v86.eax & 0xffff) * 1024;
369    }
370
371    /*
372     * If we have extended memory and did not find a suitable heap
373     * region in the SMAP, use the last 3MB of 'extended' memory as a
374     * high heap candidate.
375     */
376    if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
377	high_heap_size = HEAP_MIN;
378	high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
379    }
380}
381
382/*
383 * Try to detect a device supported by the legacy int13 BIOS
384 */
385static int
386int13probe(int drive)
387{
388    v86.ctl = V86_FLAGS;
389    v86.addr = 0x13;
390    v86.eax = 0x800;
391    v86.edx = drive;
392    v86int();
393
394    if (!V86_CY(v86.efl) &&				/* carry clear */
395	((v86.edx & 0xff) != (drive & DRV_MASK))) {	/* unit # OK */
396	if ((v86.ecx & 0x3f) == 0) {			/* absurd sector size */
397		return(0);				/* skip device */
398	}
399	return (1);
400    }
401    return(0);
402}
403
404/*
405 * We call this when we find a ZFS vdev - ZFS consumes the dsk
406 * structure so we must make a new one.
407 */
408static struct dsk *
409copy_dsk(struct dsk *dsk)
410{
411    struct dsk *newdsk;
412
413    newdsk = malloc(sizeof(struct dsk));
414    *newdsk = *dsk;
415    return (newdsk);
416}
417
418static void
419probe_drive(struct dsk *dsk)
420{
421#ifdef GPT
422    struct gpt_hdr hdr;
423    struct gpt_ent *ent;
424    daddr_t slba, elba;
425    unsigned part, entries_per_sec;
426#endif
427    struct dos_partition *dp;
428    char *sec;
429    unsigned i;
430
431    /*
432     * If we find a vdev on the whole disk, stop here. Otherwise dig
433     * out the partition table and probe each slice/partition
434     * in turn for a vdev.
435     */
436    if (vdev_probe(vdev_read, dsk, NULL) == 0)
437	return;
438
439    sec = dmadat->secbuf;
440    dsk->start = 0;
441
442#ifdef GPT
443    /*
444     * First check for GPT.
445     */
446    if (drvread(dsk, sec, 1, 1)) {
447	return;
448    }
449    memcpy(&hdr, sec, sizeof(hdr));
450    if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
451	hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
452	hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
453	goto trymbr;
454    }
455
456    /*
457     * Probe all GPT partitions for the presense of ZFS pools. We
458     * return the spa_t for the first we find (if requested). This
459     * will have the effect of booting from the first pool on the
460     * disk.
461     */
462    entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
463    slba = hdr.hdr_lba_table;
464    elba = slba + hdr.hdr_entries / entries_per_sec;
465    while (slba < elba) {
466	dsk->start = 0;
467	if (drvread(dsk, sec, slba, 1))
468	    return;
469	for (part = 0; part < entries_per_sec; part++) {
470	    ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
471	    if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
472		     sizeof(uuid_t)) == 0) {
473		dsk->start = ent->ent_lba_start;
474		if (vdev_probe(vdev_read, dsk, NULL) == 0) {
475		    /*
476		     * This slice had a vdev. We need a new dsk
477		     * structure now since the vdev now owns this one.
478		     */
479		    dsk = copy_dsk(dsk);
480		}
481	    }
482	}
483	slba++;
484    }
485    return;
486trymbr:
487#endif
488
489    if (drvread(dsk, sec, DOSBBSECTOR, 1))
490	return;
491    dp = (void *)(sec + DOSPARTOFF);
492
493    for (i = 0; i < NDOSPART; i++) {
494	if (!dp[i].dp_typ)
495	    continue;
496	dsk->start = dp[i].dp_start;
497	if (vdev_probe(vdev_read, dsk, NULL) == 0) {
498	    /*
499	     * This slice had a vdev. We need a new dsk structure now
500	     * since the vdev now owns this one.
501	     */
502	    dsk = copy_dsk(dsk);
503	}
504    }
505}
506
507int
508main(void)
509{
510    dnode_phys_t dn;
511    off_t off;
512    struct dsk *dsk;
513    int autoboot, i;
514    int nextboot;
515    int rc;
516
517    dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
518
519    bios_getmem();
520
521    if (high_heap_size > 0) {
522	heap_end = PTOV(high_heap_base + high_heap_size);
523	heap_next = PTOV(high_heap_base);
524    } else {
525	heap_next = (char *) dmadat + sizeof(*dmadat);
526	heap_end = (char *) PTOV(bios_basemem);
527    }
528
529    dsk = malloc(sizeof(struct dsk));
530    dsk->drive = *(uint8_t *)PTOV(ARGS);
531    dsk->type = dsk->drive & DRV_HARD ? TYPE_AD : TYPE_FD;
532    dsk->unit = dsk->drive & DRV_MASK;
533    dsk->slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
534    dsk->part = 0;
535    dsk->start = 0;
536    dsk->init = 0;
537
538    bootinfo.bi_version = BOOTINFO_VERSION;
539    bootinfo.bi_size = sizeof(bootinfo);
540    bootinfo.bi_basemem = bios_basemem / 1024;
541    bootinfo.bi_extmem = bios_extmem / 1024;
542    bootinfo.bi_memsizes_valid++;
543    bootinfo.bi_bios_dev = dsk->drive;
544
545    bootdev = MAKEBOOTDEV(dev_maj[dsk->type],
546			  dsk->slice, dsk->unit, dsk->part);
547
548    /* Process configuration file */
549
550    autoboot = 1;
551
552    zfs_init();
553
554    /*
555     * Probe the boot drive first - we will try to boot from whatever
556     * pool we find on that drive.
557     */
558    probe_drive(dsk);
559
560    /*
561     * Probe the rest of the drives that the bios knows about. This
562     * will find any other available pools and it may fill in missing
563     * vdevs for the boot pool.
564     */
565#ifndef VIRTUALBOX
566    for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++)
567#else
568    for (i = 0; i < MAXBDDEV; i++)
569#endif
570    {
571	if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
572	    continue;
573
574	if (!int13probe(i | DRV_HARD))
575	    break;
576
577	dsk = malloc(sizeof(struct dsk));
578	dsk->drive = i | DRV_HARD;
579	dsk->type = dsk->drive & TYPE_AD;
580	dsk->unit = i;
581	dsk->slice = 0;
582	dsk->part = 0;
583	dsk->start = 0;
584	dsk->init = 0;
585	probe_drive(dsk);
586    }
587
588    /*
589     * The first discovered pool, if any, is the pool.
590     */
591    spa = spa_get_primary();
592    if (!spa) {
593	printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
594	for (;;)
595	    ;
596    }
597
598    primary_spa = spa;
599    primary_vdev = spa_get_primary_vdev(spa);
600
601    nextboot = 0;
602    rc  = vdev_read_pad2(primary_vdev, cmd, sizeof(cmd));
603    if (vdev_clear_pad2(primary_vdev))
604	printf("failed to clear pad2 area of primary vdev\n");
605    if (rc == 0) {
606	if (*cmd) {
607	    /*
608	     * We could find an old-style ZFS Boot Block header here.
609	     * Simply ignore it.
610	     */
611	    if (*(uint64_t *)cmd != 0x2f5b007b10c) {
612		/*
613		 * Note that parse() is destructive to cmd[] and we also want
614		 * to honor RBX_QUIET option that could be present in cmd[].
615		 */
616		nextboot = 1;
617		memcpy(cmddup, cmd, sizeof(cmd));
618		if (parse()) {
619		    printf("failed to parse pad2 area of primary vdev\n");
620		    reboot();
621		}
622		if (!OPT_CHECK(RBX_QUIET))
623		    printf("zfs nextboot: %s\n", cmddup);
624	    }
625	    /* Do not process this command twice */
626	    *cmd = 0;
627	}
628    } else
629	printf("failed to read pad2 area of primary vdev\n");
630
631    /* Mount ZFS only if it's not already mounted via nextboot parsing. */
632    if (zfsmount.spa == NULL &&
633	(zfs_spa_init(spa) != 0 || zfs_mount(spa, 0, &zfsmount) != 0)) {
634	printf("%s: failed to mount default pool %s\n",
635	    BOOTPROG, spa->spa_name);
636	autoboot = 0;
637    } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 ||
638        zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) {
639	off = 0;
640	zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
641    }
642
643    if (*cmd) {
644	/*
645	 * Note that parse() is destructive to cmd[] and we also want
646	 * to honor RBX_QUIET option that could be present in cmd[].
647	 */
648	memcpy(cmddup, cmd, sizeof(cmd));
649	if (parse())
650	    autoboot = 0;
651	if (!OPT_CHECK(RBX_QUIET))
652	    printf("%s: %s\n", PATH_CONFIG, cmddup);
653	/* Do not process this command twice */
654	*cmd = 0;
655    }
656
657    /* Do not risk waiting at the prompt forever. */
658    if (nextboot && !autoboot)
659	reboot();
660
661    /*
662     * Try to exec /boot/loader. If interrupted by a keypress,
663     * or in case of failure, try to load a kernel directly instead.
664     */
665
666    if (autoboot && !*kname) {
667	memcpy(kname, PATH_LOADER_ZFS, sizeof(PATH_LOADER_ZFS));
668	if (!keyhit(3)) {
669	    load();
670	    memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
671	}
672    }
673
674    /* Present the user with the boot2 prompt. */
675
676    for (;;) {
677	if (!autoboot || !OPT_CHECK(RBX_QUIET)) {
678	    printf("\nFreeBSD/x86 boot\n");
679	    if (zfs_rlookup(spa, zfsmount.rootobj, rootname) != 0)
680		printf("Default: %s/<0x%llx>:%s\n"
681		       "boot: ",
682		       spa->spa_name, zfsmount.rootobj, kname);
683	    else if (rootname[0] != '\0')
684		printf("Default: %s/%s:%s\n"
685		       "boot: ",
686		       spa->spa_name, rootname, kname);
687	    else
688		printf("Default: %s:%s\n"
689		       "boot: ",
690		       spa->spa_name, kname);
691	}
692	if (ioctrl & IO_SERIAL)
693	    sio_flush();
694	if (!autoboot || keyhit(5))
695	    getstr(cmd, sizeof(cmd));
696	else if (!autoboot || !OPT_CHECK(RBX_QUIET))
697	    putchar('\n');
698	autoboot = 0;
699	if (parse())
700	    putchar('\a');
701	else
702	    load();
703    }
704}
705
706/* XXX - Needed for btxld to link the boot2 binary; do not remove. */
707void
708exit(int x)
709{
710    __exit(x);
711}
712
713void
714reboot(void)
715{
716    __exit(0);
717}
718
719static void
720load(void)
721{
722    union {
723	struct exec ex;
724	Elf32_Ehdr eh;
725    } hdr;
726    static Elf32_Phdr ep[2];
727    static Elf32_Shdr es[2];
728    caddr_t p;
729    dnode_phys_t dn;
730    off_t off;
731    uint32_t addr, x;
732    int fmt, i, j;
733
734    if (zfs_lookup(&zfsmount, kname, &dn)) {
735	printf("\nCan't find %s\n", kname);
736	return;
737    }
738    off = 0;
739    if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
740	return;
741    if (N_GETMAGIC(hdr.ex) == ZMAGIC)
742	fmt = 0;
743    else if (IS_ELF(hdr.eh))
744	fmt = 1;
745    else {
746	printf("Invalid %s\n", "format");
747	return;
748    }
749    if (fmt == 0) {
750	addr = hdr.ex.a_entry & 0xffffff;
751	p = PTOV(addr);
752	off = PAGE_SIZE;
753	if (xfsread(&dn, &off, p, hdr.ex.a_text))
754	    return;
755	p += roundup2(hdr.ex.a_text, PAGE_SIZE);
756	if (xfsread(&dn, &off, p, hdr.ex.a_data))
757	    return;
758	p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
759	bootinfo.bi_symtab = VTOP(p);
760	memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
761	p += sizeof(hdr.ex.a_syms);
762	if (hdr.ex.a_syms) {
763	    if (xfsread(&dn, &off, p, hdr.ex.a_syms))
764		return;
765	    p += hdr.ex.a_syms;
766	    if (xfsread(&dn, &off, p, sizeof(int)))
767		return;
768	    x = *(uint32_t *)p;
769	    p += sizeof(int);
770	    x -= sizeof(int);
771	    if (xfsread(&dn, &off, p, x))
772		return;
773	    p += x;
774	}
775    } else {
776	off = hdr.eh.e_phoff;
777	for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
778	    if (xfsread(&dn, &off, ep + j, sizeof(ep[0])))
779		return;
780	    if (ep[j].p_type == PT_LOAD)
781		j++;
782	}
783	for (i = 0; i < 2; i++) {
784	    p = PTOV(ep[i].p_paddr & 0xffffff);
785	    off = ep[i].p_offset;
786	    if (xfsread(&dn, &off, p, ep[i].p_filesz))
787		return;
788	}
789	p += roundup2(ep[1].p_memsz, PAGE_SIZE);
790	bootinfo.bi_symtab = VTOP(p);
791	if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
792	    off = hdr.eh.e_shoff + sizeof(es[0]) *
793		(hdr.eh.e_shstrndx + 1);
794	    if (xfsread(&dn, &off, &es, sizeof(es)))
795		return;
796	    for (i = 0; i < 2; i++) {
797		memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
798		p += sizeof(es[i].sh_size);
799		off = es[i].sh_offset;
800		if (xfsread(&dn, &off, p, es[i].sh_size))
801		    return;
802		p += es[i].sh_size;
803	    }
804	}
805	addr = hdr.eh.e_entry & 0xffffff;
806    }
807    bootinfo.bi_esymtab = VTOP(p);
808    bootinfo.bi_kernelname = VTOP(kname);
809    zfsargs.size = sizeof(zfsargs);
810    zfsargs.pool = zfsmount.spa->spa_guid;
811    zfsargs.root = zfsmount.rootobj;
812    zfsargs.primary_pool = primary_spa->spa_guid;
813    if (primary_vdev != NULL)
814	zfsargs.primary_vdev = primary_vdev->v_guid;
815    else
816	printf("failed to detect primary vdev\n");
817    __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
818	   bootdev,
819	   KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
820	   (uint32_t) spa->spa_guid,
821	   (uint32_t) (spa->spa_guid >> 32),
822	   VTOP(&bootinfo),
823	   zfsargs);
824}
825
826static int
827zfs_mount_ds(char *dsname)
828{
829    uint64_t newroot;
830    spa_t *newspa;
831    char *q;
832
833    q = strchr(dsname, '/');
834    if (q)
835	*q++ = '\0';
836    newspa = spa_find_by_name(dsname);
837    if (newspa == NULL) {
838	printf("\nCan't find ZFS pool %s\n", dsname);
839	return -1;
840    }
841
842    if (zfs_spa_init(newspa))
843	return -1;
844
845    newroot = 0;
846    if (q) {
847	if (zfs_lookup_dataset(newspa, q, &newroot)) {
848	    printf("\nCan't find dataset %s in ZFS pool %s\n",
849		    q, newspa->spa_name);
850	    return -1;
851	}
852    }
853    if (zfs_mount(newspa, newroot, &zfsmount)) {
854	printf("\nCan't mount ZFS dataset\n");
855	return -1;
856    }
857    spa = newspa;
858    return (0);
859}
860
861static int
862parse(void)
863{
864    char *arg = cmd;
865    char *ep, *p, *q;
866    const char *cp;
867    int c, i, j;
868
869    while ((c = *arg++)) {
870	if (c == ' ' || c == '\t' || c == '\n')
871	    continue;
872	for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
873	ep = p;
874	if (*p)
875	    *p++ = 0;
876	if (c == '-') {
877	    while ((c = *arg++)) {
878		if (c == 'P') {
879		    if (*(uint8_t *)PTOV(0x496) & 0x10) {
880			cp = "yes";
881		    } else {
882			opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
883			cp = "no";
884		    }
885		    printf("Keyboard: %s\n", cp);
886		    continue;
887		} else if (c == 'S') {
888		    j = 0;
889		    while ((unsigned int)(i = *arg++ - '0') <= 9)
890			j = j * 10 + i;
891		    if (j > 0 && i == -'0') {
892			comspeed = j;
893			break;
894		    }
895		    /* Fall through to error below ('S' not in optstr[]). */
896		}
897		for (i = 0; c != optstr[i]; i++)
898		    if (i == NOPT - 1)
899			return -1;
900		opts ^= OPT_SET(flags[i]);
901	    }
902	    ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
903		     OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
904	    if (ioctrl & IO_SERIAL) {
905	        if (sio_init(115200 / comspeed) != 0)
906		    ioctrl &= ~IO_SERIAL;
907	    }
908	} if (c == '?') {
909	    dnode_phys_t dn;
910
911	    if (zfs_lookup(&zfsmount, arg, &dn) == 0) {
912		zap_list(spa, &dn);
913	    }
914	    return -1;
915	} else {
916	    arg--;
917
918	    /*
919	     * Report pool status if the comment is 'status'. Lets
920	     * hope no-one wants to load /status as a kernel.
921	     */
922	    if (!strcmp(arg, "status")) {
923		spa_all_status();
924		return -1;
925	    }
926
927	    /*
928	     * If there is "zfs:" prefix simply ignore it.
929	     */
930	    if (strncmp(arg, "zfs:", 4) == 0)
931		arg += 4;
932
933	    /*
934	     * If there is a colon, switch pools.
935	     */
936	    q = strchr(arg, ':');
937	    if (q) {
938		*q++ = '\0';
939		if (zfs_mount_ds(arg) != 0)
940		    return -1;
941		arg = q;
942	    }
943	    if ((i = ep - arg)) {
944		if ((size_t)i >= sizeof(kname))
945		    return -1;
946		memcpy(kname, arg, i + 1);
947	    }
948	}
949	arg = p;
950    }
951    return 0;
952}
953