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 static const char howto_switches[] = "aCdrgDmphsv";
44 static int howto_masks[] = {
45 RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
46 RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
50 bi_getboothowto(char *kargs)
58 /* Get the boot options from the environment first. */
59 for (i = 0; howto_names[i].ev != NULL; i++) {
60 if (getenv(howto_names[i].ev) != NULL)
61 howto |= howto_names[i].mask;
68 opts = strchr(kargs, '-');
69 while (opts != NULL) {
70 while (*(++opts) != '\0') {
71 sw = strchr(howto_switches, *opts);
74 howto |= howto_masks[sw - howto_switches];
76 opts = strchr(opts, '-');
83 * Copy the environment into the load area starting at (addr).
84 * Each variable is formatted as <name>=<value>, with a single nul
85 * separating each variable, and a double nul terminating the environment.
88 bi_copyenv(vm_offset_t start)
91 vm_offset_t addr, last;
96 /* Traverse the environment. */
97 for (ep = environ; ep != NULL; ep = ep->ev_next) {
98 len = strlen(ep->ev_name);
99 if (ia64_copyin(ep->ev_name, addr, len) != len)
102 if (ia64_copyin("=", addr, 1) != 1)
105 if (ep->ev_value != NULL) {
106 len = strlen(ep->ev_value);
107 if (ia64_copyin(ep->ev_value, addr, len) != len)
111 if (ia64_copyin("", addr, 1) != 1)
116 if (ia64_copyin("", last++, 1) != 1)
122 * Copy module-related data into the load area, where it can be
123 * used as a directory for loaded modules.
125 * Module data is presented in a self-describing format. Each datum
126 * is preceded by a 32-bit identifier and a 32-bit size field.
128 * Currently, the following data are saved:
130 * MOD_NAME (variable) module name (string)
131 * MOD_TYPE (variable) module type (string)
132 * MOD_ARGS (variable) module parameters (string)
133 * MOD_ADDR sizeof(vm_offset_t) module load address
134 * MOD_SIZE sizeof(size_t) module size
135 * MOD_METADATA (variable) type-specific metadata
137 #define COPY32(v, a) { \
139 ia64_copyin(&x, a, sizeof(x)); \
143 #define MOD_STR(t, a, s) { \
145 COPY32(strlen(s) + 1, a); \
146 ia64_copyin(s, a, strlen(s) + 1); \
147 a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
150 #define MOD_NAME(a, s) MOD_STR(MODINFO_NAME, a, s)
151 #define MOD_TYPE(a, s) MOD_STR(MODINFO_TYPE, a, s)
152 #define MOD_ARGS(a, s) MOD_STR(MODINFO_ARGS, a, s)
154 #define MOD_VAR(t, a, s) { \
156 COPY32(sizeof(s), a); \
157 ia64_copyin(&s, a, sizeof(s)); \
158 a += roundup(sizeof(s), sizeof(u_int64_t)); \
161 #define MOD_ADDR(a, s) MOD_VAR(MODINFO_ADDR, a, s)
162 #define MOD_SIZE(a, s) MOD_VAR(MODINFO_SIZE, a, s)
164 #define MOD_METADATA(a, mm) { \
165 COPY32(MODINFO_METADATA | mm->md_type, a); \
166 COPY32(mm->md_size, a); \
167 ia64_copyin(mm->md_data, a, mm->md_size); \
168 a += roundup(mm->md_size, sizeof(u_int64_t));\
171 #define MOD_END(a) { \
172 COPY32(MODINFO_END, a); \
177 bi_copymodules(vm_offset_t addr)
179 struct preloaded_file *fp;
180 struct file_metadata *md;
182 /* Start with the first module on the list, should be the kernel. */
183 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
184 /* The name field must come first. */
185 MOD_NAME(addr, fp->f_name);
186 MOD_TYPE(addr, fp->f_type);
188 MOD_ARGS(addr, fp->f_args);
189 MOD_ADDR(addr, fp->f_addr);
190 MOD_SIZE(addr, fp->f_size);
191 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
192 if (!(md->md_type & MODINFOMD_NOCOPY))
193 MOD_METADATA(addr, md);
201 * Load the information expected by the kernel.
203 * - The kernel environment is copied into kernel space.
204 * - Module metadata are formatted and placed in kernel space.
207 ia64_bootinfo(struct preloaded_file *fp, struct bootinfo **res)
210 struct preloaded_file *xp;
211 struct file_metadata *md;
212 struct devdesc *rootdev;
214 vm_offset_t addr, ssym, esym;
218 bzero(&bi, sizeof(struct bootinfo));
219 bi.bi_magic = BOOTINFO_MAGIC;
221 bi.bi_boothowto = bi_getboothowto(fp->f_args);
224 * Allow the environment variable 'rootdev' to override the supplied
225 * device. This should perhaps go to MI code and/or have $rootdev
226 * tested/set by MI code before launching the kernel.
228 rootdevname = getenv("rootdev");
229 ia64_getdev((void**)&rootdev, rootdevname, NULL);
230 if (rootdev != NULL) {
231 /* Try reading /etc/fstab to select the root device. */
232 getrootmount(ia64_fmtdev(rootdev));
236 md = file_findmetadata(fp, MODINFOMD_SSYM);
237 ssym = (md != NULL) ? *((vm_offset_t *)&(md->md_data)) : 0;
238 md = file_findmetadata(fp, MODINFOMD_ESYM);
239 esym = (md != NULL) ? *((vm_offset_t *)&(md->md_data)) : 0;
240 if (ssym != 0 && esym != 0) {
242 bi.bi_esymtab = esym;
245 /* Find the last module in the chain. */
247 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
248 if (addr < (xp->f_addr + xp->f_size))
249 addr = xp->f_addr + xp->f_size;
252 addr = (addr + 15) & ~15;
254 /* Copy module list and metadata. */
255 bi.bi_modulep = addr;
256 addr = bi_copymodules(addr);
257 if (addr <= bi.bi_modulep) {
258 addr = bi.bi_modulep;
262 addr = (addr + 15) & ~15;
264 /* Copy our environment. */
266 addr = bi_copyenv(addr);
267 if (addr <= bi.bi_envp) {
272 addr = (addr + 15) & ~15;
273 bi.bi_kernend = addr;
275 error = ia64_platform_bootinfo(&bi, res);
279 if (IS_LEGACY_KERNEL()) {
283 bcopy(&bi, *res, sizeof(bi));
287 bi.bi_pbvm_pgtbl = (uintptr_t)ia64_pgtbl;
288 bi.bi_pbvm_pgtblsz = ia64_pgtblsz;
289 ia64_copyin((void *)bi.bi_memmap, addr, bi.bi_memmap_size);
291 addr = (addr + bi.bi_memmap_size + 15) & ~15;
292 bi.bi_kernend = addr + sizeof(bi);
293 ia64_copyin(&bi, addr, sizeof(bi));