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>
46 #define SECOND 18 /* Circa that many ticks in a second. */
48 #define RBX_ASKNAME 0x0 /* -a */
49 #define RBX_SINGLE 0x1 /* -s */
50 /* 0x2 is reserved for log2(RB_NOSYNC). */
51 /* 0x3 is reserved for log2(RB_HALT). */
52 /* 0x4 is reserved for log2(RB_INITNAME). */
53 #define RBX_DFLTROOT 0x5 /* -r */
54 #define RBX_KDB 0x6 /* -d */
55 /* 0x7 is reserved for log2(RB_RDONLY). */
56 /* 0x8 is reserved for log2(RB_DUMP). */
57 /* 0x9 is reserved for log2(RB_MINIROOT). */
58 #define RBX_CONFIG 0xa /* -c */
59 #define RBX_VERBOSE 0xb /* -v */
60 #define RBX_SERIAL 0xc /* -h */
61 #define RBX_CDROM 0xd /* -C */
62 /* 0xe is reserved for log2(RB_POWEROFF). */
63 #define RBX_GDB 0xf /* -g */
64 #define RBX_MUTE 0x10 /* -m */
65 /* 0x11 is reserved for log2(RB_SELFTEST). */
66 /* 0x12 is reserved for boot programs. */
67 /* 0x13 is reserved for boot programs. */
68 #define RBX_PAUSE 0x14 /* -p */
69 #define RBX_QUIET 0x15 /* -q */
70 #define RBX_NOINTR 0x1c /* -n */
71 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
72 #define RBX_DUAL 0x1d /* -D */
73 /* 0x1f is reserved for log2(RB_BOOTINFO). */
75 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -D */
76 #define RBX_MASK (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
77 OPT_SET(RBX_DFLTROOT) | OPT_SET(RBX_KDB ) | \
78 OPT_SET(RBX_CONFIG) | OPT_SET(RBX_VERBOSE) | \
79 OPT_SET(RBX_SERIAL) | OPT_SET(RBX_CDROM) | \
80 OPT_SET(RBX_GDB ) | OPT_SET(RBX_MUTE) | \
81 OPT_SET(RBX_PAUSE) | OPT_SET(RBX_DUAL))
83 /* Hint to loader that we came from ZFS */
84 #define KARGS_FLAGS_ZFS 0x4
86 #define PATH_CONFIG "/boot.config"
87 #define PATH_BOOT3 "/boot/loader"
88 #define PATH_KERNEL "/boot/kernel/kernel"
95 #define V86_CY(x) ((x) & 1)
96 #define V86_ZR(x) ((x) & 0x40)
103 #define TYPE_MAXHARD TYPE_DA
106 #define OPT_SET(opt) (1 << (opt))
107 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
109 extern uint32_t _end;
112 static const uuid_t freebsd_zfs_uuid = GPT_ENT_TYPE_FREEBSD_ZFS;
114 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
115 static const unsigned char flags[NOPT] = {
132 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
133 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
144 static char cmd[512];
145 static char kname[1024];
146 static uint32_t opts;
147 static int comspeed = SIOSPD;
148 static struct bootinfo bootinfo;
149 static uint32_t bootdev;
150 static uint8_t ioctrl = IO_KEYBOARD;
152 /* Buffers that must not span a 64k boundary. */
153 #define READ_BUF_SIZE 8192
155 char rdbuf[READ_BUF_SIZE]; /* for reading large things */
156 char secbuf[READ_BUF_SIZE]; /* for MBR/disklabel */
158 static struct dmadat *dmadat;
161 static void load(void);
162 static int parse(void);
163 static void printf(const char *,...);
164 static void putchar(int);
165 static uint32_t memsize(void);
166 static int drvread(struct dsk *, void *, unsigned, unsigned);
167 static int keyhit(unsigned);
168 static int xputc(int);
169 static int xgetc(int);
170 static int getc(int);
172 static void memcpy(void *, const void *, int);
174 memcpy(void *dst, const void *src, int len)
184 strcpy(char *dst, const char *src)
192 strcat(char *dst, const char *src)
202 strcmp(const char *s1, const char *s2)
204 for (; *s1 == *s2 && *s1; s1++, s2++);
205 return (unsigned char)*s1 - (unsigned char)*s2;
209 strchr(const char *s, char ch)
218 memcmp(const void *p1, const void *p2, size_t n)
220 const char *s1 = (const char *) p1;
221 const char *s2 = (const char *) p2;
222 for (; n > 0 && *s1 == *s2; s1++, s2++, n--);
224 return (unsigned char)*s1 - (unsigned char)*s2;
230 memset(void *p, char val, size_t n)
232 char *s = (char *) p;
240 static char *heap_next;
241 static char *heap_end;
244 heap_next = (char *) dmadat + sizeof(*dmadat);
245 heap_end = (char *) (640*1024);
249 if (p + n > heap_end) {
250 printf("malloc failure\n");
260 strlen(const char *s)
269 strdup(const char *s)
271 char *p = malloc(strlen(s) + 1);
279 * Read from a dnode (which must be from a ZPL filesystem).
282 zfs_read(spa_t *spa, const dnode_phys_t *dnode, off_t *offp, void *start, size_t size)
284 const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus;
289 if (*offp + n > zp->zp_size)
290 n = zp->zp_size - *offp;
292 rc = dnode_read(spa, dnode, *offp, start, n);
306 * A wrapper for dskread that doesn't have to worry about whether the
307 * buffer pointer crosses a 64k boundary.
310 vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
313 unsigned int lba, nb;
314 struct dsk *dsk = (struct dsk *) priv;
316 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
320 lba = off / DEV_BSIZE;
322 nb = bytes / DEV_BSIZE;
323 if (nb > READ_BUF_SIZE / DEV_BSIZE)
324 nb = READ_BUF_SIZE / DEV_BSIZE;
325 if (drvread(dsk, dmadat->rdbuf, lba, nb))
327 memcpy(p, dmadat->rdbuf, nb * DEV_BSIZE);
330 bytes -= nb * DEV_BSIZE;
337 xfsread(const dnode_phys_t *dnode, off_t *offp, void *buf, size_t nbyte)
339 if ((size_t)zfs_read(spa, dnode, offp, buf, nbyte) != nbyte) {
340 printf("Invalid %s\n", "format");
346 static inline uint32_t
363 switch (c = xgetc(0)) {
378 if (s - cmd < sizeof(cmd) - 1)
389 v86.eax = 0xe00 | (c & 0xff);
395 * Try to detect a device supported by the legacy int13 BIOS
398 int13probe(int drive)
406 if (!(v86.efl & 0x1) && /* carry clear */
407 ((v86.edx & 0xff) != (drive & DRV_MASK))) { /* unit # OK */
408 if ((v86.ecx & 0x3f) == 0) { /* absurd sector size */
409 return(0); /* skip device */
417 * We call this when we find a ZFS vdev - ZFS consumes the dsk
418 * structure so we must make a new one.
421 copy_dsk(struct dsk *dsk)
425 newdsk = malloc(sizeof(struct dsk));
431 probe_drive(struct dsk *dsk, spa_t **spap)
437 unsigned part, entries_per_sec;
439 struct dos_partition *dp;
444 * If we find a vdev on the whole disk, stop here. Otherwise dig
445 * out the MBR and probe each slice in turn for a vdev.
447 if (vdev_probe(vdev_read, dsk, spap) == 0)
450 sec = dmadat->secbuf;
455 * First check for GPT.
457 if (drvread(dsk, sec, 1, 1)) {
460 memcpy(&hdr, sec, sizeof(hdr));
461 if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
462 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
463 hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
468 * Probe all GPT partitions for the presense of ZFS pools. We
469 * return the spa_t for the first we find (if requested). This
470 * will have the effect of booting from the first pool on the
473 entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
474 slba = hdr.hdr_lba_table;
475 elba = slba + hdr.hdr_entries / entries_per_sec;
476 while (slba < elba) {
477 if (drvread(dsk, sec, slba, 1))
479 for (part = 0; part < entries_per_sec; part++) {
480 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
481 if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
482 sizeof(uuid_t)) == 0) {
483 dsk->start = ent->ent_lba_start;
484 if (vdev_probe(vdev_read, dsk, spap) == 0) {
486 * We record the first pool we find (we will try
487 * to boot from that one).
492 * This slice had a vdev. We need a new dsk
493 * structure now since the vdev now owns this one.
506 if (drvread(dsk, sec, DOSBBSECTOR, 1))
508 dp = (void *)(sec + DOSPARTOFF);
510 for (i = 0; i < NDOSPART; i++) {
513 dsk->start = dp[i].dp_start;
514 if (vdev_probe(vdev_read, dsk, spap) == 0) {
516 * We record the first pool we find (we will try to boot
522 * This slice had a vdev. We need a new dsk structure now
523 * since the vdev now owns this one.
538 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
541 dsk = malloc(sizeof(struct dsk));
542 dsk->drive = *(uint8_t *)PTOV(ARGS);
543 dsk->type = dsk->drive & DRV_HARD ? TYPE_AD : TYPE_FD;
544 dsk->unit = dsk->drive & DRV_MASK;
545 dsk->slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
550 bootinfo.bi_version = BOOTINFO_VERSION;
551 bootinfo.bi_size = sizeof(bootinfo);
552 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
553 bootinfo.bi_extmem = memsize();
554 bootinfo.bi_memsizes_valid++;
555 bootinfo.bi_bios_dev = dsk->drive;
557 bootdev = MAKEBOOTDEV(dev_maj[dsk->type],
558 dsk->slice, dsk->unit, dsk->part),
560 /* Process configuration file */
567 * Probe the boot drive first - we will try to boot from whatever
568 * pool we find on that drive.
570 probe_drive(dsk, &spa);
573 * Probe the rest of the drives that the bios knows about. This
574 * will find any other available pools and it may fill in missing
575 * vdevs for the boot pool.
577 for (i = 0; i < 128; i++) {
578 if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
581 if (!int13probe(i | DRV_HARD))
584 dsk = malloc(sizeof(struct dsk));
585 dsk->drive = i | DRV_HARD;
586 dsk->type = dsk->drive & TYPE_AD;
596 * If we didn't find a pool on the boot drive, default to the
597 * first pool we found, if any.
600 spa = STAILQ_FIRST(&zfs_pools);
602 printf("No ZFS pools located, can't boot\n");
610 if (zfs_lookup(spa, PATH_CONFIG, &dn) == 0) {
612 zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
618 if (!OPT_CHECK(RBX_QUIET))
619 printf("%s: %s", PATH_CONFIG, cmd);
620 /* Do not process this command twice */
625 * Try to exec stage 3 boot loader. If interrupted by a keypress,
626 * or in case of failure, try to load a kernel directly instead.
629 if (autoboot && !*kname) {
630 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
631 if (!keyhit(3*SECOND)) {
633 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
637 /* Present the user with the boot2 prompt. */
640 if (!autoboot || !OPT_CHECK(RBX_QUIET))
641 printf("\nFreeBSD/i386 boot\n"
644 spa->spa_name, kname);
645 if (ioctrl & IO_SERIAL)
647 if (!autoboot || keyhit(5*SECOND))
649 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
659 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
672 static Elf32_Phdr ep[2];
673 static Elf32_Shdr es[2];
680 if (zfs_lookup(spa, kname, &dn)) {
684 if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
686 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
688 else if (IS_ELF(hdr.eh))
691 printf("Invalid %s\n", "format");
695 addr = hdr.ex.a_entry & 0xffffff;
698 if (xfsread(&dn, &off, p, hdr.ex.a_text))
700 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
701 if (xfsread(&dn, &off, p, hdr.ex.a_data))
703 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
704 bootinfo.bi_symtab = VTOP(p);
705 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
706 p += sizeof(hdr.ex.a_syms);
708 if (xfsread(&dn, &off, p, hdr.ex.a_syms))
711 if (xfsread(&dn, &off, p, sizeof(int)))
716 if (xfsread(&dn, &off, p, x))
721 off = hdr.eh.e_phoff;
722 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
723 if (xfsread(&dn, &off, ep + j, sizeof(ep[0])))
725 if (ep[j].p_type == PT_LOAD)
728 for (i = 0; i < 2; i++) {
729 p = PTOV(ep[i].p_paddr & 0xffffff);
730 off = ep[i].p_offset;
731 if (xfsread(&dn, &off, p, ep[i].p_filesz))
734 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
735 bootinfo.bi_symtab = VTOP(p);
736 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
737 off = hdr.eh.e_shoff + sizeof(es[0]) *
738 (hdr.eh.e_shstrndx + 1);
739 if (xfsread(&dn, &off, &es, sizeof(es)))
741 for (i = 0; i < 2; i++) {
742 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
743 p += sizeof(es[i].sh_size);
744 off = es[i].sh_offset;
745 if (xfsread(&dn, &off, p, es[i].sh_size))
750 addr = hdr.eh.e_entry & 0xffffff;
752 bootinfo.bi_esymtab = VTOP(p);
753 bootinfo.bi_kernelname = VTOP(kname);
754 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
757 (uint32_t) spa->spa_guid,
758 (uint32_t) (spa->spa_guid >> 32),
771 while ((c = *arg++)) {
772 if (c == ' ' || c == '\t' || c == '\n')
774 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
779 while ((c = *arg++)) {
781 if (*(uint8_t *)PTOV(0x496) & 0x10) {
784 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
787 printf("Keyboard: %s\n", cp);
789 } else if (c == 'S') {
791 while ((unsigned int)(i = *arg++ - '0') <= 9)
793 if (j > 0 && i == -'0') {
797 /* Fall through to error below ('S' not in optstr[]). */
799 for (i = 0; c != optstr[i]; i++)
802 opts ^= OPT_SET(flags[i]);
804 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
805 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
806 if (ioctrl & IO_SERIAL)
807 sio_init(115200 / comspeed);
811 if (zfs_lookup(spa, arg, &dn) == 0) {
819 * Report pool status if the comment is 'status'. Lets
820 * hope no-one wants to load /status as a kernel.
822 if (!strcmp(arg, "status")) {
828 * If there is a colon, switch pools.
830 q = (char *) strchr(arg, ':');
835 newspa = spa_find_by_name(arg);
840 printf("\nCan't find ZFS pool %s\n", arg);
845 if ((i = ep - arg)) {
846 if ((size_t)i >= sizeof(kname))
848 memcpy(kname, arg, i + 1);
857 printf(const char *fmt,...)
870 while ((c = *fmt++)) {
890 prec = 10 * prec + (c - '0');
893 putchar(va_arg(ap, int));
896 s = va_arg(ap, char *);
917 u = va_arg(ap, unsigned);
920 *s++ = '0' + u % 10U;
952 drvread(struct dsk *dsk, void *buf, unsigned lba, unsigned nblk)
955 static unsigned c = 0x2d5c7c2f;
957 if (!OPT_CHECK(RBX_QUIET))
958 printf("%c\b", c = c << 8 | c >> 24);
961 packet.seg = VTOPOFF(buf);
962 packet.off = VTOPSEG(buf);
963 packet.lba = lba + dsk->start;
967 v86.edx = dsk->drive;
968 v86.ds = VTOPSEG(&packet);
969 v86.esi = VTOPOFF(&packet);
971 if (V86_CY(v86.efl)) {
972 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
977 static unsigned c = 0x2d5c7c2f;
980 if (!OPT_CHECK(RBX_QUIET))
981 printf("%c\b", c = c << 8 | c >> 24);
982 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
983 v86.addr = XREADORG; /* call to xread in boot1 */
984 v86.es = VTOPSEG(buf);
986 v86.ebx = VTOPOFF(buf);
988 v86.edx = nblk << 8 | dsk->drive;
991 if (V86_CY(v86.efl)) {
992 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
1000 keyhit(unsigned ticks)
1004 if (OPT_CHECK(RBX_NOINTR))
1010 t1 = *(uint32_t *)PTOV(0x46c);
1013 if (t1 < t0 || t1 >= t0 + ticks)
1021 if (ioctrl & IO_KEYBOARD)
1023 if (ioctrl & IO_SERIAL)
1031 if (OPT_CHECK(RBX_NOINTR))
1034 if (ioctrl & IO_KEYBOARD && getc(1))
1035 return fn ? 1 : getc(0);
1036 if (ioctrl & IO_SERIAL && sio_ischar())
1037 return fn ? 1 : sio_getc();
1047 * The extra comparison against zero is an attempt to work around
1048 * what appears to be a bug in QEMU and Bochs. Both emulators
1049 * sometimes report a key-press with scancode one and ascii zero
1050 * when no such key is pressed in reality. As far as I can tell,
1051 * this only happens shortly after a reboot.
1056 return fn == 0 ? v86.eax & 0xff : (!V86_ZR(v86.efl) && (v86.eax & 0xff));