]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/i386/zfsboot/zfsboot.c
MFC r335245-r335248, r335254, r335276, r335298, r335398, r335868, r335883
[FreeBSD/FreeBSD.git] / stand / 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 "stand.h"
20
21 #include <sys/param.h>
22 #include <sys/errno.h>
23 #include <sys/diskmbr.h>
24 #ifdef GPT
25 #include <sys/gpt.h>
26 #endif
27 #include <sys/reboot.h>
28 #include <sys/queue.h>
29
30 #include <machine/bootinfo.h>
31 #include <machine/elf.h>
32 #include <machine/pc/bios.h>
33
34 #include <stdarg.h>
35 #include <stddef.h>
36
37 #include <a.out.h>
38
39 #include <btxv86.h>
40
41 #include "lib.h"
42 #include "rbx.h"
43 #include "drv.h"
44 #include "edd.h"
45 #include "cons.h"
46 #include "bootargs.h"
47 #include "paths.h"
48
49 #include "libzfs.h"
50
51 #define ARGS                    0x900
52 #define NOPT                    14
53 #define NDEV                    3
54
55 #define BIOS_NUMDRIVES          0x475
56 #define DRV_HARD                0x80
57 #define DRV_MASK                0x7f
58
59 #define TYPE_AD                 0
60 #define TYPE_DA                 1
61 #define TYPE_MAXHARD            TYPE_DA
62 #define TYPE_FD                 2
63
64 #define DEV_GELIBOOT_BSIZE      4096
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 unsigned char dev_maj[NDEV] = {30, 4, 2};
91
92 static char cmd[512];
93 static char cmddup[512];
94 static char kname[1024];
95 static char rootname[256];
96 static int comspeed = SIOSPD;
97 static struct bootinfo bootinfo;
98 static uint32_t bootdev;
99 static struct zfs_boot_args zfsargs;
100
101 vm_offset_t     high_heap_base;
102 uint32_t        bios_basemem, bios_extmem, high_heap_size;
103
104 static struct bios_smap smap;
105
106 /*
107  * The minimum amount of memory to reserve in bios_extmem for the heap.
108  */
109 #define HEAP_MIN                (64 * 1024 * 1024)
110
111 static char *heap_next;
112 static char *heap_end;
113
114 /* Buffers that must not span a 64k boundary. */
115 #define READ_BUF_SIZE           8192
116 struct dmadat {
117         char rdbuf[READ_BUF_SIZE];      /* for reading large things */
118         char secbuf[READ_BUF_SIZE];     /* for MBR/disklabel */
119 };
120 static struct dmadat *dmadat;
121
122 void exit(int);
123 void reboot(void);
124 static void load(void);
125 static int parse_cmd(void);
126 static void bios_getmem(void);
127 int main(void);
128
129 #ifdef LOADER_GELI_SUPPORT
130 #include "geliboot.c"
131 static char gelipw[GELI_PW_MAXLEN];
132 static struct keybuf *gelibuf;
133 #endif
134
135 #include "zfsimpl.c"
136
137 /*
138  * Read from a dnode (which must be from a ZPL filesystem).
139  */
140 static int
141 zfs_read(spa_t *spa, const dnode_phys_t *dnode, off_t *offp, void *start, size_t size)
142 {
143         const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus;
144         size_t n;
145         int rc;
146
147         n = size;
148         if (*offp + n > zp->zp_size)
149                 n = zp->zp_size - *offp;
150
151         rc = dnode_read(spa, dnode, *offp, start, n);
152         if (rc)
153                 return (-1);
154         *offp += n;
155
156         return (n);
157 }
158
159 /*
160  * Current ZFS pool
161  */
162 static spa_t *spa;
163 static spa_t *primary_spa;
164 static vdev_t *primary_vdev;
165
166 /*
167  * A wrapper for dskread that doesn't have to worry about whether the
168  * buffer pointer crosses a 64k boundary.
169  */
170 static int
171 vdev_read(void *xvdev, void *priv, off_t off, void *buf, size_t bytes)
172 {
173         char *p;
174         daddr_t lba, alignlba;
175         off_t diff;
176         unsigned int nb, alignnb;
177         struct dsk *dsk = (struct dsk *) priv;
178
179         if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
180                 return -1;
181
182         p = buf;
183         lba = off / DEV_BSIZE;
184         lba += dsk->start;
185         /*
186          * Align reads to 4k else 4k sector GELIs will not decrypt.
187          * Round LBA down to nearest multiple of DEV_GELIBOOT_BSIZE bytes.
188          */
189         alignlba = rounddown2(off, DEV_GELIBOOT_BSIZE) / DEV_BSIZE;
190         /*
191          * The read must be aligned to DEV_GELIBOOT_BSIZE bytes relative to the
192          * start of the GELI partition, not the start of the actual disk.
193          */
194         alignlba += dsk->start;
195         diff = (lba - alignlba) * DEV_BSIZE;
196
197         while (bytes > 0) {
198                 nb = bytes / DEV_BSIZE;
199                 /*
200                  * Ensure that the read size plus the leading offset does not
201                  * exceed the size of the read buffer.
202                  */
203                 if (nb > (READ_BUF_SIZE - diff) / DEV_BSIZE)
204                         nb = (READ_BUF_SIZE - diff) / DEV_BSIZE;
205                 /*
206                  * Round the number of blocks to read up to the nearest multiple
207                  * of DEV_GELIBOOT_BSIZE.
208                  */
209                 alignnb = roundup2(nb * DEV_BSIZE + diff, DEV_GELIBOOT_BSIZE)
210                     / DEV_BSIZE;
211
212                 if (dsk->size > 0 && alignlba + alignnb > dsk->size + dsk->start) {
213                         printf("Shortening read at %lld from %d to %lld\n", alignlba,
214                             alignnb, (dsk->size + dsk->start) - alignlba);
215                         alignnb = (dsk->size + dsk->start) - alignlba;
216                 }
217
218                 if (drvread(dsk, dmadat->rdbuf, alignlba, alignnb))
219                         return -1;
220 #ifdef LOADER_GELI_SUPPORT
221                 /* decrypt */
222                 if (is_geli(dsk) == 0) {
223                         if (geli_read(dsk, ((alignlba - dsk->start) *
224                             DEV_BSIZE), dmadat->rdbuf, alignnb * DEV_BSIZE))
225                                 return (-1);
226                 }
227 #endif
228                 memcpy(p, dmadat->rdbuf + diff, nb * DEV_BSIZE);
229                 p += nb * DEV_BSIZE;
230                 lba += nb;
231                 alignlba += alignnb;
232                 bytes -= nb * DEV_BSIZE;
233                 /* Don't need the leading offset after the first block. */
234                 diff = 0;
235         }
236
237         return 0;
238 }
239 /* Match the signature exactly due to signature madness */
240 static int
241 vdev_read2(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
242 {
243         return vdev_read(vdev, priv, off, buf, bytes);
244 }
245
246
247 static int
248 vdev_write(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
249 {
250         char *p;
251         daddr_t lba;
252         unsigned int nb;
253         struct dsk *dsk = (struct dsk *) priv;
254
255         if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
256                 return -1;
257
258         p = buf;
259         lba = off / DEV_BSIZE;
260         lba += dsk->start;
261         while (bytes > 0) {
262                 nb = bytes / DEV_BSIZE;
263                 if (nb > READ_BUF_SIZE / DEV_BSIZE)
264                         nb = READ_BUF_SIZE / DEV_BSIZE;
265                 memcpy(dmadat->rdbuf, p, nb * DEV_BSIZE);
266                 if (drvwrite(dsk, dmadat->rdbuf, lba, nb))
267                         return -1;
268                 p += nb * DEV_BSIZE;
269                 lba += nb;
270                 bytes -= nb * DEV_BSIZE;
271         }
272
273         return 0;
274 }
275
276 static int
277 xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte)
278 {
279     if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) {
280         printf("Invalid format\n");
281         return -1;
282     }
283     return 0;
284 }
285
286 /*
287  * Read Pad2 (formerly "Boot Block Header") area of the first
288  * vdev label of the given vdev.
289  */
290 static int
291 vdev_read_pad2(vdev_t *vdev, char *buf, size_t size)
292 {
293         blkptr_t bp;
294         char *tmp = zap_scratch;
295         off_t off = offsetof(vdev_label_t, vl_pad2);
296
297         if (size > VDEV_PAD_SIZE)
298                 size = VDEV_PAD_SIZE;
299
300         BP_ZERO(&bp);
301         BP_SET_LSIZE(&bp, VDEV_PAD_SIZE);
302         BP_SET_PSIZE(&bp, VDEV_PAD_SIZE);
303         BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
304         BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
305         DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
306         if (vdev_read_phys(vdev, &bp, tmp, off, 0))
307                 return (EIO);
308         memcpy(buf, tmp, size);
309         return (0);
310 }
311
312 static int
313 vdev_clear_pad2(vdev_t *vdev)
314 {
315         char *zeroes = zap_scratch;
316         uint64_t *end;
317         off_t off = offsetof(vdev_label_t, vl_pad2);
318
319         memset(zeroes, 0, VDEV_PAD_SIZE);
320         end = (uint64_t *)(zeroes + VDEV_PAD_SIZE);
321         /* ZIO_CHECKSUM_LABEL magic and pre-calcualted checksum for all zeros */
322         end[-5] = 0x0210da7ab10c7a11;
323         end[-4] = 0x97f48f807f6e2a3f;
324         end[-3] = 0xaf909f1658aacefc;
325         end[-2] = 0xcbd1ea57ff6db48b;
326         end[-1] = 0x6ec692db0d465fab;
327         if (vdev_write(vdev, vdev->v_read_priv, off, zeroes, VDEV_PAD_SIZE))
328                 return (EIO);
329         return (0);
330 }
331
332 static void
333 bios_getmem(void)
334 {
335     uint64_t size;
336
337     /* Parse system memory map */
338     v86.ebx = 0;
339     do {
340         v86.ctl = V86_FLAGS;
341         v86.addr = 0x15;                /* int 0x15 function 0xe820*/
342         v86.eax = 0xe820;
343         v86.ecx = sizeof(struct bios_smap);
344         v86.edx = SMAP_SIG;
345         v86.es = VTOPSEG(&smap);
346         v86.edi = VTOPOFF(&smap);
347         v86int();
348         if (V86_CY(v86.efl) || (v86.eax != SMAP_SIG))
349             break;
350         /* look for a low-memory segment that's large enough */
351         if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
352             (smap.length >= (512 * 1024)))
353             bios_basemem = smap.length;
354         /* look for the first segment in 'extended' memory */
355         if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000)) {
356             bios_extmem = smap.length;
357         }
358
359         /*
360          * Look for the largest segment in 'extended' memory beyond
361          * 1MB but below 4GB.
362          */
363         if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
364             (smap.base < 0x100000000ull)) {
365             size = smap.length;
366
367             /*
368              * If this segment crosses the 4GB boundary, truncate it.
369              */
370             if (smap.base + size > 0x100000000ull)
371                 size = 0x100000000ull - smap.base;
372
373             if (size > high_heap_size) {
374                 high_heap_size = size;
375                 high_heap_base = smap.base;
376             }
377         }
378     } while (v86.ebx != 0);
379
380     /* Fall back to the old compatibility function for base memory */
381     if (bios_basemem == 0) {
382         v86.ctl = 0;
383         v86.addr = 0x12;                /* int 0x12 */
384         v86int();
385         
386         bios_basemem = (v86.eax & 0xffff) * 1024;
387     }
388
389     /* Fall back through several compatibility functions for extended memory */
390     if (bios_extmem == 0) {
391         v86.ctl = V86_FLAGS;
392         v86.addr = 0x15;                /* int 0x15 function 0xe801*/
393         v86.eax = 0xe801;
394         v86int();
395         if (!V86_CY(v86.efl)) {
396             bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
397         }
398     }
399     if (bios_extmem == 0) {
400         v86.ctl = 0;
401         v86.addr = 0x15;                /* int 0x15 function 0x88*/
402         v86.eax = 0x8800;
403         v86int();
404         bios_extmem = (v86.eax & 0xffff) * 1024;
405     }
406
407     /*
408      * If we have extended memory and did not find a suitable heap
409      * region in the SMAP, use the last 3MB of 'extended' memory as a
410      * high heap candidate.
411      */
412     if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
413         high_heap_size = HEAP_MIN;
414         high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
415     }
416 }
417
418 /*
419  * Try to detect a device supported by the legacy int13 BIOS
420  */
421 static int
422 int13probe(int drive)
423 {
424     v86.ctl = V86_FLAGS;
425     v86.addr = 0x13;
426     v86.eax = 0x800;
427     v86.edx = drive;
428     v86int();
429     
430     if (!V86_CY(v86.efl) &&                             /* carry clear */
431         ((v86.edx & 0xff) != (drive & DRV_MASK))) {     /* unit # OK */
432         if ((v86.ecx & 0x3f) == 0) {                    /* absurd sector size */
433                 return(0);                              /* skip device */
434         }
435         return (1);
436     }
437     return(0);
438 }
439
440 /*
441  * We call this when we find a ZFS vdev - ZFS consumes the dsk
442  * structure so we must make a new one.
443  */
444 static struct dsk *
445 copy_dsk(struct dsk *dsk)
446 {
447     struct dsk *newdsk;
448
449     newdsk = malloc(sizeof(struct dsk));
450     *newdsk = *dsk;
451     return (newdsk);
452 }
453
454 /*
455  * Get disk size from eax=0x800 and 0x4800. We need to probe both
456  * because 0x4800 may not be available and we would like to get more
457  * or less correct disk size - if it is possible at all.
458  * Note we do not really want to touch drv.c because that code is shared
459  * with boot2 and we can not afford to grow that code.
460  */
461 static uint64_t
462 drvsize_ext(struct dsk *dskp)
463 {
464         uint64_t size, tmp;
465         int cyl, hds, sec;
466
467         v86.ctl = V86_FLAGS;
468         v86.addr = 0x13;
469         v86.eax = 0x800;
470         v86.edx = dskp->drive;
471         v86int();
472
473         /* Don't error out if we get bad sector number, try EDD as well */
474         if (V86_CY(v86.efl) ||  /* carry set */
475             (v86.edx & 0xff) <= (unsigned)(dskp->drive & 0x7f)) /* unit # bad */
476                 return (0);
477         cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
478         /* Convert max head # -> # of heads */
479         hds = ((v86.edx & 0xff00) >> 8) + 1;
480         sec = v86.ecx & 0x3f;
481
482         size = (uint64_t)cyl * hds * sec;
483
484         /* Determine if we can use EDD with this device. */
485         v86.ctl = V86_FLAGS;
486         v86.addr = 0x13;
487         v86.eax = 0x4100;
488         v86.edx = dskp->drive;
489         v86.ebx = 0x55aa;
490         v86int();
491         if (V86_CY(v86.efl) ||  /* carry set */
492             (v86.ebx & 0xffff) != 0xaa55 || /* signature */
493             (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
494                 return (size);
495
496         tmp = drvsize(dskp);
497         if (tmp > size)
498                 size = tmp;
499
500         return (size);
501 }
502
503 /*
504  * The "layered" ioctl to read disk/partition size. Unfortunately
505  * the zfsboot case is hardest, because we do not have full software
506  * stack available, so we need to do some manual work here.
507  */
508 uint64_t
509 ldi_get_size(void *priv)
510 {
511         struct dsk *dskp = priv;
512         uint64_t size = dskp->size;
513
514         if (dskp->start == 0)
515                 size = drvsize_ext(dskp);
516
517         return (size * DEV_BSIZE);
518 }
519
520 static void
521 probe_drive(struct dsk *dsk)
522 {
523 #ifdef GPT
524     struct gpt_hdr hdr;
525     struct gpt_ent *ent;
526     unsigned part, entries_per_sec;
527     daddr_t slba;
528 #endif
529 #if defined(GPT) || defined(LOADER_GELI_SUPPORT)
530     daddr_t elba;
531 #endif
532
533     struct dos_partition *dp;
534     char *sec;
535     unsigned i;
536
537     /*
538      * If we find a vdev on the whole disk, stop here.
539      */
540     if (vdev_probe(vdev_read2, dsk, NULL) == 0)
541         return;
542
543 #ifdef LOADER_GELI_SUPPORT
544     /*
545      * Taste the disk, if it is GELI encrypted, decrypt it and check to see if
546      * it is a usable vdev then. Otherwise dig
547      * out the partition table and probe each slice/partition
548      * in turn for a vdev or GELI encrypted vdev.
549      */
550     elba = drvsize_ext(dsk);
551     if (elba > 0) {
552         elba--;
553     }
554     if (geli_taste(vdev_read, dsk, elba) == 0) {
555         if (geli_havekey(dsk) == 0 || geli_passphrase(gelipw, dsk->unit,
556           ':', 0, dsk) == 0) {
557             if (vdev_probe(vdev_read2, dsk, NULL) == 0) {
558                 return;
559             }
560         }
561     }
562 #endif /* LOADER_GELI_SUPPORT */
563
564     sec = dmadat->secbuf;
565     dsk->start = 0;
566
567 #ifdef GPT
568     /*
569      * First check for GPT.
570      */
571     if (drvread(dsk, sec, 1, 1)) {
572         return;
573     }
574     memcpy(&hdr, sec, sizeof(hdr));
575     if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
576         hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
577         hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
578         goto trymbr;
579     }
580
581     /*
582      * Probe all GPT partitions for the presence of ZFS pools. We
583      * return the spa_t for the first we find (if requested). This
584      * will have the effect of booting from the first pool on the
585      * disk.
586      *
587      * If no vdev is found, GELI decrypting the device and try again
588      */
589     entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
590     slba = hdr.hdr_lba_table;
591     elba = slba + hdr.hdr_entries / entries_per_sec;
592     while (slba < elba) {
593         dsk->start = 0;
594         if (drvread(dsk, sec, slba, 1))
595             return;
596         for (part = 0; part < entries_per_sec; part++) {
597             ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
598             if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
599                      sizeof(uuid_t)) == 0) {
600                 dsk->start = ent->ent_lba_start;
601                 dsk->size = ent->ent_lba_end - ent->ent_lba_start + 1;
602                 dsk->slice = part + 1;
603                 dsk->part = 255;
604                 if (vdev_probe(vdev_read2, dsk, NULL) == 0) {
605                     /*
606                      * This slice had a vdev. We need a new dsk
607                      * structure now since the vdev now owns this one.
608                      */
609                     dsk = copy_dsk(dsk);
610                 }
611 #ifdef LOADER_GELI_SUPPORT
612                 else if (geli_taste(vdev_read, dsk, ent->ent_lba_end -
613                          ent->ent_lba_start) == 0) {
614                     if (geli_havekey(dsk) == 0 || geli_passphrase(gelipw,
615                       dsk->unit, 'p', dsk->slice, dsk) == 0) {
616                         /*
617                          * This slice has GELI, check it for ZFS.
618                          */
619                         if (vdev_probe(vdev_read2, dsk, NULL) == 0) {
620                             /*
621                              * This slice had a vdev. We need a new dsk
622                              * structure now since the vdev now owns this one.
623                              */
624                             dsk = copy_dsk(dsk);
625                         }
626                         break;
627                     }
628                 }
629 #endif /* LOADER_GELI_SUPPORT */
630             }
631         }
632         slba++;
633     }
634     return;
635 trymbr:
636 #endif /* GPT */
637
638     if (drvread(dsk, sec, DOSBBSECTOR, 1))
639         return;
640     dp = (void *)(sec + DOSPARTOFF);
641
642     for (i = 0; i < NDOSPART; i++) {
643         if (!dp[i].dp_typ)
644             continue;
645         dsk->start = dp[i].dp_start;
646         dsk->size = dp[i].dp_size;
647         dsk->slice = i + 1;
648         if (vdev_probe(vdev_read2, dsk, NULL) == 0) {
649             dsk = copy_dsk(dsk);
650         }
651 #ifdef LOADER_GELI_SUPPORT
652         else if (geli_taste(vdev_read, dsk, dp[i].dp_size -
653                  dp[i].dp_start) == 0) {
654             if (geli_havekey(dsk) == 0 || geli_passphrase(gelipw, dsk->unit,
655               's', i, dsk) == 0) {
656                 /*
657                  * This slice has GELI, check it for ZFS.
658                  */
659                 if (vdev_probe(vdev_read2, dsk, NULL) == 0) {
660                     /*
661                      * This slice had a vdev. We need a new dsk
662                      * structure now since the vdev now owns this one.
663                      */
664                     dsk = copy_dsk(dsk);
665                 }
666                 break;
667             }
668         }
669 #endif /* LOADER_GELI_SUPPORT */
670     }
671 }
672
673 int
674 main(void)
675 {
676     dnode_phys_t dn;
677     off_t off;
678     struct dsk *dsk;
679     int autoboot, i;
680     int nextboot;
681     int rc;
682
683     dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
684
685     bios_getmem();
686
687     if (high_heap_size > 0) {
688         heap_end = PTOV(high_heap_base + high_heap_size);
689         heap_next = PTOV(high_heap_base);
690     } else {
691         heap_next = (char *)dmadat + sizeof(*dmadat);
692         heap_end = (char *)PTOV(bios_basemem);
693     }
694     setheap(heap_next, heap_end);
695
696     dsk = malloc(sizeof(struct dsk));
697     dsk->drive = *(uint8_t *)PTOV(ARGS);
698     dsk->type = dsk->drive & DRV_HARD ? TYPE_AD : TYPE_FD;
699     dsk->unit = dsk->drive & DRV_MASK;
700     dsk->slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
701     dsk->part = 0;
702     dsk->start = 0;
703     dsk->size = drvsize_ext(dsk);
704
705     bootinfo.bi_version = BOOTINFO_VERSION;
706     bootinfo.bi_size = sizeof(bootinfo);
707     bootinfo.bi_basemem = bios_basemem / 1024;
708     bootinfo.bi_extmem = bios_extmem / 1024;
709     bootinfo.bi_memsizes_valid++;
710     bootinfo.bi_bios_dev = dsk->drive;
711
712     bootdev = MAKEBOOTDEV(dev_maj[dsk->type],
713                           dsk->slice, dsk->unit, dsk->part);
714
715     /* Process configuration file */
716
717     autoboot = 1;
718
719 #ifdef LOADER_GELI_SUPPORT
720     geli_init();
721 #endif
722     zfs_init();
723
724     /*
725      * Probe the boot drive first - we will try to boot from whatever
726      * pool we find on that drive.
727      */
728     probe_drive(dsk);
729
730     /*
731      * Probe the rest of the drives that the bios knows about. This
732      * will find any other available pools and it may fill in missing
733      * vdevs for the boot pool.
734      */
735 #ifndef VIRTUALBOX
736     for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++)
737 #else
738     for (i = 0; i < MAXBDDEV; i++)
739 #endif
740     {
741         if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
742             continue;
743
744         if (!int13probe(i | DRV_HARD))
745             break;
746
747         dsk = malloc(sizeof(struct dsk));
748         dsk->drive = i | DRV_HARD;
749         dsk->type = dsk->drive & TYPE_AD;
750         dsk->unit = i;
751         dsk->slice = 0;
752         dsk->part = 0;
753         dsk->start = 0;
754         dsk->size = drvsize_ext(dsk);
755         probe_drive(dsk);
756     }
757
758     /*
759      * The first discovered pool, if any, is the pool.
760      */
761     spa = spa_get_primary();
762     if (!spa) {
763         printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
764         for (;;)
765             ;
766     }
767
768     primary_spa = spa;
769     primary_vdev = spa_get_primary_vdev(spa);
770
771     nextboot = 0;
772     rc  = vdev_read_pad2(primary_vdev, cmd, sizeof(cmd));
773     if (vdev_clear_pad2(primary_vdev))
774         printf("failed to clear pad2 area of primary vdev\n");
775     if (rc == 0) {
776         if (*cmd) {
777             /*
778              * We could find an old-style ZFS Boot Block header here.
779              * Simply ignore it.
780              */
781             if (*(uint64_t *)cmd != 0x2f5b007b10c) {
782                 /*
783                  * Note that parse() is destructive to cmd[] and we also want
784                  * to honor RBX_QUIET option that could be present in cmd[].
785                  */
786                 nextboot = 1;
787                 memcpy(cmddup, cmd, sizeof(cmd));
788                 if (parse_cmd()) {
789                     printf("failed to parse pad2 area of primary vdev\n");
790                     reboot();
791                 }
792                 if (!OPT_CHECK(RBX_QUIET))
793                     printf("zfs nextboot: %s\n", cmddup);
794             }
795             /* Do not process this command twice */
796             *cmd = 0;
797         }
798     } else
799         printf("failed to read pad2 area of primary vdev\n");
800
801     /* Mount ZFS only if it's not already mounted via nextboot parsing. */
802     if (zfsmount.spa == NULL &&
803         (zfs_spa_init(spa) != 0 || zfs_mount(spa, 0, &zfsmount) != 0)) {
804         printf("%s: failed to mount default pool %s\n",
805             BOOTPROG, spa->spa_name);
806         autoboot = 0;
807     } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 ||
808         zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) {
809         off = 0;
810         zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
811     }
812
813     if (*cmd) {
814         /*
815          * Note that parse_cmd() is destructive to cmd[] and we also want
816          * to honor RBX_QUIET option that could be present in cmd[].
817          */
818         memcpy(cmddup, cmd, sizeof(cmd));
819         if (parse_cmd())
820             autoboot = 0;
821         if (!OPT_CHECK(RBX_QUIET))
822             printf("%s: %s\n", PATH_CONFIG, cmddup);
823         /* Do not process this command twice */
824         *cmd = 0;
825     }
826
827     /* Do not risk waiting at the prompt forever. */
828     if (nextboot && !autoboot)
829         reboot();
830
831     /*
832      * Try to exec /boot/loader. If interrupted by a keypress,
833      * or in case of failure, try to load a kernel directly instead.
834      */
835
836     if (autoboot && !*kname) {
837         memcpy(kname, PATH_LOADER_ZFS, sizeof(PATH_LOADER_ZFS));
838         if (!keyhit(3)) {
839             load();
840             memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
841         }
842     }
843
844     /* Present the user with the boot2 prompt. */
845
846     for (;;) {
847         if (!autoboot || !OPT_CHECK(RBX_QUIET)) {
848             printf("\nFreeBSD/x86 boot\n");
849             if (zfs_rlookup(spa, zfsmount.rootobj, rootname) != 0)
850                 printf("Default: %s/<0x%llx>:%s\n"
851                        "boot: ",
852                        spa->spa_name, zfsmount.rootobj, kname);
853             else if (rootname[0] != '\0')
854                 printf("Default: %s/%s:%s\n"
855                        "boot: ",
856                        spa->spa_name, rootname, kname);
857             else
858                 printf("Default: %s:%s\n"
859                        "boot: ",
860                        spa->spa_name, kname);
861         }
862         if (ioctrl & IO_SERIAL)
863             sio_flush();
864         if (!autoboot || keyhit(5))
865             getstr(cmd, sizeof(cmd));
866         else if (!autoboot || !OPT_CHECK(RBX_QUIET))
867             putchar('\n');
868         autoboot = 0;
869         if (parse_cmd())
870             putchar('\a');
871         else
872             load();
873     }
874 }
875
876 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
877 void
878 exit(int x)
879 {
880     __exit(x);
881 }
882
883 void
884 reboot(void)
885 {
886     __exit(0);
887 }
888
889 static void
890 load(void)
891 {
892     union {
893         struct exec ex;
894         Elf32_Ehdr eh;
895     } hdr;
896     static Elf32_Phdr ep[2];
897     static Elf32_Shdr es[2];
898     caddr_t p;
899     dnode_phys_t dn;
900     off_t off;
901     uint32_t addr, x;
902     int fmt, i, j;
903
904     if (zfs_lookup(&zfsmount, kname, &dn)) {
905         printf("\nCan't find %s\n", kname);
906         return;
907     }
908     off = 0;
909     if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
910         return;
911     if (N_GETMAGIC(hdr.ex) == ZMAGIC)
912         fmt = 0;
913     else if (IS_ELF(hdr.eh))
914         fmt = 1;
915     else {
916         printf("Invalid %s\n", "format");
917         return;
918     }
919     if (fmt == 0) {
920         addr = hdr.ex.a_entry & 0xffffff;
921         p = PTOV(addr);
922         off = PAGE_SIZE;
923         if (xfsread(&dn, &off, p, hdr.ex.a_text))
924             return;
925         p += roundup2(hdr.ex.a_text, PAGE_SIZE);
926         if (xfsread(&dn, &off, p, hdr.ex.a_data))
927             return;
928         p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
929         bootinfo.bi_symtab = VTOP(p);
930         memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
931         p += sizeof(hdr.ex.a_syms);
932         if (hdr.ex.a_syms) {
933             if (xfsread(&dn, &off, p, hdr.ex.a_syms))
934                 return;
935             p += hdr.ex.a_syms;
936             if (xfsread(&dn, &off, p, sizeof(int)))
937                 return;
938             x = *(uint32_t *)p;
939             p += sizeof(int);
940             x -= sizeof(int);
941             if (xfsread(&dn, &off, p, x))
942                 return;
943             p += x;
944         }
945     } else {
946         off = hdr.eh.e_phoff;
947         for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
948             if (xfsread(&dn, &off, ep + j, sizeof(ep[0])))
949                 return;
950             if (ep[j].p_type == PT_LOAD)
951                 j++;
952         }
953         for (i = 0; i < 2; i++) {
954             p = PTOV(ep[i].p_paddr & 0xffffff);
955             off = ep[i].p_offset;
956             if (xfsread(&dn, &off, p, ep[i].p_filesz))
957                 return;
958         }
959         p += roundup2(ep[1].p_memsz, PAGE_SIZE);
960         bootinfo.bi_symtab = VTOP(p);
961         if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
962             off = hdr.eh.e_shoff + sizeof(es[0]) *
963                 (hdr.eh.e_shstrndx + 1);
964             if (xfsread(&dn, &off, &es, sizeof(es)))
965                 return;
966             for (i = 0; i < 2; i++) {
967                 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
968                 p += sizeof(es[i].sh_size);
969                 off = es[i].sh_offset;
970                 if (xfsread(&dn, &off, p, es[i].sh_size))
971                     return;
972                 p += es[i].sh_size;
973             }
974         }
975         addr = hdr.eh.e_entry & 0xffffff;
976     }
977     bootinfo.bi_esymtab = VTOP(p);
978     bootinfo.bi_kernelname = VTOP(kname);
979     zfsargs.size = sizeof(zfsargs);
980     zfsargs.pool = zfsmount.spa->spa_guid;
981     zfsargs.root = zfsmount.rootobj;
982     zfsargs.primary_pool = primary_spa->spa_guid;
983 #ifdef LOADER_GELI_SUPPORT
984     explicit_bzero(gelipw, sizeof(gelipw));
985     gelibuf = malloc(sizeof(struct keybuf) + (GELI_MAX_KEYS * sizeof(struct keybuf_ent)));
986     geli_fill_keybuf(gelibuf);
987     zfsargs.notapw = '\0';
988     zfsargs.keybuf_sentinel = KEYBUF_SENTINEL;
989     zfsargs.keybuf = gelibuf;
990 #else
991     zfsargs.gelipw[0] = '\0';
992 #endif
993     if (primary_vdev != NULL)
994         zfsargs.primary_vdev = primary_vdev->v_guid;
995     else
996         printf("failed to detect primary vdev\n");
997     __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
998            bootdev,
999            KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
1000            (uint32_t) spa->spa_guid,
1001            (uint32_t) (spa->spa_guid >> 32),
1002            VTOP(&bootinfo),
1003            zfsargs);
1004 }
1005
1006 static int
1007 zfs_mount_ds(char *dsname)
1008 {
1009     uint64_t newroot;
1010     spa_t *newspa;
1011     char *q;
1012
1013     q = strchr(dsname, '/');
1014     if (q)
1015         *q++ = '\0';
1016     newspa = spa_find_by_name(dsname);
1017     if (newspa == NULL) {
1018         printf("\nCan't find ZFS pool %s\n", dsname);
1019         return -1;
1020     }
1021
1022     if (zfs_spa_init(newspa))
1023         return -1;
1024
1025     newroot = 0;
1026     if (q) {
1027         if (zfs_lookup_dataset(newspa, q, &newroot)) {
1028             printf("\nCan't find dataset %s in ZFS pool %s\n",
1029                     q, newspa->spa_name);
1030             return -1;
1031         }
1032     }
1033     if (zfs_mount(newspa, newroot, &zfsmount)) {
1034         printf("\nCan't mount ZFS dataset\n");
1035         return -1;
1036     }
1037     spa = newspa;
1038     return (0);
1039 }
1040
1041 static int
1042 parse_cmd(void)
1043 {
1044     char *arg = cmd;
1045     char *ep, *p, *q;
1046     const char *cp;
1047     int c, i, j;
1048
1049     while ((c = *arg++)) {
1050         if (c == ' ' || c == '\t' || c == '\n')
1051             continue;
1052         for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
1053         ep = p;
1054         if (*p)
1055             *p++ = 0;
1056         if (c == '-') {
1057             while ((c = *arg++)) {
1058                 if (c == 'P') {
1059                     if (*(uint8_t *)PTOV(0x496) & 0x10) {
1060                         cp = "yes";
1061                     } else {
1062                         opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
1063                         cp = "no";
1064                     }
1065                     printf("Keyboard: %s\n", cp);
1066                     continue;
1067                 } else if (c == 'S') {
1068                     j = 0;
1069                     while ((unsigned int)(i = *arg++ - '0') <= 9)
1070                         j = j * 10 + i;
1071                     if (j > 0 && i == -'0') {
1072                         comspeed = j;
1073                         break;
1074                     }
1075                     /* Fall through to error below ('S' not in optstr[]). */
1076                 }
1077                 for (i = 0; c != optstr[i]; i++)
1078                     if (i == NOPT - 1)
1079                         return -1;
1080                 opts ^= OPT_SET(flags[i]);
1081             }
1082             ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
1083                      OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
1084             if (ioctrl & IO_SERIAL) {
1085                 if (sio_init(115200 / comspeed) != 0)
1086                     ioctrl &= ~IO_SERIAL;
1087             }
1088         } if (c == '?') {
1089             dnode_phys_t dn;
1090
1091             if (zfs_lookup(&zfsmount, arg, &dn) == 0) {
1092                 zap_list(spa, &dn);
1093             }
1094             return -1;
1095         } else {
1096             arg--;
1097
1098             /*
1099              * Report pool status if the comment is 'status'. Lets
1100              * hope no-one wants to load /status as a kernel.
1101              */
1102             if (!strcmp(arg, "status")) {
1103                 spa_all_status();
1104                 return -1;
1105             }
1106
1107             /*
1108              * If there is "zfs:" prefix simply ignore it.
1109              */
1110             if (strncmp(arg, "zfs:", 4) == 0)
1111                 arg += 4;
1112
1113             /*
1114              * If there is a colon, switch pools.
1115              */
1116             q = strchr(arg, ':');
1117             if (q) {
1118                 *q++ = '\0';
1119                 if (zfs_mount_ds(arg) != 0)
1120                     return -1;
1121                 arg = q;
1122             }
1123             if ((i = ep - arg)) {
1124                 if ((size_t)i >= sizeof(kname))
1125                     return -1;
1126                 memcpy(kname, arg, i + 1);
1127             }
1128         }
1129         arg = p;
1130     }
1131     return 0;
1132 }