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