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