2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * from: FreeBSD: src/sys/boot/sparc64/loader/metadata.c,v 1.6
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/linker.h>
36 #include <sys/reboot.h>
37 #if defined(LOADER_FDT_SUPPORT)
38 #include <fdt_platform.h>
42 #include <machine/elf.h>
44 #include <machine/metadata.h>
46 #include "bootstrap.h"
48 #ifdef LOADER_GELI_SUPPORT
53 md_getboothowto(char *kargs)
58 howto = boot_parse_cmdline(kargs);
59 howto |= boot_env_to_howto();
60 if (!strcmp(getenv("console"), "comconsole"))
62 if (!strcmp(getenv("console"), "nullconsole"))
68 * Copy the environment into the load area starting at (addr).
69 * Each variable is formatted as <name>=<value>, with a single nul
70 * separating each variable, and a double nul terminating the environment.
73 md_copyenv(vm_offset_t addr)
77 /* traverse the environment */
78 for (ep = environ; ep != NULL; ep = ep->ev_next) {
79 archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
80 addr += strlen(ep->ev_name);
81 archsw.arch_copyin("=", addr, 1);
83 if (ep->ev_value != NULL) {
84 archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
85 addr += strlen(ep->ev_value);
87 archsw.arch_copyin("", addr, 1);
90 archsw.arch_copyin("", addr, 1);
96 * Copy module-related data into the load area, where it can be
97 * used as a directory for loaded modules.
99 * Module data is presented in a self-describing format. Each datum
100 * is preceded by a 32-bit identifier and a 32-bit size field.
102 * Currently, the following data are saved:
104 * MOD_NAME (variable) module name (string)
105 * MOD_TYPE (variable) module type (string)
106 * MOD_ARGS (variable) module parameters (string)
107 * MOD_ADDR sizeof(vm_offset_t) module load address
108 * MOD_SIZE sizeof(size_t) module size
109 * MOD_METADATA (variable) type-specific metadata
114 #define COPY32(v, a, c) { \
117 archsw.arch_copyin(&x, a, sizeof(x)); \
121 #define MOD_STR(t, a, s, c) { \
123 COPY32(strlen(s) + 1, a, c) \
125 archsw.arch_copyin(s, a, strlen(s) + 1);\
126 a += roundup(strlen(s) + 1, align); \
129 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
130 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
131 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
133 #define MOD_VAR(t, a, s, c) { \
135 COPY32(sizeof(s), a, c); \
137 archsw.arch_copyin(&s, a, sizeof(s)); \
138 a += roundup(sizeof(s), align); \
141 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
142 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
144 #define MOD_METADATA(a, mm, c) { \
145 COPY32(MODINFO_METADATA | mm->md_type, a, c);\
146 COPY32(mm->md_size, a, c); \
148 archsw.arch_copyin(mm->md_data, a, mm->md_size);\
149 a += roundup(mm->md_size, align); \
152 #define MOD_END(a, c) { \
153 COPY32(MODINFO_END, a, c); \
158 md_copymodules(vm_offset_t addr, int kern64)
160 struct preloaded_file *fp;
161 struct file_metadata *md;
167 /* start with the first module on the list, should be the kernel */
168 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
170 MOD_NAME(addr, fp->f_name, c); /* this field must come first */
171 MOD_TYPE(addr, fp->f_type, c);
173 MOD_ARGS(addr, fp->f_args, c);
175 scratch64 = fp->f_addr;
176 MOD_ADDR(addr, scratch64, c);
177 scratch64 = fp->f_size;
178 MOD_SIZE(addr, scratch64, c);
180 scratch32 = fp->f_addr;
182 scratch32 -= __elfN(relocation_offset);
184 MOD_ADDR(addr, scratch32, c);
185 MOD_SIZE(addr, fp->f_size, c);
187 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
188 if (!(md->md_type & MODINFOMD_NOCOPY)) {
189 MOD_METADATA(addr, md, c);
198 * Load the information expected by a kernel.
200 * - The 'boothowto' argument is constructed
201 * - The 'bootdev' argument is constructed
202 * - The kernel environment is copied into kernel space.
203 * - Module metadata are formatted and placed in kernel space.
206 md_load_dual(char *args, vm_offset_t *modulep, vm_offset_t *dtb, int kern64)
208 struct preloaded_file *kfp;
209 struct preloaded_file *xp;
210 struct file_metadata *md;
214 #if defined(LOADER_FDT_SUPPORT)
226 * These metadata addreses must be converted for kernel after
230 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
232 #if defined(LOADER_FDT_SUPPORT)
238 align = kern64 ? 8 : 4;
239 howto = md_getboothowto(args);
242 * Allow the environment variable 'rootdev' to override the supplied
243 * device. This should perhaps go to MI code and/or have $rootdev
244 * tested/set by MI code before launching the kernel.
246 rootdevname = getenv("rootdev");
247 if (rootdevname == NULL)
248 rootdevname = getenv("currdev");
249 /* Try reading the /etc/fstab file to select the root device */
250 getrootmount(rootdevname);
252 /* Find the last module in the chain */
254 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
255 if (addr < (xp->f_addr + xp->f_size))
256 addr = xp->f_addr + xp->f_size;
258 /* Pad to a page boundary */
259 addr = roundup(addr, PAGE_SIZE);
261 /* Copy our environment */
263 addr = md_copyenv(addr);
265 /* Pad to a page boundary */
266 addr = roundup(addr, PAGE_SIZE);
268 #if defined(LOADER_FDT_SUPPORT)
271 #if defined(__powerpc__)
272 if (getenv("usefdt") != NULL)
275 size = fdt_copy(addr);
277 addr = roundup(addr + size, PAGE_SIZE);
282 kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
284 kfp = file_findfile(NULL, "elf kernel");
286 panic("can't find kernel file");
287 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
290 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
291 #if defined(LOADER_FDT_SUPPORT)
294 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof scratch64, &scratch64);
298 file_addmetadata(kfp, MODINFOMD_KERNEND,
299 sizeof scratch64, &scratch64);
301 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
302 #if defined(LOADER_FDT_SUPPORT)
304 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof fdtp, &fdtp);
306 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
308 #ifdef LOADER_GELI_SUPPORT
309 geli_export_key_metadata(kfp);
313 size = md_copymodules(0, kern64);
314 kernend = roundup(addr + size, PAGE_SIZE);
316 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
319 bcopy(&scratch64, md->md_data, sizeof scratch64);
321 bcopy(&kernend, md->md_data, sizeof kernend);
325 /* Convert addresses to the final VA */
326 *modulep -= __elfN(relocation_offset);
328 /* Do relocation fixup on metadata of each module. */
329 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
330 for (i = 0; i < nitems(mdt); i++) {
331 md = file_findmetadata(xp, mdt[i]);
333 bcopy(md->md_data, &vaddr, sizeof vaddr);
334 vaddr -= __elfN(relocation_offset);
335 bcopy(&vaddr, md->md_data, sizeof vaddr);
341 (void)md_copymodules(addr, kern64);
342 #if defined(LOADER_FDT_SUPPORT)
351 md_load(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
353 return (md_load_dual(args, modulep, dtb, 0));
356 #if defined(__powerpc__)
358 md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
360 return (md_load_dual(args, modulep, dtb, 1));