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/psl.h>
41 #define PATH_DOTCONFIG "/boot.config"
42 #define PATH_CONFIG "/boot/config"
43 #define PATH_BOOT3 "/boot/loader"
44 #define PATH_KERNEL "/boot/kernel/kernel"
57 #define TYPE_MAXHARD TYPE_DA
62 static const uuid_t freebsd_ufs_uuid = GPT_ENT_TYPE_FREEBSD_UFS;
63 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
64 static const unsigned char flags[NOPT] = {
82 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
83 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
85 static struct dsk dsk;
86 static char kname[1024];
87 static int comspeed = SIOSPD;
88 static struct bootinfo bootinfo;
91 static void load(void);
92 static int parse(char *, int *);
93 static int xfsread(ino_t, void *, size_t);
94 static int dskread(void *, daddr_t, unsigned);
95 static uint32_t memsize(void);
100 xfsread(ino_t inode, void *buf, size_t nbyte)
103 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
104 printf("Invalid %s\n", "format");
110 static inline uint32_t
124 if (gptread(&freebsd_ufs_uuid, &dsk, dmadat->secbuf) == -1) {
125 printf("%s: unable to load GPT\n", BOOTPROG);
128 if (gptfind(&freebsd_ufs_uuid, &dsk, dsk.part) == -1) {
129 printf("%s: no UFS partition was found\n", BOOTPROG);
139 char cmd[512], cmdtmp[512];
140 int autoboot, dskupdated;
143 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
145 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
146 dsk.drive = *(uint8_t *)PTOV(ARGS);
147 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
148 dsk.unit = dsk.drive & DRV_MASK;
151 bootinfo.bi_version = BOOTINFO_VERSION;
152 bootinfo.bi_size = sizeof(bootinfo);
153 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
154 bootinfo.bi_extmem = memsize();
155 bootinfo.bi_memsizes_valid++;
157 /* Process configuration file */
167 if ((ino = lookup(PATH_CONFIG)) ||
168 (ino = lookup(PATH_DOTCONFIG)))
169 fsread(ino, cmd, sizeof(cmd));
172 memcpy(cmdtmp, cmd, sizeof(cmdtmp));
173 if (parse(cmdtmp, &dskupdated))
175 if (dskupdated && gptinit() != 0)
177 if (!OPT_CHECK(RBX_QUIET))
178 printf("%s: %s", PATH_CONFIG, cmd);
182 if (autoboot && keyhit(3)) {
184 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
190 * Try to exec stage 3 boot loader. If interrupted by a
191 * keypress, or in case of failure, try to load a kernel
196 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
198 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
201 if (gptfind(&freebsd_ufs_uuid, &dsk, -1) == -1)
206 /* Present the user with the boot2 prompt. */
209 if (!OPT_CHECK(RBX_QUIET)) {
210 printf("\nFreeBSD/x86 boot\n"
211 "Default: %u:%s(%up%u)%s\n"
213 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
216 if (ioctrl & IO_SERIAL)
220 getstr(cmd, sizeof(cmd));
221 else if (!OPT_CHECK(RBX_QUIET))
223 if (parse(cmd, &dskupdated)) {
227 if (dskupdated && gptinit() != 0)
234 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
247 static Elf32_Phdr ep[2];
248 static Elf32_Shdr es[2];
254 if (!(ino = lookup(kname))) {
256 printf("%s: No %s on %u:%s(%up%u)\n", BOOTPROG,
257 kname, dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
262 if (xfsread(ino, &hdr, sizeof(hdr)))
264 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
266 else if (IS_ELF(hdr.eh))
269 printf("Invalid %s\n", "format");
273 addr = hdr.ex.a_entry & 0xffffff;
276 if (xfsread(ino, p, hdr.ex.a_text))
278 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
279 if (xfsread(ino, p, hdr.ex.a_data))
281 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
282 bootinfo.bi_symtab = VTOP(p);
283 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
284 p += sizeof(hdr.ex.a_syms);
286 if (xfsread(ino, p, hdr.ex.a_syms))
289 if (xfsread(ino, p, sizeof(int)))
294 if (xfsread(ino, p, x))
299 fs_off = hdr.eh.e_phoff;
300 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
301 if (xfsread(ino, ep + j, sizeof(ep[0])))
303 if (ep[j].p_type == PT_LOAD)
306 for (i = 0; i < 2; i++) {
307 p = PTOV(ep[i].p_paddr & 0xffffff);
308 fs_off = ep[i].p_offset;
309 if (xfsread(ino, p, ep[i].p_filesz))
312 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
313 bootinfo.bi_symtab = VTOP(p);
314 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
315 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
316 (hdr.eh.e_shstrndx + 1);
317 if (xfsread(ino, &es, sizeof(es)))
319 for (i = 0; i < 2; i++) {
320 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
321 p += sizeof(es[i].sh_size);
322 fs_off = es[i].sh_offset;
323 if (xfsread(ino, p, es[i].sh_size))
328 addr = hdr.eh.e_entry & 0xffffff;
330 bootinfo.bi_esymtab = VTOP(p);
331 bootinfo.bi_kernelname = VTOP(kname);
332 bootinfo.bi_bios_dev = dsk.drive;
333 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
334 MAKEBOOTDEV(dev_maj[dsk.type], dsk.part + 1, dsk.unit, 0xff),
335 0, 0, 0, VTOP(&bootinfo));
339 parse(char *cmdstr, int *dskupdated)
348 while ((c = *arg++)) {
349 if (c == ' ' || c == '\t' || c == '\n')
351 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
356 while ((c = *arg++)) {
358 if (*(uint8_t *)PTOV(0x496) & 0x10) {
361 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
364 printf("Keyboard: %s\n", cp);
366 } else if (c == 'S') {
368 while ((unsigned int)(i = *arg++ - '0') <= 9)
370 if (j > 0 && i == -'0') {
374 /* Fall through to error below ('S' not in optstr[]). */
376 for (i = 0; c != optstr[i]; i++)
379 opts ^= OPT_SET(flags[i]);
381 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
382 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
383 if (ioctrl & IO_SERIAL) {
384 if (sio_init(115200 / comspeed) != 0)
385 ioctrl &= ~IO_SERIAL;
388 for (q = arg--; *q && *q != '('; q++);
399 for (i = 0; arg[0] != dev_nm[i][0] ||
400 arg[1] != dev_nm[i][1]; i++)
405 dsk.unit = *arg - '0';
406 if (arg[1] != 'p' || dsk.unit > 9)
409 dsk.part = *arg - '0';
410 if (dsk.part < 1 || dsk.part > 9)
418 dsk.drive = (dsk.type <= TYPE_MAXHARD
419 ? DRV_HARD : 0) + drv;
422 if ((i = ep - arg)) {
423 if ((size_t)i >= sizeof(kname))
425 memcpy(kname, arg, i + 1);
434 dskread(void *buf, daddr_t lba, unsigned nblk)
437 return drvread(&dsk, buf, lba + dsk.start, nblk);