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 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 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 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 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 mtx_init(&efi_lock, "efi", NULL, MTX_DEF); 187 188 if (efi_systbl_phys == 0) { 189 if (bootverbose) 190 printf("EFI systbl not available\n"); 191 return (0); 192 } 193 194 efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys); 195 if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) { 196 efi_systbl = NULL; 197 if (bootverbose) 198 printf("EFI systbl signature invalid\n"); 199 return (0); 200 } 201 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL : 202 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl; 203 if (efi_cfgtbl == NULL) { 204 if (bootverbose) 205 printf("EFI config table is not present\n"); 206 } 207 208 efihdr = (struct efi_map_header *)preload_search_info(preload_kmdp, 209 MODINFO_METADATA | MODINFOMD_EFI_MAP); 210 if (efihdr == NULL) { 211 if (bootverbose) 212 printf("EFI map is not present\n"); 213 return (0); 214 } 215 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 216 map = (struct efi_md *)((uint8_t *)efihdr + efisz); 217 if (efihdr->descriptor_size == 0) 218 return (ENOMEM); 219 220 ndesc = efihdr->memory_size / efihdr->descriptor_size; 221 if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) { 222 if (bootverbose) 223 printf("EFI cannot create runtime map\n"); 224 return (ENOMEM); 225 } 226 227 efi_runtime = (efi_systbl->st_rt == 0) ? NULL : 228 (struct efi_rt *)efi_systbl->st_rt; 229 if (efi_runtime == NULL) { 230 if (bootverbose) 231 printf("EFI runtime services table is not present\n"); 232 efi_destroy_1t1_map(); 233 return (ENXIO); 234 } 235 236 #if defined(__aarch64__) || defined(__amd64__) 237 /* 238 * Some UEFI implementations have multiple implementations of the 239 * RS->GetTime function. They switch from one we can only use early 240 * in the boot process to one valid as a RunTime service only when we 241 * call RS->SetVirtualAddressMap. As this is not always the case, e.g. 242 * with an old loader.efi, check if the RS->GetTime function is within 243 * the EFI map, and fail to attach if not. 244 */ 245 rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime); 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 /* 258 * We use SHUTDOWN_PRI_LAST - 1 to trigger after IPMI, but before ACPI. 259 */ 260 efi_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final, 261 efi_shutdown_final, NULL, SHUTDOWN_PRI_LAST - 1); 262 263 return (0); 264 } 265 266 static void 267 efi_uninit(void) 268 { 269 270 /* Most likely disabled by tunable */ 271 if (efi_runtime == NULL) 272 return; 273 if (efi_shutdown_tag != NULL) 274 EVENTHANDLER_DEREGISTER(shutdown_final, efi_shutdown_tag); 275 efi_destroy_1t1_map(); 276 277 efi_systbl = NULL; 278 efi_cfgtbl = NULL; 279 efi_runtime = NULL; 280 281 mtx_destroy(&efi_lock); 282 } 283 284 static int 285 rt_ok(void) 286 { 287 288 if (efi_runtime == NULL) 289 return (ENXIO); 290 return (0); 291 } 292 293 /* 294 * The fpu_kern_enter() call in allows firmware to use FPU, as 295 * mandated by the specification. It also enters a critical section, 296 * giving us neccessary protection against context switches. 297 */ 298 static int 299 efi_enter(void) 300 { 301 struct thread *td; 302 pmap_t curpmap; 303 int error; 304 305 if (efi_runtime == NULL) 306 return (ENXIO); 307 td = curthread; 308 curpmap = &td->td_proc->p_vmspace->vm_pmap; 309 PMAP_LOCK(curpmap); 310 mtx_lock(&efi_lock); 311 fpu_kern_enter(td, NULL, FPU_KERN_NOCTX); 312 error = efi_arch_enter(); 313 if (error != 0) { 314 fpu_kern_leave(td, NULL); 315 mtx_unlock(&efi_lock); 316 PMAP_UNLOCK(curpmap); 317 } else { 318 MPASS((td->td_pflags & TDP_EFIRT) == 0); 319 td->td_pflags |= TDP_EFIRT; 320 } 321 return (error); 322 } 323 324 static void 325 efi_leave(void) 326 { 327 struct thread *td; 328 pmap_t curpmap; 329 330 td = curthread; 331 MPASS((td->td_pflags & TDP_EFIRT) != 0); 332 td->td_pflags &= ~TDP_EFIRT; 333 334 efi_arch_leave(); 335 336 curpmap = &curproc->p_vmspace->vm_pmap; 337 fpu_kern_leave(td, NULL); 338 mtx_unlock(&efi_lock); 339 PMAP_UNLOCK(curpmap); 340 } 341 342 static int 343 get_table(efi_guid_t *guid, void **ptr) 344 { 345 struct efi_cfgtbl *ct; 346 u_long count; 347 int error; 348 349 if (efi_cfgtbl == NULL || efi_systbl == NULL) 350 return (ENXIO); 351 error = efi_enter(); 352 if (error != 0) 353 return (error); 354 count = efi_systbl->st_entries; 355 ct = efi_cfgtbl; 356 while (count--) { 357 if (!bcmp(&ct->ct_guid, guid, sizeof(*guid))) { 358 *ptr = ct->ct_data; 359 efi_leave(); 360 return (0); 361 } 362 ct++; 363 } 364 365 efi_leave(); 366 return (ENOENT); 367 } 368 369 static int 370 get_table_length(enum efi_table_type type, size_t *table_len, void **taddr) 371 { 372 switch (type) { 373 case TYPE_ESRT: 374 { 375 struct efi_esrt_table *esrt = NULL; 376 efi_guid_t guid = EFI_TABLE_ESRT; 377 uint32_t fw_resource_count = 0; 378 size_t len = sizeof(*esrt); 379 int error; 380 void *buf; 381 382 error = efi_get_table(&guid, (void **)&esrt); 383 if (error != 0) 384 return (error); 385 386 buf = malloc(len, M_TEMP, M_WAITOK); 387 error = physcopyout((vm_paddr_t)esrt, buf, len); 388 if (error != 0) { 389 free(buf, M_TEMP); 390 return (error); 391 } 392 393 /* Check ESRT version */ 394 if (((struct efi_esrt_table *)buf)->fw_resource_version != 395 ESRT_FIRMWARE_RESOURCE_VERSION) { 396 free(buf, M_TEMP); 397 return (ENODEV); 398 } 399 400 fw_resource_count = ((struct efi_esrt_table *)buf)-> 401 fw_resource_count; 402 if (fw_resource_count > EFI_TABLE_ALLOC_MAX / 403 sizeof(struct efi_esrt_entry_v1)) { 404 free(buf, M_TEMP); 405 return (ENOMEM); 406 } 407 408 len += fw_resource_count * sizeof(struct efi_esrt_entry_v1); 409 *table_len = len; 410 411 if (taddr != NULL) 412 *taddr = esrt; 413 free(buf, M_TEMP); 414 return (0); 415 } 416 case TYPE_PROP: 417 { 418 efi_guid_t guid = EFI_PROPERTIES_TABLE; 419 struct efi_prop_table *prop; 420 size_t len = sizeof(*prop); 421 uint32_t prop_len; 422 int error; 423 void *buf; 424 425 error = efi_get_table(&guid, (void **)&prop); 426 if (error != 0) 427 return (error); 428 429 buf = malloc(len, M_TEMP, M_WAITOK); 430 error = physcopyout((vm_paddr_t)prop, buf, len); 431 if (error != 0) { 432 free(buf, M_TEMP); 433 return (error); 434 } 435 436 prop_len = ((struct efi_prop_table *)buf)->length; 437 if (prop_len > EFI_TABLE_ALLOC_MAX) { 438 free(buf, M_TEMP); 439 return (ENOMEM); 440 } 441 *table_len = prop_len; 442 443 if (taddr != NULL) 444 *taddr = prop; 445 free(buf, M_TEMP); 446 return (0); 447 } 448 } 449 return (ENOENT); 450 } 451 452 static int 453 copy_table(efi_guid_t *guid, void **buf, size_t buf_len, size_t *table_len) 454 { 455 static const struct known_table { 456 efi_guid_t guid; 457 enum efi_table_type type; 458 } tables[] = { 459 { EFI_TABLE_ESRT, TYPE_ESRT }, 460 { EFI_PROPERTIES_TABLE, TYPE_PROP } 461 }; 462 size_t table_idx; 463 void *taddr; 464 int rc; 465 466 for (table_idx = 0; table_idx < nitems(tables); table_idx++) { 467 if (!bcmp(&tables[table_idx].guid, guid, sizeof(*guid))) 468 break; 469 } 470 471 if (table_idx == nitems(tables)) 472 return (EINVAL); 473 474 rc = get_table_length(tables[table_idx].type, table_len, &taddr); 475 if (rc != 0) 476 return rc; 477 478 /* return table length to userspace */ 479 if (buf == NULL) 480 return (0); 481 482 *buf = malloc(*table_len, M_TEMP, M_WAITOK); 483 rc = physcopyout((vm_paddr_t)taddr, *buf, *table_len); 484 return (rc); 485 } 486 487 static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT; 488 SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN, 489 &efi_rt_handle_faults, 0, 490 "Call EFI RT methods with fault handler wrapper around"); 491 492 static int 493 efi_rt_arch_call_nofault(struct efirt_callinfo *ec) 494 { 495 496 switch (ec->ec_argcnt) { 497 case 0: 498 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(void)) 499 ec->ec_fptr)(); 500 break; 501 case 1: 502 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t)) 503 ec->ec_fptr)(ec->ec_arg1); 504 break; 505 case 2: 506 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t, 507 register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2); 508 break; 509 case 3: 510 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t, 511 register_t, register_t))ec->ec_fptr)(ec->ec_arg1, 512 ec->ec_arg2, ec->ec_arg3); 513 break; 514 case 4: 515 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t, 516 register_t, register_t, register_t))ec->ec_fptr)( 517 ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4); 518 break; 519 case 5: 520 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t, 521 register_t, register_t, register_t, register_t)) 522 ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, 523 ec->ec_arg4, ec->ec_arg5); 524 break; 525 default: 526 panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt); 527 } 528 529 return (0); 530 } 531 532 static int 533 efi_call(struct efirt_callinfo *ecp) 534 { 535 int error; 536 537 error = efi_enter(); 538 if (error != 0) 539 return (error); 540 error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) : 541 efi_rt_arch_call_nofault(ecp); 542 efi_leave(); 543 if (error == 0) 544 error = efi_status_to_errno(ecp->ec_efi_status); 545 else if (bootverbose) 546 printf("EFI %s call faulted, error %d\n", ecp->ec_name, error); 547 return (error); 548 } 549 550 #define EFI_RT_METHOD_PA(method) \ 551 ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t) \ 552 efi_runtime))->method) 553 554 static int 555 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap) 556 { 557 struct efirt_callinfo ec; 558 int error; 559 560 EFI_TIME_OWNED(); 561 if (efi_runtime == NULL) 562 return (ENXIO); 563 bzero(&ec, sizeof(ec)); 564 ec.ec_name = "rt_gettime"; 565 ec.ec_argcnt = 2; 566 ec.ec_arg1 = (uintptr_t)tm; 567 ec.ec_arg2 = (uintptr_t)tmcap; 568 ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime); 569 error = efi_call(&ec); 570 if (error == 0) 571 kmsan_mark(tm, sizeof(*tm), KMSAN_STATE_INITED); 572 return (error); 573 } 574 575 static int 576 get_time(struct efi_tm *tm) 577 { 578 struct efi_tmcap dummy; 579 int error; 580 581 if (efi_runtime == NULL) 582 return (ENXIO); 583 EFI_TIME_LOCK(); 584 /* 585 * UEFI spec states that the Capabilities argument to GetTime is 586 * optional, but some UEFI implementations choke when passed a NULL 587 * pointer. Pass a dummy efi_tmcap, even though we won't use it, 588 * to workaround such implementations. 589 */ 590 error = efi_get_time_locked(tm, &dummy); 591 EFI_TIME_UNLOCK(); 592 return (error); 593 } 594 595 static int 596 get_waketime(uint8_t *enabled, uint8_t *pending, struct efi_tm *tm) 597 { 598 struct efirt_callinfo ec; 599 int error; 600 #ifdef DEV_ACPI 601 UINT32 acpiRtcEnabled; 602 #endif 603 604 if (efi_runtime == NULL) 605 return (ENXIO); 606 607 EFI_TIME_LOCK(); 608 bzero(&ec, sizeof(ec)); 609 ec.ec_name = "rt_getwaketime"; 610 ec.ec_argcnt = 3; 611 ec.ec_arg1 = (uintptr_t)enabled; 612 ec.ec_arg2 = (uintptr_t)pending; 613 ec.ec_arg3 = (uintptr_t)tm; 614 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getwaketime); 615 error = efi_call(&ec); 616 EFI_TIME_UNLOCK(); 617 618 #ifdef DEV_ACPI 619 if (error == 0) { 620 error = AcpiReadBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE, 621 &acpiRtcEnabled); 622 if (ACPI_SUCCESS(error)) { 623 *enabled = *enabled && acpiRtcEnabled; 624 } else 625 error = EIO; 626 } 627 #endif 628 629 return (error); 630 } 631 632 static int 633 set_waketime(uint8_t enable, struct efi_tm *tm) 634 { 635 struct efirt_callinfo ec; 636 int error; 637 638 if (efi_runtime == NULL) 639 return (ENXIO); 640 641 EFI_TIME_LOCK(); 642 bzero(&ec, sizeof(ec)); 643 ec.ec_name = "rt_setwaketime"; 644 ec.ec_argcnt = 2; 645 ec.ec_arg1 = (uintptr_t)enable; 646 ec.ec_arg2 = (uintptr_t)tm; 647 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setwaketime); 648 error = efi_call(&ec); 649 EFI_TIME_UNLOCK(); 650 651 #ifdef DEV_ACPI 652 if (error == 0) { 653 error = AcpiWriteBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE, 654 (enable != 0) ? 1 : 0); 655 if (ACPI_FAILURE(error)) 656 error = EIO; 657 } 658 #endif 659 660 return (error); 661 } 662 663 static int 664 get_time_capabilities(struct efi_tmcap *tmcap) 665 { 666 struct efi_tm dummy; 667 int error; 668 669 if (efi_runtime == NULL) 670 return (ENXIO); 671 EFI_TIME_LOCK(); 672 error = efi_get_time_locked(&dummy, tmcap); 673 EFI_TIME_UNLOCK(); 674 return (error); 675 } 676 677 static int 678 reset_system(enum efi_reset type) 679 { 680 struct efirt_callinfo ec; 681 682 switch (type) { 683 case EFI_RESET_COLD: 684 case EFI_RESET_WARM: 685 case EFI_RESET_SHUTDOWN: 686 break; 687 default: 688 return (EINVAL); 689 } 690 if (efi_runtime == NULL) 691 return (ENXIO); 692 bzero(&ec, sizeof(ec)); 693 ec.ec_name = "rt_reset"; 694 ec.ec_argcnt = 4; 695 ec.ec_arg1 = (uintptr_t)type; 696 ec.ec_arg2 = (uintptr_t)0; 697 ec.ec_arg3 = (uintptr_t)0; 698 ec.ec_arg4 = (uintptr_t)NULL; 699 ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset); 700 return (efi_call(&ec)); 701 } 702 703 static int 704 efi_set_time_locked(struct efi_tm *tm) 705 { 706 struct efirt_callinfo ec; 707 708 EFI_TIME_OWNED(); 709 if (efi_runtime == NULL) 710 return (ENXIO); 711 bzero(&ec, sizeof(ec)); 712 ec.ec_name = "rt_settime"; 713 ec.ec_argcnt = 1; 714 ec.ec_arg1 = (uintptr_t)tm; 715 ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime); 716 return (efi_call(&ec)); 717 } 718 719 static int 720 set_time(struct efi_tm *tm) 721 { 722 int error; 723 724 if (efi_runtime == NULL) 725 return (ENXIO); 726 EFI_TIME_LOCK(); 727 error = efi_set_time_locked(tm); 728 EFI_TIME_UNLOCK(); 729 return (error); 730 } 731 732 static int 733 var_get(efi_char *name, efi_guid_t *vendor, uint32_t *attrib, 734 size_t *datasize, void *data) 735 { 736 struct efirt_callinfo ec; 737 int error; 738 739 if (efi_runtime == NULL) 740 return (ENXIO); 741 bzero(&ec, sizeof(ec)); 742 ec.ec_argcnt = 5; 743 ec.ec_name = "rt_getvar"; 744 ec.ec_arg1 = (uintptr_t)name; 745 ec.ec_arg2 = (uintptr_t)vendor; 746 ec.ec_arg3 = (uintptr_t)attrib; 747 ec.ec_arg4 = (uintptr_t)datasize; 748 ec.ec_arg5 = (uintptr_t)data; 749 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar); 750 error = efi_call(&ec); 751 if (error == 0) 752 kmsan_mark(data, *datasize, KMSAN_STATE_INITED); 753 return (error); 754 } 755 756 static int 757 var_nextname(size_t *namesize, efi_char *name, efi_guid_t *vendor) 758 { 759 struct efirt_callinfo ec; 760 int error; 761 762 if (efi_runtime == NULL) 763 return (ENXIO); 764 bzero(&ec, sizeof(ec)); 765 ec.ec_argcnt = 3; 766 ec.ec_name = "rt_scanvar"; 767 ec.ec_arg1 = (uintptr_t)namesize; 768 ec.ec_arg2 = (uintptr_t)name; 769 ec.ec_arg3 = (uintptr_t)vendor; 770 ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar); 771 error = efi_call(&ec); 772 if (error == 0) 773 kmsan_mark(name, *namesize, KMSAN_STATE_INITED); 774 return (error); 775 } 776 777 static int 778 var_set(efi_char *name, efi_guid_t *vendor, uint32_t attrib, 779 size_t datasize, void *data) 780 { 781 struct efirt_callinfo ec; 782 783 if (efi_runtime == NULL) 784 return (ENXIO); 785 bzero(&ec, sizeof(ec)); 786 ec.ec_argcnt = 5; 787 ec.ec_name = "rt_setvar"; 788 ec.ec_arg1 = (uintptr_t)name; 789 ec.ec_arg2 = (uintptr_t)vendor; 790 ec.ec_arg3 = (uintptr_t)attrib; 791 ec.ec_arg4 = (uintptr_t)datasize; 792 ec.ec_arg5 = (uintptr_t)data; 793 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar); 794 return (efi_call(&ec)); 795 } 796 797 const static struct efi_ops efi_ops = { 798 .rt_ok = rt_ok, 799 .get_table = get_table, 800 .copy_table = copy_table, 801 .get_time = get_time, 802 .get_time_capabilities = get_time_capabilities, 803 .reset_system = reset_system, 804 .set_time = set_time, 805 .get_waketime = get_waketime, 806 .set_waketime = set_waketime, 807 .var_get = var_get, 808 .var_nextname = var_nextname, 809 .var_set = var_set, 810 }; 811 const struct efi_ops *active_efi_ops = &efi_ops; 812 813 static int 814 efirt_modevents(module_t m, int event, void *arg __unused) 815 { 816 817 switch (event) { 818 case MOD_LOAD: 819 return (efi_init()); 820 821 case MOD_UNLOAD: 822 efi_uninit(); 823 return (0); 824 825 case MOD_SHUTDOWN: 826 return (0); 827 828 default: 829 return (EOPNOTSUPP); 830 } 831 } 832 833 static moduledata_t efirt_moddata = { 834 .name = "efirt", 835 .evhand = efirt_modevents, 836 .priv = NULL, 837 }; 838 /* After fpuinitstate, before efidev */ 839 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND); 840 MODULE_VERSION(efirt, 1); 841