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>
38 #include <machine/metadata.h>
40 #include "bootstrap.h"
43 md_getboothowto(char *kargs)
56 if (!active && (*cp == '-')) {
100 /* get equivalents from the environment */
101 for (i = 0; howto_names[i].ev != NULL; i++)
102 if (getenv(howto_names[i].ev) != NULL)
103 howto |= howto_names[i].mask;
104 if (!strcmp(getenv("console"), "comconsole"))
106 if (!strcmp(getenv("console"), "nullconsole"))
112 * Copy the environment into the load area starting at (addr).
113 * Each variable is formatted as <name>=<value>, with a single nul
114 * separating each variable, and a double nul terminating the environment.
117 md_copyenv(vm_offset_t addr)
121 /* traverse the environment */
122 for (ep = environ; ep != NULL; ep = ep->ev_next) {
123 archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
124 addr += strlen(ep->ev_name);
125 archsw.arch_copyin("=", addr, 1);
127 if (ep->ev_value != NULL) {
128 archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
129 addr += strlen(ep->ev_value);
131 archsw.arch_copyin("", addr, 1);
134 archsw.arch_copyin("", addr, 1);
140 * Copy module-related data into the load area, where it can be
141 * used as a directory for loaded modules.
143 * Module data is presented in a self-describing format. Each datum
144 * is preceded by a 32-bit identifier and a 32-bit size field.
146 * Currently, the following data are saved:
148 * MOD_NAME (variable) module name (string)
149 * MOD_TYPE (variable) module type (string)
150 * MOD_ARGS (variable) module parameters (string)
151 * MOD_ADDR sizeof(vm_offset_t) module load address
152 * MOD_SIZE sizeof(size_t) module size
153 * MOD_METADATA (variable) type-specific metadata
158 #define COPY32(v, a, c) { \
161 archsw.arch_copyin(&x, a, sizeof(x)); \
165 #define MOD_STR(t, a, s, c) { \
167 COPY32(strlen(s) + 1, a, c) \
169 archsw.arch_copyin(s, a, strlen(s) + 1);\
170 a += roundup(strlen(s) + 1, align); \
173 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
174 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
175 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
177 #define MOD_VAR(t, a, s, c) { \
179 COPY32(sizeof(s), a, c); \
181 archsw.arch_copyin(&s, a, sizeof(s)); \
182 a += roundup(sizeof(s), align); \
185 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
186 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
188 #define MOD_METADATA(a, mm, c) { \
189 COPY32(MODINFO_METADATA | mm->md_type, a, c);\
190 COPY32(mm->md_size, a, c); \
192 archsw.arch_copyin(mm->md_data, a, mm->md_size);\
193 a += roundup(mm->md_size, align); \
196 #define MOD_END(a, c) { \
197 COPY32(MODINFO_END, a, c); \
202 md_copymodules(vm_offset_t addr, int kern64)
204 struct preloaded_file *fp;
205 struct file_metadata *md;
210 /* start with the first module on the list, should be the kernel */
211 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
213 MOD_NAME(addr, fp->f_name, c); /* this field must come first */
214 MOD_TYPE(addr, fp->f_type, c);
216 MOD_ARGS(addr, fp->f_args, c);
218 scratch64 = fp->f_addr;
219 MOD_ADDR(addr, scratch64, c);
220 scratch64 = fp->f_size;
221 MOD_SIZE(addr, scratch64, c);
223 MOD_ADDR(addr, fp->f_addr, c);
224 MOD_SIZE(addr, fp->f_size, c);
226 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
227 if (!(md->md_type & MODINFOMD_NOCOPY)) {
228 MOD_METADATA(addr, md, c);
237 * Load the information expected by a powerpc kernel.
239 * - The 'boothowto' argument is constructed
240 * - The 'bootdev' argument is constructed
241 * - The kernel environment is copied into kernel space.
242 * - Module metadata are formatted and placed in kernel space.
245 md_load_dual(char *args, vm_offset_t *modulep, int kern64)
247 struct preloaded_file *kfp;
248 struct preloaded_file *xp;
249 struct file_metadata *md;
258 align = kern64 ? 8 : 4;
259 howto = md_getboothowto(args);
262 * Allow the environment variable 'rootdev' to override the supplied device
263 * This should perhaps go to MI code and/or have $rootdev tested/set by
264 * MI code before launching the kernel.
266 rootdevname = getenv("rootdev");
267 if (rootdevname == NULL)
268 rootdevname = getenv("currdev");
269 /* Try reading the /etc/fstab file to select the root device */
270 getrootmount(rootdevname);
272 /* find the last module in the chain */
274 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
275 if (addr < (xp->f_addr + xp->f_size))
276 addr = xp->f_addr + xp->f_size;
278 /* pad to a page boundary */
279 addr = roundup(addr, PAGE_SIZE);
281 /* copy our environment */
283 addr = md_copyenv(addr);
285 /* pad to a page boundary */
286 addr = roundup(addr, PAGE_SIZE);
289 kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
291 kfp = file_findfile(NULL, "elf kernel");
293 panic("can't find kernel file");
294 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
297 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
299 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof scratch64, &scratch64);
301 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
302 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
306 size = md_copymodules(0, kern64);
307 kernend = roundup(addr + size, PAGE_SIZE);
309 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
312 bcopy(&scratch64, md->md_data, sizeof scratch64);
314 bcopy(&kernend, md->md_data, sizeof kernend);
317 (void)md_copymodules(addr, kern64);
323 md_load(char *args, vm_offset_t *modulep)
325 return (md_load_dual(args, modulep, 0));
329 md_load64(char *args, vm_offset_t *modulep)
331 return (md_load_dual(args, modulep, 1));