1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * efi.c - EFI subsystem 4 * 5 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com> 6 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com> 7 * Copyright (C) 2013 Tom Gundersen <teg@jklm.no> 8 * 9 * This code registers /sys/firmware/efi{,/efivars} when EFI is supported, 10 * allowing the efivarfs to be mounted or the efivars module to be loaded. 11 * The existance of /sys/firmware/efi may also be used by userspace to 12 * determine that the system supports EFI. 13 */ 14 15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 16 17 #include <linux/kobject.h> 18 #include <linux/module.h> 19 #include <linux/init.h> 20 #include <linux/debugfs.h> 21 #include <linux/device.h> 22 #include <linux/efi.h> 23 #include <linux/of.h> 24 #include <linux/initrd.h> 25 #include <linux/io.h> 26 #include <linux/kexec.h> 27 #include <linux/platform_device.h> 28 #include <linux/random.h> 29 #include <linux/reboot.h> 30 #include <linux/slab.h> 31 #include <linux/acpi.h> 32 #include <linux/ucs2_string.h> 33 #include <linux/memblock.h> 34 #include <linux/security.h> 35 #include <linux/notifier.h> 36 37 #include <asm/early_ioremap.h> 38 39 struct efi __read_mostly efi = { 40 .runtime_supported_mask = EFI_RT_SUPPORTED_ALL, 41 .acpi = EFI_INVALID_TABLE_ADDR, 42 .acpi20 = EFI_INVALID_TABLE_ADDR, 43 .smbios = EFI_INVALID_TABLE_ADDR, 44 .smbios3 = EFI_INVALID_TABLE_ADDR, 45 .esrt = EFI_INVALID_TABLE_ADDR, 46 .tpm_log = EFI_INVALID_TABLE_ADDR, 47 .tpm_final_log = EFI_INVALID_TABLE_ADDR, 48 .ovmf_debug_log = EFI_INVALID_TABLE_ADDR, 49 #ifdef CONFIG_LOAD_UEFI_KEYS 50 .mokvar_table = EFI_INVALID_TABLE_ADDR, 51 #endif 52 #ifdef CONFIG_EFI_COCO_SECRET 53 .coco_secret = EFI_INVALID_TABLE_ADDR, 54 #endif 55 #ifdef CONFIG_UNACCEPTED_MEMORY 56 .unaccepted = EFI_INVALID_TABLE_ADDR, 57 #endif 58 }; 59 EXPORT_SYMBOL(efi); 60 61 unsigned long __ro_after_init efi_rng_seed = EFI_INVALID_TABLE_ADDR; 62 static unsigned long __initdata mem_reserve = EFI_INVALID_TABLE_ADDR; 63 static unsigned long __initdata rt_prop = EFI_INVALID_TABLE_ADDR; 64 static unsigned long __initdata initrd = EFI_INVALID_TABLE_ADDR; 65 66 extern unsigned long primary_display_table; 67 68 struct mm_struct efi_mm = { 69 .mm_mt = MTREE_INIT_EXT(mm_mt, MM_MT_FLAGS, efi_mm.mmap_lock), 70 .mm_users = ATOMIC_INIT(2), 71 .mm_count = ATOMIC_INIT(1), 72 .write_protect_seq = SEQCNT_ZERO(efi_mm.write_protect_seq), 73 MMAP_LOCK_INITIALIZER(efi_mm) 74 .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock), 75 .mmlist = LIST_HEAD_INIT(efi_mm.mmlist), 76 #ifdef CONFIG_SCHED_MM_CID 77 .mm_cid.lock = __RAW_SPIN_LOCK_UNLOCKED(efi_mm.mm_cid.lock), 78 #endif 79 .flexible_array = MM_STRUCT_FLEXIBLE_ARRAY_INIT, 80 }; 81 82 struct workqueue_struct *efi_rts_wq; 83 84 static bool disable_runtime = IS_ENABLED(CONFIG_EFI_DISABLE_RUNTIME); 85 static int __init setup_noefi(char *arg) 86 { 87 disable_runtime = true; 88 return 0; 89 } 90 early_param("noefi", setup_noefi); 91 92 bool efi_runtime_disabled(void) 93 { 94 return disable_runtime; 95 } 96 97 bool __pure __efi_soft_reserve_enabled(void) 98 { 99 return !efi_enabled(EFI_MEM_NO_SOFT_RESERVE); 100 } 101 102 static int __init parse_efi_cmdline(char *str) 103 { 104 if (!str) { 105 pr_warn("need at least one option\n"); 106 return -EINVAL; 107 } 108 109 if (parse_option_str(str, "debug")) 110 set_bit(EFI_DBG, &efi.flags); 111 112 if (parse_option_str(str, "noruntime")) 113 disable_runtime = true; 114 115 if (parse_option_str(str, "runtime")) 116 disable_runtime = false; 117 118 if (parse_option_str(str, "nosoftreserve")) 119 set_bit(EFI_MEM_NO_SOFT_RESERVE, &efi.flags); 120 121 return 0; 122 } 123 early_param("efi", parse_efi_cmdline); 124 125 struct kobject *efi_kobj; 126 127 /* 128 * Let's not leave out systab information that snuck into 129 * the efivars driver 130 * Note, do not add more fields in systab sysfs file as it breaks sysfs 131 * one value per file rule! 132 */ 133 static ssize_t systab_show(struct kobject *kobj, 134 struct kobj_attribute *attr, char *buf) 135 { 136 char *str = buf; 137 138 if (!kobj || !buf) 139 return -EINVAL; 140 141 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR) 142 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20); 143 if (efi.acpi != EFI_INVALID_TABLE_ADDR) 144 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi); 145 /* 146 * If both SMBIOS and SMBIOS3 entry points are implemented, the 147 * SMBIOS3 entry point shall be preferred, so we list it first to 148 * let applications stop parsing after the first match. 149 */ 150 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) 151 str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3); 152 if (efi.smbios != EFI_INVALID_TABLE_ADDR) 153 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios); 154 155 return str - buf; 156 } 157 158 static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400); 159 160 static ssize_t fw_platform_size_show(struct kobject *kobj, 161 struct kobj_attribute *attr, char *buf) 162 { 163 return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32); 164 } 165 166 extern __weak struct kobj_attribute efi_attr_fw_vendor; 167 extern __weak struct kobj_attribute efi_attr_runtime; 168 extern __weak struct kobj_attribute efi_attr_config_table; 169 static struct kobj_attribute efi_attr_fw_platform_size = 170 __ATTR_RO(fw_platform_size); 171 172 static struct attribute *efi_subsys_attrs[] = { 173 &efi_attr_systab.attr, 174 &efi_attr_fw_platform_size.attr, 175 &efi_attr_fw_vendor.attr, 176 &efi_attr_runtime.attr, 177 &efi_attr_config_table.attr, 178 NULL, 179 }; 180 181 umode_t __weak efi_attr_is_visible(struct kobject *kobj, struct attribute *attr, 182 int n) 183 { 184 return attr->mode; 185 } 186 187 static const struct attribute_group efi_subsys_attr_group = { 188 .attrs = efi_subsys_attrs, 189 .is_visible = efi_attr_is_visible, 190 }; 191 192 struct blocking_notifier_head efivar_ops_nh; 193 EXPORT_SYMBOL_GPL(efivar_ops_nh); 194 195 static struct efivars generic_efivars; 196 static struct efivar_operations generic_ops; 197 198 static bool generic_ops_supported(void) 199 { 200 unsigned long name_size; 201 efi_status_t status; 202 efi_char16_t name; 203 efi_guid_t guid; 204 205 name_size = sizeof(name); 206 207 if (!efi.get_next_variable) 208 return false; 209 status = efi.get_next_variable(&name_size, &name, &guid); 210 if (status == EFI_UNSUPPORTED) 211 return false; 212 213 return true; 214 } 215 216 static int generic_ops_register(void) 217 { 218 if (!generic_ops_supported()) 219 return 0; 220 221 generic_ops.get_variable = efi.get_variable; 222 generic_ops.get_next_variable = efi.get_next_variable; 223 generic_ops.query_variable_store = efi_query_variable_store; 224 generic_ops.query_variable_info = efi.query_variable_info; 225 226 if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE)) { 227 generic_ops.set_variable = efi.set_variable; 228 generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking; 229 } 230 return efivars_register(&generic_efivars, &generic_ops); 231 } 232 233 static void generic_ops_unregister(void) 234 { 235 if (!generic_ops.get_variable) 236 return; 237 238 efivars_unregister(&generic_efivars); 239 } 240 241 void efivars_generic_ops_register(void) 242 { 243 generic_ops_register(); 244 } 245 EXPORT_SYMBOL_GPL(efivars_generic_ops_register); 246 247 void efivars_generic_ops_unregister(void) 248 { 249 generic_ops_unregister(); 250 } 251 EXPORT_SYMBOL_GPL(efivars_generic_ops_unregister); 252 253 #ifdef CONFIG_EFI_CUSTOM_SSDT_OVERLAYS 254 #define EFIVAR_SSDT_NAME_MAX 16UL 255 static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata; 256 static int __init efivar_ssdt_setup(char *str) 257 { 258 int ret = security_locked_down(LOCKDOWN_ACPI_TABLES); 259 260 if (ret) 261 return ret; 262 263 if (strlen(str) < sizeof(efivar_ssdt)) 264 memcpy(efivar_ssdt, str, strlen(str)); 265 else 266 pr_warn("efivar_ssdt: name too long: %s\n", str); 267 return 1; 268 } 269 __setup("efivar_ssdt=", efivar_ssdt_setup); 270 271 static __init int efivar_ssdt_load(void) 272 { 273 unsigned long name_size = 256; 274 efi_char16_t *name = NULL; 275 efi_status_t status; 276 efi_guid_t guid; 277 int ret = 0; 278 279 if (!efivar_ssdt[0]) 280 return 0; 281 282 name = kzalloc(name_size, GFP_KERNEL); 283 if (!name) 284 return -ENOMEM; 285 286 for (;;) { 287 char utf8_name[EFIVAR_SSDT_NAME_MAX]; 288 unsigned long data_size = 0; 289 void *data; 290 int limit; 291 292 status = efi.get_next_variable(&name_size, name, &guid); 293 if (status == EFI_NOT_FOUND) { 294 break; 295 } else if (status == EFI_BUFFER_TOO_SMALL) { 296 efi_char16_t *name_tmp = 297 krealloc(name, name_size, GFP_KERNEL); 298 if (!name_tmp) { 299 ret = -ENOMEM; 300 goto out; 301 } 302 name = name_tmp; 303 continue; 304 } 305 306 limit = min(EFIVAR_SSDT_NAME_MAX, name_size); 307 ucs2_as_utf8(utf8_name, name, limit - 1); 308 if (strncmp(utf8_name, efivar_ssdt, limit) != 0) 309 continue; 310 311 pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt, &guid); 312 313 status = efi.get_variable(name, &guid, NULL, &data_size, NULL); 314 if (status != EFI_BUFFER_TOO_SMALL || !data_size) { 315 ret = -EIO; 316 goto out; 317 } 318 319 data = kmalloc(data_size, GFP_KERNEL); 320 if (!data) { 321 ret = -ENOMEM; 322 goto out; 323 } 324 325 status = efi.get_variable(name, &guid, NULL, &data_size, data); 326 if (status == EFI_SUCCESS) { 327 acpi_status acpi_ret = acpi_load_table(data, NULL); 328 if (ACPI_FAILURE(acpi_ret)) { 329 pr_err("efivar_ssdt: failed to load table: %u\n", 330 acpi_ret); 331 } else { 332 /* 333 * The @data will be in use by ACPI engine, 334 * do not free it! 335 */ 336 continue; 337 } 338 } else { 339 pr_err("efivar_ssdt: failed to get var data: 0x%lx\n", status); 340 } 341 kfree(data); 342 } 343 out: 344 kfree(name); 345 return ret; 346 } 347 #else 348 static inline int efivar_ssdt_load(void) { return 0; } 349 #endif 350 351 #ifdef CONFIG_DEBUG_FS 352 353 #define EFI_DEBUGFS_MAX_BLOBS 32 354 355 static struct debugfs_blob_wrapper debugfs_blob[EFI_DEBUGFS_MAX_BLOBS]; 356 357 static void __init efi_debugfs_init(void) 358 { 359 struct dentry *efi_debugfs; 360 efi_memory_desc_t *md; 361 char name[32]; 362 int type_count[EFI_BOOT_SERVICES_DATA + 1] = {}; 363 int i = 0; 364 365 efi_debugfs = debugfs_create_dir("efi", NULL); 366 if (IS_ERR(efi_debugfs)) 367 return; 368 369 for_each_efi_memory_desc(md) { 370 switch (md->type) { 371 case EFI_BOOT_SERVICES_CODE: 372 snprintf(name, sizeof(name), "boot_services_code%d", 373 type_count[md->type]++); 374 break; 375 case EFI_BOOT_SERVICES_DATA: 376 snprintf(name, sizeof(name), "boot_services_data%d", 377 type_count[md->type]++); 378 break; 379 default: 380 continue; 381 } 382 383 if (i >= EFI_DEBUGFS_MAX_BLOBS) { 384 pr_warn("More then %d EFI boot service segments, only showing first %d in debugfs\n", 385 EFI_DEBUGFS_MAX_BLOBS, EFI_DEBUGFS_MAX_BLOBS); 386 break; 387 } 388 389 debugfs_blob[i].size = md->num_pages << EFI_PAGE_SHIFT; 390 debugfs_blob[i].data = memremap(md->phys_addr, 391 debugfs_blob[i].size, 392 MEMREMAP_WB); 393 if (!debugfs_blob[i].data) 394 continue; 395 396 debugfs_create_blob(name, 0400, efi_debugfs, &debugfs_blob[i]); 397 i++; 398 } 399 } 400 #else 401 static inline void efi_debugfs_init(void) {} 402 #endif 403 404 static int __init efipostcore_init(void) 405 { 406 if (!efi_enabled(EFI_RUNTIME_SERVICES)) 407 efi.runtime_supported_mask = 0; 408 409 if (efi.runtime_supported_mask) { 410 /* 411 * Since we process only one efi_runtime_service() at a time, an 412 * ordered workqueue (which creates only one execution context) 413 * should suffice for all our needs. 414 */ 415 efi_rts_wq = alloc_ordered_workqueue("efi_runtime", WQ_SYSFS); 416 if (!efi_rts_wq) { 417 pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n"); 418 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 419 efi.runtime_supported_mask = 0; 420 } 421 } 422 return 0; 423 } 424 postcore_initcall(efipostcore_init); 425 426 /* 427 * We register the efi subsystem with the firmware subsystem and the 428 * efivars subsystem with the efi subsystem, if the system was booted with 429 * EFI. 430 */ 431 static int __init efisubsys_init(void) 432 { 433 int error; 434 435 if (!efi_enabled(EFI_BOOT)) 436 return 0; 437 438 if (efi_rt_services_supported(EFI_RT_SUPPORTED_TIME_SERVICES)) 439 platform_device_register_simple("rtc-efi", 0, NULL, 0); 440 441 /* We register the efi directory at /sys/firmware/efi */ 442 efi_kobj = kobject_create_and_add("efi", firmware_kobj); 443 if (!efi_kobj) { 444 pr_err("efi: Firmware registration failed.\n"); 445 error = -ENOMEM; 446 goto err_destroy_wq; 447 } 448 449 if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE | 450 EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME)) { 451 error = generic_ops_register(); 452 if (error) 453 goto err_put; 454 error = efivar_ssdt_load(); 455 if (error) 456 pr_err("efi: failed to load SSDT, error %d.\n", error); 457 platform_device_register_simple("efivars", 0, NULL, 0); 458 } 459 460 BLOCKING_INIT_NOTIFIER_HEAD(&efivar_ops_nh); 461 462 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group); 463 if (error) { 464 pr_err("efi: Sysfs attribute export failed with error %d.\n", 465 error); 466 goto err_unregister; 467 } 468 469 /* and the standard mountpoint for efivarfs */ 470 error = sysfs_create_mount_point(efi_kobj, "efivars"); 471 if (error) { 472 pr_err("efivars: Subsystem registration failed.\n"); 473 goto err_remove_group; 474 } 475 476 if (efi_enabled(EFI_DBG) && efi_enabled(EFI_PRESERVE_BS_REGIONS)) 477 efi_debugfs_init(); 478 479 #ifdef CONFIG_EFI_COCO_SECRET 480 if (efi.coco_secret != EFI_INVALID_TABLE_ADDR) 481 platform_device_register_simple("efi_secret", 0, NULL, 0); 482 #endif 483 484 if (IS_ENABLED(CONFIG_OVMF_DEBUG_LOG) && 485 efi.ovmf_debug_log != EFI_INVALID_TABLE_ADDR) 486 ovmf_log_probe(efi.ovmf_debug_log); 487 488 return 0; 489 490 err_remove_group: 491 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group); 492 err_unregister: 493 if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE | 494 EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME)) 495 generic_ops_unregister(); 496 err_put: 497 kobject_put(efi_kobj); 498 efi_kobj = NULL; 499 err_destroy_wq: 500 if (efi_rts_wq) 501 destroy_workqueue(efi_rts_wq); 502 503 return error; 504 } 505 506 subsys_initcall(efisubsys_init); 507 508 void __init efi_find_mirror(void) 509 { 510 efi_memory_desc_t *md; 511 u64 mirror_size = 0, total_size = 0; 512 513 if (!efi_enabled(EFI_MEMMAP)) 514 return; 515 516 for_each_efi_memory_desc(md) { 517 unsigned long long start = md->phys_addr; 518 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; 519 520 total_size += size; 521 if (md->attribute & EFI_MEMORY_MORE_RELIABLE) { 522 memblock_mark_mirror(start, size); 523 mirror_size += size; 524 } 525 } 526 if (mirror_size) 527 pr_info("Memory: %lldM/%lldM mirrored memory\n", 528 mirror_size>>20, total_size>>20); 529 } 530 531 /* 532 * Find the efi memory descriptor for a given physical address. Given a 533 * physical address, determine if it exists within an EFI Memory Map entry, 534 * and if so, populate the supplied memory descriptor with the appropriate 535 * data. 536 */ 537 int __efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md) 538 { 539 efi_memory_desc_t *md; 540 541 if (!efi_enabled(EFI_MEMMAP)) { 542 pr_err_once("EFI_MEMMAP is not enabled.\n"); 543 return -EINVAL; 544 } 545 546 if (!out_md) { 547 pr_err_once("out_md is null.\n"); 548 return -EINVAL; 549 } 550 551 for_each_efi_memory_desc(md) { 552 u64 size; 553 u64 end; 554 555 /* skip bogus entries (including empty ones) */ 556 if ((md->phys_addr & (EFI_PAGE_SIZE - 1)) || 557 (md->num_pages <= 0) || 558 (md->num_pages > (U64_MAX - md->phys_addr) >> EFI_PAGE_SHIFT)) 559 continue; 560 561 size = md->num_pages << EFI_PAGE_SHIFT; 562 end = md->phys_addr + size; 563 if (phys_addr >= md->phys_addr && phys_addr < end) { 564 memcpy(out_md, md, sizeof(*out_md)); 565 return 0; 566 } 567 } 568 return -ENOENT; 569 } 570 571 extern int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md) 572 __weak __alias(__efi_mem_desc_lookup); 573 EXPORT_SYMBOL_GPL(efi_mem_desc_lookup); 574 575 /* 576 * Calculate the highest address of an efi memory descriptor. 577 */ 578 u64 __init efi_mem_desc_end(efi_memory_desc_t *md) 579 { 580 u64 size = md->num_pages << EFI_PAGE_SHIFT; 581 u64 end = md->phys_addr + size; 582 return end; 583 } 584 585 void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {} 586 587 /** 588 * efi_mem_reserve - Reserve an EFI memory region 589 * @addr: Physical address to reserve 590 * @size: Size of reservation 591 * 592 * Mark a region as reserved from general kernel allocation and 593 * prevent it being released by efi_free_boot_services(). 594 * 595 * This function should be called drivers once they've parsed EFI 596 * configuration tables to figure out where their data lives, e.g. 597 * efi_esrt_init(). 598 */ 599 void __init efi_mem_reserve(phys_addr_t addr, u64 size) 600 { 601 /* efi_mem_reserve() does not work under Xen */ 602 if (WARN_ON_ONCE(efi_enabled(EFI_PARAVIRT))) 603 return; 604 605 if (!memblock_is_region_reserved(addr, size)) 606 memblock_reserve_kern(addr, size); 607 else 608 memblock_reserved_mark_kern(addr, size); 609 610 /* 611 * Some architectures (x86) reserve all boot services ranges 612 * until efi_free_boot_services() because of buggy firmware 613 * implementations. This means the above memblock_reserve() is 614 * superfluous on x86 and instead what it needs to do is 615 * ensure the @start, @size is not freed. 616 */ 617 efi_arch_mem_reserve(addr, size); 618 } 619 620 static const efi_config_table_type_t common_tables[] __initconst = { 621 {ACPI_20_TABLE_GUID, &efi.acpi20, "ACPI 2.0" }, 622 {ACPI_TABLE_GUID, &efi.acpi, "ACPI" }, 623 {SMBIOS_TABLE_GUID, &efi.smbios, "SMBIOS" }, 624 {SMBIOS3_TABLE_GUID, &efi.smbios3, "SMBIOS 3.0" }, 625 {EFI_SYSTEM_RESOURCE_TABLE_GUID, &efi.esrt, "ESRT" }, 626 {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, &efi_mem_attr_table, "MEMATTR" }, 627 {LINUX_EFI_RANDOM_SEED_TABLE_GUID, &efi_rng_seed, "RNG" }, 628 {LINUX_EFI_TPM_EVENT_LOG_GUID, &efi.tpm_log, "TPMEventLog" }, 629 {EFI_TCG2_FINAL_EVENTS_TABLE_GUID, &efi.tpm_final_log, "TPMFinalLog" }, 630 {EFI_CC_FINAL_EVENTS_TABLE_GUID, &efi.tpm_final_log, "CCFinalLog" }, 631 {LINUX_EFI_MEMRESERVE_TABLE_GUID, &mem_reserve, "MEMRESERVE" }, 632 {LINUX_EFI_INITRD_MEDIA_GUID, &initrd, "INITRD" }, 633 {EFI_RT_PROPERTIES_TABLE_GUID, &rt_prop, "RTPROP" }, 634 #ifdef CONFIG_OVMF_DEBUG_LOG 635 {OVMF_MEMORY_LOG_TABLE_GUID, &efi.ovmf_debug_log, "OvmfDebugLog" }, 636 #endif 637 #ifdef CONFIG_EFI_RCI2_TABLE 638 {DELLEMC_EFI_RCI2_TABLE_GUID, &rci2_table_phys }, 639 #endif 640 #ifdef CONFIG_LOAD_UEFI_KEYS 641 {LINUX_EFI_MOK_VARIABLE_TABLE_GUID, &efi.mokvar_table, "MOKvar" }, 642 #endif 643 #ifdef CONFIG_EFI_COCO_SECRET 644 {LINUX_EFI_COCO_SECRET_AREA_GUID, &efi.coco_secret, "CocoSecret" }, 645 #endif 646 #ifdef CONFIG_UNACCEPTED_MEMORY 647 {LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID, &efi.unaccepted, "Unaccepted" }, 648 #endif 649 #ifdef CONFIG_EFI_GENERIC_STUB 650 {LINUX_EFI_PRIMARY_DISPLAY_TABLE_GUID, &primary_display_table }, 651 #endif 652 {}, 653 }; 654 655 static __init int match_config_table(const efi_guid_t *guid, 656 unsigned long table, 657 const efi_config_table_type_t *table_types) 658 { 659 int i; 660 661 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) { 662 if (efi_guidcmp(*guid, table_types[i].guid)) 663 continue; 664 665 if (!efi_config_table_is_usable(guid, table)) { 666 if (table_types[i].name[0]) 667 pr_cont("(%s=0x%lx unusable) ", 668 table_types[i].name, table); 669 return 1; 670 } 671 672 *(table_types[i].ptr) = table; 673 if (table_types[i].name[0]) 674 pr_cont("%s=0x%lx ", table_types[i].name, table); 675 return 1; 676 } 677 678 return 0; 679 } 680 681 /** 682 * reserve_unaccepted - Map and reserve unaccepted configuration table 683 * @unaccepted: Pointer to unaccepted memory table 684 * 685 * memblock_add() makes sure that the table is mapped in direct mapping. During 686 * normal boot it happens automatically because the table is allocated from 687 * usable memory. But during crashkernel boot only memory specifically reserved 688 * for crash scenario is mapped. memblock_add() forces the table to be mapped 689 * in crashkernel case. 690 * 691 * Align the range to the nearest page borders. Ranges smaller than page size 692 * are not going to be mapped. 693 * 694 * memblock_reserve() makes sure that future allocations will not touch the 695 * table. 696 */ 697 698 static __init void reserve_unaccepted(struct efi_unaccepted_memory *unaccepted) 699 { 700 phys_addr_t start, end; 701 702 start = PAGE_ALIGN_DOWN(efi.unaccepted); 703 end = PAGE_ALIGN(efi.unaccepted + sizeof(*unaccepted) + unaccepted->size); 704 705 memblock_add(start, end - start); 706 memblock_reserve(start, end - start); 707 } 708 709 int __init efi_config_parse_tables(const efi_config_table_t *config_tables, 710 int count, 711 const efi_config_table_type_t *arch_tables) 712 { 713 const efi_config_table_64_t *tbl64 = (void *)config_tables; 714 const efi_config_table_32_t *tbl32 = (void *)config_tables; 715 const efi_guid_t *guid; 716 unsigned long table; 717 int i; 718 719 pr_info(""); 720 for (i = 0; i < count; i++) { 721 if (!IS_ENABLED(CONFIG_X86)) { 722 guid = &config_tables[i].guid; 723 table = (unsigned long)config_tables[i].table; 724 } else if (efi_enabled(EFI_64BIT)) { 725 guid = &tbl64[i].guid; 726 table = tbl64[i].table; 727 728 if (IS_ENABLED(CONFIG_X86_32) && 729 tbl64[i].table > U32_MAX) { 730 pr_cont("\n"); 731 pr_err("Table located above 4GB, disabling EFI.\n"); 732 return -EINVAL; 733 } 734 } else { 735 guid = &tbl32[i].guid; 736 table = tbl32[i].table; 737 } 738 739 if (!match_config_table(guid, table, common_tables) && arch_tables) 740 match_config_table(guid, table, arch_tables); 741 } 742 pr_cont("\n"); 743 set_bit(EFI_CONFIG_TABLES, &efi.flags); 744 745 if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) { 746 struct linux_efi_random_seed *seed; 747 u32 size = 0; 748 749 seed = early_memremap(efi_rng_seed, sizeof(*seed)); 750 if (seed != NULL) { 751 size = min_t(u32, seed->size, SZ_1K); // sanity check 752 early_memunmap(seed, sizeof(*seed)); 753 } else { 754 pr_err("Could not map UEFI random seed!\n"); 755 } 756 if (size > 0) { 757 seed = early_memremap(efi_rng_seed, 758 sizeof(*seed) + size); 759 if (seed != NULL) { 760 add_bootloader_randomness(seed->bits, size); 761 memzero_explicit(seed->bits, size); 762 early_memunmap(seed, sizeof(*seed) + size); 763 } else { 764 pr_err("Could not map UEFI random seed!\n"); 765 } 766 } 767 } 768 769 if (!IS_ENABLED(CONFIG_X86_32) && efi_enabled(EFI_MEMMAP)) 770 efi_memattr_init(); 771 772 efi_tpm_eventlog_init(); 773 774 if (mem_reserve != EFI_INVALID_TABLE_ADDR) { 775 unsigned long prsv = mem_reserve; 776 777 while (prsv) { 778 struct linux_efi_memreserve *rsv; 779 u8 *p; 780 781 /* 782 * Just map a full page: that is what we will get 783 * anyway, and it permits us to map the entire entry 784 * before knowing its size. 785 */ 786 p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE), 787 PAGE_SIZE); 788 if (p == NULL) { 789 pr_err("Could not map UEFI memreserve entry!\n"); 790 return -ENOMEM; 791 } 792 793 rsv = (void *)(p + prsv % PAGE_SIZE); 794 795 /* reserve the entry itself */ 796 memblock_reserve(prsv, 797 struct_size(rsv, entry, rsv->size)); 798 799 for (i = 0; i < atomic_read(&rsv->count); i++) { 800 memblock_reserve(rsv->entry[i].base, 801 rsv->entry[i].size); 802 } 803 804 prsv = rsv->next; 805 early_memunmap(p, PAGE_SIZE); 806 } 807 } 808 809 if (rt_prop != EFI_INVALID_TABLE_ADDR) { 810 efi_rt_properties_table_t *tbl; 811 812 tbl = early_memremap(rt_prop, sizeof(*tbl)); 813 if (tbl) { 814 efi.runtime_supported_mask &= tbl->runtime_services_supported; 815 early_memunmap(tbl, sizeof(*tbl)); 816 } 817 } 818 819 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && 820 initrd != EFI_INVALID_TABLE_ADDR && phys_initrd_size == 0) { 821 struct linux_efi_initrd *tbl; 822 823 tbl = early_memremap(initrd, sizeof(*tbl)); 824 if (tbl) { 825 phys_initrd_start = tbl->base; 826 phys_initrd_size = tbl->size; 827 tbl->base = tbl->size = 0; 828 early_memunmap(tbl, sizeof(*tbl)); 829 } 830 } 831 832 if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && 833 efi.unaccepted != EFI_INVALID_TABLE_ADDR) { 834 struct efi_unaccepted_memory *unaccepted; 835 836 unaccepted = early_memremap(efi.unaccepted, sizeof(*unaccepted)); 837 if (unaccepted) { 838 839 if (unaccepted->version == 1) { 840 reserve_unaccepted(unaccepted); 841 } else { 842 efi.unaccepted = EFI_INVALID_TABLE_ADDR; 843 } 844 845 early_memunmap(unaccepted, sizeof(*unaccepted)); 846 } 847 } 848 849 return 0; 850 } 851 852 int __init efi_systab_check_header(const efi_table_hdr_t *systab_hdr) 853 { 854 if (systab_hdr->signature != EFI_SYSTEM_TABLE_SIGNATURE) { 855 pr_err("System table signature incorrect!\n"); 856 return -EINVAL; 857 } 858 859 return 0; 860 } 861 862 static const efi_char16_t *__init map_fw_vendor(unsigned long fw_vendor, 863 size_t size) 864 { 865 const efi_char16_t *ret; 866 867 ret = early_memremap_ro(fw_vendor, size); 868 if (!ret) 869 pr_err("Could not map the firmware vendor!\n"); 870 return ret; 871 } 872 873 static void __init unmap_fw_vendor(const void *fw_vendor, size_t size) 874 { 875 early_memunmap((void *)fw_vendor, size); 876 } 877 878 void __init efi_systab_report_header(const efi_table_hdr_t *systab_hdr, 879 unsigned long fw_vendor) 880 { 881 char vendor[100] = "unknown"; 882 const efi_char16_t *c16; 883 size_t i; 884 u16 rev; 885 886 c16 = map_fw_vendor(fw_vendor, sizeof(vendor) * sizeof(efi_char16_t)); 887 if (c16) { 888 for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i) 889 vendor[i] = c16[i]; 890 vendor[i] = '\0'; 891 892 unmap_fw_vendor(c16, sizeof(vendor) * sizeof(efi_char16_t)); 893 } 894 895 rev = (u16)systab_hdr->revision; 896 pr_info("EFI v%u.%u", systab_hdr->revision >> 16, rev / 10); 897 898 rev %= 10; 899 if (rev) 900 pr_cont(".%u", rev); 901 902 pr_cont(" by %s\n", vendor); 903 904 if (IS_ENABLED(CONFIG_X86_64) && 905 systab_hdr->revision > EFI_1_10_SYSTEM_TABLE_REVISION && 906 !strcmp(vendor, "Apple")) { 907 pr_info("Apple Mac detected, using EFI v1.10 runtime services only\n"); 908 efi.runtime_version = EFI_1_10_SYSTEM_TABLE_REVISION; 909 } 910 } 911 912 static __initdata char memory_type_name[][13] = { 913 "Reserved", 914 "Loader Code", 915 "Loader Data", 916 "Boot Code", 917 "Boot Data", 918 "Runtime Code", 919 "Runtime Data", 920 "Conventional", 921 "Unusable", 922 "ACPI Reclaim", 923 "ACPI Mem NVS", 924 "MMIO", 925 "MMIO Port", 926 "PAL Code", 927 "Persistent", 928 "Unaccepted", 929 }; 930 931 char * __init efi_md_typeattr_format(char *buf, size_t size, 932 const efi_memory_desc_t *md) 933 { 934 char *pos; 935 int type_len; 936 u64 attr; 937 938 pos = buf; 939 if (md->type >= ARRAY_SIZE(memory_type_name)) 940 type_len = snprintf(pos, size, "[type=%u", md->type); 941 else 942 type_len = snprintf(pos, size, "[%-*s", 943 (int)(sizeof(memory_type_name[0]) - 1), 944 memory_type_name[md->type]); 945 if (type_len >= size) 946 return buf; 947 948 pos += type_len; 949 size -= type_len; 950 951 attr = md->attribute; 952 if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT | 953 EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO | 954 EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP | 955 EFI_MEMORY_NV | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO | 956 EFI_MEMORY_MORE_RELIABLE | EFI_MEMORY_HOT_PLUGGABLE | 957 EFI_MEMORY_RUNTIME)) 958 snprintf(pos, size, "|attr=0x%016llx]", 959 (unsigned long long)attr); 960 else 961 snprintf(pos, size, 962 "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]", 963 attr & EFI_MEMORY_RUNTIME ? "RUN" : "", 964 attr & EFI_MEMORY_HOT_PLUGGABLE ? "HP" : "", 965 attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "", 966 attr & EFI_MEMORY_CPU_CRYPTO ? "CC" : "", 967 attr & EFI_MEMORY_SP ? "SP" : "", 968 attr & EFI_MEMORY_NV ? "NV" : "", 969 attr & EFI_MEMORY_XP ? "XP" : "", 970 attr & EFI_MEMORY_RP ? "RP" : "", 971 attr & EFI_MEMORY_WP ? "WP" : "", 972 attr & EFI_MEMORY_RO ? "RO" : "", 973 attr & EFI_MEMORY_UCE ? "UCE" : "", 974 attr & EFI_MEMORY_WB ? "WB" : "", 975 attr & EFI_MEMORY_WT ? "WT" : "", 976 attr & EFI_MEMORY_WC ? "WC" : "", 977 attr & EFI_MEMORY_UC ? "UC" : ""); 978 return buf; 979 } 980 981 /* 982 * efi_mem_attributes - lookup memmap attributes for physical address 983 * @phys_addr: the physical address to lookup 984 * 985 * Search in the EFI memory map for the region covering 986 * @phys_addr. Returns the EFI memory attributes if the region 987 * was found in the memory map, 0 otherwise. 988 */ 989 u64 efi_mem_attributes(unsigned long phys_addr) 990 { 991 efi_memory_desc_t md; 992 993 if (efi_mem_desc_lookup(phys_addr, &md)) 994 return 0; 995 996 return md.attribute; 997 } 998 999 /* 1000 * efi_mem_type - lookup memmap type for physical address 1001 * @phys_addr: the physical address to lookup 1002 * 1003 * Search in the EFI memory map for the region covering @phys_addr. 1004 * Returns the EFI memory type if the region was found in the memory 1005 * map, -EINVAL otherwise. 1006 */ 1007 int efi_mem_type(unsigned long phys_addr) 1008 { 1009 efi_memory_desc_t md; 1010 1011 if (!efi_enabled(EFI_MEMMAP) && !efi_enabled(EFI_PARAVIRT)) 1012 return -ENOTSUPP; 1013 1014 if (efi_mem_desc_lookup(phys_addr, &md)) 1015 return -EINVAL; 1016 1017 return md.type; 1018 } 1019 1020 int efi_status_to_err(efi_status_t status) 1021 { 1022 int err; 1023 1024 switch (status) { 1025 case EFI_SUCCESS: 1026 err = 0; 1027 break; 1028 case EFI_INVALID_PARAMETER: 1029 err = -EINVAL; 1030 break; 1031 case EFI_OUT_OF_RESOURCES: 1032 err = -ENOSPC; 1033 break; 1034 case EFI_DEVICE_ERROR: 1035 err = -EIO; 1036 break; 1037 case EFI_WRITE_PROTECTED: 1038 err = -EROFS; 1039 break; 1040 case EFI_SECURITY_VIOLATION: 1041 err = -EACCES; 1042 break; 1043 case EFI_NOT_FOUND: 1044 err = -ENOENT; 1045 break; 1046 case EFI_ABORTED: 1047 err = -EINTR; 1048 break; 1049 default: 1050 err = -EINVAL; 1051 } 1052 1053 return err; 1054 } 1055 EXPORT_SYMBOL_GPL(efi_status_to_err); 1056 1057 static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock); 1058 static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init; 1059 1060 static int __init efi_memreserve_map_root(void) 1061 { 1062 if (mem_reserve == EFI_INVALID_TABLE_ADDR) 1063 return -ENODEV; 1064 1065 efi_memreserve_root = memremap(mem_reserve, 1066 sizeof(*efi_memreserve_root), 1067 MEMREMAP_WB); 1068 if (WARN_ON_ONCE(!efi_memreserve_root)) 1069 return -ENOMEM; 1070 return 0; 1071 } 1072 1073 static int efi_mem_reserve_iomem(phys_addr_t addr, u64 size) 1074 { 1075 struct resource *res, *parent; 1076 int ret; 1077 1078 res = kzalloc_obj(struct resource, GFP_ATOMIC); 1079 if (!res) 1080 return -ENOMEM; 1081 1082 res->name = "reserved"; 1083 res->flags = IORESOURCE_MEM; 1084 res->start = addr; 1085 res->end = addr + size - 1; 1086 1087 /* we expect a conflict with a 'System RAM' region */ 1088 parent = request_resource_conflict(&iomem_resource, res); 1089 ret = parent ? request_resource(parent, res) : 0; 1090 1091 /* 1092 * Given that efi_mem_reserve_iomem() can be called at any 1093 * time, only call memblock_reserve() if the architecture 1094 * keeps the infrastructure around. 1095 */ 1096 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !ret) 1097 memblock_reserve(addr, size); 1098 1099 return ret; 1100 } 1101 1102 int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size) 1103 { 1104 struct linux_efi_memreserve *rsv; 1105 unsigned long prsv; 1106 int rc, index; 1107 1108 if (efi_memreserve_root == (void *)ULONG_MAX) 1109 return -ENODEV; 1110 1111 if (!efi_memreserve_root) { 1112 rc = efi_memreserve_map_root(); 1113 if (rc) 1114 return rc; 1115 } 1116 1117 /* first try to find a slot in an existing linked list entry */ 1118 for (prsv = efi_memreserve_root->next; prsv; ) { 1119 rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB); 1120 if (!rsv) 1121 return -ENOMEM; 1122 index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size); 1123 if (index < rsv->size) { 1124 rsv->entry[index].base = addr; 1125 rsv->entry[index].size = size; 1126 1127 memunmap(rsv); 1128 return efi_mem_reserve_iomem(addr, size); 1129 } 1130 prsv = rsv->next; 1131 memunmap(rsv); 1132 } 1133 1134 /* no slot found - allocate a new linked list entry */ 1135 rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC); 1136 if (!rsv) 1137 return -ENOMEM; 1138 1139 rc = efi_mem_reserve_iomem(__pa(rsv), SZ_4K); 1140 if (rc) { 1141 free_page((unsigned long)rsv); 1142 return rc; 1143 } 1144 1145 /* 1146 * The memremap() call above assumes that a linux_efi_memreserve entry 1147 * never crosses a page boundary, so let's ensure that this remains true 1148 * even when kexec'ing a 4k pages kernel from a >4k pages kernel, by 1149 * using SZ_4K explicitly in the size calculation below. 1150 */ 1151 rsv->size = EFI_MEMRESERVE_COUNT(SZ_4K); 1152 atomic_set(&rsv->count, 1); 1153 rsv->entry[0].base = addr; 1154 rsv->entry[0].size = size; 1155 1156 spin_lock(&efi_mem_reserve_persistent_lock); 1157 rsv->next = efi_memreserve_root->next; 1158 efi_memreserve_root->next = __pa(rsv); 1159 spin_unlock(&efi_mem_reserve_persistent_lock); 1160 1161 return efi_mem_reserve_iomem(addr, size); 1162 } 1163 1164 static int __init efi_memreserve_root_init(void) 1165 { 1166 if (efi_memreserve_root) 1167 return 0; 1168 if (efi_memreserve_map_root()) 1169 efi_memreserve_root = (void *)ULONG_MAX; 1170 return 0; 1171 } 1172 early_initcall(efi_memreserve_root_init); 1173 1174 #ifdef CONFIG_KEXEC 1175 static int update_efi_random_seed(struct notifier_block *nb, 1176 unsigned long code, void *unused) 1177 { 1178 struct linux_efi_random_seed *seed; 1179 u32 size = 0; 1180 1181 if (!kexec_in_progress) 1182 return NOTIFY_DONE; 1183 1184 seed = memremap(efi_rng_seed, sizeof(*seed), MEMREMAP_WB); 1185 if (seed != NULL) { 1186 size = min(seed->size, EFI_RANDOM_SEED_SIZE); 1187 memunmap(seed); 1188 } else { 1189 pr_err("Could not map UEFI random seed!\n"); 1190 } 1191 if (size > 0) { 1192 seed = memremap(efi_rng_seed, sizeof(*seed) + size, 1193 MEMREMAP_WB); 1194 if (seed != NULL) { 1195 seed->size = size; 1196 get_random_bytes(seed->bits, seed->size); 1197 memunmap(seed); 1198 } else { 1199 pr_err("Could not map UEFI random seed!\n"); 1200 } 1201 } 1202 return NOTIFY_DONE; 1203 } 1204 1205 static struct notifier_block efi_random_seed_nb = { 1206 .notifier_call = update_efi_random_seed, 1207 }; 1208 1209 static int __init register_update_efi_random_seed(void) 1210 { 1211 if (efi_rng_seed == EFI_INVALID_TABLE_ADDR) 1212 return 0; 1213 return register_reboot_notifier(&efi_random_seed_nb); 1214 } 1215 late_initcall(register_update_efi_random_seed); 1216 #endif 1217