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