xref: /freebsd/stand/common/metadata.c (revision 780fb4a2)
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/linker.h>
35 #include <sys/reboot.h>
36 #if defined(LOADER_FDT_SUPPORT)
37 #include <fdt_platform.h>
38 #endif
39 
40 #ifdef __arm__
41 #include <machine/elf.h>
42 #endif
43 #include <machine/metadata.h>
44 
45 #include "bootstrap.h"
46 
47 #if defined(__sparc64__)
48 #include <openfirm.h>
49 
50 extern struct tlb_entry *dtlb_store;
51 extern struct tlb_entry *itlb_store;
52 
53 extern int dtlb_slot;
54 extern int itlb_slot;
55 
56 static int
57 md_bootserial(void)
58 {
59     char        buf[64];
60     ihandle_t        inst;
61     phandle_t        input;
62     phandle_t        node;
63     phandle_t        output;
64 
65     if ((node = OF_finddevice("/options")) == -1)
66         return(-1);
67     if (OF_getprop(node, "input-device", buf, sizeof(buf)) == -1)
68         return(-1);
69     input = OF_finddevice(buf);
70     if (OF_getprop(node, "output-device", buf, sizeof(buf)) == -1)
71         return(-1);
72     output = OF_finddevice(buf);
73     if (input == -1 || output == -1 ||
74         OF_getproplen(input, "keyboard") >= 0) {
75         if ((node = OF_finddevice("/chosen")) == -1)
76             return(-1);
77         if (OF_getprop(node, "stdin", &inst, sizeof(inst)) == -1)
78             return(-1);
79         if ((input = OF_instance_to_package(inst)) == -1)
80             return(-1);
81         if (OF_getprop(node, "stdout", &inst, sizeof(inst)) == -1)
82             return(-1);
83         if ((output = OF_instance_to_package(inst)) == -1)
84             return(-1);
85     }
86     if (input != output)
87         return(-1);
88     if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
89         return(-1);
90     if (strcmp(buf, "serial") != 0)
91         return(-1);
92     return(0);
93 }
94 #endif
95 
96 static int
97 md_getboothowto(char *kargs)
98 {
99     char	*cp;
100     int		howto;
101     int		active;
102 
103     /* Parse kargs */
104     howto = 0;
105     if (kargs != NULL) {
106 	cp = kargs;
107 	active = 0;
108 	while (*cp != 0) {
109 	    if (!active && (*cp == '-')) {
110 		active = 1;
111 	    } else if (active)
112 		switch (*cp) {
113 		case 'a':
114 		    howto |= RB_ASKNAME;
115 		    break;
116 		case 'C':
117 		    howto |= RB_CDROM;
118 		    break;
119 		case 'd':
120 		    howto |= RB_KDB;
121 		    break;
122 		case 'D':
123 		    howto |= RB_MULTIPLE;
124 		    break;
125 		case 'm':
126 		    howto |= RB_MUTE;
127 		    break;
128 		case 'g':
129 		    howto |= RB_GDB;
130 		    break;
131 		case 'h':
132 		    howto |= RB_SERIAL;
133 		    break;
134 		case 'p':
135 		    howto |= RB_PAUSE;
136 		    break;
137 		case 'r':
138 		    howto |= RB_DFLTROOT;
139 		    break;
140 		case 's':
141 		    howto |= RB_SINGLE;
142 		    break;
143 		case 'v':
144 		    howto |= RB_VERBOSE;
145 		    break;
146 		default:
147 		    active = 0;
148 		    break;
149 		}
150 	    cp++;
151 	}
152     }
153 
154     howto |= bootenv_flags();
155 #if defined(__sparc64__)
156     if (md_bootserial() != -1)
157 	howto |= RB_SERIAL;
158 #else
159     if (!strcmp(getenv("console"), "comconsole"))
160 	howto |= RB_SERIAL;
161     if (!strcmp(getenv("console"), "nullconsole"))
162 	howto |= RB_MUTE;
163 #endif
164     return(howto);
165 }
166 
167 /*
168  * Copy the environment into the load area starting at (addr).
169  * Each variable is formatted as <name>=<value>, with a single nul
170  * separating each variable, and a double nul terminating the environment.
171  */
172 static vm_offset_t
173 md_copyenv(vm_offset_t addr)
174 {
175     struct env_var	*ep;
176 
177     /* traverse the environment */
178     for (ep = environ; ep != NULL; ep = ep->ev_next) {
179 	archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
180 	addr += strlen(ep->ev_name);
181 	archsw.arch_copyin("=", addr, 1);
182 	addr++;
183 	if (ep->ev_value != NULL) {
184 	    archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
185 	    addr += strlen(ep->ev_value);
186 	}
187 	archsw.arch_copyin("", addr, 1);
188 	addr++;
189     }
190     archsw.arch_copyin("", addr, 1);
191     addr++;
192     return(addr);
193 }
194 
195 /*
196  * Copy module-related data into the load area, where it can be
197  * used as a directory for loaded modules.
198  *
199  * Module data is presented in a self-describing format.  Each datum
200  * is preceded by a 32-bit identifier and a 32-bit size field.
201  *
202  * Currently, the following data are saved:
203  *
204  * MOD_NAME	(variable)		module name (string)
205  * MOD_TYPE	(variable)		module type (string)
206  * MOD_ARGS	(variable)		module parameters (string)
207  * MOD_ADDR	sizeof(vm_offset_t)	module load address
208  * MOD_SIZE	sizeof(size_t)		module size
209  * MOD_METADATA	(variable)		type-specific metadata
210  */
211 
212 static int align;
213 
214 #define COPY32(v, a, c) {			\
215     uint32_t	x = (v);			\
216     if (c)					\
217         archsw.arch_copyin(&x, a, sizeof(x));	\
218     a += sizeof(x);				\
219 }
220 
221 #define MOD_STR(t, a, s, c) {			\
222     COPY32(t, a, c);				\
223     COPY32(strlen(s) + 1, a, c)			\
224     if (c)					\
225         archsw.arch_copyin(s, a, strlen(s) + 1);\
226     a += roundup(strlen(s) + 1, align);		\
227 }
228 
229 #define MOD_NAME(a, s, c)	MOD_STR(MODINFO_NAME, a, s, c)
230 #define MOD_TYPE(a, s, c)	MOD_STR(MODINFO_TYPE, a, s, c)
231 #define MOD_ARGS(a, s, c)	MOD_STR(MODINFO_ARGS, a, s, c)
232 
233 #define MOD_VAR(t, a, s, c) {			\
234     COPY32(t, a, c);				\
235     COPY32(sizeof(s), a, c);			\
236     if (c)					\
237         archsw.arch_copyin(&s, a, sizeof(s));	\
238     a += roundup(sizeof(s), align);		\
239 }
240 
241 #define MOD_ADDR(a, s, c)	MOD_VAR(MODINFO_ADDR, a, s, c)
242 #define MOD_SIZE(a, s, c)	MOD_VAR(MODINFO_SIZE, a, s, c)
243 
244 #define MOD_METADATA(a, mm, c) {		\
245     COPY32(MODINFO_METADATA | mm->md_type, a, c);\
246     COPY32(mm->md_size, a, c);			\
247     if (c)					\
248         archsw.arch_copyin(mm->md_data, a, mm->md_size);\
249     a += roundup(mm->md_size, align);		\
250 }
251 
252 #define MOD_END(a, c) {				\
253     COPY32(MODINFO_END, a, c);			\
254     COPY32(0, a, c);				\
255 }
256 
257 static vm_offset_t
258 md_copymodules(vm_offset_t addr, int kern64)
259 {
260     struct preloaded_file	*fp;
261     struct file_metadata	*md;
262     uint64_t			scratch64;
263     uint32_t			scratch32;
264     int				c;
265 
266     c = addr != 0;
267     /* start with the first module on the list, should be the kernel */
268     for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
269 
270 	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
271 	MOD_TYPE(addr, fp->f_type, c);
272 	if (fp->f_args)
273 	    MOD_ARGS(addr, fp->f_args, c);
274 	if (kern64) {
275 		scratch64 = fp->f_addr;
276 		MOD_ADDR(addr, scratch64, c);
277 		scratch64 = fp->f_size;
278 		MOD_SIZE(addr, scratch64, c);
279 	} else {
280 		scratch32 = fp->f_addr;
281 #ifdef __arm__
282 		scratch32 -= __elfN(relocation_offset);
283 #endif
284 		MOD_ADDR(addr, scratch32, 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 static 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 #if !defined(__sparc64__)
459 int
460 md_load(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
461 {
462     return (md_load_dual(args, modulep, dtb, 0));
463 }
464 #endif
465 
466 #if defined(__mips__) || defined(__powerpc__) || defined(__sparc64__)
467 int
468 md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
469 {
470     return (md_load_dual(args, modulep, dtb, 1));
471 }
472 #endif
473