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;
120 static void load(void);
121 static int parse(void);
122 static void bios_getmem(void);
128 if (p + n > heap_end) {
129 printf("malloc failure\n");
139 strdup(const char *s)
141 char *p = malloc(strlen(s) + 1);
149 * Read from a dnode (which must be from a ZPL filesystem).
152 zfs_read(spa_t *spa, const dnode_phys_t *dnode, off_t *offp, void *start, size_t size)
154 const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus;
159 if (*offp + n > zp->zp_size)
160 n = zp->zp_size - *offp;
162 rc = dnode_read(spa, dnode, *offp, start, n);
174 static spa_t *primary_spa;
175 static vdev_t *primary_vdev;
178 * A wrapper for dskread that doesn't have to worry about whether the
179 * buffer pointer crosses a 64k boundary.
182 vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
187 struct dsk *dsk = (struct dsk *) priv;
189 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
193 lba = off / DEV_BSIZE;
196 nb = bytes / DEV_BSIZE;
197 if (nb > READ_BUF_SIZE / DEV_BSIZE)
198 nb = READ_BUF_SIZE / DEV_BSIZE;
199 if (drvread(dsk, dmadat->rdbuf, lba, nb))
201 memcpy(p, dmadat->rdbuf, nb * DEV_BSIZE);
204 bytes -= nb * DEV_BSIZE;
211 xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte)
213 if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) {
214 printf("Invalid format\n");
225 /* Parse system memory map */
229 v86.addr = 0x15; /* int 0x15 function 0xe820*/
231 v86.ecx = sizeof(struct bios_smap);
233 v86.es = VTOPSEG(&smap);
234 v86.edi = VTOPOFF(&smap);
236 if (V86_CY(v86.efl) || (v86.eax != SMAP_SIG))
238 /* look for a low-memory segment that's large enough */
239 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
240 (smap.length >= (512 * 1024)))
241 bios_basemem = smap.length;
242 /* look for the first segment in 'extended' memory */
243 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000)) {
244 bios_extmem = smap.length;
248 * Look for the largest segment in 'extended' memory beyond
251 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
252 (smap.base < 0x100000000ull)) {
256 * If this segment crosses the 4GB boundary, truncate it.
258 if (smap.base + size > 0x100000000ull)
259 size = 0x100000000ull - smap.base;
261 if (size > high_heap_size) {
262 high_heap_size = size;
263 high_heap_base = smap.base;
266 } while (v86.ebx != 0);
268 /* Fall back to the old compatibility function for base memory */
269 if (bios_basemem == 0) {
271 v86.addr = 0x12; /* int 0x12 */
274 bios_basemem = (v86.eax & 0xffff) * 1024;
277 /* Fall back through several compatibility functions for extended memory */
278 if (bios_extmem == 0) {
280 v86.addr = 0x15; /* int 0x15 function 0xe801*/
283 if (!V86_CY(v86.efl)) {
284 bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
287 if (bios_extmem == 0) {
289 v86.addr = 0x15; /* int 0x15 function 0x88*/
292 bios_extmem = (v86.eax & 0xffff) * 1024;
296 * If we have extended memory and did not find a suitable heap
297 * region in the SMAP, use the last 3MB of 'extended' memory as a
298 * high heap candidate.
300 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
301 high_heap_size = HEAP_MIN;
302 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
307 * Try to detect a device supported by the legacy int13 BIOS
310 int13probe(int drive)
318 if (!V86_CY(v86.efl) && /* carry clear */
319 ((v86.edx & 0xff) != (drive & DRV_MASK))) { /* unit # OK */
320 if ((v86.ecx & 0x3f) == 0) { /* absurd sector size */
321 return(0); /* skip device */
329 * We call this when we find a ZFS vdev - ZFS consumes the dsk
330 * structure so we must make a new one.
333 copy_dsk(struct dsk *dsk)
337 newdsk = malloc(sizeof(struct dsk));
343 probe_drive(struct dsk *dsk)
349 unsigned part, entries_per_sec;
351 struct dos_partition *dp;
356 * If we find a vdev on the whole disk, stop here. Otherwise dig
357 * out the partition table and probe each slice/partition
358 * in turn for a vdev.
360 if (vdev_probe(vdev_read, dsk, NULL) == 0)
363 sec = dmadat->secbuf;
368 * First check for GPT.
370 if (drvread(dsk, sec, 1, 1)) {
373 memcpy(&hdr, sec, sizeof(hdr));
374 if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
375 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
376 hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
381 * Probe all GPT partitions for the presense of ZFS pools. We
382 * return the spa_t for the first we find (if requested). This
383 * will have the effect of booting from the first pool on the
386 entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
387 slba = hdr.hdr_lba_table;
388 elba = slba + hdr.hdr_entries / entries_per_sec;
389 while (slba < elba) {
391 if (drvread(dsk, sec, slba, 1))
393 for (part = 0; part < entries_per_sec; part++) {
394 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
395 if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
396 sizeof(uuid_t)) == 0) {
397 dsk->start = ent->ent_lba_start;
398 if (vdev_probe(vdev_read, dsk, NULL) == 0) {
400 * This slice had a vdev. We need a new dsk
401 * structure now since the vdev now owns this one.
413 if (drvread(dsk, sec, DOSBBSECTOR, 1))
415 dp = (void *)(sec + DOSPARTOFF);
417 for (i = 0; i < NDOSPART; i++) {
420 dsk->start = dp[i].dp_start;
421 if (vdev_probe(vdev_read, dsk, NULL) == 0) {
423 * This slice had a vdev. We need a new dsk structure now
424 * since the vdev now owns this one.
439 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
443 if (high_heap_size > 0) {
444 heap_end = PTOV(high_heap_base + high_heap_size);
445 heap_next = PTOV(high_heap_base);
447 heap_next = (char *) dmadat + sizeof(*dmadat);
448 heap_end = (char *) PTOV(bios_basemem);
451 dsk = malloc(sizeof(struct dsk));
452 dsk->drive = *(uint8_t *)PTOV(ARGS);
453 dsk->type = dsk->drive & DRV_HARD ? TYPE_AD : TYPE_FD;
454 dsk->unit = dsk->drive & DRV_MASK;
455 dsk->slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
460 bootinfo.bi_version = BOOTINFO_VERSION;
461 bootinfo.bi_size = sizeof(bootinfo);
462 bootinfo.bi_basemem = bios_basemem / 1024;
463 bootinfo.bi_extmem = bios_extmem / 1024;
464 bootinfo.bi_memsizes_valid++;
465 bootinfo.bi_bios_dev = dsk->drive;
467 bootdev = MAKEBOOTDEV(dev_maj[dsk->type],
468 dsk->slice, dsk->unit, dsk->part);
470 /* Process configuration file */
477 * Probe the boot drive first - we will try to boot from whatever
478 * pool we find on that drive.
483 * Probe the rest of the drives that the bios knows about. This
484 * will find any other available pools and it may fill in missing
485 * vdevs for the boot pool.
488 for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++)
490 for (i = 0; i < MAXBDDEV; i++)
493 if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
496 if (!int13probe(i | DRV_HARD))
499 dsk = malloc(sizeof(struct dsk));
500 dsk->drive = i | DRV_HARD;
501 dsk->type = dsk->drive & TYPE_AD;
511 * The first discovered pool, if any, is the pool.
513 spa = spa_get_primary();
515 printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
521 primary_vdev = spa_get_primary_vdev(spa);
523 if (zfs_spa_init(spa) != 0 || zfs_mount(spa, 0, &zfsmount) != 0) {
524 printf("%s: failed to mount default pool %s\n",
525 BOOTPROG, spa->spa_name);
527 } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 ||
528 zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) {
530 zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
535 * Note that parse() is destructive to cmd[] and we also want
536 * to honor RBX_QUIET option that could be present in cmd[].
538 memcpy(cmddup, cmd, sizeof(cmd));
541 if (!OPT_CHECK(RBX_QUIET))
542 printf("%s: %s\n", PATH_CONFIG, cmddup);
543 /* Do not process this command twice */
548 * Try to exec /boot/loader. If interrupted by a keypress,
549 * or in case of failure, try to load a kernel directly instead.
552 if (autoboot && !*kname) {
553 memcpy(kname, PATH_LOADER_ZFS, sizeof(PATH_LOADER_ZFS));
556 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
560 /* Present the user with the boot2 prompt. */
563 if (!autoboot || !OPT_CHECK(RBX_QUIET)) {
564 printf("\nFreeBSD/x86 boot\n");
565 if (zfs_rlookup(spa, zfsmount.rootobj, rootname) != 0)
566 printf("Default: %s/<0x%llx>:%s\n"
568 spa->spa_name, zfsmount.rootobj, kname);
569 else if (rootname[0] != '\0')
570 printf("Default: %s/%s:%s\n"
572 spa->spa_name, rootname, kname);
574 printf("Default: %s:%s\n"
576 spa->spa_name, kname);
578 if (ioctrl & IO_SERIAL)
580 if (!autoboot || keyhit(5))
581 getstr(cmd, sizeof(cmd));
582 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
592 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
605 static Elf32_Phdr ep[2];
606 static Elf32_Shdr es[2];
613 if (zfs_lookup(&zfsmount, kname, &dn)) {
614 printf("\nCan't find %s\n", kname);
618 if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
620 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
622 else if (IS_ELF(hdr.eh))
625 printf("Invalid %s\n", "format");
629 addr = hdr.ex.a_entry & 0xffffff;
632 if (xfsread(&dn, &off, p, hdr.ex.a_text))
634 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
635 if (xfsread(&dn, &off, p, hdr.ex.a_data))
637 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
638 bootinfo.bi_symtab = VTOP(p);
639 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
640 p += sizeof(hdr.ex.a_syms);
642 if (xfsread(&dn, &off, p, hdr.ex.a_syms))
645 if (xfsread(&dn, &off, p, sizeof(int)))
650 if (xfsread(&dn, &off, p, x))
655 off = hdr.eh.e_phoff;
656 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
657 if (xfsread(&dn, &off, ep + j, sizeof(ep[0])))
659 if (ep[j].p_type == PT_LOAD)
662 for (i = 0; i < 2; i++) {
663 p = PTOV(ep[i].p_paddr & 0xffffff);
664 off = ep[i].p_offset;
665 if (xfsread(&dn, &off, p, ep[i].p_filesz))
668 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
669 bootinfo.bi_symtab = VTOP(p);
670 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
671 off = hdr.eh.e_shoff + sizeof(es[0]) *
672 (hdr.eh.e_shstrndx + 1);
673 if (xfsread(&dn, &off, &es, sizeof(es)))
675 for (i = 0; i < 2; i++) {
676 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
677 p += sizeof(es[i].sh_size);
678 off = es[i].sh_offset;
679 if (xfsread(&dn, &off, p, es[i].sh_size))
684 addr = hdr.eh.e_entry & 0xffffff;
686 bootinfo.bi_esymtab = VTOP(p);
687 bootinfo.bi_kernelname = VTOP(kname);
688 zfsargs.size = sizeof(zfsargs);
689 zfsargs.pool = zfsmount.spa->spa_guid;
690 zfsargs.root = zfsmount.rootobj;
691 zfsargs.primary_pool = primary_spa->spa_guid;
692 if (primary_vdev != NULL)
693 zfsargs.primary_vdev = primary_vdev->v_guid;
695 printf("failed to detect primary vdev\n");
696 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
698 KARGS_FLAGS_ZFS | KARGS_FLAGS_EXTARG,
699 (uint32_t) spa->spa_guid,
700 (uint32_t) (spa->spa_guid >> 32),
706 zfs_mount_ds(char *dsname)
712 q = strchr(dsname, '/');
715 newspa = spa_find_by_name(dsname);
716 if (newspa == NULL) {
717 printf("\nCan't find ZFS pool %s\n", dsname);
721 if (zfs_spa_init(newspa))
726 if (zfs_lookup_dataset(newspa, q, &newroot)) {
727 printf("\nCan't find dataset %s in ZFS pool %s\n",
728 q, newspa->spa_name);
732 if (zfs_mount(newspa, newroot, &zfsmount)) {
733 printf("\nCan't mount ZFS dataset\n");
748 while ((c = *arg++)) {
749 if (c == ' ' || c == '\t' || c == '\n')
751 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
756 while ((c = *arg++)) {
758 if (*(uint8_t *)PTOV(0x496) & 0x10) {
761 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
764 printf("Keyboard: %s\n", cp);
766 } else if (c == 'S') {
768 while ((unsigned int)(i = *arg++ - '0') <= 9)
770 if (j > 0 && i == -'0') {
774 /* Fall through to error below ('S' not in optstr[]). */
776 for (i = 0; c != optstr[i]; i++)
779 opts ^= OPT_SET(flags[i]);
781 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
782 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
783 if (ioctrl & IO_SERIAL) {
784 if (sio_init(115200 / comspeed) != 0)
785 ioctrl &= ~IO_SERIAL;
790 if (zfs_lookup(&zfsmount, arg, &dn) == 0) {
798 * Report pool status if the comment is 'status'. Lets
799 * hope no-one wants to load /status as a kernel.
801 if (!strcmp(arg, "status")) {
807 * If there is "zfs:" prefix simply ignore it.
809 if (strncmp(arg, "zfs:", 4) == 0)
813 * If there is a colon, switch pools.
815 q = strchr(arg, ':');
818 if (zfs_mount_ds(arg) != 0)
822 if ((i = ep - arg)) {
823 if ((size_t)i >= sizeof(kname))
825 memcpy(kname, arg, i + 1);