]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/boot/arm/ixp425/boot2/boot2.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / boot / arm / ixp425 / boot2 / boot2.c
1 /*-
2  * Copyright (c) 2008 John Hay
3  * Copyright (c) 1998 Robert Nordier
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are freely
7  * permitted provided that the above copyright notice and this
8  * paragraph and the following disclaimer are duplicated in all
9  * such forms.
10  *
11  * This software is provided "AS IS" and without any express or
12  * implied warranties, including, without limitation, the implied
13  * warranties of merchantability and fitness for a particular
14  * purpose.
15  */
16
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
19
20 #include <sys/param.h>
21 #include <sys/disklabel.h>
22 #include <sys/diskmbr.h>
23 #include <sys/dirent.h>
24 #include <sys/reboot.h>
25
26 #include <machine/elf.h>
27
28 #include <stdarg.h>
29
30 #include "lib.h"
31
32 #define RBX_ASKNAME     0x0     /* -a */
33 #define RBX_SINGLE      0x1     /* -s */
34 /* 0x2 is reserved for log2(RB_NOSYNC). */
35 /* 0x3 is reserved for log2(RB_HALT). */
36 /* 0x4 is reserved for log2(RB_INITNAME). */
37 #define RBX_DFLTROOT    0x5     /* -r */
38 /* #define RBX_KDB      0x6        -d */
39 /* 0x7 is reserved for log2(RB_RDONLY). */
40 /* 0x8 is reserved for log2(RB_DUMP). */
41 /* 0x9 is reserved for log2(RB_MINIROOT). */
42 #define RBX_CONFIG      0xa     /* -c */
43 #define RBX_VERBOSE     0xb     /* -v */
44 /* #define RBX_SERIAL   0xc        -h */
45 /* #define RBX_CDROM    0xd        -C */
46 /* 0xe is reserved for log2(RB_POWEROFF). */
47 #define RBX_GDB         0xf     /* -g */
48 /* #define RBX_MUTE     0x10       -m */
49 /* 0x11 is reserved for log2(RB_SELFTEST). */
50 /* 0x12 is reserved for boot programs. */
51 /* 0x13 is reserved for boot programs. */
52 /* #define RBX_PAUSE    0x14       -p */
53 /* #define RBX_QUIET    0x15       -q */
54 #define RBX_NOINTR      0x1c    /* -n */
55 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
56 /* #define RBX_DUAL     0x1d       -D */
57 /* 0x1f is reserved for log2(RB_BOOTINFO). */
58
59 /* pass: -a, -s, -r, -v, -g */
60 #define RBX_MASK        (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
61                         OPT_SET(RBX_DFLTROOT) | \
62                         OPT_SET(RBX_VERBOSE) | \
63                         OPT_SET(RBX_GDB))
64
65 #define PATH_DOTCONFIG  "/boot.config"
66 #define PATH_CONFIG     "/boot/config"
67 #define PATH_KERNEL     "/boot/kernel/kernel"
68
69 extern uint32_t _end;
70
71 #define NOPT            6
72
73 #define OPT_SET(opt)    (1 << (opt))
74 #define OPT_CHECK(opt)  ((opts) & OPT_SET(opt))
75
76 static const char optstr[NOPT] = "agnrsv";
77 static const unsigned char flags[NOPT] = {
78         RBX_ASKNAME,
79         RBX_GDB,
80         RBX_NOINTR,
81         RBX_DFLTROOT,
82         RBX_SINGLE,
83         RBX_VERBOSE
84 };
85
86 static unsigned dsk_start;
87 static char cmd[512];
88 static char kname[1024];
89 static uint32_t opts;
90 static uint8_t dsk_meta;
91 static int bootslice;
92 static int bootpart;
93 static int disk_layout;
94 #define DL_UNKNOWN      0
95 #define DL_RAW          1       /* Dangerously dedicated */
96 #define DL_SLICE        2       /* Use only slices (DOS partitions) */
97 #define DL_SLICEPART    3       /* Use slices and partitions */
98
99 static void load(void);
100 static int parse(void);
101 static int xfsread(ino_t, void *, size_t);
102 static int dskread(void *, unsigned, unsigned);
103 static int drvread(void *, unsigned, unsigned);
104 #ifdef FIXUP_BOOT_DRV
105 static void fixup_boot_drv(caddr_t, int, int, int);
106 #endif
107
108 #include "ufsread.c"
109
110 #ifdef DEBUG
111 #define DPRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
112 #else
113 #define DPRINTF(fmt, ...)
114 #endif
115
116 static inline int
117 xfsread(ino_t inode, void *buf, size_t nbyte)
118 {
119         if ((size_t)fsread(inode, buf, nbyte) != nbyte)
120                 return -1;
121         return 0;
122 }
123
124 static inline void
125 getstr(int c)
126 {
127         char *s;
128
129         s = cmd;
130         if (c == 0)
131                 c = getc(10000);
132         for (;;) {
133                 switch (c) {
134                 case 0:
135                         break;
136                 case '\177':
137                 case '\b':
138                         if (s > cmd) {
139                                 s--;
140                                 printf("\b \b");
141                         }
142                         break;
143                 case '\n':
144                 case '\r':
145                         *s = 0;
146                         return;
147                 default:
148                         if (s - cmd < sizeof(cmd) - 1)
149                                 *s++ = c;
150                         xputchar(c);
151                 }
152                 c = getc(10000);
153         }
154 }
155
156 int
157 main(void)
158 {
159         const char *bt;
160         int autoboot, c = 0;
161         ino_t ino;
162
163         dmadat = (void *)(0x1c0000);
164         p_memset((char *)dmadat, 0, 32 * 1024);
165         bt = board_init();
166
167         printf("FreeBSD ARM (%s) boot2 v%d.%d\n", bt, 0, 4);
168
169         autoboot = 1;
170
171         /* Process configuration file */
172         if ((ino = lookup(PATH_CONFIG)) ||
173             (ino = lookup(PATH_DOTCONFIG)))
174                 fsread(ino, cmd, sizeof(cmd));
175
176         if (*cmd) {
177                 if (parse())
178                         autoboot = 0;
179                 printf("%s: %s\n", PATH_CONFIG, cmd);
180                 /* Do not process this command twice */
181                 *cmd = 0;
182         }
183
184         if (*kname == '\0')
185                 strcpy(kname, PATH_KERNEL);
186
187         /* Present the user with the boot2 prompt. */
188         for (;;) {
189                 printf("\nDefault: %s\nboot: ", kname);
190                 if (!autoboot ||
191                     (OPT_CHECK(RBX_NOINTR) == 0 && (c = getc(2)) != 0))
192                         getstr(c);
193                 xputchar('\n');
194                 autoboot = 0;
195                 c = 0;
196                 DPRINTF("cmd is '%s'\n", cmd);
197                 if (parse())
198                         xputchar('\a');
199                 else
200                         load();
201         }
202 }
203
204 static void
205 load(void)
206 {
207         Elf32_Ehdr eh;
208         static Elf32_Phdr ep[2];
209         caddr_t p;
210         ino_t ino;
211         uint32_t addr;
212         int i, j;
213 #ifdef FIXUP_BOOT_DRV
214         caddr_t staddr;
215         int klen;
216
217         staddr = (caddr_t)0xffffffff;
218         klen = 0;
219 #endif
220         if (!(ino = lookup(kname))) {
221                 if (!ls)
222                         printf("No %s\n", kname);
223                 return;
224         }
225         DPRINTF("Found %s\n", kname);
226         if (xfsread(ino, &eh, sizeof(eh)))
227                 return;
228         if (!IS_ELF(eh)) {
229                 printf("Invalid %s\n", "format");
230                 return;
231         }
232         fs_off = eh.e_phoff;
233         for (j = i = 0; i < eh.e_phnum && j < 2; i++) {
234                 if (xfsread(ino, ep + j, sizeof(ep[0])))
235                         return;
236                 if (ep[j].p_type == PT_LOAD)
237                         j++;
238         }
239         for (i = 0; i < 2; i++) {
240                 p = (caddr_t)(ep[i].p_paddr & 0x0fffffff);
241                 fs_off = ep[i].p_offset;
242 #ifdef FIXUP_BOOT_DRV
243                 if (staddr == (caddr_t)0xffffffff)
244                         staddr = p;
245                 klen += ep[i].p_filesz;
246 #endif
247                 if (xfsread(ino, p, ep[i].p_filesz))
248                         return;
249         }
250         addr = eh.e_entry & 0x0fffffff;
251         DPRINTF("Entry point %x for %s\n", addr, kname);
252         clr_board();
253 #ifdef FIXUP_BOOT_DRV
254         fixup_boot_drv(staddr, klen, bootslice, bootpart);
255 #endif
256         ((void(*)(int))addr)(RB_BOOTINFO /* XXX | (opts & RBX_MASK) */);
257 }
258
259 static int
260 parse()
261 {
262         char *arg = cmd;
263         char *ep, *p;
264         int c, i;
265
266         while ((c = *arg++)) {
267                 if (c == ' ' || c == '\t' || c == '\n')
268                         continue;
269                 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
270                 ep = p;
271                 if (*p)
272                         *p++ = 0;
273                 if (c == '-') {
274                         while ((c = *arg++)) {
275                                 for (i = 0; c != optstr[i]; i++)
276                                         if (i == NOPT - 1)
277                                                 return -1;
278                                 opts ^= OPT_SET(flags[i]);
279                         }
280                 } else {
281                         arg--;
282                         /* look for ad0s1a:... | ad0s1:... */
283                         if (strlen(arg) > 6 && arg[0] == 'a' &&
284                             arg[1] == 'd' && arg[3] == 's' &&
285                             (arg[5] == ':' || arg[6] == ':')) {
286                                 /* XXX Should also handle disk. */
287                                 bootslice = arg[4] - '0';
288                                 if (bootslice < 1 || bootslice > 4)
289                                         return (-1);
290                                 bootpart = 0;
291                                 if (arg[5] != ':')
292                                         bootpart = arg[5] - 'a';
293                                 if (bootpart < 0 || bootpart > 7)
294                                         return (-1);
295                                 dsk_meta = 0;
296                                 if (arg[5] == ':')
297                                         arg += 6;
298                                 else
299                                         arg += 7;
300                         /* look for ad0a:... */
301                         } else if (strlen(arg) > 4 && arg[0] == 'a' &&
302                             arg[1] == 'd' && arg[2] == '0' && arg[4] == ':') {
303                                 bootslice = 0;
304                                 bootpart = arg[3] - 'a';
305                                 if (bootpart < 0 || bootpart > 7)
306                                         return (-1);
307                                 dsk_meta = 0;
308                                 arg += 5;
309                         }
310                         if ((i = ep - arg)) {
311                                 if ((size_t)i >= sizeof(kname))
312                                         return -1;
313                                 memcpy(kname, arg, i + 1);
314                         }
315                 }
316                 arg = p;
317         }
318         return 0;
319 }
320
321 /*
322  * dskread() will try to handle the disk layouts that are typically
323  * encountered.
324  * - raw or "Dangerously Dedicated" mode. No real slice table, just the
325  *   default one that is included with bsdlabel -B. Typically this is
326  *   used with ROOTDEVNAME=\"ufs:ad0a\".
327  * - slice only. Only a slice table is installed with no bsd label or
328  *   bsd partition table. This is typically used with
329  *   ROOTDEVNAME=\"ufs:ad0s1\".
330  * - slice + bsd label + partition table. This is typically done with
331  *   with fdisk + bsdlabel and is used with ROOTDEVNAME=\"ufs:ad0s1a\".
332  */
333 static int
334 dskread(void *buf, unsigned lba, unsigned nblk)
335 {
336         struct dos_partition *dp;
337         struct disklabel *d;
338         char *sec;
339         int i;
340
341         if (!dsk_meta) {
342                 sec = dmadat->secbuf;
343                 dsk_start = 0;
344                 if (drvread(sec, DOSBBSECTOR, 1))
345                         return -1;
346                 dp = (void *)(sec + DOSPARTOFF);
347                 if (bootslice != 0) {
348                         i = bootslice - 1;
349                         if (dp[i].dp_typ != DOSPTYP_386BSD)
350                                 return -1;
351                 } else {
352                         for (i = 0; i < NDOSPART; i++) {
353                                 if ((dp[i].dp_typ == DOSPTYP_386BSD) &&
354                                     (dp[i].dp_flag == 0x80))
355                                         break;
356                         }
357                 }
358                 if (i != NDOSPART) {
359                         bootslice = i + 1;
360                         DPRINTF("Found an active fbsd slice. (%d)\n", i + 1);
361                         /*
362                          * Although dp_start is aligned within the disk
363                          * partition structure, DOSPARTOFF is 446, which
364                          * is only word (2) aligned, not longword (4)
365                          * aligned. Cope by using memcpy to fetch the
366                          * start of this partition.
367                          */
368                         memcpy(&dsk_start, &dp[i].dp_start, 4);
369                         dsk_start = swap32(dsk_start);
370                         DPRINTF("dsk_start %x\n", dsk_start);
371                         if ((bootslice == 4) && (dsk_start == 0)) {
372                                 disk_layout = DL_RAW;
373                                 bootslice = 0;
374                         }
375                 }
376                 if (drvread(sec, dsk_start + LABELSECTOR, 1))
377                         return -1;
378                 d = (void *)(sec + LABELOFFSET);
379                 if ((d->d_magic == DISKMAGIC && d->d_magic2 == DISKMAGIC) ||
380                     (swap32(d->d_magic) == DISKMAGIC &&
381                     swap32(d->d_magic2) == DISKMAGIC)) {
382                         DPRINTF("p_size = %x\n",
383                             !d->d_partitions[bootpart].p_size);
384                         if (!d->d_partitions[bootpart].p_size) {
385                                 printf("Invalid partition\n");
386                                 return -1;
387                         }
388                         DPRINTF("p_offset %x, RAW %x\n",
389                             swap32(d->d_partitions[bootpart].p_offset),
390                             swap32(d->d_partitions[RAW_PART].p_offset));
391                         dsk_start += swap32(d->d_partitions[bootpart].p_offset);
392                         dsk_start -= swap32(d->d_partitions[RAW_PART].p_offset);
393                         if ((disk_layout == DL_UNKNOWN) && (bootslice == 0))
394                                 disk_layout = DL_RAW;
395                         else if (disk_layout == DL_UNKNOWN)
396                                 disk_layout = DL_SLICEPART;
397                 } else {
398                         disk_layout = DL_SLICE;
399                         DPRINTF("Invalid %s\n", "label");
400                 }
401                 DPRINTF("bootslice %d, bootpart %d, dsk_start %u\n", bootslice,
402                     bootpart, dsk_start);
403                 dsk_meta++;
404         }
405         return drvread(buf, dsk_start + lba, nblk);
406 }
407
408 static int
409 drvread(void *buf, unsigned lba, unsigned nblk)
410 {
411         static unsigned c = 0x2d5c7c2f;
412
413         printf("%c\b", c = c << 8 | c >> 24);
414         return (avila_read((char *)buf, lba, nblk));
415 }
416
417 #ifdef FIXUP_BOOT_DRV
418 /*
419  * fixup_boot_drv() will try to find the ROOTDEVNAME spec in the kernel
420  * and change it to what was specified on the comandline or /boot.conf
421  * file or to what was encountered on the disk. It will try to handle 3
422  * different disk layouts, raw (dangerously dedicated), slice only and
423  * slice + partition. It will look for the following strings in the
424  * kernel, but if it is one of the first three, the string in the kernel
425  * must use the correct form to match the actual disk layout:
426  * - ufs:ad0a
427  * - ufs:ad0s1
428  * - ufs:ad0s1a
429  * - ufs:ROOTDEVNAME
430  * In the case of the first three strings, only the "a" at the end and
431  * the "1" after the "s" will be modified, if they exist. The string
432  * length will not be changed. In the case of the last string, the
433  * whole string will be built up and nul, '\0' terminated.
434  */
435 static void
436 fixup_boot_drv(caddr_t addr, int klen, int bs, int bp)
437 {
438         const u_int8_t op[] = "ufs:ROOTDEVNAME";
439         const u_int8_t op2[] = "ufs:ad0";
440         u_int8_t *p, *ps;
441
442         DPRINTF("fixup_boot_drv: 0x%x, %d, slice %d, partition %d\n",
443             (int)addr, klen, bs, bp);
444         if (bs > 4)
445                 return;
446         if (bp > 7)
447                 return;
448         ps = memmem(addr, klen, op, sizeof(op));
449         if (ps != NULL) {
450                 p = ps + 4;     /* past ufs: */
451                 DPRINTF("Found it at 0x%x\n", (int)ps);
452                 p[0] = 'a'; p[1] = 'd'; p[2] = '0';     /* ad0 */
453                 p += 3;
454                 if (bs > 0) {
455                         /* append slice */
456                         *p++ = 's';
457                         *p++ = bs + '0';
458                 }
459                 if (disk_layout != DL_SLICE) {
460                         /* append partition */
461                         *p++ = bp + 'a';
462                 }
463                 *p = '\0';
464         } else {
465                 ps = memmem(addr, klen, op2, sizeof(op2) - 1);
466                 if (ps != NULL) {
467                         p = ps + sizeof(op2) - 1;
468                         DPRINTF("Found it at 0x%x\n", (int)ps);
469                         if (*p == 's') {
470                                 /* fix slice */
471                                 p++;
472                                 *p++ = bs + '0';
473                         }
474                         if (*p == 'a')
475                                 *p = bp + 'a';
476                 }
477         }
478         if (ps == NULL) {
479                 printf("Could not locate \"%s\" to fix kernel boot device, "
480                      "check ROOTDEVNAME is set\n", op);
481                 return;
482         }
483         DPRINTF("Changed boot device to %s\n", ps);
484 }
485 #endif