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