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>
35 /* Forward declared to avoid warnings -- these shouldn't be needed */
36 int strcasecmp(const char *s1, const char *s2);
37 void explicit_bzero(void *b, size_t len);
59 #define TYPE_MAXHARD TYPE_DA
64 static const uuid_t freebsd_ufs_uuid = GPT_ENT_TYPE_FREEBSD_UFS;
65 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
66 static const unsigned char flags[NOPT] = {
84 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
85 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
87 static struct dsk dsk;
88 static char kname[1024];
89 static int comspeed = SIOSPD;
90 static struct bootinfo bootinfo;
91 #ifdef LOADER_GELI_SUPPORT
92 static struct geli_boot_args geliargs;
95 static vm_offset_t high_heap_base;
96 static uint32_t bios_basemem, bios_extmem, high_heap_size;
98 static struct bios_smap smap;
101 * The minimum amount of memory to reserve in bios_extmem for the heap.
103 #define HEAP_MIN (3 * 1024 * 1024)
105 static char *heap_next;
106 static char *heap_end;
109 static void load(void);
110 static int parse_cmds(char *, int *);
111 static int dskread(void *, daddr_t, unsigned);
112 void *malloc(size_t n);
113 void free(void *ptr);
114 #ifdef LOADER_GELI_SUPPORT
115 static int vdev_read(void *vdev __unused, void *priv, off_t off, void *buf,
123 if (p + n > heap_end) {
124 printf("malloc failure\n");
143 #ifdef LOADER_GELI_SUPPORT
144 #include "geliboot.c"
145 static char gelipw[GELI_PW_MAXLEN];
146 static struct keybuf *gelibuf;
150 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
153 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
154 printf("Invalid %s\n", "format");
165 /* Parse system memory map */
169 v86.addr = MEM_EXT; /* int 0x15 function 0xe820*/
171 v86.ecx = sizeof(struct bios_smap);
173 v86.es = VTOPSEG(&smap);
174 v86.edi = VTOPOFF(&smap);
176 if ((v86.efl & 1) || (v86.eax != SMAP_SIG))
178 /* look for a low-memory segment that's large enough */
179 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
180 (smap.length >= (512 * 1024)))
181 bios_basemem = smap.length;
182 /* look for the first segment in 'extended' memory */
183 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000)) {
184 bios_extmem = smap.length;
188 * Look for the largest segment in 'extended' memory beyond
191 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
192 (smap.base < 0x100000000ull)) {
196 * If this segment crosses the 4GB boundary, truncate it.
198 if (smap.base + size > 0x100000000ull)
199 size = 0x100000000ull - smap.base;
201 if (size > high_heap_size) {
202 high_heap_size = size;
203 high_heap_base = smap.base;
206 } while (v86.ebx != 0);
208 /* Fall back to the old compatibility function for base memory */
209 if (bios_basemem == 0) {
211 v86.addr = 0x12; /* int 0x12 */
214 bios_basemem = (v86.eax & 0xffff) * 1024;
217 /* Fall back through several compatibility functions for extended memory */
218 if (bios_extmem == 0) {
220 v86.addr = 0x15; /* int 0x15 function 0xe801*/
223 if (!(v86.efl & 1)) {
224 bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
227 if (bios_extmem == 0) {
229 v86.addr = 0x15; /* int 0x15 function 0x88*/
232 bios_extmem = (v86.eax & 0xffff) * 1024;
236 * If we have extended memory and did not find a suitable heap
237 * region in the SMAP, use the last 3MB of 'extended' memory as a
238 * high heap candidate.
240 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
241 high_heap_size = HEAP_MIN;
242 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
250 if (gptread(&freebsd_ufs_uuid, &dsk, dmadat->secbuf) == -1) {
251 printf("%s: unable to load GPT\n", BOOTPROG);
254 if (gptfind(&freebsd_ufs_uuid, &dsk, dsk.part) == -1) {
255 printf("%s: no UFS partition was found\n", BOOTPROG);
258 #ifdef LOADER_GELI_SUPPORT
259 if (geli_taste(vdev_read, &dsk, (gpttable[curent].ent_lba_end -
260 gpttable[curent].ent_lba_start)) == 0) {
261 if (geli_havekey(&dsk) != 0 && geli_passphrase(gelipw,
262 dsk.unit, 'p', curent + 1, &dsk) != 0) {
263 printf("%s: unable to decrypt GELI key\n", BOOTPROG);
278 char cmd[512], cmdtmp[512];
280 int autoboot, dskupdated;
283 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
287 if (high_heap_size > 0) {
288 heap_end = PTOV(high_heap_base + high_heap_size);
289 heap_next = PTOV(high_heap_base);
291 heap_next = (char *)dmadat + sizeof(*dmadat);
292 heap_end = (char *)PTOV(bios_basemem);
296 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
297 dsk.drive = *(uint8_t *)PTOV(ARGS);
298 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
299 dsk.unit = dsk.drive & DRV_MASK;
302 bootinfo.bi_version = BOOTINFO_VERSION;
303 bootinfo.bi_size = sizeof(bootinfo);
304 bootinfo.bi_basemem = bios_basemem / 1024;
305 bootinfo.bi_extmem = bios_extmem / 1024;
306 bootinfo.bi_memsizes_valid++;
307 bootinfo.bi_bios_dev = dsk.drive;
309 #ifdef LOADER_GELI_SUPPORT
312 /* Process configuration file */
322 if ((ino = lookup(PATH_CONFIG)) ||
323 (ino = lookup(PATH_DOTCONFIG))) {
324 sz = fsread(ino, cmd, sizeof(cmd) - 1);
325 cmd[(sz < 0) ? 0 : sz] = '\0';
328 memcpy(cmdtmp, cmd, sizeof(cmdtmp));
329 if (parse_cmds(cmdtmp, &dskupdated))
331 if (dskupdated && gptinit() != 0)
333 if (!OPT_CHECK(RBX_QUIET))
334 printf("%s: %s", PATH_CONFIG, cmd);
338 if (autoboot && keyhit(3)) {
340 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
346 * Try to exec stage 3 boot loader. If interrupted by a
347 * keypress, or in case of failure, try to load a kernel
352 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
354 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
357 if (gptfind(&freebsd_ufs_uuid, &dsk, -1) == -1)
362 /* Present the user with the boot2 prompt. */
365 if (!OPT_CHECK(RBX_QUIET)) {
366 printf("\nFreeBSD/x86 boot\n"
367 "Default: %u:%s(%up%u)%s\n"
369 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
372 if (ioctrl & IO_SERIAL)
376 getstr(cmd, sizeof(cmd));
377 else if (!OPT_CHECK(RBX_QUIET))
379 if (parse_cmds(cmd, &dskupdated)) {
383 if (dskupdated && gptinit() != 0)
390 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
403 static Elf32_Phdr ep[2];
404 static Elf32_Shdr es[2];
410 if (!(ino = lookup(kname))) {
412 printf("%s: No %s on %u:%s(%up%u)\n", BOOTPROG,
413 kname, dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
418 if (xfsread(ino, &hdr, sizeof(hdr)))
420 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
422 else if (IS_ELF(hdr.eh))
425 printf("Invalid %s\n", "format");
429 addr = hdr.ex.a_entry & 0xffffff;
432 if (xfsread(ino, p, hdr.ex.a_text))
434 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
435 if (xfsread(ino, p, hdr.ex.a_data))
437 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
438 bootinfo.bi_symtab = VTOP(p);
439 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
440 p += sizeof(hdr.ex.a_syms);
442 if (xfsread(ino, p, hdr.ex.a_syms))
445 if (xfsread(ino, p, sizeof(int)))
450 if (xfsread(ino, p, x))
455 fs_off = hdr.eh.e_phoff;
456 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
457 if (xfsread(ino, ep + j, sizeof(ep[0])))
459 if (ep[j].p_type == PT_LOAD)
462 for (i = 0; i < 2; i++) {
463 p = PTOV(ep[i].p_paddr & 0xffffff);
464 fs_off = ep[i].p_offset;
465 if (xfsread(ino, p, ep[i].p_filesz))
468 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
469 bootinfo.bi_symtab = VTOP(p);
470 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
471 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
472 (hdr.eh.e_shstrndx + 1);
473 if (xfsread(ino, &es, sizeof(es)))
475 for (i = 0; i < 2; i++) {
476 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
477 p += sizeof(es[i].sh_size);
478 fs_off = es[i].sh_offset;
479 if (xfsread(ino, p, es[i].sh_size))
484 addr = hdr.eh.e_entry & 0xffffff;
486 bootinfo.bi_esymtab = VTOP(p);
487 bootinfo.bi_kernelname = VTOP(kname);
488 bootinfo.bi_bios_dev = dsk.drive;
489 #ifdef LOADER_GELI_SUPPORT
490 geliargs.size = sizeof(geliargs);
491 explicit_bzero(gelipw, sizeof(gelipw));
492 gelibuf = malloc(sizeof(struct keybuf) + (GELI_MAX_KEYS * sizeof(struct keybuf_ent)));
493 geli_fill_keybuf(gelibuf);
494 geliargs.notapw = '\0';
495 geliargs.keybuf_sentinel = KEYBUF_SENTINEL;
496 geliargs.keybuf = gelibuf;
498 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
499 MAKEBOOTDEV(dev_maj[dsk.type], dsk.part + 1, dsk.unit, 0xff),
500 KARGS_FLAGS_EXTARG, 0, 0, VTOP(&bootinfo)
501 #ifdef LOADER_GELI_SUPPORT
508 parse_cmds(char *cmdstr, int *dskupdated)
517 while ((c = *arg++)) {
518 if (c == ' ' || c == '\t' || c == '\n')
520 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
525 while ((c = *arg++)) {
527 if (*(uint8_t *)PTOV(0x496) & 0x10) {
530 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
533 printf("Keyboard: %s\n", cp);
535 } else if (c == 'S') {
537 while ((unsigned int)(i = *arg++ - '0') <= 9)
539 if (j > 0 && i == -'0') {
543 /* Fall through to error below ('S' not in optstr[]). */
545 for (i = 0; c != optstr[i]; i++)
548 opts ^= OPT_SET(flags[i]);
550 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
551 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
552 if (ioctrl & IO_SERIAL) {
553 if (sio_init(115200 / comspeed) != 0)
554 ioctrl &= ~IO_SERIAL;
557 for (q = arg--; *q && *q != '('; q++);
568 for (i = 0; arg[0] != dev_nm[i][0] ||
569 arg[1] != dev_nm[i][1]; i++)
574 dsk.unit = *arg - '0';
575 if (arg[1] != 'p' || dsk.unit > 9)
578 dsk.part = *arg - '0';
579 if (dsk.part < 1 || dsk.part > 9)
587 dsk.drive = (dsk.type <= TYPE_MAXHARD
588 ? DRV_HARD : 0) + drv;
591 if ((i = ep - arg)) {
592 if ((size_t)i >= sizeof(kname))
594 memcpy(kname, arg, i + 1);
603 dskread(void *buf, daddr_t lba, unsigned nblk)
607 err = drvread(&dsk, buf, lba + dsk.start, nblk);
609 #ifdef LOADER_GELI_SUPPORT
610 if (err == 0 && is_geli(&dsk) == 0) {
612 if (geli_read(&dsk, lba * DEV_BSIZE, buf, nblk * DEV_BSIZE))
620 #ifdef LOADER_GELI_SUPPORT
622 * Read function compartible with the ZFS callback, required to keep the GELI
623 * Implementation the same for both UFS and ZFS
626 vdev_read(void *vdev __unused, void *priv, off_t off, void *buf, size_t bytes)
631 struct dsk *dskp = (struct dsk *) priv;
633 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
637 lba = off / DEV_BSIZE;
641 nb = bytes / DEV_BSIZE;
642 if (nb > VBLKSIZE / DEV_BSIZE)
643 nb = VBLKSIZE / DEV_BSIZE;
644 if (drvread(dskp, dmadat->blkbuf, lba, nb))
646 memcpy(p, dmadat->blkbuf, nb * DEV_BSIZE);
649 bytes -= nb * DEV_BSIZE;
654 #endif /* LOADER_GELI_SUPPORT */