2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2006 Marcel Moolenaar
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/reboot.h>
35 #include <sys/linker.h>
41 #include "bootstrap.h"
43 #include <machine/bootinfo.h>
45 static const char howto_switches[] = "aCdrgDmphsv";
46 static int howto_masks[] = {
47 RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
48 RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
52 bi_getboothowto(char *kargs)
60 /* Get the boot options from the environment first. */
61 for (i = 0; howto_names[i].ev != NULL; i++) {
62 if (getenv(howto_names[i].ev) != NULL)
63 howto |= howto_names[i].mask;
70 opts = strchr(kargs, '-');
71 while (opts != NULL) {
72 while (*(++opts) != '\0') {
73 sw = strchr(howto_switches, *opts);
76 howto |= howto_masks[sw - howto_switches];
78 opts = strchr(opts, '-');
85 * Copy the environment into the load area starting at (addr).
86 * Each variable is formatted as <name>=<value>, with a single nul
87 * separating each variable, and a double nul terminating the environment.
90 bi_copyenv(vm_offset_t start)
93 vm_offset_t addr, last;
98 /* Traverse the environment. */
99 for (ep = environ; ep != NULL; ep = ep->ev_next) {
100 len = strlen(ep->ev_name);
101 if (i386_copyin(ep->ev_name, addr, len) != len)
104 if (i386_copyin("=", addr, 1) != 1)
107 if (ep->ev_value != NULL) {
108 len = strlen(ep->ev_value);
109 if (i386_copyin(ep->ev_value, addr, len) != len)
113 if (i386_copyin("", addr, 1) != 1)
118 if (i386_copyin("", last++, 1) != 1)
124 * Copy module-related data into the load area, where it can be
125 * used as a directory for loaded modules.
127 * Module data is presented in a self-describing format. Each datum
128 * is preceded by a 32-bit identifier and a 32-bit size field.
130 * Currently, the following data are saved:
132 * MOD_NAME (variable) module name (string)
133 * MOD_TYPE (variable) module type (string)
134 * MOD_ARGS (variable) module parameters (string)
135 * MOD_ADDR sizeof(vm_offset_t) module load address
136 * MOD_SIZE sizeof(size_t) module size
137 * MOD_METADATA (variable) type-specific metadata
139 #define COPY32(v, a) { \
141 i386_copyin(&x, a, sizeof(x)); \
145 #define MOD_STR(t, a, s) { \
147 COPY32(strlen(s) + 1, a); \
148 i386_copyin(s, a, strlen(s) + 1); \
149 a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
152 #define MOD_NAME(a, s) MOD_STR(MODINFO_NAME, a, s)
153 #define MOD_TYPE(a, s) MOD_STR(MODINFO_TYPE, a, s)
154 #define MOD_ARGS(a, s) MOD_STR(MODINFO_ARGS, a, s)
156 #define MOD_VAR(t, a, s) { \
158 COPY32(sizeof(s), a); \
159 i386_copyin(&s, a, sizeof(s)); \
160 a += roundup(sizeof(s), sizeof(u_int64_t)); \
163 #define MOD_ADDR(a, s) MOD_VAR(MODINFO_ADDR, a, s)
164 #define MOD_SIZE(a, s) MOD_VAR(MODINFO_SIZE, a, s)
166 #define MOD_METADATA(a, mm) { \
167 COPY32(MODINFO_METADATA | mm->md_type, a); \
168 COPY32(mm->md_size, a); \
169 i386_copyin(mm->md_data, a, mm->md_size); \
170 a += roundup(mm->md_size, sizeof(u_int64_t));\
173 #define MOD_END(a) { \
174 COPY32(MODINFO_END, a); \
179 bi_copymodules(vm_offset_t addr)
181 struct preloaded_file *fp;
182 struct file_metadata *md;
184 /* Start with the first module on the list, should be the kernel. */
185 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
186 /* The name field must come first. */
187 MOD_NAME(addr, fp->f_name);
188 MOD_TYPE(addr, fp->f_type);
190 MOD_ARGS(addr, fp->f_args);
191 MOD_ADDR(addr, fp->f_addr);
192 MOD_SIZE(addr, fp->f_size);
193 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
194 if (!(md->md_type & MODINFOMD_NOCOPY))
195 MOD_METADATA(addr, md);
203 * Load the information expected by the kernel.
205 * - The kernel environment is copied into kernel space.
206 * - Module metadata are formatted and placed in kernel space.
209 bi_load(struct preloaded_file *fp, uint64_t *bi_addr)
212 struct preloaded_file *xp;
213 struct file_metadata *md;
214 struct devdesc *rootdev;
216 vm_offset_t addr, ssym, esym;
218 bzero(&bi, sizeof(struct bootinfo));
220 // bi.bi_boothowto = bi_getboothowto(fp->f_args);
223 * Allow the environment variable 'rootdev' to override the supplied
224 * device. This should perhaps go to MI code and/or have $rootdev
225 * tested/set by MI code before launching the kernel.
227 rootdevname = getenv("rootdev");
228 i386_getdev((void**)&rootdev, rootdevname, NULL);
229 if (rootdev != NULL) {
230 /* Try reading /etc/fstab to select the root device. */
231 getrootmount(i386_fmtdev(rootdev));
235 md = file_findmetadata(fp, MODINFOMD_SSYM);
236 ssym = (md != NULL) ? *((vm_offset_t *)&(md->md_data)) : 0;
237 md = file_findmetadata(fp, MODINFOMD_ESYM);
238 esym = (md != NULL) ? *((vm_offset_t *)&(md->md_data)) : 0;
239 if (ssym != 0 && esym != 0) {
241 bi.bi_esymtab = esym;
244 /* Find the last module in the chain. */
246 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
247 if (addr < (xp->f_addr + xp->f_size))
248 addr = xp->f_addr + xp->f_size;
251 addr = (addr + 15) & ~15;
253 /* Copy module list and metadata. */
254 bi.bi_modulep = addr;
255 addr = bi_copymodules(addr);
256 if (addr <= bi.bi_modulep) {
257 addr = bi.bi_modulep;
261 addr = (addr + 15) & ~15;
263 /* Copy our environment. */
265 addr = bi_copyenv(addr);
266 if (addr <= bi.bi_envp) {
271 addr = (addr + PAGE_MASK) & ~PAGE_MASK;
272 bi.bi_kernend = addr;
274 return (ldr_bootinfo(&bi, bi_addr));