Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / boot / i386 / libi386 / 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/i386/libi386/bootinfo.c,v 1.23.2.6 2002/01/07 07:37:52 jhb Exp $
27  * $DragonFly: src/sys/boot/i386/libi386/Attic/bootinfo.c,v 1.2 2003/06/17 04:28:18 dillon Exp $
28  */
29
30 #include <stand.h>
31 #include <sys/param.h>
32 #include <sys/reboot.h>
33 #include <sys/linker.h>
34 #include <machine/bootinfo.h>
35 #include "bootstrap.h"
36 #include "libi386.h"
37 #include "btxv86.h"
38
39 static struct bootinfo  bi;
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     {NULL,      0}
58 };
59
60 vm_offset_t     bi_copymodules(vm_offset_t addr);
61
62 int
63 bi_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 'm':
93                     howto |= RB_MUTE;
94                     break;
95                 case 'g':
96                     howto |= RB_GDB;
97                     break;
98                 case 'h':
99                     howto |= RB_SERIAL;
100                     break;
101                 case 'p':
102                     howto |= RB_PAUSE;
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 bi_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         i386_copyin(ep->ev_name, addr, strlen(ep->ev_name));
144         addr += strlen(ep->ev_name);
145         i386_copyin("=", addr, 1);
146         addr++;
147         if (ep->ev_value != NULL) {
148             i386_copyin(ep->ev_value, addr, strlen(ep->ev_value));
149             addr += strlen(ep->ev_value);
150         }
151         i386_copyin("", addr, 1);
152         addr++;
153     }
154     i386_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 preceeded 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) {                          \
176     u_int32_t   x = (v);                        \
177     i386_copyin(&x, a, sizeof(x));              \
178     a += sizeof(x);                             \
179 }
180
181 #define MOD_STR(t, a, s) {                      \
182     COPY32(t, a);                               \
183     COPY32(strlen(s) + 1, a);                   \
184     i386_copyin(s, a, strlen(s) + 1);           \
185     a += roundup(strlen(s) + 1, sizeof(u_long));\
186 }
187
188 #define MOD_NAME(a, s)  MOD_STR(MODINFO_NAME, a, s)
189 #define MOD_TYPE(a, s)  MOD_STR(MODINFO_TYPE, a, s)
190 #define MOD_ARGS(a, s)  MOD_STR(MODINFO_ARGS, a, s)
191
192 #define MOD_VAR(t, a, s) {                      \
193     COPY32(t, a);                               \
194     COPY32(sizeof(s), a);                       \
195     i386_copyin(&s, a, sizeof(s));              \
196     a += roundup(sizeof(s), sizeof(u_long));    \
197 }
198
199 #define MOD_ADDR(a, s)  MOD_VAR(MODINFO_ADDR, a, s)
200 #define MOD_SIZE(a, s)  MOD_VAR(MODINFO_SIZE, a, s)
201
202 #define MOD_METADATA(a, mm) {                   \
203     COPY32(MODINFO_METADATA | mm->md_type, a);  \
204     COPY32(mm->md_size, a);                     \
205     i386_copyin(mm->md_data, a, mm->md_size);   \
206     a += roundup(mm->md_size, sizeof(u_long));\
207 }
208
209 #define MOD_END(a) {                            \
210     COPY32(MODINFO_END, a);                     \
211     COPY32(0, a);                               \
212 }
213
214 vm_offset_t
215 bi_copymodules(vm_offset_t addr)
216 {
217     struct loaded_module        *mp;
218     struct module_metadata      *md;
219
220     /* start with the first module on the list, should be the kernel */
221     for (mp = mod_findmodule(NULL, NULL); mp != NULL; mp = mp->m_next) {
222
223         MOD_NAME(addr, mp->m_name);     /* this field must come first */
224         MOD_TYPE(addr, mp->m_type);
225         if (mp->m_args)
226             MOD_ARGS(addr, mp->m_args);
227         MOD_ADDR(addr, mp->m_addr);
228         MOD_SIZE(addr, mp->m_size);
229         for (md = mp->m_metadata; md != NULL; md = md->md_next)
230             if (!(md->md_type & MODINFOMD_NOCOPY))
231                 MOD_METADATA(addr, md);
232     }
233     MOD_END(addr);
234     return(addr);
235 }
236
237 /*
238  * Load the information expected by an i386 kernel.
239  *
240  * - The 'boothowto' argument is constructed
241  * - The 'bootdev' argument is constructed
242  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
243  * - The kernel environment is copied into kernel space.
244  * - Module metadata are formatted and placed in kernel space.
245  */
246 int
247 bi_load(char *args, int *howtop, int *bootdevp, vm_offset_t *bip)
248 {
249     struct loaded_module        *xp;
250     struct i386_devdesc         *rootdev;
251     vm_offset_t                 addr;
252     char                        *rootdevname;
253     int                         bootdevnr, i;
254     u_int                       pad;
255     char                        *kernelname;
256     const char                  *kernelpath;
257
258     *howtop = bi_getboothowto(args);
259
260     /* 
261      * Allow the environment variable 'rootdev' to override the supplied device 
262      * This should perhaps go to MI code and/or have $rootdev tested/set by
263      * MI code before launching the kernel.
264      */
265     rootdevname = getenv("rootdev");
266     i386_getdev((void **)(&rootdev), rootdevname, NULL);
267     if (rootdev == NULL) {              /* bad $rootdev/$currdev */
268         printf("can't determine root device\n");
269         return(EINVAL);
270     }
271
272     /* Try reading the /etc/fstab file to select the root device */
273     getrootmount(i386_fmtdev((void *)rootdev));
274
275     /* Do legacy rootdev guessing */
276
277     /* XXX - use a default bootdev of 0.  Is this ok??? */
278     bootdevnr = 0;
279
280     switch(rootdev->d_type) {
281     case DEVT_CD:
282             /* Pass in BIOS device number. */
283             bi.bi_bios_dev = bc_unit2bios(rootdev->d_kind.bioscd.unit);
284             bootdevnr = bc_getdev(rootdev);
285             break;
286
287     case DEVT_DISK:
288         /* pass in the BIOS device number of the current disk */
289         bi.bi_bios_dev = bd_unit2bios(rootdev->d_kind.biosdisk.unit);
290         bootdevnr = bd_getdev(rootdev);
291         break;
292
293     case DEVT_NET:
294             break;
295             
296     default:
297         printf("WARNING - don't know how to boot from device type %d\n", rootdev->d_type);
298     }
299     if (bootdevnr == -1) {
300         printf("root device %s invalid\n", i386_fmtdev(rootdev));
301         return (EINVAL);
302     }
303     free(rootdev);
304     *bootdevp = bootdevnr;
305
306     /* legacy bootinfo structure */
307     bi.bi_version = BOOTINFO_VERSION;
308     bi.bi_kernelname = 0;               /* XXX char * -> kernel name */
309     bi.bi_nfs_diskless = 0;             /* struct nfs_diskless * */
310     bi.bi_n_bios_used = 0;              /* XXX would have to hook biosdisk driver for these */
311     for (i = 0; i < N_BIOS_GEOM; i++)
312         bi.bi_bios_geom[i] = bd_getbigeom(i);
313     bi.bi_size = sizeof(bi);
314     bi.bi_memsizes_valid = 1;
315     bi.bi_basemem = bios_basemem / 1024;
316     bi.bi_extmem = bios_extmem / 1024;
317
318     /* find the last module in the chain */
319     addr = 0;
320     for (xp = mod_findmodule(NULL, NULL); xp != NULL; xp = xp->m_next) {
321         if (addr < (xp->m_addr + xp->m_size))
322             addr = xp->m_addr + xp->m_size;
323     }
324     /* pad to a page boundary */
325     pad = (u_int)addr & PAGE_MASK;
326     if (pad != 0) {
327         pad = PAGE_SIZE - pad;
328         addr += pad;
329     }
330
331     /* copy our environment */
332     bi.bi_envp = addr;
333     addr = bi_copyenv(addr);
334
335     /* pad to a page boundary */
336     pad = (u_int)addr & PAGE_MASK;
337     if (pad != 0) {
338         pad = PAGE_SIZE - pad;
339         addr += pad;
340     }
341     /* copy module list and metadata */
342     bi.bi_modulep = addr;
343     addr = bi_copymodules(addr);
344
345     /* all done copying stuff in, save end of loaded object space */
346     bi.bi_kernend = addr;
347
348     *howtop |= RB_BOOTINFO;             /* it's there now */
349
350     /*
351      * Get the kernel name, strip off any device prefix.
352      */
353     kernelname = getenv("kernelname");
354     i386_getdev(NULL, kernelname, &kernelpath);
355     bi.bi_kernelname = VTOP(kernelpath);
356     *bip = VTOP(&bi);
357
358     return(0);
359 }