1 /*-
2 * Copyright (c) 2004 Marcel Moolenaar
3 * Copyright (c) 2001 Doug Rabson
4 * Copyright (c) 2016, 2018 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Konstantin Belousov
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #include "opt_acpi.h"
34
35 #include <sys/param.h>
36 #include <sys/efi.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/linker.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/msan.h>
44 #include <sys/mutex.h>
45 #include <sys/clock.h>
46 #include <sys/proc.h>
47 #include <sys/reboot.h>
48 #include <sys/rwlock.h>
49 #include <sys/sched.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52 #include <sys/uio.h>
53 #include <sys/vmmeter.h>
54
55 #include <machine/fpu.h>
56 #include <machine/efi.h>
57 #include <machine/metadata.h>
58 #include <machine/vmparam.h>
59
60 #include <vm/vm.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_map.h>
63
64 #ifdef DEV_ACPI
65 #include <contrib/dev/acpica/include/acpi.h>
66 #endif
67
68 #define EFI_TABLE_ALLOC_MAX 0x800000
69
70 static struct efi_systbl *efi_systbl;
71 static eventhandler_tag efi_shutdown_tag;
72 /*
73 * The following pointers point to tables in the EFI runtime service data pages.
74 * Care should be taken to make sure that we've properly entered the EFI runtime
75 * environment (efi_enter()) before dereferencing them.
76 */
77 static struct efi_cfgtbl *efi_cfgtbl;
78 static struct efi_rt *efi_runtime;
79
80 static int efi_status2err[25] = {
81 0, /* EFI_SUCCESS */
82 ENOEXEC, /* EFI_LOAD_ERROR */
83 EINVAL, /* EFI_INVALID_PARAMETER */
84 ENOSYS, /* EFI_UNSUPPORTED */
85 EMSGSIZE, /* EFI_BAD_BUFFER_SIZE */
86 EOVERFLOW, /* EFI_BUFFER_TOO_SMALL */
87 EBUSY, /* EFI_NOT_READY */
88 EIO, /* EFI_DEVICE_ERROR */
89 EROFS, /* EFI_WRITE_PROTECTED */
90 EAGAIN, /* EFI_OUT_OF_RESOURCES */
91 EIO, /* EFI_VOLUME_CORRUPTED */
92 ENOSPC, /* EFI_VOLUME_FULL */
93 ENXIO, /* EFI_NO_MEDIA */
94 ESTALE, /* EFI_MEDIA_CHANGED */
95 ENOENT, /* EFI_NOT_FOUND */
96 EACCES, /* EFI_ACCESS_DENIED */
97 ETIMEDOUT, /* EFI_NO_RESPONSE */
98 EADDRNOTAVAIL, /* EFI_NO_MAPPING */
99 ETIMEDOUT, /* EFI_TIMEOUT */
100 EDOOFUS, /* EFI_NOT_STARTED */
101 EALREADY, /* EFI_ALREADY_STARTED */
102 ECANCELED, /* EFI_ABORTED */
103 EPROTO, /* EFI_ICMP_ERROR */
104 EPROTO, /* EFI_TFTP_ERROR */
105 EPROTO /* EFI_PROTOCOL_ERROR */
106 };
107
108 enum efi_table_type {
109 TYPE_ESRT = 0,
110 TYPE_PROP
111 };
112
113 static int efi_enter(void);
114 static void efi_leave(void);
115
116 int
efi_status_to_errno(efi_status status)117 efi_status_to_errno(efi_status status)
118 {
119 u_long code;
120
121 code = status & 0x3ffffffffffffffful;
122 return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
123 }
124
125 static struct mtx efi_lock;
126 static SYSCTL_NODE(_hw, OID_AUTO, efi, CTLFLAG_RWTUN | CTLFLAG_MPSAFE, NULL,
127 "EFI");
128 static bool efi_poweroff = true;
129 SYSCTL_BOOL(_hw_efi, OID_AUTO, poweroff, CTLFLAG_RWTUN, &efi_poweroff, 0,
130 "If true, use EFI runtime services to power off in preference to ACPI");
131
132 static bool
efi_is_in_map(struct efi_md * map,int ndesc,int descsz,vm_offset_t addr)133 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr)
134 {
135 struct efi_md *p;
136 int i;
137
138 for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
139 descsz)) {
140 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
141 continue;
142
143 if (addr >= p->md_virt &&
144 addr < p->md_virt + p->md_pages * EFI_PAGE_SIZE)
145 return (true);
146 }
147
148 return (false);
149 }
150
151 static void
efi_shutdown_final(void * dummy __unused,int howto)152 efi_shutdown_final(void *dummy __unused, int howto)
153 {
154
155 /*
156 * On some systems, ACPI S5 is missing or does not function properly.
157 * When present, shutdown via EFI Runtime Services instead, unless
158 * disabled.
159 */
160 if ((howto & RB_POWEROFF) != 0 && efi_poweroff)
161 (void)efi_reset_system(EFI_RESET_SHUTDOWN);
162 }
163
164 static int
efi_init(void)165 efi_init(void)
166 {
167 struct efi_map_header *efihdr;
168 struct efi_md *map;
169 struct efi_rt *rtdm;
170 size_t efisz;
171 int ndesc, rt_disabled;
172
173 rt_disabled = 0;
174 TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
175 if (rt_disabled == 1)
176 return (0);
177 mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
178
179 if (efi_systbl_phys == 0) {
180 if (bootverbose)
181 printf("EFI systbl not available\n");
182 return (0);
183 }
184
185 efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys);
186 if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
187 efi_systbl = NULL;
188 if (bootverbose)
189 printf("EFI systbl signature invalid\n");
190 return (0);
191 }
192 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
193 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
194 if (efi_cfgtbl == NULL) {
195 if (bootverbose)
196 printf("EFI config table is not present\n");
197 }
198
199 efihdr = (struct efi_map_header *)preload_search_info(preload_kmdp,
200 MODINFO_METADATA | MODINFOMD_EFI_MAP);
201 if (efihdr == NULL) {
202 if (bootverbose)
203 printf("EFI map is not present\n");
204 return (0);
205 }
206 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
207 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
208 if (efihdr->descriptor_size == 0)
209 return (ENOMEM);
210
211 ndesc = efihdr->memory_size / efihdr->descriptor_size;
212 if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) {
213 if (bootverbose)
214 printf("EFI cannot create runtime map\n");
215 return (ENOMEM);
216 }
217
218 efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
219 (struct efi_rt *)efi_systbl->st_rt;
220 if (efi_runtime == NULL) {
221 if (bootverbose)
222 printf("EFI runtime services table is not present\n");
223 efi_destroy_1t1_map();
224 return (ENXIO);
225 }
226
227 #if defined(__aarch64__) || defined(__amd64__)
228 /*
229 * Some UEFI implementations have multiple implementations of the
230 * RS->GetTime function. They switch from one we can only use early
231 * in the boot process to one valid as a RunTime service only when we
232 * call RS->SetVirtualAddressMap. As this is not always the case, e.g.
233 * with an old loader.efi, check if the RS->GetTime function is within
234 * the EFI map, and fail to attach if not.
235 */
236 rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime);
237 if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size,
238 (vm_offset_t)rtdm->rt_gettime)) {
239 if (bootverbose)
240 printf(
241 "EFI runtime services table has an invalid pointer\n");
242 efi_runtime = NULL;
243 efi_destroy_1t1_map();
244 return (ENXIO);
245 }
246 #endif
247
248 /*
249 * We use SHUTDOWN_PRI_LAST - 1 to trigger after IPMI, but before ACPI.
250 */
251 efi_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final,
252 efi_shutdown_final, NULL, SHUTDOWN_PRI_LAST - 1);
253
254 return (0);
255 }
256
257 static void
efi_uninit(void)258 efi_uninit(void)
259 {
260
261 /* Most likely disabled by tunable */
262 if (efi_runtime == NULL)
263 return;
264 if (efi_shutdown_tag != NULL)
265 EVENTHANDLER_DEREGISTER(shutdown_final, efi_shutdown_tag);
266 efi_destroy_1t1_map();
267
268 efi_systbl = NULL;
269 efi_cfgtbl = NULL;
270 efi_runtime = NULL;
271
272 mtx_destroy(&efi_lock);
273 }
274
275 static int
rt_ok(void)276 rt_ok(void)
277 {
278
279 if (efi_runtime == NULL)
280 return (ENXIO);
281 return (0);
282 }
283
284 /*
285 * The fpu_kern_enter() call in allows firmware to use FPU, as
286 * mandated by the specification. It also enters a critical section,
287 * giving us neccessary protection against context switches.
288 */
289 static int
efi_enter(void)290 efi_enter(void)
291 {
292 struct thread *td;
293 pmap_t curpmap;
294 int error;
295
296 if (efi_runtime == NULL)
297 return (ENXIO);
298 td = curthread;
299 curpmap = &td->td_proc->p_vmspace->vm_pmap;
300 PMAP_LOCK(curpmap);
301 mtx_lock(&efi_lock);
302 fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
303 error = efi_arch_enter();
304 if (error != 0) {
305 fpu_kern_leave(td, NULL);
306 mtx_unlock(&efi_lock);
307 PMAP_UNLOCK(curpmap);
308 } else {
309 MPASS((td->td_pflags & TDP_EFIRT) == 0);
310 td->td_pflags |= TDP_EFIRT;
311 }
312 return (error);
313 }
314
315 static void
efi_leave(void)316 efi_leave(void)
317 {
318 struct thread *td;
319 pmap_t curpmap;
320
321 td = curthread;
322 MPASS((td->td_pflags & TDP_EFIRT) != 0);
323 td->td_pflags &= ~TDP_EFIRT;
324
325 efi_arch_leave();
326
327 curpmap = &curproc->p_vmspace->vm_pmap;
328 fpu_kern_leave(td, NULL);
329 mtx_unlock(&efi_lock);
330 PMAP_UNLOCK(curpmap);
331 }
332
333 static int
get_table(struct uuid * uuid,void ** ptr)334 get_table(struct uuid *uuid, void **ptr)
335 {
336 struct efi_cfgtbl *ct;
337 u_long count;
338 int error;
339
340 if (efi_cfgtbl == NULL || efi_systbl == NULL)
341 return (ENXIO);
342 error = efi_enter();
343 if (error != 0)
344 return (error);
345 count = efi_systbl->st_entries;
346 ct = efi_cfgtbl;
347 while (count--) {
348 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
349 *ptr = ct->ct_data;
350 efi_leave();
351 return (0);
352 }
353 ct++;
354 }
355
356 efi_leave();
357 return (ENOENT);
358 }
359
360 static int
get_table_length(enum efi_table_type type,size_t * table_len,void ** taddr)361 get_table_length(enum efi_table_type type, size_t *table_len, void **taddr)
362 {
363 switch (type) {
364 case TYPE_ESRT:
365 {
366 struct efi_esrt_table *esrt = NULL;
367 struct uuid uuid = EFI_TABLE_ESRT;
368 uint32_t fw_resource_count = 0;
369 size_t len = sizeof(*esrt);
370 int error;
371 void *buf;
372
373 error = efi_get_table(&uuid, (void **)&esrt);
374 if (error != 0)
375 return (error);
376
377 buf = malloc(len, M_TEMP, M_WAITOK);
378 error = physcopyout((vm_paddr_t)esrt, buf, len);
379 if (error != 0) {
380 free(buf, M_TEMP);
381 return (error);
382 }
383
384 /* Check ESRT version */
385 if (((struct efi_esrt_table *)buf)->fw_resource_version !=
386 ESRT_FIRMWARE_RESOURCE_VERSION) {
387 free(buf, M_TEMP);
388 return (ENODEV);
389 }
390
391 fw_resource_count = ((struct efi_esrt_table *)buf)->
392 fw_resource_count;
393 if (fw_resource_count > EFI_TABLE_ALLOC_MAX /
394 sizeof(struct efi_esrt_entry_v1)) {
395 free(buf, M_TEMP);
396 return (ENOMEM);
397 }
398
399 len += fw_resource_count * sizeof(struct efi_esrt_entry_v1);
400 *table_len = len;
401
402 if (taddr != NULL)
403 *taddr = esrt;
404 free(buf, M_TEMP);
405 return (0);
406 }
407 case TYPE_PROP:
408 {
409 struct uuid uuid = EFI_PROPERTIES_TABLE;
410 struct efi_prop_table *prop;
411 size_t len = sizeof(*prop);
412 uint32_t prop_len;
413 int error;
414 void *buf;
415
416 error = efi_get_table(&uuid, (void **)&prop);
417 if (error != 0)
418 return (error);
419
420 buf = malloc(len, M_TEMP, M_WAITOK);
421 error = physcopyout((vm_paddr_t)prop, buf, len);
422 if (error != 0) {
423 free(buf, M_TEMP);
424 return (error);
425 }
426
427 prop_len = ((struct efi_prop_table *)buf)->length;
428 if (prop_len > EFI_TABLE_ALLOC_MAX) {
429 free(buf, M_TEMP);
430 return (ENOMEM);
431 }
432 *table_len = prop_len;
433
434 if (taddr != NULL)
435 *taddr = prop;
436 free(buf, M_TEMP);
437 return (0);
438 }
439 }
440 return (ENOENT);
441 }
442
443 static int
copy_table(struct uuid * uuid,void ** buf,size_t buf_len,size_t * table_len)444 copy_table(struct uuid *uuid, void **buf, size_t buf_len, size_t *table_len)
445 {
446 static const struct known_table {
447 struct uuid uuid;
448 enum efi_table_type type;
449 } tables[] = {
450 { EFI_TABLE_ESRT, TYPE_ESRT },
451 { EFI_PROPERTIES_TABLE, TYPE_PROP }
452 };
453 size_t table_idx;
454 void *taddr;
455 int rc;
456
457 for (table_idx = 0; table_idx < nitems(tables); table_idx++) {
458 if (!bcmp(&tables[table_idx].uuid, uuid, sizeof(*uuid)))
459 break;
460 }
461
462 if (table_idx == nitems(tables))
463 return (EINVAL);
464
465 rc = get_table_length(tables[table_idx].type, table_len, &taddr);
466 if (rc != 0)
467 return rc;
468
469 /* return table length to userspace */
470 if (buf == NULL)
471 return (0);
472
473 *buf = malloc(*table_len, M_TEMP, M_WAITOK);
474 rc = physcopyout((vm_paddr_t)taddr, *buf, *table_len);
475 return (rc);
476 }
477
478 static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT;
479 SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN,
480 &efi_rt_handle_faults, 0,
481 "Call EFI RT methods with fault handler wrapper around");
482
483 static int
efi_rt_arch_call_nofault(struct efirt_callinfo * ec)484 efi_rt_arch_call_nofault(struct efirt_callinfo *ec)
485 {
486
487 switch (ec->ec_argcnt) {
488 case 0:
489 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(void))
490 ec->ec_fptr)();
491 break;
492 case 1:
493 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t))
494 ec->ec_fptr)(ec->ec_arg1);
495 break;
496 case 2:
497 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t,
498 register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2);
499 break;
500 case 3:
501 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t,
502 register_t, register_t))ec->ec_fptr)(ec->ec_arg1,
503 ec->ec_arg2, ec->ec_arg3);
504 break;
505 case 4:
506 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t,
507 register_t, register_t, register_t))ec->ec_fptr)(
508 ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4);
509 break;
510 case 5:
511 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t,
512 register_t, register_t, register_t, register_t))
513 ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2, ec->ec_arg3,
514 ec->ec_arg4, ec->ec_arg5);
515 break;
516 default:
517 panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt);
518 }
519
520 return (0);
521 }
522
523 static int
efi_call(struct efirt_callinfo * ecp)524 efi_call(struct efirt_callinfo *ecp)
525 {
526 int error;
527
528 error = efi_enter();
529 if (error != 0)
530 return (error);
531 error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) :
532 efi_rt_arch_call_nofault(ecp);
533 efi_leave();
534 if (error == 0)
535 error = efi_status_to_errno(ecp->ec_efi_status);
536 else if (bootverbose)
537 printf("EFI %s call faulted, error %d\n", ecp->ec_name, error);
538 return (error);
539 }
540
541 #define EFI_RT_METHOD_PA(method) \
542 ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t) \
543 efi_runtime))->method)
544
545 static int
efi_get_time_locked(struct efi_tm * tm,struct efi_tmcap * tmcap)546 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
547 {
548 struct efirt_callinfo ec;
549 int error;
550
551 EFI_TIME_OWNED();
552 if (efi_runtime == NULL)
553 return (ENXIO);
554 bzero(&ec, sizeof(ec));
555 ec.ec_name = "rt_gettime";
556 ec.ec_argcnt = 2;
557 ec.ec_arg1 = (uintptr_t)tm;
558 ec.ec_arg2 = (uintptr_t)tmcap;
559 ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime);
560 error = efi_call(&ec);
561 if (error == 0)
562 kmsan_mark(tm, sizeof(*tm), KMSAN_STATE_INITED);
563 return (error);
564 }
565
566 static int
get_time(struct efi_tm * tm)567 get_time(struct efi_tm *tm)
568 {
569 struct efi_tmcap dummy;
570 int error;
571
572 if (efi_runtime == NULL)
573 return (ENXIO);
574 EFI_TIME_LOCK();
575 /*
576 * UEFI spec states that the Capabilities argument to GetTime is
577 * optional, but some UEFI implementations choke when passed a NULL
578 * pointer. Pass a dummy efi_tmcap, even though we won't use it,
579 * to workaround such implementations.
580 */
581 error = efi_get_time_locked(tm, &dummy);
582 EFI_TIME_UNLOCK();
583 return (error);
584 }
585
586 static int
get_waketime(uint8_t * enabled,uint8_t * pending,struct efi_tm * tm)587 get_waketime(uint8_t *enabled, uint8_t *pending, struct efi_tm *tm)
588 {
589 struct efirt_callinfo ec;
590 int error;
591 #ifdef DEV_ACPI
592 UINT32 acpiRtcEnabled;
593 #endif
594
595 if (efi_runtime == NULL)
596 return (ENXIO);
597
598 EFI_TIME_LOCK();
599 bzero(&ec, sizeof(ec));
600 ec.ec_name = "rt_getwaketime";
601 ec.ec_argcnt = 3;
602 ec.ec_arg1 = (uintptr_t)enabled;
603 ec.ec_arg2 = (uintptr_t)pending;
604 ec.ec_arg3 = (uintptr_t)tm;
605 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getwaketime);
606 error = efi_call(&ec);
607 EFI_TIME_UNLOCK();
608
609 #ifdef DEV_ACPI
610 if (error == 0) {
611 error = AcpiReadBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE,
612 &acpiRtcEnabled);
613 if (ACPI_SUCCESS(error)) {
614 *enabled = *enabled && acpiRtcEnabled;
615 } else
616 error = EIO;
617 }
618 #endif
619
620 return (error);
621 }
622
623 static int
set_waketime(uint8_t enable,struct efi_tm * tm)624 set_waketime(uint8_t enable, struct efi_tm *tm)
625 {
626 struct efirt_callinfo ec;
627 int error;
628
629 if (efi_runtime == NULL)
630 return (ENXIO);
631
632 EFI_TIME_LOCK();
633 bzero(&ec, sizeof(ec));
634 ec.ec_name = "rt_setwaketime";
635 ec.ec_argcnt = 2;
636 ec.ec_arg1 = (uintptr_t)enable;
637 ec.ec_arg2 = (uintptr_t)tm;
638 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setwaketime);
639 error = efi_call(&ec);
640 EFI_TIME_UNLOCK();
641
642 #ifdef DEV_ACPI
643 if (error == 0) {
644 error = AcpiWriteBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE,
645 (enable != 0) ? 1 : 0);
646 if (ACPI_FAILURE(error))
647 error = EIO;
648 }
649 #endif
650
651 return (error);
652 }
653
654 static int
get_time_capabilities(struct efi_tmcap * tmcap)655 get_time_capabilities(struct efi_tmcap *tmcap)
656 {
657 struct efi_tm dummy;
658 int error;
659
660 if (efi_runtime == NULL)
661 return (ENXIO);
662 EFI_TIME_LOCK();
663 error = efi_get_time_locked(&dummy, tmcap);
664 EFI_TIME_UNLOCK();
665 return (error);
666 }
667
668 static int
reset_system(enum efi_reset type)669 reset_system(enum efi_reset type)
670 {
671 struct efirt_callinfo ec;
672
673 switch (type) {
674 case EFI_RESET_COLD:
675 case EFI_RESET_WARM:
676 case EFI_RESET_SHUTDOWN:
677 break;
678 default:
679 return (EINVAL);
680 }
681 if (efi_runtime == NULL)
682 return (ENXIO);
683 bzero(&ec, sizeof(ec));
684 ec.ec_name = "rt_reset";
685 ec.ec_argcnt = 4;
686 ec.ec_arg1 = (uintptr_t)type;
687 ec.ec_arg2 = (uintptr_t)0;
688 ec.ec_arg3 = (uintptr_t)0;
689 ec.ec_arg4 = (uintptr_t)NULL;
690 ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset);
691 return (efi_call(&ec));
692 }
693
694 static int
efi_set_time_locked(struct efi_tm * tm)695 efi_set_time_locked(struct efi_tm *tm)
696 {
697 struct efirt_callinfo ec;
698
699 EFI_TIME_OWNED();
700 if (efi_runtime == NULL)
701 return (ENXIO);
702 bzero(&ec, sizeof(ec));
703 ec.ec_name = "rt_settime";
704 ec.ec_argcnt = 1;
705 ec.ec_arg1 = (uintptr_t)tm;
706 ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime);
707 return (efi_call(&ec));
708 }
709
710 static int
set_time(struct efi_tm * tm)711 set_time(struct efi_tm *tm)
712 {
713 int error;
714
715 if (efi_runtime == NULL)
716 return (ENXIO);
717 EFI_TIME_LOCK();
718 error = efi_set_time_locked(tm);
719 EFI_TIME_UNLOCK();
720 return (error);
721 }
722
723 static int
var_get(efi_char * name,struct uuid * vendor,uint32_t * attrib,size_t * datasize,void * data)724 var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
725 size_t *datasize, void *data)
726 {
727 struct efirt_callinfo ec;
728 int error;
729
730 if (efi_runtime == NULL)
731 return (ENXIO);
732 bzero(&ec, sizeof(ec));
733 ec.ec_argcnt = 5;
734 ec.ec_name = "rt_getvar";
735 ec.ec_arg1 = (uintptr_t)name;
736 ec.ec_arg2 = (uintptr_t)vendor;
737 ec.ec_arg3 = (uintptr_t)attrib;
738 ec.ec_arg4 = (uintptr_t)datasize;
739 ec.ec_arg5 = (uintptr_t)data;
740 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar);
741 error = efi_call(&ec);
742 if (error == 0)
743 kmsan_mark(data, *datasize, KMSAN_STATE_INITED);
744 return (error);
745 }
746
747 static int
var_nextname(size_t * namesize,efi_char * name,struct uuid * vendor)748 var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
749 {
750 struct efirt_callinfo ec;
751 int error;
752
753 if (efi_runtime == NULL)
754 return (ENXIO);
755 bzero(&ec, sizeof(ec));
756 ec.ec_argcnt = 3;
757 ec.ec_name = "rt_scanvar";
758 ec.ec_arg1 = (uintptr_t)namesize;
759 ec.ec_arg2 = (uintptr_t)name;
760 ec.ec_arg3 = (uintptr_t)vendor;
761 ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar);
762 error = efi_call(&ec);
763 if (error == 0)
764 kmsan_mark(name, *namesize, KMSAN_STATE_INITED);
765 return (error);
766 }
767
768 static int
var_set(efi_char * name,struct uuid * vendor,uint32_t attrib,size_t datasize,void * data)769 var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
770 size_t datasize, void *data)
771 {
772 struct efirt_callinfo ec;
773
774 if (efi_runtime == NULL)
775 return (ENXIO);
776 bzero(&ec, sizeof(ec));
777 ec.ec_argcnt = 5;
778 ec.ec_name = "rt_setvar";
779 ec.ec_arg1 = (uintptr_t)name;
780 ec.ec_arg2 = (uintptr_t)vendor;
781 ec.ec_arg3 = (uintptr_t)attrib;
782 ec.ec_arg4 = (uintptr_t)datasize;
783 ec.ec_arg5 = (uintptr_t)data;
784 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar);
785 return (efi_call(&ec));
786 }
787
788 const static struct efi_ops efi_ops = {
789 .rt_ok = rt_ok,
790 .get_table = get_table,
791 .copy_table = copy_table,
792 .get_time = get_time,
793 .get_time_capabilities = get_time_capabilities,
794 .reset_system = reset_system,
795 .set_time = set_time,
796 .get_waketime = get_waketime,
797 .set_waketime = set_waketime,
798 .var_get = var_get,
799 .var_nextname = var_nextname,
800 .var_set = var_set,
801 };
802 const struct efi_ops *active_efi_ops = &efi_ops;
803
804 static int
efirt_modevents(module_t m,int event,void * arg __unused)805 efirt_modevents(module_t m, int event, void *arg __unused)
806 {
807
808 switch (event) {
809 case MOD_LOAD:
810 return (efi_init());
811
812 case MOD_UNLOAD:
813 efi_uninit();
814 return (0);
815
816 case MOD_SHUTDOWN:
817 return (0);
818
819 default:
820 return (EOPNOTSUPP);
821 }
822 }
823
824 static moduledata_t efirt_moddata = {
825 .name = "efirt",
826 .evhand = efirt_modevents,
827 .priv = NULL,
828 };
829 /* After fpuinitstate, before efidev */
830 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
831 MODULE_VERSION(efirt, 1);
832