2 * Copyright (c) 2004 Marcel Moolenaar
3 * Copyright (c) 2001 Doug Rabson
4 * Copyright (c) 2016 The FreeBSD Foundation
7 * Portions of this software were developed by Konstantin Belousov
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/linker.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/clock.h>
44 #include <sys/rwlock.h>
45 #include <sys/sched.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/vmmeter.h>
50 #include <machine/fpu.h>
51 #include <machine/efi.h>
52 #include <machine/metadata.h>
53 #include <machine/vmparam.h>
57 #include <vm/vm_map.h>
59 static struct efi_systbl *efi_systbl;
60 static struct efi_cfgtbl *efi_cfgtbl;
61 static struct efi_rt *efi_runtime;
63 static int efi_status2err[25] = {
65 ENOEXEC, /* EFI_LOAD_ERROR */
66 EINVAL, /* EFI_INVALID_PARAMETER */
67 ENOSYS, /* EFI_UNSUPPORTED */
68 EMSGSIZE, /* EFI_BAD_BUFFER_SIZE */
69 EOVERFLOW, /* EFI_BUFFER_TOO_SMALL */
70 EBUSY, /* EFI_NOT_READY */
71 EIO, /* EFI_DEVICE_ERROR */
72 EROFS, /* EFI_WRITE_PROTECTED */
73 EAGAIN, /* EFI_OUT_OF_RESOURCES */
74 EIO, /* EFI_VOLUME_CORRUPTED */
75 ENOSPC, /* EFI_VOLUME_FULL */
76 ENXIO, /* EFI_NO_MEDIA */
77 ESTALE, /* EFI_MEDIA_CHANGED */
78 ENOENT, /* EFI_NOT_FOUND */
79 EACCES, /* EFI_ACCESS_DENIED */
80 ETIMEDOUT, /* EFI_NO_RESPONSE */
81 EADDRNOTAVAIL, /* EFI_NO_MAPPING */
82 ETIMEDOUT, /* EFI_TIMEOUT */
83 EDOOFUS, /* EFI_NOT_STARTED */
84 EALREADY, /* EFI_ALREADY_STARTED */
85 ECANCELED, /* EFI_ABORTED */
86 EPROTO, /* EFI_ICMP_ERROR */
87 EPROTO, /* EFI_TFTP_ERROR */
88 EPROTO /* EFI_PROTOCOL_ERROR */
92 efi_status_to_errno(efi_status status)
96 code = status & 0x3ffffffffffffffful;
97 return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
100 static struct mtx efi_lock;
105 struct efi_map_header *efihdr;
110 mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
112 if (efi_systbl_phys == 0) {
114 printf("EFI systbl not available\n");
117 if (!PMAP_HAS_DMAP) {
119 printf("EFI systbl requires direct map\n");
122 efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys);
123 if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
126 printf("EFI systbl signature invalid\n");
129 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
130 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
131 if (efi_cfgtbl == NULL) {
133 printf("EFI config table is not present\n");
136 kmdp = preload_search_by_type("elf kernel");
138 kmdp = preload_search_by_type("elf64 kernel");
139 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
140 MODINFO_METADATA | MODINFOMD_EFI_MAP);
141 if (efihdr == NULL) {
143 printf("EFI map is not present\n");
146 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
147 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
148 if (efihdr->descriptor_size == 0)
151 if (!efi_create_1t1_map(map, efihdr->memory_size /
152 efihdr->descriptor_size, efihdr->descriptor_size)) {
154 printf("EFI cannot create runtime map\n");
158 efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
159 (struct efi_rt *)efi_systbl->st_rt;
160 if (efi_runtime == NULL) {
162 printf("EFI runtime services table is not present\n");
163 efi_destroy_1t1_map();
174 efi_destroy_1t1_map();
180 mtx_destroy(&efi_lock);
187 if (efi_runtime == NULL)
199 if (efi_runtime == NULL)
202 curpmap = &td->td_proc->p_vmspace->vm_pmap;
205 error = fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
207 PMAP_UNLOCK(curpmap);
211 return (efi_arch_enter());
222 curpmap = &curproc->p_vmspace->vm_pmap;
224 fpu_kern_leave(td, NULL);
225 mtx_unlock(&efi_lock);
226 PMAP_UNLOCK(curpmap);
230 efi_get_table(struct uuid *uuid, void **ptr)
232 struct efi_cfgtbl *ct;
235 if (efi_cfgtbl == NULL || efi_systbl == NULL)
237 count = efi_systbl->st_entries;
240 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
241 *ptr = (void *)PHYS_TO_DMAP(ct->ct_data);
250 efi_get_time_locked(struct efi_tm *tm)
259 status = efi_runtime->rt_gettime(tm, NULL);
261 error = efi_status_to_errno(status);
266 efi_get_time(struct efi_tm *tm)
270 if (efi_runtime == NULL)
273 error = efi_get_time_locked(tm);
279 efi_reset_system(void)
286 efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL);
292 efi_set_time_locked(struct efi_tm *tm)
301 status = efi_runtime->rt_settime(tm);
303 error = efi_status_to_errno(status);
308 efi_set_time(struct efi_tm *tm)
312 if (efi_runtime == NULL)
315 error = efi_set_time_locked(tm);
321 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
322 size_t *datasize, void *data)
330 status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data);
332 error = efi_status_to_errno(status);
337 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
345 status = efi_runtime->rt_scanvar(namesize, name, vendor);
347 error = efi_status_to_errno(status);
352 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
353 size_t datasize, void *data)
361 status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data);
363 error = efi_status_to_errno(status);
368 efirt_modevents(module_t m, int event, void *arg __unused)
387 static moduledata_t efirt_moddata = {
389 .evhand = efirt_modevents,
392 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_VM_CONF, SI_ORDER_ANY);
393 MODULE_VERSION(efirt, 1);