xref: /freebsd/stand/common/metadata.c (revision 74ca7bf1d4c7173d5575ba168bc4b5f6d181ff5a)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
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/reboot.h>
35 #include <sys/linker.h>
36 #include <sys/boot.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 #if defined(__sparc64__)
49 #include <openfirm.h>
50 
51 extern struct tlb_entry *dtlb_store;
52 extern struct tlb_entry *itlb_store;
53 
54 extern int dtlb_slot;
55 extern int itlb_slot;
56 
57 static int
58 md_bootserial(void)
59 {
60     char        buf[64];
61     ihandle_t        inst;
62     phandle_t        input;
63     phandle_t        node;
64     phandle_t        output;
65 
66     if ((node = OF_finddevice("/options")) == -1)
67         return(-1);
68     if (OF_getprop(node, "input-device", buf, sizeof(buf)) == -1)
69         return(-1);
70     input = OF_finddevice(buf);
71     if (OF_getprop(node, "output-device", buf, sizeof(buf)) == -1)
72         return(-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)
77             return(-1);
78         if (OF_getprop(node, "stdin", &inst, sizeof(inst)) == -1)
79             return(-1);
80         if ((input = OF_instance_to_package(inst)) == -1)
81             return(-1);
82         if (OF_getprop(node, "stdout", &inst, sizeof(inst)) == -1)
83             return(-1);
84         if ((output = OF_instance_to_package(inst)) == -1)
85             return(-1);
86     }
87     if (input != output)
88         return(-1);
89     if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
90         return(-1);
91     if (strcmp(buf, "serial") != 0)
92         return(-1);
93     return(0);
94 }
95 #endif
96 
97 int
98 md_getboothowto(char *kargs)
99 {
100     char	*cp;
101     int		howto;
102     int		active;
103     int		i;
104 
105     /* Parse kargs */
106     howto = 0;
107     if (kargs != NULL) {
108 	cp = kargs;
109 	active = 0;
110 	while (*cp != 0) {
111 	    if (!active && (*cp == '-')) {
112 		active = 1;
113 	    } else if (active)
114 		switch (*cp) {
115 		case 'a':
116 		    howto |= RB_ASKNAME;
117 		    break;
118 		case 'C':
119 		    howto |= RB_CDROM;
120 		    break;
121 		case 'd':
122 		    howto |= RB_KDB;
123 		    break;
124 		case 'D':
125 		    howto |= RB_MULTIPLE;
126 		    break;
127 		case 'm':
128 		    howto |= RB_MUTE;
129 		    break;
130 		case 'g':
131 		    howto |= RB_GDB;
132 		    break;
133 		case 'h':
134 		    howto |= RB_SERIAL;
135 		    break;
136 		case 'p':
137 		    howto |= RB_PAUSE;
138 		    break;
139 		case 'r':
140 		    howto |= RB_DFLTROOT;
141 		    break;
142 		case 's':
143 		    howto |= RB_SINGLE;
144 		    break;
145 		case 'v':
146 		    howto |= RB_VERBOSE;
147 		    break;
148 		default:
149 		    active = 0;
150 		    break;
151 		}
152 	    cp++;
153 	}
154     }
155 
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)
162 	howto |= RB_SERIAL;
163 #else
164     if (!strcmp(getenv("console"), "comconsole"))
165 	howto |= RB_SERIAL;
166     if (!strcmp(getenv("console"), "nullconsole"))
167 	howto |= RB_MUTE;
168 #endif
169     return(howto);
170 }
171 
172 /*
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.
176  */
177 static vm_offset_t
178 md_copyenv(vm_offset_t addr)
179 {
180     struct env_var	*ep;
181 
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);
187 	addr++;
188 	if (ep->ev_value != NULL) {
189 	    archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
190 	    addr += strlen(ep->ev_value);
191 	}
192 	archsw.arch_copyin("", addr, 1);
193 	addr++;
194     }
195     archsw.arch_copyin("", addr, 1);
196     addr++;
197     return(addr);
198 }
199 
200 /*
201  * Copy module-related data into the load area, where it can be
202  * used as a directory for loaded modules.
203  *
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.
206  *
207  * Currently, the following data are saved:
208  *
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
215  */
216 
217 static int align;
218 
219 #define COPY32(v, a, c) {			\
220     uint32_t	x = (v);			\
221     if (c)					\
222         archsw.arch_copyin(&x, a, sizeof(x));	\
223     a += sizeof(x);				\
224 }
225 
226 #define MOD_STR(t, a, s, c) {			\
227     COPY32(t, a, c);				\
228     COPY32(strlen(s) + 1, a, c)			\
229     if (c)					\
230         archsw.arch_copyin(s, a, strlen(s) + 1);\
231     a += roundup(strlen(s) + 1, align);		\
232 }
233 
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)
237 
238 #define MOD_VAR(t, a, s, c) {			\
239     COPY32(t, a, c);				\
240     COPY32(sizeof(s), a, c);			\
241     if (c)					\
242         archsw.arch_copyin(&s, a, sizeof(s));	\
243     a += roundup(sizeof(s), align);		\
244 }
245 
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)
248 
249 #define MOD_METADATA(a, mm, c) {		\
250     COPY32(MODINFO_METADATA | mm->md_type, a, c);\
251     COPY32(mm->md_size, a, c);			\
252     if (c)					\
253         archsw.arch_copyin(mm->md_data, a, mm->md_size);\
254     a += roundup(mm->md_size, align);		\
255 }
256 
257 #define MOD_END(a, c) {				\
258     COPY32(MODINFO_END, a, c);			\
259     COPY32(0, a, c);				\
260 }
261 
262 static vm_offset_t
263 md_copymodules(vm_offset_t addr, int kern64)
264 {
265     struct preloaded_file	*fp;
266     struct file_metadata	*md;
267     uint64_t			scratch64;
268     uint32_t			scratch32;
269     int				c;
270 
271     c = addr != 0;
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) {
274 
275 	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
276 	MOD_TYPE(addr, fp->f_type, c);
277 	if (fp->f_args)
278 	    MOD_ARGS(addr, fp->f_args, c);
279 	if (kern64) {
280 		scratch64 = fp->f_addr;
281 		MOD_ADDR(addr, scratch64, c);
282 		scratch64 = fp->f_size;
283 		MOD_SIZE(addr, scratch64, c);
284 	} else {
285 		scratch32 = fp->f_addr;
286 #ifdef __arm__
287 		scratch32 -= __elfN(relocation_offset);
288 #endif
289 		MOD_ADDR(addr, scratch32, c);
290 		MOD_SIZE(addr, fp->f_size, c);
291 	}
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);
295 	    }
296 	}
297     }
298     MOD_END(addr, c);
299     return(addr);
300 }
301 
302 /*
303  * Load the information expected by a kernel.
304  *
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.
309  */
310 int
311 md_load_dual(char *args, vm_offset_t *modulep, vm_offset_t *dtb, int kern64)
312 {
313     struct preloaded_file	*kfp;
314     struct preloaded_file	*xp;
315     struct file_metadata	*md;
316     vm_offset_t			kernend;
317     vm_offset_t			addr;
318     vm_offset_t			envp;
319 #if defined(LOADER_FDT_SUPPORT)
320     vm_offset_t			fdtp;
321 #endif
322     vm_offset_t			size;
323     uint64_t			scratch64;
324     char			*rootdevname;
325     int				howto;
326 #ifdef __arm__
327     vm_offset_t			vaddr;
328     int				i;
329 
330 	/*
331 	 * These metadata addreses must be converted for kernel after
332 	 * relocation.
333 	 */
334     uint32_t			mdt[] = {
335 	    MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
336 	    MODINFOMD_ENVP,
337 #if defined(LOADER_FDT_SUPPORT)
338 	    MODINFOMD_DTBP
339 #endif
340     };
341 #endif
342 
343     align = kern64 ? 8 : 4;
344     howto = md_getboothowto(args);
345 
346     /*
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.
350      */
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);
356 
357     /* Find the last module in the chain */
358     addr = 0;
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;
362     }
363     /* Pad to a page boundary */
364     addr = roundup(addr, PAGE_SIZE);
365 
366     /* Copy our environment */
367     envp = addr;
368     addr = md_copyenv(addr);
369 
370     /* Pad to a page boundary */
371     addr = roundup(addr, PAGE_SIZE);
372 
373 #if defined(LOADER_FDT_SUPPORT)
374     /* Copy out FDT */
375     fdtp = 0;
376 #if defined(__powerpc__)
377     if (getenv("usefdt") != NULL)
378 #endif
379     {
380 	size = fdt_copy(addr);
381 	fdtp = addr;
382 	addr = roundup(addr + size, PAGE_SIZE);
383     }
384 #endif
385 
386     kernend = 0;
387     kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
388     if (kfp == NULL)
389 	kfp = file_findfile(NULL, "elf kernel");
390     if (kfp == NULL)
391 	panic("can't find kernel file");
392     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
393     if (kern64) {
394 	scratch64 = envp;
395 	file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
396 #if defined(LOADER_FDT_SUPPORT)
397 	if (fdtp != 0) {
398 	    scratch64 = fdtp;
399 	    file_addmetadata(kfp, MODINFOMD_DTBP, sizeof scratch64, &scratch64);
400 	}
401 #endif
402 	scratch64 = kernend;
403 	file_addmetadata(kfp, MODINFOMD_KERNEND,
404 		sizeof scratch64, &scratch64);
405     } else {
406 	file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
407 #if defined(LOADER_FDT_SUPPORT)
408 	if (fdtp != 0)
409 	    file_addmetadata(kfp, MODINFOMD_DTBP, sizeof fdtp, &fdtp);
410 #endif
411 	file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
412     }
413 
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);
423 #endif
424 
425     *modulep = addr;
426     size = md_copymodules(0, kern64);
427     kernend = roundup(addr + size, PAGE_SIZE);
428 
429     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
430     if (kern64) {
431 	scratch64 = kernend;
432 	bcopy(&scratch64, md->md_data, sizeof scratch64);
433     } else {
434 	bcopy(&kernend, md->md_data, sizeof kernend);
435     }
436 
437 #ifdef __arm__
438     /* Convert addresses to the final VA */
439     *modulep -= __elfN(relocation_offset);
440 
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]);
445                 if (md) {
446                     bcopy(md->md_data, &vaddr, sizeof vaddr);
447                     vaddr -= __elfN(relocation_offset);
448                     bcopy(&vaddr, md->md_data, sizeof vaddr);
449                 }
450             }
451     }
452 #endif
453 
454     (void)md_copymodules(addr, kern64);
455 #if defined(LOADER_FDT_SUPPORT)
456     if (dtb != NULL)
457 	*dtb = fdtp;
458 #endif
459 
460     return(0);
461 }
462 
463 int
464 md_load(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
465 {
466     return (md_load_dual(args, modulep, dtb, 0));
467 }
468 
469 #if defined(__mips__) || defined(__powerpc__)
470 int
471 md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
472 {
473     return (md_load_dual(args, modulep, dtb, 1));
474 }
475 #endif
476