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 #if defined(__sparc64__)
51 extern struct tlb_entry *dtlb_store;
52 extern struct tlb_entry *itlb_store;
66 if ((node = OF_finddevice("/options")) == -1)
68 if (OF_getprop(node, "input-device", buf, sizeof(buf)) == -1)
70 input = OF_finddevice(buf);
71 if (OF_getprop(node, "output-device", buf, sizeof(buf)) == -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)
78 if (OF_getprop(node, "stdin", &inst, sizeof(inst)) == -1)
80 if ((input = OF_instance_to_package(inst)) == -1)
82 if (OF_getprop(node, "stdout", &inst, sizeof(inst)) == -1)
84 if ((output = OF_instance_to_package(inst)) == -1)
89 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
91 if (strcmp(buf, "serial") != 0)
98 md_getboothowto(char *kargs)
103 howto = boot_parse_cmdline(kargs);
104 howto |= boot_env_to_howto();
105 #if defined(__sparc64__)
106 if (md_bootserial() != -1)
109 if (!strcmp(getenv("console"), "comconsole"))
111 if (!strcmp(getenv("console"), "nullconsole"))
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.
123 md_copyenv(vm_offset_t addr)
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);
133 if (ep->ev_value != NULL) {
134 archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
135 addr += strlen(ep->ev_value);
137 archsw.arch_copyin("", addr, 1);
140 archsw.arch_copyin("", addr, 1);
146 * Copy module-related data into the load area, where it can be
147 * used as a directory for loaded modules.
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.
152 * Currently, the following data are saved:
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
164 #define COPY32(v, a, c) { \
167 archsw.arch_copyin(&x, a, sizeof(x)); \
171 #define MOD_STR(t, a, s, c) { \
173 COPY32(strlen(s) + 1, a, c) \
175 archsw.arch_copyin(s, a, strlen(s) + 1);\
176 a += roundup(strlen(s) + 1, align); \
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)
183 #define MOD_VAR(t, a, s, c) { \
185 COPY32(sizeof(s), a, c); \
187 archsw.arch_copyin(&s, a, sizeof(s)); \
188 a += roundup(sizeof(s), align); \
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)
194 #define MOD_METADATA(a, mm, c) { \
195 COPY32(MODINFO_METADATA | mm->md_type, a, c);\
196 COPY32(mm->md_size, a, c); \
198 archsw.arch_copyin(mm->md_data, a, mm->md_size);\
199 a += roundup(mm->md_size, align); \
202 #define MOD_END(a, c) { \
203 COPY32(MODINFO_END, a, c); \
208 md_copymodules(vm_offset_t addr, int kern64)
210 struct preloaded_file *fp;
211 struct file_metadata *md;
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) {
220 MOD_NAME(addr, fp->f_name, c); /* this field must come first */
221 MOD_TYPE(addr, fp->f_type, c);
223 MOD_ARGS(addr, fp->f_args, c);
225 scratch64 = fp->f_addr;
226 MOD_ADDR(addr, scratch64, c);
227 scratch64 = fp->f_size;
228 MOD_SIZE(addr, scratch64, c);
230 scratch32 = fp->f_addr;
232 scratch32 -= __elfN(relocation_offset);
234 MOD_ADDR(addr, scratch32, c);
235 MOD_SIZE(addr, fp->f_size, c);
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);
248 * Load the information expected by a kernel.
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.
256 md_load_dual(char *args, vm_offset_t *modulep, vm_offset_t *dtb, int kern64)
258 struct preloaded_file *kfp;
259 struct preloaded_file *xp;
260 struct file_metadata *md;
264 #if defined(LOADER_FDT_SUPPORT)
276 * These metadata addreses must be converted for kernel after
280 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
282 #if defined(LOADER_FDT_SUPPORT)
288 align = kern64 ? 8 : 4;
289 howto = md_getboothowto(args);
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.
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);
302 /* Find the last module in the chain */
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;
308 /* Pad to a page boundary */
309 addr = roundup(addr, PAGE_SIZE);
311 /* Copy our environment */
313 addr = md_copyenv(addr);
315 /* Pad to a page boundary */
316 addr = roundup(addr, PAGE_SIZE);
318 #if defined(LOADER_FDT_SUPPORT)
321 #if defined(__powerpc__)
322 if (getenv("usefdt") != NULL)
325 size = fdt_copy(addr);
327 addr = roundup(addr + size, PAGE_SIZE);
332 kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
334 kfp = file_findfile(NULL, "elf kernel");
336 panic("can't find kernel file");
337 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
340 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
341 #if defined(LOADER_FDT_SUPPORT)
344 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof scratch64, &scratch64);
348 file_addmetadata(kfp, MODINFOMD_KERNEND,
349 sizeof scratch64, &scratch64);
351 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
352 #if defined(LOADER_FDT_SUPPORT)
354 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof fdtp, &fdtp);
356 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
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);
371 size = md_copymodules(0, kern64);
372 kernend = roundup(addr + size, PAGE_SIZE);
374 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
377 bcopy(&scratch64, md->md_data, sizeof scratch64);
379 bcopy(&kernend, md->md_data, sizeof kernend);
383 /* Convert addresses to the final VA */
384 *modulep -= __elfN(relocation_offset);
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]);
391 bcopy(md->md_data, &vaddr, sizeof vaddr);
392 vaddr -= __elfN(relocation_offset);
393 bcopy(&vaddr, md->md_data, sizeof vaddr);
399 (void)md_copymodules(addr, kern64);
400 #if defined(LOADER_FDT_SUPPORT)
408 #if !defined(__sparc64__)
410 md_load(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
412 return (md_load_dual(args, modulep, dtb, 0));
416 #if defined(__mips__) || defined(__powerpc__) || defined(__sparc64__)
418 md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
420 return (md_load_dual(args, modulep, dtb, 1));