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 xfsread(ino_t, void *, size_t);
142 static int dskread(void *, unsigned, unsigned);
143 static void printf(const char *,...);
144 static void putchar(int);
145 static int drvread(void *, unsigned, unsigned);
146 static int keyhit(unsigned);
147 static int xputc(int);
148 static int xgetc(int);
149 static inline int getc(int);
151 static void memcpy(void *, const void *, int);
153 memcpy(void *dst, const void *src, int len)
163 strcmp(const char *s1, const char *s2)
165 for (; *s1 == *s2 && *s1; s1++, s2++);
166 return (unsigned char)*s1 - (unsigned char)*s2;
169 #define UFS_SMALL_CGBASE
173 xfsread(ino_t inode, void *buf, size_t nbyte)
175 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
176 printf("Invalid %s\n", "format");
190 switch (c = xgetc(0)) {
205 if (s - cmd < sizeof(cmd) - 1)
216 v86.eax = 0xe00 | (c & 0xff);
228 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
230 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
231 dsk.drive = *(uint8_t *)PTOV(ARGS);
232 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
233 dsk.unit = dsk.drive & DRV_MASK;
234 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
235 bootinfo.bi_version = BOOTINFO_VERSION;
236 bootinfo.bi_size = sizeof(bootinfo);
238 /* Process configuration file */
242 if ((ino = lookup(PATH_CONFIG)) ||
243 (ino = lookup(PATH_DOTCONFIG))) {
244 nbyte = fsread(ino, cmd, sizeof(cmd) - 1);
249 memcpy(cmddup, cmd, sizeof(cmd));
252 if (!OPT_CHECK(RBX_QUIET))
253 printf("%s: %s", PATH_CONFIG, cmddup);
254 /* Do not process this command twice */
259 * Try to exec stage 3 boot loader. If interrupted by a keypress,
260 * or in case of failure, try to load a kernel directly instead.
265 if (autoboot && !keyhit(3*SECOND)) {
271 /* Present the user with the boot2 prompt. */
274 if (!autoboot || !OPT_CHECK(RBX_QUIET))
275 printf("\nFreeBSD/x86 boot\n"
276 "Default: %u:%s(%u,%c)%s\n"
278 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
279 'a' + dsk.part, kname);
280 if (ioctrl & IO_SERIAL)
282 if (!autoboot || keyhit(3*SECOND))
284 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
294 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
307 static Elf32_Phdr ep[2];
308 static Elf32_Shdr es[2];
314 if (!(ino = lookup(kname))) {
316 printf("No %s\n", kname);
319 if (xfsread(ino, &hdr, sizeof(hdr)))
322 if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
323 addr = hdr.ex.a_entry & 0xffffff;
326 if (xfsread(ino, p, hdr.ex.a_text))
328 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
329 if (xfsread(ino, p, hdr.ex.a_data))
331 } else if (IS_ELF(hdr.eh)) {
332 fs_off = hdr.eh.e_phoff;
333 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
334 if (xfsread(ino, ep + j, sizeof(ep[0])))
336 if (ep[j].p_type == PT_LOAD)
339 for (i = 0; i < 2; i++) {
340 p = PTOV(ep[i].p_paddr & 0xffffff);
341 fs_off = ep[i].p_offset;
342 if (xfsread(ino, p, ep[i].p_filesz))
345 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
346 bootinfo.bi_symtab = VTOP(p);
347 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
348 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
349 (hdr.eh.e_shstrndx + 1);
350 if (xfsread(ino, &es, sizeof(es)))
352 for (i = 0; i < 2; i++) {
353 *(Elf32_Word *)p = es[i].sh_size;
354 p += sizeof(es[i].sh_size);
355 fs_off = es[i].sh_offset;
356 if (xfsread(ino, p, es[i].sh_size))
361 addr = hdr.eh.e_entry & 0xffffff;
362 bootinfo.bi_esymtab = VTOP(p);
364 printf("Invalid %s\n", "format");
368 bootinfo.bi_kernelname = VTOP(kname);
369 bootinfo.bi_bios_dev = dsk.drive;
370 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
371 MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
372 0, 0, 0, VTOP(&bootinfo));
384 while ((c = *arg++)) {
385 if (c == ' ' || c == '\t' || c == '\n')
387 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
392 while ((c = *arg++)) {
394 if (*(uint8_t *)PTOV(0x496) & 0x10) {
397 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
400 printf("Keyboard: %s\n", cp);
402 } else if (c == 'S') {
404 while ((unsigned int)(i = *arg++ - '0') <= 9)
406 if (j > 0 && i == -'0') {
410 /* Fall through to error below ('S' not in optstr[]). */
412 for (i = 0; c != optstr[i]; i++)
415 opts ^= OPT_SET(flags[i]);
417 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
418 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
419 if (ioctrl & IO_SERIAL) {
420 if (sio_init(115200 / comspeed) != 0)
421 ioctrl &= ~IO_SERIAL;
424 for (q = arg--; *q && *q != '('; q++);
435 for (i = 0; arg[0] != dev_nm[i][0] ||
436 arg[1] != dev_nm[i][1]; i++)
441 dsk.unit = *arg - '0';
442 if (arg[1] != ',' || dsk.unit > 9)
445 dsk.slice = WHOLE_DISK_SLICE;
447 dsk.slice = *arg - '0' + 1;
448 if (dsk.slice > NDOSPART + 1)
454 dsk.part = *arg - 'a';
460 dsk.drive = (dsk.type <= TYPE_MAXHARD
461 ? DRV_HARD : 0) + drv;
464 if ((i = ep - arg)) {
465 if ((size_t)i >= sizeof(knamebuf))
467 memcpy(knamebuf, arg, i + 1);
477 dskread(void *buf, unsigned lba, unsigned nblk)
479 struct dos_partition *dp;
486 sec = dmadat->secbuf;
488 if (drvread(sec, DOSBBSECTOR, 1))
490 dp = (void *)(sec + DOSPARTOFF);
492 if (sl < BASE_SLICE) {
493 for (i = 0; i < NDOSPART; i++)
494 if (dp[i].dp_typ == DOSPTYP_386BSD &&
495 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
497 if (dp[i].dp_flag & 0x80 ||
498 dsk.slice == COMPATIBILITY_SLICE)
501 if (dsk.slice == WHOLE_DISK_SLICE)
504 if (sl != WHOLE_DISK_SLICE) {
505 if (sl != COMPATIBILITY_SLICE)
506 dp += sl - BASE_SLICE;
507 if (dp->dp_typ != DOSPTYP_386BSD) {
508 printf("Invalid %s\n", "slice");
511 dsk.start = dp->dp_start;
513 if (drvread(sec, dsk.start + LABELSECTOR, 1))
515 d = (void *)(sec + LABELOFFSET);
516 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
517 if (dsk.part != RAW_PART) {
518 printf("Invalid %s\n", "label");
523 if (d->d_type == DTYPE_SCSI)
527 if (dsk.part >= d->d_npartitions ||
528 !d->d_partitions[dsk.part].p_size) {
529 printf("Invalid %s\n", "partition");
532 dsk.start += d->d_partitions[dsk.part].p_offset;
533 dsk.start -= d->d_partitions[RAW_PART].p_offset;
536 return drvread(buf, dsk.start + lba, nblk);
540 printf(const char *fmt,...)
549 while ((c = *fmt++)) {
554 putchar(va_arg(ap, int));
557 for (s = va_arg(ap, char *); *s; s++)
561 u = va_arg(ap, unsigned);
564 *s++ = '0' + u % 10U;
586 drvread(void *buf, unsigned lba, unsigned nblk)
588 static unsigned c = 0x2d5c7c2f;
590 if (!OPT_CHECK(RBX_QUIET))
591 printf("%c\b", c = c << 8 | c >> 24);
592 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
593 v86.addr = XREADORG; /* call to xread in boot1 */
594 v86.es = VTOPSEG(buf);
596 v86.ebx = VTOPOFF(buf);
598 v86.edx = nblk << 8 | dsk.drive;
601 if (V86_CY(v86.efl)) {
602 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
609 keyhit(unsigned ticks)
613 if (OPT_CHECK(RBX_NOINTR))
619 t1 = *(uint32_t *)PTOV(0x46c);
622 if ((uint32_t)(t1 - t0) >= ticks)
630 if (ioctrl & IO_KEYBOARD)
632 if (ioctrl & IO_SERIAL)
643 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
649 if (OPT_CHECK(RBX_NOINTR))
652 if (ioctrl & IO_KEYBOARD && getc(1))
653 return fn ? 1 : getc(0);
654 if (ioctrl & IO_SERIAL && sio_ischar())
655 return fn ? 1 : sio_getc();