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