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 char *const dev_nm[NDEV] = {"ad", "da", "fd"};
87 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
90 static char cmddup[512];
91 static char kname[1024];
92 static char rootname[256];
93 static int comspeed = SIOSPD;
94 static struct bootinfo bootinfo;
95 static uint32_t bootdev;
96 static struct zfs_boot_args zfsargs;
97 static struct zfsmount zfsmount;
99 vm_offset_t high_heap_base;
100 uint32_t bios_basemem, bios_extmem, high_heap_size;
102 static struct bios_smap smap;
105 * The minimum amount of memory to reserve in bios_extmem for the heap.
107 #define HEAP_MIN (3 * 1024 * 1024)
109 static char *heap_next;
110 static char *heap_end;
112 /* Buffers that must not span a 64k boundary. */
113 #define READ_BUF_SIZE 8192
115 char rdbuf[READ_BUF_SIZE]; /* for reading large things */
116 char secbuf[READ_BUF_SIZE]; /* for MBR/disklabel */
118 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 xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte)
214 if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) {
215 printf("Invalid format\n");
226 /* Parse system memory map */
230 v86.addr = 0x15; /* int 0x15 function 0xe820*/
232 v86.ecx = sizeof(struct bios_smap);
234 v86.es = VTOPSEG(&smap);
235 v86.edi = VTOPOFF(&smap);
237 if (V86_CY(v86.efl) || (v86.eax != SMAP_SIG))
239 /* look for a low-memory segment that's large enough */
240 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
241 (smap.length >= (512 * 1024)))
242 bios_basemem = smap.length;
243 /* look for the first segment in 'extended' memory */
244 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000)) {
245 bios_extmem = smap.length;
249 * Look for the largest segment in 'extended' memory beyond
252 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
253 (smap.base < 0x100000000ull)) {
257 * If this segment crosses the 4GB boundary, truncate it.
259 if (smap.base + size > 0x100000000ull)
260 size = 0x100000000ull - smap.base;
262 if (size > high_heap_size) {
263 high_heap_size = size;
264 high_heap_base = smap.base;
267 } while (v86.ebx != 0);
269 /* Fall back to the old compatibility function for base memory */
270 if (bios_basemem == 0) {
272 v86.addr = 0x12; /* int 0x12 */
275 bios_basemem = (v86.eax & 0xffff) * 1024;
278 /* Fall back through several compatibility functions for extended memory */
279 if (bios_extmem == 0) {
281 v86.addr = 0x15; /* int 0x15 function 0xe801*/
284 if (!V86_CY(v86.efl)) {
285 bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
288 if (bios_extmem == 0) {
290 v86.addr = 0x15; /* int 0x15 function 0x88*/
293 bios_extmem = (v86.eax & 0xffff) * 1024;
297 * If we have extended memory and did not find a suitable heap
298 * region in the SMAP, use the last 3MB of 'extended' memory as a
299 * high heap candidate.
301 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
302 high_heap_size = HEAP_MIN;
303 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
308 * Try to detect a device supported by the legacy int13 BIOS
311 int13probe(int drive)
319 if (!V86_CY(v86.efl) && /* carry clear */
320 ((v86.edx & 0xff) != (drive & DRV_MASK))) { /* unit # OK */
321 if ((v86.ecx & 0x3f) == 0) { /* absurd sector size */
322 return(0); /* skip device */
330 * We call this when we find a ZFS vdev - ZFS consumes the dsk
331 * structure so we must make a new one.
334 copy_dsk(struct dsk *dsk)
338 newdsk = malloc(sizeof(struct dsk));
344 probe_drive(struct dsk *dsk)
350 unsigned part, entries_per_sec;
352 struct dos_partition *dp;
357 * If we find a vdev on the whole disk, stop here. Otherwise dig
358 * out the partition table and probe each slice/partition
359 * in turn for a vdev.
361 if (vdev_probe(vdev_read, dsk, NULL) == 0)
364 sec = dmadat->secbuf;
369 * First check for GPT.
371 if (drvread(dsk, sec, 1, 1)) {
374 memcpy(&hdr, sec, sizeof(hdr));
375 if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
376 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
377 hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
382 * Probe all GPT partitions for the presense of ZFS pools. We
383 * return the spa_t for the first we find (if requested). This
384 * will have the effect of booting from the first pool on the
387 entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
388 slba = hdr.hdr_lba_table;
389 elba = slba + hdr.hdr_entries / entries_per_sec;
390 while (slba < elba) {
392 if (drvread(dsk, sec, slba, 1))
394 for (part = 0; part < entries_per_sec; part++) {
395 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
396 if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
397 sizeof(uuid_t)) == 0) {
398 dsk->start = ent->ent_lba_start;
399 if (vdev_probe(vdev_read, dsk, NULL) == 0) {
401 * This slice had a vdev. We need a new dsk
402 * structure now since the vdev now owns this one.
414 if (drvread(dsk, sec, DOSBBSECTOR, 1))
416 dp = (void *)(sec + DOSPARTOFF);
418 for (i = 0; i < NDOSPART; i++) {
421 dsk->start = dp[i].dp_start;
422 if (vdev_probe(vdev_read, dsk, NULL) == 0) {
424 * This slice had a vdev. We need a new dsk structure now
425 * since the vdev now owns this one.
440 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
444 if (high_heap_size > 0) {
445 heap_end = PTOV(high_heap_base + high_heap_size);
446 heap_next = PTOV(high_heap_base);
448 heap_next = (char *) dmadat + sizeof(*dmadat);
449 heap_end = (char *) PTOV(bios_basemem);
452 dsk = malloc(sizeof(struct dsk));
453 dsk->drive = *(uint8_t *)PTOV(ARGS);
454 dsk->type = dsk->drive & DRV_HARD ? TYPE_AD : TYPE_FD;
455 dsk->unit = dsk->drive & DRV_MASK;
456 dsk->slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
461 bootinfo.bi_version = BOOTINFO_VERSION;
462 bootinfo.bi_size = sizeof(bootinfo);
463 bootinfo.bi_basemem = bios_basemem / 1024;
464 bootinfo.bi_extmem = bios_extmem / 1024;
465 bootinfo.bi_memsizes_valid++;
466 bootinfo.bi_bios_dev = dsk->drive;
468 bootdev = MAKEBOOTDEV(dev_maj[dsk->type],
469 dsk->slice, dsk->unit, dsk->part),
471 /* Process configuration file */
478 * Probe the boot drive first - we will try to boot from whatever
479 * pool we find on that drive.
484 * Probe the rest of the drives that the bios knows about. This
485 * will find any other available pools and it may fill in missing
486 * vdevs for the boot pool.
489 for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++)
491 for (i = 0; i < MAXBDDEV; i++)
494 if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
497 if (!int13probe(i | DRV_HARD))
500 dsk = malloc(sizeof(struct dsk));
501 dsk->drive = i | DRV_HARD;
502 dsk->type = dsk->drive & TYPE_AD;
512 * The first discovered pool, if any, is the pool.
514 spa = spa_get_primary();
516 printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
522 primary_vdev = spa_get_primary_vdev(spa);
524 if (zfs_spa_init(spa) != 0 || zfs_mount(spa, 0, &zfsmount) != 0) {
525 printf("%s: failed to mount default pool %s\n",
526 BOOTPROG, spa->spa_name);
528 } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 ||
529 zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) {
531 zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
536 * Note that parse() is destructive to cmd[] and we also want
537 * to honor RBX_QUIET option that could be present in cmd[].
539 memcpy(cmddup, cmd, sizeof(cmd));
542 if (!OPT_CHECK(RBX_QUIET))
543 printf("%s: %s\n", PATH_CONFIG, cmddup);
544 /* Do not process this command twice */
549 * Try to exec /boot/loader. If interrupted by a keypress,
550 * or in case of failure, try to load a kernel directly instead.
553 if (autoboot && !*kname) {
554 memcpy(kname, PATH_LOADER_ZFS, sizeof(PATH_LOADER_ZFS));
557 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
561 /* Present the user with the boot2 prompt. */
564 if (!autoboot || !OPT_CHECK(RBX_QUIET)) {
565 printf("\nFreeBSD/x86 boot\n");
566 if (zfs_rlookup(spa, zfsmount.rootobj, rootname) != 0)
567 printf("Default: %s/<0x%llx>:%s\n"
569 spa->spa_name, zfsmount.rootobj, kname);
570 else if (rootname[0] != '\0')
571 printf("Default: %s/%s:%s\n"
573 spa->spa_name, rootname, kname);
575 printf("Default: %s:%s\n"
577 spa->spa_name, kname);
579 if (ioctrl & IO_SERIAL)
581 if (!autoboot || keyhit(5))
582 getstr(cmd, sizeof(cmd));
583 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
593 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
606 static Elf32_Phdr ep[2];
607 static Elf32_Shdr es[2];
614 if (zfs_lookup(&zfsmount, kname, &dn)) {
615 printf("\nCan't find %s\n", kname);
619 if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
621 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
623 else if (IS_ELF(hdr.eh))
626 printf("Invalid %s\n", "format");
630 addr = hdr.ex.a_entry & 0xffffff;
633 if (xfsread(&dn, &off, p, hdr.ex.a_text))
635 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
636 if (xfsread(&dn, &off, p, hdr.ex.a_data))
638 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
639 bootinfo.bi_symtab = VTOP(p);
640 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
641 p += sizeof(hdr.ex.a_syms);
643 if (xfsread(&dn, &off, p, hdr.ex.a_syms))
646 if (xfsread(&dn, &off, p, sizeof(int)))
651 if (xfsread(&dn, &off, p, x))
656 off = hdr.eh.e_phoff;
657 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
658 if (xfsread(&dn, &off, ep + j, sizeof(ep[0])))
660 if (ep[j].p_type == PT_LOAD)
663 for (i = 0; i < 2; i++) {
664 p = PTOV(ep[i].p_paddr & 0xffffff);
665 off = ep[i].p_offset;
666 if (xfsread(&dn, &off, p, ep[i].p_filesz))
669 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
670 bootinfo.bi_symtab = VTOP(p);
671 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
672 off = hdr.eh.e_shoff + sizeof(es[0]) *
673 (hdr.eh.e_shstrndx + 1);
674 if (xfsread(&dn, &off, &es, sizeof(es)))
676 for (i = 0; i < 2; i++) {
677 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
678 p += sizeof(es[i].sh_size);
679 off = es[i].sh_offset;
680 if (xfsread(&dn, &off, p, es[i].sh_size))
685 addr = hdr.eh.e_entry & 0xffffff;
687 bootinfo.bi_esymtab = VTOP(p);
688 bootinfo.bi_kernelname = VTOP(kname);
689 zfsargs.size = sizeof(zfsargs);
690 zfsargs.pool = zfsmount.spa->spa_guid;
691 zfsargs.root = zfsmount.rootobj;
692 zfsargs.primary_pool = primary_spa->spa_guid;
693 if (primary_vdev != NULL)
694 zfsargs.primary_vdev = primary_vdev->v_guid;
696 printf("failed to detect primary vdev\n");
697 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
699 KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
700 (uint32_t) spa->spa_guid,
701 (uint32_t) (spa->spa_guid >> 32),
707 zfs_mount_ds(char *dsname)
713 q = strchr(dsname, '/');
716 newspa = spa_find_by_name(dsname);
717 if (newspa == NULL) {
718 printf("\nCan't find ZFS pool %s\n", dsname);
722 if (zfs_spa_init(newspa))
727 if (zfs_lookup_dataset(newspa, q, &newroot)) {
728 printf("\nCan't find dataset %s in ZFS pool %s\n",
729 q, newspa->spa_name);
733 if (zfs_mount(newspa, newroot, &zfsmount)) {
734 printf("\nCan't mount ZFS dataset\n");
749 while ((c = *arg++)) {
750 if (c == ' ' || c == '\t' || c == '\n')
752 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
757 while ((c = *arg++)) {
759 if (*(uint8_t *)PTOV(0x496) & 0x10) {
762 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
765 printf("Keyboard: %s\n", cp);
767 } else if (c == 'S') {
769 while ((unsigned int)(i = *arg++ - '0') <= 9)
771 if (j > 0 && i == -'0') {
775 /* Fall through to error below ('S' not in optstr[]). */
777 for (i = 0; c != optstr[i]; i++)
780 opts ^= OPT_SET(flags[i]);
782 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
783 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
784 if (ioctrl & IO_SERIAL) {
785 if (sio_init(115200 / comspeed) != 0)
786 ioctrl &= ~IO_SERIAL;
791 if (zfs_lookup(&zfsmount, arg, &dn) == 0) {
799 * Report pool status if the comment is 'status'. Lets
800 * hope no-one wants to load /status as a kernel.
802 if (!strcmp(arg, "status")) {
808 * If there is "zfs:" prefix simply ignore it.
810 if (strncmp(arg, "zfs:", 4) == 0)
814 * If there is a colon, switch pools.
816 q = strchr(arg, ':');
819 if (zfs_mount_ds(arg) != 0)
823 if ((i = ep - arg)) {
824 if ((size_t)i >= sizeof(kname))
826 memcpy(kname, arg, i + 1);