xref: /trueos/sys/boot/powerpc/ofw/metadata.c (revision 7a1ab70b9590c4c122be3d913b579be45424f95a)
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 
38 #include <machine/metadata.h>
39 
40 #include "bootstrap.h"
41 #include "libofw.h"
42 
43 int
md_getboothowto(char * kargs)44 md_getboothowto(char *kargs)
45 {
46     char	*cp;
47     int		howto;
48     int		active;
49     int		i;
50 
51     /* Parse kargs */
52     howto = 0;
53     if (kargs != NULL) {
54 	cp = kargs;
55 	active = 0;
56 	while (*cp != 0) {
57 	    if (!active && (*cp == '-')) {
58 		active = 1;
59 	    } else if (active)
60 		switch (*cp) {
61 		case 'a':
62 		    howto |= RB_ASKNAME;
63 		    break;
64 		case 'C':
65 		    howto |= RB_CDROM;
66 		    break;
67 		case 'd':
68 		    howto |= RB_KDB;
69 		    break;
70 		case 'D':
71 		    howto |= RB_MULTIPLE;
72 		    break;
73 		case 'm':
74 		    howto |= RB_MUTE;
75 		    break;
76 		case 'g':
77 		    howto |= RB_GDB;
78 		    break;
79 		case 'h':
80 		    howto |= RB_SERIAL;
81 		    break;
82 		case 'p':
83 		    howto |= RB_PAUSE;
84 		    break;
85 		case 'r':
86 		    howto |= RB_DFLTROOT;
87 		    break;
88 		case 's':
89 		    howto |= RB_SINGLE;
90 		    break;
91 		case 'v':
92 		    howto |= RB_VERBOSE;
93 		    break;
94 		default:
95 		    active = 0;
96 		    break;
97 		}
98 	    cp++;
99 	}
100     }
101     /* get equivalents from the environment */
102     for (i = 0; howto_names[i].ev != NULL; i++)
103 	if (getenv(howto_names[i].ev) != NULL)
104 	    howto |= howto_names[i].mask;
105     if (!strcmp(getenv("console"), "comconsole"))
106 	howto |= RB_SERIAL;
107     if (!strcmp(getenv("console"), "nullconsole"))
108 	howto |= RB_MUTE;
109     return(howto);
110 }
111 
112 /*
113  * Copy the environment into the load area starting at (addr).
114  * Each variable is formatted as <name>=<value>, with a single nul
115  * separating each variable, and a double nul terminating the environment.
116  */
117 vm_offset_t
md_copyenv(vm_offset_t addr)118 md_copyenv(vm_offset_t addr)
119 {
120     struct env_var	*ep;
121 
122     /* traverse the environment */
123     for (ep = environ; ep != NULL; ep = ep->ev_next) {
124 	archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
125 	addr += strlen(ep->ev_name);
126 	archsw.arch_copyin("=", addr, 1);
127 	addr++;
128 	if (ep->ev_value != NULL) {
129 	    archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
130 	    addr += strlen(ep->ev_value);
131 	}
132 	archsw.arch_copyin("", addr, 1);
133 	addr++;
134     }
135     archsw.arch_copyin("", addr, 1);
136     addr++;
137     return(addr);
138 }
139 
140 /*
141  * Copy module-related data into the load area, where it can be
142  * used as a directory for loaded modules.
143  *
144  * Module data is presented in a self-describing format.  Each datum
145  * is preceded by a 32-bit identifier and a 32-bit size field.
146  *
147  * Currently, the following data are saved:
148  *
149  * MOD_NAME	(variable)		module name (string)
150  * MOD_TYPE	(variable)		module type (string)
151  * MOD_ARGS	(variable)		module parameters (string)
152  * MOD_ADDR	sizeof(vm_offset_t)	module load address
153  * MOD_SIZE	sizeof(size_t)		module size
154  * MOD_METADATA	(variable)		type-specific metadata
155  */
156 
157 static int align;
158 
159 #define COPY32(v, a, c) {			\
160     u_int32_t	x = (v);			\
161     if (c)					\
162         archsw.arch_copyin(&x, a, sizeof(x));	\
163     a += sizeof(x);				\
164 }
165 
166 #define MOD_STR(t, a, s, c) {			\
167     COPY32(t, a, c);				\
168     COPY32(strlen(s) + 1, a, c)			\
169     if (c)					\
170         archsw.arch_copyin(s, a, strlen(s) + 1);\
171     a += roundup(strlen(s) + 1, align);		\
172 }
173 
174 #define MOD_NAME(a, s, c)	MOD_STR(MODINFO_NAME, a, s, c)
175 #define MOD_TYPE(a, s, c)	MOD_STR(MODINFO_TYPE, a, s, c)
176 #define MOD_ARGS(a, s, c)	MOD_STR(MODINFO_ARGS, a, s, c)
177 
178 #define MOD_VAR(t, a, s, c) {			\
179     COPY32(t, a, c);				\
180     COPY32(sizeof(s), a, c);			\
181     if (c)					\
182         archsw.arch_copyin(&s, a, sizeof(s));	\
183     a += roundup(sizeof(s), align);		\
184 }
185 
186 #define MOD_ADDR(a, s, c)	MOD_VAR(MODINFO_ADDR, a, s, c)
187 #define MOD_SIZE(a, s, c)	MOD_VAR(MODINFO_SIZE, a, s, c)
188 
189 #define MOD_METADATA(a, mm, c) {		\
190     COPY32(MODINFO_METADATA | mm->md_type, a, c);\
191     COPY32(mm->md_size, a, c);			\
192     if (c)					\
193         archsw.arch_copyin(mm->md_data, a, mm->md_size);\
194     a += roundup(mm->md_size, align);		\
195 }
196 
197 #define MOD_END(a, c) {				\
198     COPY32(MODINFO_END, a, c);			\
199     COPY32(0, a, c);				\
200 }
201 
202 vm_offset_t
md_copymodules(vm_offset_t addr,int kern64)203 md_copymodules(vm_offset_t addr, int kern64)
204 {
205     struct preloaded_file	*fp;
206     struct file_metadata	*md;
207     uint64_t			scratch64;
208     int				c;
209 
210     c = addr != 0;
211     /* start with the first module on the list, should be the kernel */
212     for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
213 
214 	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
215 	MOD_TYPE(addr, fp->f_type, c);
216 	if (fp->f_args)
217 	    MOD_ARGS(addr, fp->f_args, c);
218 	if (kern64) {
219 		scratch64 = fp->f_addr;
220 		MOD_ADDR(addr, scratch64, c);
221 		scratch64 = fp->f_size;
222 		MOD_SIZE(addr, scratch64, c);
223 	} else {
224 		MOD_ADDR(addr, fp->f_addr, c);
225 		MOD_SIZE(addr, fp->f_size, c);
226 	}
227 	for (md = fp->f_metadata; md != NULL; md = md->md_next) {
228 	    if (!(md->md_type & MODINFOMD_NOCOPY)) {
229 		MOD_METADATA(addr, md, c);
230 	    }
231 	}
232     }
233     MOD_END(addr, c);
234     return(addr);
235 }
236 
237 /*
238  * Load the information expected by a powerpc kernel.
239  *
240  * - The 'boothowto' argument is constructed
241  * - The 'bootdev' argument is constructed
242  * - The kernel environment is copied into kernel space.
243  * - Module metadata are formatted and placed in kernel space.
244  */
245 int
md_load_dual(char * args,vm_offset_t * modulep,int kern64)246 md_load_dual(char *args, vm_offset_t *modulep, int kern64)
247 {
248     struct preloaded_file	*kfp;
249     struct preloaded_file	*xp;
250     struct file_metadata	*md;
251     vm_offset_t			kernend;
252     vm_offset_t			addr;
253     vm_offset_t			envp;
254     vm_offset_t			size;
255     uint64_t			scratch64;
256     char			*rootdevname;
257     int				howto;
258 
259     align = kern64 ? 8 : 4;
260     howto = md_getboothowto(args);
261 
262     /*
263      * Allow the environment variable 'rootdev' to override the supplied device
264      * This should perhaps go to MI code and/or have $rootdev tested/set by
265      * MI code before launching the kernel.
266      */
267     rootdevname = getenv("rootdev");
268     if (rootdevname == NULL)
269 	    rootdevname = getenv("currdev");
270     /* Try reading the /etc/fstab file to select the root device */
271     getrootmount(rootdevname);
272 
273     /* find the last module in the chain */
274     addr = 0;
275     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
276 	if (addr < (xp->f_addr + xp->f_size))
277 	    addr = xp->f_addr + xp->f_size;
278     }
279     /* pad to a page boundary */
280     addr = roundup(addr, PAGE_SIZE);
281 
282     /* copy our environment */
283     envp = addr;
284     addr = md_copyenv(addr);
285 
286     /* pad to a page boundary */
287     addr = roundup(addr, PAGE_SIZE);
288 
289     kernend = 0;
290     kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel");
291     if (kfp == NULL)
292 	kfp = file_findfile(NULL, "elf kernel");
293     if (kfp == NULL)
294 	panic("can't find kernel file");
295     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
296     if (kern64) {
297 	scratch64 = envp;
298 	file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
299 	scratch64 = kernend;
300 	file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof scratch64, &scratch64);
301     } else {
302 	file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
303 	file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
304     }
305 
306     *modulep = addr;
307     size = md_copymodules(0, kern64);
308     kernend = roundup(addr + size, PAGE_SIZE);
309 
310     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
311     if (kern64) {
312 	scratch64 = kernend;
313 	bcopy(&scratch64, md->md_data, sizeof scratch64);
314     } else {
315 	bcopy(&kernend, md->md_data, sizeof kernend);
316     }
317 
318     (void)md_copymodules(addr, kern64);
319 
320     return(0);
321 }
322 
323 int
md_load(char * args,vm_offset_t * modulep)324 md_load(char *args, vm_offset_t *modulep)
325 {
326     return (md_load_dual(args, modulep, 0));
327 }
328 
329 int
md_load64(char * args,vm_offset_t * modulep)330 md_load64(char *args, vm_offset_t *modulep)
331 {
332     return (md_load_dual(args, modulep, 1));
333 }
334 
335