Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / boot / pc32 / 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  */
28
29 #include <stand.h>
30 #include <sys/param.h>
31 #include <sys/reboot.h>
32 #include <sys/linker.h>
33 #include <machine/bootinfo.h>
34 #include "bootstrap.h"
35 #include "libi386.h"
36 #include "btxv86.h"
37
38 static struct bootinfo  bi;
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     {NULL,      0}
57 };
58
59 vm_offset_t     bi_copymodules(vm_offset_t addr);
60
61 int
62 bi_getboothowto(char *kargs)
63 {
64     char        *cp;
65     int         howto;
66     int         active;
67     int         i;
68     
69     /* Parse kargs */
70     howto = 0;
71     if (kargs  != NULL) {
72         cp = kargs;
73         active = 0;
74         while (*cp != 0) {
75             if (!active && (*cp == '-')) {
76                 active = 1;
77             } else if (active)
78                 switch (*cp) {
79                 case 'a':
80                     howto |= RB_ASKNAME;
81                     break;
82                 case 'c':
83                     howto |= RB_CONFIG;
84                     break;
85                 case 'C':
86                     howto |= RB_CDROM;
87                     break;
88                 case 'd':
89                     howto |= RB_KDB;
90                     break;
91                 case 'm':
92                     howto |= RB_MUTE;
93                     break;
94                 case 'g':
95                     howto |= RB_GDB;
96                     break;
97                 case 'h':
98                     howto |= RB_SERIAL;
99                     break;
100                 case 'p':
101                     howto |= RB_PAUSE;
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         i386_copyin(ep->ev_name, addr, strlen(ep->ev_name));
143         addr += strlen(ep->ev_name);
144         i386_copyin("=", addr, 1);
145         addr++;
146         if (ep->ev_value != NULL) {
147             i386_copyin(ep->ev_value, addr, strlen(ep->ev_value));
148             addr += strlen(ep->ev_value);
149         }
150         i386_copyin("", addr, 1);
151         addr++;
152     }
153     i386_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 preceeded 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     i386_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     i386_copyin(s, a, strlen(s) + 1);           \
184     a += roundup(strlen(s) + 1, sizeof(u_long));\
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     i386_copyin(&s, a, sizeof(s));              \
195     a += roundup(sizeof(s), sizeof(u_long));    \
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     i386_copyin(mm->md_data, a, mm->md_size);   \
205     a += roundup(mm->md_size, sizeof(u_long));\
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 loaded_module        *mp;
217     struct module_metadata      *md;
218
219     /* start with the first module on the list, should be the kernel */
220     for (mp = mod_findmodule(NULL, NULL); mp != NULL; mp = mp->m_next) {
221
222         MOD_NAME(addr, mp->m_name);     /* this field must come first */
223         MOD_TYPE(addr, mp->m_type);
224         if (mp->m_args)
225             MOD_ARGS(addr, mp->m_args);
226         MOD_ADDR(addr, mp->m_addr);
227         MOD_SIZE(addr, mp->m_size);
228         for (md = mp->m_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 i386 kernel.
238  *
239  * - The 'boothowto' argument is constructed
240  * - The 'bootdev' argument is constructed
241  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
242  * - The kernel environment is copied into kernel space.
243  * - Module metadata are formatted and placed in kernel space.
244  */
245 int
246 bi_load(char *args, int *howtop, int *bootdevp, vm_offset_t *bip)
247 {
248     struct loaded_module        *xp;
249     struct i386_devdesc         *rootdev;
250     vm_offset_t                 addr;
251     char                        *rootdevname;
252     int                         bootdevnr, i;
253     u_int                       pad;
254     char                        *kernelname;
255     const char                  *kernelpath;
256
257     *howtop = bi_getboothowto(args);
258
259     /* 
260      * Allow the environment variable 'rootdev' to override the supplied device 
261      * This should perhaps go to MI code and/or have $rootdev tested/set by
262      * MI code before launching the kernel.
263      */
264     rootdevname = getenv("rootdev");
265     i386_getdev((void **)(&rootdev), rootdevname, NULL);
266     if (rootdev == NULL) {              /* bad $rootdev/$currdev */
267         printf("can't determine root device\n");
268         return(EINVAL);
269     }
270
271     /* Try reading the /etc/fstab file to select the root device */
272     getrootmount(i386_fmtdev((void *)rootdev));
273
274     /* Do legacy rootdev guessing */
275
276     /* XXX - use a default bootdev of 0.  Is this ok??? */
277     bootdevnr = 0;
278
279     switch(rootdev->d_type) {
280     case DEVT_CD:
281             /* Pass in BIOS device number. */
282             bi.bi_bios_dev = bc_unit2bios(rootdev->d_kind.bioscd.unit);
283             bootdevnr = bc_getdev(rootdev);
284             break;
285
286     case DEVT_DISK:
287         /* pass in the BIOS device number of the current disk */
288         bi.bi_bios_dev = bd_unit2bios(rootdev->d_kind.biosdisk.unit);
289         bootdevnr = bd_getdev(rootdev);
290         break;
291
292     case DEVT_NET:
293             break;
294             
295     default:
296         printf("WARNING - don't know how to boot from device type %d\n", rootdev->d_type);
297     }
298     if (bootdevnr == -1) {
299         printf("root device %s invalid\n", i386_fmtdev(rootdev));
300         return (EINVAL);
301     }
302     free(rootdev);
303     *bootdevp = bootdevnr;
304
305     /* legacy bootinfo structure */
306     bi.bi_version = BOOTINFO_VERSION;
307     bi.bi_kernelname = 0;               /* XXX char * -> kernel name */
308     bi.bi_nfs_diskless = 0;             /* struct nfs_diskless * */
309     bi.bi_n_bios_used = 0;              /* XXX would have to hook biosdisk driver for these */
310     for (i = 0; i < N_BIOS_GEOM; i++)
311         bi.bi_bios_geom[i] = bd_getbigeom(i);
312     bi.bi_size = sizeof(bi);
313     bi.bi_memsizes_valid = 1;
314     bi.bi_basemem = bios_basemem / 1024;
315     bi.bi_extmem = bios_extmem / 1024;
316
317     /* find the last module in the chain */
318     addr = 0;
319     for (xp = mod_findmodule(NULL, NULL); xp != NULL; xp = xp->m_next) {
320         if (addr < (xp->m_addr + xp->m_size))
321             addr = xp->m_addr + xp->m_size;
322     }
323     /* pad to a page boundary */
324     pad = (u_int)addr & PAGE_MASK;
325     if (pad != 0) {
326         pad = PAGE_SIZE - pad;
327         addr += pad;
328     }
329
330     /* copy our environment */
331     bi.bi_envp = addr;
332     addr = bi_copyenv(addr);
333
334     /* pad to a page boundary */
335     pad = (u_int)addr & PAGE_MASK;
336     if (pad != 0) {
337         pad = PAGE_SIZE - pad;
338         addr += pad;
339     }
340     /* copy module list and metadata */
341     bi.bi_modulep = addr;
342     addr = bi_copymodules(addr);
343
344     /* all done copying stuff in, save end of loaded object space */
345     bi.bi_kernend = addr;
346
347     *howtop |= RB_BOOTINFO;             /* it's there now */
348
349     /*
350      * Get the kernel name, strip off any device prefix.
351      */
352     kernelname = getenv("kernelname");
353     i386_getdev(NULL, kernelname, &kernelpath);
354     bi.bi_kernelname = VTOP(kernelpath);
355     *bip = VTOP(&bi);
356
357     return(0);
358 }