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