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$");
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>
28 #include <machine/bootinfo.h>
29 #include <machine/elf.h>
30 #include <machine/pc/bios.h>
53 #define BIOS_NUMDRIVES 0x475
59 #define TYPE_MAXHARD TYPE_DA
65 static const uuid_t freebsd_zfs_uuid = GPT_ENT_TYPE_FREEBSD_ZFS;
67 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
68 static const unsigned char flags[NOPT] = {
86 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
89 static char cmddup[512];
90 static char kname[1024];
91 static char rootname[256];
92 static int comspeed = SIOSPD;
93 static struct bootinfo bootinfo;
94 static uint32_t bootdev;
95 static struct zfs_boot_args zfsargs;
96 static struct zfsmount zfsmount;
98 vm_offset_t high_heap_base;
99 uint32_t bios_basemem, bios_extmem, high_heap_size;
101 static struct bios_smap smap;
104 * The minimum amount of memory to reserve in bios_extmem for the heap.
106 #define HEAP_MIN (3 * 1024 * 1024)
108 static char *heap_next;
109 static char *heap_end;
111 /* Buffers that must not span a 64k boundary. */
112 #define READ_BUF_SIZE 8192
114 char rdbuf[READ_BUF_SIZE]; /* for reading large things */
115 char secbuf[READ_BUF_SIZE]; /* for MBR/disklabel */
117 static struct dmadat *dmadat;
121 static void load(void);
122 static int parse(void);
123 static void bios_getmem(void);
129 if (p + n > heap_end) {
130 printf("malloc failure\n");
140 strdup(const char *s)
142 char *p = malloc(strlen(s) + 1);
150 * Read from a dnode (which must be from a ZPL filesystem).
153 zfs_read(spa_t *spa, const dnode_phys_t *dnode, off_t *offp, void *start, size_t size)
155 const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus;
160 if (*offp + n > zp->zp_size)
161 n = zp->zp_size - *offp;
163 rc = dnode_read(spa, dnode, *offp, start, n);
175 static spa_t *primary_spa;
176 static vdev_t *primary_vdev;
179 * A wrapper for dskread that doesn't have to worry about whether the
180 * buffer pointer crosses a 64k boundary.
183 vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
188 struct dsk *dsk = (struct dsk *) priv;
190 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
194 lba = off / DEV_BSIZE;
197 nb = bytes / DEV_BSIZE;
198 if (nb > READ_BUF_SIZE / DEV_BSIZE)
199 nb = READ_BUF_SIZE / DEV_BSIZE;
200 if (drvread(dsk, dmadat->rdbuf, lba, nb))
202 memcpy(p, dmadat->rdbuf, nb * DEV_BSIZE);
205 bytes -= nb * DEV_BSIZE;
212 vdev_write(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
217 struct dsk *dsk = (struct dsk *) priv;
219 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
223 lba = off / DEV_BSIZE;
226 nb = bytes / DEV_BSIZE;
227 if (nb > READ_BUF_SIZE / DEV_BSIZE)
228 nb = READ_BUF_SIZE / DEV_BSIZE;
229 memcpy(dmadat->rdbuf, p, nb * DEV_BSIZE);
230 if (drvwrite(dsk, dmadat->rdbuf, lba, nb))
234 bytes -= nb * DEV_BSIZE;
241 xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte)
243 if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) {
244 printf("Invalid format\n");
251 * Read Pad2 (formerly "Boot Block Header") area of the first
252 * vdev label of the given vdev.
255 vdev_read_pad2(vdev_t *vdev, char *buf, size_t size)
258 char *tmp = zap_scratch;
259 off_t off = offsetof(vdev_label_t, vl_pad2);
261 if (size > VDEV_PAD_SIZE)
262 size = VDEV_PAD_SIZE;
265 BP_SET_LSIZE(&bp, VDEV_PAD_SIZE);
266 BP_SET_PSIZE(&bp, VDEV_PAD_SIZE);
267 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
268 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
269 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
270 if (vdev_read_phys(vdev, &bp, tmp, off, 0))
272 memcpy(buf, tmp, size);
277 vdev_clear_pad2(vdev_t *vdev)
279 char *zeroes = zap_scratch;
281 off_t off = offsetof(vdev_label_t, vl_pad2);
283 memset(zeroes, 0, VDEV_PAD_SIZE);
284 end = (uint64_t *)(zeroes + VDEV_PAD_SIZE);
285 /* ZIO_CHECKSUM_LABEL magic and pre-calcualted checksum for all zeros */
286 end[-5] = 0x0210da7ab10c7a11;
287 end[-4] = 0x97f48f807f6e2a3f;
288 end[-3] = 0xaf909f1658aacefc;
289 end[-2] = 0xcbd1ea57ff6db48b;
290 end[-1] = 0x6ec692db0d465fab;
291 if (vdev_write(vdev, vdev->v_read_priv, off, zeroes, VDEV_PAD_SIZE))
301 /* Parse system memory map */
305 v86.addr = 0x15; /* int 0x15 function 0xe820*/
307 v86.ecx = sizeof(struct bios_smap);
309 v86.es = VTOPSEG(&smap);
310 v86.edi = VTOPOFF(&smap);
312 if (V86_CY(v86.efl) || (v86.eax != SMAP_SIG))
314 /* look for a low-memory segment that's large enough */
315 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
316 (smap.length >= (512 * 1024)))
317 bios_basemem = smap.length;
318 /* look for the first segment in 'extended' memory */
319 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000)) {
320 bios_extmem = smap.length;
324 * Look for the largest segment in 'extended' memory beyond
327 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
328 (smap.base < 0x100000000ull)) {
332 * If this segment crosses the 4GB boundary, truncate it.
334 if (smap.base + size > 0x100000000ull)
335 size = 0x100000000ull - smap.base;
337 if (size > high_heap_size) {
338 high_heap_size = size;
339 high_heap_base = smap.base;
342 } while (v86.ebx != 0);
344 /* Fall back to the old compatibility function for base memory */
345 if (bios_basemem == 0) {
347 v86.addr = 0x12; /* int 0x12 */
350 bios_basemem = (v86.eax & 0xffff) * 1024;
353 /* Fall back through several compatibility functions for extended memory */
354 if (bios_extmem == 0) {
356 v86.addr = 0x15; /* int 0x15 function 0xe801*/
359 if (!V86_CY(v86.efl)) {
360 bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
363 if (bios_extmem == 0) {
365 v86.addr = 0x15; /* int 0x15 function 0x88*/
368 bios_extmem = (v86.eax & 0xffff) * 1024;
372 * If we have extended memory and did not find a suitable heap
373 * region in the SMAP, use the last 3MB of 'extended' memory as a
374 * high heap candidate.
376 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
377 high_heap_size = HEAP_MIN;
378 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
383 * Try to detect a device supported by the legacy int13 BIOS
386 int13probe(int drive)
394 if (!V86_CY(v86.efl) && /* carry clear */
395 ((v86.edx & 0xff) != (drive & DRV_MASK))) { /* unit # OK */
396 if ((v86.ecx & 0x3f) == 0) { /* absurd sector size */
397 return(0); /* skip device */
405 * We call this when we find a ZFS vdev - ZFS consumes the dsk
406 * structure so we must make a new one.
409 copy_dsk(struct dsk *dsk)
413 newdsk = malloc(sizeof(struct dsk));
419 probe_drive(struct dsk *dsk)
425 unsigned part, entries_per_sec;
427 struct dos_partition *dp;
432 * If we find a vdev on the whole disk, stop here. Otherwise dig
433 * out the partition table and probe each slice/partition
434 * in turn for a vdev.
436 if (vdev_probe(vdev_read, dsk, NULL) == 0)
439 sec = dmadat->secbuf;
444 * First check for GPT.
446 if (drvread(dsk, sec, 1, 1)) {
449 memcpy(&hdr, sec, sizeof(hdr));
450 if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
451 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
452 hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
457 * Probe all GPT partitions for the presense of ZFS pools. We
458 * return the spa_t for the first we find (if requested). This
459 * will have the effect of booting from the first pool on the
462 entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
463 slba = hdr.hdr_lba_table;
464 elba = slba + hdr.hdr_entries / entries_per_sec;
465 while (slba < elba) {
467 if (drvread(dsk, sec, slba, 1))
469 for (part = 0; part < entries_per_sec; part++) {
470 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
471 if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
472 sizeof(uuid_t)) == 0) {
473 dsk->start = ent->ent_lba_start;
474 if (vdev_probe(vdev_read, dsk, NULL) == 0) {
476 * This slice had a vdev. We need a new dsk
477 * structure now since the vdev now owns this one.
489 if (drvread(dsk, sec, DOSBBSECTOR, 1))
491 dp = (void *)(sec + DOSPARTOFF);
493 for (i = 0; i < NDOSPART; i++) {
496 dsk->start = dp[i].dp_start;
497 if (vdev_probe(vdev_read, dsk, NULL) == 0) {
499 * This slice had a vdev. We need a new dsk structure now
500 * since the vdev now owns this one.
517 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
521 if (high_heap_size > 0) {
522 heap_end = PTOV(high_heap_base + high_heap_size);
523 heap_next = PTOV(high_heap_base);
525 heap_next = (char *) dmadat + sizeof(*dmadat);
526 heap_end = (char *) PTOV(bios_basemem);
529 dsk = malloc(sizeof(struct dsk));
530 dsk->drive = *(uint8_t *)PTOV(ARGS);
531 dsk->type = dsk->drive & DRV_HARD ? TYPE_AD : TYPE_FD;
532 dsk->unit = dsk->drive & DRV_MASK;
533 dsk->slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
538 bootinfo.bi_version = BOOTINFO_VERSION;
539 bootinfo.bi_size = sizeof(bootinfo);
540 bootinfo.bi_basemem = bios_basemem / 1024;
541 bootinfo.bi_extmem = bios_extmem / 1024;
542 bootinfo.bi_memsizes_valid++;
543 bootinfo.bi_bios_dev = dsk->drive;
545 bootdev = MAKEBOOTDEV(dev_maj[dsk->type],
546 dsk->slice, dsk->unit, dsk->part);
548 /* Process configuration file */
555 * Probe the boot drive first - we will try to boot from whatever
556 * pool we find on that drive.
561 * Probe the rest of the drives that the bios knows about. This
562 * will find any other available pools and it may fill in missing
563 * vdevs for the boot pool.
566 for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++)
568 for (i = 0; i < MAXBDDEV; i++)
571 if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
574 if (!int13probe(i | DRV_HARD))
577 dsk = malloc(sizeof(struct dsk));
578 dsk->drive = i | DRV_HARD;
579 dsk->type = dsk->drive & TYPE_AD;
589 * The first discovered pool, if any, is the pool.
591 spa = spa_get_primary();
593 printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
599 primary_vdev = spa_get_primary_vdev(spa);
602 rc = vdev_read_pad2(primary_vdev, cmd, sizeof(cmd));
603 if (vdev_clear_pad2(primary_vdev))
604 printf("failed to clear pad2 area of primary vdev\n");
608 * We could find an old-style ZFS Boot Block header here.
611 if (*(uint64_t *)cmd != 0x2f5b007b10c) {
613 * Note that parse() is destructive to cmd[] and we also want
614 * to honor RBX_QUIET option that could be present in cmd[].
617 memcpy(cmddup, cmd, sizeof(cmd));
619 printf("failed to parse pad2 area of primary vdev\n");
622 if (!OPT_CHECK(RBX_QUIET))
623 printf("zfs nextboot: %s\n", cmddup);
625 /* Do not process this command twice */
629 printf("failed to read pad2 area of primary vdev\n");
631 /* Mount ZFS only if it's not already mounted via nextboot parsing. */
632 if (zfsmount.spa == NULL &&
633 (zfs_spa_init(spa) != 0 || zfs_mount(spa, 0, &zfsmount) != 0)) {
634 printf("%s: failed to mount default pool %s\n",
635 BOOTPROG, spa->spa_name);
637 } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 ||
638 zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) {
640 zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
645 * Note that parse() is destructive to cmd[] and we also want
646 * to honor RBX_QUIET option that could be present in cmd[].
648 memcpy(cmddup, cmd, sizeof(cmd));
651 if (!OPT_CHECK(RBX_QUIET))
652 printf("%s: %s\n", PATH_CONFIG, cmddup);
653 /* Do not process this command twice */
657 /* Do not risk waiting at the prompt forever. */
658 if (nextboot && !autoboot)
662 * Try to exec /boot/loader. If interrupted by a keypress,
663 * or in case of failure, try to load a kernel directly instead.
666 if (autoboot && !*kname) {
667 memcpy(kname, PATH_LOADER_ZFS, sizeof(PATH_LOADER_ZFS));
670 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
674 /* Present the user with the boot2 prompt. */
677 if (!autoboot || !OPT_CHECK(RBX_QUIET)) {
678 printf("\nFreeBSD/x86 boot\n");
679 if (zfs_rlookup(spa, zfsmount.rootobj, rootname) != 0)
680 printf("Default: %s/<0x%llx>:%s\n"
682 spa->spa_name, zfsmount.rootobj, kname);
683 else if (rootname[0] != '\0')
684 printf("Default: %s/%s:%s\n"
686 spa->spa_name, rootname, kname);
688 printf("Default: %s:%s\n"
690 spa->spa_name, kname);
692 if (ioctrl & IO_SERIAL)
694 if (!autoboot || keyhit(5))
695 getstr(cmd, sizeof(cmd));
696 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
706 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
726 static Elf32_Phdr ep[2];
727 static Elf32_Shdr es[2];
734 if (zfs_lookup(&zfsmount, kname, &dn)) {
735 printf("\nCan't find %s\n", kname);
739 if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
741 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
743 else if (IS_ELF(hdr.eh))
746 printf("Invalid %s\n", "format");
750 addr = hdr.ex.a_entry & 0xffffff;
753 if (xfsread(&dn, &off, p, hdr.ex.a_text))
755 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
756 if (xfsread(&dn, &off, p, hdr.ex.a_data))
758 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
759 bootinfo.bi_symtab = VTOP(p);
760 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
761 p += sizeof(hdr.ex.a_syms);
763 if (xfsread(&dn, &off, p, hdr.ex.a_syms))
766 if (xfsread(&dn, &off, p, sizeof(int)))
771 if (xfsread(&dn, &off, p, x))
776 off = hdr.eh.e_phoff;
777 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
778 if (xfsread(&dn, &off, ep + j, sizeof(ep[0])))
780 if (ep[j].p_type == PT_LOAD)
783 for (i = 0; i < 2; i++) {
784 p = PTOV(ep[i].p_paddr & 0xffffff);
785 off = ep[i].p_offset;
786 if (xfsread(&dn, &off, p, ep[i].p_filesz))
789 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
790 bootinfo.bi_symtab = VTOP(p);
791 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
792 off = hdr.eh.e_shoff + sizeof(es[0]) *
793 (hdr.eh.e_shstrndx + 1);
794 if (xfsread(&dn, &off, &es, sizeof(es)))
796 for (i = 0; i < 2; i++) {
797 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
798 p += sizeof(es[i].sh_size);
799 off = es[i].sh_offset;
800 if (xfsread(&dn, &off, p, es[i].sh_size))
805 addr = hdr.eh.e_entry & 0xffffff;
807 bootinfo.bi_esymtab = VTOP(p);
808 bootinfo.bi_kernelname = VTOP(kname);
809 zfsargs.size = sizeof(zfsargs);
810 zfsargs.pool = zfsmount.spa->spa_guid;
811 zfsargs.root = zfsmount.rootobj;
812 zfsargs.primary_pool = primary_spa->spa_guid;
813 if (primary_vdev != NULL)
814 zfsargs.primary_vdev = primary_vdev->v_guid;
816 printf("failed to detect primary vdev\n");
817 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
819 KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
820 (uint32_t) spa->spa_guid,
821 (uint32_t) (spa->spa_guid >> 32),
827 zfs_mount_ds(char *dsname)
833 q = strchr(dsname, '/');
836 newspa = spa_find_by_name(dsname);
837 if (newspa == NULL) {
838 printf("\nCan't find ZFS pool %s\n", dsname);
842 if (zfs_spa_init(newspa))
847 if (zfs_lookup_dataset(newspa, q, &newroot)) {
848 printf("\nCan't find dataset %s in ZFS pool %s\n",
849 q, newspa->spa_name);
853 if (zfs_mount(newspa, newroot, &zfsmount)) {
854 printf("\nCan't mount ZFS dataset\n");
869 while ((c = *arg++)) {
870 if (c == ' ' || c == '\t' || c == '\n')
872 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
877 while ((c = *arg++)) {
879 if (*(uint8_t *)PTOV(0x496) & 0x10) {
882 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
885 printf("Keyboard: %s\n", cp);
887 } else if (c == 'S') {
889 while ((unsigned int)(i = *arg++ - '0') <= 9)
891 if (j > 0 && i == -'0') {
895 /* Fall through to error below ('S' not in optstr[]). */
897 for (i = 0; c != optstr[i]; i++)
900 opts ^= OPT_SET(flags[i]);
902 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
903 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
904 if (ioctrl & IO_SERIAL) {
905 if (sio_init(115200 / comspeed) != 0)
906 ioctrl &= ~IO_SERIAL;
911 if (zfs_lookup(&zfsmount, arg, &dn) == 0) {
919 * Report pool status if the comment is 'status'. Lets
920 * hope no-one wants to load /status as a kernel.
922 if (!strcmp(arg, "status")) {
928 * If there is "zfs:" prefix simply ignore it.
930 if (strncmp(arg, "zfs:", 4) == 0)
934 * If there is a colon, switch pools.
936 q = strchr(arg, ':');
939 if (zfs_mount_ds(arg) != 0)
943 if ((i = ep - arg)) {
944 if ((size_t)i >= sizeof(kname))
946 memcpy(kname, arg, i + 1);