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