xref: /freebsd/stand/common/metadata.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
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     u_int32_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     int				c;
269 
270     c = addr != 0;
271     /* start with the first module on the list, should be the kernel */
272     for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
273 
274 	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
275 	MOD_TYPE(addr, fp->f_type, c);
276 	if (fp->f_args)
277 	    MOD_ARGS(addr, fp->f_args, c);
278 	if (kern64) {
279 		scratch64 = fp->f_addr;
280 		MOD_ADDR(addr, scratch64, c);
281 		scratch64 = fp->f_size;
282 		MOD_SIZE(addr, scratch64, c);
283 	} else {
284 		MOD_ADDR(addr, fp->f_addr, c);
285 		MOD_SIZE(addr, fp->f_size, c);
286 	}
287 	for (md = fp->f_metadata; md != NULL; md = md->md_next) {
288 	    if (!(md->md_type & MODINFOMD_NOCOPY)) {
289 		MOD_METADATA(addr, md, c);
290 	    }
291 	}
292     }
293     MOD_END(addr, c);
294     return(addr);
295 }
296 
297 /*
298  * Load the information expected by a kernel.
299  *
300  * - The 'boothowto' argument is constructed
301  * - The 'bootdev' argument is constructed
302  * - The kernel environment is copied into kernel space.
303  * - Module metadata are formatted and placed in kernel space.
304  */
305 int
306 md_load_dual(char *args, vm_offset_t *modulep, vm_offset_t *dtb, int kern64)
307 {
308     struct preloaded_file	*kfp;
309     struct preloaded_file	*xp;
310     struct file_metadata	*md;
311     vm_offset_t			kernend;
312     vm_offset_t			addr;
313     vm_offset_t			envp;
314 #if defined(LOADER_FDT_SUPPORT)
315     vm_offset_t			fdtp;
316 #endif
317     vm_offset_t			size;
318     uint64_t			scratch64;
319     char			*rootdevname;
320     int				howto;
321 #ifdef __arm__
322     vm_offset_t			vaddr;
323     int				i;
324 
325 	/*
326 	 * These metadata addreses must be converted for kernel after
327 	 * relocation.
328 	 */
329     uint32_t			mdt[] = {
330 	    MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
331 	    MODINFOMD_ENVP,
332 #if defined(LOADER_FDT_SUPPORT)
333 	    MODINFOMD_DTBP
334 #endif
335     };
336 #endif
337 
338     align = kern64 ? 8 : 4;
339     howto = md_getboothowto(args);
340 
341     /*
342      * Allow the environment variable 'rootdev' to override the supplied
343      * device. This should perhaps go to MI code and/or have $rootdev
344      * tested/set by MI code before launching the kernel.
345      */
346     rootdevname = getenv("rootdev");
347     if (rootdevname == NULL)
348 	rootdevname = getenv("currdev");
349     /* Try reading the /etc/fstab file to select the root device */
350     getrootmount(rootdevname);
351 
352     /* Find the last module in the chain */
353     addr = 0;
354     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
355 	if (addr < (xp->f_addr + xp->f_size))
356 	    addr = xp->f_addr + xp->f_size;
357     }
358     /* Pad to a page boundary */
359     addr = roundup(addr, PAGE_SIZE);
360 
361     /* Copy our environment */
362     envp = addr;
363     addr = md_copyenv(addr);
364 
365     /* Pad to a page boundary */
366     addr = roundup(addr, PAGE_SIZE);
367 
368 #if defined(LOADER_FDT_SUPPORT)
369     /* Copy out FDT */
370     fdtp = 0;
371 #if defined(__powerpc__)
372     if (getenv("usefdt") != NULL)
373 #endif
374     {
375 	size = fdt_copy(addr);
376 	fdtp = addr;
377 	addr = roundup(addr + size, PAGE_SIZE);
378     }
379 #endif
380 
381     kernend = 0;
382     kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
383     if (kfp == NULL)
384 	kfp = file_findfile(NULL, "elf kernel");
385     if (kfp == NULL)
386 	panic("can't find kernel file");
387     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
388     if (kern64) {
389 	scratch64 = envp;
390 	file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
391 #if defined(LOADER_FDT_SUPPORT)
392 	if (fdtp != 0) {
393 	    scratch64 = fdtp;
394 	    file_addmetadata(kfp, MODINFOMD_DTBP, sizeof scratch64, &scratch64);
395 	}
396 #endif
397 	scratch64 = kernend;
398 	file_addmetadata(kfp, MODINFOMD_KERNEND,
399 		sizeof scratch64, &scratch64);
400     } else {
401 	file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
402 #if defined(LOADER_FDT_SUPPORT)
403 	if (fdtp != 0)
404 	    file_addmetadata(kfp, MODINFOMD_DTBP, sizeof fdtp, &fdtp);
405 #endif
406 	file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
407     }
408 
409 #if defined(__sparc64__)
410     file_addmetadata(kfp, MODINFOMD_DTLB_SLOTS,
411 	sizeof dtlb_slot, &dtlb_slot);
412     file_addmetadata(kfp, MODINFOMD_ITLB_SLOTS,
413 	sizeof itlb_slot, &itlb_slot);
414     file_addmetadata(kfp, MODINFOMD_DTLB,
415 	dtlb_slot * sizeof(*dtlb_store), dtlb_store);
416     file_addmetadata(kfp, MODINFOMD_ITLB,
417 	itlb_slot * sizeof(*itlb_store), itlb_store);
418 #endif
419 
420     *modulep = addr;
421     size = md_copymodules(0, kern64);
422     kernend = roundup(addr + size, PAGE_SIZE);
423 
424     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
425     if (kern64) {
426 	scratch64 = kernend;
427 	bcopy(&scratch64, md->md_data, sizeof scratch64);
428     } else {
429 	bcopy(&kernend, md->md_data, sizeof kernend);
430     }
431 
432 #ifdef __arm__
433     /* Convert addresses to the final VA */
434     *modulep -= __elfN(relocation_offset);
435 
436     /* Do relocation fixup on metadata of each module. */
437     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
438         for (i = 0; i < nitems(mdt); i++) {
439             md = file_findmetadata(xp, mdt[i]);
440                 if (md) {
441                     bcopy(md->md_data, &vaddr, sizeof vaddr);
442                     vaddr -= __elfN(relocation_offset);
443                     bcopy(&vaddr, md->md_data, sizeof vaddr);
444                 }
445             }
446     }
447 #endif
448 
449     (void)md_copymodules(addr, kern64);
450 #if defined(LOADER_FDT_SUPPORT)
451     if (dtb != NULL)
452 	*dtb = fdtp;
453 #endif
454 
455     return(0);
456 }
457 
458 int
459 md_load(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
460 {
461     return (md_load_dual(args, modulep, dtb, 0));
462 }
463 
464 #if defined(__mips__) || defined(__powerpc__)
465 int
466 md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
467 {
468     return (md_load_dual(args, modulep, dtb, 1));
469 }
470 #endif
471