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.h"
117 static char gelipw[GELI_PW_MAXLEN];
118 static struct keybuf *gelibuf;
123 #ifdef LOADER_GELI_SUPPORT
124 struct geli_dev *gdev;
128 static struct gptdsk gdsk;
131 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
134 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
135 printf("Invalid %s\n", "format");
146 /* Parse system memory map */
150 v86.addr = MEM_EXT; /* int 0x15 function 0xe820*/
152 v86.ecx = sizeof(struct bios_smap);
154 v86.es = VTOPSEG(&smap);
155 v86.edi = VTOPOFF(&smap);
157 if ((v86.efl & 1) || (v86.eax != SMAP_SIG))
159 /* look for a low-memory segment that's large enough */
160 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
161 (smap.length >= (512 * 1024)))
162 bios_basemem = smap.length;
163 /* look for the first segment in 'extended' memory */
164 if ((smap.type == SMAP_TYPE_MEMORY) &&
165 (smap.base == 0x100000)) {
166 bios_extmem = smap.length;
170 * Look for the largest segment in 'extended' memory beyond
173 if ((smap.type == SMAP_TYPE_MEMORY) &&
174 (smap.base > 0x100000) && (smap.base < 0x100000000ull)) {
178 * If this segment crosses the 4GB boundary,
181 if (smap.base + size > 0x100000000ull)
182 size = 0x100000000ull - smap.base;
184 if (size > high_heap_size) {
185 high_heap_size = size;
186 high_heap_base = smap.base;
189 } while (v86.ebx != 0);
191 /* Fall back to the old compatibility function for base memory */
192 if (bios_basemem == 0) {
194 v86.addr = 0x12; /* int 0x12 */
197 bios_basemem = (v86.eax & 0xffff) * 1024;
201 * Fall back through several compatibility functions for extended
204 if (bios_extmem == 0) {
206 v86.addr = 0x15; /* int 0x15 function 0xe801*/
209 if (!(v86.efl & 1)) {
210 bios_extmem = ((v86.ecx & 0xffff) +
211 ((v86.edx & 0xffff) * 64)) * 1024;
214 if (bios_extmem == 0) {
216 v86.addr = 0x15; /* int 0x15 function 0x88*/
219 bios_extmem = (v86.eax & 0xffff) * 1024;
223 * If we have extended memory and did not find a suitable heap
224 * region in the SMAP, use the last 3MB of 'extended' memory as a
225 * high heap candidate.
227 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
228 high_heap_size = HEAP_MIN;
229 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
237 if (gptread(&freebsd_ufs_uuid, &gdsk.dsk, dmadat->secbuf) == -1) {
238 printf("%s: unable to load GPT\n", BOOTPROG);
241 if (gptfind(&freebsd_ufs_uuid, &gdsk.dsk, gdsk.dsk.part) == -1) {
242 printf("%s: no UFS partition was found\n", BOOTPROG);
245 #ifdef LOADER_GELI_SUPPORT
246 gdsk.gdev = geli_taste(vdev_read, &gdsk.dsk,
247 (gpttable[curent].ent_lba_end - gpttable[curent].ent_lba_start),
248 "disk%up%u:", gdsk.dsk.unit, curent + 1);
249 if (gdsk.gdev != NULL) {
250 if (geli_havekey(gdsk.gdev) != 0 &&
251 geli_passphrase(gdsk.gdev, gelipw) != 0) {
252 printf("%s: unable to decrypt GELI key\n", BOOTPROG);
267 char cmd[512], cmdtmp[512];
269 int autoboot, dskupdated;
272 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
276 if (high_heap_size > 0) {
277 heap_end = PTOV(high_heap_base + high_heap_size);
278 heap_next = PTOV(high_heap_base);
280 heap_next = (char *)dmadat + sizeof(*dmadat);
281 heap_end = (char *)PTOV(bios_basemem);
283 setheap(heap_next, heap_end);
286 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
287 gdsk.dsk.drive = *(uint8_t *)PTOV(ARGS);
288 gdsk.dsk.type = gdsk.dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
289 gdsk.dsk.unit = gdsk.dsk.drive & DRV_MASK;
292 bootinfo.bi_version = BOOTINFO_VERSION;
293 bootinfo.bi_size = sizeof(bootinfo);
294 bootinfo.bi_basemem = bios_basemem / 1024;
295 bootinfo.bi_extmem = bios_extmem / 1024;
296 bootinfo.bi_memsizes_valid++;
297 bootinfo.bi_bios_dev = gdsk.dsk.drive;
299 /* Process configuration file */
309 if ((ino = lookup(PATH_CONFIG)) ||
310 (ino = lookup(PATH_DOTCONFIG))) {
311 sz = fsread(ino, cmd, sizeof(cmd) - 1);
312 cmd[(sz < 0) ? 0 : sz] = '\0';
315 memcpy(cmdtmp, cmd, sizeof(cmdtmp));
316 if (parse_cmds(cmdtmp, &dskupdated))
318 if (dskupdated && gptinit() != 0)
320 if (!OPT_CHECK(RBX_QUIET))
321 printf("%s: %s", PATH_CONFIG, cmd);
325 if (autoboot && keyhit(3)) {
327 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
333 * Try to exec stage 3 boot loader. If interrupted by a
334 * keypress, or in case of failure, try to load a kernel
339 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
341 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
343 gptbootfailed(&gdsk.dsk);
344 if (gptfind(&freebsd_ufs_uuid, &gdsk.dsk, -1) == -1)
349 /* Present the user with the boot2 prompt. */
352 if (!OPT_CHECK(RBX_QUIET)) {
353 printf("\nFreeBSD/x86 boot\n"
354 "Default: %u:%s(%up%u)%s\n"
356 gdsk.dsk.drive & DRV_MASK, dev_nm[gdsk.dsk.type],
357 gdsk.dsk.unit, gdsk.dsk.part, kname);
359 if (ioctrl & IO_SERIAL)
363 getstr(cmd, sizeof(cmd));
364 else if (!OPT_CHECK(RBX_QUIET))
366 if (parse_cmds(cmd, &dskupdated)) {
370 if (dskupdated && gptinit() != 0)
377 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
393 static Elf32_Phdr ep[2];
394 static Elf32_Shdr es[2];
400 if (!(ino = lookup(kname))) {
402 printf("%s: No %s on %u:%s(%up%u)\n", BOOTPROG,
403 kname, gdsk.dsk.drive & DRV_MASK,
404 dev_nm[gdsk.dsk.type], gdsk.dsk.unit,
409 if (xfsread(ino, &hdr, sizeof(hdr)))
411 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
413 else if (IS_ELF(hdr.eh))
416 printf("Invalid %s\n", "format");
420 addr = hdr.ex.a_entry & 0xffffff;
423 if (xfsread(ino, p, hdr.ex.a_text))
425 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
426 if (xfsread(ino, p, hdr.ex.a_data))
428 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
429 bootinfo.bi_symtab = VTOP(p);
430 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
431 p += sizeof(hdr.ex.a_syms);
433 if (xfsread(ino, p, hdr.ex.a_syms))
436 if (xfsread(ino, p, sizeof(int)))
441 if (xfsread(ino, p, x))
446 fs_off = hdr.eh.e_phoff;
447 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
448 if (xfsread(ino, ep + j, sizeof(ep[0])))
450 if (ep[j].p_type == PT_LOAD)
453 for (i = 0; i < 2; i++) {
454 p = PTOV(ep[i].p_paddr & 0xffffff);
455 fs_off = ep[i].p_offset;
456 if (xfsread(ino, p, ep[i].p_filesz))
459 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
460 bootinfo.bi_symtab = VTOP(p);
461 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
462 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
463 (hdr.eh.e_shstrndx + 1);
464 if (xfsread(ino, &es, sizeof(es)))
466 for (i = 0; i < 2; i++) {
467 memcpy(p, &es[i].sh_size,
468 sizeof(es[i].sh_size));
469 p += sizeof(es[i].sh_size);
470 fs_off = es[i].sh_offset;
471 if (xfsread(ino, p, es[i].sh_size))
476 addr = hdr.eh.e_entry & 0xffffff;
478 bootinfo.bi_esymtab = VTOP(p);
479 bootinfo.bi_kernelname = VTOP(kname);
480 bootinfo.bi_bios_dev = gdsk.dsk.drive;
481 #ifdef LOADER_GELI_SUPPORT
482 geliargs.size = sizeof(geliargs);
483 explicit_bzero(gelipw, sizeof(gelipw));
484 gelibuf = malloc(sizeof(struct keybuf) +
485 (GELI_MAX_KEYS * sizeof(struct keybuf_ent)));
486 geli_export_key_buffer(gelibuf);
487 geliargs.notapw = '\0';
488 geliargs.keybuf_sentinel = KEYBUF_SENTINEL;
489 geliargs.keybuf = gelibuf;
491 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
492 MAKEBOOTDEV(dev_maj[gdsk.dsk.type], gdsk.dsk.part + 1, gdsk.dsk.unit, 0xff),
493 #ifdef LOADER_GELI_SUPPORT
494 KARGS_FLAGS_EXTARG, 0, 0, VTOP(&bootinfo), geliargs
496 0, 0, 0, VTOP(&bootinfo)
502 parse_cmds(char *cmdstr, int *dskupdated)
512 while ((c = *arg++)) {
513 if (c == ' ' || c == '\t' || c == '\n')
515 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
520 while ((c = *arg++)) {
522 if (*(uint8_t *)PTOV(0x496) & 0x10) {
525 opts |= OPT_SET(RBX_DUAL) |
529 printf("Keyboard: %s\n", cp);
531 } else if (c == 'S') {
533 while ((unsigned int)(i = *arg++ - '0')
536 if (j > 0 && i == -'0') {
541 * Fall through to error below
542 * ('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(&gdsk.dsk, buf, lba + gdsk.dsk.start, nblk);
609 #ifdef LOADER_GELI_SUPPORT
610 if (err == 0 && gdsk.gdev != NULL) {
612 if (geli_read(gdsk.gdev, lba * DEV_BSIZE, buf,
621 #ifdef LOADER_GELI_SUPPORT
623 * Read function compatible with the ZFS callback, required to keep the GELI
624 * implementation the same for both UFS and ZFS.
627 vdev_read(void *vdev __unused, void *priv, off_t off, void *buf, size_t bytes)
634 dskp = (struct gptdsk *)priv;
636 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
640 lba = off / DEV_BSIZE;
641 lba += dskp->dsk.start;
644 nb = bytes / DEV_BSIZE;
645 if (nb > VBLKSIZE / DEV_BSIZE)
646 nb = VBLKSIZE / DEV_BSIZE;
647 if (drvread(&dskp->dsk, dmadat->blkbuf, lba, nb))
649 memcpy(p, dmadat->blkbuf, nb * DEV_BSIZE);
652 bytes -= nb * DEV_BSIZE;
657 #endif /* LOADER_GELI_SUPPORT */