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
52 #if defined(__sparc64__)
55 extern struct tlb_entry *dtlb_store;
56 extern struct tlb_entry *itlb_store;
70 if ((node = OF_finddevice("/options")) == -1)
72 if (OF_getprop(node, "input-device", buf, sizeof(buf)) == -1)
74 input = OF_finddevice(buf);
75 if (OF_getprop(node, "output-device", buf, sizeof(buf)) == -1)
77 output = OF_finddevice(buf);
78 if (input == -1 || output == -1 ||
79 OF_getproplen(input, "keyboard") >= 0) {
80 if ((node = OF_finddevice("/chosen")) == -1)
82 if (OF_getprop(node, "stdin", &inst, sizeof(inst)) == -1)
84 if ((input = OF_instance_to_package(inst)) == -1)
86 if (OF_getprop(node, "stdout", &inst, sizeof(inst)) == -1)
88 if ((output = OF_instance_to_package(inst)) == -1)
93 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
95 if (strcmp(buf, "serial") != 0)
102 md_getboothowto(char *kargs)
107 howto = boot_parse_cmdline(kargs);
108 howto |= boot_env_to_howto();
109 #if defined(__sparc64__)
110 if (md_bootserial() != -1)
113 if (!strcmp(getenv("console"), "comconsole"))
115 if (!strcmp(getenv("console"), "nullconsole"))
122 * Copy the environment into the load area starting at (addr).
123 * Each variable is formatted as <name>=<value>, with a single nul
124 * separating each variable, and a double nul terminating the environment.
127 md_copyenv(vm_offset_t addr)
131 /* traverse the environment */
132 for (ep = environ; ep != NULL; ep = ep->ev_next) {
133 archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
134 addr += strlen(ep->ev_name);
135 archsw.arch_copyin("=", addr, 1);
137 if (ep->ev_value != NULL) {
138 archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
139 addr += strlen(ep->ev_value);
141 archsw.arch_copyin("", addr, 1);
144 archsw.arch_copyin("", addr, 1);
150 * Copy module-related data into the load area, where it can be
151 * used as a directory for loaded modules.
153 * Module data is presented in a self-describing format. Each datum
154 * is preceded by a 32-bit identifier and a 32-bit size field.
156 * Currently, the following data are saved:
158 * MOD_NAME (variable) module name (string)
159 * MOD_TYPE (variable) module type (string)
160 * MOD_ARGS (variable) module parameters (string)
161 * MOD_ADDR sizeof(vm_offset_t) module load address
162 * MOD_SIZE sizeof(size_t) module size
163 * MOD_METADATA (variable) type-specific metadata
168 #define COPY32(v, a, c) { \
171 archsw.arch_copyin(&x, a, sizeof(x)); \
175 #define MOD_STR(t, a, s, c) { \
177 COPY32(strlen(s) + 1, a, c) \
179 archsw.arch_copyin(s, a, strlen(s) + 1);\
180 a += roundup(strlen(s) + 1, align); \
183 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
184 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
185 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
187 #define MOD_VAR(t, a, s, c) { \
189 COPY32(sizeof(s), a, c); \
191 archsw.arch_copyin(&s, a, sizeof(s)); \
192 a += roundup(sizeof(s), align); \
195 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
196 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
198 #define MOD_METADATA(a, mm, c) { \
199 COPY32(MODINFO_METADATA | mm->md_type, a, c);\
200 COPY32(mm->md_size, a, c); \
202 archsw.arch_copyin(mm->md_data, a, mm->md_size);\
203 a += roundup(mm->md_size, align); \
206 #define MOD_END(a, c) { \
207 COPY32(MODINFO_END, a, c); \
212 md_copymodules(vm_offset_t addr, int kern64)
214 struct preloaded_file *fp;
215 struct file_metadata *md;
221 /* start with the first module on the list, should be the kernel */
222 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
224 MOD_NAME(addr, fp->f_name, c); /* this field must come first */
225 MOD_TYPE(addr, fp->f_type, c);
227 MOD_ARGS(addr, fp->f_args, c);
229 scratch64 = fp->f_addr;
230 MOD_ADDR(addr, scratch64, c);
231 scratch64 = fp->f_size;
232 MOD_SIZE(addr, scratch64, c);
234 scratch32 = fp->f_addr;
236 scratch32 -= __elfN(relocation_offset);
238 MOD_ADDR(addr, scratch32, c);
239 MOD_SIZE(addr, fp->f_size, c);
241 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
242 if (!(md->md_type & MODINFOMD_NOCOPY)) {
243 MOD_METADATA(addr, md, c);
252 * Load the information expected by a kernel.
254 * - The 'boothowto' argument is constructed
255 * - The 'bootdev' argument is constructed
256 * - The kernel environment is copied into kernel space.
257 * - Module metadata are formatted and placed in kernel space.
260 md_load_dual(char *args, vm_offset_t *modulep, vm_offset_t *dtb, int kern64)
262 struct preloaded_file *kfp;
263 struct preloaded_file *xp;
264 struct file_metadata *md;
268 #if defined(LOADER_FDT_SUPPORT)
280 * These metadata addreses must be converted for kernel after
284 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
286 #if defined(LOADER_FDT_SUPPORT)
292 align = kern64 ? 8 : 4;
293 howto = md_getboothowto(args);
296 * Allow the environment variable 'rootdev' to override the supplied
297 * device. This should perhaps go to MI code and/or have $rootdev
298 * tested/set by MI code before launching the kernel.
300 rootdevname = getenv("rootdev");
301 if (rootdevname == NULL)
302 rootdevname = getenv("currdev");
303 /* Try reading the /etc/fstab file to select the root device */
304 getrootmount(rootdevname);
306 /* Find the last module in the chain */
308 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
309 if (addr < (xp->f_addr + xp->f_size))
310 addr = xp->f_addr + xp->f_size;
312 /* Pad to a page boundary */
313 addr = roundup(addr, PAGE_SIZE);
315 /* Copy our environment */
317 addr = md_copyenv(addr);
319 /* Pad to a page boundary */
320 addr = roundup(addr, PAGE_SIZE);
322 #if defined(LOADER_FDT_SUPPORT)
325 #if defined(__powerpc__)
326 if (getenv("usefdt") != NULL)
329 size = fdt_copy(addr);
331 addr = roundup(addr + size, PAGE_SIZE);
336 kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
338 kfp = file_findfile(NULL, "elf kernel");
340 panic("can't find kernel file");
341 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
344 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
345 #if defined(LOADER_FDT_SUPPORT)
348 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof scratch64, &scratch64);
352 file_addmetadata(kfp, MODINFOMD_KERNEND,
353 sizeof scratch64, &scratch64);
355 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
356 #if defined(LOADER_FDT_SUPPORT)
358 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof fdtp, &fdtp);
360 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
362 #ifdef LOADER_GELI_SUPPORT
363 geli_export_key_metadata(kfp);
365 #if defined(__sparc64__)
366 file_addmetadata(kfp, MODINFOMD_DTLB_SLOTS,
367 sizeof dtlb_slot, &dtlb_slot);
368 file_addmetadata(kfp, MODINFOMD_ITLB_SLOTS,
369 sizeof itlb_slot, &itlb_slot);
370 file_addmetadata(kfp, MODINFOMD_DTLB,
371 dtlb_slot * sizeof(*dtlb_store), dtlb_store);
372 file_addmetadata(kfp, MODINFOMD_ITLB,
373 itlb_slot * sizeof(*itlb_store), itlb_store);
377 size = md_copymodules(0, kern64);
378 kernend = roundup(addr + size, PAGE_SIZE);
380 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
383 bcopy(&scratch64, md->md_data, sizeof scratch64);
385 bcopy(&kernend, md->md_data, sizeof kernend);
389 /* Convert addresses to the final VA */
390 *modulep -= __elfN(relocation_offset);
392 /* Do relocation fixup on metadata of each module. */
393 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
394 for (i = 0; i < nitems(mdt); i++) {
395 md = file_findmetadata(xp, mdt[i]);
397 bcopy(md->md_data, &vaddr, sizeof vaddr);
398 vaddr -= __elfN(relocation_offset);
399 bcopy(&vaddr, md->md_data, sizeof vaddr);
405 (void)md_copymodules(addr, kern64);
406 #if defined(LOADER_FDT_SUPPORT)
414 #if !defined(__sparc64__)
416 md_load(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
418 return (md_load_dual(args, modulep, dtb, 0));
422 #if defined(__mips__) || defined(__powerpc__) || defined(__sparc64__)
424 md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
426 return (md_load_dual(args, modulep, dtb, 1));