2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (C) 2006 Semihalf, Piotr Kruszynski <ppk@semihalf.com>
4 * Copyright (C) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/reboot.h>
35 #include <sys/linker.h>
38 #include <machine/elf.h>
39 #include <machine/metadata.h>
41 #include "api_public.h"
42 #include "bootstrap.h"
45 #if defined(LOADER_FDT_SUPPORT)
46 #include <fdt_platform.h>
50 md_getboothowto(char *kargs)
64 if (!active && (*cp == '-'))
109 /* get equivalents from the environment */
110 for (i = 0; howto_names[i].ev != NULL; i++) {
111 if (getenv(howto_names[i].ev) != NULL)
112 howto |= howto_names[i].mask;
114 if ((p = getenv("console"))) {
115 if (!strcmp(p, "comconsole"))
117 if (!strcmp(p, "nullconsole"))
125 * Copy the environment into the load area starting at (addr).
126 * Each variable is formatted as <name>=<value>, with a single nul
127 * separating each variable, and a double nul terminating the environment.
130 md_copyenv(vm_offset_t addr)
134 /* traverse the environment */
135 for (ep = environ; ep != NULL; ep = ep->ev_next) {
136 archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
137 addr += strlen(ep->ev_name);
138 archsw.arch_copyin("=", addr, 1);
140 if (ep->ev_value != NULL) {
141 archsw.arch_copyin(ep->ev_value, addr,
142 strlen(ep->ev_value));
143 addr += strlen(ep->ev_value);
145 archsw.arch_copyin("", addr, 1);
148 archsw.arch_copyin("", addr, 1);
154 * Copy module-related data into the load area, where it can be
155 * used as a directory for loaded modules.
157 * Module data is presented in a self-describing format. Each datum
158 * is preceded by a 32-bit identifier and a 32-bit size field.
160 * Currently, the following data are saved:
162 * MOD_NAME (variable) module name (string)
163 * MOD_TYPE (variable) module type (string)
164 * MOD_ARGS (variable) module parameters (string)
165 * MOD_ADDR sizeof(vm_offset_t) module load address
166 * MOD_SIZE sizeof(size_t) module size
167 * MOD_METADATA (variable) type-specific metadata
169 #define COPY32(v, a, c) { \
172 archsw.arch_copyin(&x, a, sizeof(x)); \
176 #define MOD_STR(t, a, s, c) { \
178 COPY32(strlen(s) + 1, a, c) \
180 archsw.arch_copyin(s, a, strlen(s) + 1);\
181 a += roundup(strlen(s) + 1, sizeof(u_long));\
184 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
185 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
186 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
188 #define MOD_VAR(t, a, s, c) { \
190 COPY32(sizeof(s), a, c); \
192 archsw.arch_copyin(&s, a, sizeof(s)); \
193 a += roundup(sizeof(s), sizeof(u_long)); \
196 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
197 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
199 #define MOD_METADATA(a, mm, c) { \
200 COPY32(MODINFO_METADATA | mm->md_type, a, c);\
201 COPY32(mm->md_size, a, c); \
203 archsw.arch_copyin(mm->md_data, a, mm->md_size);\
204 a += roundup(mm->md_size, sizeof(u_long)); \
207 #define MOD_END(a, c) { \
208 COPY32(MODINFO_END, a, c); \
213 md_copymodules(vm_offset_t addr)
215 struct preloaded_file *fp;
216 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 be first */
225 MOD_TYPE(addr, fp->f_type, c);
227 MOD_ARGS(addr, fp->f_args, c);
228 a = fp->f_addr - __elfN(relocation_offset);
229 MOD_ADDR(addr, a, c);
230 MOD_SIZE(addr, fp->f_size, c);
231 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
232 if (!(md->md_type & MODINFOMD_NOCOPY))
233 MOD_METADATA(addr, md, c);
241 * Load the information expected by a kernel.
243 * - The 'boothowto' argument is constructed
244 * - The 'bootdev' argument is constructed
245 * - The kernel environment is copied into kernel space.
246 * - Module metadata are formatted and placed in kernel space.
249 md_load(char *args, vm_offset_t *modulep)
251 struct preloaded_file *kfp, *bfp;
252 struct preloaded_file *xp;
253 struct file_metadata *md;
254 struct bootinfo *bip;
260 #if defined(LOADER_FDT_SUPPORT)
269 * These metadata addreses must be converted for kernel after
273 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
275 #if defined(LOADER_FDT_SUPPORT)
280 howto = md_getboothowto(args);
283 * Allow the environment variable 'rootdev' to override the supplied
284 * device. This should perhaps go to MI code and/or have $rootdev
285 * tested/set by MI code before launching the kernel.
287 rootdevname = getenv("rootdev");
288 if (rootdevname == NULL)
289 rootdevname = getenv("currdev");
290 /* Try reading the /etc/fstab file to select the root device */
291 getrootmount(rootdevname);
293 /* Find the last module in the chain */
295 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
296 if (addr < (xp->f_addr + xp->f_size))
297 addr = xp->f_addr + xp->f_size;
299 /* Pad to a page boundary */
300 addr = roundup(addr, PAGE_SIZE);
302 /* Copy our environment */
304 addr = md_copyenv(addr);
306 /* Pad to a page boundary */
307 addr = roundup(addr, PAGE_SIZE);
309 #if defined(LOADER_FDT_SUPPORT)
310 /* Handle device tree blob */
312 dtb_size = fdt_copy(addr);
314 /* Pad to a page boundary */
316 addr += roundup(dtb_size, PAGE_SIZE);
320 kfp = file_findfile(NULL, "elf32 kernel");
322 kfp = file_findfile(NULL, "elf kernel");
324 panic("can't find kernel file");
325 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
326 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
328 #if defined(LOADER_FDT_SUPPORT)
330 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
332 pager_output("WARNING! Trying to fire up the kernel, but no "
333 "device tree blob found!\n");
336 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
338 /* Figure out the size and location of the metadata */
340 size = md_copymodules(0);
341 kernend = roundup(addr + size, PAGE_SIZE);
343 /* Provide MODINFOMD_KERNEND */
344 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
345 bcopy(&kernend, md->md_data, sizeof kernend);
347 /* Convert addresses to the final VA */
348 *modulep -= __elfN(relocation_offset);
350 /* Do relocation fixup on metadata of each module. */
351 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
352 for (i = 0; i < sizeof mdt / sizeof mdt[0]; i++) {
353 md = file_findmetadata(xp, mdt[i]);
355 bcopy(md->md_data, &vaddr, sizeof vaddr);
356 vaddr -= __elfN(relocation_offset);
357 bcopy(&vaddr, md->md_data, sizeof vaddr);
362 /* Only now copy actual modules and metadata */
363 (void)md_copymodules(addr);