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>
45 /* Hint to loader that we came from ZFS */
46 #define KARGS_FLAGS_ZFS 0x4
48 #define PATH_CONFIG "/boot.config"
49 #define PATH_BOOT3 "/boot/zfsloader"
50 #define PATH_KERNEL "/boot/kernel/kernel"
56 #define BIOS_NUMDRIVES 0x475
62 #define TYPE_MAXHARD TYPE_DA
70 static const uuid_t freebsd_zfs_uuid = GPT_ENT_TYPE_FREEBSD_ZFS;
72 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
73 static const unsigned char flags[NOPT] = {
91 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
92 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
95 static char kname[1024];
96 static int comspeed = SIOSPD;
97 static struct bootinfo bootinfo;
98 static uint32_t bootdev;
100 vm_offset_t high_heap_base;
101 uint32_t bios_basemem, bios_extmem, high_heap_size;
103 static struct bios_smap smap;
106 * The minimum amount of memory to reserve in bios_extmem for the heap.
108 #define HEAP_MIN (3 * 1024 * 1024)
110 static char *heap_next;
111 static char *heap_end;
113 /* Buffers that must not span a 64k boundary. */
114 #define READ_BUF_SIZE 8192
116 char rdbuf[READ_BUF_SIZE]; /* for reading large things */
117 char secbuf[READ_BUF_SIZE]; /* for MBR/disklabel */
119 static struct dmadat *dmadat;
122 static void load(void);
123 static int parse(void);
124 static void bios_getmem(void);
130 if (p + n > heap_end) {
131 printf("malloc failure\n");
141 strdup(const char *s)
143 char *p = malloc(strlen(s) + 1);
151 * Read from a dnode (which must be from a ZPL filesystem).
154 zfs_read(spa_t *spa, const dnode_phys_t *dnode, off_t *offp, void *start, size_t size)
156 const znode_phys_t *zp = (const znode_phys_t *) dnode->dn_bonus;
161 if (*offp + n > zp->zp_size)
162 n = zp->zp_size - *offp;
164 rc = dnode_read(spa, dnode, *offp, start, n);
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 %s\n", "format");
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.efl & 1) || (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.efl & 1)) {
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.efl & 0x1) && /* 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, spa_t **spap)
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 MBR and probe each slice in turn for a vdev.
359 if (vdev_probe(vdev_read, dsk, spap) == 0)
362 sec = dmadat->secbuf;
367 * First check for GPT.
369 if (drvread(dsk, sec, 1, 1)) {
372 memcpy(&hdr, sec, sizeof(hdr));
373 if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
374 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
375 hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
380 * Probe all GPT partitions for the presense of ZFS pools. We
381 * return the spa_t for the first we find (if requested). This
382 * will have the effect of booting from the first pool on the
385 entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
386 slba = hdr.hdr_lba_table;
387 elba = slba + hdr.hdr_entries / entries_per_sec;
388 while (slba < elba) {
390 if (drvread(dsk, sec, slba, 1))
392 for (part = 0; part < entries_per_sec; part++) {
393 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
394 if (memcmp(&ent->ent_type, &freebsd_zfs_uuid,
395 sizeof(uuid_t)) == 0) {
396 dsk->start = ent->ent_lba_start;
397 if (vdev_probe(vdev_read, dsk, spap) == 0) {
399 * We record the first pool we find (we will try
400 * to boot from that one).
405 * This slice had a vdev. We need a new dsk
406 * structure now since the vdev now owns this one.
418 if (drvread(dsk, sec, DOSBBSECTOR, 1))
420 dp = (void *)(sec + DOSPARTOFF);
422 for (i = 0; i < NDOSPART; i++) {
425 dsk->start = dp[i].dp_start;
426 if (vdev_probe(vdev_read, dsk, spap) == 0) {
428 * We record the first pool we find (we will try to boot
434 * This slice had a vdev. We need a new dsk structure now
435 * since the vdev now owns this one.
450 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
454 if (high_heap_size > 0) {
455 heap_end = PTOV(high_heap_base + high_heap_size);
456 heap_next = PTOV(high_heap_base);
458 heap_next = (char *) dmadat + sizeof(*dmadat);
459 heap_end = (char *) PTOV(bios_basemem);
462 dsk = malloc(sizeof(struct dsk));
463 dsk->drive = *(uint8_t *)PTOV(ARGS);
464 dsk->type = dsk->drive & DRV_HARD ? TYPE_AD : TYPE_FD;
465 dsk->unit = dsk->drive & DRV_MASK;
466 dsk->slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
471 bootinfo.bi_version = BOOTINFO_VERSION;
472 bootinfo.bi_size = sizeof(bootinfo);
473 bootinfo.bi_basemem = bios_basemem / 1024;
474 bootinfo.bi_extmem = bios_extmem / 1024;
475 bootinfo.bi_memsizes_valid++;
476 bootinfo.bi_bios_dev = dsk->drive;
478 bootdev = MAKEBOOTDEV(dev_maj[dsk->type],
479 dsk->slice, dsk->unit, dsk->part),
481 /* Process configuration file */
488 * Probe the boot drive first - we will try to boot from whatever
489 * pool we find on that drive.
491 probe_drive(dsk, &spa);
494 * Probe the rest of the drives that the bios knows about. This
495 * will find any other available pools and it may fill in missing
496 * vdevs for the boot pool.
498 for (i = 0; i < *(unsigned char *)PTOV(BIOS_NUMDRIVES); i++) {
499 if ((i | DRV_HARD) == *(uint8_t *)PTOV(ARGS))
502 if (!int13probe(i | DRV_HARD))
505 dsk = malloc(sizeof(struct dsk));
506 dsk->drive = i | DRV_HARD;
507 dsk->type = dsk->drive & TYPE_AD;
513 probe_drive(dsk, NULL);
517 * If we didn't find a pool on the boot drive, default to the
518 * first pool we found, if any.
521 spa = STAILQ_FIRST(&zfs_pools);
523 printf("%s: No ZFS pools located, can't boot\n", BOOTPROG);
531 if (zfs_lookup(spa, PATH_CONFIG, &dn) == 0) {
533 zfs_read(spa, &dn, &off, cmd, sizeof(cmd));
539 if (!OPT_CHECK(RBX_QUIET))
540 printf("%s: %s", PATH_CONFIG, cmd);
541 /* Do not process this command twice */
546 * Try to exec stage 3 boot loader. If interrupted by a keypress,
547 * or in case of failure, try to load a kernel directly instead.
550 if (autoboot && !*kname) {
551 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
554 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
558 /* Present the user with the boot2 prompt. */
561 if (!autoboot || !OPT_CHECK(RBX_QUIET))
562 printf("\nFreeBSD/x86 boot\n"
565 spa->spa_name, kname);
566 if (ioctrl & IO_SERIAL)
568 if (!autoboot || keyhit(5))
569 getstr(cmd, sizeof(cmd));
570 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
580 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
593 static Elf32_Phdr ep[2];
594 static Elf32_Shdr es[2];
601 if (zfs_lookup(spa, kname, &dn)) {
605 if (xfsread(&dn, &off, &hdr, sizeof(hdr)))
607 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
609 else if (IS_ELF(hdr.eh))
612 printf("Invalid %s\n", "format");
616 addr = hdr.ex.a_entry & 0xffffff;
619 if (xfsread(&dn, &off, p, hdr.ex.a_text))
621 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
622 if (xfsread(&dn, &off, p, hdr.ex.a_data))
624 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
625 bootinfo.bi_symtab = VTOP(p);
626 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
627 p += sizeof(hdr.ex.a_syms);
629 if (xfsread(&dn, &off, p, hdr.ex.a_syms))
632 if (xfsread(&dn, &off, p, sizeof(int)))
637 if (xfsread(&dn, &off, p, x))
642 off = hdr.eh.e_phoff;
643 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
644 if (xfsread(&dn, &off, ep + j, sizeof(ep[0])))
646 if (ep[j].p_type == PT_LOAD)
649 for (i = 0; i < 2; i++) {
650 p = PTOV(ep[i].p_paddr & 0xffffff);
651 off = ep[i].p_offset;
652 if (xfsread(&dn, &off, p, ep[i].p_filesz))
655 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
656 bootinfo.bi_symtab = VTOP(p);
657 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
658 off = hdr.eh.e_shoff + sizeof(es[0]) *
659 (hdr.eh.e_shstrndx + 1);
660 if (xfsread(&dn, &off, &es, sizeof(es)))
662 for (i = 0; i < 2; i++) {
663 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
664 p += sizeof(es[i].sh_size);
665 off = es[i].sh_offset;
666 if (xfsread(&dn, &off, p, es[i].sh_size))
671 addr = hdr.eh.e_entry & 0xffffff;
673 bootinfo.bi_esymtab = VTOP(p);
674 bootinfo.bi_kernelname = VTOP(kname);
675 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
678 (uint32_t) spa->spa_guid,
679 (uint32_t) (spa->spa_guid >> 32),
692 while ((c = *arg++)) {
693 if (c == ' ' || c == '\t' || c == '\n')
695 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
700 while ((c = *arg++)) {
702 if (*(uint8_t *)PTOV(0x496) & 0x10) {
705 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
708 printf("Keyboard: %s\n", cp);
710 } else if (c == 'S') {
712 while ((unsigned int)(i = *arg++ - '0') <= 9)
714 if (j > 0 && i == -'0') {
718 /* Fall through to error below ('S' not in optstr[]). */
720 for (i = 0; c != optstr[i]; i++)
723 opts ^= OPT_SET(flags[i]);
725 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
726 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
727 if (ioctrl & IO_SERIAL)
728 sio_init(115200 / comspeed);
732 if (zfs_lookup(spa, arg, &dn) == 0) {
740 * Report pool status if the comment is 'status'. Lets
741 * hope no-one wants to load /status as a kernel.
743 if (!strcmp(arg, "status")) {
749 * If there is a colon, switch pools.
751 q = (char *) strchr(arg, ':');
756 newspa = spa_find_by_name(arg);
761 printf("\nCan't find ZFS pool %s\n", arg);
766 if ((i = ep - arg)) {
767 if ((size_t)i >= sizeof(kname))
769 memcpy(kname, arg, i + 1);