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 dskread(void *, daddr_t, unsigned);
94 static uint32_t memsize(void);
99 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
102 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
103 printf("Invalid %s\n", "format");
109 static inline uint32_t
123 if (gptread(&freebsd_ufs_uuid, &dsk, dmadat->secbuf) == -1) {
124 printf("%s: unable to load GPT\n", BOOTPROG);
127 if (gptfind(&freebsd_ufs_uuid, &dsk, dsk.part) == -1) {
128 printf("%s: no UFS partition was found\n", BOOTPROG);
138 char cmd[512], cmdtmp[512];
139 int autoboot, dskupdated;
142 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
144 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
145 dsk.drive = *(uint8_t *)PTOV(ARGS);
146 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
147 dsk.unit = dsk.drive & DRV_MASK;
150 bootinfo.bi_version = BOOTINFO_VERSION;
151 bootinfo.bi_size = sizeof(bootinfo);
152 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
153 bootinfo.bi_extmem = memsize();
154 bootinfo.bi_memsizes_valid++;
156 /* Process configuration file */
166 if ((ino = lookup(PATH_CONFIG)) ||
167 (ino = lookup(PATH_DOTCONFIG)))
168 fsread(ino, cmd, sizeof(cmd));
171 memcpy(cmdtmp, cmd, sizeof(cmdtmp));
172 if (parse(cmdtmp, &dskupdated))
174 if (dskupdated && gptinit() != 0)
176 if (!OPT_CHECK(RBX_QUIET))
177 printf("%s: %s", PATH_CONFIG, cmd);
181 if (autoboot && keyhit(3)) {
183 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
189 * Try to exec stage 3 boot loader. If interrupted by a
190 * keypress, or in case of failure, try to load a kernel
195 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
197 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
200 if (gptfind(&freebsd_ufs_uuid, &dsk, -1) == -1)
205 /* Present the user with the boot2 prompt. */
208 if (!OPT_CHECK(RBX_QUIET)) {
209 printf("\nFreeBSD/x86 boot\n"
210 "Default: %u:%s(%up%u)%s\n"
212 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
215 if (ioctrl & IO_SERIAL)
219 getstr(cmd, sizeof(cmd));
220 else if (!OPT_CHECK(RBX_QUIET))
222 if (parse(cmd, &dskupdated)) {
226 if (dskupdated && gptinit() != 0)
233 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
246 static Elf32_Phdr ep[2];
247 static Elf32_Shdr es[2];
253 if (!(ino = lookup(kname))) {
255 printf("%s: No %s on %u:%s(%up%u)\n", BOOTPROG,
256 kname, dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
261 if (xfsread(ino, &hdr, sizeof(hdr)))
263 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
265 else if (IS_ELF(hdr.eh))
268 printf("Invalid %s\n", "format");
272 addr = hdr.ex.a_entry & 0xffffff;
275 if (xfsread(ino, p, hdr.ex.a_text))
277 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
278 if (xfsread(ino, p, hdr.ex.a_data))
280 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
281 bootinfo.bi_symtab = VTOP(p);
282 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
283 p += sizeof(hdr.ex.a_syms);
285 if (xfsread(ino, p, hdr.ex.a_syms))
288 if (xfsread(ino, p, sizeof(int)))
293 if (xfsread(ino, p, x))
298 fs_off = hdr.eh.e_phoff;
299 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
300 if (xfsread(ino, ep + j, sizeof(ep[0])))
302 if (ep[j].p_type == PT_LOAD)
305 for (i = 0; i < 2; i++) {
306 p = PTOV(ep[i].p_paddr & 0xffffff);
307 fs_off = ep[i].p_offset;
308 if (xfsread(ino, p, ep[i].p_filesz))
311 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
312 bootinfo.bi_symtab = VTOP(p);
313 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
314 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
315 (hdr.eh.e_shstrndx + 1);
316 if (xfsread(ino, &es, sizeof(es)))
318 for (i = 0; i < 2; i++) {
319 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
320 p += sizeof(es[i].sh_size);
321 fs_off = es[i].sh_offset;
322 if (xfsread(ino, p, es[i].sh_size))
327 addr = hdr.eh.e_entry & 0xffffff;
329 bootinfo.bi_esymtab = VTOP(p);
330 bootinfo.bi_kernelname = VTOP(kname);
331 bootinfo.bi_bios_dev = dsk.drive;
332 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
333 MAKEBOOTDEV(dev_maj[dsk.type], dsk.part + 1, dsk.unit, 0xff),
334 0, 0, 0, VTOP(&bootinfo));
338 parse(char *cmdstr, int *dskupdated)
347 while ((c = *arg++)) {
348 if (c == ' ' || c == '\t' || c == '\n')
350 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
355 while ((c = *arg++)) {
357 if (*(uint8_t *)PTOV(0x496) & 0x10) {
360 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
363 printf("Keyboard: %s\n", cp);
365 } else if (c == 'S') {
367 while ((unsigned int)(i = *arg++ - '0') <= 9)
369 if (j > 0 && i == -'0') {
373 /* Fall through to error below ('S' not in optstr[]). */
375 for (i = 0; c != optstr[i]; i++)
378 opts ^= OPT_SET(flags[i]);
380 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
381 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
382 if (ioctrl & IO_SERIAL) {
383 if (sio_init(115200 / comspeed) != 0)
384 ioctrl &= ~IO_SERIAL;
387 for (q = arg--; *q && *q != '('; q++);
398 for (i = 0; arg[0] != dev_nm[i][0] ||
399 arg[1] != dev_nm[i][1]; i++)
404 dsk.unit = *arg - '0';
405 if (arg[1] != 'p' || dsk.unit > 9)
408 dsk.part = *arg - '0';
409 if (dsk.part < 1 || dsk.part > 9)
417 dsk.drive = (dsk.type <= TYPE_MAXHARD
418 ? DRV_HARD : 0) + drv;
421 if ((i = ep - arg)) {
422 if ((size_t)i >= sizeof(kname))
424 memcpy(kname, arg, i + 1);
433 dskread(void *buf, daddr_t lba, unsigned nblk)
436 return drvread(&dsk, buf, lba + dsk.start, nblk);