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>
19 #include <sys/param.h>
20 #include <sys/errno.h>
21 #include <sys/diskmbr.h>
25 #include <sys/reboot.h>
26 #include <sys/queue.h>
27 #ifdef LOADER_ZFS_SUPPORT
28 #include <sys/zfs_bootenv.h>
31 #include <machine/bootinfo.h>
32 #include <machine/elf.h>
33 #include <machine/pc/bios.h>
39 #include "bootstrap.h"
57 #define BIOS_NUMDRIVES 0x475
63 #define TYPE_MAXHARD TYPE_DA
68 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
69 static const unsigned char flags[NOPT] = {
88 * Paths to try loading before falling back to the boot2 prompt.
90 * /boot/zfsloader must be tried before /boot/loader in order to remain
91 * backward compatible with ZFS boot environments where /boot/loader exists
92 * but does not have ZFS support, which was the case before FreeBSD 12.
94 * If no loader is found, try to load a kernel directly instead.
96 static const struct string {
100 { PATH_LOADER_ZFS, sizeof(PATH_LOADER_ZFS) },
101 { PATH_LOADER, sizeof(PATH_LOADER) },
102 { PATH_KERNEL, sizeof(PATH_KERNEL) },
105 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
107 static struct i386_devdesc *bdev;
108 static char cmd[512];
109 static char cmddup[512];
110 static char kname[1024];
111 static int comspeed = SIOSPD;
112 static struct bootinfo bootinfo;
113 static uint32_t bootdev;
114 static struct zfs_boot_args zfsargs;
115 #ifdef LOADER_GELI_SUPPORT
116 static struct geli_boot_args geliargs;
119 extern vm_offset_t high_heap_base;
120 extern uint32_t bios_basemem, bios_extmem, high_heap_size;
122 static char *heap_top;
123 static char *heap_bottom;
126 static void i386_zfs_probe(void);
127 static void load(void);
128 static int parse_cmd(void);
130 #ifdef LOADER_GELI_SUPPORT
131 #include "geliboot.h"
132 static char gelipw[GELI_PW_MAXLEN];
135 struct arch_switch archsw; /* MI/MD interface boundary */
136 static char boot_devname[2 * ZFS_MAXNAMELEN + 8]; /* disk or pool:dataset */
138 struct devsw *devsw[] = {
140 #if defined(LOADER_ZFS_SUPPORT)
146 struct fs_ops *file_system[] = {
147 #if defined(LOADER_ZFS_SUPPORT)
150 #if defined(LOADER_UFS_SUPPORT)
168 int auto_boot, fd, nextboot = 0;
169 struct disk_devdesc *devdesc;
173 if (high_heap_size > 0) {
174 heap_top = PTOV(high_heap_base + high_heap_size);
175 heap_bottom = PTOV(high_heap_base);
177 heap_bottom = (char *)
178 (roundup2(__base + (int32_t)&_end, 0x10000) - __base);
179 heap_top = (char *)PTOV(bios_basemem);
181 setheap(heap_bottom, heap_top);
184 * Initialise the block cache. Set the upper limit.
186 bcache_init(32768, 512);
188 archsw.arch_autoload = NULL;
189 archsw.arch_getdev = i386_getdev;
190 archsw.arch_copyin = NULL;
191 archsw.arch_copyout = NULL;
192 archsw.arch_readin = NULL;
193 archsw.arch_isainb = NULL;
194 archsw.arch_isaoutb = NULL;
195 archsw.arch_zfs_probe = i386_zfs_probe;
197 bootinfo.bi_version = BOOTINFO_VERSION;
198 bootinfo.bi_size = sizeof(bootinfo);
199 bootinfo.bi_basemem = bios_basemem / 1024;
200 bootinfo.bi_extmem = bios_extmem / 1024;
201 bootinfo.bi_memsizes_valid++;
202 bootinfo.bi_bios_dev = *(uint8_t *)PTOV(ARGS);
204 /* Set up fall back device name. */
205 snprintf(boot_devname, sizeof (boot_devname), "disk%d:",
206 bd_bios2unit(bootinfo.bi_bios_dev));
208 /* Set up currdev variable to have hooks in place. */
209 env_setenv("currdev", EV_VOLATILE, "", gen_setcurrdev,
214 /* XXX assumes this will be a disk, but it looks likely give above */
215 disk_parsedev((struct devdesc **)&devdesc, boot_devname, NULL);
217 bootdev = MAKEBOOTDEV(dev_maj[DEVT_DISK], devdesc->d_slice + 1,
219 devdesc->d_partition >= 0 ? devdesc->d_partition : 0xff);
223 * devformat() can be called only after dv_init
225 if (bdev != NULL && bdev->dd.d_dev->dv_type == DEVT_ZFS) {
226 /* set up proper device name string for ZFS */
227 strncpy(boot_devname, devformat(&bdev->dd), sizeof (boot_devname));
228 if (zfs_get_bootonce(bdev, OS_BOOTONCE, cmd,
233 memcpy(cmddup, cmd, sizeof(cmd));
235 if (!OPT_CHECK(RBX_QUIET))
236 printf("failed to parse bootonce "
240 if (!OPT_CHECK(RBX_QUIET))
241 printf("zfs bootonce: %s\n", cmddup);
243 if (zfs_get_bootenv(bdev, &benv) == 0) {
244 nvlist_add_string(benv, OS_BOOTONCE_USED,
246 zfs_set_bootenv(bdev, benv);
248 /* Do not process this command twice */
253 /* now make sure we have bdev on all cases */
255 i386_getdev((void **)&bdev, boot_devname, NULL);
257 env_setenv("currdev", EV_VOLATILE, boot_devname, gen_setcurrdev,
260 /* Process configuration file */
263 fd = open(PATH_CONFIG, O_RDONLY);
265 fd = open(PATH_DOTCONFIG, O_RDONLY);
270 if ((cmdlen = read(fd, cmd, sizeof(cmd))) > 0)
279 * Note that parse_cmd() is destructive to cmd[] and we also
280 * want to honor RBX_QUIET option that could be present in
283 memcpy(cmddup, cmd, sizeof(cmd));
286 if (!OPT_CHECK(RBX_QUIET))
287 printf("%s: %s\n", PATH_CONFIG, cmddup);
288 /* Do not process this command twice */
292 /* Do not risk waiting at the prompt forever. */
293 if (nextboot && !auto_boot)
296 if (auto_boot && !*kname) {
298 * Iterate through the list of loader and kernel paths,
299 * trying to load. If interrupted by a keypress, or in case of
300 * failure, drop the user to the boot2 prompt.
302 for (i = 0; i < nitems(loadpath); i++) {
303 memcpy(kname, loadpath[i].p, loadpath[i].len);
310 /* Present the user with the boot2 prompt. */
313 if (!auto_boot || !OPT_CHECK(RBX_QUIET)) {
314 printf("\nFreeBSD/x86 boot\n");
315 printf("Default: %s%s\nboot: ", boot_devname, kname);
317 if (ioctrl & IO_SERIAL)
319 if (!auto_boot || keyhit(5))
320 getstr(cmd, sizeof(cmd));
321 else if (!auto_boot || !OPT_CHECK(RBX_QUIET))
331 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
345 static Elf32_Phdr ep[2];
346 static Elf32_Shdr es[2];
352 if ((fd = open(kname, O_RDONLY)) == -1) {
353 printf("\nCan't find %s\n", kname);
358 if (read(fd, &hdr, sizeof (hdr)) != size) {
362 if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
364 } else if (IS_ELF(hdr.eh)) {
367 printf("Invalid %s\n", "format");
372 addr = hdr.ex.a_entry & 0xffffff;
374 lseek(fd, PAGE_SIZE, SEEK_SET);
375 size = hdr.ex.a_text;
376 if (read(fd, p, hdr.ex.a_text) != size) {
380 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
381 size = hdr.ex.a_data;
382 if (read(fd, p, hdr.ex.a_data) != size) {
386 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
387 bootinfo.bi_symtab = VTOP(p);
388 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
389 p += sizeof(hdr.ex.a_syms);
391 size = hdr.ex.a_syms;
392 if (read(fd, p, hdr.ex.a_syms) != size) {
398 if (read(fd, p, sizeof (int)) != size) {
406 if (read(fd, p, x) != size) {
413 lseek(fd, hdr.eh.e_phoff, SEEK_SET);
414 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
415 size = sizeof (ep[0]);
416 if (read(fd, ep + j, sizeof (ep[0])) != size) {
420 if (ep[j].p_type == PT_LOAD)
423 for (i = 0; i < 2; i++) {
424 p = PTOV(ep[i].p_paddr & 0xffffff);
425 lseek(fd, ep[i].p_offset, SEEK_SET);
426 size = ep[i].p_filesz;
427 if (read(fd, p, ep[i].p_filesz) != size) {
432 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
433 bootinfo.bi_symtab = VTOP(p);
434 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
435 lseek(fd, hdr.eh.e_shoff +
436 sizeof (es[0]) * (hdr.eh.e_shstrndx + 1),
439 if (read(fd, &es, sizeof (es)) != size) {
443 for (i = 0; i < 2; i++) {
444 memcpy(p, &es[i].sh_size,
445 sizeof(es[i].sh_size));
446 p += sizeof(es[i].sh_size);
447 lseek(fd, es[i].sh_offset, SEEK_SET);
448 size = es[i].sh_size;
449 if (read(fd, p, es[i].sh_size) != size) {
456 addr = hdr.eh.e_entry & 0xffffff;
460 bootinfo.bi_esymtab = VTOP(p);
461 bootinfo.bi_kernelname = VTOP(kname);
462 #ifdef LOADER_GELI_SUPPORT
463 explicit_bzero(gelipw, sizeof(gelipw));
466 if (bdev->dd.d_dev->dv_type == DEVT_ZFS) {
467 zfsargs.size = sizeof(zfsargs);
468 zfsargs.pool = bdev->zfs.pool_guid;
469 zfsargs.root = bdev->zfs.root_guid;
470 #ifdef LOADER_GELI_SUPPORT
471 export_geli_boot_data(&zfsargs.gelidata);
474 * Note that the zfsargs struct is passed by value, not by
475 * pointer. Code in btxldr.S copies the values from the entry
476 * stack to a fixed location within loader(8) at startup due
477 * to the presence of KARGS_FLAGS_EXTARG.
479 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
481 KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
482 (uint32_t)bdev->zfs.pool_guid,
483 (uint32_t)(bdev->zfs.pool_guid >> 32),
487 #ifdef LOADER_GELI_SUPPORT
488 geliargs.size = sizeof(geliargs);
489 export_geli_boot_data(&geliargs.gelidata);
493 * Note that the geliargs struct is passed by value, not by
494 * pointer. Code in btxldr.S copies the values from the entry
495 * stack to a fixed location within loader(8) at startup due
496 * to the presence of the KARGS_FLAGS_EXTARG flag.
498 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
500 #ifdef LOADER_GELI_SUPPORT
501 KARGS_FLAGS_GELI | KARGS_FLAGS_EXTARG, 0, 0,
502 VTOP(&bootinfo), geliargs
504 0, 0, 0, VTOP(&bootinfo)
511 mount_root(char *arg)
514 struct i386_devdesc *ddesc;
517 if (asprintf(&root, "%s:", arg) < 0)
520 if (i386_getdev((void **)&ddesc, root, NULL)) {
525 /* we should have new device descriptor, free old and replace it. */
528 if (bdev->dd.d_dev->dv_type == DEVT_DISK) {
529 if (bdev->disk.d_partition == -1)
532 part = bdev->disk.d_partition;
533 bootdev = MAKEBOOTDEV(dev_maj[bdev->dd.d_dev->dv_type],
534 bdev->disk.d_slice + 1, bdev->dd.d_unit, part);
535 bootinfo.bi_bios_dev = bd_unit2bios(bdev);
537 strncpy(boot_devname, root, sizeof (boot_devname));
538 setenv("currdev", root, 1);
550 fd = open(arg, O_RDONLY);
554 while ((d = readdirfd(fd)) != NULL) {
555 sprintf(line, "%s\n", d->d_name);
556 if (pager_output(line))
572 while ((c = *arg++)) {
573 if (c == ' ' || c == '\t' || c == '\n')
575 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
581 while ((c = *arg++)) {
583 if (*(uint8_t *)PTOV(0x496) & 0x10) {
586 opts |= OPT_SET(RBX_DUAL);
587 opts |= OPT_SET(RBX_SERIAL);
590 printf("Keyboard: %s\n", cp);
592 } else if (c == 'S') {
594 while ((unsigned int)
595 (i = *arg++ - '0') <= 9)
597 if (j > 0 && i == -'0') {
602 * Fall through to error below
603 * ('S' not in optstr[]).
606 for (i = 0; c != optstr[i]; i++)
609 opts ^= OPT_SET(flags[i]);
611 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
612 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
613 if (ioctrl & IO_SERIAL) {
614 if (sio_init(115200 / comspeed) != 0)
615 ioctrl &= ~IO_SERIAL;
630 * Report pool status if the comment is 'status'. Lets
631 * hope no-one wants to load /status as a kernel.
633 if (strcmp(arg, "status") == 0) {
635 for (i = 0; devsw[i] != NULL; i++) {
636 if (devsw[i]->dv_print != NULL) {
637 if (devsw[i]->dv_print(1))
640 snprintf(line, sizeof(line),
643 if (pager_output(line))
652 * If there is "zfs:" prefix simply ignore it.
655 if (strncmp(ptr, "zfs:", 4) == 0)
659 * If there is a colon, switch pools.
661 q = strchr(ptr, ':');
664 if (mount_root(arg) != 0) {
669 if ((i = ep - arg)) {
670 if ((size_t)i >= sizeof(kname))
672 memcpy(kname, arg, i + 1);
681 * Probe all disks to discover ZFS pools. The idea is to walk all possible
682 * disk devices, however, we also need to identify possible boot pool.
683 * For boot pool detection we have boot disk passed us from BIOS, recorded
684 * in bootinfo.bi_bios_dev.
691 struct i386_devdesc dev;
692 uint64_t pool_guid = 0;
694 dev.dd.d_dev = &bioshd;
695 /* Translate bios dev to our unit number. */
696 boot_unit = bd_bios2unit(bootinfo.bi_bios_dev);
699 * Open all the disks we can find and see if we can reconstruct
700 * ZFS pools from them.
702 for (dev.dd.d_unit = 0; bd_unit2bios(&dev) >= 0; dev.dd.d_unit++) {
703 snprintf(devname, sizeof (devname), "%s%d:", bioshd.dv_name,
705 /* If this is not boot disk, use generic probe. */
706 if (dev.dd.d_unit != boot_unit)
707 zfs_probe_dev(devname, NULL, true);
709 zfs_probe_dev(devname, &pool_guid, true);
711 if (pool_guid != 0 && bdev == NULL) {
712 bdev = malloc(sizeof (struct i386_devdesc));
713 bzero(bdev, sizeof (struct i386_devdesc));
714 bdev->zfs.dd.d_dev = &zfs_dev;
715 bdev->zfs.pool_guid = pool_guid;