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;
174 xfsread(ino_t inode, void *buf, size_t nbyte)
176 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
177 printf("Invalid %s\n", "format");
183 static inline uint32_t
200 switch (c = xgetc(0)) {
215 if (s - cmd < sizeof(cmd) - 1)
226 v86.eax = 0xe00 | (c & 0xff);
237 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
239 dsk.drive = *(uint8_t *)PTOV(ARGS);
240 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
241 dsk.unit = dsk.drive & DRV_MASK;
242 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
243 bootinfo.bi_version = BOOTINFO_VERSION;
244 bootinfo.bi_size = sizeof(bootinfo);
245 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
246 bootinfo.bi_extmem = memsize();
247 bootinfo.bi_memsizes_valid++;
249 /* Process configuration file */
253 if ((ino = lookup(PATH_CONFIG)))
254 fsread(ino, cmd, sizeof(cmd));
259 if (!OPT_CHECK(RBX_QUIET))
260 printf("%s: %s", PATH_CONFIG, cmd);
261 /* Do not process this command twice */
266 * Try to exec stage 3 boot loader. If interrupted by a keypress,
267 * or in case of failure, try to load a kernel directly instead.
270 if (autoboot && !*kname) {
271 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
272 if (!keyhit(3*SECOND)) {
274 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
278 /* Present the user with the boot2 prompt. */
281 if (!autoboot || !OPT_CHECK(RBX_QUIET))
282 printf("\nFreeBSD/i386 boot\n"
283 "Default: %u:%s(%u,%c)%s\n"
285 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
286 'a' + dsk.part, kname);
287 if (ioctrl & IO_SERIAL)
289 if (!autoboot || keyhit(5*SECOND))
291 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
301 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
314 static Elf32_Phdr ep[2];
315 static Elf32_Shdr es[2];
321 if (!(ino = lookup(kname))) {
323 printf("No %s\n", kname);
326 if (xfsread(ino, &hdr, sizeof(hdr)))
328 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
330 else if (IS_ELF(hdr.eh))
333 printf("Invalid %s\n", "format");
337 addr = hdr.ex.a_entry & 0xffffff;
340 if (xfsread(ino, p, hdr.ex.a_text))
342 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
343 if (xfsread(ino, p, hdr.ex.a_data))
345 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
346 bootinfo.bi_symtab = VTOP(p);
347 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
348 p += sizeof(hdr.ex.a_syms);
350 if (xfsread(ino, p, hdr.ex.a_syms))
353 if (xfsread(ino, p, sizeof(int)))
358 if (xfsread(ino, p, x))
363 fs_off = hdr.eh.e_phoff;
364 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
365 if (xfsread(ino, ep + j, sizeof(ep[0])))
367 if (ep[j].p_type == PT_LOAD)
370 for (i = 0; i < 2; i++) {
371 p = PTOV(ep[i].p_paddr & 0xffffff);
372 fs_off = ep[i].p_offset;
373 if (xfsread(ino, p, ep[i].p_filesz))
376 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
377 bootinfo.bi_symtab = VTOP(p);
378 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
379 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
380 (hdr.eh.e_shstrndx + 1);
381 if (xfsread(ino, &es, sizeof(es)))
383 for (i = 0; i < 2; i++) {
384 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
385 p += sizeof(es[i].sh_size);
386 fs_off = es[i].sh_offset;
387 if (xfsread(ino, p, es[i].sh_size))
392 addr = hdr.eh.e_entry & 0xffffff;
394 bootinfo.bi_esymtab = VTOP(p);
395 bootinfo.bi_kernelname = VTOP(kname);
396 bootinfo.bi_bios_dev = dsk.drive;
397 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
398 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
399 0, 0, 0, VTOP(&bootinfo));
411 while ((c = *arg++)) {
412 if (c == ' ' || c == '\t' || c == '\n')
414 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
419 while ((c = *arg++)) {
421 if (*(uint8_t *)PTOV(0x496) & 0x10) {
424 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
427 printf("Keyboard: %s\n", cp);
429 } else if (c == 'S') {
431 while ((unsigned int)(i = *arg++ - '0') <= 9)
433 if (j > 0 && i == -'0') {
437 /* Fall through to error below ('S' not in optstr[]). */
439 for (i = 0; c != optstr[i]; i++)
442 opts ^= OPT_SET(flags[i]);
444 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
445 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
446 if (ioctrl & IO_SERIAL)
447 sio_init(115200 / comspeed);
449 for (q = arg--; *q && *q != '('; q++);
460 for (i = 0; arg[0] != dev_nm[i][0] ||
461 arg[1] != dev_nm[i][1]; i++)
466 dsk.unit = *arg - '0';
467 if (arg[1] != ',' || dsk.unit > 9)
470 dsk.slice = WHOLE_DISK_SLICE;
472 dsk.slice = *arg - '0' + 1;
473 if (dsk.slice > NDOSPART)
479 dsk.part = *arg - 'a';
485 dsk.drive = (dsk.type <= TYPE_MAXHARD
486 ? DRV_HARD : 0) + drv;
489 if ((i = ep - arg)) {
490 if ((size_t)i >= sizeof(kname))
492 memcpy(kname, arg, i + 1);
501 dskread(void *buf, unsigned lba, unsigned nblk)
503 struct dos_partition *dp;
509 sec = dmadat->secbuf;
511 if (drvread(sec, DOSBBSECTOR, 1))
513 dp = (void *)(sec + DOSPARTOFF);
515 if (sl < BASE_SLICE) {
516 for (i = 0; i < NDOSPART; i++)
517 if (dp[i].dp_typ == DOSPTYP_386BSD &&
518 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
520 if (dp[i].dp_flag & 0x80 ||
521 dsk.slice == COMPATIBILITY_SLICE)
524 if (dsk.slice == WHOLE_DISK_SLICE)
527 if (sl != WHOLE_DISK_SLICE) {
528 if (sl != COMPATIBILITY_SLICE)
529 dp += sl - BASE_SLICE;
530 if (dp->dp_typ != DOSPTYP_386BSD) {
531 printf("Invalid %s\n", "slice");
534 dsk.start = dp->dp_start;
536 if (drvread(sec, dsk.start + LABELSECTOR, 1))
538 d = (void *)(sec + LABELOFFSET);
539 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
540 if (dsk.part != RAW_PART) {
541 printf("Invalid %s\n", "label");
546 if (d->d_type == DTYPE_SCSI)
550 if (dsk.part >= d->d_npartitions ||
551 !d->d_partitions[dsk.part].p_size) {
552 printf("Invalid %s\n", "partition");
555 dsk.start += d->d_partitions[dsk.part].p_offset;
556 dsk.start -= d->d_partitions[RAW_PART].p_offset;
559 return drvread(buf, dsk.start + lba, nblk);
563 printf(const char *fmt,...)
572 while ((c = *fmt++)) {
577 putchar(va_arg(ap, int));
580 for (s = va_arg(ap, char *); *s; s++)
584 u = va_arg(ap, unsigned);
587 *s++ = '0' + u % 10U;
609 drvread(void *buf, unsigned lba, unsigned nblk)
611 static unsigned c = 0x2d5c7c2f;
613 if (!OPT_CHECK(RBX_QUIET))
614 printf("%c\b", c = c << 8 | c >> 24);
615 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
616 v86.addr = XREADORG; /* call to xread in boot1 */
617 v86.es = VTOPSEG(buf);
619 v86.ebx = VTOPOFF(buf);
621 v86.edx = nblk << 8 | dsk.drive;
624 if (V86_CY(v86.efl)) {
625 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
632 keyhit(unsigned ticks)
636 if (OPT_CHECK(RBX_NOINTR))
642 t1 = *(uint32_t *)PTOV(0x46c);
645 if (t1 < t0 || t1 >= t0 + ticks)
653 if (ioctrl & IO_KEYBOARD)
655 if (ioctrl & IO_SERIAL)
663 if (OPT_CHECK(RBX_NOINTR))
666 if (ioctrl & IO_KEYBOARD && getc(1))
667 return fn ? 1 : getc(0);
668 if (ioctrl & IO_SERIAL && sio_ischar())
669 return fn ? 1 : sio_getc();
681 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);