]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/boot/powerpc/ofw/metadata.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / boot / powerpc / ofw / metadata.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      from: FreeBSD: src/sys/boot/sparc64/loader/metadata.c,v 1.6
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <stand.h>
33 #include <sys/param.h>
34 #include <sys/reboot.h>
35 #include <sys/linker.h>
36
37 #include <machine/metadata.h>
38
39 #include "bootstrap.h"
40 #include "libofw.h"
41
42 /*
43  * Return a 'boothowto' value corresponding to the kernel arguments in
44  * (kargs) and any relevant environment variables.
45  */
46 static struct 
47 {
48     const char  *ev;
49     int         mask;
50 } howto_names[] = {
51     {"boot_askname",    RB_ASKNAME},
52     {"boot_cdrom",      RB_CDROM},
53     {"boot_ddb",        RB_KDB},
54     {"boot_dfltroot",   RB_DFLTROOT},
55     {"boot_gdb",        RB_GDB},
56     {"boot_multicons",  RB_MULTIPLE},
57     {"boot_mute",       RB_MUTE},
58     {"boot_pause",      RB_PAUSE},
59     {"boot_serial",     RB_SERIAL},
60     {"boot_single",     RB_SINGLE},
61     {"boot_verbose",    RB_VERBOSE},
62     {NULL,      0}
63 };
64
65 int
66 md_getboothowto(char *kargs)
67 {
68     char        *cp;
69     int         howto;
70     int         active;
71     int         i;
72     
73     /* Parse kargs */
74     howto = 0;
75     if (kargs != NULL) {
76         cp = kargs;
77         active = 0;
78         while (*cp != 0) {
79             if (!active && (*cp == '-')) {
80                 active = 1;
81             } else if (active)
82                 switch (*cp) {
83                 case 'a':
84                     howto |= RB_ASKNAME;
85                     break;
86                 case 'C':
87                     howto |= RB_CDROM;
88                     break;
89                 case 'd':
90                     howto |= RB_KDB;
91                     break;
92                 case 'D':
93                     howto |= RB_MULTIPLE;
94                     break;
95                 case 'm':
96                     howto |= RB_MUTE;
97                     break;
98                 case 'g':
99                     howto |= RB_GDB;
100                     break;
101                 case 'h':
102                     howto |= RB_SERIAL;
103                     break;
104                 case 'p':
105                     howto |= RB_PAUSE;
106                     break;
107                 case 'r':
108                     howto |= RB_DFLTROOT;
109                     break;
110                 case 's':
111                     howto |= RB_SINGLE;
112                     break;
113                 case 'v':
114                     howto |= RB_VERBOSE;
115                     break;
116                 default:
117                     active = 0;
118                     break;
119                 }
120             cp++;
121         }
122     }
123     /* get equivalents from the environment */
124     for (i = 0; howto_names[i].ev != NULL; i++)
125         if (getenv(howto_names[i].ev) != NULL)
126             howto |= howto_names[i].mask;
127     if (!strcmp(getenv("console"), "comconsole"))
128         howto |= RB_SERIAL;
129     if (!strcmp(getenv("console"), "nullconsole"))
130         howto |= RB_MUTE;
131     return(howto);
132 }
133
134 /*
135  * Copy the environment into the load area starting at (addr).
136  * Each variable is formatted as <name>=<value>, with a single nul
137  * separating each variable, and a double nul terminating the environment.
138  */
139 vm_offset_t
140 md_copyenv(vm_offset_t addr)
141 {
142     struct env_var      *ep;
143     
144     /* traverse the environment */
145     for (ep = environ; ep != NULL; ep = ep->ev_next) {
146         archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
147         addr += strlen(ep->ev_name);
148         archsw.arch_copyin("=", addr, 1);
149         addr++;
150         if (ep->ev_value != NULL) {
151             archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
152             addr += strlen(ep->ev_value);
153         }
154         archsw.arch_copyin("", addr, 1);
155         addr++;
156     }
157     archsw.arch_copyin("", addr, 1);
158     addr++;
159     return(addr);
160 }
161
162 /*
163  * Copy module-related data into the load area, where it can be
164  * used as a directory for loaded modules.
165  *
166  * Module data is presented in a self-describing format.  Each datum
167  * is preceded by a 32-bit identifier and a 32-bit size field.
168  *
169  * Currently, the following data are saved:
170  *
171  * MOD_NAME     (variable)              module name (string)
172  * MOD_TYPE     (variable)              module type (string)
173  * MOD_ARGS     (variable)              module parameters (string)
174  * MOD_ADDR     sizeof(vm_offset_t)     module load address
175  * MOD_SIZE     sizeof(size_t)          module size
176  * MOD_METADATA (variable)              type-specific metadata
177  */
178
179 static int align;
180
181 #define COPY32(v, a, c) {                       \
182     u_int32_t   x = (v);                        \
183     if (c)                                      \
184         archsw.arch_copyin(&x, a, sizeof(x));   \
185     a += sizeof(x);                             \
186 }
187
188 #define MOD_STR(t, a, s, c) {                   \
189     COPY32(t, a, c);                            \
190     COPY32(strlen(s) + 1, a, c)                 \
191     if (c)                                      \
192         archsw.arch_copyin(s, a, strlen(s) + 1);\
193     a += roundup(strlen(s) + 1, align);         \
194 }
195
196 #define MOD_NAME(a, s, c)       MOD_STR(MODINFO_NAME, a, s, c)
197 #define MOD_TYPE(a, s, c)       MOD_STR(MODINFO_TYPE, a, s, c)
198 #define MOD_ARGS(a, s, c)       MOD_STR(MODINFO_ARGS, a, s, c)
199
200 #define MOD_VAR(t, a, s, c) {                   \
201     COPY32(t, a, c);                            \
202     COPY32(sizeof(s), a, c);                    \
203     if (c)                                      \
204         archsw.arch_copyin(&s, a, sizeof(s));   \
205     a += roundup(sizeof(s), align);             \
206 }
207
208 #define MOD_ADDR(a, s, c)       MOD_VAR(MODINFO_ADDR, a, s, c)
209 #define MOD_SIZE(a, s, c)       MOD_VAR(MODINFO_SIZE, a, s, c)
210
211 #define MOD_METADATA(a, mm, c) {                \
212     COPY32(MODINFO_METADATA | mm->md_type, a, c);\
213     COPY32(mm->md_size, a, c);                  \
214     if (c)                                      \
215         archsw.arch_copyin(mm->md_data, a, mm->md_size);\
216     a += roundup(mm->md_size, align);           \
217 }
218
219 #define MOD_END(a, c) {                         \
220     COPY32(MODINFO_END, a, c);                  \
221     COPY32(0, a, c);                            \
222 }
223
224 vm_offset_t
225 md_copymodules(vm_offset_t addr, int kern64)
226 {
227     struct preloaded_file       *fp;
228     struct file_metadata        *md;
229     uint64_t                    scratch64;
230     int                         c;
231
232     c = addr != 0;
233     /* start with the first module on the list, should be the kernel */
234     for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
235
236         MOD_NAME(addr, fp->f_name, c);  /* this field must come first */
237         MOD_TYPE(addr, fp->f_type, c);
238         if (fp->f_args)
239             MOD_ARGS(addr, fp->f_args, c);
240         if (kern64) {
241                 scratch64 = fp->f_addr;
242                 MOD_ADDR(addr, scratch64, c);
243                 scratch64 = fp->f_size;
244                 MOD_SIZE(addr, scratch64, c);
245         } else {
246                 MOD_ADDR(addr, fp->f_addr, c);
247                 MOD_SIZE(addr, fp->f_size, c);
248         }
249         for (md = fp->f_metadata; md != NULL; md = md->md_next) {
250             if (!(md->md_type & MODINFOMD_NOCOPY)) {
251                 MOD_METADATA(addr, md, c);
252             }
253         }
254     }
255     MOD_END(addr, c);
256     return(addr);
257 }
258
259 /*
260  * Load the information expected by a powerpc kernel.
261  *
262  * - The 'boothowto' argument is constructed
263  * - The 'bootdev' argument is constructed
264  * - The kernel environment is copied into kernel space.
265  * - Module metadata are formatted and placed in kernel space.
266  */
267 int
268 md_load_dual(char *args, vm_offset_t *modulep, int kern64)
269 {
270     struct preloaded_file       *kfp;
271     struct preloaded_file       *xp;
272     struct file_metadata        *md;
273     vm_offset_t                 kernend;
274     vm_offset_t                 addr;
275     vm_offset_t                 envp;
276     vm_offset_t                 size;
277     uint64_t                    scratch64;
278     char                        *rootdevname;
279     int                         howto;
280
281     align = kern64 ? 8 : 4;
282     howto = md_getboothowto(args);
283
284     /* 
285      * Allow the environment variable 'rootdev' to override the supplied device 
286      * This should perhaps go to MI code and/or have $rootdev tested/set by
287      * MI code before launching the kernel.
288      */
289     rootdevname = getenv("rootdev");
290     if (rootdevname == NULL)
291             rootdevname = getenv("currdev");
292     /* Try reading the /etc/fstab file to select the root device */
293     getrootmount(rootdevname);
294
295     /* find the last module in the chain */
296     addr = 0;
297     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
298         if (addr < (xp->f_addr + xp->f_size))
299             addr = xp->f_addr + xp->f_size;
300     }
301     /* pad to a page boundary */
302     addr = roundup(addr, PAGE_SIZE);
303
304     /* copy our environment */
305     envp = addr;
306     addr = md_copyenv(addr);
307
308     /* pad to a page boundary */
309     addr = roundup(addr, PAGE_SIZE);
310
311     kernend = 0;
312     kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
313     if (kfp == NULL)
314         kfp = file_findfile(NULL, "elf kernel");
315     if (kfp == NULL)
316         panic("can't find kernel file");
317     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
318     if (kern64) {
319         scratch64 = envp;
320         file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
321         scratch64 = kernend;
322         file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof scratch64, &scratch64);
323     } else {
324         file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
325         file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
326     }
327
328     *modulep = addr;
329     size = md_copymodules(0, kern64);
330     kernend = roundup(addr + size, PAGE_SIZE);
331
332     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
333     if (kern64) {
334         scratch64 = kernend;
335         bcopy(&scratch64, md->md_data, sizeof scratch64);
336     } else {
337         bcopy(&kernend, md->md_data, sizeof kernend);
338     }
339         
340     (void)md_copymodules(addr, kern64);
341
342     return(0);
343 }
344
345 int
346 md_load(char *args, vm_offset_t *modulep)
347 {
348     return (md_load_dual(args, modulep, 0));
349 }
350
351 int
352 md_load64(char *args, vm_offset_t *modulep)
353 {
354     return (md_load_dual(args, modulep, 1));
355 }
356