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 struct dsk dsk;
85 static char kname[1024];
86 static int comspeed = SIOSPD;
87 static struct bootinfo bootinfo;
88 #ifdef LOADER_GELI_SUPPORT
89 static struct geli_boot_args geliargs;
92 static vm_offset_t high_heap_base;
93 static uint32_t bios_basemem, bios_extmem, high_heap_size;
95 static struct bios_smap smap;
98 * The minimum amount of memory to reserve in bios_extmem for the heap.
100 #define HEAP_MIN (3 * 1024 * 1024)
102 static char *heap_next;
103 static char *heap_end;
105 static void load(void);
106 static int parse_cmds(char *, int *);
107 static int dskread(void *, daddr_t, unsigned);
108 #ifdef LOADER_GELI_SUPPORT
109 static int vdev_read(void *vdev __unused, void *priv, off_t off, void *buf,
115 #ifdef LOADER_GELI_SUPPORT
116 #include "geliboot.c"
117 static char gelipw[GELI_PW_MAXLEN];
118 static struct keybuf *gelibuf;
122 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
125 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
126 printf("Invalid %s\n", "format");
137 /* Parse system memory map */
141 v86.addr = MEM_EXT; /* int 0x15 function 0xe820*/
143 v86.ecx = sizeof(struct bios_smap);
145 v86.es = VTOPSEG(&smap);
146 v86.edi = VTOPOFF(&smap);
148 if ((v86.efl & 1) || (v86.eax != SMAP_SIG))
150 /* look for a low-memory segment that's large enough */
151 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
152 (smap.length >= (512 * 1024)))
153 bios_basemem = smap.length;
154 /* look for the first segment in 'extended' memory */
155 if ((smap.type == SMAP_TYPE_MEMORY) &&
156 (smap.base == 0x100000)) {
157 bios_extmem = smap.length;
161 * Look for the largest segment in 'extended' memory beyond
164 if ((smap.type == SMAP_TYPE_MEMORY) &&
165 (smap.base > 0x100000) && (smap.base < 0x100000000ull)) {
169 * If this segment crosses the 4GB boundary,
172 if (smap.base + size > 0x100000000ull)
173 size = 0x100000000ull - smap.base;
175 if (size > high_heap_size) {
176 high_heap_size = size;
177 high_heap_base = smap.base;
180 } while (v86.ebx != 0);
182 /* Fall back to the old compatibility function for base memory */
183 if (bios_basemem == 0) {
185 v86.addr = 0x12; /* int 0x12 */
188 bios_basemem = (v86.eax & 0xffff) * 1024;
192 * Fall back through several compatibility functions for extended
195 if (bios_extmem == 0) {
197 v86.addr = 0x15; /* int 0x15 function 0xe801*/
200 if (!(v86.efl & 1)) {
201 bios_extmem = ((v86.ecx & 0xffff) +
202 ((v86.edx & 0xffff) * 64)) * 1024;
205 if (bios_extmem == 0) {
207 v86.addr = 0x15; /* int 0x15 function 0x88*/
210 bios_extmem = (v86.eax & 0xffff) * 1024;
214 * If we have extended memory and did not find a suitable heap
215 * region in the SMAP, use the last 3MB of 'extended' memory as a
216 * high heap candidate.
218 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
219 high_heap_size = HEAP_MIN;
220 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
228 if (gptread(&freebsd_ufs_uuid, &dsk, dmadat->secbuf) == -1) {
229 printf("%s: unable to load GPT\n", BOOTPROG);
232 if (gptfind(&freebsd_ufs_uuid, &dsk, dsk.part) == -1) {
233 printf("%s: no UFS partition was found\n", BOOTPROG);
236 #ifdef LOADER_GELI_SUPPORT
237 if (geli_taste(vdev_read, &dsk, (gpttable[curent].ent_lba_end -
238 gpttable[curent].ent_lba_start)) == 0) {
239 if (geli_havekey(&dsk) != 0 && geli_passphrase(gelipw,
240 dsk.unit, 'p', curent + 1, &dsk) != 0) {
241 printf("%s: unable to decrypt GELI key\n", BOOTPROG);
256 char cmd[512], cmdtmp[512];
258 int autoboot, dskupdated;
261 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
265 if (high_heap_size > 0) {
266 heap_end = PTOV(high_heap_base + high_heap_size);
267 heap_next = PTOV(high_heap_base);
269 heap_next = (char *)dmadat + sizeof(*dmadat);
270 heap_end = (char *)PTOV(bios_basemem);
272 setheap(heap_next, heap_end);
275 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
276 dsk.drive = *(uint8_t *)PTOV(ARGS);
277 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
278 dsk.unit = dsk.drive & DRV_MASK;
281 bootinfo.bi_version = BOOTINFO_VERSION;
282 bootinfo.bi_size = sizeof(bootinfo);
283 bootinfo.bi_basemem = bios_basemem / 1024;
284 bootinfo.bi_extmem = bios_extmem / 1024;
285 bootinfo.bi_memsizes_valid++;
286 bootinfo.bi_bios_dev = dsk.drive;
288 #ifdef LOADER_GELI_SUPPORT
291 /* Process configuration file */
301 if ((ino = lookup(PATH_CONFIG)) ||
302 (ino = lookup(PATH_DOTCONFIG))) {
303 sz = fsread(ino, cmd, sizeof(cmd) - 1);
304 cmd[(sz < 0) ? 0 : sz] = '\0';
307 memcpy(cmdtmp, cmd, sizeof(cmdtmp));
308 if (parse_cmds(cmdtmp, &dskupdated))
310 if (dskupdated && gptinit() != 0)
312 if (!OPT_CHECK(RBX_QUIET))
313 printf("%s: %s", PATH_CONFIG, cmd);
317 if (autoboot && keyhit(3)) {
319 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
325 * Try to exec stage 3 boot loader. If interrupted by a
326 * keypress, or in case of failure, try to load a kernel
331 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
333 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
336 if (gptfind(&freebsd_ufs_uuid, &dsk, -1) == -1)
341 /* Present the user with the boot2 prompt. */
344 if (!OPT_CHECK(RBX_QUIET)) {
345 printf("\nFreeBSD/x86 boot\n"
346 "Default: %u:%s(%up%u)%s\n"
348 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
351 if (ioctrl & IO_SERIAL)
355 getstr(cmd, sizeof(cmd));
356 else if (!OPT_CHECK(RBX_QUIET))
358 if (parse_cmds(cmd, &dskupdated)) {
362 if (dskupdated && gptinit() != 0)
369 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
385 static Elf32_Phdr ep[2];
386 static Elf32_Shdr es[2];
392 if (!(ino = lookup(kname))) {
394 printf("%s: No %s on %u:%s(%up%u)\n", BOOTPROG,
395 kname, dsk.drive & DRV_MASK, dev_nm[dsk.type],
401 if (xfsread(ino, &hdr, sizeof(hdr)))
403 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
405 else if (IS_ELF(hdr.eh))
408 printf("Invalid %s\n", "format");
412 addr = hdr.ex.a_entry & 0xffffff;
415 if (xfsread(ino, p, hdr.ex.a_text))
417 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
418 if (xfsread(ino, p, hdr.ex.a_data))
420 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
421 bootinfo.bi_symtab = VTOP(p);
422 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
423 p += sizeof(hdr.ex.a_syms);
425 if (xfsread(ino, p, hdr.ex.a_syms))
428 if (xfsread(ino, p, sizeof(int)))
433 if (xfsread(ino, p, x))
438 fs_off = hdr.eh.e_phoff;
439 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
440 if (xfsread(ino, ep + j, sizeof(ep[0])))
442 if (ep[j].p_type == PT_LOAD)
445 for (i = 0; i < 2; i++) {
446 p = PTOV(ep[i].p_paddr & 0xffffff);
447 fs_off = ep[i].p_offset;
448 if (xfsread(ino, p, ep[i].p_filesz))
451 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
452 bootinfo.bi_symtab = VTOP(p);
453 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
454 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
455 (hdr.eh.e_shstrndx + 1);
456 if (xfsread(ino, &es, sizeof(es)))
458 for (i = 0; i < 2; i++) {
459 memcpy(p, &es[i].sh_size,
460 sizeof(es[i].sh_size));
461 p += sizeof(es[i].sh_size);
462 fs_off = es[i].sh_offset;
463 if (xfsread(ino, p, es[i].sh_size))
468 addr = hdr.eh.e_entry & 0xffffff;
470 bootinfo.bi_esymtab = VTOP(p);
471 bootinfo.bi_kernelname = VTOP(kname);
472 bootinfo.bi_bios_dev = dsk.drive;
473 #ifdef LOADER_GELI_SUPPORT
474 geliargs.size = sizeof(geliargs);
475 explicit_bzero(gelipw, sizeof(gelipw));
476 gelibuf = malloc(sizeof(struct keybuf) +
477 (GELI_MAX_KEYS * sizeof(struct keybuf_ent)));
478 geli_fill_keybuf(gelibuf);
479 geliargs.notapw = '\0';
480 geliargs.keybuf_sentinel = KEYBUF_SENTINEL;
481 geliargs.keybuf = gelibuf;
483 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
484 MAKEBOOTDEV(dev_maj[dsk.type], dsk.part + 1, dsk.unit, 0xff),
485 KARGS_FLAGS_EXTARG, 0, 0, VTOP(&bootinfo)
486 #ifdef LOADER_GELI_SUPPORT
493 parse_cmds(char *cmdstr, int *dskupdated)
503 while ((c = *arg++)) {
504 if (c == ' ' || c == '\t' || c == '\n')
506 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
511 while ((c = *arg++)) {
513 if (*(uint8_t *)PTOV(0x496) & 0x10) {
516 opts |= OPT_SET(RBX_DUAL) |
520 printf("Keyboard: %s\n", cp);
522 } else if (c == 'S') {
524 while ((unsigned int)(i = *arg++ - '0')
527 if (j > 0 && i == -'0') {
532 * Fall through to error below
533 * ('S' not in optstr[]).
536 for (i = 0; c != optstr[i]; i++)
539 opts ^= OPT_SET(flags[i]);
541 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
542 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
543 if (ioctrl & IO_SERIAL) {
544 if (sio_init(115200 / comspeed) != 0)
545 ioctrl &= ~IO_SERIAL;
548 for (q = arg--; *q && *q != '('; q++);
559 for (i = 0; arg[0] != dev_nm[i][0] ||
560 arg[1] != dev_nm[i][1]; i++)
565 dsk.unit = *arg - '0';
566 if (arg[1] != 'p' || dsk.unit > 9)
569 dsk.part = *arg - '0';
570 if (dsk.part < 1 || dsk.part > 9)
578 dsk.drive = (dsk.type <= TYPE_MAXHARD
579 ? DRV_HARD : 0) + drv;
582 if ((i = ep - arg)) {
583 if ((size_t)i >= sizeof(kname))
585 memcpy(kname, arg, i + 1);
594 dskread(void *buf, daddr_t lba, unsigned nblk)
598 err = drvread(&dsk, buf, lba + dsk.start, nblk);
600 #ifdef LOADER_GELI_SUPPORT
601 if (err == 0 && is_geli(&dsk) == 0) {
603 if (geli_read(&dsk, lba * DEV_BSIZE, buf, nblk * DEV_BSIZE))
611 #ifdef LOADER_GELI_SUPPORT
613 * Read function compartible with the ZFS callback, required to keep the GELI
614 * Implementation the same for both UFS and ZFS
617 vdev_read(void *vdev __unused, void *priv, off_t off, void *buf, size_t bytes)
624 dskp = (struct dsk *)priv;
626 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
630 lba = off / DEV_BSIZE;
634 nb = bytes / DEV_BSIZE;
635 if (nb > VBLKSIZE / DEV_BSIZE)
636 nb = VBLKSIZE / DEV_BSIZE;
637 if (drvread(dskp, dmadat->blkbuf, lba, nb))
639 memcpy(p, dmadat->blkbuf, nb * DEV_BSIZE);
642 bytes -= nb * DEV_BSIZE;
647 #endif /* LOADER_GELI_SUPPORT */