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