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>
43 * Return a 'boothowto' value corresponding to the kernel arguments in
44 * (kargs) and any relevant environment variables.
51 { "boot_askname", RB_ASKNAME},
52 { "boot_cdrom", RB_CDROM},
53 { "boot_ddb", RB_KDB},
54 { "boot_dfltroot", RB_DFLTROOT},
55 { "boot_gdb", RB_GDB},
56 { "boot_multicons", RB_MULTIPLE},
57 { "boot_mute", RB_MUTE},
58 { "boot_pause", RB_PAUSE},
59 { "boot_serial", RB_SERIAL},
60 { "boot_single", RB_SINGLE},
61 { "boot_verbose", RB_VERBOSE},
65 static const char howto_switches[] = "aCdrgDmphsv";
66 static int howto_masks[] = {
67 RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
68 RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
72 bi_getboothowto(char *kargs)
80 /* Get the boot options from the environment first. */
81 for (i = 0; howto_names[i].ev != NULL; i++) {
82 if (getenv(howto_names[i].ev) != NULL)
83 howto |= howto_names[i].mask;
90 opts = strchr(kargs, '-');
91 while (opts != NULL) {
92 while (*(++opts) != '\0') {
93 sw = strchr(howto_switches, *opts);
96 howto |= howto_masks[sw - howto_switches];
98 opts = strchr(opts, '-');
105 * Copy the environment into the load area starting at (addr).
106 * Each variable is formatted as <name>=<value>, with a single nul
107 * separating each variable, and a double nul terminating the environment.
110 bi_copyenv(vm_offset_t start)
113 vm_offset_t addr, last;
118 /* Traverse the environment. */
119 for (ep = environ; ep != NULL; ep = ep->ev_next) {
120 len = strlen(ep->ev_name);
121 if (ia64_copyin(ep->ev_name, addr, len) != len)
124 if (ia64_copyin("=", addr, 1) != 1)
127 if (ep->ev_value != NULL) {
128 len = strlen(ep->ev_value);
129 if (ia64_copyin(ep->ev_value, addr, len) != len)
133 if (ia64_copyin("", addr, 1) != 1)
138 if (ia64_copyin("", last++, 1) != 1)
144 * Copy module-related data into the load area, where it can be
145 * used as a directory for loaded modules.
147 * Module data is presented in a self-describing format. Each datum
148 * is preceded by a 32-bit identifier and a 32-bit size field.
150 * Currently, the following data are saved:
152 * MOD_NAME (variable) module name (string)
153 * MOD_TYPE (variable) module type (string)
154 * MOD_ARGS (variable) module parameters (string)
155 * MOD_ADDR sizeof(vm_offset_t) module load address
156 * MOD_SIZE sizeof(size_t) module size
157 * MOD_METADATA (variable) type-specific metadata
159 #define COPY32(v, a) { \
161 ia64_copyin(&x, a, sizeof(x)); \
165 #define MOD_STR(t, a, s) { \
167 COPY32(strlen(s) + 1, a); \
168 ia64_copyin(s, a, strlen(s) + 1); \
169 a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
172 #define MOD_NAME(a, s) MOD_STR(MODINFO_NAME, a, s)
173 #define MOD_TYPE(a, s) MOD_STR(MODINFO_TYPE, a, s)
174 #define MOD_ARGS(a, s) MOD_STR(MODINFO_ARGS, a, s)
176 #define MOD_VAR(t, a, s) { \
178 COPY32(sizeof(s), a); \
179 ia64_copyin(&s, a, sizeof(s)); \
180 a += roundup(sizeof(s), sizeof(u_int64_t)); \
183 #define MOD_ADDR(a, s) MOD_VAR(MODINFO_ADDR, a, s)
184 #define MOD_SIZE(a, s) MOD_VAR(MODINFO_SIZE, a, s)
186 #define MOD_METADATA(a, mm) { \
187 COPY32(MODINFO_METADATA | mm->md_type, a); \
188 COPY32(mm->md_size, a); \
189 ia64_copyin(mm->md_data, a, mm->md_size); \
190 a += roundup(mm->md_size, sizeof(u_int64_t));\
193 #define MOD_END(a) { \
194 COPY32(MODINFO_END, a); \
199 bi_copymodules(vm_offset_t addr)
201 struct preloaded_file *fp;
202 struct file_metadata *md;
204 /* Start with the first module on the list, should be the kernel. */
205 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
206 /* The name field must come first. */
207 MOD_NAME(addr, fp->f_name);
208 MOD_TYPE(addr, fp->f_type);
210 MOD_ARGS(addr, fp->f_args);
211 MOD_ADDR(addr, fp->f_addr);
212 MOD_SIZE(addr, fp->f_size);
213 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
214 if (!(md->md_type & MODINFOMD_NOCOPY))
215 MOD_METADATA(addr, md);
223 * Load the information expected by the kernel.
225 * - The kernel environment is copied into kernel space.
226 * - Module metadata are formatted and placed in kernel space.
229 bi_load(struct preloaded_file *fp, uint64_t *bi_addr)
232 struct preloaded_file *xp;
233 struct file_metadata *md;
234 struct devdesc *rootdev;
236 vm_offset_t addr, ssym, esym;
238 bzero(&bi, sizeof(struct bootinfo));
239 bi.bi_magic = BOOTINFO_MAGIC;
241 bi.bi_boothowto = bi_getboothowto(fp->f_args);
244 * Allow the environment variable 'rootdev' to override the supplied
245 * device. This should perhaps go to MI code and/or have $rootdev
246 * tested/set by MI code before launching the kernel.
248 rootdevname = getenv("rootdev");
249 ia64_getdev((void**)&rootdev, rootdevname, NULL);
250 if (rootdev != NULL) {
251 /* Try reading /etc/fstab to select the root device. */
252 getrootmount(ia64_fmtdev(rootdev));
256 md = file_findmetadata(fp, MODINFOMD_SSYM);
257 ssym = (md != NULL) ? *((vm_offset_t *)&(md->md_data)) : 0;
258 md = file_findmetadata(fp, MODINFOMD_ESYM);
259 esym = (md != NULL) ? *((vm_offset_t *)&(md->md_data)) : 0;
260 if (ssym != 0 && esym != 0) {
262 bi.bi_esymtab = esym;
265 /* Find the last module in the chain. */
267 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
268 if (addr < (xp->f_addr + xp->f_size))
269 addr = xp->f_addr + xp->f_size;
272 addr = (addr + 15) & ~15;
274 /* Copy module list and metadata. */
275 bi.bi_modulep = addr;
276 addr = bi_copymodules(addr);
277 if (addr <= bi.bi_modulep) {
278 addr = bi.bi_modulep;
282 addr = (addr + 15) & ~15;
284 /* Copy our environment. */
286 addr = bi_copyenv(addr);
287 if (addr <= bi.bi_envp) {
292 addr = (addr + PAGE_MASK) & ~PAGE_MASK;
293 bi.bi_kernend = addr;
295 return (ldr_bootinfo(&bi, bi_addr));