HAMMER 59C/Many: Stabilization pass - fixes for large file issues
[dragonfly.git] / sys / boot / efi / libefi / bootinfo.c
bloba1ea248e8806ae2a94078d7565d7e0e24a849daa
1 /*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $FreeBSD$
27 * $DragonFly: src/sys/boot/efi/libefi/bootinfo.c,v 1.1 2003/11/10 06:08:32 dillon Exp $
30 #include <stand.h>
31 #include <string.h>
32 #include <sys/param.h>
33 #include <sys/reboot.h>
34 #include <sys/linker.h>
35 #include <machine/elf.h>
36 #include <machine/bootinfo.h>
38 #include <efi.h>
39 #include <efilib.h>
41 #include "bootstrap.h"
43 static EFI_GUID hcdp = HCDP_TABLE_GUID;
46 * Return a 'boothowto' value corresponding to the kernel arguments in
47 * (kargs) and any relevant environment variables.
49 static struct
51 const char *ev;
52 int mask;
53 } howto_names[] = {
54 {"boot_askname", RB_ASKNAME},
55 {"boot_cdrom", RB_CDROM},
56 {"boot_userconfig", RB_CONFIG},
57 {"boot_ddb", RB_KDB},
58 {"boot_gdb", RB_GDB},
59 {"boot_single", RB_SINGLE},
60 {"boot_verbose", RB_VERBOSE},
61 {"boot_multicons", RB_MULTIPLE},
62 {"boot_serial", RB_SERIAL},
63 {NULL, 0}
66 extern char *efi_fmtdev(void *vdev);
68 int
69 bi_getboothowto(char *kargs)
71 char *cp;
72 int howto;
73 int active;
74 int i;
76 /* Parse kargs */
77 howto = 0;
78 if (kargs != NULL) {
79 cp = kargs;
80 active = 0;
81 while (*cp != 0) {
82 if (!active && (*cp == '-')) {
83 active = 1;
84 } else if (active)
85 switch (*cp) {
86 case 'a':
87 howto |= RB_ASKNAME;
88 break;
89 case 'c':
90 howto |= RB_CONFIG;
91 break;
92 case 'C':
93 howto |= RB_CDROM;
94 break;
95 case 'd':
96 howto |= RB_KDB;
97 break;
98 case 'D':
99 howto |= RB_MULTIPLE;
100 break;
101 case 'm':
102 howto |= RB_MUTE;
103 break;
104 case 'g':
105 howto |= RB_GDB;
106 break;
107 case 'h':
108 howto |= RB_SERIAL;
109 break;
110 case 'r':
111 howto |= RB_DFLTROOT;
112 break;
113 case 's':
114 howto |= RB_SINGLE;
115 break;
116 case 'v':
117 howto |= RB_VERBOSE;
118 break;
119 default:
120 active = 0;
121 break;
123 cp++;
126 /* get equivalents from the environment */
127 for (i = 0; howto_names[i].ev != NULL; i++)
128 if (getenv(howto_names[i].ev) != NULL)
129 howto |= howto_names[i].mask;
130 if (!strcmp(getenv("console"), "comconsole"))
131 howto |= RB_SERIAL;
132 if (!strcmp(getenv("console"), "nullconsole"))
133 howto |= RB_MUTE;
134 return(howto);
138 * Copy the environment into the load area starting at (addr).
139 * Each variable is formatted as <name>=<value>, with a single nul
140 * separating each variable, and a double nul terminating the environment.
142 vm_offset_t
143 bi_copyenv(vm_offset_t addr)
145 struct env_var *ep;
147 /* traverse the environment */
148 for (ep = environ; ep != NULL; ep = ep->ev_next) {
149 efi_copyin(ep->ev_name, addr, strlen(ep->ev_name));
150 addr += strlen(ep->ev_name);
151 efi_copyin("=", addr, 1);
152 addr++;
153 if (ep->ev_value != NULL) {
154 efi_copyin(ep->ev_value, addr, strlen(ep->ev_value));
155 addr += strlen(ep->ev_value);
157 efi_copyin("", addr, 1);
158 addr++;
160 efi_copyin("", addr, 1);
161 addr++;
162 return(addr);
166 * Copy module-related data into the load area, where it can be
167 * used as a directory for loaded modules.
169 * Module data is presented in a self-describing format. Each datum
170 * is preceded by a 32-bit identifier and a 32-bit size field.
172 * Currently, the following data are saved:
174 * MOD_NAME (variable) module name (string)
175 * MOD_TYPE (variable) module type (string)
176 * MOD_ARGS (variable) module parameters (string)
177 * MOD_ADDR sizeof(vm_offset_t) module load address
178 * MOD_SIZE sizeof(size_t) module size
179 * MOD_METADATA (variable) type-specific metadata
181 #define COPY32(v, a) { \
182 u_int32_t x = (v); \
183 efi_copyin(&x, a, sizeof(x)); \
184 a += sizeof(x); \
187 #define MOD_STR(t, a, s) { \
188 COPY32(t, a); \
189 COPY32(strlen(s) + 1, a); \
190 efi_copyin(s, a, strlen(s) + 1); \
191 a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
194 #define MOD_NAME(a, s) MOD_STR(MODINFO_NAME, a, s)
195 #define MOD_TYPE(a, s) MOD_STR(MODINFO_TYPE, a, s)
196 #define MOD_ARGS(a, s) MOD_STR(MODINFO_ARGS, a, s)
198 #define MOD_VAR(t, a, s) { \
199 COPY32(t, a); \
200 COPY32(sizeof(s), a); \
201 efi_copyin(&s, a, sizeof(s)); \
202 a += roundup(sizeof(s), sizeof(u_int64_t)); \
205 #define MOD_ADDR(a, s) MOD_VAR(MODINFO_ADDR, a, s)
206 #define MOD_SIZE(a, s) MOD_VAR(MODINFO_SIZE, a, s)
208 #define MOD_METADATA(a, mm) { \
209 COPY32(MODINFO_METADATA | mm->md_type, a); \
210 COPY32(mm->md_size, a); \
211 efi_copyin(mm->md_data, a, mm->md_size); \
212 a += roundup(mm->md_size, sizeof(u_int64_t));\
215 #define MOD_END(a) { \
216 COPY32(MODINFO_END, a); \
217 COPY32(0, a); \
220 vm_offset_t
221 bi_copymodules(vm_offset_t addr)
223 struct preloaded_file *fp;
224 struct file_metadata *md;
226 /* start with the first module on the list, should be the kernel */
227 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
229 MOD_NAME(addr, fp->f_name); /* this field must come first */
230 MOD_TYPE(addr, fp->f_type);
231 if (fp->f_args)
232 MOD_ARGS(addr, fp->f_args);
233 MOD_ADDR(addr, fp->f_addr);
234 MOD_SIZE(addr, fp->f_size);
235 for (md = fp->f_metadata; md != NULL; md = md->md_next)
236 if (!(md->md_type & MODINFOMD_NOCOPY))
237 MOD_METADATA(addr, md);
239 MOD_END(addr);
240 return(addr);
244 * Load the information expected by an alpha kernel.
246 * - The kernel environment is copied into kernel space.
247 * - Module metadata are formatted and placed in kernel space.
250 bi_load(struct bootinfo *bi, struct preloaded_file *fp, UINTN *mapkey,
251 UINTN pages)
253 char *rootdevname;
254 struct efi_devdesc *rootdev;
255 struct preloaded_file *xp;
256 vm_offset_t addr, bootinfo_addr;
257 vm_offset_t ssym, esym;
258 struct file_metadata *md;
259 EFI_STATUS status;
260 UINTN bisz, key;
263 * Version 1 bootinfo.
265 bi->bi_magic = BOOTINFO_MAGIC;
266 bi->bi_version = 1;
269 * Calculate boothowto.
271 bi->bi_boothowto = bi_getboothowto(fp->f_args);
274 * Stash EFI System Table.
276 bi->bi_systab = (u_int64_t) ST;
279 * Allow the environment variable 'rootdev' to override the supplied
280 * device. This should perhaps go to MI code and/or have $rootdev
281 * tested/set by MI code before launching the kernel.
283 rootdevname = getenv("rootdev");
284 efi_getdev((void **)(&rootdev), rootdevname, NULL);
285 if (rootdev == NULL) { /* bad $rootdev/$currdev */
286 printf("can't determine root device\n");
287 return(EINVAL);
290 /* Try reading the /etc/fstab file to select the root device */
291 getrootmount(efi_fmtdev((void *)rootdev));
292 free(rootdev);
294 ssym = esym = 0;
295 if ((md = file_findmetadata(fp, MODINFOMD_SSYM)) != NULL)
296 ssym = *((vm_offset_t *)&(md->md_data));
297 if ((md = file_findmetadata(fp, MODINFOMD_ESYM)) != NULL)
298 esym = *((vm_offset_t *)&(md->md_data));
299 if (ssym == 0 || esym == 0)
300 ssym = esym = 0; /* sanity */
302 bi->bi_symtab = ssym;
303 bi->bi_esymtab = esym;
305 bi->bi_hcdp = (uint64_t)efi_get_table(&hcdp); /* DIG64 HCDP table addr. */
306 fpswa_init(&bi->bi_fpswa); /* find FPSWA interface */
308 /* find the last module in the chain */
309 addr = 0;
310 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
311 if (addr < (xp->f_addr + xp->f_size))
312 addr = xp->f_addr + xp->f_size;
315 /* pad to a page boundary */
316 addr = (addr + PAGE_MASK) & ~PAGE_MASK;
318 /* copy our environment */
319 bi->bi_envp = addr;
320 addr = bi_copyenv(addr);
322 /* pad to a page boundary */
323 addr = (addr + PAGE_MASK) & ~PAGE_MASK;
325 /* copy module list and metadata */
326 bi->bi_modulep = addr;
327 addr = bi_copymodules(addr);
329 /* all done copying stuff in, save end of loaded object space */
330 bi->bi_kernend = addr;
333 * Read the memory map and stash it after bootinfo. Align the memory map
334 * on a 16-byte boundary (the bootinfo block is page aligned).
336 bisz = (sizeof(struct bootinfo) + 0x0f) & ~0x0f;
337 bi->bi_memmap = ((u_int64_t)bi) + bisz;
338 bi->bi_memmap_size = EFI_PAGE_SIZE * pages - bisz;
339 status = BS->GetMemoryMap(&bi->bi_memmap_size,
340 (EFI_MEMORY_DESCRIPTOR *)bi->bi_memmap, &key,
341 &bi->bi_memdesc_size, &bi->bi_memdesc_version);
342 if (EFI_ERROR(status)) {
343 printf("bi_load: Can't read memory map\n");
344 return EINVAL;
346 *mapkey = key;
348 return(0);