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