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>
53 #define TYPE_MAXHARD TYPE_DA
58 static const uuid_t freebsd_ufs_uuid = GPT_ENT_TYPE_FREEBSD_UFS;
59 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
60 static const unsigned char flags[NOPT] = {
78 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
79 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
81 static struct dsk dsk;
82 static char kname[1024];
83 static int comspeed = SIOSPD;
84 static struct bootinfo bootinfo;
87 static void load(void);
88 static int parse(char *, int *);
89 static int dskread(void *, daddr_t, unsigned);
90 static uint32_t memsize(void);
95 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
98 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
99 printf("Invalid %s\n", "format");
105 static inline uint32_t
119 if (gptread(&freebsd_ufs_uuid, &dsk, dmadat->secbuf) == -1) {
120 printf("%s: unable to load GPT\n", BOOTPROG);
123 if (gptfind(&freebsd_ufs_uuid, &dsk, dsk.part) == -1) {
124 printf("%s: no UFS partition was found\n", BOOTPROG);
134 char cmd[512], cmdtmp[512];
136 int autoboot, dskupdated;
139 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
141 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
142 dsk.drive = *(uint8_t *)PTOV(ARGS);
143 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
144 dsk.unit = dsk.drive & DRV_MASK;
147 bootinfo.bi_version = BOOTINFO_VERSION;
148 bootinfo.bi_size = sizeof(bootinfo);
149 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
150 bootinfo.bi_extmem = memsize();
151 bootinfo.bi_memsizes_valid++;
153 /* Process configuration file */
163 if ((ino = lookup(PATH_CONFIG)) ||
164 (ino = lookup(PATH_DOTCONFIG))) {
165 sz = fsread(ino, cmd, sizeof(cmd) - 1);
166 cmd[(sz < 0) ? 0 : sz] = '\0';
169 memcpy(cmdtmp, cmd, sizeof(cmdtmp));
170 if (parse(cmdtmp, &dskupdated))
172 if (dskupdated && gptinit() != 0)
174 if (!OPT_CHECK(RBX_QUIET))
175 printf("%s: %s", PATH_CONFIG, cmd);
179 if (autoboot && keyhit(3)) {
181 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
187 * Try to exec stage 3 boot loader. If interrupted by a
188 * keypress, or in case of failure, try to load a kernel
193 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
195 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
198 if (gptfind(&freebsd_ufs_uuid, &dsk, -1) == -1)
203 /* Present the user with the boot2 prompt. */
206 if (!OPT_CHECK(RBX_QUIET)) {
207 printf("\nFreeBSD/x86 boot\n"
208 "Default: %u:%s(%up%u)%s\n"
210 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
213 if (ioctrl & IO_SERIAL)
217 getstr(cmd, sizeof(cmd));
218 else if (!OPT_CHECK(RBX_QUIET))
220 if (parse(cmd, &dskupdated)) {
224 if (dskupdated && gptinit() != 0)
231 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
244 static Elf32_Phdr ep[2];
245 static Elf32_Shdr es[2];
251 if (!(ino = lookup(kname))) {
253 printf("%s: No %s on %u:%s(%up%u)\n", BOOTPROG,
254 kname, dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
259 if (xfsread(ino, &hdr, sizeof(hdr)))
261 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
263 else if (IS_ELF(hdr.eh))
266 printf("Invalid %s\n", "format");
270 addr = hdr.ex.a_entry & 0xffffff;
273 if (xfsread(ino, p, hdr.ex.a_text))
275 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
276 if (xfsread(ino, p, hdr.ex.a_data))
278 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
279 bootinfo.bi_symtab = VTOP(p);
280 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
281 p += sizeof(hdr.ex.a_syms);
283 if (xfsread(ino, p, hdr.ex.a_syms))
286 if (xfsread(ino, p, sizeof(int)))
291 if (xfsread(ino, p, x))
296 fs_off = hdr.eh.e_phoff;
297 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
298 if (xfsread(ino, ep + j, sizeof(ep[0])))
300 if (ep[j].p_type == PT_LOAD)
303 for (i = 0; i < 2; i++) {
304 p = PTOV(ep[i].p_paddr & 0xffffff);
305 fs_off = ep[i].p_offset;
306 if (xfsread(ino, p, ep[i].p_filesz))
309 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
310 bootinfo.bi_symtab = VTOP(p);
311 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
312 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
313 (hdr.eh.e_shstrndx + 1);
314 if (xfsread(ino, &es, sizeof(es)))
316 for (i = 0; i < 2; i++) {
317 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
318 p += sizeof(es[i].sh_size);
319 fs_off = es[i].sh_offset;
320 if (xfsread(ino, p, es[i].sh_size))
325 addr = hdr.eh.e_entry & 0xffffff;
327 bootinfo.bi_esymtab = VTOP(p);
328 bootinfo.bi_kernelname = VTOP(kname);
329 bootinfo.bi_bios_dev = dsk.drive;
330 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
331 MAKEBOOTDEV(dev_maj[dsk.type], dsk.part + 1, dsk.unit, 0xff),
332 0, 0, 0, VTOP(&bootinfo));
336 parse(char *cmdstr, int *dskupdated)
345 while ((c = *arg++)) {
346 if (c == ' ' || c == '\t' || c == '\n')
348 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
353 while ((c = *arg++)) {
355 if (*(uint8_t *)PTOV(0x496) & 0x10) {
358 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
361 printf("Keyboard: %s\n", cp);
363 } else if (c == 'S') {
365 while ((unsigned int)(i = *arg++ - '0') <= 9)
367 if (j > 0 && i == -'0') {
371 /* Fall through to error below ('S' not in optstr[]). */
373 for (i = 0; c != optstr[i]; i++)
376 opts ^= OPT_SET(flags[i]);
378 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
379 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
380 if (ioctrl & IO_SERIAL) {
381 if (sio_init(115200 / comspeed) != 0)
382 ioctrl &= ~IO_SERIAL;
385 for (q = arg--; *q && *q != '('; q++);
396 for (i = 0; arg[0] != dev_nm[i][0] ||
397 arg[1] != dev_nm[i][1]; i++)
402 dsk.unit = *arg - '0';
403 if (arg[1] != 'p' || dsk.unit > 9)
406 dsk.part = *arg - '0';
407 if (dsk.part < 1 || dsk.part > 9)
415 dsk.drive = (dsk.type <= TYPE_MAXHARD
416 ? DRV_HARD : 0) + drv;
419 if ((i = ep - arg)) {
420 if ((size_t)i >= sizeof(kname))
422 memcpy(kname, arg, i + 1);
431 dskread(void *buf, daddr_t lba, unsigned nblk)
434 return drvread(&dsk, buf, lba + dsk.start, nblk);