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