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