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;
113 static void load(void);
114 static int parse(void);
115 static int dskread(void *, unsigned, unsigned);
116 static void printf(const char *,...);
117 static void putchar(int);
118 static int drvread(void *, unsigned, unsigned);
119 static int keyhit(unsigned);
120 static int xputc(int);
121 static int xgetc(int);
122 static inline int getc(int);
124 static void memcpy(void *, const void *, int);
126 memcpy(void *dst, const void *src, int len)
136 strcmp(const char *s1, const char *s2)
138 for (; *s1 == *s2 && *s1; s1++, s2++);
139 return (unsigned char)*s1 - (unsigned char)*s2;
142 #define UFS_SMALL_CGBASE
146 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
148 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
149 printf("Invalid %s\n", "format");
163 switch (c = xgetc(0)) {
178 if (s - cmd < sizeof(cmd) - 1)
189 v86.eax = 0xe00 | (c & 0xff);
201 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
203 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
204 dsk.drive = *(uint8_t *)PTOV(ARGS);
205 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
206 dsk.unit = dsk.drive & DRV_MASK;
207 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
208 bootinfo.bi_version = BOOTINFO_VERSION;
209 bootinfo.bi_size = sizeof(bootinfo);
211 /* Process configuration file */
215 if ((ino = lookup(PATH_CONFIG)) ||
216 (ino = lookup(PATH_DOTCONFIG))) {
217 nbyte = fsread(ino, cmd, sizeof(cmd) - 1);
222 memcpy(cmddup, cmd, sizeof(cmd));
225 if (!OPT_CHECK(RBX_QUIET))
226 printf("%s: %s", PATH_CONFIG, cmddup);
227 /* Do not process this command twice */
232 * Try to exec stage 3 boot loader. If interrupted by a keypress,
233 * or in case of failure, try to load a kernel directly instead.
238 if (autoboot && !keyhit(3*SECOND)) {
244 /* Present the user with the boot2 prompt. */
247 if (!autoboot || !OPT_CHECK(RBX_QUIET))
248 printf("\nFreeBSD/x86 boot\n"
249 "Default: %u:%s(%u,%c)%s\n"
251 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
252 'a' + dsk.part, kname);
255 if (!autoboot || keyhit(3*SECOND))
257 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
267 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
280 static Elf32_Phdr ep[2];
281 static Elf32_Shdr es[2];
287 if (!(ino = lookup(kname))) {
289 printf("No %s\n", kname);
292 if (xfsread(ino, &hdr, sizeof(hdr)))
295 if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
296 addr = hdr.ex.a_entry & 0xffffff;
299 if (xfsread(ino, p, hdr.ex.a_text))
301 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
302 if (xfsread(ino, p, hdr.ex.a_data))
304 } else if (IS_ELF(hdr.eh)) {
305 fs_off = hdr.eh.e_phoff;
306 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
307 if (xfsread(ino, ep + j, sizeof(ep[0])))
309 if (ep[j].p_type == PT_LOAD)
312 for (i = 0; i < 2; i++) {
313 p = PTOV(ep[i].p_paddr & 0xffffff);
314 fs_off = ep[i].p_offset;
315 if (xfsread(ino, p, ep[i].p_filesz))
318 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
319 bootinfo.bi_symtab = VTOP(p);
320 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
321 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
322 (hdr.eh.e_shstrndx + 1);
323 if (xfsread(ino, &es, sizeof(es)))
325 for (i = 0; i < 2; i++) {
326 *(Elf32_Word *)p = es[i].sh_size;
327 p += sizeof(es[i].sh_size);
328 fs_off = es[i].sh_offset;
329 if (xfsread(ino, p, es[i].sh_size))
334 addr = hdr.eh.e_entry & 0xffffff;
335 bootinfo.bi_esymtab = VTOP(p);
337 printf("Invalid %s\n", "format");
341 bootinfo.bi_kernelname = VTOP(kname);
342 bootinfo.bi_bios_dev = dsk.drive;
343 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
344 MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
345 0, 0, 0, VTOP(&bootinfo));
357 while ((c = *arg++)) {
358 if (c == ' ' || c == '\t' || c == '\n')
360 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
365 while ((c = *arg++)) {
367 if (*(uint8_t *)PTOV(0x496) & 0x10) {
370 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
373 printf("Keyboard: %s\n", cp);
376 } else if (c == 'S') {
378 while ((unsigned int)(i = *arg++ - '0') <= 9)
380 if (j > 0 && i == -'0') {
384 /* Fall through to error below ('S' not in optstr[]). */
387 for (i = 0; c != optstr[i]; i++)
390 opts ^= OPT_SET(flags[i]);
393 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
394 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
396 if (sio_init(115200 / comspeed) != 0)
397 ioctrl &= ~IO_SERIAL;
401 for (q = arg--; *q && *q != '('; q++);
412 for (i = 0; arg[0] != dev_nm[i][0] ||
413 arg[1] != dev_nm[i][1]; i++)
418 dsk.unit = *arg - '0';
419 if (arg[1] != ',' || dsk.unit > 9)
422 dsk.slice = WHOLE_DISK_SLICE;
424 dsk.slice = *arg - '0' + 1;
425 if (dsk.slice > NDOSPART + 1)
431 dsk.part = *arg - 'a';
437 dsk.drive = (dsk.type <= TYPE_MAXHARD
438 ? DRV_HARD : 0) + drv;
441 if ((i = ep - arg)) {
442 if ((size_t)i >= sizeof(knamebuf))
444 memcpy(knamebuf, arg, i + 1);
454 dskread(void *buf, unsigned lba, unsigned nblk)
456 struct dos_partition *dp;
463 sec = dmadat->secbuf;
465 if (drvread(sec, DOSBBSECTOR, 1))
467 dp = (void *)(sec + DOSPARTOFF);
469 if (sl < BASE_SLICE) {
470 for (i = 0; i < NDOSPART; i++)
471 if (dp[i].dp_typ == DOSPTYP_386BSD &&
472 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
474 if (dp[i].dp_flag & 0x80 ||
475 dsk.slice == COMPATIBILITY_SLICE)
478 if (dsk.slice == WHOLE_DISK_SLICE)
481 if (sl != WHOLE_DISK_SLICE) {
482 if (sl != COMPATIBILITY_SLICE)
483 dp += sl - BASE_SLICE;
484 if (dp->dp_typ != DOSPTYP_386BSD) {
485 printf("Invalid %s\n", "slice");
488 dsk.start = dp->dp_start;
490 if (drvread(sec, dsk.start + LABELSECTOR, 1))
492 d = (void *)(sec + LABELOFFSET);
493 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
494 if (dsk.part != RAW_PART) {
495 printf("Invalid %s\n", "label");
500 if (d->d_type == DTYPE_SCSI)
504 if (dsk.part >= d->d_npartitions ||
505 !d->d_partitions[dsk.part].p_size) {
506 printf("Invalid %s\n", "partition");
509 dsk.start += d->d_partitions[dsk.part].p_offset;
510 dsk.start -= d->d_partitions[RAW_PART].p_offset;
513 return drvread(buf, dsk.start + lba, nblk);
517 printf(const char *fmt,...)
526 while ((c = *fmt++)) {
531 putchar(va_arg(ap, int));
534 for (s = va_arg(ap, char *); *s; s++)
538 u = va_arg(ap, unsigned);
541 *s++ = '0' + u % 10U;
563 drvread(void *buf, unsigned lba, unsigned nblk)
565 static unsigned c = 0x2d5c7c2f;
567 if (!OPT_CHECK(RBX_QUIET))
568 printf("%c\b", c = c << 8 | c >> 24);
569 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
570 v86.addr = XREADORG; /* call to xread in boot1 */
571 v86.es = VTOPSEG(buf);
573 v86.ebx = VTOPOFF(buf);
575 v86.edx = nblk << 8 | dsk.drive;
578 if (V86_CY(v86.efl)) {
579 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
586 keyhit(unsigned ticks)
590 if (OPT_CHECK(RBX_NOINTR))
596 t1 = *(uint32_t *)PTOV(0x46c);
599 if ((uint32_t)(t1 - t0) >= ticks)
620 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
626 if (OPT_CHECK(RBX_NOINTR))
629 if (DO_KBD && getc(1))
630 return fn ? 1 : getc(0);
631 if (DO_SIO && sio_ischar())
632 return fn ? 1 : sio_getc();