]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/boot/powerpc/ps3/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 / ps3 / 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
41 /*
42  * Return a 'boothowto' value corresponding to the kernel arguments in
43  * (kargs) and any relevant environment variables.
44  */
45 static struct 
46 {
47     const char  *ev;
48     int         mask;
49 } howto_names[] = {
50     {"boot_askname",    RB_ASKNAME},
51     {"boot_cdrom",      RB_CDROM},
52     {"boot_ddb",        RB_KDB},
53     {"boot_dfltroot",   RB_DFLTROOT},
54     {"boot_gdb",        RB_GDB},
55     {"boot_multicons",  RB_MULTIPLE},
56     {"boot_mute",       RB_MUTE},
57     {"boot_pause",      RB_PAUSE},
58     {"boot_serial",     RB_SERIAL},
59     {"boot_single",     RB_SINGLE},
60     {"boot_verbose",    RB_VERBOSE},
61     {NULL,      0}
62 };
63
64 int
65 md_getboothowto(char *kargs)
66 {
67     char        *cp;
68     int         howto;
69     int         active;
70     int         i;
71     
72     /* Parse kargs */
73     howto = 0;
74     if (kargs != NULL) {
75         cp = kargs;
76         active = 0;
77         while (*cp != 0) {
78             if (!active && (*cp == '-')) {
79                 active = 1;
80             } else if (active)
81                 switch (*cp) {
82                 case 'a':
83                     howto |= RB_ASKNAME;
84                     break;
85                 case 'C':
86                     howto |= RB_CDROM;
87                     break;
88                 case 'd':
89                     howto |= RB_KDB;
90                     break;
91                 case 'D':
92                     howto |= RB_MULTIPLE;
93                     break;
94                 case 'm':
95                     howto |= RB_MUTE;
96                     break;
97                 case 'g':
98                     howto |= RB_GDB;
99                     break;
100                 case 'h':
101                     howto |= RB_SERIAL;
102                     break;
103                 case 'p':
104                     howto |= RB_PAUSE;
105                     break;
106                 case 'r':
107                     howto |= RB_DFLTROOT;
108                     break;
109                 case 's':
110                     howto |= RB_SINGLE;
111                     break;
112                 case 'v':
113                     howto |= RB_VERBOSE;
114                     break;
115                 default:
116                     active = 0;
117                     break;
118                 }
119             cp++;
120         }
121     }
122     /* get equivalents from the environment */
123     for (i = 0; howto_names[i].ev != NULL; i++)
124         if (getenv(howto_names[i].ev) != NULL)
125             howto |= howto_names[i].mask;
126     if (!strcmp(getenv("console"), "comconsole"))
127         howto |= RB_SERIAL;
128     if (!strcmp(getenv("console"), "nullconsole"))
129         howto |= RB_MUTE;
130     return(howto);
131 }
132
133 /*
134  * Copy the environment into the load area starting at (addr).
135  * Each variable is formatted as <name>=<value>, with a single nul
136  * separating each variable, and a double nul terminating the environment.
137  */
138 vm_offset_t
139 md_copyenv(vm_offset_t addr)
140 {
141     struct env_var      *ep;
142     
143     /* traverse the environment */
144     for (ep = environ; ep != NULL; ep = ep->ev_next) {
145         archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
146         addr += strlen(ep->ev_name);
147         archsw.arch_copyin("=", addr, 1);
148         addr++;
149         if (ep->ev_value != NULL) {
150             archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
151             addr += strlen(ep->ev_value);
152         }
153         archsw.arch_copyin("", addr, 1);
154         addr++;
155     }
156     archsw.arch_copyin("", addr, 1);
157     addr++;
158     return(addr);
159 }
160
161 /*
162  * Copy module-related data into the load area, where it can be
163  * used as a directory for loaded modules.
164  *
165  * Module data is presented in a self-describing format.  Each datum
166  * is preceded by a 32-bit identifier and a 32-bit size field.
167  *
168  * Currently, the following data are saved:
169  *
170  * MOD_NAME     (variable)              module name (string)
171  * MOD_TYPE     (variable)              module type (string)
172  * MOD_ARGS     (variable)              module parameters (string)
173  * MOD_ADDR     sizeof(vm_offset_t)     module load address
174  * MOD_SIZE     sizeof(size_t)          module size
175  * MOD_METADATA (variable)              type-specific metadata
176  */
177
178 static int align;
179
180 #define COPY32(v, a, c) {                       \
181     u_int32_t   x = (v);                        \
182     if (c)                                      \
183         archsw.arch_copyin(&x, a, sizeof(x));   \
184     a += sizeof(x);                             \
185 }
186
187 #define MOD_STR(t, a, s, c) {                   \
188     COPY32(t, a, c);                            \
189     COPY32(strlen(s) + 1, a, c)                 \
190     if (c)                                      \
191         archsw.arch_copyin(s, a, strlen(s) + 1);\
192     a += roundup(strlen(s) + 1, align);         \
193 }
194
195 #define MOD_NAME(a, s, c)       MOD_STR(MODINFO_NAME, a, s, c)
196 #define MOD_TYPE(a, s, c)       MOD_STR(MODINFO_TYPE, a, s, c)
197 #define MOD_ARGS(a, s, c)       MOD_STR(MODINFO_ARGS, a, s, c)
198
199 #define MOD_VAR(t, a, s, c) {                   \
200     COPY32(t, a, c);                            \
201     COPY32(sizeof(s), a, c);                    \
202     if (c)                                      \
203         archsw.arch_copyin(&s, a, sizeof(s));   \
204     a += roundup(sizeof(s), align);             \
205 }
206
207 #define MOD_ADDR(a, s, c)       MOD_VAR(MODINFO_ADDR, a, s, c)
208 #define MOD_SIZE(a, s, c)       MOD_VAR(MODINFO_SIZE, a, s, c)
209
210 #define MOD_METADATA(a, mm, c) {                \
211     COPY32(MODINFO_METADATA | mm->md_type, a, c);\
212     COPY32(mm->md_size, a, c);                  \
213     if (c)                                      \
214         archsw.arch_copyin(mm->md_data, a, mm->md_size);\
215     a += roundup(mm->md_size, align);           \
216 }
217
218 #define MOD_END(a, c) {                         \
219     COPY32(MODINFO_END, a, c);                  \
220     COPY32(0, a, c);                            \
221 }
222
223 vm_offset_t
224 md_copymodules(vm_offset_t addr, int kern64)
225 {
226     struct preloaded_file       *fp;
227     struct file_metadata        *md;
228     uint64_t                    scratch64;
229     int                         c;
230
231     c = addr != 0;
232     /* start with the first module on the list, should be the kernel */
233     for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
234
235         MOD_NAME(addr, fp->f_name, c);  /* this field must come first */
236         MOD_TYPE(addr, fp->f_type, c);
237         if (fp->f_args)
238             MOD_ARGS(addr, fp->f_args, c);
239         if (kern64) {
240                 scratch64 = fp->f_addr;
241                 MOD_ADDR(addr, scratch64, c);
242                 scratch64 = fp->f_size;
243                 MOD_SIZE(addr, scratch64, c);
244         } else {
245                 MOD_ADDR(addr, fp->f_addr, c);
246                 MOD_SIZE(addr, fp->f_size, c);
247         }
248         for (md = fp->f_metadata; md != NULL; md = md->md_next) {
249             if (!(md->md_type & MODINFOMD_NOCOPY)) {
250                 MOD_METADATA(addr, md, c);
251             }
252         }
253     }
254     MOD_END(addr, c);
255     return(addr);
256 }
257
258 /*
259  * Load the information expected by a powerpc kernel.
260  *
261  * - The 'boothowto' argument is constructed
262  * - The 'bootdev' argument is constructed
263  * - The kernel environment is copied into kernel space.
264  * - Module metadata are formatted and placed in kernel space.
265  */
266 int
267 md_load_dual(char *args, vm_offset_t *modulep, int kern64)
268 {
269     struct preloaded_file       *kfp;
270     struct preloaded_file       *xp;
271     struct file_metadata        *md;
272     vm_offset_t                 kernend;
273     vm_offset_t                 addr;
274     vm_offset_t                 envp;
275     vm_offset_t                 size;
276     uint64_t                    scratch64;
277     char                        *rootdevname;
278     int                         howto;
279
280     align = kern64 ? 8 : 4;
281     howto = md_getboothowto(args);
282
283     /* 
284      * Allow the environment variable 'rootdev' to override the supplied device 
285      * This should perhaps go to MI code and/or have $rootdev tested/set by
286      * MI code before launching the kernel.
287      */
288     rootdevname = getenv("rootdev");
289     if (rootdevname == NULL)
290             rootdevname = getenv("currdev");
291     /* Try reading the /etc/fstab file to select the root device */
292     getrootmount(rootdevname);
293
294     /* find the last module in the chain */
295     addr = 0;
296     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
297         if (addr < (xp->f_addr + xp->f_size))
298             addr = xp->f_addr + xp->f_size;
299     }
300     /* pad to a page boundary */
301     addr = roundup(addr, PAGE_SIZE);
302
303     /* copy our environment */
304     envp = addr;
305     addr = md_copyenv(addr);
306
307     /* pad to a page boundary */
308     addr = roundup(addr, PAGE_SIZE);
309
310     kernend = 0;
311     kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
312     if (kfp == NULL)
313         kfp = file_findfile(NULL, "elf kernel");
314     if (kfp == NULL)
315         panic("can't find kernel file");
316     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
317     if (kern64) {
318         scratch64 = envp;
319         file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
320         scratch64 = kernend;
321         file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof scratch64, &scratch64);
322     } else {
323         file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
324         file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
325     }
326
327     *modulep = addr;
328     size = md_copymodules(0, kern64);
329     kernend = roundup(addr + size, PAGE_SIZE);
330
331     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
332     if (kern64) {
333         scratch64 = kernend;
334         bcopy(&scratch64, md->md_data, sizeof scratch64);
335     } else {
336         bcopy(&kernend, md->md_data, sizeof kernend);
337     }
338         
339     (void)md_copymodules(addr, kern64);
340
341     return(0);
342 }
343
344 int
345 md_load(char *args, vm_offset_t *modulep)
346 {
347     return (md_load_dual(args, modulep, 0));
348 }
349
350 int
351 md_load64(char *args, vm_offset_t *modulep)
352 {
353     return (md_load_dual(args, modulep, 1));
354 }
355