1 /*-
2 * Copyright (c) 1998 Michael Smith <[email protected]>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * from: FreeBSD: src/sys/boot/sparc64/loader/metadata.c,v 1.6
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <stand.h>
33 #include <sys/param.h>
34 #include <sys/linker.h>
35 #include <sys/boot.h>
36 #include <sys/reboot.h>
37 #if defined(LOADER_FDT_SUPPORT)
38 #include <fdt_platform.h>
39 #endif
40
41 #ifdef __arm__
42 #include <machine/elf.h>
43 #endif
44 #include <machine/metadata.h>
45
46 #include "bootstrap.h"
47
48 #ifdef LOADER_GELI_SUPPORT
49 #include "geliboot.h"
50 #endif
51
52 static int
md_getboothowto(char * kargs)53 md_getboothowto(char *kargs)
54 {
55 int howto;
56
57 /* Parse kargs */
58 howto = boot_parse_cmdline(kargs);
59 howto |= boot_env_to_howto();
60 if (!strcmp(getenv("console"), "comconsole"))
61 howto |= RB_SERIAL;
62 if (!strcmp(getenv("console"), "nullconsole"))
63 howto |= RB_MUTE;
64 return(howto);
65 }
66
67 /*
68 * Copy the environment into the load area starting at (addr).
69 * Each variable is formatted as <name>=<value>, with a single nul
70 * separating each variable, and a double nul terminating the environment.
71 */
72 static vm_offset_t
md_copyenv(vm_offset_t addr)73 md_copyenv(vm_offset_t addr)
74 {
75 struct env_var *ep;
76
77 /* traverse the environment */
78 for (ep = environ; ep != NULL; ep = ep->ev_next) {
79 archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
80 addr += strlen(ep->ev_name);
81 archsw.arch_copyin("=", addr, 1);
82 addr++;
83 if (ep->ev_value != NULL) {
84 archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
85 addr += strlen(ep->ev_value);
86 }
87 archsw.arch_copyin("", addr, 1);
88 addr++;
89 }
90 archsw.arch_copyin("", addr, 1);
91 addr++;
92 return(addr);
93 }
94
95 /*
96 * Copy module-related data into the load area, where it can be
97 * used as a directory for loaded modules.
98 *
99 * Module data is presented in a self-describing format. Each datum
100 * is preceded by a 32-bit identifier and a 32-bit size field.
101 *
102 * Currently, the following data are saved:
103 *
104 * MOD_NAME (variable) module name (string)
105 * MOD_TYPE (variable) module type (string)
106 * MOD_ARGS (variable) module parameters (string)
107 * MOD_ADDR sizeof(vm_offset_t) module load address
108 * MOD_SIZE sizeof(size_t) module size
109 * MOD_METADATA (variable) type-specific metadata
110 */
111
112 static int align;
113
114 #define COPY32(v, a, c) { \
115 uint32_t x = (v); \
116 if (c) \
117 archsw.arch_copyin(&x, a, sizeof(x)); \
118 a += sizeof(x); \
119 }
120
121 #define MOD_STR(t, a, s, c) { \
122 COPY32(t, a, c); \
123 COPY32(strlen(s) + 1, a, c) \
124 if (c) \
125 archsw.arch_copyin(s, a, strlen(s) + 1);\
126 a += roundup(strlen(s) + 1, align); \
127 }
128
129 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
130 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
131 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
132
133 #define MOD_VAR(t, a, s, c) { \
134 COPY32(t, a, c); \
135 COPY32(sizeof(s), a, c); \
136 if (c) \
137 archsw.arch_copyin(&s, a, sizeof(s)); \
138 a += roundup(sizeof(s), align); \
139 }
140
141 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
142 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
143
144 #define MOD_METADATA(a, mm, c) { \
145 COPY32(MODINFO_METADATA | mm->md_type, a, c);\
146 COPY32(mm->md_size, a, c); \
147 if (c) \
148 archsw.arch_copyin(mm->md_data, a, mm->md_size);\
149 a += roundup(mm->md_size, align); \
150 }
151
152 #define MOD_END(a, c) { \
153 COPY32(MODINFO_END, a, c); \
154 COPY32(0, a, c); \
155 }
156
157 static vm_offset_t
md_copymodules(vm_offset_t addr,int kern64)158 md_copymodules(vm_offset_t addr, int kern64)
159 {
160 struct preloaded_file *fp;
161 struct file_metadata *md;
162 uint64_t scratch64;
163 uint32_t scratch32;
164 int c;
165
166 c = addr != 0;
167 /* start with the first module on the list, should be the kernel */
168 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
169
170 MOD_NAME(addr, fp->f_name, c); /* this field must come first */
171 MOD_TYPE(addr, fp->f_type, c);
172 if (fp->f_args)
173 MOD_ARGS(addr, fp->f_args, c);
174 if (kern64) {
175 scratch64 = fp->f_addr;
176 MOD_ADDR(addr, scratch64, c);
177 scratch64 = fp->f_size;
178 MOD_SIZE(addr, scratch64, c);
179 } else {
180 scratch32 = fp->f_addr;
181 #ifdef __arm__
182 scratch32 -= __elfN(relocation_offset);
183 #endif
184 MOD_ADDR(addr, scratch32, c);
185 MOD_SIZE(addr, fp->f_size, c);
186 }
187 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
188 if (!(md->md_type & MODINFOMD_NOCOPY)) {
189 MOD_METADATA(addr, md, c);
190 }
191 }
192 }
193 MOD_END(addr, c);
194 return(addr);
195 }
196
197 /*
198 * Load the information expected by a kernel.
199 *
200 * - The 'boothowto' argument is constructed
201 * - The 'bootdev' argument is constructed
202 * - The kernel environment is copied into kernel space.
203 * - Module metadata are formatted and placed in kernel space.
204 */
205 static int
md_load_dual(char * args,vm_offset_t * modulep,vm_offset_t * dtb,int kern64)206 md_load_dual(char *args, vm_offset_t *modulep, vm_offset_t *dtb, int kern64)
207 {
208 struct preloaded_file *kfp;
209 struct preloaded_file *xp;
210 struct file_metadata *md;
211 vm_offset_t kernend;
212 vm_offset_t addr;
213 vm_offset_t envp;
214 #if defined(LOADER_FDT_SUPPORT)
215 vm_offset_t fdtp;
216 #endif
217 vm_offset_t size;
218 uint64_t scratch64;
219 char *rootdevname;
220 int howto;
221 #ifdef __arm__
222 vm_offset_t vaddr;
223 int i;
224
225 /*
226 * These metadata addreses must be converted for kernel after
227 * relocation.
228 */
229 uint32_t mdt[] = {
230 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
231 MODINFOMD_ENVP,
232 #if defined(LOADER_FDT_SUPPORT)
233 MODINFOMD_DTBP
234 #endif
235 };
236 #endif
237
238 align = kern64 ? 8 : 4;
239 howto = md_getboothowto(args);
240
241 /*
242 * Allow the environment variable 'rootdev' to override the supplied
243 * device. This should perhaps go to MI code and/or have $rootdev
244 * tested/set by MI code before launching the kernel.
245 */
246 rootdevname = getenv("rootdev");
247 if (rootdevname == NULL)
248 rootdevname = getenv("currdev");
249 /* Try reading the /etc/fstab file to select the root device */
250 getrootmount(rootdevname);
251
252 /* Find the last module in the chain */
253 addr = 0;
254 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
255 if (addr < (xp->f_addr + xp->f_size))
256 addr = xp->f_addr + xp->f_size;
257 }
258 /* Pad to a page boundary */
259 addr = roundup(addr, PAGE_SIZE);
260
261 /* Copy our environment */
262 envp = addr;
263 addr = md_copyenv(addr);
264
265 /* Pad to a page boundary */
266 addr = roundup(addr, PAGE_SIZE);
267
268 #if defined(LOADER_FDT_SUPPORT)
269 /* Copy out FDT */
270 fdtp = 0;
271 #if defined(__powerpc__)
272 if (getenv("usefdt") != NULL)
273 #endif
274 {
275 size = fdt_copy(addr);
276 fdtp = addr;
277 addr = roundup(addr + size, PAGE_SIZE);
278 }
279 #endif
280
281 kernend = 0;
282 kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
283 if (kfp == NULL)
284 kfp = file_findfile(NULL, "elf kernel");
285 if (kfp == NULL)
286 panic("can't find kernel file");
287 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
288 if (kern64) {
289 scratch64 = envp;
290 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
291 #if defined(LOADER_FDT_SUPPORT)
292 if (fdtp != 0) {
293 scratch64 = fdtp;
294 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof scratch64, &scratch64);
295 }
296 #endif
297 scratch64 = kernend;
298 file_addmetadata(kfp, MODINFOMD_KERNEND,
299 sizeof scratch64, &scratch64);
300 } else {
301 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
302 #if defined(LOADER_FDT_SUPPORT)
303 if (fdtp != 0)
304 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof fdtp, &fdtp);
305 #endif
306 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
307 }
308 #ifdef LOADER_GELI_SUPPORT
309 geli_export_key_metadata(kfp);
310 #endif
311
312 *modulep = addr;
313 size = md_copymodules(0, kern64);
314 kernend = roundup(addr + size, PAGE_SIZE);
315
316 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
317 if (kern64) {
318 scratch64 = kernend;
319 bcopy(&scratch64, md->md_data, sizeof scratch64);
320 } else {
321 bcopy(&kernend, md->md_data, sizeof kernend);
322 }
323
324 #ifdef __arm__
325 /* Convert addresses to the final VA */
326 *modulep -= __elfN(relocation_offset);
327
328 /* Do relocation fixup on metadata of each module. */
329 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
330 for (i = 0; i < nitems(mdt); i++) {
331 md = file_findmetadata(xp, mdt[i]);
332 if (md) {
333 bcopy(md->md_data, &vaddr, sizeof vaddr);
334 vaddr -= __elfN(relocation_offset);
335 bcopy(&vaddr, md->md_data, sizeof vaddr);
336 }
337 }
338 }
339 #endif
340
341 (void)md_copymodules(addr, kern64);
342 #if defined(LOADER_FDT_SUPPORT)
343 if (dtb != NULL)
344 *dtb = fdtp;
345 #endif
346
347 return(0);
348 }
349
350 int
md_load(char * args,vm_offset_t * modulep,vm_offset_t * dtb)351 md_load(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
352 {
353 return (md_load_dual(args, modulep, dtb, 0));
354 }
355
356 #if defined(__mips__) || defined(__powerpc__)
357 int
md_load64(char * args,vm_offset_t * modulep,vm_offset_t * dtb)358 md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
359 {
360 return (md_load_dual(args, modulep, dtb, 1));
361 }
362 #endif
363