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>
21 #include <sys/dirent.h>
22 #include <sys/reboot.h>
24 #include <machine/bootinfo.h>
25 #include <machine/elf.h>
26 #include <machine/pc/bios.h>
27 #include <machine/psl.h>
56 #define TYPE_MAXHARD TYPE_DA
61 static const uuid_t freebsd_ufs_uuid = GPT_ENT_TYPE_FREEBSD_UFS;
62 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
63 static const unsigned char flags[NOPT] = {
81 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
82 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
84 static char kname[1024];
85 static int comspeed = SIOSPD;
86 static struct bootinfo bootinfo;
87 #ifdef LOADER_GELI_SUPPORT
88 static struct geli_boot_args geliargs;
91 static vm_offset_t high_heap_base;
92 static uint32_t bios_basemem, bios_extmem, high_heap_size;
94 static struct bios_smap smap;
97 * The minimum amount of memory to reserve in bios_extmem for the heap.
99 #define HEAP_MIN (3 * 1024 * 1024)
101 static char *heap_next;
102 static char *heap_end;
104 static void load(void);
105 static int parse_cmds(char *, int *);
106 static int dskread(void *, daddr_t, unsigned);
107 #ifdef LOADER_GELI_SUPPORT
108 static int vdev_read(void *vdev __unused, void *priv, off_t off, void *buf,
114 #ifdef LOADER_GELI_SUPPORT
115 #include "geliboot.h"
116 static char gelipw[GELI_PW_MAXLEN];
121 #ifdef LOADER_GELI_SUPPORT
122 struct geli_dev *gdev;
126 static struct gptdsk gdsk;
129 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
132 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
133 printf("Invalid %s\n", "format");
144 /* Parse system memory map */
148 v86.addr = MEM_EXT; /* int 0x15 function 0xe820*/
150 v86.ecx = sizeof(struct bios_smap);
152 v86.es = VTOPSEG(&smap);
153 v86.edi = VTOPOFF(&smap);
155 if ((v86.efl & 1) || (v86.eax != SMAP_SIG))
157 /* look for a low-memory segment that's large enough */
158 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
159 (smap.length >= (512 * 1024)))
160 bios_basemem = smap.length;
161 /* look for the first segment in 'extended' memory */
162 if ((smap.type == SMAP_TYPE_MEMORY) &&
163 (smap.base == 0x100000)) {
164 bios_extmem = smap.length;
168 * Look for the largest segment in 'extended' memory beyond
171 if ((smap.type == SMAP_TYPE_MEMORY) &&
172 (smap.base > 0x100000) && (smap.base < 0x100000000ull)) {
176 * If this segment crosses the 4GB boundary,
179 if (smap.base + size > 0x100000000ull)
180 size = 0x100000000ull - smap.base;
182 if (size > high_heap_size) {
183 high_heap_size = size;
184 high_heap_base = smap.base;
187 } while (v86.ebx != 0);
189 /* Fall back to the old compatibility function for base memory */
190 if (bios_basemem == 0) {
192 v86.addr = 0x12; /* int 0x12 */
195 bios_basemem = (v86.eax & 0xffff) * 1024;
199 * Fall back through several compatibility functions for extended
202 if (bios_extmem == 0) {
204 v86.addr = 0x15; /* int 0x15 function 0xe801*/
207 if (!(v86.efl & 1)) {
208 bios_extmem = ((v86.ecx & 0xffff) +
209 ((v86.edx & 0xffff) * 64)) * 1024;
212 if (bios_extmem == 0) {
214 v86.addr = 0x15; /* int 0x15 function 0x88*/
217 bios_extmem = (v86.eax & 0xffff) * 1024;
221 * If we have extended memory and did not find a suitable heap
222 * region in the SMAP, use the last 3MB of 'extended' memory as a
223 * high heap candidate.
225 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
226 high_heap_size = HEAP_MIN;
227 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
235 if (gptread(&gdsk.dsk, dmadat->secbuf) == -1) {
236 printf("%s: unable to load GPT\n", BOOTPROG);
239 if (gptfind(&freebsd_ufs_uuid, &gdsk.dsk, gdsk.dsk.part) == -1) {
240 printf("%s: no UFS partition was found\n", BOOTPROG);
243 #ifdef LOADER_GELI_SUPPORT
244 gdsk.gdev = geli_taste(vdev_read, &gdsk.dsk,
245 (gpttable[curent].ent_lba_end - gpttable[curent].ent_lba_start),
246 "disk%up%u:", gdsk.dsk.unit, curent + 1);
247 if (gdsk.gdev != NULL) {
248 if (geli_havekey(gdsk.gdev) != 0 &&
249 geli_passphrase(gdsk.gdev, gelipw) != 0) {
250 printf("%s: unable to decrypt GELI key\n", BOOTPROG);
265 char cmd[512], cmdtmp[512];
267 int autoboot, dskupdated;
270 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
274 if (high_heap_size > 0) {
275 heap_end = PTOV(high_heap_base + high_heap_size);
276 heap_next = PTOV(high_heap_base);
278 heap_next = (char *)dmadat + sizeof(*dmadat);
279 heap_end = (char *)PTOV(bios_basemem);
281 setheap(heap_next, heap_end);
284 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
285 gdsk.dsk.drive = *(uint8_t *)PTOV(ARGS);
286 gdsk.dsk.type = gdsk.dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
287 gdsk.dsk.unit = gdsk.dsk.drive & DRV_MASK;
290 bootinfo.bi_version = BOOTINFO_VERSION;
291 bootinfo.bi_size = sizeof(bootinfo);
292 bootinfo.bi_basemem = bios_basemem / 1024;
293 bootinfo.bi_extmem = bios_extmem / 1024;
294 bootinfo.bi_memsizes_valid++;
295 bootinfo.bi_bios_dev = gdsk.dsk.drive;
297 /* Process configuration file */
307 if ((ino = lookup(PATH_CONFIG)) ||
308 (ino = lookup(PATH_DOTCONFIG))) {
309 sz = fsread(ino, cmd, sizeof(cmd) - 1);
310 cmd[(sz < 0) ? 0 : sz] = '\0';
313 memcpy(cmdtmp, cmd, sizeof(cmdtmp));
314 if (parse_cmds(cmdtmp, &dskupdated))
316 if (dskupdated && gptinit() != 0)
318 if (!OPT_CHECK(RBX_QUIET))
319 printf("%s: %s", PATH_CONFIG, cmd);
323 if (autoboot && keyhit(3)) {
325 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
331 * Try to exec stage 3 boot loader. If interrupted by a
332 * keypress, or in case of failure, try to load a kernel
337 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
339 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
341 gptbootfailed(&gdsk.dsk);
342 if (gptfind(&freebsd_ufs_uuid, &gdsk.dsk, -1) == -1)
347 /* Present the user with the boot2 prompt. */
350 if (!OPT_CHECK(RBX_QUIET)) {
351 printf("\nFreeBSD/x86 boot\n"
352 "Default: %u:%s(%up%u)%s\n"
354 gdsk.dsk.drive & DRV_MASK, dev_nm[gdsk.dsk.type],
355 gdsk.dsk.unit, gdsk.dsk.part, kname);
357 if (ioctrl & IO_SERIAL)
361 getstr(cmd, sizeof(cmd));
362 else if (!OPT_CHECK(RBX_QUIET))
364 if (parse_cmds(cmd, &dskupdated)) {
368 if (dskupdated && gptinit() != 0)
375 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
391 static Elf32_Phdr ep[2];
392 static Elf32_Shdr es[2];
398 if (!(ino = lookup(kname))) {
400 printf("%s: No %s on %u:%s(%up%u)\n", BOOTPROG,
401 kname, gdsk.dsk.drive & DRV_MASK,
402 dev_nm[gdsk.dsk.type], gdsk.dsk.unit,
407 if (xfsread(ino, &hdr, sizeof(hdr)))
409 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
411 else if (IS_ELF(hdr.eh))
414 printf("Invalid %s\n", "format");
418 addr = hdr.ex.a_entry & 0xffffff;
421 if (xfsread(ino, p, hdr.ex.a_text))
423 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
424 if (xfsread(ino, p, hdr.ex.a_data))
426 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
427 bootinfo.bi_symtab = VTOP(p);
428 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
429 p += sizeof(hdr.ex.a_syms);
431 if (xfsread(ino, p, hdr.ex.a_syms))
434 if (xfsread(ino, p, sizeof(int)))
439 if (xfsread(ino, p, x))
444 fs_off = hdr.eh.e_phoff;
445 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
446 if (xfsread(ino, ep + j, sizeof(ep[0])))
448 if (ep[j].p_type == PT_LOAD)
451 for (i = 0; i < 2; i++) {
452 p = PTOV(ep[i].p_paddr & 0xffffff);
453 fs_off = ep[i].p_offset;
454 if (xfsread(ino, p, ep[i].p_filesz))
457 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
458 bootinfo.bi_symtab = VTOP(p);
459 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
460 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
461 (hdr.eh.e_shstrndx + 1);
462 if (xfsread(ino, &es, sizeof(es)))
464 for (i = 0; i < 2; i++) {
465 memcpy(p, &es[i].sh_size,
466 sizeof(es[i].sh_size));
467 p += sizeof(es[i].sh_size);
468 fs_off = es[i].sh_offset;
469 if (xfsread(ino, p, es[i].sh_size))
474 addr = hdr.eh.e_entry & 0xffffff;
476 bootinfo.bi_esymtab = VTOP(p);
477 bootinfo.bi_kernelname = VTOP(kname);
478 bootinfo.bi_bios_dev = gdsk.dsk.drive;
479 #ifdef LOADER_GELI_SUPPORT
480 geliargs.size = sizeof(geliargs);
481 explicit_bzero(gelipw, sizeof(gelipw));
482 export_geli_boot_data(&geliargs.gelidata);
485 * Note that the geliargs struct is passed by value, not by pointer.
486 * Code in btxldr.S copies the values from the entry stack to a fixed
487 * location within loader(8) at startup due to the presence of the
488 * KARGS_FLAGS_EXTARG flag.
490 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
491 MAKEBOOTDEV(dev_maj[gdsk.dsk.type], gdsk.dsk.part + 1, gdsk.dsk.unit, 0xff),
492 #ifdef LOADER_GELI_SUPPORT
493 KARGS_FLAGS_GELI | KARGS_FLAGS_EXTARG, 0, 0, VTOP(&bootinfo), geliargs
495 0, 0, 0, VTOP(&bootinfo)
501 parse_cmds(char *cmdstr, int *dskupdated)
511 while ((c = *arg++)) {
512 if (c == ' ' || c == '\t' || c == '\n')
514 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
519 while ((c = *arg++)) {
521 if (*(uint8_t *)PTOV(0x496) & 0x10) {
524 opts |= OPT_SET(RBX_DUAL) |
528 printf("Keyboard: %s\n", cp);
530 } else if (c == 'S') {
532 while ((unsigned int)(i = *arg++ - '0')
535 if (j > 0 && i == -'0') {
540 * Fall through to error below
541 * ('S' not in optstr[]).
544 for (i = 0; c != optstr[i]; i++)
547 opts ^= OPT_SET(flags[i]);
549 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
550 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
551 if (ioctrl & IO_SERIAL) {
552 if (sio_init(115200 / comspeed) != 0)
553 ioctrl &= ~IO_SERIAL;
556 for (q = arg--; *q && *q != '('; q++);
567 for (i = 0; arg[0] != dev_nm[i][0] ||
568 arg[1] != dev_nm[i][1]; i++)
573 gdsk.dsk.unit = *arg - '0';
574 if (arg[1] != 'p' || gdsk.dsk.unit > 9)
578 while (*arg >= '0' && *arg <= '9')
579 j = j * 10 + *arg++ - '0';
581 if (gdsk.dsk.part < 1 || gdsk.dsk.part > 128)
588 gdsk.dsk.drive = (gdsk.dsk.type <= TYPE_MAXHARD
589 ? DRV_HARD : 0) + drv;
592 if ((i = ep - arg)) {
593 if ((size_t)i >= sizeof(kname))
595 memcpy(kname, arg, i + 1);
604 dskread(void *buf, daddr_t lba, unsigned nblk)
608 err = drvread(&gdsk.dsk, buf, lba + gdsk.dsk.start, nblk);
610 #ifdef LOADER_GELI_SUPPORT
611 if (err == 0 && gdsk.gdev != NULL) {
613 if (geli_io(gdsk.gdev, GELI_DECRYPT, lba * DEV_BSIZE, buf,
622 #ifdef LOADER_GELI_SUPPORT
624 * Read function compatible with the ZFS callback, required to keep the GELI
625 * implementation the same for both UFS and ZFS.
628 vdev_read(void *vdev __unused, void *priv, off_t off, void *buf, size_t bytes)
635 dskp = (struct gptdsk *)priv;
637 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
641 lba = off / DEV_BSIZE;
642 lba += dskp->dsk.start;
645 nb = bytes / DEV_BSIZE;
646 if (nb > VBLKSIZE / DEV_BSIZE)
647 nb = VBLKSIZE / DEV_BSIZE;
648 if (drvread(&dskp->dsk, dmadat->blkbuf, lba, nb))
650 memcpy(p, dmadat->blkbuf, nb * DEV_BSIZE);
653 bytes -= nb * DEV_BSIZE;
658 #endif /* LOADER_GELI_SUPPORT */