2 * Copyright (c) 2013-2014 Robert N. M. Watson
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Copyright (c) 1998 Robert Nordier
31 * All rights reserved.
33 * Redistribution and use in source and binary forms are freely
34 * permitted provided that the above copyright notice and this
35 * paragraph and the following disclaimer are duplicated in all
38 * This software is provided "AS IS" and without any express or
39 * implied warranties, including, without limitation, the implied
40 * warranties of merchantability and fitness for a particular
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/disklabel.h>
49 #include <sys/diskmbr.h>
50 #include <sys/dirent.h>
51 #include <sys/endian.h>
52 #include <sys/reboot.h>
54 #include <machine/bootinfo.h>
55 #include <machine/elf.h>
71 static const char **beri_argv, **beri_envv;
72 static uint64_t beri_memsize;
77 #define SECOND 1 /* Circa that many ticks in a second. */
85 * XXXRW: I think this has to do with whether boot2 expects a partition
91 /* Default to using CFI flash. */
92 #define TYPE_DEFAULT BOOTINFO_DEV_TYPE_SDCARD
94 /* Hard-coded assumption about location of JTAG-loaded kernel. */
95 #define DRAM_KERNEL_ADDR ((void *)mips_phys_to_cached(0x20000))
99 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
100 static const unsigned char flags[NOPT] = {
117 /* These must match BOOTINFO_DEV_TYPE constants. */
118 static const char *const dev_nm[] = {"dram", "cfi", "sdcard"};
119 static const u_int dev_nm_count = nitems(dev_nm);
122 unsigned type; /* BOOTINFO_DEV_TYPE_x object type. */
123 uintptr_t unitptr; /* Unit number or pointer to object. */
131 static char cmd[512], cmddup[512], knamebuf[1024];
132 static const char *kname;
135 static int comspeed = SIOSPD;
137 struct bootinfo bootinfo;
138 static uint8_t ioctrl = IO_KEYBOARD;
142 static void boot_fromdram(void);
143 static void boot_fromfs(void);
144 static void load(void);
145 static int parse(void);
146 static int dskread(void *, unsigned, unsigned);
147 static int xputc(int);
148 static int xgetc(int);
150 #define UFS_SMALL_CGBASE
153 static struct dmadat __dmadat;
156 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
158 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
159 printf("Invalid %s\n", "format");
173 switch (c = xgetc(0)) {
189 if (s - cmd < sizeof(cmd) - 1)
197 main(u_int argc, const char *argv[], const char *envv[], uint64_t memsize)
203 /* Arguments from Miniboot. */
207 beri_memsize = memsize;
211 /* XXXRW: more here. */
213 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
214 dsk.drive = *(uint8_t *)PTOV(ARGS);
216 dsk.type = TYPE_DEFAULT;
218 dsk.unit = dsk.drive & DRV_MASK;
219 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
221 bootinfo.bi_version = BOOTINFO_VERSION;
222 bootinfo.bi_size = sizeof(bootinfo);
224 /* Process configuration file */
228 if ((ino = lookup(PATH_CONFIG)) ||
229 (ino = lookup(PATH_DOTCONFIG))) {
230 nbyte = fsread(ino, cmd, sizeof(cmd) - 1);
235 memcpy(cmddup, cmd, sizeof(cmd));
238 if (!OPT_CHECK(RBX_QUIET))
239 printf("%s: %s", PATH_CONFIG, cmddup);
240 /* Do not process this command twice */
245 * Try to exec stage 3 boot loader. If interrupted by a keypress,
246 * or in case of failure, try to load a kernel directly instead.
251 if (autoboot && !keyhit(3*SECOND)) {
257 /* Present the user with the boot2 prompt. */
260 if (!autoboot || !OPT_CHECK(RBX_QUIET))
261 printf("\nFreeBSD/mips boot\n"
262 "Default: %s%ju:%s\n"
264 dev_nm[dsk.type], dsk.unitptr, kname);
266 if (ioctrl & IO_SERIAL)
269 if (!autoboot || keyhit(3*SECOND))
271 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
281 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
288 boot(void *entryp, int argc, const char *argv[], const char *envv[])
291 bootinfo.bi_kernelname = (bi_ptr_t)kname;
292 bootinfo.bi_boot2opts = opts & RBX_MASK;
293 bootinfo.bi_boot_dev_type = dsk.type;
294 bootinfo.bi_boot_dev_unitptr = dsk.unitptr;
295 bootinfo.bi_memsize = beri_memsize;
298 * XXXRW: A possible future way to distinguish Miniboot passing a memory
301 if (beri_memsize <= BERI_MEMVSDTB)
302 bootinfo.bi_memsize = beri_memsize;
304 bootinfo.bi_dtb = beri_memsize;
306 ((void(*)(int, const char **, const char **, void *))entryp)(argc, argv,
311 * Boot a kernel that has mysteriously (i.e., by JTAG) appeared in DRAM;
312 * assume that it is already properly relocated, etc, and invoke its entry
313 * address without question or concern.
318 void *kaddr = DRAM_KERNEL_ADDR; /* XXXRW: Something better here. */
319 Elf64_Ehdr *ehp = kaddr;
322 printf("Invalid %s\n", "format");
325 boot((void *)ehp->e_entry, beri_argc, beri_argv, beri_envv);
334 static Elf64_Phdr ep[2];
336 static Elf64_Shdr es[2];
343 if (!(ino = lookup(kname))) {
345 printf("No %s\n", kname);
348 if (xfsread(ino, &hdr, sizeof(hdr)))
351 if (IS_ELF(hdr.eh)) {
352 fs_off = hdr.eh.e_phoff;
353 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
354 if (xfsread(ino, ep + j, sizeof(ep[0])))
356 if (ep[j].p_type == PT_LOAD)
359 for (i = 0; i < 2; i++) {
360 p = (caddr_t)ep[i].p_paddr;
361 fs_off = ep[i].p_offset;
362 if (xfsread(ino, p, ep[i].p_filesz))
365 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
367 bootinfo.bi_symtab = VTOP(p);
368 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
369 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
370 (hdr.eh.e_shstrndx + 1);
371 if (xfsread(ino, &es, sizeof(es)))
373 for (i = 0; i < 2; i++) {
374 *(Elf32_Word *)p = es[i].sh_size;
375 p += sizeof(es[i].sh_size);
376 fs_off = es[i].sh_offset;
377 if (xfsread(ino, p, es[i].sh_size))
383 addr = hdr.eh.e_entry;
385 bootinfo.bi_esymtab = VTOP(p);
388 printf("Invalid %s\n", "format");
391 boot((void *)addr, beri_argc, beri_argv, beri_envv);
399 case BOOTINFO_DEV_TYPE_DRAM:
423 while ((c = *arg++)) {
424 if (c == ' ' || c == '\t' || c == '\n')
426 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
431 while ((c = *arg++)) {
436 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
440 printf("Keyboard: %s\n", cp);
443 } else if (c == 'S') {
445 while ((unsigned int)(i = *arg++ - '0') <= 9)
447 if (j > 0 && i == -'0') {
451 /* Fall through to error below ('S' not in optstr[]). */
454 for (i = 0; c != optstr[i]; i++)
457 opts ^= OPT_SET(flags[i]);
459 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
460 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
462 if (ioctrl & IO_SERIAL) {
463 if (sio_init(115200 / comspeed) != 0)
464 ioctrl &= ~IO_SERIAL;
469 * Parse a device/kernel name. Format(s):
474 * NB: Utterly incomprehensible but space-efficient ARM/i386
475 * parsing removed in favour of larger but easier-to-read C. This
476 * is still not great, however -- e.g., relating to unit handling.
478 * TODO: it would be nice if a DRAM pointer could be specified
481 * XXXRW: Pick up pieces here.
485 * Search for a parens; if none, then it's just a path.
486 * Otherwise, it's a devicename.
489 q = strsep(&arg, ":");
493 printf("Invalid device: name too short\n");
498 * First, handle one-digit unit.
501 if (unit < '0' || unit > '9') {
502 printf("Invalid device: invalid unit %c\n",
510 * Next, find matching device.
512 for (i = 0; i < dev_nm_count; i++) {
513 if (strcmp(q, dev_nm[i]) == 0)
516 if (i == dev_nm_count) {
517 printf("Invalid device: no driver match\n");
521 dsk.unitptr = unit; /* Someday: also a DRAM pointer? */
524 if ((i = ep - arg)) {
525 if ((size_t)i >= sizeof(knamebuf))
527 memcpy(knamebuf, arg, i + 1);
537 drvread(void *buf, unsigned lba, unsigned nblk)
540 /* XXXRW: eventually, we may want to pass 'drive' and 'unit' here. */
542 case BOOTINFO_DEV_TYPE_CFI:
543 return (cfi_read(buf, lba, nblk));
545 case BOOTINFO_DEV_TYPE_SDCARD:
546 return (altera_sdcard_read(buf, lba, nblk));
554 dskread(void *buf, unsigned lba, unsigned nblk)
558 * XXXRW: For now, assume no partition table around the file system; it's
561 struct dos_partition *dp;
568 sec = dmadat->secbuf;
570 if (drvread(sec, DOSBBSECTOR, 1))
572 dp = (void *)(sec + DOSPARTOFF);
574 if (sl < BASE_SLICE) {
575 for (i = 0; i < NDOSPART; i++)
576 if (dp[i].dp_typ == DOSPTYP_386BSD &&
577 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
579 if (dp[i].dp_flag & 0x80 ||
580 dsk.slice == COMPATIBILITY_SLICE)
583 if (dsk.slice == WHOLE_DISK_SLICE)
586 if (sl != WHOLE_DISK_SLICE) {
587 if (sl != COMPATIBILITY_SLICE)
588 dp += sl - BASE_SLICE;
589 if (dp->dp_typ != DOSPTYP_386BSD) {
590 printf("Invalid %s\n", "slice");
593 dsk.start = le32toh(dp->dp_start);
595 if (drvread(sec, dsk.start + LABELSECTOR, 1))
597 d = (void *)(sec + LABELOFFSET);
598 if (le32toh(d->d_magic) != DISKMAGIC ||
599 le32toh(d->d_magic2) != DISKMAGIC) {
600 if (dsk.part != RAW_PART) {
601 printf("Invalid %s\n", "label");
606 if (le16toh(d->d_type) == DTYPE_SCSI)
610 if (dsk.part >= le16toh(d->d_npartitions) ||
611 !(le32toh(d->d_partitions[dsk.part].p_size))) {
612 printf("Invalid %s\n", "partition");
615 dsk.start += le32toh(d->d_partitions[dsk.part].p_offset);
616 dsk.start -= le32toh(d->d_partitions[RAW_PART].p_offset);
619 return drvread(buf, dsk.start + lba, nblk);
621 return drvread(buf, lba, nblk);
636 if (ioctrl & IO_KEYBOARD)
639 if (ioctrl & IO_SERIAL)
648 if (OPT_CHECK(RBX_NOINTR))
651 if (ioctrl & IO_KEYBOARD && keyhit(0))
652 return fn ? 1 : getc();
654 if (ioctrl & IO_SERIAL && sio_ischar())
655 return fn ? 1 : sio_getc();