]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/common/metadata.c
MFC r336244, r336246-r336247: Standardize boot arg parsing
[FreeBSD/FreeBSD.git] / stand / common / 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/linker.h>
35 #include <sys/boot.h>
36 #include <sys/reboot.h>
37 #if defined(LOADER_FDT_SUPPORT)
38 #include <fdt_platform.h>
39 #endif
40
41 #ifdef __arm__
42 #include <machine/elf.h>
43 #endif
44 #include <machine/metadata.h>
45
46 #include "bootstrap.h"
47
48 #if defined(__sparc64__)
49 #include <openfirm.h>
50
51 extern struct tlb_entry *dtlb_store;
52 extern struct tlb_entry *itlb_store;
53
54 extern int dtlb_slot;
55 extern int itlb_slot;
56
57 static int
58 md_bootserial(void)
59 {
60     char        buf[64];
61     ihandle_t        inst;
62     phandle_t        input;
63     phandle_t        node;
64     phandle_t        output;
65
66     if ((node = OF_finddevice("/options")) == -1)
67         return(-1);
68     if (OF_getprop(node, "input-device", buf, sizeof(buf)) == -1)
69         return(-1);
70     input = OF_finddevice(buf);
71     if (OF_getprop(node, "output-device", buf, sizeof(buf)) == -1)
72         return(-1);
73     output = OF_finddevice(buf);
74     if (input == -1 || output == -1 ||
75         OF_getproplen(input, "keyboard") >= 0) {
76         if ((node = OF_finddevice("/chosen")) == -1)
77             return(-1);
78         if (OF_getprop(node, "stdin", &inst, sizeof(inst)) == -1)
79             return(-1);
80         if ((input = OF_instance_to_package(inst)) == -1)
81             return(-1);
82         if (OF_getprop(node, "stdout", &inst, sizeof(inst)) == -1)
83             return(-1);
84         if ((output = OF_instance_to_package(inst)) == -1)
85             return(-1);
86     }
87     if (input != output)
88         return(-1);
89     if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
90         return(-1);
91     if (strcmp(buf, "serial") != 0)
92         return(-1);
93     return(0);
94 }
95 #endif
96
97 static int
98 md_getboothowto(char *kargs)
99 {
100     int         howto;
101
102     /* Parse kargs */
103     howto = boot_parse_cmdline(kargs);
104     howto |= boot_env_to_howto();
105 #if defined(__sparc64__)
106     if (md_bootserial() != -1)
107         howto |= RB_SERIAL;
108 #else
109     if (!strcmp(getenv("console"), "comconsole"))
110         howto |= RB_SERIAL;
111     if (!strcmp(getenv("console"), "nullconsole"))
112         howto |= RB_MUTE;
113 #endif
114     return(howto);
115 }
116
117 /*
118  * Copy the environment into the load area starting at (addr).
119  * Each variable is formatted as <name>=<value>, with a single nul
120  * separating each variable, and a double nul terminating the environment.
121  */
122 static vm_offset_t
123 md_copyenv(vm_offset_t addr)
124 {
125     struct env_var      *ep;
126
127     /* traverse the environment */
128     for (ep = environ; ep != NULL; ep = ep->ev_next) {
129         archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
130         addr += strlen(ep->ev_name);
131         archsw.arch_copyin("=", addr, 1);
132         addr++;
133         if (ep->ev_value != NULL) {
134             archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
135             addr += strlen(ep->ev_value);
136         }
137         archsw.arch_copyin("", addr, 1);
138         addr++;
139     }
140     archsw.arch_copyin("", addr, 1);
141     addr++;
142     return(addr);
143 }
144
145 /*
146  * Copy module-related data into the load area, where it can be
147  * used as a directory for loaded modules.
148  *
149  * Module data is presented in a self-describing format.  Each datum
150  * is preceded by a 32-bit identifier and a 32-bit size field.
151  *
152  * Currently, the following data are saved:
153  *
154  * MOD_NAME     (variable)              module name (string)
155  * MOD_TYPE     (variable)              module type (string)
156  * MOD_ARGS     (variable)              module parameters (string)
157  * MOD_ADDR     sizeof(vm_offset_t)     module load address
158  * MOD_SIZE     sizeof(size_t)          module size
159  * MOD_METADATA (variable)              type-specific metadata
160  */
161
162 static int align;
163
164 #define COPY32(v, a, c) {                       \
165     uint32_t    x = (v);                        \
166     if (c)                                      \
167         archsw.arch_copyin(&x, a, sizeof(x));   \
168     a += sizeof(x);                             \
169 }
170
171 #define MOD_STR(t, a, s, c) {                   \
172     COPY32(t, a, c);                            \
173     COPY32(strlen(s) + 1, a, c)                 \
174     if (c)                                      \
175         archsw.arch_copyin(s, a, strlen(s) + 1);\
176     a += roundup(strlen(s) + 1, align);         \
177 }
178
179 #define MOD_NAME(a, s, c)       MOD_STR(MODINFO_NAME, a, s, c)
180 #define MOD_TYPE(a, s, c)       MOD_STR(MODINFO_TYPE, a, s, c)
181 #define MOD_ARGS(a, s, c)       MOD_STR(MODINFO_ARGS, a, s, c)
182
183 #define MOD_VAR(t, a, s, c) {                   \
184     COPY32(t, a, c);                            \
185     COPY32(sizeof(s), a, c);                    \
186     if (c)                                      \
187         archsw.arch_copyin(&s, a, sizeof(s));   \
188     a += roundup(sizeof(s), align);             \
189 }
190
191 #define MOD_ADDR(a, s, c)       MOD_VAR(MODINFO_ADDR, a, s, c)
192 #define MOD_SIZE(a, s, c)       MOD_VAR(MODINFO_SIZE, a, s, c)
193
194 #define MOD_METADATA(a, mm, c) {                \
195     COPY32(MODINFO_METADATA | mm->md_type, a, c);\
196     COPY32(mm->md_size, a, c);                  \
197     if (c)                                      \
198         archsw.arch_copyin(mm->md_data, a, mm->md_size);\
199     a += roundup(mm->md_size, align);           \
200 }
201
202 #define MOD_END(a, c) {                         \
203     COPY32(MODINFO_END, a, c);                  \
204     COPY32(0, a, c);                            \
205 }
206
207 static vm_offset_t
208 md_copymodules(vm_offset_t addr, int kern64)
209 {
210     struct preloaded_file       *fp;
211     struct file_metadata        *md;
212     uint64_t                    scratch64;
213     uint32_t                    scratch32;
214     int                         c;
215
216     c = addr != 0;
217     /* start with the first module on the list, should be the kernel */
218     for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
219
220         MOD_NAME(addr, fp->f_name, c);  /* this field must come first */
221         MOD_TYPE(addr, fp->f_type, c);
222         if (fp->f_args)
223             MOD_ARGS(addr, fp->f_args, c);
224         if (kern64) {
225                 scratch64 = fp->f_addr;
226                 MOD_ADDR(addr, scratch64, c);
227                 scratch64 = fp->f_size;
228                 MOD_SIZE(addr, scratch64, c);
229         } else {
230                 scratch32 = fp->f_addr;
231 #ifdef __arm__
232                 scratch32 -= __elfN(relocation_offset);
233 #endif
234                 MOD_ADDR(addr, scratch32, c);
235                 MOD_SIZE(addr, fp->f_size, c);
236         }
237         for (md = fp->f_metadata; md != NULL; md = md->md_next) {
238             if (!(md->md_type & MODINFOMD_NOCOPY)) {
239                 MOD_METADATA(addr, md, c);
240             }
241         }
242     }
243     MOD_END(addr, c);
244     return(addr);
245 }
246
247 /*
248  * Load the information expected by a kernel.
249  *
250  * - The 'boothowto' argument is constructed
251  * - The 'bootdev' argument is constructed
252  * - The kernel environment is copied into kernel space.
253  * - Module metadata are formatted and placed in kernel space.
254  */
255 static int
256 md_load_dual(char *args, vm_offset_t *modulep, vm_offset_t *dtb, int kern64)
257 {
258     struct preloaded_file       *kfp;
259     struct preloaded_file       *xp;
260     struct file_metadata        *md;
261     vm_offset_t                 kernend;
262     vm_offset_t                 addr;
263     vm_offset_t                 envp;
264 #if defined(LOADER_FDT_SUPPORT)
265     vm_offset_t                 fdtp;
266 #endif
267     vm_offset_t                 size;
268     uint64_t                    scratch64;
269     char                        *rootdevname;
270     int                         howto;
271 #ifdef __arm__
272     vm_offset_t                 vaddr;
273     int                         i;
274
275         /*
276          * These metadata addreses must be converted for kernel after
277          * relocation.
278          */
279     uint32_t                    mdt[] = {
280             MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
281             MODINFOMD_ENVP,
282 #if defined(LOADER_FDT_SUPPORT)
283             MODINFOMD_DTBP
284 #endif
285     };
286 #endif
287
288     align = kern64 ? 8 : 4;
289     howto = md_getboothowto(args);
290
291     /*
292      * Allow the environment variable 'rootdev' to override the supplied
293      * device. This should perhaps go to MI code and/or have $rootdev
294      * tested/set by MI code before launching the kernel.
295      */
296     rootdevname = getenv("rootdev");
297     if (rootdevname == NULL)
298         rootdevname = getenv("currdev");
299     /* Try reading the /etc/fstab file to select the root device */
300     getrootmount(rootdevname);
301
302     /* Find the last module in the chain */
303     addr = 0;
304     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
305         if (addr < (xp->f_addr + xp->f_size))
306             addr = xp->f_addr + xp->f_size;
307     }
308     /* Pad to a page boundary */
309     addr = roundup(addr, PAGE_SIZE);
310
311     /* Copy our environment */
312     envp = addr;
313     addr = md_copyenv(addr);
314
315     /* Pad to a page boundary */
316     addr = roundup(addr, PAGE_SIZE);
317
318 #if defined(LOADER_FDT_SUPPORT)
319     /* Copy out FDT */
320     fdtp = 0;
321 #if defined(__powerpc__)
322     if (getenv("usefdt") != NULL)
323 #endif
324     {
325         size = fdt_copy(addr);
326         fdtp = addr;
327         addr = roundup(addr + size, PAGE_SIZE);
328     }
329 #endif
330
331     kernend = 0;
332     kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
333     if (kfp == NULL)
334         kfp = file_findfile(NULL, "elf kernel");
335     if (kfp == NULL)
336         panic("can't find kernel file");
337     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
338     if (kern64) {
339         scratch64 = envp;
340         file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
341 #if defined(LOADER_FDT_SUPPORT)
342         if (fdtp != 0) {
343             scratch64 = fdtp;
344             file_addmetadata(kfp, MODINFOMD_DTBP, sizeof scratch64, &scratch64);
345         }
346 #endif
347         scratch64 = kernend;
348         file_addmetadata(kfp, MODINFOMD_KERNEND,
349                 sizeof scratch64, &scratch64);
350     } else {
351         file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
352 #if defined(LOADER_FDT_SUPPORT)
353         if (fdtp != 0)
354             file_addmetadata(kfp, MODINFOMD_DTBP, sizeof fdtp, &fdtp);
355 #endif
356         file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
357     }
358
359 #if defined(__sparc64__)
360     file_addmetadata(kfp, MODINFOMD_DTLB_SLOTS,
361         sizeof dtlb_slot, &dtlb_slot);
362     file_addmetadata(kfp, MODINFOMD_ITLB_SLOTS,
363         sizeof itlb_slot, &itlb_slot);
364     file_addmetadata(kfp, MODINFOMD_DTLB,
365         dtlb_slot * sizeof(*dtlb_store), dtlb_store);
366     file_addmetadata(kfp, MODINFOMD_ITLB,
367         itlb_slot * sizeof(*itlb_store), itlb_store);
368 #endif
369
370     *modulep = addr;
371     size = md_copymodules(0, kern64);
372     kernend = roundup(addr + size, PAGE_SIZE);
373
374     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
375     if (kern64) {
376         scratch64 = kernend;
377         bcopy(&scratch64, md->md_data, sizeof scratch64);
378     } else {
379         bcopy(&kernend, md->md_data, sizeof kernend);
380     }
381
382 #ifdef __arm__
383     /* Convert addresses to the final VA */
384     *modulep -= __elfN(relocation_offset);
385
386     /* Do relocation fixup on metadata of each module. */
387     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
388         for (i = 0; i < nitems(mdt); i++) {
389             md = file_findmetadata(xp, mdt[i]);
390                 if (md) {
391                     bcopy(md->md_data, &vaddr, sizeof vaddr);
392                     vaddr -= __elfN(relocation_offset);
393                     bcopy(&vaddr, md->md_data, sizeof vaddr);
394                 }
395             }
396     }
397 #endif
398
399     (void)md_copymodules(addr, kern64);
400 #if defined(LOADER_FDT_SUPPORT)
401     if (dtb != NULL)
402         *dtb = fdtp;
403 #endif
404
405     return(0);
406 }
407
408 #if !defined(__sparc64__)
409 int
410 md_load(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
411 {
412     return (md_load_dual(args, modulep, dtb, 0));
413 }
414 #endif
415
416 #if defined(__mips__) || defined(__powerpc__) || defined(__sparc64__)
417 int
418 md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
419 {
420     return (md_load_dual(args, modulep, dtb, 1));
421 }
422 #endif