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