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>
48 #define PATH_DOTCONFIG "/boot.config"
49 #define PATH_CONFIG "/boot/config"
50 #define PATH_BOOT3 "/boot/zfsloader"
51 #define PATH_KERNEL "/boot/kernel/kernel"
57 #define BIOS_NUMDRIVES 0x475
63 #define TYPE_MAXHARD TYPE_DA
69 static const uuid_t freebsd_zfs_uuid = GPT_ENT_TYPE_FREEBSD_ZFS;
71 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
72 static const unsigned char flags[NOPT] = {
90 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
91 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
94 static char cmddup[512];
95 static char kname[1024];
96 static char rootname[256];
97 static int comspeed = SIOSPD;
98 static struct bootinfo bootinfo;
99 static uint32_t bootdev;
100 static struct zfs_boot_args zfsargs;
101 static struct zfsmount zfsmount;
103 vm_offset_t high_heap_base;
104 uint32_t bios_basemem, bios_extmem, high_heap_size;
106 static struct bios_smap smap;
109 * The minimum amount of memory to reserve in bios_extmem for the heap.
111 #define HEAP_MIN (3 * 1024 * 1024)
113 static char *heap_next;
114 static char *heap_end;
116 /* Buffers that must not span a 64k boundary. */
117 #define READ_BUF_SIZE 8192
119 char rdbuf[READ_BUF_SIZE]; /* for reading large things */
120 char secbuf[READ_BUF_SIZE]; /* for MBR/disklabel */
122 static struct dmadat *dmadat;
125 static void load(void);
126 static int parse(void);
127 static void bios_getmem(void);
133 if (p + n > heap_end) {
134 printf("malloc failure\n");
144 strdup(const char *s)
146 char *p = malloc(strlen(s) + 1);
154 * Read from a dnode (which must be from a ZPL filesystem).
157 zfs_read(spa_t *spa, const dnode_phys_t *dnode, off_t *offp, void *start, size_t size)
159 const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus;
164 if (*offp + n > zp->zp_size)
165 n = zp->zp_size - *offp;
167 rc = dnode_read(spa, dnode, *offp, start, n);
181 * A wrapper for dskread that doesn't have to worry about whether the
182 * buffer pointer crosses a 64k boundary.
185 vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
190 struct dsk *dsk = (struct dsk *) priv;
192 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
196 lba = off / DEV_BSIZE;
199 nb = bytes / DEV_BSIZE;
200 if (nb > READ_BUF_SIZE / DEV_BSIZE)
201 nb = READ_BUF_SIZE / DEV_BSIZE;
202 if (drvread(dsk, dmadat->rdbuf, lba, nb))
204 memcpy(p, dmadat->rdbuf, nb * DEV_BSIZE);
207 bytes -= nb * DEV_BSIZE;
214 xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte)
216 if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) {
217 printf("Invalid format\n");
228 /* Parse system memory map */
232 v86.addr = 0x15; /* int 0x15 function 0xe820*/
234 v86.ecx = sizeof(struct bios_smap);
236 v86.es = VTOPSEG(&smap);
237 v86.edi = VTOPOFF(&smap);
239 if ((v86.efl & 1) || (v86.eax != SMAP_SIG))
241 /* look for a low-memory segment that's large enough */
242 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
243 (smap.length >= (512 * 1024)))
244 bios_basemem = smap.length;
245 /* look for the first segment in 'extended' memory */
246 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000)) {
247 bios_extmem = smap.length;
251 * Look for the largest segment in 'extended' memory beyond
254 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
255 (smap.base < 0x100000000ull)) {
259 * If this segment crosses the 4GB boundary, truncate it.
261 if (smap.base + size > 0x100000000ull)
262 size = 0x100000000ull - smap.base;
264 if (size > high_heap_size) {
265 high_heap_size = size;
266 high_heap_base = smap.base;
269 } while (v86.ebx != 0);
271 /* Fall back to the old compatibility function for base memory */
272 if (bios_basemem == 0) {
274 v86.addr = 0x12; /* int 0x12 */
277 bios_basemem = (v86.eax & 0xffff) * 1024;
280 /* Fall back through several compatibility functions for extended memory */
281 if (bios_extmem == 0) {
283 v86.addr = 0x15; /* int 0x15 function 0xe801*/
286 if (!(v86.efl & 1)) {
287 bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
290 if (bios_extmem == 0) {
292 v86.addr = 0x15; /* int 0x15 function 0x88*/
295 bios_extmem = (v86.eax & 0xffff) * 1024;
299 * If we have extended memory and did not find a suitable heap
300 * region in the SMAP, use the last 3MB of 'extended' memory as a
301 * high heap candidate.
303 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
304 high_heap_size = HEAP_MIN;
305 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
310 * Try to detect a device supported by the legacy int13 BIOS
313 int13probe(int drive)
321 if (!(v86.efl & 0x1) && /* carry clear */
322 ((v86.edx & 0xff) != (drive & DRV_MASK))) { /* unit # OK */
323 if ((v86.ecx & 0x3f) == 0) { /* absurd sector size */
324 return(0); /* skip device */
332 * We call this when we find a ZFS vdev - ZFS consumes the dsk
333 * structure so we must make a new one.
336 copy_dsk(struct dsk *dsk)
340 newdsk = malloc(sizeof(struct dsk));
346 probe_drive(struct dsk *dsk, spa_t **spap)
352 unsigned part, entries_per_sec;
354 struct dos_partition *dp;
359 * If we find a vdev on the whole disk, stop here. Otherwise dig
360 * out the MBR and probe each slice in turn for a vdev.
362 if (vdev_probe(vdev_read, dsk, spap) == 0)
365 sec = dmadat->secbuf;
370 * First check for GPT.
372 if (drvread(dsk, sec, 1, 1)) {
375 memcpy(&hdr, sec, sizeof(hdr));
376 if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
377 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
378 hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
383 * Probe all GPT partitions for the presense of ZFS pools. We
384 * return the spa_t for the first we find (if requested). This
385 * will have the effect of booting from the first pool on the
388 entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
389 slba = hdr.hdr_lba_table;
390 elba = slba + hdr.hdr_entries / entries_per_sec;
391 while (slba < elba) {
393 if (drvread(dsk, sec, slba, 1))
395 for (part = 0; part < entries_per_sec; part++) {
396 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
397 if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
398 sizeof(uuid_t)) == 0) {
399 dsk->start = ent->ent_lba_start;
400 if (vdev_probe(vdev_read, dsk, spap) == 0) {
402 * We record the first pool we find (we will try
403 * to boot from that one).
408 * This slice had a vdev. We need a new dsk
409 * structure now since the vdev now owns this one.
421 if (drvread(dsk, sec, DOSBBSECTOR, 1))
423 dp = (void *)(sec + DOSPARTOFF);
425 for (i = 0; i < NDOSPART; i++) {
428 dsk->start = dp[i].dp_start;
429 if (vdev_probe(vdev_read, dsk, spap) == 0) {
431 * We record the first pool we find (we will try to boot
437 * This slice had a vdev. We need a new dsk structure now
438 * since the vdev now owns this one.
453 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
457 if (high_heap_size > 0) {
458 heap_end = PTOV(high_heap_base + high_heap_size);
459 heap_next = PTOV(high_heap_base);
461 heap_next = (char *) dmadat + sizeof(*dmadat);
462 heap_end = (char *) PTOV(bios_basemem);
465 dsk = malloc(sizeof(struct dsk));
466 dsk->drive = *(uint8_t *)PTOV(ARGS);
467 dsk->type = dsk->drive & DRV_HARD ? TYPE_AD : TYPE_FD;
468 dsk->unit = dsk->drive & DRV_MASK;
469 dsk->slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
474 bootinfo.bi_version = BOOTINFO_VERSION;
475 bootinfo.bi_size = sizeof(bootinfo);
476 bootinfo.bi_basemem = bios_basemem / 1024;
477 bootinfo.bi_extmem = bios_extmem / 1024;
478 bootinfo.bi_memsizes_valid++;
479 bootinfo.bi_bios_dev = dsk->drive;
481 bootdev = MAKEBOOTDEV(dev_maj[dsk->type],
482 dsk->slice, dsk->unit, dsk->part),
484 /* Process configuration file */
491 * Probe the boot drive first - we will try to boot from whatever
492 * pool we find on that drive.
494 probe_drive(dsk, &spa);
497 * Probe the rest of the drives that the bios knows about. This
498 * will find any other available pools and it may fill in missing
499 * vdevs for the boot pool.
501 for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++) {
502 if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
505 if (!int13probe(i | DRV_HARD))
508 dsk = malloc(sizeof(struct dsk));
509 dsk->drive = i | DRV_HARD;
510 dsk->type = dsk->drive & TYPE_AD;
516 probe_drive(dsk, NULL);
520 * If we didn't find a pool on the boot drive, default to the
521 * first pool we found, if any.
524 spa = STAILQ_FIRST(&zfs_pools);
526 printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
532 if (zfs_spa_init(spa) != 0 || zfs_mount(spa, 0, &zfsmount) != 0) {
533 printf("%s: failed to mount default pool %s\n",
534 BOOTPROG, spa->spa_name);
536 } else if (zfs_lookup(&zfsmount, PATH_CONFIG, &dn) == 0 ||
537 zfs_lookup(&zfsmount, PATH_DOTCONFIG, &dn) == 0) {
539 zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
544 * Note that parse() is destructive to cmd[] and we also want
545 * to honor RBX_QUIET option that could be present in cmd[].
547 memcpy(cmddup, cmd, sizeof(cmd));
550 if (!OPT_CHECK(RBX_QUIET))
551 printf("%s: %s", PATH_CONFIG, cmddup);
552 /* Do not process this command twice */
557 * Try to exec stage 3 boot loader. If interrupted by a keypress,
558 * or in case of failure, try to load a kernel directly instead.
561 if (autoboot && !*kname) {
562 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
565 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
569 /* Present the user with the boot2 prompt. */
572 if (!autoboot || !OPT_CHECK(RBX_QUIET)) {
573 printf("\nFreeBSD/x86 boot\n");
574 if (zfs_rlookup(spa, zfsmount.rootobj, rootname) != 0)
575 printf("Default: %s:<0x%llx>:%s\n"
577 spa->spa_name, zfsmount.rootobj, kname);
579 printf("Default: %s:%s:%s\n"
581 spa->spa_name, rootname, kname);
583 if (ioctrl & IO_SERIAL)
585 if (!autoboot || keyhit(5))
586 getstr(cmd, sizeof(cmd));
587 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
597 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
610 static Elf32_Phdr ep[2];
611 static Elf32_Shdr es[2];
618 if (zfs_lookup(&zfsmount, kname, &dn)) {
619 printf("\nCan't find %s\n", kname);
623 if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
625 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
627 else if (IS_ELF(hdr.eh))
630 printf("Invalid %s\n", "format");
634 addr = hdr.ex.a_entry & 0xffffff;
637 if (xfsread(&dn, &off, p, hdr.ex.a_text))
639 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
640 if (xfsread(&dn, &off, p, hdr.ex.a_data))
642 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
643 bootinfo.bi_symtab = VTOP(p);
644 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
645 p += sizeof(hdr.ex.a_syms);
647 if (xfsread(&dn, &off, p, hdr.ex.a_syms))
650 if (xfsread(&dn, &off, p, sizeof(int)))
655 if (xfsread(&dn, &off, p, x))
660 off = hdr.eh.e_phoff;
661 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
662 if (xfsread(&dn, &off, ep + j, sizeof(ep[0])))
664 if (ep[j].p_type == PT_LOAD)
667 for (i = 0; i < 2; i++) {
668 p = PTOV(ep[i].p_paddr & 0xffffff);
669 off = ep[i].p_offset;
670 if (xfsread(&dn, &off, p, ep[i].p_filesz))
673 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
674 bootinfo.bi_symtab = VTOP(p);
675 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
676 off = hdr.eh.e_shoff + sizeof(es[0]) *
677 (hdr.eh.e_shstrndx + 1);
678 if (xfsread(&dn, &off, &es, sizeof(es)))
680 for (i = 0; i < 2; i++) {
681 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
682 p += sizeof(es[i].sh_size);
683 off = es[i].sh_offset;
684 if (xfsread(&dn, &off, p, es[i].sh_size))
689 addr = hdr.eh.e_entry & 0xffffff;
691 bootinfo.bi_esymtab = VTOP(p);
692 bootinfo.bi_kernelname = VTOP(kname);
693 zfsargs.size = sizeof(zfsargs);
694 zfsargs.pool = zfsmount.spa->spa_guid;
695 zfsargs.root = zfsmount.rootobj;
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),
714 while ((c = *arg++)) {
715 if (c == ' ' || c == '\t' || c == '\n')
717 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
722 while ((c = *arg++)) {
724 if (*(uint8_t *)PTOV(0x496) & 0x10) {
727 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
730 printf("Keyboard: %s\n", cp);
732 } else if (c == 'S') {
734 while ((unsigned int)(i = *arg++ - '0') <= 9)
736 if (j > 0 && i == -'0') {
740 /* Fall through to error below ('S' not in optstr[]). */
742 for (i = 0; c != optstr[i]; i++)
745 opts ^= OPT_SET(flags[i]);
747 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
748 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
749 if (ioctrl & IO_SERIAL)
750 sio_init(115200 / comspeed);
754 if (zfs_lookup(&zfsmount, arg, &dn) == 0) {
762 * Report pool status if the comment is 'status'. Lets
763 * hope no-one wants to load /status as a kernel.
765 if (!strcmp(arg, "status")) {
771 * If there is a colon, switch pools.
773 q = (char *) strchr(arg, ':');
779 newspa = spa_find_by_name(arg);
784 q = (char *) strchr(arg, ':');
787 if (zfs_lookup_dataset(spa, arg, &newroot)) {
788 printf("\nCan't find dataset %s in ZFS pool %s\n",
794 if (zfs_mount(spa, newroot, &zfsmount)) {
795 printf("\nCan't mount ZFS dataset\n");
799 printf("\nCan't find ZFS pool %s\n", arg);
803 if ((i = ep - arg)) {
804 if ((size_t)i >= sizeof(kname))
806 memcpy(kname, arg, i + 1);