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>
27 #include <machine/psl.h>
41 #define SECOND 18 /* Circa that many ticks in a second. */
43 #define RBX_ASKNAME 0x0 /* -a */
44 #define RBX_SINGLE 0x1 /* -s */
45 /* 0x2 is reserved for log2(RB_NOSYNC). */
46 /* 0x3 is reserved for log2(RB_HALT). */
47 /* 0x4 is reserved for log2(RB_INITNAME). */
48 #define RBX_DFLTROOT 0x5 /* -r */
49 #define RBX_KDB 0x6 /* -d */
50 /* 0x7 is reserved for log2(RB_RDONLY). */
51 /* 0x8 is reserved for log2(RB_DUMP). */
52 /* 0x9 is reserved for log2(RB_MINIROOT). */
53 #define RBX_CONFIG 0xa /* -c */
54 #define RBX_VERBOSE 0xb /* -v */
55 #define RBX_SERIAL 0xc /* -h */
56 #define RBX_CDROM 0xd /* -C */
57 /* 0xe is reserved for log2(RB_POWEROFF). */
58 #define RBX_GDB 0xf /* -g */
59 #define RBX_MUTE 0x10 /* -m */
60 /* 0x11 is reserved for log2(RB_SELFTEST). */
61 /* 0x12 is reserved for boot programs. */
62 /* 0x13 is reserved for boot programs. */
63 #define RBX_PAUSE 0x14 /* -p */
64 #define RBX_QUIET 0x15 /* -q */
65 #define RBX_NOINTR 0x1c /* -n */
66 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
67 #define RBX_DUAL 0x1d /* -D */
68 /* 0x1f is reserved for log2(RB_BOOTINFO). */
70 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -D */
71 #define RBX_MASK (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
72 OPT_SET(RBX_DFLTROOT) | OPT_SET(RBX_KDB ) | \
73 OPT_SET(RBX_CONFIG) | OPT_SET(RBX_VERBOSE) | \
74 OPT_SET(RBX_SERIAL) | OPT_SET(RBX_CDROM) | \
75 OPT_SET(RBX_GDB ) | OPT_SET(RBX_MUTE) | \
76 OPT_SET(RBX_PAUSE) | OPT_SET(RBX_DUAL))
78 #define PATH_CONFIG "/boot.config"
79 #define PATH_BOOT3 "/boot/loader"
80 #define PATH_KERNEL "/boot/kernel/kernel"
87 #define V86_CY(x) ((x) & PSL_C)
88 #define V86_ZR(x) ((x) & PSL_Z)
95 #define TYPE_MAXHARD TYPE_DA
98 #define OPT_SET(opt) (1 << (opt))
99 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
101 extern uint32_t _end;
103 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
104 static const unsigned char flags[NOPT] = {
121 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
122 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
133 static char cmd[512], cmddup[512];
134 static char kname[1024];
135 static uint32_t opts;
136 static int comspeed = SIOSPD;
137 static struct bootinfo bootinfo;
138 static uint8_t ioctrl = IO_KEYBOARD;
141 static void load(void);
142 static int parse(void);
143 static int xfsread(ino_t, void *, size_t);
144 static int dskread(void *, unsigned, unsigned);
145 static void printf(const char *,...);
146 static void putchar(int);
147 static uint32_t memsize(void);
148 static int drvread(void *, unsigned, unsigned);
149 static int keyhit(unsigned);
150 static int xputc(int);
151 static int xgetc(int);
152 static int getc(int);
154 static void memcpy(void *, const void *, int);
156 memcpy(void *dst, const void *src, int len)
166 strcmp(const char *s1, const char *s2)
168 for (; *s1 == *s2 && *s1; s1++, s2++);
169 return (unsigned char)*s1 - (unsigned char)*s2;
172 #define UFS_SMALL_CGBASE
176 xfsread(ino_t inode, void *buf, size_t nbyte)
178 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
179 printf("Invalid %s\n", "format");
185 static inline uint32_t
202 switch (c = xgetc(0)) {
217 if (s - cmd < sizeof(cmd) - 1)
228 v86.eax = 0xe00 | (c & 0xff);
239 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
241 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
242 dsk.drive = *(uint8_t *)PTOV(ARGS);
243 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
244 dsk.unit = dsk.drive & DRV_MASK;
245 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
246 bootinfo.bi_version = BOOTINFO_VERSION;
247 bootinfo.bi_size = sizeof(bootinfo);
248 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
249 bootinfo.bi_extmem = memsize();
250 bootinfo.bi_memsizes_valid++;
252 /* Process configuration file */
256 if ((ino = lookup(PATH_CONFIG)))
257 fsread(ino, cmd, sizeof(cmd));
260 memcpy(cmddup, cmd, sizeof(cmd));
263 if (!OPT_CHECK(RBX_QUIET))
264 printf("%s: %s", PATH_CONFIG, cmddup);
265 /* Do not process this command twice */
270 * Try to exec stage 3 boot loader. If interrupted by a keypress,
271 * or in case of failure, try to load a kernel directly instead.
274 if (autoboot && !*kname) {
275 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
276 if (!keyhit(3*SECOND)) {
278 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
282 /* Present the user with the boot2 prompt. */
285 if (!autoboot || !OPT_CHECK(RBX_QUIET))
286 printf("\nFreeBSD/x86 boot\n"
287 "Default: %u:%s(%u,%c)%s\n"
289 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
290 'a' + dsk.part, kname);
291 if (ioctrl & IO_SERIAL)
293 if (!autoboot || keyhit(5*SECOND))
295 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
305 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
318 static Elf32_Phdr ep[2];
319 static Elf32_Shdr es[2];
325 if (!(ino = lookup(kname))) {
327 printf("No %s\n", kname);
330 if (xfsread(ino, &hdr, sizeof(hdr)))
332 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
334 else if (IS_ELF(hdr.eh))
337 printf("Invalid %s\n", "format");
341 addr = hdr.ex.a_entry & 0xffffff;
344 if (xfsread(ino, p, hdr.ex.a_text))
346 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
347 if (xfsread(ino, p, hdr.ex.a_data))
349 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
350 bootinfo.bi_symtab = VTOP(p);
351 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
352 p += sizeof(hdr.ex.a_syms);
354 if (xfsread(ino, p, hdr.ex.a_syms))
357 if (xfsread(ino, p, sizeof(int)))
362 if (xfsread(ino, p, x))
367 fs_off = hdr.eh.e_phoff;
368 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
369 if (xfsread(ino, ep + j, sizeof(ep[0])))
371 if (ep[j].p_type == PT_LOAD)
374 for (i = 0; i < 2; i++) {
375 p = PTOV(ep[i].p_paddr & 0xffffff);
376 fs_off = ep[i].p_offset;
377 if (xfsread(ino, p, ep[i].p_filesz))
380 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
381 bootinfo.bi_symtab = VTOP(p);
382 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
383 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
384 (hdr.eh.e_shstrndx + 1);
385 if (xfsread(ino, &es, sizeof(es)))
387 for (i = 0; i < 2; i++) {
388 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
389 p += sizeof(es[i].sh_size);
390 fs_off = es[i].sh_offset;
391 if (xfsread(ino, p, es[i].sh_size))
396 addr = hdr.eh.e_entry & 0xffffff;
398 bootinfo.bi_esymtab = VTOP(p);
399 bootinfo.bi_kernelname = VTOP(kname);
400 bootinfo.bi_bios_dev = dsk.drive;
401 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
402 MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
403 0, 0, 0, VTOP(&bootinfo));
415 while ((c = *arg++)) {
416 if (c == ' ' || c == '\t' || c == '\n')
418 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
423 while ((c = *arg++)) {
425 if (*(uint8_t *)PTOV(0x496) & 0x10) {
428 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
431 printf("Keyboard: %s\n", cp);
433 } else if (c == 'S') {
435 while ((unsigned int)(i = *arg++ - '0') <= 9)
437 if (j > 0 && i == -'0') {
441 /* Fall through to error below ('S' not in optstr[]). */
443 for (i = 0; c != optstr[i]; i++)
446 opts ^= OPT_SET(flags[i]);
448 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
449 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
450 if (ioctrl & IO_SERIAL)
451 sio_init(115200 / comspeed);
453 for (q = arg--; *q && *q != '('; q++);
464 for (i = 0; arg[0] != dev_nm[i][0] ||
465 arg[1] != dev_nm[i][1]; i++)
470 dsk.unit = *arg - '0';
471 if (arg[1] != ',' || dsk.unit > 9)
474 dsk.slice = WHOLE_DISK_SLICE;
476 dsk.slice = *arg - '0' + 1;
477 if (dsk.slice > NDOSPART + 1)
483 dsk.part = *arg - 'a';
489 dsk.drive = (dsk.type <= TYPE_MAXHARD
490 ? DRV_HARD : 0) + drv;
493 if ((i = ep - arg)) {
494 if ((size_t)i >= sizeof(kname))
496 memcpy(kname, arg, i + 1);
505 dskread(void *buf, unsigned lba, unsigned nblk)
507 struct dos_partition *dp;
513 sec = dmadat->secbuf;
515 if (drvread(sec, DOSBBSECTOR, 1))
517 dp = (void *)(sec + DOSPARTOFF);
519 if (sl < BASE_SLICE) {
520 for (i = 0; i < NDOSPART; i++)
521 if (dp[i].dp_typ == DOSPTYP_386BSD &&
522 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
524 if (dp[i].dp_flag & 0x80 ||
525 dsk.slice == COMPATIBILITY_SLICE)
528 if (dsk.slice == WHOLE_DISK_SLICE)
531 if (sl != WHOLE_DISK_SLICE) {
532 if (sl != COMPATIBILITY_SLICE)
533 dp += sl - BASE_SLICE;
534 if (dp->dp_typ != DOSPTYP_386BSD) {
535 printf("Invalid %s\n", "slice");
538 dsk.start = dp->dp_start;
540 if (drvread(sec, dsk.start + LABELSECTOR, 1))
542 d = (void *)(sec + LABELOFFSET);
543 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
544 if (dsk.part != RAW_PART) {
545 printf("Invalid %s\n", "label");
550 if (d->d_type == DTYPE_SCSI)
554 if (dsk.part >= d->d_npartitions ||
555 !d->d_partitions[dsk.part].p_size) {
556 printf("Invalid %s\n", "partition");
559 dsk.start += d->d_partitions[dsk.part].p_offset;
560 dsk.start -= d->d_partitions[RAW_PART].p_offset;
563 return drvread(buf, dsk.start + lba, nblk);
567 printf(const char *fmt,...)
576 while ((c = *fmt++)) {
581 putchar(va_arg(ap, int));
584 for (s = va_arg(ap, char *); *s; s++)
588 u = va_arg(ap, unsigned);
591 *s++ = '0' + u % 10U;
613 drvread(void *buf, unsigned lba, unsigned nblk)
615 static unsigned c = 0x2d5c7c2f;
617 if (!OPT_CHECK(RBX_QUIET))
618 printf("%c\b", c = c << 8 | c >> 24);
619 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
620 v86.addr = XREADORG; /* call to xread in boot1 */
621 v86.es = VTOPSEG(buf);
623 v86.ebx = VTOPOFF(buf);
625 v86.edx = nblk << 8 | dsk.drive;
628 if (V86_CY(v86.efl)) {
629 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
636 keyhit(unsigned ticks)
640 if (OPT_CHECK(RBX_NOINTR))
646 t1 = *(uint32_t *)PTOV(0x46c);
649 if (t1 < t0 || t1 >= t0 + ticks)
657 if (ioctrl & IO_KEYBOARD)
659 if (ioctrl & IO_SERIAL)
667 if (OPT_CHECK(RBX_NOINTR))
670 if (ioctrl & IO_KEYBOARD && getc(1))
671 return fn ? 1 : getc(0);
672 if (ioctrl & IO_SERIAL && sio_ischar())
673 return fn ? 1 : sio_getc();
685 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);