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>
37 #include <machine/elf.h>
38 #include <machine/metadata.h>
40 #include "api_public.h"
41 #include "bootstrap.h"
44 #if defined(LOADER_FDT_SUPPORT)
45 extern int fdt_copy(vm_offset_t);
49 * Return a 'boothowto' value corresponding to the kernel arguments in
50 * (kargs) and any relevant environment variables.
57 {"boot_askname", RB_ASKNAME},
58 {"boot_cdrom", RB_CDROM},
60 {"boot_dfltroot", RB_DFLTROOT},
62 {"boot_multicons", RB_MULTIPLE},
63 {"boot_mute", RB_MUTE},
64 {"boot_pause", RB_PAUSE},
65 {"boot_serial", RB_SERIAL},
66 {"boot_single", RB_SINGLE},
67 {"boot_verbose", RB_VERBOSE},
72 md_getboothowto(char *kargs)
86 if (!active && (*cp == '-'))
100 howto |= RB_MULTIPLE;
115 howto |= RB_DFLTROOT;
131 /* get equivalents from the environment */
132 for (i = 0; howto_names[i].ev != NULL; i++) {
133 if (getenv(howto_names[i].ev) != NULL)
134 howto |= howto_names[i].mask;
136 if ((p = getenv("console"))) {
137 if (!strcmp(p, "comconsole"))
139 if (!strcmp(p, "nullconsole"))
147 * Copy the environment into the load area starting at (addr).
148 * Each variable is formatted as <name>=<value>, with a single nul
149 * separating each variable, and a double nul terminating the environment.
152 md_copyenv(vm_offset_t addr)
156 /* traverse the environment */
157 for (ep = environ; ep != NULL; ep = ep->ev_next) {
158 archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
159 addr += strlen(ep->ev_name);
160 archsw.arch_copyin("=", addr, 1);
162 if (ep->ev_value != NULL) {
163 archsw.arch_copyin(ep->ev_value, addr,
164 strlen(ep->ev_value));
165 addr += strlen(ep->ev_value);
167 archsw.arch_copyin("", addr, 1);
170 archsw.arch_copyin("", addr, 1);
176 * Copy module-related data into the load area, where it can be
177 * used as a directory for loaded modules.
179 * Module data is presented in a self-describing format. Each datum
180 * is preceded by a 32-bit identifier and a 32-bit size field.
182 * Currently, the following data are saved:
184 * MOD_NAME (variable) module name (string)
185 * MOD_TYPE (variable) module type (string)
186 * MOD_ARGS (variable) module parameters (string)
187 * MOD_ADDR sizeof(vm_offset_t) module load address
188 * MOD_SIZE sizeof(size_t) module size
189 * MOD_METADATA (variable) type-specific metadata
191 #define COPY32(v, a, c) { \
194 archsw.arch_copyin(&x, a, sizeof(x)); \
198 #define MOD_STR(t, a, s, c) { \
200 COPY32(strlen(s) + 1, a, c) \
202 archsw.arch_copyin(s, a, strlen(s) + 1);\
203 a += roundup(strlen(s) + 1, sizeof(u_long));\
206 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
207 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
208 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
210 #define MOD_VAR(t, a, s, c) { \
212 COPY32(sizeof(s), a, c); \
214 archsw.arch_copyin(&s, a, sizeof(s)); \
215 a += roundup(sizeof(s), sizeof(u_long)); \
218 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
219 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
221 #define MOD_METADATA(a, mm, c) { \
222 COPY32(MODINFO_METADATA | mm->md_type, a, c);\
223 COPY32(mm->md_size, a, c); \
225 archsw.arch_copyin(mm->md_data, a, mm->md_size);\
226 a += roundup(mm->md_size, sizeof(u_long)); \
229 #define MOD_END(a, c) { \
230 COPY32(MODINFO_END, a, c); \
235 md_copymodules(vm_offset_t addr)
237 struct preloaded_file *fp;
238 struct file_metadata *md;
243 /* start with the first module on the list, should be the kernel */
244 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
246 MOD_NAME(addr, fp->f_name, c); /* this field must be first */
247 MOD_TYPE(addr, fp->f_type, c);
249 MOD_ARGS(addr, fp->f_args, c);
250 a = fp->f_addr - __elfN(relocation_offset);
251 MOD_ADDR(addr, a, c);
252 MOD_SIZE(addr, fp->f_size, c);
253 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
254 if (!(md->md_type & MODINFOMD_NOCOPY))
255 MOD_METADATA(addr, md, c);
263 * Load the information expected by a kernel.
265 * - The 'boothowto' argument is constructed
266 * - The 'bootdev' argument is constructed
267 * - The kernel environment is copied into kernel space.
268 * - Module metadata are formatted and placed in kernel space.
271 md_load(char *args, vm_offset_t *modulep)
273 struct preloaded_file *kfp, *bfp;
274 struct preloaded_file *xp;
275 struct file_metadata *md;
276 struct bootinfo *bip;
282 #if defined(LOADER_FDT_SUPPORT)
291 * These metadata addreses must be converted for kernel after
295 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
297 #if defined(LOADER_FDT_SUPPORT)
302 howto = md_getboothowto(args);
305 * Allow the environment variable 'rootdev' to override the supplied
306 * device. This should perhaps go to MI code and/or have $rootdev
307 * tested/set by MI code before launching the kernel.
309 rootdevname = getenv("rootdev");
310 if (rootdevname == NULL)
311 rootdevname = getenv("currdev");
312 /* Try reading the /etc/fstab file to select the root device */
313 getrootmount(rootdevname);
315 /* Find the last module in the chain */
317 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
318 if (addr < (xp->f_addr + xp->f_size))
319 addr = xp->f_addr + xp->f_size;
321 /* Pad to a page boundary */
322 addr = roundup(addr, PAGE_SIZE);
324 /* Copy our environment */
326 addr = md_copyenv(addr);
328 /* Pad to a page boundary */
329 addr = roundup(addr, PAGE_SIZE);
331 #if defined(LOADER_FDT_SUPPORT)
332 /* Handle device tree blob */
334 dtb_size = fdt_copy(addr);
336 /* Pad to a page boundary */
338 addr += roundup(dtb_size, PAGE_SIZE);
342 kfp = file_findfile(NULL, "elf32 kernel");
344 kfp = file_findfile(NULL, "elf kernel");
346 panic("can't find kernel file");
347 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
348 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
350 #if defined(LOADER_FDT_SUPPORT)
352 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
354 pager_output("WARNING! Trying to fire up the kernel, but no "
355 "device tree blob found!\n");
358 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
360 /* Figure out the size and location of the metadata */
362 size = md_copymodules(0);
363 kernend = roundup(addr + size, PAGE_SIZE);
365 /* Provide MODINFOMD_KERNEND */
366 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
367 bcopy(&kernend, md->md_data, sizeof kernend);
369 /* Convert addresses to the final VA */
370 *modulep -= __elfN(relocation_offset);
372 /* Do relocation fixup on metadata of each module. */
373 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
374 for (i = 0; i < sizeof mdt / sizeof mdt[0]; i++) {
375 md = file_findmetadata(xp, mdt[i]);
377 bcopy(md->md_data, &vaddr, sizeof vaddr);
378 vaddr -= __elfN(relocation_offset);
379 bcopy(&vaddr, md->md_data, sizeof vaddr);
384 /* Only now copy actual modules and metadata */
385 (void)md_copymodules(addr);