xref: /freebsd/stand/i386/zfsboot/zfsboot.c (revision e17f5b1d)
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 #include "bootstrap.h"
39 #include "libi386.h"
40 #include <btxv86.h>
41 
42 #include "lib.h"
43 #include "rbx.h"
44 #include "cons.h"
45 #include "bootargs.h"
46 #include "disk.h"
47 #include "part.h"
48 #include "paths.h"
49 
50 #include "libzfs.h"
51 
52 #define	ARGS			0x900
53 #define	NOPT			14
54 #define	NDEV			3
55 
56 #define	BIOS_NUMDRIVES		0x475
57 #define	DRV_HARD		0x80
58 #define	DRV_MASK		0x7f
59 
60 #define	TYPE_AD			0
61 #define	TYPE_DA			1
62 #define	TYPE_MAXHARD		TYPE_DA
63 #define	TYPE_FD			2
64 
65 extern uint32_t _end;
66 
67 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
68 static const unsigned char flags[NOPT] = {
69     RBX_DUAL,
70     RBX_SERIAL,
71     RBX_ASKNAME,
72     RBX_CDROM,
73     RBX_CONFIG,
74     RBX_KDB,
75     RBX_GDB,
76     RBX_MUTE,
77     RBX_NOINTR,
78     RBX_PAUSE,
79     RBX_QUIET,
80     RBX_DFLTROOT,
81     RBX_SINGLE,
82     RBX_VERBOSE
83 };
84 uint32_t opts;
85 
86 /*
87  * Paths to try loading before falling back to the boot2 prompt.
88  *
89  * /boot/zfsloader must be tried before /boot/loader in order to remain
90  * backward compatible with ZFS boot environments where /boot/loader exists
91  * but does not have ZFS support, which was the case before FreeBSD 12.
92  *
93  * If no loader is found, try to load a kernel directly instead.
94  */
95 static const struct string {
96 	const char *p;
97 	size_t len;
98 } loadpath[] = {
99 	{ PATH_LOADER_ZFS, sizeof(PATH_LOADER_ZFS) },
100 	{ PATH_LOADER, sizeof(PATH_LOADER) },
101 	{ PATH_KERNEL, sizeof(PATH_KERNEL) },
102 };
103 
104 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
105 
106 static struct i386_devdesc *bdev;
107 static char cmd[512];
108 static char cmddup[512];
109 static char kname[1024];
110 static int comspeed = SIOSPD;
111 static struct bootinfo bootinfo;
112 static uint32_t bootdev;
113 static struct zfs_boot_args zfsargs;
114 #ifdef LOADER_GELI_SUPPORT
115 static struct geli_boot_args geliargs;
116 #endif
117 
118 extern vm_offset_t high_heap_base;
119 extern uint32_t	bios_basemem, bios_extmem, high_heap_size;
120 
121 static char *heap_top;
122 static char *heap_bottom;
123 
124 void exit(int);
125 static void i386_zfs_probe(void);
126 static void load(void);
127 static int parse_cmd(void);
128 
129 #ifdef LOADER_GELI_SUPPORT
130 #include "geliboot.h"
131 static char gelipw[GELI_PW_MAXLEN];
132 #endif
133 
134 struct arch_switch archsw;	/* MI/MD interface boundary */
135 static char boot_devname[2 * ZFS_MAXNAMELEN + 8]; /* disk or pool:dataset */
136 
137 struct devsw *devsw[] = {
138 	&bioshd,
139 #if defined(LOADER_ZFS_SUPPORT)
140 	&zfs_dev,
141 #endif
142 	NULL
143 };
144 
145 struct fs_ops *file_system[] = {
146 #if defined(LOADER_ZFS_SUPPORT)
147 	&zfs_fsops,
148 #endif
149 #if defined(LOADER_UFS_SUPPORT)
150 	&ufs_fsops,
151 #endif
152 	NULL
153 };
154 
155 caddr_t
156 ptov(uintptr_t x)
157 {
158 	return (PTOV(x));
159 }
160 
161 int
162 main(void)
163 {
164 	unsigned i;
165 	int auto_boot, fd, nextboot = 0;
166 	struct disk_devdesc devdesc;
167 
168 	bios_getmem();
169 
170 	if (high_heap_size > 0) {
171 		heap_top = PTOV(high_heap_base + high_heap_size);
172 		heap_bottom = PTOV(high_heap_base);
173 	} else {
174 		heap_bottom = (char *)
175 		    (roundup2(__base + (int32_t)&_end, 0x10000) - __base);
176 		heap_top = (char *)PTOV(bios_basemem);
177 	}
178 	setheap(heap_bottom, heap_top);
179 
180 	/*
181 	 * Initialise the block cache. Set the upper limit.
182 	 */
183 	bcache_init(32768, 512);
184 
185 	archsw.arch_autoload = NULL;
186 	archsw.arch_getdev = i386_getdev;
187 	archsw.arch_copyin = NULL;
188 	archsw.arch_copyout = NULL;
189 	archsw.arch_readin = NULL;
190 	archsw.arch_isainb = NULL;
191 	archsw.arch_isaoutb = NULL;
192 	archsw.arch_zfs_probe = i386_zfs_probe;
193 
194 	bootinfo.bi_version = BOOTINFO_VERSION;
195 	bootinfo.bi_size = sizeof(bootinfo);
196 	bootinfo.bi_basemem = bios_basemem / 1024;
197 	bootinfo.bi_extmem = bios_extmem / 1024;
198 	bootinfo.bi_memsizes_valid++;
199 	bootinfo.bi_bios_dev = *(uint8_t *)PTOV(ARGS);
200 
201 	/* Set up fall back device name. */
202 	snprintf(boot_devname, sizeof (boot_devname), "disk%d:",
203 	    bd_bios2unit(bootinfo.bi_bios_dev));
204 
205 	for (i = 0; devsw[i] != NULL; i++)
206 		if (devsw[i]->dv_init != NULL)
207 			(devsw[i]->dv_init)();
208 
209 	disk_parsedev(&devdesc, boot_devname + 4, NULL);
210 
211 	bootdev = MAKEBOOTDEV(dev_maj[DEVT_DISK], devdesc.d_slice + 1,
212 	    devdesc.dd.d_unit,
213 	    devdesc.d_partition >= 0 ? devdesc.d_partition : 0xff);
214 
215 	/*
216 	 * zfs_fmtdev() can be called only after dv_init
217 	 */
218 	if (bdev != NULL && bdev->dd.d_dev->dv_type == DEVT_ZFS) {
219 		/* set up proper device name string for ZFS */
220 		strncpy(boot_devname, zfs_fmtdev(bdev), sizeof (boot_devname));
221 		if (zfs_nextboot(bdev, cmd, sizeof(cmd)) == 0) {
222 			nextboot = 1;
223 			memcpy(cmddup, cmd, sizeof(cmd));
224 			if (parse_cmd()) {
225 				if (!OPT_CHECK(RBX_QUIET))
226 					printf("failed to parse pad2 area\n");
227 				exit(0);
228 			}
229 			if (!OPT_CHECK(RBX_QUIET))
230 				printf("zfs nextboot: %s\n", cmddup);
231 			/* Do not process this command twice */
232 			*cmd = 0;
233 		}
234 	}
235 
236 	/* now make sure we have bdev on all cases */
237 	free(bdev);
238 	i386_getdev((void **)&bdev, boot_devname, NULL);
239 
240 	env_setenv("currdev", EV_VOLATILE, boot_devname, i386_setcurrdev,
241 	    env_nounset);
242 
243 	/* Process configuration file */
244 	auto_boot = 1;
245 
246 	fd = open(PATH_CONFIG, O_RDONLY);
247 	if (fd == -1)
248 		fd = open(PATH_DOTCONFIG, O_RDONLY);
249 
250 	if (fd != -1) {
251 		read(fd, cmd, sizeof (cmd));
252 		close(fd);
253 	}
254 
255 	if (*cmd) {
256 		/*
257 		 * Note that parse_cmd() is destructive to cmd[] and we also
258 		 * want to honor RBX_QUIET option that could be present in
259 		 * cmd[].
260 		 */
261 		memcpy(cmddup, cmd, sizeof(cmd));
262 		if (parse_cmd())
263 			auto_boot = 0;
264 		if (!OPT_CHECK(RBX_QUIET))
265 			printf("%s: %s\n", PATH_CONFIG, cmddup);
266 		/* Do not process this command twice */
267 		*cmd = 0;
268 	}
269 
270 	/* Do not risk waiting at the prompt forever. */
271 	if (nextboot && !auto_boot)
272 		exit(0);
273 
274 	if (auto_boot && !*kname) {
275 		/*
276 		 * Iterate through the list of loader and kernel paths,
277 		 * trying to load. If interrupted by a keypress, or in case of
278 		 * failure, drop the user to the boot2 prompt.
279 		 */
280 		for (i = 0; i < nitems(loadpath); i++) {
281 			memcpy(kname, loadpath[i].p, loadpath[i].len);
282 			if (keyhit(3))
283 				break;
284 			load();
285 		}
286 	}
287 
288 	/* Present the user with the boot2 prompt. */
289 
290 	for (;;) {
291 		if (!auto_boot || !OPT_CHECK(RBX_QUIET)) {
292 			printf("\nFreeBSD/x86 boot\n");
293 			printf("Default: %s%s\nboot: ", boot_devname, kname);
294 		}
295 		if (ioctrl & IO_SERIAL)
296 			sio_flush();
297 		if (!auto_boot || keyhit(5))
298 			getstr(cmd, sizeof(cmd));
299 		else if (!auto_boot || !OPT_CHECK(RBX_QUIET))
300 			putchar('\n');
301 		auto_boot = 0;
302 		if (parse_cmd())
303 			putchar('\a');
304 		else
305 			load();
306 	}
307 }
308 
309 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
310 void
311 exit(int x)
312 {
313 	__exit(x);
314 }
315 
316 static void
317 load(void)
318 {
319 	union {
320 		struct exec ex;
321 		Elf32_Ehdr eh;
322 	} hdr;
323 	static Elf32_Phdr ep[2];
324 	static Elf32_Shdr es[2];
325 	caddr_t p;
326 	uint32_t addr, x;
327 	int fd, fmt, i, j;
328 	ssize_t size;
329 
330 	if ((fd = open(kname, O_RDONLY)) == -1) {
331 		printf("\nCan't find %s\n", kname);
332 		return;
333 	}
334 
335 	size = sizeof(hdr);
336 	if (read(fd, &hdr, sizeof (hdr)) != size) {
337 		close(fd);
338 		return;
339 	}
340 	if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
341 		fmt = 0;
342 	} else if (IS_ELF(hdr.eh)) {
343 		fmt = 1;
344 	} else {
345 		printf("Invalid %s\n", "format");
346 		close(fd);
347 		return;
348 	}
349 	if (fmt == 0) {
350 		addr = hdr.ex.a_entry & 0xffffff;
351 		p = PTOV(addr);
352 		lseek(fd, PAGE_SIZE, SEEK_SET);
353 		size = hdr.ex.a_text;
354 		if (read(fd, p, hdr.ex.a_text) != size) {
355 			close(fd);
356 			return;
357 		}
358 		p += roundup2(hdr.ex.a_text, PAGE_SIZE);
359 		size = hdr.ex.a_data;
360 		if (read(fd, p, hdr.ex.a_data) != size) {
361 			close(fd);
362 			return;
363 		}
364 		p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
365 		bootinfo.bi_symtab = VTOP(p);
366 		memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
367 		p += sizeof(hdr.ex.a_syms);
368 		if (hdr.ex.a_syms) {
369 			size = hdr.ex.a_syms;
370 			if (read(fd, p, hdr.ex.a_syms) != size) {
371 				close(fd);
372 				return;
373 			}
374 			p += hdr.ex.a_syms;
375 			size = sizeof (int);
376 			if (read(fd, p, sizeof (int)) != size) {
377 				close(fd);
378 				return;
379 			}
380 			x = *(uint32_t *)p;
381 			p += sizeof(int);
382 			x -= sizeof(int);
383 			size = x;
384 			if (read(fd, p, x) != size) {
385 				close(fd);
386 				return;
387 			}
388 			p += x;
389 		}
390 	} else {
391 		lseek(fd, hdr.eh.e_phoff, SEEK_SET);
392 		for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
393 			size = sizeof (ep[0]);
394 			if (read(fd, ep + j, sizeof (ep[0])) != size) {
395 				close(fd);
396 				return;
397 			}
398 			if (ep[j].p_type == PT_LOAD)
399 				j++;
400 		}
401 		for (i = 0; i < 2; i++) {
402 			p = PTOV(ep[i].p_paddr & 0xffffff);
403 			lseek(fd, ep[i].p_offset, SEEK_SET);
404 			size = ep[i].p_filesz;
405 			if (read(fd, p, ep[i].p_filesz) != size) {
406 				close(fd);
407 				return;
408 			}
409 		}
410 		p += roundup2(ep[1].p_memsz, PAGE_SIZE);
411 		bootinfo.bi_symtab = VTOP(p);
412 		if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
413 			lseek(fd, hdr.eh.e_shoff +
414 			    sizeof (es[0]) * (hdr.eh.e_shstrndx + 1),
415 			    SEEK_SET);
416 			size = sizeof(es);
417 			if (read(fd, &es, sizeof (es)) != size) {
418 				close(fd);
419 				return;
420 			}
421 			for (i = 0; i < 2; i++) {
422 				memcpy(p, &es[i].sh_size,
423 				    sizeof(es[i].sh_size));
424 				p += sizeof(es[i].sh_size);
425 				lseek(fd, es[i].sh_offset, SEEK_SET);
426 				size = es[i].sh_size;
427 				if (read(fd, p, es[i].sh_size) != size) {
428 					close(fd);
429 					return;
430 				}
431 				p += es[i].sh_size;
432 			}
433 		}
434 		addr = hdr.eh.e_entry & 0xffffff;
435 	}
436 	close(fd);
437 
438 	bootinfo.bi_esymtab = VTOP(p);
439 	bootinfo.bi_kernelname = VTOP(kname);
440 #ifdef LOADER_GELI_SUPPORT
441 	explicit_bzero(gelipw, sizeof(gelipw));
442 #endif
443 
444 	if (bdev->dd.d_dev->dv_type == DEVT_ZFS) {
445 		zfsargs.size = sizeof(zfsargs);
446 		zfsargs.pool = bdev->d_kind.zfs.pool_guid;
447 		zfsargs.root = bdev->d_kind.zfs.root_guid;
448 #ifdef LOADER_GELI_SUPPORT
449 		export_geli_boot_data(&zfsargs.gelidata);
450 #endif
451 		/*
452 		 * Note that the zfsargs struct is passed by value, not by
453 		 * pointer. Code in btxldr.S copies the values from the entry
454 		 * stack to a fixed location within loader(8) at startup due
455 		 * to the presence of KARGS_FLAGS_EXTARG.
456 		 */
457 		__exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
458 		    bootdev,
459 		    KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
460 		    (uint32_t)bdev->d_kind.zfs.pool_guid,
461 		    (uint32_t)(bdev->d_kind.zfs.pool_guid >> 32),
462 		    VTOP(&bootinfo),
463 		    zfsargs);
464 	} else {
465 #ifdef LOADER_GELI_SUPPORT
466 		geliargs.size = sizeof(geliargs);
467 		export_geli_boot_data(&geliargs.gelidata);
468 #endif
469 
470 		/*
471 		 * Note that the geliargs struct is passed by value, not by
472 		 * pointer. Code in btxldr.S copies the values from the entry
473 		 * stack to a fixed location within loader(8) at startup due
474 		 * to the presence of the KARGS_FLAGS_EXTARG flag.
475 		 */
476 		__exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
477 		    bootdev,
478 #ifdef LOADER_GELI_SUPPORT
479 		    KARGS_FLAGS_GELI | KARGS_FLAGS_EXTARG, 0, 0,
480 		    VTOP(&bootinfo), geliargs
481 #else
482 		    0, 0, 0, VTOP(&bootinfo)
483 #endif
484 		    );
485 	}
486 }
487 
488 static int
489 mount_root(char *arg)
490 {
491 	char *root;
492 	struct i386_devdesc *ddesc;
493 	uint8_t part;
494 
495 	if (asprintf(&root, "%s:", arg) < 0)
496 		return (1);
497 
498 	if (i386_getdev((void **)&ddesc, root, NULL)) {
499 		free(root);
500 		return (1);
501 	}
502 
503 	/* we should have new device descriptor, free old and replace it. */
504 	free(bdev);
505 	bdev = ddesc;
506 	if (bdev->dd.d_dev->dv_type == DEVT_DISK) {
507 		if (bdev->d_kind.biosdisk.partition == -1)
508 			part = 0xff;
509 		else
510 			part = bdev->d_kind.biosdisk.partition;
511 		bootdev = MAKEBOOTDEV(dev_maj[bdev->dd.d_dev->dv_type],
512 		    bdev->d_kind.biosdisk.slice + 1,
513 		    bdev->dd.d_unit, part);
514 		bootinfo.bi_bios_dev = bd_unit2bios(bdev);
515 	}
516 	strncpy(boot_devname, root, sizeof (boot_devname));
517 	setenv("currdev", root, 1);
518 	free(root);
519 	return (0);
520 }
521 
522 static void
523 fs_list(char *arg)
524 {
525 	int fd;
526 	struct dirent *d;
527 	char line[80];
528 
529 	fd = open(arg, O_RDONLY);
530 	if (fd < 0)
531 		return;
532 	pager_open();
533 	while ((d = readdirfd(fd)) != NULL) {
534 		sprintf(line, "%s\n", d->d_name);
535 		if (pager_output(line))
536 			break;
537 	}
538 	pager_close();
539 	close(fd);
540 }
541 
542 static int
543 parse_cmd(void)
544 {
545 	char *arg = cmd;
546 	char *ep, *p, *q;
547 	const char *cp;
548 	char line[80];
549 	int c, i, j;
550 
551 	while ((c = *arg++)) {
552 		if (c == ' ' || c == '\t' || c == '\n')
553 			continue;
554 		for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
555 			;
556 		ep = p;
557 		if (*p)
558 			*p++ = 0;
559 		if (c == '-') {
560 			while ((c = *arg++)) {
561 				if (c == 'P') {
562 					if (*(uint8_t *)PTOV(0x496) & 0x10) {
563 						cp = "yes";
564 					} else {
565 						opts |= OPT_SET(RBX_DUAL);
566 						opts |= OPT_SET(RBX_SERIAL);
567 						cp = "no";
568 					}
569 					printf("Keyboard: %s\n", cp);
570 					continue;
571 				} else if (c == 'S') {
572 					j = 0;
573 					while ((unsigned int)
574 					    (i = *arg++ - '0') <= 9)
575 						j = j * 10 + i;
576 					if (j > 0 && i == -'0') {
577 						comspeed = j;
578 						break;
579 					}
580 					/*
581 					 * Fall through to error below
582 					 * ('S' not in optstr[]).
583 					 */
584 				}
585 				for (i = 0; c != optstr[i]; i++)
586 					if (i == NOPT - 1)
587 						return (-1);
588 				opts ^= OPT_SET(flags[i]);
589 			}
590 			ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
591 			    OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
592 			if (ioctrl & IO_SERIAL) {
593 				if (sio_init(115200 / comspeed) != 0)
594 					ioctrl &= ~IO_SERIAL;
595 			}
596 		} if (c == '?') {
597 			printf("\n");
598 			if (*arg == '\0')
599 				arg = (char *)"/";
600 			fs_list(arg);
601 			zfs_list(arg);
602 			return (-1);
603 		} else {
604 			char *ptr;
605 			printf("\n");
606 			arg--;
607 
608 			/*
609 			 * Report pool status if the comment is 'status'. Lets
610 			 * hope no-one wants to load /status as a kernel.
611 			 */
612 			if (strcmp(arg, "status") == 0) {
613 				pager_open();
614 				for (i = 0; devsw[i] != NULL; i++) {
615 					if (devsw[i]->dv_print != NULL) {
616 						if (devsw[i]->dv_print(1))
617 							break;
618 					} else {
619 						snprintf(line, sizeof(line),
620 						    "%s: (unknown)\n",
621 						    devsw[i]->dv_name);
622 						if (pager_output(line))
623 							break;
624 					}
625 				}
626 				pager_close();
627 				return (-1);
628 			}
629 
630 			/*
631 			 * If there is "zfs:" prefix simply ignore it.
632 			 */
633 			ptr = arg;
634 			if (strncmp(ptr, "zfs:", 4) == 0)
635 				ptr += 4;
636 
637 			/*
638 			 * If there is a colon, switch pools.
639 			 */
640 			q = strchr(ptr, ':');
641 			if (q) {
642 				*q++ = '\0';
643 				if (mount_root(arg) != 0) {
644 					return (-1);
645 				}
646 				arg = q;
647 			}
648 			if ((i = ep - arg)) {
649 				if ((size_t)i >= sizeof(kname))
650 					return (-1);
651 				memcpy(kname, arg, i + 1);
652 			}
653 		}
654 		arg = p;
655 	}
656 	return (0);
657 }
658 
659 /*
660  * Probe all disks to discover ZFS pools. The idea is to walk all possible
661  * disk devices, however, we also need to identify possible boot pool.
662  * For boot pool detection we have boot disk passed us from BIOS, recorded
663  * in bootinfo.bi_bios_dev.
664  */
665 static void
666 i386_zfs_probe(void)
667 {
668 	char devname[32];
669 	int boot_unit;
670 	struct i386_devdesc dev;
671 	uint64_t pool_guid = 0;
672 
673 	dev.dd.d_dev = &bioshd;
674 	/* Translate bios dev to our unit number. */
675 	boot_unit = bd_bios2unit(bootinfo.bi_bios_dev);
676 
677 	/*
678 	 * Open all the disks we can find and see if we can reconstruct
679 	 * ZFS pools from them.
680 	 */
681 	for (dev.dd.d_unit = 0; bd_unit2bios(&dev) >= 0; dev.dd.d_unit++) {
682 		snprintf(devname, sizeof (devname), "%s%d:", bioshd.dv_name,
683 		    dev.dd.d_unit);
684 		/* If this is not boot disk, use generic probe. */
685 		if (dev.dd.d_unit != boot_unit)
686 			zfs_probe_dev(devname, NULL);
687 		else
688 			zfs_probe_dev(devname, &pool_guid);
689 
690 		if (pool_guid != 0 && bdev == NULL) {
691 			bdev = malloc(sizeof (struct i386_devdesc));
692 			bzero(bdev, sizeof (struct i386_devdesc));
693 			bdev->dd.d_dev = &zfs_dev;
694 			bdev->d_kind.zfs.pool_guid = pool_guid;
695 		}
696 	}
697 }
698