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