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)
140 strcmp(const char *s1, const char *s2)
143 for (; *s1 == *s2 && *s1; s1++, s2++);
144 return ((unsigned char)*s1 - (unsigned char)*s2);
147 #define UFS_SMALL_CGBASE
151 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
154 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
155 printf("Invalid %s\n", "format");
169 switch (c = xgetc(0)) {
184 if (s - cmd < sizeof(cmd) - 1)
196 v86.eax = 0xe00 | (c & 0xff);
208 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
210 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
211 dsk.drive = *(uint8_t *)PTOV(ARGS);
212 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
213 dsk.unit = dsk.drive & DRV_MASK;
214 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
215 bootinfo.bi_version = BOOTINFO_VERSION;
216 bootinfo.bi_size = sizeof(bootinfo);
218 /* Process configuration file */
222 if ((ino = lookup(PATH_CONFIG)) ||
223 (ino = lookup(PATH_DOTCONFIG))) {
224 nbyte = fsread(ino, cmd, sizeof(cmd) - 1);
229 memcpy(cmddup, cmd, sizeof(cmd));
232 if (!OPT_CHECK(RBX_QUIET))
233 printf("%s: %s", PATH_CONFIG, cmddup);
234 /* Do not process this command twice */
239 * Try to exec stage 3 boot loader. If interrupted by a keypress,
240 * or in case of failure, try to load a kernel directly instead.
245 if (autoboot && !keyhit(3*SECOND)) {
251 /* Present the user with the boot2 prompt. */
254 if (!autoboot || !OPT_CHECK(RBX_QUIET))
255 printf("\nFreeBSD/x86 boot\n"
256 "Default: %u:%s(%u,%c)%s\n"
258 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
259 'a' + dsk.part, kname);
262 if (!autoboot || keyhit(3*SECOND))
264 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
274 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
288 static Elf32_Phdr ep[2];
289 static Elf32_Shdr es[2];
296 if (!(ino = lookup(kname))) {
298 printf("No %s\n", kname);
301 if (xfsread(ino, &hdr, sizeof(hdr)))
304 if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
305 addr = hdr.ex.a_entry & 0xffffff;
308 if (xfsread(ino, p, hdr.ex.a_text))
310 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
311 if (xfsread(ino, p, hdr.ex.a_data))
313 } else if (IS_ELF(hdr.eh)) {
314 fs_off = hdr.eh.e_phoff;
315 for (j = k = 0; k < hdr.eh.e_phnum && j < 2; k++) {
316 if (xfsread(ino, ep + j, sizeof(ep[0])))
318 if (ep[j].p_type == PT_LOAD)
321 for (i = 0; i < 2; i++) {
322 p = PTOV(ep[i].p_paddr & 0xffffff);
323 fs_off = ep[i].p_offset;
324 if (xfsread(ino, p, ep[i].p_filesz))
327 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
328 bootinfo.bi_symtab = VTOP(p);
329 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
330 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
331 (hdr.eh.e_shstrndx + 1);
332 if (xfsread(ino, &es, sizeof(es)))
334 for (i = 0; i < 2; i++) {
335 *(Elf32_Word *)p = es[i].sh_size;
336 p += sizeof(es[i].sh_size);
337 fs_off = es[i].sh_offset;
338 if (xfsread(ino, p, es[i].sh_size))
343 addr = hdr.eh.e_entry & 0xffffff;
344 bootinfo.bi_esymtab = VTOP(p);
346 printf("Invalid %s\n", "format");
350 bootinfo.bi_kernelname = VTOP(kname);
351 bootinfo.bi_bios_dev = dsk.drive;
352 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
353 MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
354 0, 0, 0, VTOP(&bootinfo));
360 char *arg, *ep, *p, *q;
368 while ((c = *arg++)) {
369 if (c == ' ' || c == '\t' || c == '\n')
371 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
376 while ((c = *arg++)) {
378 if (*(uint8_t *)PTOV(0x496) & 0x10) {
381 opts |= OPT_SET(RBX_DUAL) |
385 printf("Keyboard: %s\n", cp);
388 } else if (c == 'S') {
390 while ((u_int)(i = *arg++ - '0') <= 9)
392 if (j > 0 && i == -'0') {
397 * Fall through to error below
398 * ('S' not in optstr[]).
402 for (i = 0; c != optstr[i]; i++)
405 opts ^= OPT_SET(flags[i]);
408 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
409 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
411 if (sio_init(115200 / comspeed) != 0)
412 ioctrl &= ~IO_SERIAL;
416 for (q = arg--; *q && *q != '('; q++);
427 for (i = 0; arg[0] != dev_nm[i][0] ||
428 arg[1] != dev_nm[i][1]; i++)
433 dsk.unit = *arg - '0';
434 if (arg[1] != ',' || dsk.unit > 9)
437 dsk.slice = WHOLE_DISK_SLICE;
439 dsk.slice = *arg - '0' + 1;
440 if (dsk.slice > NDOSPART + 1)
446 dsk.part = *arg - 'a';
452 dsk.drive = (dsk.type <= TYPE_MAXHARD
453 ? DRV_HARD : 0) + drv;
458 if (k >= sizeof(knamebuf))
460 memcpy(knamebuf, arg, k + 1);
470 dskread(void *buf, unsigned lba, unsigned nblk)
472 struct dos_partition *dp;
480 sec = dmadat->secbuf;
482 if (drvread(sec, DOSBBSECTOR, 1))
484 dp = (void *)(sec + DOSPARTOFF);
486 if (sl < BASE_SLICE) {
487 for (i = 0; i < NDOSPART; i++)
488 if (dp[i].dp_typ == DOSPTYP_386BSD &&
489 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
491 if (dp[i].dp_flag & 0x80 ||
492 dsk.slice == COMPATIBILITY_SLICE)
495 if (dsk.slice == WHOLE_DISK_SLICE)
498 if (sl != WHOLE_DISK_SLICE) {
499 if (sl != COMPATIBILITY_SLICE)
500 dp += sl - BASE_SLICE;
501 if (dp->dp_typ != DOSPTYP_386BSD) {
505 dsk.start = dp->dp_start;
507 if (drvread(sec, dsk.start + LABELSECTOR, 1))
509 d = (void *)(sec + LABELOFFSET);
510 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
511 if (dsk.part != RAW_PART) {
517 if (d->d_type == DTYPE_SCSI)
521 if (dsk.part >= d->d_npartitions ||
522 !d->d_partitions[dsk.part].p_size) {
523 reason = "partition";
526 dsk.start += d->d_partitions[dsk.part].p_offset;
527 dsk.start -= d->d_partitions[RAW_PART].p_offset;
530 return (drvread(buf, dsk.start + lba, nblk));
532 printf("Invalid %s\n", reason);
537 printf(const char *fmt,...)
546 while ((c = *fmt++)) {
551 putchar(va_arg(ap, int));
554 for (s = va_arg(ap, char *); *s; s++)
558 u = va_arg(ap, unsigned);
561 *s++ = '0' + u % 10U;
584 drvread(void *buf, unsigned lba, unsigned nblk)
586 static unsigned c = 0x2d5c7c2f;
588 if (!OPT_CHECK(RBX_QUIET)) {
589 xputc(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)
645 return (fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl));
652 if (OPT_CHECK(RBX_NOINTR))
655 if (DO_KBD && getc(1))
656 return (fn ? 1 : getc(0));
657 if (DO_SIO && sio_ischar())
658 return (fn ? 1 : sio_getc());