2 * Copyright (c) 1998 Robert Nordier
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
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
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
21 #include <sys/param.h>
22 #include <sys/errno.h>
23 #include <sys/diskmbr.h>
27 #include <sys/reboot.h>
28 #include <sys/queue.h>
29 #ifdef LOADER_ZFS_SUPPORT
30 #include <sys/zfs_bootenv.h>
33 #include <machine/bootinfo.h>
34 #include <machine/elf.h>
35 #include <machine/pc/bios.h>
41 #include "bootstrap.h"
59 #define BIOS_NUMDRIVES 0x475
65 #define TYPE_MAXHARD TYPE_DA
70 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
71 static const unsigned char flags[NOPT] = {
90 * Paths to try loading before falling back to the boot2 prompt.
92 * /boot/zfsloader must be tried before /boot/loader in order to remain
93 * backward compatible with ZFS boot environments where /boot/loader exists
94 * but does not have ZFS support, which was the case before FreeBSD 12.
96 * If no loader is found, try to load a kernel directly instead.
98 static const struct string {
102 { PATH_LOADER_ZFS, sizeof(PATH_LOADER_ZFS) },
103 { PATH_LOADER, sizeof(PATH_LOADER) },
104 { PATH_KERNEL, sizeof(PATH_KERNEL) },
107 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
109 static struct i386_devdesc *bdev;
110 static char cmd[512];
111 static char cmddup[512];
112 static char kname[1024];
113 static int comspeed = SIOSPD;
114 static struct bootinfo bootinfo;
115 static uint32_t bootdev;
116 static struct zfs_boot_args zfsargs;
117 #ifdef LOADER_GELI_SUPPORT
118 static struct geli_boot_args geliargs;
121 extern vm_offset_t high_heap_base;
122 extern uint32_t bios_basemem, bios_extmem, high_heap_size;
124 static char *heap_top;
125 static char *heap_bottom;
128 static void i386_zfs_probe(void);
129 static void load(void);
130 static int parse_cmd(void);
132 #ifdef LOADER_GELI_SUPPORT
133 #include "geliboot.h"
134 static char gelipw[GELI_PW_MAXLEN];
137 struct arch_switch archsw; /* MI/MD interface boundary */
138 static char boot_devname[2 * ZFS_MAXNAMELEN + 8]; /* disk or pool:dataset */
140 struct devsw *devsw[] = {
142 #if defined(LOADER_ZFS_SUPPORT)
148 struct fs_ops *file_system[] = {
149 #if defined(LOADER_ZFS_SUPPORT)
152 #if defined(LOADER_UFS_SUPPORT)
170 int auto_boot, fd, nextboot = 0;
171 struct disk_devdesc *devdesc;
175 if (high_heap_size > 0) {
176 heap_top = PTOV(high_heap_base + high_heap_size);
177 heap_bottom = PTOV(high_heap_base);
179 heap_bottom = (char *)
180 (roundup2(__base + (int32_t)&_end, 0x10000) - __base);
181 heap_top = (char *)PTOV(bios_basemem);
183 setheap(heap_bottom, heap_top);
186 * Initialise the block cache. Set the upper limit.
188 bcache_init(32768, 512);
190 archsw.arch_autoload = NULL;
191 archsw.arch_getdev = i386_getdev;
192 archsw.arch_copyin = NULL;
193 archsw.arch_copyout = NULL;
194 archsw.arch_readin = NULL;
195 archsw.arch_isainb = NULL;
196 archsw.arch_isaoutb = NULL;
197 archsw.arch_zfs_probe = i386_zfs_probe;
199 bootinfo.bi_version = BOOTINFO_VERSION;
200 bootinfo.bi_size = sizeof(bootinfo);
201 bootinfo.bi_basemem = bios_basemem / 1024;
202 bootinfo.bi_extmem = bios_extmem / 1024;
203 bootinfo.bi_memsizes_valid++;
204 bootinfo.bi_bios_dev = *(uint8_t *)PTOV(ARGS);
206 /* Set up fall back device name. */
207 snprintf(boot_devname, sizeof (boot_devname), "disk%d:",
208 bd_bios2unit(bootinfo.bi_bios_dev));
210 /* Set up currdev variable to have hooks in place. */
211 env_setenv("currdev", EV_VOLATILE, "", gen_setcurrdev,
216 /* XXX assumes this will be a disk, but it looks likely give above */
217 disk_parsedev((struct devdesc **)&devdesc, boot_devname, NULL);
219 bootdev = MAKEBOOTDEV(dev_maj[DEVT_DISK], devdesc->d_slice + 1,
221 devdesc->d_partition >= 0 ? devdesc->d_partition : 0xff);
225 * devformat() can be called only after dv_init
227 if (bdev != NULL && bdev->dd.d_dev->dv_type == DEVT_ZFS) {
228 /* set up proper device name string for ZFS */
229 strncpy(boot_devname, devformat(&bdev->dd), sizeof (boot_devname));
230 if (zfs_get_bootonce(bdev, OS_BOOTONCE, cmd,
235 memcpy(cmddup, cmd, sizeof(cmd));
237 if (!OPT_CHECK(RBX_QUIET))
238 printf("failed to parse bootonce "
242 if (!OPT_CHECK(RBX_QUIET))
243 printf("zfs bootonce: %s\n", cmddup);
245 if (zfs_get_bootenv(bdev, &benv) == 0) {
246 nvlist_add_string(benv, OS_BOOTONCE_USED,
248 zfs_set_bootenv(bdev, benv);
250 /* Do not process this command twice */
255 /* now make sure we have bdev on all cases */
257 i386_getdev((void **)&bdev, boot_devname, NULL);
259 env_setenv("currdev", EV_VOLATILE, boot_devname, gen_setcurrdev,
262 /* Process configuration file */
265 fd = open(PATH_CONFIG, O_RDONLY);
267 fd = open(PATH_DOTCONFIG, O_RDONLY);
272 if ((cmdlen = read(fd, cmd, sizeof(cmd))) > 0)
281 * Note that parse_cmd() is destructive to cmd[] and we also
282 * want to honor RBX_QUIET option that could be present in
285 memcpy(cmddup, cmd, sizeof(cmd));
288 if (!OPT_CHECK(RBX_QUIET))
289 printf("%s: %s\n", PATH_CONFIG, cmddup);
290 /* Do not process this command twice */
294 /* Do not risk waiting at the prompt forever. */
295 if (nextboot && !auto_boot)
298 if (auto_boot && !*kname) {
300 * Iterate through the list of loader and kernel paths,
301 * trying to load. If interrupted by a keypress, or in case of
302 * failure, drop the user to the boot2 prompt.
304 for (i = 0; i < nitems(loadpath); i++) {
305 memcpy(kname, loadpath[i].p, loadpath[i].len);
312 /* Present the user with the boot2 prompt. */
315 if (!auto_boot || !OPT_CHECK(RBX_QUIET)) {
316 printf("\nFreeBSD/x86 boot\n");
317 printf("Default: %s%s\nboot: ", boot_devname, kname);
319 if (ioctrl & IO_SERIAL)
321 if (!auto_boot || keyhit(5))
322 getstr(cmd, sizeof(cmd));
323 else if (!auto_boot || !OPT_CHECK(RBX_QUIET))
333 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
347 static Elf32_Phdr ep[2];
348 static Elf32_Shdr es[2];
354 if ((fd = open(kname, O_RDONLY)) == -1) {
355 printf("\nCan't find %s\n", kname);
360 if (read(fd, &hdr, sizeof (hdr)) != size) {
364 if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
366 } else if (IS_ELF(hdr.eh)) {
369 printf("Invalid %s\n", "format");
374 addr = hdr.ex.a_entry & 0xffffff;
376 lseek(fd, PAGE_SIZE, SEEK_SET);
377 size = hdr.ex.a_text;
378 if (read(fd, p, hdr.ex.a_text) != size) {
382 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
383 size = hdr.ex.a_data;
384 if (read(fd, p, hdr.ex.a_data) != size) {
388 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
389 bootinfo.bi_symtab = VTOP(p);
390 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
391 p += sizeof(hdr.ex.a_syms);
393 size = hdr.ex.a_syms;
394 if (read(fd, p, hdr.ex.a_syms) != size) {
400 if (read(fd, p, sizeof (int)) != size) {
408 if (read(fd, p, x) != size) {
415 lseek(fd, hdr.eh.e_phoff, SEEK_SET);
416 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
417 size = sizeof (ep[0]);
418 if (read(fd, ep + j, sizeof (ep[0])) != size) {
422 if (ep[j].p_type == PT_LOAD)
425 for (i = 0; i < 2; i++) {
426 p = PTOV(ep[i].p_paddr & 0xffffff);
427 lseek(fd, ep[i].p_offset, SEEK_SET);
428 size = ep[i].p_filesz;
429 if (read(fd, p, ep[i].p_filesz) != size) {
434 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
435 bootinfo.bi_symtab = VTOP(p);
436 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
437 lseek(fd, hdr.eh.e_shoff +
438 sizeof (es[0]) * (hdr.eh.e_shstrndx + 1),
441 if (read(fd, &es, sizeof (es)) != size) {
445 for (i = 0; i < 2; i++) {
446 memcpy(p, &es[i].sh_size,
447 sizeof(es[i].sh_size));
448 p += sizeof(es[i].sh_size);
449 lseek(fd, es[i].sh_offset, SEEK_SET);
450 size = es[i].sh_size;
451 if (read(fd, p, es[i].sh_size) != size) {
458 addr = hdr.eh.e_entry & 0xffffff;
462 bootinfo.bi_esymtab = VTOP(p);
463 bootinfo.bi_kernelname = VTOP(kname);
464 #ifdef LOADER_GELI_SUPPORT
465 explicit_bzero(gelipw, sizeof(gelipw));
468 if (bdev->dd.d_dev->dv_type == DEVT_ZFS) {
469 zfsargs.size = sizeof(zfsargs);
470 zfsargs.pool = bdev->zfs.pool_guid;
471 zfsargs.root = bdev->zfs.root_guid;
472 #ifdef LOADER_GELI_SUPPORT
473 export_geli_boot_data(&zfsargs.gelidata);
476 * Note that the zfsargs struct is passed by value, not by
477 * pointer. Code in btxldr.S copies the values from the entry
478 * stack to a fixed location within loader(8) at startup due
479 * to the presence of KARGS_FLAGS_EXTARG.
481 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
483 KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
484 (uint32_t)bdev->zfs.pool_guid,
485 (uint32_t)(bdev->zfs.pool_guid >> 32),
489 #ifdef LOADER_GELI_SUPPORT
490 geliargs.size = sizeof(geliargs);
491 export_geli_boot_data(&geliargs.gelidata);
495 * Note that the geliargs struct is passed by value, not by
496 * pointer. Code in btxldr.S copies the values from the entry
497 * stack to a fixed location within loader(8) at startup due
498 * to the presence of the KARGS_FLAGS_EXTARG flag.
500 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
502 #ifdef LOADER_GELI_SUPPORT
503 KARGS_FLAGS_GELI | KARGS_FLAGS_EXTARG, 0, 0,
504 VTOP(&bootinfo), geliargs
506 0, 0, 0, VTOP(&bootinfo)
513 mount_root(char *arg)
516 struct i386_devdesc *ddesc;
519 if (asprintf(&root, "%s:", arg) < 0)
522 if (i386_getdev((void **)&ddesc, root, NULL)) {
527 /* we should have new device descriptor, free old and replace it. */
530 if (bdev->dd.d_dev->dv_type == DEVT_DISK) {
531 if (bdev->disk.d_partition == -1)
534 part = bdev->disk.d_partition;
535 bootdev = MAKEBOOTDEV(dev_maj[bdev->dd.d_dev->dv_type],
536 bdev->disk.d_slice + 1, bdev->dd.d_unit, part);
537 bootinfo.bi_bios_dev = bd_unit2bios(bdev);
539 strncpy(boot_devname, root, sizeof (boot_devname));
540 setenv("currdev", root, 1);
552 fd = open(arg, O_RDONLY);
556 while ((d = readdirfd(fd)) != NULL) {
557 sprintf(line, "%s\n", d->d_name);
558 if (pager_output(line))
574 while ((c = *arg++)) {
575 if (c == ' ' || c == '\t' || c == '\n')
577 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
583 while ((c = *arg++)) {
585 if (*(uint8_t *)PTOV(0x496) & 0x10) {
588 opts |= OPT_SET(RBX_DUAL);
589 opts |= OPT_SET(RBX_SERIAL);
592 printf("Keyboard: %s\n", cp);
594 } else if (c == 'S') {
596 while ((unsigned int)
597 (i = *arg++ - '0') <= 9)
599 if (j > 0 && i == -'0') {
604 * Fall through to error below
605 * ('S' not in optstr[]).
608 for (i = 0; c != optstr[i]; i++)
611 opts ^= OPT_SET(flags[i]);
613 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
614 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
615 if (ioctrl & IO_SERIAL) {
616 if (sio_init(115200 / comspeed) != 0)
617 ioctrl &= ~IO_SERIAL;
632 * Report pool status if the comment is 'status'. Lets
633 * hope no-one wants to load /status as a kernel.
635 if (strcmp(arg, "status") == 0) {
637 for (i = 0; devsw[i] != NULL; i++) {
638 if (devsw[i]->dv_print != NULL) {
639 if (devsw[i]->dv_print(1))
642 snprintf(line, sizeof(line),
645 if (pager_output(line))
654 * If there is "zfs:" prefix simply ignore it.
657 if (strncmp(ptr, "zfs:", 4) == 0)
661 * If there is a colon, switch pools.
663 q = strchr(ptr, ':');
666 if (mount_root(arg) != 0) {
671 if ((i = ep - arg)) {
672 if ((size_t)i >= sizeof(kname))
674 memcpy(kname, arg, i + 1);
683 * Probe all disks to discover ZFS pools. The idea is to walk all possible
684 * disk devices, however, we also need to identify possible boot pool.
685 * For boot pool detection we have boot disk passed us from BIOS, recorded
686 * in bootinfo.bi_bios_dev.
693 struct i386_devdesc dev;
694 uint64_t pool_guid = 0;
696 dev.dd.d_dev = &bioshd;
697 /* Translate bios dev to our unit number. */
698 boot_unit = bd_bios2unit(bootinfo.bi_bios_dev);
701 * Open all the disks we can find and see if we can reconstruct
702 * ZFS pools from them.
704 for (dev.dd.d_unit = 0; bd_unit2bios(&dev) >= 0; dev.dd.d_unit++) {
705 snprintf(devname, sizeof (devname), "%s%d:", bioshd.dv_name,
707 /* If this is not boot disk, use generic probe. */
708 if (dev.dd.d_unit != boot_unit)
709 zfs_probe_dev(devname, NULL, true);
711 zfs_probe_dev(devname, &pool_guid, true);
713 if (pool_guid != 0 && bdev == NULL) {
714 bdev = malloc(sizeof (struct i386_devdesc));
715 bzero(bdev, sizeof (struct i386_devdesc));
716 bdev->zfs.dd.d_dev = &zfs_dev;
717 bdev->zfs.pool_guid = pool_guid;