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