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