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>
21 #include <sys/dirent.h>
22 #include <sys/reboot.h>
24 #include <machine/bootinfo.h>
25 #include <machine/elf.h>
26 #include <machine/psl.h>
39 #define SECOND 18 /* Circa that many ticks in a second. */
41 #define RBX_ASKNAME 0x0 /* -a */
42 #define RBX_SINGLE 0x1 /* -s */
43 /* 0x2 is reserved for log2(RB_NOSYNC). */
44 /* 0x3 is reserved for log2(RB_HALT). */
45 /* 0x4 is reserved for log2(RB_INITNAME). */
46 #define RBX_DFLTROOT 0x5 /* -r */
47 #define RBX_KDB 0x6 /* -d */
48 /* 0x7 is reserved for log2(RB_RDONLY). */
49 /* 0x8 is reserved for log2(RB_DUMP). */
50 /* 0x9 is reserved for log2(RB_MINIROOT). */
51 #define RBX_CONFIG 0xa /* -c */
52 #define RBX_VERBOSE 0xb /* -v */
53 #define RBX_SERIAL 0xc /* -h */
54 #define RBX_CDROM 0xd /* -C */
55 /* 0xe is reserved for log2(RB_POWEROFF). */
56 #define RBX_GDB 0xf /* -g */
57 #define RBX_MUTE 0x10 /* -m */
58 /* 0x11 is reserved for log2(RB_SELFTEST). */
59 /* 0x12 is reserved for boot programs. */
60 /* 0x13 is reserved for boot programs. */
61 #define RBX_PAUSE 0x14 /* -p */
62 #define RBX_QUIET 0x15 /* -q */
63 #define RBX_NOINTR 0x1c /* -n */
64 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
65 #define RBX_DUAL 0x1d /* -D */
66 /* 0x1f is reserved for log2(RB_BOOTINFO). */
68 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -D */
69 #define RBX_MASK (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
70 OPT_SET(RBX_DFLTROOT) | OPT_SET(RBX_KDB ) | \
71 OPT_SET(RBX_CONFIG) | OPT_SET(RBX_VERBOSE) | \
72 OPT_SET(RBX_SERIAL) | OPT_SET(RBX_CDROM) | \
73 OPT_SET(RBX_GDB ) | OPT_SET(RBX_MUTE) | \
74 OPT_SET(RBX_PAUSE) | OPT_SET(RBX_DUAL))
76 #define PATH_CONFIG "/boot.config"
77 #define PATH_BOOT3 "/boot/loader"
78 #define PATH_KERNEL "/boot/kernel/kernel"
85 #define V86_CY(x) ((x) & PSL_C)
86 #define V86_ZR(x) ((x) & PSL_Z)
93 #define TYPE_MAXHARD TYPE_DA
96 #define OPT_SET(opt) (1 << (opt))
97 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
101 static const uuid_t freebsd_ufs_uuid = GPT_ENT_TYPE_FREEBSD_UFS;
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};
131 static char cmd[512], cmddup[512];
132 static char kname[1024];
133 static uint32_t opts;
134 static int comspeed = SIOSPD;
135 static struct bootinfo bootinfo;
136 static uint8_t ioctrl = IO_KEYBOARD;
139 static int bcmp(const void *, const void *, size_t);
140 static void load(void);
141 static int parse(void);
142 static int xfsread(ino_t, void *, size_t);
143 static int dskread(void *, daddr_t, unsigned);
144 static void printf(const char *,...);
145 static void putchar(int);
146 static void memcpy(void *, const void *, int);
147 static uint32_t memsize(void);
148 static int drvread(void *, daddr_t, unsigned);
149 static int keyhit(unsigned);
150 static int xputc(int);
151 static int xgetc(int);
152 static int getc(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 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
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;
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));
258 memcpy(cmddup, cmd, sizeof(cmd));
261 if (!OPT_CHECK(RBX_QUIET))
262 printf("%s: %s", PATH_CONFIG, cmddup);
263 /* Do not process this command twice */
268 * Try to exec stage 3 boot loader. If interrupted by a keypress,
269 * or in case of failure, try to load a kernel directly instead.
272 if (autoboot && !*kname) {
273 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
274 if (!keyhit(3*SECOND)) {
276 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
280 /* Present the user with the boot2 prompt. */
283 if (!autoboot || !OPT_CHECK(RBX_QUIET))
284 printf("\nFreeBSD/i386 boot\n"
285 "Default: %u:%s(%up%u)%s\n"
287 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
289 if (ioctrl & IO_SERIAL)
291 if (!autoboot || keyhit(5*SECOND))
293 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
303 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
316 static Elf32_Phdr ep[2];
317 static Elf32_Shdr es[2];
323 if (!(ino = lookup(kname))) {
325 printf("No %s\n", kname);
328 if (xfsread(ino, &hdr, sizeof(hdr)))
330 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
332 else if (IS_ELF(hdr.eh))
335 printf("Invalid %s\n", "format");
339 addr = hdr.ex.a_entry & 0xffffff;
342 if (xfsread(ino, p, hdr.ex.a_text))
344 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
345 if (xfsread(ino, p, hdr.ex.a_data))
347 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
348 bootinfo.bi_symtab = VTOP(p);
349 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
350 p += sizeof(hdr.ex.a_syms);
352 if (xfsread(ino, p, hdr.ex.a_syms))
355 if (xfsread(ino, p, sizeof(int)))
360 if (xfsread(ino, p, x))
365 fs_off = hdr.eh.e_phoff;
366 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
367 if (xfsread(ino, ep + j, sizeof(ep[0])))
369 if (ep[j].p_type == PT_LOAD)
372 for (i = 0; i < 2; i++) {
373 p = PTOV(ep[i].p_paddr & 0xffffff);
374 fs_off = ep[i].p_offset;
375 if (xfsread(ino, p, ep[i].p_filesz))
378 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
379 bootinfo.bi_symtab = VTOP(p);
380 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
381 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
382 (hdr.eh.e_shstrndx + 1);
383 if (xfsread(ino, &es, sizeof(es)))
385 for (i = 0; i < 2; i++) {
386 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
387 p += sizeof(es[i].sh_size);
388 fs_off = es[i].sh_offset;
389 if (xfsread(ino, p, es[i].sh_size))
394 addr = hdr.eh.e_entry & 0xffffff;
396 bootinfo.bi_esymtab = VTOP(p);
397 bootinfo.bi_kernelname = VTOP(kname);
398 bootinfo.bi_bios_dev = dsk.drive;
399 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
400 MAKEBOOTDEV(dev_maj[dsk.type], dsk.part + 1, dsk.unit, 0xff),
401 0, 0, 0, VTOP(&bootinfo));
413 while ((c = *arg++)) {
414 if (c == ' ' || c == '\t' || c == '\n')
416 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
421 while ((c = *arg++)) {
423 if (*(uint8_t *)PTOV(0x496) & 0x10) {
426 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
429 printf("Keyboard: %s\n", cp);
431 } else if (c == 'S') {
433 while ((unsigned int)(i = *arg++ - '0') <= 9)
435 if (j > 0 && i == -'0') {
439 /* Fall through to error below ('S' not in optstr[]). */
441 for (i = 0; c != optstr[i]; i++)
444 opts ^= OPT_SET(flags[i]);
446 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
447 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
448 if (ioctrl & IO_SERIAL)
449 sio_init(115200 / comspeed);
451 for (q = arg--; *q && *q != '('; q++);
462 for (i = 0; arg[0] != dev_nm[i][0] ||
463 arg[1] != dev_nm[i][1]; i++)
468 dsk.unit = *arg - '0';
469 if (arg[1] != 'p' || dsk.unit > 9)
472 dsk.part = *arg - '0';
473 if (dsk.part < 1 || dsk.part > 9)
481 dsk.drive = (dsk.type <= TYPE_MAXHARD
482 ? DRV_HARD : 0) + drv;
485 if ((i = ep - arg)) {
486 if ((size_t)i >= sizeof(kname))
488 memcpy(kname, arg, i + 1);
497 dskread(void *buf, daddr_t lba, unsigned nblk)
503 int part, entries_per_sec;
506 /* Read and verify GPT. */
507 sec = dmadat->secbuf;
509 if (drvread(sec, 1, 1))
511 memcpy(&hdr, sec, sizeof(hdr));
512 if (bcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0 ||
513 hdr.hdr_lba_self != 1 || hdr.hdr_revision < 0x00010000 ||
514 hdr.hdr_entsz < sizeof(*ent) || DEV_BSIZE % hdr.hdr_entsz != 0) {
515 printf("Invalid GPT header\n");
519 /* XXX: CRC check? */
522 * If the partition isn't specified, then search for the first UFS
523 * partition and hope it is /. Perhaps we should be using an OS
524 * flag in the GPT entry to mark / partitions.
526 * If the partition is specified, then figure out the LBA for the
527 * sector containing that partition index and load it.
529 entries_per_sec = DEV_BSIZE / hdr.hdr_entsz;
530 if (dsk.part == -1) {
531 slba = hdr.hdr_lba_table;
532 elba = slba + hdr.hdr_entries / entries_per_sec;
533 while (slba < elba && dsk.part == -1) {
534 if (drvread(sec, slba, 1))
536 for (part = 0; part < entries_per_sec; part++) {
537 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
538 if (bcmp(&ent->ent_type, &freebsd_ufs_uuid,
539 sizeof(uuid_t)) == 0) {
540 dsk.part = (slba - hdr.hdr_lba_table) *
541 entries_per_sec + part + 1;
542 dsk.start = ent->ent_lba_start;
548 if (dsk.part == -1) {
549 printf("No UFS partition was found\n");
553 if (dsk.part > hdr.hdr_entries) {
554 printf("Invalid partition index\n");
557 slba = hdr.hdr_lba_table + (dsk.part - 1) / entries_per_sec;
558 if (drvread(sec, slba, 1))
560 part = (dsk.part - 1) % entries_per_sec;
561 ent = (struct gpt_ent *)(sec + part * hdr.hdr_entsz);
562 if (bcmp(&ent->ent_type, &freebsd_ufs_uuid, sizeof(uuid_t)) != 0) {
563 printf("Specified partition is not UFS\n");
566 dsk.start = ent->ent_lba_start;
569 * XXX: No way to detect SCSI vs. ATA currently.
573 if (d->d_type == DTYPE_SCSI)
579 return drvread(buf, dsk.start + lba, nblk);
583 printf(const char *fmt,...)
592 while ((c = *fmt++)) {
597 putchar(va_arg(ap, int));
600 for (s = va_arg(ap, char *); *s; s++)
604 u = va_arg(ap, unsigned);
607 *s++ = '0' + u % 10U;
629 bcmp(const void *b1, const void *b2, size_t length)
631 const char *p1 = b1, *p2 = b2;
651 drvread(void *buf, daddr_t lba, unsigned nblk)
653 static unsigned c = 0x2d5c7c2f;
655 if (!OPT_CHECK(RBX_QUIET))
656 printf("%c\b", c = c << 8 | c >> 24);
659 packet.off = VTOPOFF(buf);
660 packet.seg = VTOPSEG(buf);
666 v86.ds = VTOPSEG(&packet);
667 v86.esi = VTOPOFF(&packet);
669 if (V86_CY(v86.efl)) {
670 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
677 keyhit(unsigned ticks)
681 if (OPT_CHECK(RBX_NOINTR))
687 t1 = *(uint32_t *)PTOV(0x46c);
690 if (t1 < t0 || t1 >= t0 + ticks)
698 if (ioctrl & IO_KEYBOARD)
700 if (ioctrl & IO_SERIAL)
708 if (OPT_CHECK(RBX_NOINTR))
711 if (ioctrl & IO_KEYBOARD && getc(1))
712 return fn ? 1 : getc(0);
713 if (ioctrl & IO_SERIAL && sio_ischar())
714 return fn ? 1 : sio_getc();
726 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);