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 char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
62 static const unsigned char flags[NOPT] = {
80 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
81 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
83 static struct dsk dsk;
84 static char kname[1024];
85 static int comspeed = SIOSPD;
86 static struct bootinfo bootinfo;
88 static vm_offset_t high_heap_base;
89 static uint32_t bios_basemem, bios_extmem, high_heap_size;
91 static struct bios_smap smap;
94 * The minimum amount of memory to reserve in bios_extmem for the heap.
96 #define HEAP_MIN (3 * 1024 * 1024)
98 static char *heap_next;
99 static char *heap_end;
103 static void load(void);
104 static int parse_cmds(char *, int *);
106 static uint8_t ls, dsk_meta;
107 static uint32_t fs_off;
109 #include "cd9660read.c"
112 xfsread(uint64_t inode, void *buf, size_t nbyte)
115 if ((size_t)cd9660_fsread(inode, buf, nbyte) != nbyte) {
116 printf("Invalid %s\n", "format");
127 /* Parse system memory map */
131 v86.addr = MEM_EXT; /* int 0x15 function 0xe820*/
133 v86.ecx = sizeof(struct bios_smap);
135 v86.es = VTOPSEG(&smap);
136 v86.edi = VTOPOFF(&smap);
138 if ((v86.efl & 1) || (v86.eax != SMAP_SIG))
140 /* look for a low-memory segment that's large enough */
141 if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
142 (smap.length >= (512 * 1024)))
143 bios_basemem = smap.length;
144 /* look for the first segment in 'extended' memory */
145 if ((smap.type == SMAP_TYPE_MEMORY) &&
146 (smap.base == 0x100000)) {
147 bios_extmem = smap.length;
151 * Look for the largest segment in 'extended' memory beyond
154 if ((smap.type == SMAP_TYPE_MEMORY) &&
155 (smap.base > 0x100000) && (smap.base < 0x100000000ull)) {
159 * If this segment crosses the 4GB boundary,
162 if (smap.base + size > 0x100000000ull)
163 size = 0x100000000ull - smap.base;
165 if (size > high_heap_size) {
166 high_heap_size = size;
167 high_heap_base = smap.base;
170 } while (v86.ebx != 0);
172 /* Fall back to the old compatibility function for base memory */
173 if (bios_basemem == 0) {
175 v86.addr = 0x12; /* int 0x12 */
178 bios_basemem = (v86.eax & 0xffff) * 1024;
182 * Fall back through several compatibility functions for extended
185 if (bios_extmem == 0) {
187 v86.addr = 0x15; /* int 0x15 function 0xe801*/
190 if (!(v86.efl & 1)) {
191 bios_extmem = ((v86.ecx & 0xffff) +
192 ((v86.edx & 0xffff) * 64)) * 1024;
195 if (bios_extmem == 0) {
197 v86.addr = 0x15; /* int 0x15 function 0x88*/
200 bios_extmem = (v86.eax & 0xffff) * 1024;
204 * If we have extended memory and did not find a suitable heap
205 * region in the SMAP, use the last 3MB of 'extended' memory as a
206 * high heap candidate.
208 if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
209 high_heap_size = HEAP_MIN;
210 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
217 char cmd[512], cmdtmp[512];
219 int autoboot, dskupdated;
224 if (high_heap_size > 0) {
225 heap_end = PTOV(high_heap_base + high_heap_size);
226 heap_next = PTOV(high_heap_base);
229 (roundup2(__base + (int32_t)&_end, 0x10000) - __base);
230 heap_end = (char *)PTOV(bios_basemem);
232 setheap(heap_next, heap_end);
235 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
236 dsk.drive = *(uint8_t *)PTOV(ARGS);
237 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
238 dsk.unit = dsk.drive & DRV_MASK;
241 bootinfo.bi_version = BOOTINFO_VERSION;
242 bootinfo.bi_size = sizeof(bootinfo);
243 bootinfo.bi_basemem = bios_basemem / 1024;
244 bootinfo.bi_extmem = bios_extmem / 1024;
245 bootinfo.bi_memsizes_valid++;
246 bootinfo.bi_bios_dev = dsk.drive;
253 if ((ino = cd9660_lookup(PATH_CONFIG)) ||
254 (ino = cd9660_lookup(PATH_DOTCONFIG))) {
255 sz = cd9660_fsread(ino, cmd, sizeof(cmd) - 1);
256 cmd[(sz < 0) ? 0 : sz] = '\0';
259 memcpy(cmdtmp, cmd, sizeof(cmdtmp));
260 if (parse_cmds(cmdtmp, &dskupdated))
262 if (!OPT_CHECK(RBX_QUIET))
263 printf("%s: %s", PATH_CONFIG, cmd);
267 if (autoboot && keyhit(3)) {
269 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
275 * Try to exec stage 3 boot loader. If interrupted by a
276 * keypress, or in case of failure, try to load a kernel
281 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
283 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
288 /* Present the user with the boot2 prompt. */
291 if (!OPT_CHECK(RBX_QUIET)) {
292 printf("\nFreeBSD/x86 boot\n"
293 "Default: %u:%s(%up%u)%s\n"
295 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
298 if (ioctrl & IO_SERIAL)
302 getstr(cmd, sizeof(cmd));
303 else if (!OPT_CHECK(RBX_QUIET))
305 if (parse_cmds(cmd, &dskupdated)) {
314 /* Needed so btxld can link us properly; do not remove. */
330 static Elf32_Phdr ep[2];
331 static Elf32_Shdr es[2];
337 if (!(ino = cd9660_lookup(kname))) {
339 printf("%s: No %s on %u:%s(%up%u)\n", BOOTPROG,
340 kname, dsk.drive & DRV_MASK, dev_nm[dsk.type],
346 if (xfsread(ino, &hdr, sizeof(hdr)))
348 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
350 else if (IS_ELF(hdr.eh))
353 printf("Invalid %s\n", "format");
357 addr = hdr.ex.a_entry & 0xffffff;
360 if (xfsread(ino, p, hdr.ex.a_text))
362 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
363 if (xfsread(ino, p, hdr.ex.a_data))
365 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
366 bootinfo.bi_symtab = VTOP(p);
367 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
368 p += sizeof(hdr.ex.a_syms);
370 if (xfsread(ino, p, hdr.ex.a_syms))
373 if (xfsread(ino, p, sizeof(int)))
378 if (xfsread(ino, p, x))
383 fs_off = hdr.eh.e_phoff;
384 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
385 if (xfsread(ino, ep + j, sizeof(ep[0])))
387 if (ep[j].p_type == PT_LOAD)
390 for (i = 0; i < 2; i++) {
391 p = PTOV(ep[i].p_paddr & 0xffffff);
392 fs_off = ep[i].p_offset;
393 if (xfsread(ino, p, ep[i].p_filesz))
396 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
397 bootinfo.bi_symtab = VTOP(p);
398 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
399 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
400 (hdr.eh.e_shstrndx + 1);
401 if (xfsread(ino, &es, sizeof(es)))
403 for (i = 0; i < 2; i++) {
404 memcpy(p, &es[i].sh_size,
405 sizeof(es[i].sh_size));
406 p += sizeof(es[i].sh_size);
407 fs_off = es[i].sh_offset;
408 if (xfsread(ino, p, es[i].sh_size))
413 addr = hdr.eh.e_entry & 0xffffff;
415 bootinfo.bi_esymtab = VTOP(p);
416 bootinfo.bi_kernelname = VTOP(kname);
417 bootinfo.bi_bios_dev = dsk.drive;
418 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
419 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.unit, 0),
420 0, 0, 0, VTOP(&bootinfo));
424 parse_cmds(char *cmdstr, int *dskupdated)
434 while ((c = *arg++)) {
435 if (c == ' ' || c == '\t' || c == '\n')
437 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
442 while ((c = *arg++)) {
444 if (*(uint8_t *)PTOV(0x496) & 0x10) {
447 opts |= OPT_SET(RBX_DUAL) |
451 printf("Keyboard: %s\n", cp);
453 } else if (c == 'S') {
455 while ((unsigned int)(i = *arg++ - '0')
458 if (j > 0 && i == -'0') {
463 * Fall through to error below
464 * ('S' not in optstr[]).
467 for (i = 0; c != optstr[i]; i++)
470 opts ^= OPT_SET(flags[i]);
472 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
473 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
474 if (ioctrl & IO_SERIAL) {
475 if (sio_init(115200 / comspeed) != 0)
476 ioctrl &= ~IO_SERIAL;
479 for (q = arg--; *q && *q != '('; q++);
490 for (i = 0; arg[0] != dev_nm[i][0] ||
491 arg[1] != dev_nm[i][1]; i++)
496 dsk.unit = *arg - '0';
497 if (arg[1] != 'p' || dsk.unit > 9)
500 dsk.part = *arg - '0';
501 if (dsk.part < 1 || dsk.part > 9)
509 dsk.drive = (dsk.type <= TYPE_MAXHARD
510 ? DRV_HARD : 0) + drv;
513 if ((i = ep - arg)) {
514 if ((size_t)i >= sizeof(kname))
516 memcpy(kname, arg, i + 1);