2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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/pc32/libi386/bootinfo.c,v 1.2 2003/06/17 04:28:18 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/reboot.h>
33 #include <sys/linker.h>
34 #include <machine/bootinfo.h>
35 #include "bootstrap.h"
39 static struct bootinfo bi;
42 * Return a 'boothowto' value corresponding to the kernel arguments in
43 * (kargs) and any relevant environment variables.
50 {"boot_askname", RB_ASKNAME},
51 {"boot_cdrom", RB_CDROM},
52 {"boot_userconfig", RB_CONFIG},
55 {"boot_single", RB_SINGLE},
56 {"boot_verbose", RB_VERBOSE},
60 vm_offset_t bi_copymodules(vm_offset_t addr);
63 bi_getboothowto(char *kargs)
76 if (!active && (*cp == '-')) {
105 howto |= RB_DFLTROOT;
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"))
126 if (!strcmp(getenv("console"), "nullconsole"))
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.
137 bi_copyenv(vm_offset_t addr)
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);
147 if (ep->ev_value != NULL) {
148 i386_copyin(ep->ev_value, addr, strlen(ep->ev_value));
149 addr += strlen(ep->ev_value);
151 i386_copyin("", addr, 1);
154 i386_copyin("", addr, 1);
160 * Copy module-related data into the load area, where it can be
161 * used as a directory for loaded modules.
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.
166 * Currently, the following data are saved:
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
175 #define COPY32(v, a) { \
177 i386_copyin(&x, a, sizeof(x)); \
181 #define MOD_STR(t, a, s) { \
183 COPY32(strlen(s) + 1, a); \
184 i386_copyin(s, a, strlen(s) + 1); \
185 a += roundup(strlen(s) + 1, sizeof(u_long));\
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)
192 #define MOD_VAR(t, a, s) { \
194 COPY32(sizeof(s), a); \
195 i386_copyin(&s, a, sizeof(s)); \
196 a += roundup(sizeof(s), sizeof(u_long)); \
199 #define MOD_ADDR(a, s) MOD_VAR(MODINFO_ADDR, a, s)
200 #define MOD_SIZE(a, s) MOD_VAR(MODINFO_SIZE, a, s)
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));\
209 #define MOD_END(a) { \
210 COPY32(MODINFO_END, a); \
215 bi_copymodules(vm_offset_t addr)
217 struct loaded_module *mp;
218 struct module_metadata *md;
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) {
223 MOD_NAME(addr, mp->m_name); /* this field must come first */
224 MOD_TYPE(addr, mp->m_type);
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);
238 * Load the information expected by an i386 kernel.
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.
247 bi_load(char *args, int *howtop, int *bootdevp, vm_offset_t *bip)
249 struct loaded_module *xp;
250 struct i386_devdesc *rootdev;
256 const char *kernelpath;
258 *howtop = bi_getboothowto(args);
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.
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");
272 /* Try reading the /etc/fstab file to select the root device */
273 getrootmount(i386_fmtdev((void *)rootdev));
275 /* Do legacy rootdev guessing */
277 /* XXX - use a default bootdev of 0. Is this ok??? */
280 switch(rootdev->d_type) {
282 /* Pass in BIOS device number. */
283 bi.bi_bios_dev = bc_unit2bios(rootdev->d_kind.bioscd.unit);
284 bootdevnr = bc_getdev(rootdev);
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);
297 printf("WARNING - don't know how to boot from device type %d\n", rootdev->d_type);
299 if (bootdevnr == -1) {
300 printf("root device %s invalid\n", i386_fmtdev(rootdev));
304 *bootdevp = bootdevnr;
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;
318 /* find the last module in the chain */
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;
324 /* pad to a page boundary */
325 pad = (u_int)addr & PAGE_MASK;
327 pad = PAGE_SIZE - pad;
331 /* copy our environment */
333 addr = bi_copyenv(addr);
335 /* pad to a page boundary */
336 pad = (u_int)addr & PAGE_MASK;
338 pad = PAGE_SIZE - pad;
341 /* copy module list and metadata */
342 bi.bi_modulep = addr;
343 addr = bi_copymodules(addr);
345 /* all done copying stuff in, save end of loaded object space */
346 bi.bi_kernend = addr;
348 *howtop |= RB_BOOTINFO; /* it's there now */
351 * Get the kernel name, strip off any device prefix.
353 kernelname = getenv("kernelname");
354 i386_getdev(NULL, kernelname, &kernelpath);
355 bi.bi_kernelname = VTOP(kernelpath);