1 /* $NetBSD: bootinfo.c,v 1.2 2006/04/22 07:58:53 cherry Exp $ */
4 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/bootinfo.c,v 1.10 2004/01/04 23:28:16 obrien Exp $"); */
32 #include <lib/libsa/stand.h>
33 #include <lib/libsa/loadfile.h>
35 #include <sys/param.h>
36 #include <sys/reboot.h>
37 #include <sys/boot_flag.h>
38 #include <sys/exec_elf.h>
41 #include <machine/vmparam.h>
42 #include <machine/elf_machdep.h>
43 #include <machine/bootinfo.h>
49 #include "bootstrap.h"
51 static EFI_GUID hcdp
= HCDP_TABLE_GUID
;
54 * Return a 'boothowto' value corresponding to the kernel arguments in
55 * (kargs) and any relevant environment variables.
62 {"boot_askname", RB_ASKNAME
},
63 {"boot_single", RB_SINGLE
},
64 {"boot_nosync", RB_NOSYNC
},
65 {"boot_halt", RB_HALT
},
67 {"boot_rdonly", RB_RDONLY
},
68 {"boot_dump", RB_DUMP
},
69 {"boot_miniroot", RB_MINIROOT
},
70 {"boot_userconf", RB_USERCONF
},
74 extern char *efi_fmtdev(void *vdev
);
77 bi_getboothowto(char *kargs
)
90 if (!active
&& (*cp
== '-')) {
94 BOOT_FLAG(*cp
, howto
);
99 /* get equivalents from the environment */
100 for (i
= 0; howto_names
[i
].ev
!= NULL
; i
++)
101 if (getenv(howto_names
[i
].ev
) != NULL
)
102 howto
|= howto_names
[i
].mask
;
107 * Copy the environment into the load area starting at (addr).
108 * Each variable is formatted as <name>=<value>, with a single nul
109 * separating each variable, and a double nul terminating the environment.
112 bi_copyenv(vaddr_t addr
)
116 /* traverse the environment */
117 for (ep
= environ
; ep
!= NULL
; ep
= ep
->ev_next
) {
118 efi_copyin(ep
->ev_name
, addr
, strlen(ep
->ev_name
));
119 addr
+= strlen(ep
->ev_name
);
120 efi_copyin("=", addr
, 1);
122 if (ep
->ev_value
!= NULL
) {
123 efi_copyin(ep
->ev_value
, addr
, strlen(ep
->ev_value
));
124 addr
+= strlen(ep
->ev_value
);
126 efi_copyin("", addr
, 1);
129 efi_copyin("", addr
, 1);
135 * Copy module-related data into the load area, where it can be
136 * used as a directory for loaded modules.
138 * Module data is presented in a self-describing format. Each datum
139 * is preceded by a 32-bit identifier and a 32-bit size field.
141 * Currently, the following data are saved:
143 * MOD_NAME (variable) module name (string)
144 * MOD_TYPE (variable) module type (string)
145 * MOD_ARGS (variable) module parameters (string)
146 * MOD_ADDR sizeof(vm_offset_t) module load address
147 * MOD_SIZE sizeof(size_t) module size
148 * MOD_METADATA (variable) type-specific metadata
150 #define COPY32(v, a) { \
152 efi_copyin(&x, a, sizeof(x)); \
156 #define MOD_STR(t, a, s) { \
158 COPY32(strlen(s) + 1, a); \
159 efi_copyin(s, a, strlen(s) + 1); \
160 a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
163 #define MOD_NAME(a, s) MOD_STR(MODINFO_NAME, a, s)
164 #define MOD_TYPE(a, s) MOD_STR(MODINFO_TYPE, a, s)
165 #define MOD_ARGS(a, s) MOD_STR(MODINFO_ARGS, a, s)
167 #define MOD_VAR(t, a, s) { \
169 COPY32(sizeof(s), a); \
170 efi_copyin(&s, a, sizeof(s)); \
171 a += roundup(sizeof(s), sizeof(u_int64_t)); \
174 #define MOD_ADDR(a, s) MOD_VAR(MODINFO_ADDR, a, s)
175 #define MOD_SIZE(a, s) MOD_VAR(MODINFO_SIZE, a, s)
177 #define MOD_METADATA(a, mm) { \
178 COPY32(MODINFO_METADATA | mm->md_type, a); \
179 COPY32(mm->md_size, a); \
180 efi_copyin(mm->md_data, a, mm->md_size); \
181 a += roundup(mm->md_size, sizeof(u_int64_t));\
184 #define MOD_END(a) { \
185 COPY32(MODINFO_END, a); \
190 * Load the information expected by the kernel.
192 * - The kernel environment is copied into kernel space.
193 * - Module metadata are formatted and placed in kernel space.
196 bi_load(struct bootinfo
*bi
, struct preloaded_file
*fp
, UINTN
*mapkey
,
200 struct efi_devdesc
*rootdev
;
201 struct preloaded_file
*xp
;
202 vaddr_t addr
, bootinfo_addr
;
204 struct file_metadata
*md
;
209 * Version 1 bootinfo.
211 bi
->bi_magic
= BOOTINFO_MAGIC
;
215 * Calculate boothowto.
217 bi
->bi_boothowto
= bi_getboothowto(fp
->f_args
);
220 * Stash EFI System Table.
222 bi
->bi_systab
= (u_int64_t
) ST
;
225 * Allow the environment variable 'rootdev' to override the supplied
226 * device. This should perhaps go to MI code and/or have $rootdev
227 * tested/set by MI code before launching the kernel.
229 rootdevname
= getenv("rootdev");
230 efi_getdev((void **)(&rootdev
), rootdevname
, NULL
);
231 if (rootdev
== NULL
) { /* bad $rootdev/$currdev */
232 printf("can't determine root device\n");
236 /* Try reading the /etc/fstab file to select the root device */
237 getrootmount(efi_fmtdev((void *)rootdev
));
242 ssym
= fp
->marks
[MARK_SYM
];
243 esym
= fp
->marks
[MARK_END
];
245 if (ssym
== 0 || esym
== 0)
246 ssym
= esym
= 0; /* sanity */
248 bi
->bi_symtab
= ssym
;
249 bi
->bi_esymtab
= esym
;
251 bi
->bi_hcdp
= (uint64_t)efi_get_table(&hcdp
); /* DIG64 HCDP table addr. */
252 fpswa_init(&bi
->bi_fpswa
); /* find FPSWA interface */
254 /* find the last module in the chain */
256 for (xp
= file_findfile(NULL
, NULL
); xp
!= NULL
; xp
= xp
->f_next
) {
257 if (addr
< (xp
->f_addr
+ xp
->f_size
))
258 addr
= xp
->f_addr
+ xp
->f_size
;
261 /* pad to a page boundary */
262 addr
= (addr
+ PAGE_MASK
) & ~PAGE_MASK
;
264 /* copy our environment */
266 addr
= bi_copyenv(addr
);
268 /* pad to a page boundary */
269 addr
= (addr
+ PAGE_MASK
) & ~PAGE_MASK
;
271 /* all done copying stuff in, save end of loaded object space */
272 bi
->bi_kernend
= addr
;
275 * Read the memory map and stash it after bootinfo. Align the memory map
276 * on a 16-byte boundary (the bootinfo block is page aligned).
278 bisz
= (sizeof(struct bootinfo
) + 0x0f) & ~0x0f;
279 bi
->bi_memmap
= ((u_int64_t
)bi
) + bisz
;
280 bi
->bi_memmap_size
= EFI_PAGE_SIZE
* pages
- bisz
;
281 status
= BS
->GetMemoryMap(&bi
->bi_memmap_size
,
282 (EFI_MEMORY_DESCRIPTOR
*)bi
->bi_memmap
, &key
,
283 &bi
->bi_memdesc_size
, &bi
->bi_memdesc_version
);
284 if (EFI_ERROR(status
)) {
285 printf("bi_load: Can't read memory map\n");