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>
20 #include <sys/disklabel.h>
21 #include <sys/diskmbr.h>
22 #include <sys/dirent.h>
23 #include <sys/reboot.h>
25 #include <machine/bootinfo.h>
26 #include <machine/elf.h>
40 #define SECOND 18 /* Circa that many ticks in a second. */
42 #define RBX_ASKNAME 0x0 /* -a */
43 #define RBX_SINGLE 0x1 /* -s */
44 /* 0x2 is reserved for log2(RB_NOSYNC). */
45 /* 0x3 is reserved for log2(RB_HALT). */
46 /* 0x4 is reserved for log2(RB_INITNAME). */
47 #define RBX_DFLTROOT 0x5 /* -r */
48 #define RBX_KDB 0x6 /* -d */
49 /* 0x7 is reserved for log2(RB_RDONLY). */
50 /* 0x8 is reserved for log2(RB_DUMP). */
51 /* 0x9 is reserved for log2(RB_MINIROOT). */
52 #define RBX_CONFIG 0xa /* -c */
53 #define RBX_VERBOSE 0xb /* -v */
54 #define RBX_SERIAL 0xc /* -h */
55 #define RBX_CDROM 0xd /* -C */
56 /* 0xe is reserved for log2(RB_POWEROFF). */
57 #define RBX_GDB 0xf /* -g */
58 #define RBX_MUTE 0x10 /* -m */
59 /* 0x11 is reserved for log2(RB_SELFTEST). */
60 /* 0x12 is reserved for boot programs. */
61 /* 0x13 is reserved for boot programs. */
62 #define RBX_PAUSE 0x14 /* -p */
63 #define RBX_QUIET 0x15 /* -q */
64 #define RBX_NOINTR 0x1c /* -n */
65 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
66 #define RBX_DUAL 0x1d /* -D */
67 /* 0x1f is reserved for log2(RB_BOOTINFO). */
69 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -D */
70 #define RBX_MASK (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
71 OPT_SET(RBX_DFLTROOT) | OPT_SET(RBX_KDB ) | \
72 OPT_SET(RBX_CONFIG) | OPT_SET(RBX_VERBOSE) | \
73 OPT_SET(RBX_SERIAL) | OPT_SET(RBX_CDROM) | \
74 OPT_SET(RBX_GDB ) | OPT_SET(RBX_MUTE) | \
75 OPT_SET(RBX_PAUSE) | OPT_SET(RBX_DUAL))
77 #define PATH_DOTCONFIG "/boot.config"
78 #define PATH_CONFIG "/boot/config"
79 #define PATH_BOOT3 "/boot/loader"
80 #define PATH_KERNEL "/boot/kernel/kernel"
93 #define TYPE_MAXHARD TYPE_DA
96 #define OPT_SET(opt) (1 << (opt))
97 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
101 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
102 static const unsigned char flags[NOPT] = {
119 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
120 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
131 static char cmd[512], cmddup[512], knamebuf[1024];
132 static const char *kname;
133 static uint32_t opts;
134 static int comspeed = SIOSPD;
135 static struct bootinfo bootinfo;
136 static uint8_t ioctrl = IO_KEYBOARD;
139 static void load(void);
140 static int parse(void);
141 static int dskread(void *, unsigned, unsigned);
142 static void printf(const char *,...);
143 static void putchar(int);
144 static int drvread(void *, unsigned, unsigned);
145 static int keyhit(unsigned);
146 static int xputc(int);
147 static int xgetc(int);
148 static inline int getc(int);
150 static void memcpy(void *, const void *, int);
152 memcpy(void *dst, const void *src, int len)
162 strcmp(const char *s1, const char *s2)
164 for (; *s1 == *s2 && *s1; s1++, s2++);
165 return (unsigned char)*s1 - (unsigned char)*s2;
168 #define UFS_SMALL_CGBASE
172 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
174 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
175 printf("Invalid %s\n", "format");
189 switch (c = xgetc(0)) {
204 if (s - cmd < sizeof(cmd) - 1)
215 v86.eax = 0xe00 | (c & 0xff);
227 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
229 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
230 dsk.drive = *(uint8_t *)PTOV(ARGS);
231 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
232 dsk.unit = dsk.drive & DRV_MASK;
233 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
234 bootinfo.bi_version = BOOTINFO_VERSION;
235 bootinfo.bi_size = sizeof(bootinfo);
237 /* Process configuration file */
241 if ((ino = lookup(PATH_CONFIG)) ||
242 (ino = lookup(PATH_DOTCONFIG))) {
243 nbyte = fsread(ino, cmd, sizeof(cmd) - 1);
248 memcpy(cmddup, cmd, sizeof(cmd));
251 if (!OPT_CHECK(RBX_QUIET))
252 printf("%s: %s", PATH_CONFIG, cmddup);
253 /* Do not process this command twice */
258 * Try to exec stage 3 boot loader. If interrupted by a keypress,
259 * or in case of failure, try to load a kernel directly instead.
264 if (autoboot && !keyhit(3*SECOND)) {
270 /* Present the user with the boot2 prompt. */
273 if (!autoboot || !OPT_CHECK(RBX_QUIET))
274 printf("\nFreeBSD/x86 boot\n"
275 "Default: %u:%s(%u,%c)%s\n"
277 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
278 'a' + dsk.part, kname);
279 if (ioctrl & IO_SERIAL)
281 if (!autoboot || keyhit(3*SECOND))
283 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
293 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
306 static Elf32_Phdr ep[2];
307 static Elf32_Shdr es[2];
313 if (!(ino = lookup(kname))) {
315 printf("No %s\n", kname);
318 if (xfsread(ino, &hdr, sizeof(hdr)))
321 if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
322 addr = hdr.ex.a_entry & 0xffffff;
325 if (xfsread(ino, p, hdr.ex.a_text))
327 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
328 if (xfsread(ino, p, hdr.ex.a_data))
330 } else if (IS_ELF(hdr.eh)) {
331 fs_off = hdr.eh.e_phoff;
332 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
333 if (xfsread(ino, ep + j, sizeof(ep[0])))
335 if (ep[j].p_type == PT_LOAD)
338 for (i = 0; i < 2; i++) {
339 p = PTOV(ep[i].p_paddr & 0xffffff);
340 fs_off = ep[i].p_offset;
341 if (xfsread(ino, p, ep[i].p_filesz))
344 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
345 bootinfo.bi_symtab = VTOP(p);
346 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
347 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
348 (hdr.eh.e_shstrndx + 1);
349 if (xfsread(ino, &es, sizeof(es)))
351 for (i = 0; i < 2; i++) {
352 *(Elf32_Word *)p = es[i].sh_size;
353 p += sizeof(es[i].sh_size);
354 fs_off = es[i].sh_offset;
355 if (xfsread(ino, p, es[i].sh_size))
360 addr = hdr.eh.e_entry & 0xffffff;
361 bootinfo.bi_esymtab = VTOP(p);
363 printf("Invalid %s\n", "format");
367 bootinfo.bi_kernelname = VTOP(kname);
368 bootinfo.bi_bios_dev = dsk.drive;
369 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
370 MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
371 0, 0, 0, VTOP(&bootinfo));
383 while ((c = *arg++)) {
384 if (c == ' ' || c == '\t' || c == '\n')
386 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
391 while ((c = *arg++)) {
393 if (*(uint8_t *)PTOV(0x496) & 0x10) {
396 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
399 printf("Keyboard: %s\n", cp);
401 } else if (c == 'S') {
403 while ((unsigned int)(i = *arg++ - '0') <= 9)
405 if (j > 0 && i == -'0') {
409 /* Fall through to error below ('S' not in optstr[]). */
411 for (i = 0; c != optstr[i]; i++)
414 opts ^= OPT_SET(flags[i]);
416 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
417 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
418 if (ioctrl & IO_SERIAL) {
419 if (sio_init(115200 / comspeed) != 0)
420 ioctrl &= ~IO_SERIAL;
423 for (q = arg--; *q && *q != '('; q++);
434 for (i = 0; arg[0] != dev_nm[i][0] ||
435 arg[1] != dev_nm[i][1]; i++)
440 dsk.unit = *arg - '0';
441 if (arg[1] != ',' || dsk.unit > 9)
444 dsk.slice = WHOLE_DISK_SLICE;
446 dsk.slice = *arg - '0' + 1;
447 if (dsk.slice > NDOSPART + 1)
453 dsk.part = *arg - 'a';
459 dsk.drive = (dsk.type <= TYPE_MAXHARD
460 ? DRV_HARD : 0) + drv;
463 if ((i = ep - arg)) {
464 if ((size_t)i >= sizeof(knamebuf))
466 memcpy(knamebuf, arg, i + 1);
476 dskread(void *buf, unsigned lba, unsigned nblk)
478 struct dos_partition *dp;
485 sec = dmadat->secbuf;
487 if (drvread(sec, DOSBBSECTOR, 1))
489 dp = (void *)(sec + DOSPARTOFF);
491 if (sl < BASE_SLICE) {
492 for (i = 0; i < NDOSPART; i++)
493 if (dp[i].dp_typ == DOSPTYP_386BSD &&
494 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
496 if (dp[i].dp_flag & 0x80 ||
497 dsk.slice == COMPATIBILITY_SLICE)
500 if (dsk.slice == WHOLE_DISK_SLICE)
503 if (sl != WHOLE_DISK_SLICE) {
504 if (sl != COMPATIBILITY_SLICE)
505 dp += sl - BASE_SLICE;
506 if (dp->dp_typ != DOSPTYP_386BSD) {
507 printf("Invalid %s\n", "slice");
510 dsk.start = dp->dp_start;
512 if (drvread(sec, dsk.start + LABELSECTOR, 1))
514 d = (void *)(sec + LABELOFFSET);
515 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
516 if (dsk.part != RAW_PART) {
517 printf("Invalid %s\n", "label");
522 if (d->d_type == DTYPE_SCSI)
526 if (dsk.part >= d->d_npartitions ||
527 !d->d_partitions[dsk.part].p_size) {
528 printf("Invalid %s\n", "partition");
531 dsk.start += d->d_partitions[dsk.part].p_offset;
532 dsk.start -= d->d_partitions[RAW_PART].p_offset;
535 return drvread(buf, dsk.start + lba, nblk);
539 printf(const char *fmt,...)
548 while ((c = *fmt++)) {
553 putchar(va_arg(ap, int));
556 for (s = va_arg(ap, char *); *s; s++)
560 u = va_arg(ap, unsigned);
563 *s++ = '0' + u % 10U;
585 drvread(void *buf, unsigned lba, unsigned nblk)
587 static unsigned c = 0x2d5c7c2f;
589 if (!OPT_CHECK(RBX_QUIET))
590 printf("%c\b", c = c << 8 | c >> 24);
591 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
592 v86.addr = XREADORG; /* call to xread in boot1 */
593 v86.es = VTOPSEG(buf);
595 v86.ebx = VTOPOFF(buf);
597 v86.edx = nblk << 8 | dsk.drive;
600 if (V86_CY(v86.efl)) {
601 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
608 keyhit(unsigned ticks)
612 if (OPT_CHECK(RBX_NOINTR))
618 t1 = *(uint32_t *)PTOV(0x46c);
621 if ((uint32_t)(t1 - t0) >= ticks)
629 if (ioctrl & IO_KEYBOARD)
631 if (ioctrl & IO_SERIAL)
642 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
648 if (OPT_CHECK(RBX_NOINTR))
651 if (ioctrl & IO_KEYBOARD && getc(1))
652 return fn ? 1 : getc(0);
653 if (ioctrl & IO_SERIAL && sio_ischar())
654 return fn ? 1 : sio_getc();