2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
27 * $DragonFly: src/sys/boot/efi/libefi/bootinfo.c,v 1.1 2003/11/10 06:08:32 dillon Exp $
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>
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.
54 {"boot_askname", RB_ASKNAME
},
55 {"boot_cdrom", RB_CDROM
},
56 {"boot_userconfig", RB_CONFIG
},
59 {"boot_single", RB_SINGLE
},
60 {"boot_verbose", RB_VERBOSE
},
61 {"boot_multicons", RB_MULTIPLE
},
62 {"boot_serial", RB_SERIAL
},
66 extern char *efi_fmtdev(void *vdev
);
69 bi_getboothowto(char *kargs
)
82 if (!active
&& (*cp
== '-')) {
111 howto
|= RB_DFLTROOT
;
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"))
132 if (!strcmp(getenv("console"), "nullconsole"))
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.
143 bi_copyenv(vm_offset_t addr
)
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);
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);
160 efi_copyin("", addr
, 1);
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) { \
183 efi_copyin(&x, a, sizeof(x)); \
187 #define MOD_STR(t, a, s) { \
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) { \
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); \
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
);
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
);
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
,
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
;
263 * Version 1 bootinfo.
265 bi
->bi_magic
= BOOTINFO_MAGIC
;
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");
290 /* Try reading the /etc/fstab file to select the root device */
291 getrootmount(efi_fmtdev((void *)rootdev
));
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 */
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 */
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");