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_CONFIG "/boot.config"
78 #define PATH_BOOT3 "/boot/loader"
79 #define PATH_KERNEL "/boot/kernel/kernel"
86 #define V86_CY(x) ((x) & 1)
87 #define V86_ZR(x) ((x) & 0x40)
94 #define TYPE_MAXHARD TYPE_DA
97 #define OPT_SET(opt) (1 << (opt))
98 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
100 extern uint32_t _end;
102 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
103 static const unsigned char flags[NOPT] = {
120 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
121 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
132 static char cmd[512];
133 static char kname[1024];
134 static uint32_t opts;
135 static int comspeed = SIOSPD;
136 static struct bootinfo bootinfo;
137 static uint8_t ioctrl = IO_KEYBOARD;
140 static void load(void);
141 static int parse(void);
142 static int xfsread(ino_t, void *, size_t);
143 static int dskread(void *, unsigned, unsigned);
144 static void printf(const char *,...);
145 static void putchar(int);
146 static uint32_t memsize(void);
147 static int drvread(void *, unsigned, unsigned);
148 static int keyhit(unsigned);
149 static int xputc(int);
150 static int xgetc(int);
151 static int getc(int);
153 static void memcpy(void *, const void *, int);
155 memcpy(void *dst, const void *src, int len)
165 strcmp(const char *s1, const char *s2)
167 for (; *s1 == *s2 && *s1; s1++, s2++);
168 return (unsigned char)*s1 - (unsigned char)*s2;
171 #define UFS_SMALL_CGBASE
175 xfsread(ino_t inode, void *buf, size_t nbyte)
177 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
178 printf("Invalid %s\n", "format");
184 static inline uint32_t
201 switch (c = xgetc(0)) {
216 if (s - cmd < sizeof(cmd) - 1)
227 v86.eax = 0xe00 | (c & 0xff);
238 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
240 dsk.drive = *(uint8_t *)PTOV(ARGS);
241 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
242 dsk.unit = dsk.drive & DRV_MASK;
243 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
244 bootinfo.bi_version = BOOTINFO_VERSION;
245 bootinfo.bi_size = sizeof(bootinfo);
246 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
247 bootinfo.bi_extmem = memsize();
248 bootinfo.bi_memsizes_valid++;
250 /* Process configuration file */
254 if ((ino = lookup(PATH_CONFIG)))
255 fsread(ino, cmd, sizeof(cmd));
260 if (!OPT_CHECK(RBX_QUIET))
261 printf("%s: %s", PATH_CONFIG, cmd);
262 /* Do not process this command twice */
267 * Try to exec stage 3 boot loader. If interrupted by a keypress,
268 * or in case of failure, try to load a kernel directly instead.
271 if (autoboot && !*kname) {
272 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
273 if (!keyhit(3*SECOND)) {
275 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
279 /* Present the user with the boot2 prompt. */
282 if (!autoboot || !OPT_CHECK(RBX_QUIET))
283 printf("\nFreeBSD/i386 boot\n"
284 "Default: %u:%s(%u,%c)%s\n"
286 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
287 'a' + dsk.part, kname);
288 if (ioctrl & IO_SERIAL)
290 if (!autoboot || keyhit(5*SECOND))
292 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
302 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
315 static Elf32_Phdr ep[2];
316 static Elf32_Shdr es[2];
322 if (!(ino = lookup(kname))) {
324 printf("No %s\n", kname);
327 if (xfsread(ino, &hdr, sizeof(hdr)))
329 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
331 else if (IS_ELF(hdr.eh))
334 printf("Invalid %s\n", "format");
338 addr = hdr.ex.a_entry & 0xffffff;
341 if (xfsread(ino, p, hdr.ex.a_text))
343 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
344 if (xfsread(ino, p, hdr.ex.a_data))
346 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
347 bootinfo.bi_symtab = VTOP(p);
348 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
349 p += sizeof(hdr.ex.a_syms);
351 if (xfsread(ino, p, hdr.ex.a_syms))
354 if (xfsread(ino, p, sizeof(int)))
359 if (xfsread(ino, p, x))
364 fs_off = hdr.eh.e_phoff;
365 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
366 if (xfsread(ino, ep + j, sizeof(ep[0])))
368 if (ep[j].p_type == PT_LOAD)
371 for (i = 0; i < 2; i++) {
372 p = PTOV(ep[i].p_paddr & 0xffffff);
373 fs_off = ep[i].p_offset;
374 if (xfsread(ino, p, ep[i].p_filesz))
377 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
378 bootinfo.bi_symtab = VTOP(p);
379 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
380 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
381 (hdr.eh.e_shstrndx + 1);
382 if (xfsread(ino, &es, sizeof(es)))
384 for (i = 0; i < 2; i++) {
385 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
386 p += sizeof(es[i].sh_size);
387 fs_off = es[i].sh_offset;
388 if (xfsread(ino, p, es[i].sh_size))
393 addr = hdr.eh.e_entry & 0xffffff;
395 bootinfo.bi_esymtab = VTOP(p);
396 bootinfo.bi_kernelname = VTOP(kname);
397 bootinfo.bi_bios_dev = dsk.drive;
398 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
399 MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
400 0, 0, 0, VTOP(&bootinfo));
412 while ((c = *arg++)) {
413 if (c == ' ' || c == '\t' || c == '\n')
415 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
420 while ((c = *arg++)) {
422 if (*(uint8_t *)PTOV(0x496) & 0x10) {
425 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
428 printf("Keyboard: %s\n", cp);
430 } else if (c == 'S') {
432 while ((unsigned int)(i = *arg++ - '0') <= 9)
434 if (j > 0 && i == -'0') {
438 /* Fall through to error below ('S' not in optstr[]). */
440 for (i = 0; c != optstr[i]; i++)
443 opts ^= OPT_SET(flags[i]);
445 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
446 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
447 if (ioctrl & IO_SERIAL)
448 sio_init(115200 / comspeed);
450 for (q = arg--; *q && *q != '('; q++);
461 for (i = 0; arg[0] != dev_nm[i][0] ||
462 arg[1] != dev_nm[i][1]; i++)
467 dsk.unit = *arg - '0';
468 if (arg[1] != ',' || dsk.unit > 9)
471 dsk.slice = WHOLE_DISK_SLICE;
473 dsk.slice = *arg - '0' + 1;
474 if (dsk.slice > NDOSPART)
480 dsk.part = *arg - 'a';
486 dsk.drive = (dsk.type <= TYPE_MAXHARD
487 ? DRV_HARD : 0) + drv;
490 if ((i = ep - arg)) {
491 if ((size_t)i >= sizeof(kname))
493 memcpy(kname, arg, i + 1);
502 dskread(void *buf, unsigned lba, unsigned nblk)
504 struct dos_partition *dp;
510 sec = dmadat->secbuf;
512 if (drvread(sec, DOSBBSECTOR, 1))
514 dp = (void *)(sec + DOSPARTOFF);
516 if (sl < BASE_SLICE) {
517 for (i = 0; i < NDOSPART; i++)
518 if (dp[i].dp_typ == DOSPTYP_386BSD &&
519 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
521 if (dp[i].dp_flag & 0x80 ||
522 dsk.slice == COMPATIBILITY_SLICE)
525 if (dsk.slice == WHOLE_DISK_SLICE)
528 if (sl != WHOLE_DISK_SLICE) {
529 if (sl != COMPATIBILITY_SLICE)
530 dp += sl - BASE_SLICE;
531 if (dp->dp_typ != DOSPTYP_386BSD) {
532 printf("Invalid %s\n", "slice");
535 dsk.start = dp->dp_start;
537 if (drvread(sec, dsk.start + LABELSECTOR, 1))
539 d = (void *)(sec + LABELOFFSET);
540 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
541 if (dsk.part != RAW_PART) {
542 printf("Invalid %s\n", "label");
547 if (d->d_type == DTYPE_SCSI)
551 if (dsk.part >= d->d_npartitions ||
552 !d->d_partitions[dsk.part].p_size) {
553 printf("Invalid %s\n", "partition");
556 dsk.start += d->d_partitions[dsk.part].p_offset;
557 dsk.start -= d->d_partitions[RAW_PART].p_offset;
560 return drvread(buf, dsk.start + lba, nblk);
564 printf(const char *fmt,...)
573 while ((c = *fmt++)) {
578 putchar(va_arg(ap, int));
581 for (s = va_arg(ap, char *); *s; s++)
585 u = va_arg(ap, unsigned);
588 *s++ = '0' + u % 10U;
610 drvread(void *buf, unsigned lba, unsigned nblk)
612 static unsigned c = 0x2d5c7c2f;
614 if (!OPT_CHECK(RBX_QUIET))
615 printf("%c\b", c = c << 8 | c >> 24);
616 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
617 v86.addr = XREADORG; /* call to xread in boot1 */
618 v86.es = VTOPSEG(buf);
620 v86.ebx = VTOPOFF(buf);
622 v86.edx = nblk << 8 | dsk.drive;
625 if (V86_CY(v86.efl)) {
626 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
633 keyhit(unsigned ticks)
637 if (OPT_CHECK(RBX_NOINTR))
643 t1 = *(uint32_t *)PTOV(0x46c);
646 if (t1 < t0 || t1 >= t0 + ticks)
654 if (ioctrl & IO_KEYBOARD)
656 if (ioctrl & IO_SERIAL)
664 if (OPT_CHECK(RBX_NOINTR))
667 if (ioctrl & IO_KEYBOARD && getc(1))
668 return fn ? 1 : getc(0);
669 if (ioctrl & IO_SERIAL && sio_ischar())
670 return fn ? 1 : sio_getc();
682 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);