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>
39 /* Define to 0 to omit serial support */
48 #define DO_KBD (ioctrl & IO_KEYBOARD)
49 #define DO_SIO (ioctrl & IO_SERIAL)
55 #define SECOND 18 /* Circa that many ticks in a second. */
68 #define TYPE_MAXHARD TYPE_DA
73 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
74 static const unsigned char flags[NOPT] = {
91 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
92 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
103 static char cmd[512], cmddup[512], knamebuf[1024];
104 static const char *kname;
106 static struct bootinfo bootinfo;
108 static int comspeed = SIOSPD;
109 static uint8_t ioctrl = IO_KEYBOARD;
114 static void load(void);
115 static int parse(void);
116 static int dskread(void *, unsigned, unsigned);
117 static void printf(const char *,...);
118 static void putchar(int);
119 static int drvread(void *, unsigned, unsigned);
120 static int keyhit(unsigned);
121 static int xputc(int);
122 static int xgetc(int);
123 static inline int getc(int);
125 static void memcpy(void *, const void *, int);
127 memcpy(void *dst, const void *src, int len)
137 strcmp(const char *s1, const char *s2)
139 for (; *s1 == *s2 && *s1; s1++, s2++);
140 return (unsigned char)*s1 - (unsigned char)*s2;
143 #define UFS_SMALL_CGBASE
147 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
149 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
150 printf("Invalid %s\n", "format");
164 switch (c = xgetc(0)) {
179 if (s - cmd < sizeof(cmd) - 1)
190 v86.eax = 0xe00 | (c & 0xff);
202 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
204 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
205 dsk.drive = *(uint8_t *)PTOV(ARGS);
206 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
207 dsk.unit = dsk.drive & DRV_MASK;
208 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
209 bootinfo.bi_version = BOOTINFO_VERSION;
210 bootinfo.bi_size = sizeof(bootinfo);
212 /* Process configuration file */
216 if ((ino = lookup(PATH_CONFIG)) ||
217 (ino = lookup(PATH_DOTCONFIG))) {
218 nbyte = fsread(ino, cmd, sizeof(cmd) - 1);
223 memcpy(cmddup, cmd, sizeof(cmd));
226 if (!OPT_CHECK(RBX_QUIET))
227 printf("%s: %s", PATH_CONFIG, cmddup);
228 /* Do not process this command twice */
233 * Try to exec stage 3 boot loader. If interrupted by a keypress,
234 * or in case of failure, try to load a kernel directly instead.
239 if (autoboot && !keyhit(3*SECOND)) {
245 /* Present the user with the boot2 prompt. */
248 if (!autoboot || !OPT_CHECK(RBX_QUIET))
249 printf("\nFreeBSD/x86 boot\n"
250 "Default: %u:%s(%u,%c)%s\n"
252 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
253 'a' + dsk.part, kname);
256 if (!autoboot || keyhit(3*SECOND))
258 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
268 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
281 static Elf32_Phdr ep[2];
282 static Elf32_Shdr es[2];
289 if (!(ino = lookup(kname))) {
291 printf("No %s\n", kname);
294 if (xfsread(ino, &hdr, sizeof(hdr)))
297 if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
298 addr = hdr.ex.a_entry & 0xffffff;
301 if (xfsread(ino, p, hdr.ex.a_text))
303 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
304 if (xfsread(ino, p, hdr.ex.a_data))
306 } else if (IS_ELF(hdr.eh)) {
307 fs_off = hdr.eh.e_phoff;
308 for (j = k = 0; k < hdr.eh.e_phnum && j < 2; k++) {
309 if (xfsread(ino, ep + j, sizeof(ep[0])))
311 if (ep[j].p_type == PT_LOAD)
314 for (i = 0; i < 2; i++) {
315 p = PTOV(ep[i].p_paddr & 0xffffff);
316 fs_off = ep[i].p_offset;
317 if (xfsread(ino, p, ep[i].p_filesz))
320 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
321 bootinfo.bi_symtab = VTOP(p);
322 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
323 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
324 (hdr.eh.e_shstrndx + 1);
325 if (xfsread(ino, &es, sizeof(es)))
327 for (i = 0; i < 2; i++) {
328 *(Elf32_Word *)p = es[i].sh_size;
329 p += sizeof(es[i].sh_size);
330 fs_off = es[i].sh_offset;
331 if (xfsread(ino, p, es[i].sh_size))
336 addr = hdr.eh.e_entry & 0xffffff;
337 bootinfo.bi_esymtab = VTOP(p);
339 printf("Invalid %s\n", "format");
343 bootinfo.bi_kernelname = VTOP(kname);
344 bootinfo.bi_bios_dev = dsk.drive;
345 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
346 MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
347 0, 0, 0, VTOP(&bootinfo));
360 while ((c = *arg++)) {
361 if (c == ' ' || c == '\t' || c == '\n')
363 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
368 while ((c = *arg++)) {
370 if (*(uint8_t *)PTOV(0x496) & 0x10) {
373 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
376 printf("Keyboard: %s\n", cp);
379 } else if (c == 'S') {
381 while ((unsigned int)(i = *arg++ - '0') <= 9)
383 if (j > 0 && i == -'0') {
387 /* Fall through to error below ('S' not in optstr[]). */
390 for (i = 0; c != optstr[i]; i++)
393 opts ^= OPT_SET(flags[i]);
396 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
397 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
399 if (sio_init(115200 / comspeed) != 0)
400 ioctrl &= ~IO_SERIAL;
404 for (q = arg--; *q && *q != '('; q++);
415 for (i = 0; arg[0] != dev_nm[i][0] ||
416 arg[1] != dev_nm[i][1]; i++)
421 dsk.unit = *arg - '0';
422 if (arg[1] != ',' || dsk.unit > 9)
425 dsk.slice = WHOLE_DISK_SLICE;
427 dsk.slice = *arg - '0' + 1;
428 if (dsk.slice > NDOSPART + 1)
434 dsk.part = *arg - 'a';
440 dsk.drive = (dsk.type <= TYPE_MAXHARD
441 ? DRV_HARD : 0) + drv;
446 if (k >= sizeof(knamebuf))
448 memcpy(knamebuf, arg, k + 1);
458 dskread(void *buf, unsigned lba, unsigned nblk)
460 struct dos_partition *dp;
468 sec = dmadat->secbuf;
470 if (drvread(sec, DOSBBSECTOR, 1))
472 dp = (void *)(sec + DOSPARTOFF);
474 if (sl < BASE_SLICE) {
475 for (i = 0; i < NDOSPART; i++)
476 if (dp[i].dp_typ == DOSPTYP_386BSD &&
477 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
479 if (dp[i].dp_flag & 0x80 ||
480 dsk.slice == COMPATIBILITY_SLICE)
483 if (dsk.slice == WHOLE_DISK_SLICE)
486 if (sl != WHOLE_DISK_SLICE) {
487 if (sl != COMPATIBILITY_SLICE)
488 dp += sl - BASE_SLICE;
489 if (dp->dp_typ != DOSPTYP_386BSD) {
493 dsk.start = dp->dp_start;
495 if (drvread(sec, dsk.start + LABELSECTOR, 1))
497 d = (void *)(sec + LABELOFFSET);
498 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
499 if (dsk.part != RAW_PART) {
505 if (d->d_type == DTYPE_SCSI)
509 if (dsk.part >= d->d_npartitions ||
510 !d->d_partitions[dsk.part].p_size) {
511 reason = "partition";
514 dsk.start += d->d_partitions[dsk.part].p_offset;
515 dsk.start -= d->d_partitions[RAW_PART].p_offset;
518 return drvread(buf, dsk.start + lba, nblk);
520 printf("Invalid %s\n", reason);
525 printf(const char *fmt,...)
534 while ((c = *fmt++)) {
539 putchar(va_arg(ap, int));
542 for (s = va_arg(ap, char *); *s; s++)
546 u = va_arg(ap, unsigned);
549 *s++ = '0' + u % 10U;
571 drvread(void *buf, unsigned lba, unsigned nblk)
573 static unsigned c = 0x2d5c7c2f;
575 if (!OPT_CHECK(RBX_QUIET)) {
576 xputc(c = c << 8 | c >> 24);
579 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
580 v86.addr = XREADORG; /* call to xread in boot1 */
581 v86.es = VTOPSEG(buf);
583 v86.ebx = VTOPOFF(buf);
585 v86.edx = nblk << 8 | dsk.drive;
588 if (V86_CY(v86.efl)) {
589 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
596 keyhit(unsigned ticks)
600 if (OPT_CHECK(RBX_NOINTR))
606 t1 = *(uint32_t *)PTOV(0x46c);
609 if ((uint32_t)(t1 - t0) >= ticks)
630 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
636 if (OPT_CHECK(RBX_NOINTR))
639 if (DO_KBD && getc(1))
640 return fn ? 1 : getc(0);
641 if (DO_SIO && sio_ischar())
642 return fn ? 1 : sio_getc();