]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/boot/arm/at91/boot2/boot2.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / boot / arm / at91 / boot2 / boot2.c
1 /*-
2  * Copyright (c) 1998 Robert Nordier
3  * All rights reserved.
4  *
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
8  * such forms.
9  *
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
13  * purpose.
14  */
15
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
18
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>
24
25 #include <machine/elf.h>
26
27 #include <stdarg.h>
28
29 #include "lib.h"
30 #include "board.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_CONFIG     "/boot.config"
66 //#define PATH_KERNEL   "/boot/kernel/kernel"
67 #define PATH_KERNEL     "/boot/kernel/kernel.gz.tramp"
68
69 #define NOPT            5
70
71 #define OPT_SET(opt)    (1 << (opt))
72 #define OPT_CHECK(opt)  ((opts) & OPT_SET(opt))
73
74 extern uint32_t _end;
75
76 static const char optstr[NOPT] = "agrsv";
77 static const unsigned char flags[NOPT] = {
78     RBX_ASKNAME,
79     RBX_GDB,
80     RBX_DFLTROOT,
81     RBX_SINGLE,
82     RBX_VERBOSE
83 };
84
85 unsigned dsk_start;
86 static char cmd[512];
87 static char kname[1024];
88 static uint32_t opts;
89 static int dsk_meta;
90
91 static void load(void);
92 static int parse(void);
93 static int xfsread(ino_t, void *, size_t);
94 static int dskread(void *, unsigned, unsigned);
95
96 #define UFS_SMALL_CGBASE
97 #include "ufsread.c"
98
99 static inline int
100 xfsread(ino_t inode, void *buf, size_t nbyte)
101 {
102     if ((size_t)fsread(inode, buf, nbyte) != nbyte)
103         return -1;
104     return 0;
105 }
106
107 static inline void
108 getstr(int c)
109 {
110     char *s;
111
112     s = cmd;
113     if (c == 0)
114         c = getc(10000);
115     for (;;) {
116         switch (c) {
117         case 0:
118             break;
119         case '\177':
120         case '\b':
121             if (s > cmd) {
122                 s--;
123                 printf("\b \b");
124             }
125             break;
126         case '\n':
127         case '\r':
128             *s = 0;
129             return;
130         default:
131             if (s - cmd < sizeof(cmd) - 1)
132                 *s++ = c;
133             xputchar(c);
134         }
135         c = getc(10000);
136     }
137 }
138
139 int
140 main(void)
141 {
142     int autoboot, c = 0;
143     ino_t ino;
144
145     board_init();
146
147     dmadat = (void *)(0x20000000 + (16 << 20));
148     /* Process configuration file */
149
150     autoboot = 1;
151
152     if ((ino = lookup(PATH_CONFIG)))
153         fsread(ino, cmd, sizeof(cmd));
154
155     if (*cmd) {
156         if (parse())
157             autoboot = 0;
158         printf("%s: %s", PATH_CONFIG, cmd);
159         /* Do not process this command twice */
160         *cmd = 0;
161     }
162
163     /* Present the user with the boot2 prompt. */
164
165     if (*kname == '\0')
166         strcpy(kname, PATH_KERNEL);
167     for (;;) {
168         printf("\nDefault: %s\nboot: ", kname);
169         if (!autoboot || (c = getc(2)) != -1)
170             getstr(c);
171         xputchar('\n');
172         autoboot = 0;
173         c = 0;
174         if (parse())
175             xputchar('\a');
176 #ifdef XMODEM_DL
177         else if (*cmd == '*')
178             Update();
179 #endif
180         else
181             load();
182     }
183 }
184
185 static void
186 load(void)
187 {
188     Elf32_Ehdr eh;
189     static Elf32_Phdr ep[2];
190     caddr_t p;
191     ino_t ino;
192     uint32_t addr;
193     int i, j;
194
195     if (!(ino = lookup(kname))) {
196         if (!ls)
197             printf("No %s\n", kname);
198         return;
199     }
200     if (xfsread(ino, &eh, sizeof(eh)))
201         return;
202     if (!IS_ELF(eh)) {
203         printf("Invalid %s\n", "format");
204         return;
205     }
206     fs_off = eh.e_phoff;
207     for (j = i = 0; i < eh.e_phnum && j < 2; i++) {
208         if (xfsread(ino, ep + j, sizeof(ep[0])))
209             return;
210         if (ep[j].p_type == PT_LOAD)
211             j++;
212     }
213     for (i = 0; i < 2; i++) {
214         p = (caddr_t)ep[i].p_paddr;
215         fs_off = ep[i].p_offset;
216         if (xfsread(ino, p, ep[i].p_filesz))
217             return;
218     }
219     addr = eh.e_entry;
220     ((void(*)(int))addr)(opts & RBX_MASK);
221 }
222
223 static int
224 parse()
225 {
226     char *arg = cmd;
227     char *ep, *p;
228     int c, i;
229
230     while ((c = *arg++)) {
231         if (c == ' ' || c == '\t' || c == '\n')
232             continue;
233         for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
234         ep = p;
235         if (*p)
236             *p++ = 0;
237         if (c == '-') {
238             while ((c = *arg++)) {
239                 for (i = 0; c != optstr[i]; i++)
240                     if (i == NOPT - 1)
241                         return -1;
242                 opts ^= OPT_SET(flags[i]);
243             }
244         } else {
245             arg--;
246             if ((i = ep - arg)) {
247                 if ((size_t)i >= sizeof(kname))
248                     return -1;
249                 memcpy(kname, arg, i + 1);
250             }
251         }
252         arg = p;
253     }
254     return 0;
255 }
256
257 static int
258 dskread(void *buf, unsigned lba, unsigned nblk)
259 {
260     struct dos_partition *dp;
261     struct disklabel *d;
262     char *sec;
263     int i;
264
265     if (!dsk_meta) {
266         sec = dmadat->secbuf;
267         dsk_start = 0;
268         if (drvread(sec, DOSBBSECTOR, 1))
269             return -1;
270         dp = (void *)(sec + DOSPARTOFF);
271         for (i = 0; i < NDOSPART; i++) {
272             if (dp[i].dp_typ == DOSPTYP_386BSD)
273                 break;
274         }
275         if (i == NDOSPART)
276             return -1;
277         // Although dp_start is aligned within the disk partition structure,
278         // DOSPARTOFF is 446, which is only word (2) aligned, not longword (4)
279         // aligned.  Cope by using memcpy to fetch the start of this partition.
280         memcpy(&dsk_start, &dp[1].dp_start, 4);
281         if (drvread(sec, dsk_start + LABELSECTOR, 1))
282                 return -1;
283         d = (void *)(sec + LABELOFFSET);
284         if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
285                 printf("Invalid %s\n", "label");
286                 return -1;
287         }
288         if (!d->d_partitions[0].p_size) {
289                 printf("Invalid %s\n", "partition");
290                 return -1;
291         }
292         dsk_start += d->d_partitions[0].p_offset;
293         dsk_start -= d->d_partitions[RAW_PART].p_offset;
294         dsk_meta++;
295     }
296     return drvread(buf, dsk_start + lba, nblk);
297 }