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