]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/mips/beri/boot2/boot2.c
Move all the separate copies of the same strings into paths.h. There's
[FreeBSD/FreeBSD.git] / sys / boot / mips / beri / boot2 / boot2.c
1 /*-
2  * Copyright (c) 2013-2014 Robert N. M. Watson
3  * All rights reserved.
4  *
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.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * Copyright (c) 1998 Robert Nordier
31  * All rights reserved.
32  *
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
36  * such forms.
37  *
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
41  * purpose.
42  */
43
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
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>
53
54 #include <machine/bootinfo.h>
55 #include <machine/elf.h>
56
57 #include <stand.h>
58 #include <stdarg.h>
59 #include <string.h>
60
61 #include <beri.h>
62 #include <cfi.h>
63 #include <cons.h>
64 #include <mips.h>
65 #include <sdcard.h>
66
67 #include "paths.h"
68
69 static int               beri_argc;
70 static const char       **beri_argv, **beri_envv;
71 static uint64_t          beri_memsize;
72
73 #define IO_KEYBOARD     1
74 #define IO_SERIAL       2
75
76 #define SECOND          1       /* Circa that many ticks in a second. */
77
78 #define RBX_ASKNAME     0x0     /* -a */
79 #define RBX_SINGLE      0x1     /* -s */
80 /* 0x2 is reserved for log2(RB_NOSYNC). */
81 /* 0x3 is reserved for log2(RB_HALT). */
82 /* 0x4 is reserved for log2(RB_INITNAME). */
83 #define RBX_DFLTROOT    0x5     /* -r */
84 #define RBX_KDB         0x6     /* -d */
85 /* 0x7 is reserved for log2(RB_RDONLY). */
86 /* 0x8 is reserved for log2(RB_DUMP). */
87 /* 0x9 is reserved for log2(RB_MINIROOT). */
88 #define RBX_CONFIG      0xa     /* -c */
89 #define RBX_VERBOSE     0xb     /* -v */
90 #define RBX_SERIAL      0xc     /* -h */
91 #define RBX_CDROM       0xd     /* -C */
92 /* 0xe is reserved for log2(RB_POWEROFF). */
93 #define RBX_GDB         0xf     /* -g */
94 #define RBX_MUTE        0x10    /* -m */
95 /* 0x11 is reserved for log2(RB_SELFTEST). */
96 /* 0x12 is reserved for boot programs. */
97 /* 0x13 is reserved for boot programs. */
98 #define RBX_PAUSE       0x14    /* -p */
99 #define RBX_QUIET       0x15    /* -q */
100 #define RBX_NOINTR      0x1c    /* -n */
101 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
102 #define RBX_DUAL        0x1d    /* -D */
103 /* 0x1f is reserved for log2(RB_BOOTINFO). */
104
105 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -D */
106 #define RBX_MASK        (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
107                         OPT_SET(RBX_DFLTROOT) | OPT_SET(RBX_KDB ) | \
108                         OPT_SET(RBX_CONFIG) | OPT_SET(RBX_VERBOSE) | \
109                         OPT_SET(RBX_SERIAL) | OPT_SET(RBX_CDROM) | \
110                         OPT_SET(RBX_GDB ) | OPT_SET(RBX_MUTE) | \
111                         OPT_SET(RBX_PAUSE) | OPT_SET(RBX_DUAL))
112
113 #define ARGS            0x900
114 #define NOPT            14
115 #define MEM_BASE        0x12
116 #define MEM_EXT         0x15
117
118 /*
119  * XXXRW: I think this has to do with whether boot2 expects a partition
120  * table?
121  */
122 #define DRV_HARD        0x80
123 #define DRV_MASK        0x7f
124
125 /* Default to using CFI flash. */
126 #define TYPE_DEFAULT    BOOTINFO_DEV_TYPE_SDCARD
127
128 /* Hard-coded assumption about location of JTAG-loaded kernel. */
129 #define DRAM_KERNEL_ADDR        ((void *)mips_phys_to_cached(0x20000))
130
131 #define OPT_SET(opt)    (1 << (opt))
132 #define OPT_CHECK(opt)  ((opts) & OPT_SET(opt))
133
134 extern uint32_t _end;
135
136 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
137 static const unsigned char flags[NOPT] = {
138     RBX_DUAL,
139     RBX_SERIAL,
140     RBX_ASKNAME,
141     RBX_CDROM,
142     RBX_CONFIG,
143     RBX_KDB,
144     RBX_GDB,
145     RBX_MUTE,
146     RBX_NOINTR,
147     RBX_PAUSE,
148     RBX_QUIET,
149     RBX_DFLTROOT,
150     RBX_SINGLE,
151     RBX_VERBOSE
152 };
153
154 /* These must match BOOTINFO_DEV_TYPE constants. */
155 static const char *const dev_nm[] = {"dram", "cfi", "sdcard"};
156 static const u_int dev_nm_count = sizeof(dev_nm) / sizeof(dev_nm[0]);
157
158 static struct dmadat __dmadat;
159
160 static struct dsk {
161     unsigned type;              /* BOOTINFO_DEV_TYPE_x object type. */
162     uintptr_t unitptr;          /* Unit number or pointer to object. */
163     uint8_t slice;
164     uint8_t part;
165 #if 0
166     unsigned start;
167     int init;
168 #endif
169 } dsk;
170 static char cmd[512], cmddup[512], knamebuf[1024];
171 static const char *kname;
172 static uint32_t opts;
173 #if 0
174 static int comspeed = SIOSPD;
175 #endif
176 struct bootinfo bootinfo;
177 static uint8_t ioctrl = IO_KEYBOARD;
178
179 void exit(int);
180 void putchar(int);
181 static void boot_fromdram(void);
182 static void boot_fromfs(void);
183 static void load(void);
184 static int parse(void);
185 static int dskread(void *, unsigned, unsigned);
186 static int xputc(int);
187 static int xgetc(int);
188
189
190 #define UFS_SMALL_CGBASE
191 #include "ufsread.c"
192
193 static inline int
194 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
195 {
196     if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
197         printf("Invalid %s\n", "format");
198         return -1;
199     }
200     return 0;
201 }
202
203 static inline void
204 getstr(void)
205 {
206     char *s;
207     int c;
208
209     s = cmd;
210     for (;;) {
211         switch (c = xgetc(0)) {
212         case 0:
213             break;
214         case '\177':
215         case '\b':
216             if (s > cmd) {
217                 s--;
218                 printf("\b \b");
219             }
220             break;
221         case '\n':
222         case '\r':
223                 putchar('\n');
224             *s = 0;
225             return;
226         default:
227             if (s - cmd < sizeof(cmd) - 1)
228                 *s++ = c;
229             putchar(c);
230         }
231     }
232 }
233
234 int
235 main(u_int argc, const char *argv[], const char *envv[], uint64_t memsize)
236 {
237     uint8_t autoboot;
238     ufs_ino_t ino;
239     size_t nbyte;
240
241     /* Arguments from Miniboot. */
242     beri_argc = argc;
243     beri_argv = argv;
244     beri_envv = envv;
245     beri_memsize = memsize;
246
247     dmadat = &__dmadat;
248 #if 0
249     /* XXXRW: more here. */
250     v86.ctl = V86_FLAGS;
251     v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
252     dsk.drive = *(uint8_t *)PTOV(ARGS);
253 #endif
254     dsk.type = TYPE_DEFAULT;
255 #if 0
256     dsk.unit = dsk.drive & DRV_MASK;
257     dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
258 #endif
259     bootinfo.bi_version = BOOTINFO_VERSION;
260     bootinfo.bi_size = sizeof(bootinfo);
261
262     /* Process configuration file */
263
264     autoboot = 1;
265
266     if ((ino = lookup(PATH_CONFIG)) ||
267         (ino = lookup(PATH_DOTCONFIG))) {
268         nbyte = fsread(ino, cmd, sizeof(cmd) - 1);
269         cmd[nbyte] = '\0';
270     }
271
272     if (*cmd) {
273         memcpy(cmddup, cmd, sizeof(cmd));
274         if (parse())
275             autoboot = 0;
276         if (!OPT_CHECK(RBX_QUIET))
277             printf("%s: %s", PATH_CONFIG, cmddup);
278         /* Do not process this command twice */
279         *cmd = 0;
280     }
281
282     /*
283      * Try to exec stage 3 boot loader. If interrupted by a keypress,
284      * or in case of failure, try to load a kernel directly instead.
285      */
286
287     if (!kname) {
288         kname = PATH_BOOT3;
289         if (autoboot && !keyhit(3*SECOND)) {
290             boot_fromfs();
291             kname = PATH_KERNEL;
292         }
293     }
294
295     /* Present the user with the boot2 prompt. */
296
297     for (;;) {
298         if (!autoboot || !OPT_CHECK(RBX_QUIET))
299             printf("\nFreeBSD/mips boot\n"
300                    "Default: %s%ju:%s\n"
301                    "boot: ",
302                    dev_nm[dsk.type], dsk.unitptr, kname);
303 #if 0
304         if (ioctrl & IO_SERIAL)
305             sio_flush();
306 #endif
307         if (!autoboot || keyhit(3*SECOND))
308             getstr();
309         else if (!autoboot || !OPT_CHECK(RBX_QUIET))
310             putchar('\n');
311         autoboot = 0;
312         if (parse())
313             putchar('\a');
314         else
315             load();
316     }
317 }
318
319 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
320 void
321 exit(int x)
322 {
323 }
324
325 static void
326 boot(void *entryp, int argc, const char *argv[], const char *envv[])
327 {
328
329     bootinfo.bi_kernelname = (bi_ptr_t)kname;
330     bootinfo.bi_boot2opts = opts & RBX_MASK;
331     bootinfo.bi_boot_dev_type = dsk.type;
332     bootinfo.bi_boot_dev_unitptr = dsk.unitptr;
333     bootinfo.bi_memsize = beri_memsize;
334 #if 0
335     /*
336      * XXXRW: A possible future way to distinguish Miniboot passing a memory
337      * size vs DTB..?
338      */
339     if (beri_memsize <= BERI_MEMVSDTB)
340         bootinfo.bi_memsize = beri_memsize;
341     else
342         bootinfo.bi_dtb = beri_memsize;
343 #endif
344     ((void(*)(int, const char **, const char **, void *))entryp)(argc, argv,
345       envv, &bootinfo);
346 }
347
348 /*
349  * Boot a kernel that has mysteriously (i.e., by JTAG) appeared in DRAM;
350  * assume that it is already properly relocated, etc, and invoke its entry
351  * address without question or concern.
352  */
353 static void
354 boot_fromdram(void)
355 {
356     void *kaddr = DRAM_KERNEL_ADDR;     /* XXXRW: Something better here. */
357     Elf64_Ehdr *ehp = kaddr;
358
359     if (!IS_ELF(*ehp)) {
360         printf("Invalid %s\n", "format");
361         return;
362     }
363     boot((void *)ehp->e_entry, beri_argc, beri_argv, beri_envv);
364 }
365
366 static void
367 boot_fromfs(void)
368 {
369     union {
370         Elf64_Ehdr eh;
371     } hdr;
372     static Elf64_Phdr ep[2];
373 #if 0
374     static Elf64_Shdr es[2];
375 #endif
376     caddr_t p;
377     ufs_ino_t ino;
378     uint64_t addr;
379     int i, j;
380
381     if (!(ino = lookup(kname))) {
382         if (!ls)
383             printf("No %s\n", kname);
384         return;
385     }
386     if (xfsread(ino, &hdr, sizeof(hdr)))
387         return;
388
389     if (IS_ELF(hdr.eh)) {
390         fs_off = hdr.eh.e_phoff;
391         for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
392             if (xfsread(ino, ep + j, sizeof(ep[0])))
393                 return;
394             if (ep[j].p_type == PT_LOAD)
395                 j++;
396         }
397         for (i = 0; i < 2; i++) {
398             p = (caddr_t)ep[i].p_paddr;
399             fs_off = ep[i].p_offset;
400             if (xfsread(ino, p, ep[i].p_filesz))
401                 return;
402         }
403         p += roundup2(ep[1].p_memsz, PAGE_SIZE);
404 #if 0
405         bootinfo.bi_symtab = VTOP(p);
406         if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
407             fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
408                 (hdr.eh.e_shstrndx + 1);
409             if (xfsread(ino, &es, sizeof(es)))
410                 return;
411             for (i = 0; i < 2; i++) {
412                 *(Elf32_Word *)p = es[i].sh_size;
413                 p += sizeof(es[i].sh_size);
414                 fs_off = es[i].sh_offset;
415                 if (xfsread(ino, p, es[i].sh_size))
416                     return;
417                 p += es[i].sh_size;
418             }
419         }
420 #endif
421         addr = hdr.eh.e_entry;
422 #if 0
423         bootinfo.bi_esymtab = VTOP(p);
424 #endif
425     } else {
426         printf("Invalid %s\n", "format");
427         return;
428     }
429     boot((void *)addr, beri_argc, beri_argv, beri_envv);
430 }
431
432 static void
433 load(void)
434 {
435
436         switch (dsk.type) {
437         case BOOTINFO_DEV_TYPE_DRAM:
438                 boot_fromdram();
439                 break;
440
441         default:
442                 boot_fromfs();
443                 break;
444         }
445 }
446
447 static int
448 parse()
449 {
450     char *arg = cmd;
451     char *ep, *p, *q;
452     char unit;
453     size_t len;
454     const char *cp;
455 #if 0
456     int c, i, j;
457 #else
458     int c, i;
459 #endif
460
461     while ((c = *arg++)) {
462         if (c == ' ' || c == '\t' || c == '\n')
463             continue;
464         for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
465         ep = p;
466         if (*p)
467             *p++ = 0;
468         if (c == '-') {
469             while ((c = *arg++)) {
470                 if (c == 'P') {
471                         cp = "yes";
472 #if 0
473                     } else {
474                         opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
475                         cp = "no";
476                     }
477 #endif
478                     printf("Keyboard: %s\n", cp);
479                     continue;
480 #if 0
481                 } else if (c == 'S') {
482                     j = 0;
483                     while ((unsigned int)(i = *arg++ - '0') <= 9)
484                         j = j * 10 + i;
485                     if (j > 0 && i == -'0') {
486                         comspeed = j;
487                         break;
488                     }
489                     /* Fall through to error below ('S' not in optstr[]). */
490 #endif
491                 }
492                 for (i = 0; c != optstr[i]; i++)
493                     if (i == NOPT - 1)
494                         return -1;
495                 opts ^= OPT_SET(flags[i]);
496             }
497             ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
498                      OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
499 #if 0
500             if (ioctrl & IO_SERIAL) {
501                 if (sio_init(115200 / comspeed) != 0)
502                     ioctrl &= ~IO_SERIAL;
503             }
504 #endif
505         } else {
506             /*-
507              * Parse a device/kernel name.  Format(s):
508              *
509              *   path
510              *   deviceX:path
511              *
512              * NB: Utterly incomprehensible but space-efficient ARM/i386
513              * parsing removed in favour of larger but easier-to-read C.  This
514              * is still not great, however -- e.g., relating to unit handling.
515              *
516              * TODO: it would be nice if a DRAM pointer could be specified
517              * here.
518              *
519              * XXXRW: Pick up pieces here.
520              */
521
522             /*
523              * Search for a parens; if none, then it's just a path.
524              * Otherwise, it's a devicename.
525              */
526             arg--;
527             q = strsep(&arg, ":");
528             if (arg != NULL) {
529                 len = strlen(q);
530                 if (len < 2) {
531                     printf("Invalid device: name too short\n");
532                     return (-1);
533                 }
534
535                 /*
536                  * First, handle one-digit unit.
537                  */
538                 unit = q[len-1];
539                 if (unit < '0' || unit > '9') {
540                     printf("Invalid device: invalid unit\n", q,
541                       unit);
542                     return (-1);
543                 }
544                 unit -= '0';
545                 q[len-1] = '\0';
546
547                 /*
548                  * Next, find matching device.
549                  */
550                 for (i = 0; i < dev_nm_count; i++) {
551                     if (strcmp(q, dev_nm[i]) == 0)
552                         break;
553                 }
554                 if (i == dev_nm_count) {
555                     printf("Invalid device: no driver match\n");
556                     return (-1);
557                 }
558                 dsk.type = i;
559                 dsk.unitptr = unit;     /* Someday: also a DRAM pointer? */
560             } else
561                 arg = q;
562             if ((i = ep - arg)) {
563                 if ((size_t)i >= sizeof(knamebuf))
564                     return -1;
565                 memcpy(knamebuf, arg, i + 1);
566                 kname = knamebuf;
567             }
568         }
569         arg = p;
570     }
571     return 0;
572 }
573
574 static int
575 drvread(void *buf, unsigned lba, unsigned nblk)
576 {
577
578         /* XXXRW: eventually, we may want to pass 'drive' and 'unit' here. */
579         switch (dsk.type) {
580         case BOOTINFO_DEV_TYPE_CFI:
581                 return (cfi_read(buf, lba, nblk));
582
583         case BOOTINFO_DEV_TYPE_SDCARD:
584                 return (altera_sdcard_read(buf, lba, nblk));
585
586         default:
587                 return (-1);
588         }
589 }
590
591 static int
592 dskread(void *buf, unsigned lba, unsigned nblk)
593 {
594 #if 0
595     /*
596      * XXXRW: For now, assume no partition table around the file system; it's
597      * just in raw flash.
598      */
599     struct dos_partition *dp;
600     struct disklabel *d;
601     char *sec;
602     unsigned i;
603     uint8_t sl;
604
605     if (!dsk_meta) {
606         sec = dmadat->secbuf;
607         dsk.start = 0;
608         if (drvread(sec, DOSBBSECTOR, 1))
609             return -1;
610         dp = (void *)(sec + DOSPARTOFF);
611         sl = dsk.slice;
612         if (sl < BASE_SLICE) {
613             for (i = 0; i < NDOSPART; i++)
614                 if (dp[i].dp_typ == DOSPTYP_386BSD &&
615                     (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
616                     sl = BASE_SLICE + i;
617                     if (dp[i].dp_flag & 0x80 ||
618                         dsk.slice == COMPATIBILITY_SLICE)
619                         break;
620                 }
621             if (dsk.slice == WHOLE_DISK_SLICE)
622                 dsk.slice = sl;
623         }
624         if (sl != WHOLE_DISK_SLICE) {
625             if (sl != COMPATIBILITY_SLICE)
626                 dp += sl - BASE_SLICE;
627             if (dp->dp_typ != DOSPTYP_386BSD) {
628                 printf("Invalid %s\n", "slice");
629                 return -1;
630             }
631             dsk.start = le32toh(dp->dp_start);
632         }
633         if (drvread(sec, dsk.start + LABELSECTOR, 1))
634                 return -1;
635         d = (void *)(sec + LABELOFFSET);
636         if (le32toh(d->d_magic) != DISKMAGIC ||
637             le32toh(d->d_magic2) != DISKMAGIC) {
638             if (dsk.part != RAW_PART) {
639                 printf("Invalid %s\n", "label");
640                 return -1;
641             }
642         } else {
643             if (!dsk.init) {
644                 if (le16toh(d->d_type) == DTYPE_SCSI)
645                     dsk.type = TYPE_DA;
646                 dsk.init++;
647             }
648             if (dsk.part >= le16toh(d->d_npartitions) ||
649                 !(le32toh(d->d_partitions[dsk.part].p_size))) {
650                 printf("Invalid %s\n", "partition");
651                 return -1;
652             }
653             dsk.start += le32toh(d->d_partitions[dsk.part].p_offset);
654             dsk.start -= le32toh(d->d_partitions[RAW_PART].p_offset);
655         }
656     }
657     return drvread(buf, dsk.start + lba, nblk);
658 #else
659     return drvread(buf, lba, nblk);
660 #endif
661 }
662
663 void
664 putchar(int c)
665 {
666     if (c == '\n')
667         xputc('\r');
668     xputc(c);
669 }
670
671 static int
672 xputc(int c)
673 {
674     if (ioctrl & IO_KEYBOARD)
675         putc(c);
676 #if 0
677     if (ioctrl & IO_SERIAL)
678         sio_putc(c);
679 #endif
680     return c;
681 }
682
683 static int
684 xgetc(int fn)
685 {
686     if (OPT_CHECK(RBX_NOINTR))
687         return 0;
688     for (;;) {
689         if (ioctrl & IO_KEYBOARD && keyhit(0))
690             return fn ? 1 : getc();
691 #if 0
692         if (ioctrl & IO_SERIAL && sio_ischar())
693             return fn ? 1 : sio_getc();
694 #endif
695         if (fn)
696             return 0;
697     }
698 }