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/reboot.h>
35 #include <sys/linker.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)
111 if (!active && (*cp == '-')) {
125 howto |= RB_MULTIPLE;
140 howto |= RB_DFLTROOT;
156 /* get equivalents from the environment */
157 for (i = 0; howto_names[i].ev != NULL; i++)
158 if (getenv(howto_names[i].ev) != NULL)
159 howto |= howto_names[i].mask;
160 #if defined(__sparc64__)
161 if (md_bootserial() != -1)
164 if (!strcmp(getenv("console"), "comconsole"))
166 if (!strcmp(getenv("console"), "nullconsole"))
173 * Copy the environment into the load area starting at (addr).
174 * Each variable is formatted as <name>=<value>, with a single nul
175 * separating each variable, and a double nul terminating the environment.
178 md_copyenv(vm_offset_t addr)
182 /* traverse the environment */
183 for (ep = environ; ep != NULL; ep = ep->ev_next) {
184 archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
185 addr += strlen(ep->ev_name);
186 archsw.arch_copyin("=", addr, 1);
188 if (ep->ev_value != NULL) {
189 archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
190 addr += strlen(ep->ev_value);
192 archsw.arch_copyin("", addr, 1);
195 archsw.arch_copyin("", addr, 1);
201 * Copy module-related data into the load area, where it can be
202 * used as a directory for loaded modules.
204 * Module data is presented in a self-describing format. Each datum
205 * is preceded by a 32-bit identifier and a 32-bit size field.
207 * Currently, the following data are saved:
209 * MOD_NAME (variable) module name (string)
210 * MOD_TYPE (variable) module type (string)
211 * MOD_ARGS (variable) module parameters (string)
212 * MOD_ADDR sizeof(vm_offset_t) module load address
213 * MOD_SIZE sizeof(size_t) module size
214 * MOD_METADATA (variable) type-specific metadata
219 #define COPY32(v, a, c) { \
222 archsw.arch_copyin(&x, a, sizeof(x)); \
226 #define MOD_STR(t, a, s, c) { \
228 COPY32(strlen(s) + 1, a, c) \
230 archsw.arch_copyin(s, a, strlen(s) + 1);\
231 a += roundup(strlen(s) + 1, align); \
234 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
235 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
236 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
238 #define MOD_VAR(t, a, s, c) { \
240 COPY32(sizeof(s), a, c); \
242 archsw.arch_copyin(&s, a, sizeof(s)); \
243 a += roundup(sizeof(s), align); \
246 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
247 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
249 #define MOD_METADATA(a, mm, c) { \
250 COPY32(MODINFO_METADATA | mm->md_type, a, c);\
251 COPY32(mm->md_size, a, c); \
253 archsw.arch_copyin(mm->md_data, a, mm->md_size);\
254 a += roundup(mm->md_size, align); \
257 #define MOD_END(a, c) { \
258 COPY32(MODINFO_END, a, c); \
263 md_copymodules(vm_offset_t addr, int kern64)
265 struct preloaded_file *fp;
266 struct file_metadata *md;
272 /* start with the first module on the list, should be the kernel */
273 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
275 MOD_NAME(addr, fp->f_name, c); /* this field must come first */
276 MOD_TYPE(addr, fp->f_type, c);
278 MOD_ARGS(addr, fp->f_args, c);
280 scratch64 = fp->f_addr;
281 MOD_ADDR(addr, scratch64, c);
282 scratch64 = fp->f_size;
283 MOD_SIZE(addr, scratch64, c);
285 scratch32 = fp->f_addr;
287 scratch32 -= __elfN(relocation_offset);
289 MOD_ADDR(addr, scratch32, c);
290 MOD_SIZE(addr, fp->f_size, c);
292 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
293 if (!(md->md_type & MODINFOMD_NOCOPY)) {
294 MOD_METADATA(addr, md, c);
303 * Load the information expected by a kernel.
305 * - The 'boothowto' argument is constructed
306 * - The 'bootdev' argument is constructed
307 * - The kernel environment is copied into kernel space.
308 * - Module metadata are formatted and placed in kernel space.
311 md_load_dual(char *args, vm_offset_t *modulep, vm_offset_t *dtb, int kern64)
313 struct preloaded_file *kfp;
314 struct preloaded_file *xp;
315 struct file_metadata *md;
319 #if defined(LOADER_FDT_SUPPORT)
331 * These metadata addreses must be converted for kernel after
335 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
337 #if defined(LOADER_FDT_SUPPORT)
343 align = kern64 ? 8 : 4;
344 howto = md_getboothowto(args);
347 * Allow the environment variable 'rootdev' to override the supplied
348 * device. This should perhaps go to MI code and/or have $rootdev
349 * tested/set by MI code before launching the kernel.
351 rootdevname = getenv("rootdev");
352 if (rootdevname == NULL)
353 rootdevname = getenv("currdev");
354 /* Try reading the /etc/fstab file to select the root device */
355 getrootmount(rootdevname);
357 /* Find the last module in the chain */
359 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
360 if (addr < (xp->f_addr + xp->f_size))
361 addr = xp->f_addr + xp->f_size;
363 /* Pad to a page boundary */
364 addr = roundup(addr, PAGE_SIZE);
366 /* Copy our environment */
368 addr = md_copyenv(addr);
370 /* Pad to a page boundary */
371 addr = roundup(addr, PAGE_SIZE);
373 #if defined(LOADER_FDT_SUPPORT)
376 #if defined(__powerpc__)
377 if (getenv("usefdt") != NULL)
380 size = fdt_copy(addr);
382 addr = roundup(addr + size, PAGE_SIZE);
387 kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
389 kfp = file_findfile(NULL, "elf kernel");
391 panic("can't find kernel file");
392 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
395 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
396 #if defined(LOADER_FDT_SUPPORT)
399 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof scratch64, &scratch64);
403 file_addmetadata(kfp, MODINFOMD_KERNEND,
404 sizeof scratch64, &scratch64);
406 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
407 #if defined(LOADER_FDT_SUPPORT)
409 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof fdtp, &fdtp);
411 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
414 #if defined(__sparc64__)
415 file_addmetadata(kfp, MODINFOMD_DTLB_SLOTS,
416 sizeof dtlb_slot, &dtlb_slot);
417 file_addmetadata(kfp, MODINFOMD_ITLB_SLOTS,
418 sizeof itlb_slot, &itlb_slot);
419 file_addmetadata(kfp, MODINFOMD_DTLB,
420 dtlb_slot * sizeof(*dtlb_store), dtlb_store);
421 file_addmetadata(kfp, MODINFOMD_ITLB,
422 itlb_slot * sizeof(*itlb_store), itlb_store);
426 size = md_copymodules(0, kern64);
427 kernend = roundup(addr + size, PAGE_SIZE);
429 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
432 bcopy(&scratch64, md->md_data, sizeof scratch64);
434 bcopy(&kernend, md->md_data, sizeof kernend);
438 /* Convert addresses to the final VA */
439 *modulep -= __elfN(relocation_offset);
441 /* Do relocation fixup on metadata of each module. */
442 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
443 for (i = 0; i < nitems(mdt); i++) {
444 md = file_findmetadata(xp, mdt[i]);
446 bcopy(md->md_data, &vaddr, sizeof vaddr);
447 vaddr -= __elfN(relocation_offset);
448 bcopy(&vaddr, md->md_data, sizeof vaddr);
454 (void)md_copymodules(addr, kern64);
455 #if defined(LOADER_FDT_SUPPORT)
464 md_load(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
466 return (md_load_dual(args, modulep, dtb, 0));
469 #if defined(__mips__) || defined(__powerpc__)
471 md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
473 return (md_load_dual(args, modulep, dtb, 1));