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$");
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)
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 partition table and probe each slice/partition
363     * in turn for a vdev.
364     */
365    if (vdev_probe(vdev_read, dsk, NULL) == 0)
366	return;
367
368    sec = dmadat->secbuf;
369    dsk->start = 0;
370
371#ifdef GPT
372    /*
373     * First check for GPT.
374     */
375    if (drvread(dsk, sec, 1, 1)) {
376	return;
377    }
378    memcpy(&hdr, sec, sizeof(hdr));
379    if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
380	hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
381	hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
382	goto trymbr;
383    }
384
385    /*
386     * Probe all GPT partitions for the presense of ZFS pools. We
387     * return the spa_t for the first we find (if requested). This
388     * will have the effect of booting from the first pool on the
389     * disk.
390     */
391    entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
392    slba = hdr.hdr_lba_table;
393    elba = slba + hdr.hdr_entries / entries_per_sec;
394    while (slba < elba) {
395	dsk->start = 0;
396	if (drvread(dsk, sec, slba, 1))
397	    return;
398	for (part = 0; part < entries_per_sec; part++) {
399	    ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
400	    if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
401		     sizeof(uuid_t)) == 0) {
402		dsk->start = ent->ent_lba_start;
403		if (vdev_probe(vdev_read, dsk, NULL) == 0) {
404		    /*
405		     * This slice had a vdev. We need a new dsk
406		     * structure now since the vdev now owns this one.
407		     */
408		    dsk = copy_dsk(dsk);
409		}
410	    }
411	}
412	slba++;
413    }
414    return;
415trymbr:
416#endif
417
418    if (drvread(dsk, sec, DOSBBSECTOR, 1))
419	return;
420    dp = (void *)(sec + DOSPARTOFF);
421
422    for (i = 0; i < NDOSPART; i++) {
423	if (!dp[i].dp_typ)
424	    continue;
425	dsk->start = dp[i].dp_start;
426	if (vdev_probe(vdev_read, dsk, NULL) == 0) {
427	    /*
428	     * This slice had a vdev. We need a new dsk structure now
429	     * since the vdev now owns this one.
430	     */
431	    dsk = copy_dsk(dsk);
432	}
433    }
434}
435
436int
437main(void)
438{
439    int autoboot, i;
440    dnode_phys_t dn;
441    off_t off;
442    struct dsk *dsk;
443
444    dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
445
446    bios_getmem();
447
448    if (high_heap_size > 0) {
449	heap_end = PTOV(high_heap_base + high_heap_size);
450	heap_next = PTOV(high_heap_base);
451    } else {
452	heap_next = (char *) dmadat + sizeof(*dmadat);
453	heap_end = (char *) PTOV(bios_basemem);
454    }
455
456    dsk = malloc(sizeof(struct dsk));
457    dsk->drive = *(uint8_t *)PTOV(ARGS);
458    dsk->type = dsk->drive & DRV_HARD ? TYPE_AD : TYPE_FD;
459    dsk->unit = dsk->drive & DRV_MASK;
460    dsk->slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
461    dsk->part = 0;
462    dsk->start = 0;
463    dsk->init = 0;
464
465    bootinfo.bi_version = BOOTINFO_VERSION;
466    bootinfo.bi_size = sizeof(bootinfo);
467    bootinfo.bi_basemem = bios_basemem / 1024;
468    bootinfo.bi_extmem = bios_extmem / 1024;
469    bootinfo.bi_memsizes_valid++;
470    bootinfo.bi_bios_dev = dsk->drive;
471
472    bootdev = MAKEBOOTDEV(dev_maj[dsk->type],
473			  dsk->slice, dsk->unit, dsk->part),
474
475    /* Process configuration file */
476
477    autoboot = 1;
478
479    zfs_init();
480
481    /*
482     * Probe the boot drive first - we will try to boot from whatever
483     * pool we find on that drive.
484     */
485    probe_drive(dsk);
486
487    /*
488     * Probe the rest of the drives that the bios knows about. This
489     * will find any other available pools and it may fill in missing
490     * vdevs for the boot pool.
491     */
492#ifndef VIRTUALBOX
493    for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++)
494#else
495    for (i = 0; i < MAXBDDEV; i++)
496#endif
497    {
498	if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
499	    continue;
500
501	if (!int13probe(i | DRV_HARD))
502	    break;
503
504	dsk = malloc(sizeof(struct dsk));
505	dsk->drive = i | DRV_HARD;
506	dsk->type = dsk->drive & TYPE_AD;
507	dsk->unit = i;
508	dsk->slice = 0;
509	dsk->part = 0;
510	dsk->start = 0;
511	dsk->init = 0;
512	probe_drive(dsk);
513    }
514
515    /*
516     * The first discovered pool, if any, is the pool.
517     */
518    spa = spa_get_primary();
519    if (!spa) {
520	printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
521	for (;;)
522	    ;
523    }
524
525    primary_spa = spa;
526    primary_vdev = spa_get_primary_vdev(spa);
527
528    if (zfs_spa_init(spa) != 0 || zfs_mount(spa, 0, &zfsmount) != 0) {
529	printf("%s: failed to mount default pool %s\n",
530	    BOOTPROG, spa->spa_name);
531	autoboot = 0;
532    } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 ||
533        zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) {
534	off = 0;
535	zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
536    }
537
538    if (*cmd) {
539	/*
540	 * Note that parse() is destructive to cmd[] and we also want
541	 * to honor RBX_QUIET option that could be present in cmd[].
542	 */
543	memcpy(cmddup, cmd, sizeof(cmd));
544	if (parse())
545	    autoboot = 0;
546	if (!OPT_CHECK(RBX_QUIET))
547	    printf("%s: %s\n", PATH_CONFIG, cmddup);
548	/* Do not process this command twice */
549	*cmd = 0;
550    }
551
552    /*
553     * Try to exec stage 3 boot loader. If interrupted by a keypress,
554     * or in case of failure, try to load a kernel directly instead.
555     */
556
557    if (autoboot && !*kname) {
558	memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
559	if (!keyhit(3)) {
560	    load();
561	    memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
562	}
563    }
564
565    /* Present the user with the boot2 prompt. */
566
567    for (;;) {
568	if (!autoboot || !OPT_CHECK(RBX_QUIET)) {
569	    printf("\nFreeBSD/x86 boot\n");
570	    if (zfs_rlookup(spa, zfsmount.rootobj, rootname) != 0)
571		printf("Default: %s/<0x%llx>:%s\n"
572		       "boot: ",
573		       spa->spa_name, zfsmount.rootobj, kname);
574	    else if (rootname[0] != '\0')
575		printf("Default: %s/%s:%s\n"
576		       "boot: ",
577		       spa->spa_name, rootname, kname);
578	    else
579		printf("Default: %s:%s\n"
580		       "boot: ",
581		       spa->spa_name, kname);
582	}
583	if (ioctrl & IO_SERIAL)
584	    sio_flush();
585	if (!autoboot || keyhit(5))
586	    getstr(cmd, sizeof(cmd));
587	else if (!autoboot || !OPT_CHECK(RBX_QUIET))
588	    putchar('\n');
589	autoboot = 0;
590	if (parse())
591	    putchar('\a');
592	else
593	    load();
594    }
595}
596
597/* XXX - Needed for btxld to link the boot2 binary; do not remove. */
598void
599exit(int x)
600{
601}
602
603static void
604load(void)
605{
606    union {
607	struct exec ex;
608	Elf32_Ehdr eh;
609    } hdr;
610    static Elf32_Phdr ep[2];
611    static Elf32_Shdr es[2];
612    caddr_t p;
613    dnode_phys_t dn;
614    off_t off;
615    uint32_t addr, x;
616    int fmt, i, j;
617
618    if (zfs_lookup(&zfsmount, kname, &dn)) {
619	printf("\nCan't find %s\n", kname);
620	return;
621    }
622    off = 0;
623    if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
624	return;
625    if (N_GETMAGIC(hdr.ex) == ZMAGIC)
626	fmt = 0;
627    else if (IS_ELF(hdr.eh))
628	fmt = 1;
629    else {
630	printf("Invalid %s\n", "format");
631	return;
632    }
633    if (fmt == 0) {
634	addr = hdr.ex.a_entry & 0xffffff;
635	p = PTOV(addr);
636	off = PAGE_SIZE;
637	if (xfsread(&dn, &off, p, hdr.ex.a_text))
638	    return;
639	p += roundup2(hdr.ex.a_text, PAGE_SIZE);
640	if (xfsread(&dn, &off, p, hdr.ex.a_data))
641	    return;
642	p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
643	bootinfo.bi_symtab = VTOP(p);
644	memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
645	p += sizeof(hdr.ex.a_syms);
646	if (hdr.ex.a_syms) {
647	    if (xfsread(&dn, &off, p, hdr.ex.a_syms))
648		return;
649	    p += hdr.ex.a_syms;
650	    if (xfsread(&dn, &off, p, sizeof(int)))
651		return;
652	    x = *(uint32_t *)p;
653	    p += sizeof(int);
654	    x -= sizeof(int);
655	    if (xfsread(&dn, &off, p, x))
656		return;
657	    p += x;
658	}
659    } else {
660	off = hdr.eh.e_phoff;
661	for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
662	    if (xfsread(&dn, &off, ep + j, sizeof(ep[0])))
663		return;
664	    if (ep[j].p_type == PT_LOAD)
665		j++;
666	}
667	for (i = 0; i < 2; i++) {
668	    p = PTOV(ep[i].p_paddr & 0xffffff);
669	    off = ep[i].p_offset;
670	    if (xfsread(&dn, &off, p, ep[i].p_filesz))
671		return;
672	}
673	p += roundup2(ep[1].p_memsz, PAGE_SIZE);
674	bootinfo.bi_symtab = VTOP(p);
675	if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
676	    off = hdr.eh.e_shoff + sizeof(es[0]) *
677		(hdr.eh.e_shstrndx + 1);
678	    if (xfsread(&dn, &off, &es, sizeof(es)))
679		return;
680	    for (i = 0; i < 2; i++) {
681		memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
682		p += sizeof(es[i].sh_size);
683		off = es[i].sh_offset;
684		if (xfsread(&dn, &off, p, es[i].sh_size))
685		    return;
686		p += es[i].sh_size;
687	    }
688	}
689	addr = hdr.eh.e_entry & 0xffffff;
690    }
691    bootinfo.bi_esymtab = VTOP(p);
692    bootinfo.bi_kernelname = VTOP(kname);
693    zfsargs.size = sizeof(zfsargs);
694    zfsargs.pool = zfsmount.spa->spa_guid;
695    zfsargs.root = zfsmount.rootobj;
696    zfsargs.primary_pool = primary_spa->spa_guid;
697    if (primary_vdev != NULL)
698	zfsargs.primary_vdev = primary_vdev->v_guid;
699    else
700	printf("failed to detect primary vdev\n");
701    __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
702	   bootdev,
703	   KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
704	   (uint32_t) spa->spa_guid,
705	   (uint32_t) (spa->spa_guid >> 32),
706	   VTOP(&bootinfo),
707	   zfsargs);
708}
709
710static int
711zfs_mount_ds(char *dsname)
712{
713    uint64_t newroot;
714    spa_t *newspa;
715    char *q;
716
717    q = strchr(dsname, '/');
718    if (q)
719	*q++ = '\0';
720    newspa = spa_find_by_name(dsname);
721    if (newspa == NULL) {
722	printf("\nCan't find ZFS pool %s\n", dsname);
723	return -1;
724    }
725
726    if (zfs_spa_init(newspa))
727	return -1;
728
729    newroot = 0;
730    if (q) {
731	if (zfs_lookup_dataset(newspa, q, &newroot)) {
732	    printf("\nCan't find dataset %s in ZFS pool %s\n",
733		    q, newspa->spa_name);
734	    return -1;
735	}
736    }
737    if (zfs_mount(newspa, newroot, &zfsmount)) {
738	printf("\nCan't mount ZFS dataset\n");
739	return -1;
740    }
741    spa = newspa;
742    return (0);
743}
744
745static int
746parse(void)
747{
748    char *arg = cmd;
749    char *ep, *p, *q;
750    const char *cp;
751    int c, i, j;
752
753    while ((c = *arg++)) {
754	if (c == ' ' || c == '\t' || c == '\n')
755	    continue;
756	for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
757	ep = p;
758	if (*p)
759	    *p++ = 0;
760	if (c == '-') {
761	    while ((c = *arg++)) {
762		if (c == 'P') {
763		    if (*(uint8_t *)PTOV(0x496) & 0x10) {
764			cp = "yes";
765		    } else {
766			opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
767			cp = "no";
768		    }
769		    printf("Keyboard: %s\n", cp);
770		    continue;
771		} else if (c == 'S') {
772		    j = 0;
773		    while ((unsigned int)(i = *arg++ - '0') <= 9)
774			j = j * 10 + i;
775		    if (j > 0 && i == -'0') {
776			comspeed = j;
777			break;
778		    }
779		    /* Fall through to error below ('S' not in optstr[]). */
780		}
781		for (i = 0; c != optstr[i]; i++)
782		    if (i == NOPT - 1)
783			return -1;
784		opts ^= OPT_SET(flags[i]);
785	    }
786	    ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
787		     OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
788	    if (ioctrl & IO_SERIAL) {
789	        if (sio_init(115200 / comspeed) != 0)
790		    ioctrl &= ~IO_SERIAL;
791	    }
792	} if (c == '?') {
793	    dnode_phys_t dn;
794
795	    if (zfs_lookup(&zfsmount, arg, &dn) == 0) {
796		zap_list(spa, &dn);
797	    }
798	    return -1;
799	} else {
800	    arg--;
801
802	    /*
803	     * Report pool status if the comment is 'status'. Lets
804	     * hope no-one wants to load /status as a kernel.
805	     */
806	    if (!strcmp(arg, "status")) {
807		spa_all_status();
808		return -1;
809	    }
810
811	    /*
812	     * If there is "zfs:" prefix simply ignore it.
813	     */
814	    if (strncmp(arg, "zfs:", 4) == 0)
815		arg += 4;
816
817	    /*
818	     * If there is a colon, switch pools.
819	     */
820	    q = strchr(arg, ':');
821	    if (q) {
822		*q++ = '\0';
823		if (zfs_mount_ds(arg) != 0)
824		    return -1;
825		arg = q;
826	    }
827	    if ((i = ep - arg)) {
828		if ((size_t)i >= sizeof(kname))
829		    return -1;
830		memcpy(kname, arg, i + 1);
831	    }
832	}
833	arg = p;
834    }
835    return 0;
836}
837