2 * Copyright (c) 2004 Marcel Moolenaar
3 * Copyright (c) 2001 Doug Rabson
4 * Copyright (c) 2016, 2018 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;
61 * The following pointers point to tables in the EFI runtime service data pages.
62 * Care should be taken to make sure that we've properly entered the EFI runtime
63 * environment (efi_enter()) before dereferencing them.
65 static struct efi_cfgtbl *efi_cfgtbl;
66 static struct efi_rt *efi_runtime;
68 static int efi_status2err[25] = {
70 ENOEXEC, /* EFI_LOAD_ERROR */
71 EINVAL, /* EFI_INVALID_PARAMETER */
72 ENOSYS, /* EFI_UNSUPPORTED */
73 EMSGSIZE, /* EFI_BAD_BUFFER_SIZE */
74 EOVERFLOW, /* EFI_BUFFER_TOO_SMALL */
75 EBUSY, /* EFI_NOT_READY */
76 EIO, /* EFI_DEVICE_ERROR */
77 EROFS, /* EFI_WRITE_PROTECTED */
78 EAGAIN, /* EFI_OUT_OF_RESOURCES */
79 EIO, /* EFI_VOLUME_CORRUPTED */
80 ENOSPC, /* EFI_VOLUME_FULL */
81 ENXIO, /* EFI_NO_MEDIA */
82 ESTALE, /* EFI_MEDIA_CHANGED */
83 ENOENT, /* EFI_NOT_FOUND */
84 EACCES, /* EFI_ACCESS_DENIED */
85 ETIMEDOUT, /* EFI_NO_RESPONSE */
86 EADDRNOTAVAIL, /* EFI_NO_MAPPING */
87 ETIMEDOUT, /* EFI_TIMEOUT */
88 EDOOFUS, /* EFI_NOT_STARTED */
89 EALREADY, /* EFI_ALREADY_STARTED */
90 ECANCELED, /* EFI_ABORTED */
91 EPROTO, /* EFI_ICMP_ERROR */
92 EPROTO, /* EFI_TFTP_ERROR */
93 EPROTO /* EFI_PROTOCOL_ERROR */
96 static int efi_enter(void);
97 static void efi_leave(void);
100 efi_status_to_errno(efi_status status)
104 code = status & 0x3ffffffffffffffful;
105 return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
108 static struct mtx efi_lock;
111 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr)
116 for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
118 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
121 if (addr >= (uintptr_t)p->md_virt &&
122 addr < (uintptr_t)p->md_virt + p->md_pages * PAGE_SIZE)
132 struct efi_map_header *efihdr;
137 int ndesc, rt_disabled;
140 TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
141 if (rt_disabled == 1)
143 mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
145 if (efi_systbl_phys == 0) {
147 printf("EFI systbl not available\n");
151 efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys);
152 if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
155 printf("EFI systbl signature invalid\n");
158 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
159 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
160 if (efi_cfgtbl == NULL) {
162 printf("EFI config table is not present\n");
165 kmdp = preload_search_by_type("elf kernel");
167 kmdp = preload_search_by_type("elf64 kernel");
168 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
169 MODINFO_METADATA | MODINFOMD_EFI_MAP);
170 if (efihdr == NULL) {
172 printf("EFI map is not present\n");
175 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
176 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
177 if (efihdr->descriptor_size == 0)
180 ndesc = efihdr->memory_size / efihdr->descriptor_size;
181 if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) {
183 printf("EFI cannot create runtime map\n");
187 efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
188 (struct efi_rt *)efi_systbl->st_rt;
189 if (efi_runtime == NULL) {
191 printf("EFI runtime services table is not present\n");
192 efi_destroy_1t1_map();
196 #if defined(__aarch64__) || defined(__amd64__)
198 * Some UEFI implementations have multiple implementations of the
199 * RS->GetTime function. They switch from one we can only use early
200 * in the boot process to one valid as a RunTime service only when we
201 * call RS->SetVirtualAddressMap. As this is not always the case, e.g.
202 * with an old loader.efi, check if the RS->GetTime function is within
203 * the EFI map, and fail to attach if not.
205 rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime);
206 if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size,
207 (vm_offset_t)rtdm->rt_gettime)) {
210 "EFI runtime services table has an invalid pointer\n");
212 efi_destroy_1t1_map();
224 /* Most likely disabled by tunable */
225 if (efi_runtime == NULL)
227 efi_destroy_1t1_map();
233 mtx_destroy(&efi_lock);
240 if (efi_runtime == NULL)
251 if (efi_runtime == NULL)
254 curpmap = &td->td_proc->p_vmspace->vm_pmap;
257 fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
258 return (efi_arch_enter());
269 curpmap = &curproc->p_vmspace->vm_pmap;
271 fpu_kern_leave(td, NULL);
272 mtx_unlock(&efi_lock);
273 PMAP_UNLOCK(curpmap);
277 efi_get_table(struct uuid *uuid, void **ptr)
279 struct efi_cfgtbl *ct;
282 if (efi_cfgtbl == NULL || efi_systbl == NULL)
284 count = efi_systbl->st_entries;
287 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
288 *ptr = (void *)efi_phys_to_kva(ct->ct_data);
296 static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT;
297 SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN,
298 &efi_rt_handle_faults, 0,
299 "Call EFI RT methods with fault handler wrapper around");
302 efi_rt_arch_call_nofault(struct efirt_callinfo *ec)
305 switch (ec->ec_argcnt) {
307 ec->ec_efi_status = ((register_t (*)(void))ec->ec_fptr)();
310 ec->ec_efi_status = ((register_t (*)(register_t))ec->ec_fptr)
314 ec->ec_efi_status = ((register_t (*)(register_t, register_t))
315 ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2);
318 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
319 register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2,
323 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
324 register_t, register_t))ec->ec_fptr)(ec->ec_arg1,
325 ec->ec_arg2, ec->ec_arg3, ec->ec_arg4);
328 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
329 register_t, register_t, register_t))ec->ec_fptr)(
330 ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4,
334 panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt);
341 efi_call(struct efirt_callinfo *ecp)
348 error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) :
349 efi_rt_arch_call_nofault(ecp);
352 error = efi_status_to_errno(ecp->ec_efi_status);
353 else if (bootverbose)
354 printf("EFI %s call faulted, error %d\n", ecp->ec_name, error);
358 #define EFI_RT_METHOD_PA(method) \
359 ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t) \
360 efi_runtime))->method)
363 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
365 struct efirt_callinfo ec;
368 if (efi_runtime == NULL)
370 bzero(&ec, sizeof(ec));
371 ec.ec_name = "rt_gettime";
373 ec.ec_arg1 = (uintptr_t)tm;
374 ec.ec_arg2 = (uintptr_t)tmcap;
375 ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime);
376 return (efi_call(&ec));
380 efi_get_time(struct efi_tm *tm)
382 struct efi_tmcap dummy;
385 if (efi_runtime == NULL)
389 * UEFI spec states that the Capabilities argument to GetTime is
390 * optional, but some UEFI implementations choke when passed a NULL
391 * pointer. Pass a dummy efi_tmcap, even though we won't use it,
392 * to workaround such implementations.
394 error = efi_get_time_locked(tm, &dummy);
400 efi_get_time_capabilities(struct efi_tmcap *tmcap)
405 if (efi_runtime == NULL)
408 error = efi_get_time_locked(&dummy, tmcap);
414 efi_reset_system(void)
416 struct efirt_callinfo ec;
418 if (efi_runtime == NULL)
420 bzero(&ec, sizeof(ec));
421 ec.ec_name = "rt_reset";
423 ec.ec_arg1 = (uintptr_t)EFI_RESET_WARM;
424 ec.ec_arg2 = (uintptr_t)0;
425 ec.ec_arg3 = (uintptr_t)0;
426 ec.ec_arg4 = (uintptr_t)NULL;
427 ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset);
428 return (efi_call(&ec));
432 efi_set_time_locked(struct efi_tm *tm)
434 struct efirt_callinfo ec;
437 if (efi_runtime == NULL)
439 bzero(&ec, sizeof(ec));
440 ec.ec_name = "rt_settime";
442 ec.ec_arg1 = (uintptr_t)tm;
443 ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime);
444 return (efi_call(&ec));
448 efi_set_time(struct efi_tm *tm)
452 if (efi_runtime == NULL)
455 error = efi_set_time_locked(tm);
461 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
462 size_t *datasize, void *data)
464 struct efirt_callinfo ec;
466 if (efi_runtime == NULL)
468 bzero(&ec, sizeof(ec));
470 ec.ec_name = "rt_getvar";
471 ec.ec_arg1 = (uintptr_t)name;
472 ec.ec_arg2 = (uintptr_t)vendor;
473 ec.ec_arg3 = (uintptr_t)attrib;
474 ec.ec_arg4 = (uintptr_t)datasize;
475 ec.ec_arg5 = (uintptr_t)data;
476 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar);
477 return (efi_call(&ec));
481 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
483 struct efirt_callinfo ec;
485 if (efi_runtime == NULL)
487 bzero(&ec, sizeof(ec));
489 ec.ec_name = "rt_scanvar";
490 ec.ec_arg1 = (uintptr_t)namesize;
491 ec.ec_arg2 = (uintptr_t)name;
492 ec.ec_arg3 = (uintptr_t)vendor;
493 ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar);
494 return (efi_call(&ec));
498 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
499 size_t datasize, void *data)
501 struct efirt_callinfo ec;
503 if (efi_runtime == NULL)
505 bzero(&ec, sizeof(ec));
507 ec.ec_name = "rt_setvar";
508 ec.ec_arg1 = (uintptr_t)name;
509 ec.ec_arg2 = (uintptr_t)vendor;
510 ec.ec_arg3 = (uintptr_t)attrib;
511 ec.ec_arg4 = (uintptr_t)datasize;
512 ec.ec_arg5 = (uintptr_t)data;
513 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar);
514 return (efi_call(&ec));
518 efirt_modevents(module_t m, int event, void *arg __unused)
537 static moduledata_t efirt_moddata = {
539 .evhand = efirt_modevents,
542 /* After fpuinitstate, before efidev */
543 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
544 MODULE_VERSION(efirt, 1);