bootinfo.c revision 1.2 1 1.2 cherry /* $NetBSD: bootinfo.c,v 1.2 2006/04/22 07:58:53 cherry Exp $ */
2 1.1 cherry
3 1.1 cherry /*-
4 1.1 cherry * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
5 1.1 cherry * All rights reserved.
6 1.1 cherry *
7 1.1 cherry * Redistribution and use in source and binary forms, with or without
8 1.1 cherry * modification, are permitted provided that the following conditions
9 1.1 cherry * are met:
10 1.1 cherry * 1. Redistributions of source code must retain the above copyright
11 1.1 cherry * notice, this list of conditions and the following disclaimer.
12 1.1 cherry * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cherry * notice, this list of conditions and the following disclaimer in the
14 1.1 cherry * documentation and/or other materials provided with the distribution.
15 1.1 cherry *
16 1.1 cherry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 cherry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 cherry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 cherry * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 cherry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 cherry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 cherry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 cherry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 cherry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 cherry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 cherry * SUCH DAMAGE.
27 1.1 cherry */
28 1.1 cherry
29 1.1 cherry #include <sys/cdefs.h>
30 1.2 cherry /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/bootinfo.c,v 1.10 2004/01/04 23:28:16 obrien Exp $"); */
31 1.1 cherry
32 1.1 cherry #include <lib/libsa/stand.h>
33 1.1 cherry #include <lib/libsa/loadfile.h>
34 1.1 cherry
35 1.1 cherry #include <sys/param.h>
36 1.1 cherry #include <sys/reboot.h>
37 1.1 cherry #include <sys/boot_flag.h>
38 1.1 cherry #include <sys/exec_elf.h>
39 1.1 cherry #include <sys/lock.h>
40 1.1 cherry
41 1.1 cherry #include <machine/vmparam.h>
42 1.1 cherry #include <machine/elf_machdep.h>
43 1.1 cherry #include <machine/bootinfo.h>
44 1.1 cherry
45 1.1 cherry
46 1.1 cherry #include <efi.h>
47 1.1 cherry #include <efilib.h>
48 1.1 cherry
49 1.1 cherry #include "bootstrap.h"
50 1.1 cherry
51 1.1 cherry static EFI_GUID hcdp = HCDP_TABLE_GUID;
52 1.1 cherry
53 1.1 cherry /*
54 1.1 cherry * Return a 'boothowto' value corresponding to the kernel arguments in
55 1.1 cherry * (kargs) and any relevant environment variables.
56 1.1 cherry */
57 1.1 cherry static struct
58 1.1 cherry {
59 1.1 cherry const char *ev;
60 1.1 cherry int mask;
61 1.1 cherry } howto_names[] = {
62 1.1 cherry {"boot_askname", RB_ASKNAME},
63 1.1 cherry {"boot_single", RB_SINGLE},
64 1.1 cherry {"boot_nosync", RB_NOSYNC},
65 1.1 cherry {"boot_halt", RB_HALT},
66 1.1 cherry {"boot_kdb", RB_KDB},
67 1.1 cherry {"boot_rdonly", RB_RDONLY},
68 1.1 cherry {"boot_dump", RB_DUMP},
69 1.1 cherry {"boot_miniroot", RB_MINIROOT},
70 1.1 cherry {"boot_userconf", RB_USERCONF},
71 1.1 cherry {NULL, 0}
72 1.1 cherry };
73 1.1 cherry
74 1.1 cherry extern char *efi_fmtdev(void *vdev);
75 1.1 cherry
76 1.1 cherry int
77 1.1 cherry bi_getboothowto(char *kargs)
78 1.1 cherry {
79 1.1 cherry char *cp;
80 1.1 cherry int howto;
81 1.1 cherry int active;
82 1.1 cherry int i;
83 1.1 cherry
84 1.1 cherry /* Parse kargs */
85 1.1 cherry howto = 0;
86 1.1 cherry if (kargs != NULL) {
87 1.1 cherry cp = kargs;
88 1.1 cherry active = 0;
89 1.1 cherry while (*cp != 0) {
90 1.1 cherry if (!active && (*cp == '-')) {
91 1.1 cherry active = 1;
92 1.1 cherry } else if (active)
93 1.1 cherry
94 1.1 cherry BOOT_FLAG(*cp, howto);
95 1.1 cherry
96 1.1 cherry cp++;
97 1.1 cherry }
98 1.1 cherry }
99 1.1 cherry /* get equivalents from the environment */
100 1.1 cherry for (i = 0; howto_names[i].ev != NULL; i++)
101 1.1 cherry if (getenv(howto_names[i].ev) != NULL)
102 1.1 cherry howto |= howto_names[i].mask;
103 1.1 cherry return(howto);
104 1.1 cherry }
105 1.1 cherry
106 1.1 cherry /*
107 1.1 cherry * Copy the environment into the load area starting at (addr).
108 1.1 cherry * Each variable is formatted as <name>=<value>, with a single nul
109 1.1 cherry * separating each variable, and a double nul terminating the environment.
110 1.1 cherry */
111 1.1 cherry vaddr_t
112 1.1 cherry bi_copyenv(vaddr_t addr)
113 1.1 cherry {
114 1.1 cherry struct env_var *ep;
115 1.1 cherry
116 1.1 cherry /* traverse the environment */
117 1.1 cherry for (ep = environ; ep != NULL; ep = ep->ev_next) {
118 1.1 cherry efi_copyin(ep->ev_name, addr, strlen(ep->ev_name));
119 1.1 cherry addr += strlen(ep->ev_name);
120 1.1 cherry efi_copyin("=", addr, 1);
121 1.1 cherry addr++;
122 1.1 cherry if (ep->ev_value != NULL) {
123 1.1 cherry efi_copyin(ep->ev_value, addr, strlen(ep->ev_value));
124 1.1 cherry addr += strlen(ep->ev_value);
125 1.1 cherry }
126 1.1 cherry efi_copyin("", addr, 1);
127 1.1 cherry addr++;
128 1.1 cherry }
129 1.1 cherry efi_copyin("", addr, 1);
130 1.1 cherry addr++;
131 1.1 cherry return(addr);
132 1.1 cherry }
133 1.1 cherry
134 1.1 cherry /*
135 1.1 cherry * Copy module-related data into the load area, where it can be
136 1.1 cherry * used as a directory for loaded modules.
137 1.1 cherry *
138 1.1 cherry * Module data is presented in a self-describing format. Each datum
139 1.1 cherry * is preceded by a 32-bit identifier and a 32-bit size field.
140 1.1 cherry *
141 1.1 cherry * Currently, the following data are saved:
142 1.1 cherry *
143 1.1 cherry * MOD_NAME (variable) module name (string)
144 1.1 cherry * MOD_TYPE (variable) module type (string)
145 1.1 cherry * MOD_ARGS (variable) module parameters (string)
146 1.1 cherry * MOD_ADDR sizeof(vm_offset_t) module load address
147 1.1 cherry * MOD_SIZE sizeof(size_t) module size
148 1.1 cherry * MOD_METADATA (variable) type-specific metadata
149 1.1 cherry */
150 1.1 cherry #define COPY32(v, a) { \
151 1.1 cherry u_int32_t x = (v); \
152 1.1 cherry efi_copyin(&x, a, sizeof(x)); \
153 1.1 cherry a += sizeof(x); \
154 1.1 cherry }
155 1.1 cherry
156 1.1 cherry #define MOD_STR(t, a, s) { \
157 1.1 cherry COPY32(t, a); \
158 1.1 cherry COPY32(strlen(s) + 1, a); \
159 1.1 cherry efi_copyin(s, a, strlen(s) + 1); \
160 1.1 cherry a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
161 1.1 cherry }
162 1.1 cherry
163 1.1 cherry #define MOD_NAME(a, s) MOD_STR(MODINFO_NAME, a, s)
164 1.1 cherry #define MOD_TYPE(a, s) MOD_STR(MODINFO_TYPE, a, s)
165 1.1 cherry #define MOD_ARGS(a, s) MOD_STR(MODINFO_ARGS, a, s)
166 1.1 cherry
167 1.1 cherry #define MOD_VAR(t, a, s) { \
168 1.1 cherry COPY32(t, a); \
169 1.1 cherry COPY32(sizeof(s), a); \
170 1.1 cherry efi_copyin(&s, a, sizeof(s)); \
171 1.1 cherry a += roundup(sizeof(s), sizeof(u_int64_t)); \
172 1.1 cherry }
173 1.1 cherry
174 1.1 cherry #define MOD_ADDR(a, s) MOD_VAR(MODINFO_ADDR, a, s)
175 1.1 cherry #define MOD_SIZE(a, s) MOD_VAR(MODINFO_SIZE, a, s)
176 1.1 cherry
177 1.1 cherry #define MOD_METADATA(a, mm) { \
178 1.1 cherry COPY32(MODINFO_METADATA | mm->md_type, a); \
179 1.1 cherry COPY32(mm->md_size, a); \
180 1.1 cherry efi_copyin(mm->md_data, a, mm->md_size); \
181 1.1 cherry a += roundup(mm->md_size, sizeof(u_int64_t));\
182 1.1 cherry }
183 1.1 cherry
184 1.1 cherry #define MOD_END(a) { \
185 1.1 cherry COPY32(MODINFO_END, a); \
186 1.1 cherry COPY32(0, a); \
187 1.1 cherry }
188 1.1 cherry
189 1.1 cherry /*
190 1.1 cherry * Load the information expected by an alpha kernel.
191 1.1 cherry *
192 1.1 cherry * - The kernel environment is copied into kernel space.
193 1.1 cherry * - Module metadata are formatted and placed in kernel space.
194 1.1 cherry */
195 1.1 cherry int
196 1.1 cherry bi_load(struct bootinfo *bi, struct preloaded_file *fp, UINTN *mapkey,
197 1.1 cherry UINTN pages)
198 1.1 cherry {
199 1.1 cherry char *rootdevname;
200 1.1 cherry struct efi_devdesc *rootdev;
201 1.1 cherry struct preloaded_file *xp;
202 1.1 cherry vaddr_t addr, bootinfo_addr;
203 1.1 cherry vaddr_t ssym, esym;
204 1.1 cherry struct file_metadata *md;
205 1.1 cherry EFI_STATUS status;
206 1.1 cherry UINTN bisz, key;
207 1.1 cherry
208 1.1 cherry /*
209 1.1 cherry * Version 1 bootinfo.
210 1.1 cherry */
211 1.1 cherry bi->bi_magic = BOOTINFO_MAGIC;
212 1.1 cherry bi->bi_version = 1;
213 1.1 cherry
214 1.1 cherry /*
215 1.1 cherry * Calculate boothowto.
216 1.1 cherry */
217 1.1 cherry bi->bi_boothowto = bi_getboothowto(fp->f_args);
218 1.1 cherry
219 1.1 cherry /*
220 1.1 cherry * Stash EFI System Table.
221 1.1 cherry */
222 1.1 cherry bi->bi_systab = (u_int64_t) ST;
223 1.1 cherry
224 1.1 cherry /*
225 1.1 cherry * Allow the environment variable 'rootdev' to override the supplied
226 1.1 cherry * device. This should perhaps go to MI code and/or have $rootdev
227 1.1 cherry * tested/set by MI code before launching the kernel.
228 1.1 cherry */
229 1.1 cherry rootdevname = getenv("rootdev");
230 1.1 cherry efi_getdev((void **)(&rootdev), rootdevname, NULL);
231 1.1 cherry if (rootdev == NULL) { /* bad $rootdev/$currdev */
232 1.1 cherry printf("can't determine root device\n");
233 1.1 cherry return(EINVAL);
234 1.1 cherry }
235 1.1 cherry
236 1.1 cherry /* Try reading the /etc/fstab file to select the root device */
237 1.1 cherry getrootmount(efi_fmtdev((void *)rootdev));
238 1.1 cherry free(rootdev);
239 1.1 cherry
240 1.1 cherry ssym = esym = 0;
241 1.1 cherry
242 1.1 cherry ssym = fp->marks[MARK_SYM];
243 1.1 cherry esym = fp->marks[MARK_END];
244 1.1 cherry
245 1.1 cherry if (ssym == 0 || esym == 0)
246 1.1 cherry ssym = esym = 0; /* sanity */
247 1.1 cherry
248 1.1 cherry bi->bi_symtab = ssym;
249 1.1 cherry bi->bi_esymtab = esym;
250 1.1 cherry
251 1.1 cherry bi->bi_hcdp = (uint64_t)efi_get_table(&hcdp); /* DIG64 HCDP table addr. */
252 1.1 cherry fpswa_init(&bi->bi_fpswa); /* find FPSWA interface */
253 1.1 cherry
254 1.1 cherry /* find the last module in the chain */
255 1.1 cherry addr = 0;
256 1.1 cherry for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
257 1.1 cherry if (addr < (xp->f_addr + xp->f_size))
258 1.1 cherry addr = xp->f_addr + xp->f_size;
259 1.1 cherry }
260 1.1 cherry
261 1.1 cherry /* pad to a page boundary */
262 1.1 cherry addr = (addr + PAGE_MASK) & ~PAGE_MASK;
263 1.1 cherry
264 1.1 cherry /* copy our environment */
265 1.1 cherry bi->bi_envp = addr;
266 1.1 cherry addr = bi_copyenv(addr);
267 1.1 cherry
268 1.1 cherry /* pad to a page boundary */
269 1.1 cherry addr = (addr + PAGE_MASK) & ~PAGE_MASK;
270 1.1 cherry
271 1.1 cherry /* all done copying stuff in, save end of loaded object space */
272 1.1 cherry bi->bi_kernend = addr;
273 1.1 cherry
274 1.1 cherry /*
275 1.1 cherry * Read the memory map and stash it after bootinfo. Align the memory map
276 1.1 cherry * on a 16-byte boundary (the bootinfo block is page aligned).
277 1.1 cherry */
278 1.1 cherry bisz = (sizeof(struct bootinfo) + 0x0f) & ~0x0f;
279 1.1 cherry bi->bi_memmap = ((u_int64_t)bi) + bisz;
280 1.1 cherry bi->bi_memmap_size = EFI_PAGE_SIZE * pages - bisz;
281 1.1 cherry status = BS->GetMemoryMap(&bi->bi_memmap_size,
282 1.1 cherry (EFI_MEMORY_DESCRIPTOR *)bi->bi_memmap, &key,
283 1.1 cherry &bi->bi_memdesc_size, &bi->bi_memdesc_version);
284 1.1 cherry if (EFI_ERROR(status)) {
285 1.1 cherry printf("bi_load: Can't read memory map\n");
286 1.1 cherry return EINVAL;
287 1.1 cherry }
288 1.1 cherry *mapkey = key;
289 1.1 cherry
290 1.1 cherry return(0);
291 1.1 cherry }
292