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/of_fdt.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 36 #include <asm/early_ioremap.h> 37 38 struct efi __read_mostly efi = { 39 .mps = EFI_INVALID_TABLE_ADDR, 40 .acpi = EFI_INVALID_TABLE_ADDR, 41 .acpi20 = EFI_INVALID_TABLE_ADDR, 42 .smbios = EFI_INVALID_TABLE_ADDR, 43 .smbios3 = EFI_INVALID_TABLE_ADDR, 44 .boot_info = EFI_INVALID_TABLE_ADDR, 45 .hcdp = EFI_INVALID_TABLE_ADDR, 46 .uga = EFI_INVALID_TABLE_ADDR, 47 .fw_vendor = EFI_INVALID_TABLE_ADDR, 48 .runtime = EFI_INVALID_TABLE_ADDR, 49 .config_table = EFI_INVALID_TABLE_ADDR, 50 .esrt = EFI_INVALID_TABLE_ADDR, 51 .properties_table = EFI_INVALID_TABLE_ADDR, 52 .mem_attr_table = EFI_INVALID_TABLE_ADDR, 53 .rng_seed = EFI_INVALID_TABLE_ADDR, 54 .tpm_log = EFI_INVALID_TABLE_ADDR, 55 .tpm_final_log = EFI_INVALID_TABLE_ADDR, 56 .mem_reserve = EFI_INVALID_TABLE_ADDR, 57 }; 58 EXPORT_SYMBOL(efi); 59 60 struct mm_struct efi_mm = { 61 .mm_rb = RB_ROOT, 62 .mm_users = ATOMIC_INIT(2), 63 .mm_count = ATOMIC_INIT(1), 64 .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem), 65 .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock), 66 .mmlist = LIST_HEAD_INIT(efi_mm.mmlist), 67 .cpu_bitmap = { [BITS_TO_LONGS(NR_CPUS)] = 0}, 68 }; 69 70 struct workqueue_struct *efi_rts_wq; 71 72 static bool disable_runtime; 73 static int __init setup_noefi(char *arg) 74 { 75 disable_runtime = true; 76 return 0; 77 } 78 early_param("noefi", setup_noefi); 79 80 bool efi_runtime_disabled(void) 81 { 82 return disable_runtime; 83 } 84 85 bool __pure __efi_soft_reserve_enabled(void) 86 { 87 return !efi_enabled(EFI_MEM_NO_SOFT_RESERVE); 88 } 89 90 static int __init parse_efi_cmdline(char *str) 91 { 92 if (!str) { 93 pr_warn("need at least one option\n"); 94 return -EINVAL; 95 } 96 97 if (parse_option_str(str, "debug")) 98 set_bit(EFI_DBG, &efi.flags); 99 100 if (parse_option_str(str, "noruntime")) 101 disable_runtime = true; 102 103 if (parse_option_str(str, "nosoftreserve")) 104 set_bit(EFI_MEM_NO_SOFT_RESERVE, &efi.flags); 105 106 return 0; 107 } 108 early_param("efi", parse_efi_cmdline); 109 110 struct kobject *efi_kobj; 111 112 /* 113 * Let's not leave out systab information that snuck into 114 * the efivars driver 115 * Note, do not add more fields in systab sysfs file as it breaks sysfs 116 * one value per file rule! 117 */ 118 static ssize_t systab_show(struct kobject *kobj, 119 struct kobj_attribute *attr, char *buf) 120 { 121 char *str = buf; 122 123 if (!kobj || !buf) 124 return -EINVAL; 125 126 if (efi.mps != EFI_INVALID_TABLE_ADDR) 127 str += sprintf(str, "MPS=0x%lx\n", efi.mps); 128 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR) 129 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20); 130 if (efi.acpi != EFI_INVALID_TABLE_ADDR) 131 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi); 132 /* 133 * If both SMBIOS and SMBIOS3 entry points are implemented, the 134 * SMBIOS3 entry point shall be preferred, so we list it first to 135 * let applications stop parsing after the first match. 136 */ 137 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) 138 str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3); 139 if (efi.smbios != EFI_INVALID_TABLE_ADDR) 140 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios); 141 if (efi.hcdp != EFI_INVALID_TABLE_ADDR) 142 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp); 143 if (efi.boot_info != EFI_INVALID_TABLE_ADDR) 144 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info); 145 if (efi.uga != EFI_INVALID_TABLE_ADDR) 146 str += sprintf(str, "UGA=0x%lx\n", efi.uga); 147 148 return str - buf; 149 } 150 151 static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400); 152 153 #define EFI_FIELD(var) efi.var 154 155 #define EFI_ATTR_SHOW(name) \ 156 static ssize_t name##_show(struct kobject *kobj, \ 157 struct kobj_attribute *attr, char *buf) \ 158 { \ 159 return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \ 160 } 161 162 EFI_ATTR_SHOW(fw_vendor); 163 EFI_ATTR_SHOW(runtime); 164 EFI_ATTR_SHOW(config_table); 165 166 static ssize_t fw_platform_size_show(struct kobject *kobj, 167 struct kobj_attribute *attr, char *buf) 168 { 169 return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32); 170 } 171 172 static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor); 173 static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime); 174 static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table); 175 static struct kobj_attribute efi_attr_fw_platform_size = 176 __ATTR_RO(fw_platform_size); 177 178 static struct attribute *efi_subsys_attrs[] = { 179 &efi_attr_systab.attr, 180 &efi_attr_fw_vendor.attr, 181 &efi_attr_runtime.attr, 182 &efi_attr_config_table.attr, 183 &efi_attr_fw_platform_size.attr, 184 NULL, 185 }; 186 187 static umode_t efi_attr_is_visible(struct kobject *kobj, 188 struct attribute *attr, int n) 189 { 190 if (attr == &efi_attr_fw_vendor.attr) { 191 if (efi_enabled(EFI_PARAVIRT) || 192 efi.fw_vendor == EFI_INVALID_TABLE_ADDR) 193 return 0; 194 } else if (attr == &efi_attr_runtime.attr) { 195 if (efi.runtime == EFI_INVALID_TABLE_ADDR) 196 return 0; 197 } else if (attr == &efi_attr_config_table.attr) { 198 if (efi.config_table == EFI_INVALID_TABLE_ADDR) 199 return 0; 200 } 201 202 return attr->mode; 203 } 204 205 static const struct attribute_group efi_subsys_attr_group = { 206 .attrs = efi_subsys_attrs, 207 .is_visible = efi_attr_is_visible, 208 }; 209 210 static struct efivars generic_efivars; 211 static struct efivar_operations generic_ops; 212 213 static int generic_ops_register(void) 214 { 215 generic_ops.get_variable = efi.get_variable; 216 generic_ops.set_variable = efi.set_variable; 217 generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking; 218 generic_ops.get_next_variable = efi.get_next_variable; 219 generic_ops.query_variable_store = efi_query_variable_store; 220 221 return efivars_register(&generic_efivars, &generic_ops, efi_kobj); 222 } 223 224 static void generic_ops_unregister(void) 225 { 226 efivars_unregister(&generic_efivars); 227 } 228 229 #if IS_ENABLED(CONFIG_ACPI) 230 #define EFIVAR_SSDT_NAME_MAX 16 231 static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata; 232 static int __init efivar_ssdt_setup(char *str) 233 { 234 int ret = security_locked_down(LOCKDOWN_ACPI_TABLES); 235 236 if (ret) 237 return ret; 238 239 if (strlen(str) < sizeof(efivar_ssdt)) 240 memcpy(efivar_ssdt, str, strlen(str)); 241 else 242 pr_warn("efivar_ssdt: name too long: %s\n", str); 243 return 0; 244 } 245 __setup("efivar_ssdt=", efivar_ssdt_setup); 246 247 static __init int efivar_ssdt_iter(efi_char16_t *name, efi_guid_t vendor, 248 unsigned long name_size, void *data) 249 { 250 struct efivar_entry *entry; 251 struct list_head *list = data; 252 char utf8_name[EFIVAR_SSDT_NAME_MAX]; 253 int limit = min_t(unsigned long, EFIVAR_SSDT_NAME_MAX, name_size); 254 255 ucs2_as_utf8(utf8_name, name, limit - 1); 256 if (strncmp(utf8_name, efivar_ssdt, limit) != 0) 257 return 0; 258 259 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 260 if (!entry) 261 return 0; 262 263 memcpy(entry->var.VariableName, name, name_size); 264 memcpy(&entry->var.VendorGuid, &vendor, sizeof(efi_guid_t)); 265 266 efivar_entry_add(entry, list); 267 268 return 0; 269 } 270 271 static __init int efivar_ssdt_load(void) 272 { 273 LIST_HEAD(entries); 274 struct efivar_entry *entry, *aux; 275 unsigned long size; 276 void *data; 277 int ret; 278 279 if (!efivar_ssdt[0]) 280 return 0; 281 282 ret = efivar_init(efivar_ssdt_iter, &entries, true, &entries); 283 284 list_for_each_entry_safe(entry, aux, &entries, list) { 285 pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt, 286 &entry->var.VendorGuid); 287 288 list_del(&entry->list); 289 290 ret = efivar_entry_size(entry, &size); 291 if (ret) { 292 pr_err("failed to get var size\n"); 293 goto free_entry; 294 } 295 296 data = kmalloc(size, GFP_KERNEL); 297 if (!data) { 298 ret = -ENOMEM; 299 goto free_entry; 300 } 301 302 ret = efivar_entry_get(entry, NULL, &size, data); 303 if (ret) { 304 pr_err("failed to get var data\n"); 305 goto free_data; 306 } 307 308 ret = acpi_load_table(data, NULL); 309 if (ret) { 310 pr_err("failed to load table: %d\n", ret); 311 goto free_data; 312 } 313 314 goto free_entry; 315 316 free_data: 317 kfree(data); 318 319 free_entry: 320 kfree(entry); 321 } 322 323 return ret; 324 } 325 #else 326 static inline int efivar_ssdt_load(void) { return 0; } 327 #endif 328 329 #ifdef CONFIG_DEBUG_FS 330 331 #define EFI_DEBUGFS_MAX_BLOBS 32 332 333 static struct debugfs_blob_wrapper debugfs_blob[EFI_DEBUGFS_MAX_BLOBS]; 334 335 static void __init efi_debugfs_init(void) 336 { 337 struct dentry *efi_debugfs; 338 efi_memory_desc_t *md; 339 char name[32]; 340 int type_count[EFI_BOOT_SERVICES_DATA + 1] = {}; 341 int i = 0; 342 343 efi_debugfs = debugfs_create_dir("efi", NULL); 344 if (IS_ERR_OR_NULL(efi_debugfs)) 345 return; 346 347 for_each_efi_memory_desc(md) { 348 switch (md->type) { 349 case EFI_BOOT_SERVICES_CODE: 350 snprintf(name, sizeof(name), "boot_services_code%d", 351 type_count[md->type]++); 352 break; 353 case EFI_BOOT_SERVICES_DATA: 354 snprintf(name, sizeof(name), "boot_services_data%d", 355 type_count[md->type]++); 356 break; 357 default: 358 continue; 359 } 360 361 if (i >= EFI_DEBUGFS_MAX_BLOBS) { 362 pr_warn("More then %d EFI boot service segments, only showing first %d in debugfs\n", 363 EFI_DEBUGFS_MAX_BLOBS, EFI_DEBUGFS_MAX_BLOBS); 364 break; 365 } 366 367 debugfs_blob[i].size = md->num_pages << EFI_PAGE_SHIFT; 368 debugfs_blob[i].data = memremap(md->phys_addr, 369 debugfs_blob[i].size, 370 MEMREMAP_WB); 371 if (!debugfs_blob[i].data) 372 continue; 373 374 debugfs_create_blob(name, 0400, efi_debugfs, &debugfs_blob[i]); 375 i++; 376 } 377 } 378 #else 379 static inline void efi_debugfs_init(void) {} 380 #endif 381 382 /* 383 * We register the efi subsystem with the firmware subsystem and the 384 * efivars subsystem with the efi subsystem, if the system was booted with 385 * EFI. 386 */ 387 static int __init efisubsys_init(void) 388 { 389 int error; 390 391 if (!efi_enabled(EFI_BOOT)) 392 return 0; 393 394 /* 395 * Since we process only one efi_runtime_service() at a time, an 396 * ordered workqueue (which creates only one execution context) 397 * should suffice all our needs. 398 */ 399 efi_rts_wq = alloc_ordered_workqueue("efi_rts_wq", 0); 400 if (!efi_rts_wq) { 401 pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n"); 402 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 403 return 0; 404 } 405 406 /* We register the efi directory at /sys/firmware/efi */ 407 efi_kobj = kobject_create_and_add("efi", firmware_kobj); 408 if (!efi_kobj) { 409 pr_err("efi: Firmware registration failed.\n"); 410 return -ENOMEM; 411 } 412 413 error = generic_ops_register(); 414 if (error) 415 goto err_put; 416 417 if (efi_enabled(EFI_RUNTIME_SERVICES)) 418 efivar_ssdt_load(); 419 420 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group); 421 if (error) { 422 pr_err("efi: Sysfs attribute export failed with error %d.\n", 423 error); 424 goto err_unregister; 425 } 426 427 error = efi_runtime_map_init(efi_kobj); 428 if (error) 429 goto err_remove_group; 430 431 /* and the standard mountpoint for efivarfs */ 432 error = sysfs_create_mount_point(efi_kobj, "efivars"); 433 if (error) { 434 pr_err("efivars: Subsystem registration failed.\n"); 435 goto err_remove_group; 436 } 437 438 if (efi_enabled(EFI_DBG) && efi_enabled(EFI_PRESERVE_BS_REGIONS)) 439 efi_debugfs_init(); 440 441 return 0; 442 443 err_remove_group: 444 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group); 445 err_unregister: 446 generic_ops_unregister(); 447 err_put: 448 kobject_put(efi_kobj); 449 return error; 450 } 451 452 subsys_initcall(efisubsys_init); 453 454 /* 455 * Find the efi memory descriptor for a given physical address. Given a 456 * physical address, determine if it exists within an EFI Memory Map entry, 457 * and if so, populate the supplied memory descriptor with the appropriate 458 * data. 459 */ 460 int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md) 461 { 462 efi_memory_desc_t *md; 463 464 if (!efi_enabled(EFI_MEMMAP)) { 465 pr_err_once("EFI_MEMMAP is not enabled.\n"); 466 return -EINVAL; 467 } 468 469 if (!out_md) { 470 pr_err_once("out_md is null.\n"); 471 return -EINVAL; 472 } 473 474 for_each_efi_memory_desc(md) { 475 u64 size; 476 u64 end; 477 478 size = md->num_pages << EFI_PAGE_SHIFT; 479 end = md->phys_addr + size; 480 if (phys_addr >= md->phys_addr && phys_addr < end) { 481 memcpy(out_md, md, sizeof(*out_md)); 482 return 0; 483 } 484 } 485 return -ENOENT; 486 } 487 488 /* 489 * Calculate the highest address of an efi memory descriptor. 490 */ 491 u64 __init efi_mem_desc_end(efi_memory_desc_t *md) 492 { 493 u64 size = md->num_pages << EFI_PAGE_SHIFT; 494 u64 end = md->phys_addr + size; 495 return end; 496 } 497 498 void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {} 499 500 /** 501 * efi_mem_reserve - Reserve an EFI memory region 502 * @addr: Physical address to reserve 503 * @size: Size of reservation 504 * 505 * Mark a region as reserved from general kernel allocation and 506 * prevent it being released by efi_free_boot_services(). 507 * 508 * This function should be called drivers once they've parsed EFI 509 * configuration tables to figure out where their data lives, e.g. 510 * efi_esrt_init(). 511 */ 512 void __init efi_mem_reserve(phys_addr_t addr, u64 size) 513 { 514 if (!memblock_is_region_reserved(addr, size)) 515 memblock_reserve(addr, size); 516 517 /* 518 * Some architectures (x86) reserve all boot services ranges 519 * until efi_free_boot_services() because of buggy firmware 520 * implementations. This means the above memblock_reserve() is 521 * superfluous on x86 and instead what it needs to do is 522 * ensure the @start, @size is not freed. 523 */ 524 efi_arch_mem_reserve(addr, size); 525 } 526 527 static __initdata efi_config_table_type_t common_tables[] = { 528 {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20}, 529 {ACPI_TABLE_GUID, "ACPI", &efi.acpi}, 530 {HCDP_TABLE_GUID, "HCDP", &efi.hcdp}, 531 {MPS_TABLE_GUID, "MPS", &efi.mps}, 532 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios}, 533 {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3}, 534 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga}, 535 {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt}, 536 {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table}, 537 {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table}, 538 {LINUX_EFI_RANDOM_SEED_TABLE_GUID, "RNG", &efi.rng_seed}, 539 {LINUX_EFI_TPM_EVENT_LOG_GUID, "TPMEventLog", &efi.tpm_log}, 540 {LINUX_EFI_TPM_FINAL_LOG_GUID, "TPMFinalLog", &efi.tpm_final_log}, 541 {LINUX_EFI_MEMRESERVE_TABLE_GUID, "MEMRESERVE", &efi.mem_reserve}, 542 #ifdef CONFIG_EFI_RCI2_TABLE 543 {DELLEMC_EFI_RCI2_TABLE_GUID, NULL, &rci2_table_phys}, 544 #endif 545 {NULL_GUID, NULL, NULL}, 546 }; 547 548 static __init int match_config_table(efi_guid_t *guid, 549 unsigned long table, 550 efi_config_table_type_t *table_types) 551 { 552 int i; 553 554 if (table_types) { 555 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) { 556 if (!efi_guidcmp(*guid, table_types[i].guid)) { 557 *(table_types[i].ptr) = table; 558 if (table_types[i].name) 559 pr_cont(" %s=0x%lx ", 560 table_types[i].name, table); 561 return 1; 562 } 563 } 564 } 565 566 return 0; 567 } 568 569 int __init efi_config_parse_tables(void *config_tables, int count, int sz, 570 efi_config_table_type_t *arch_tables) 571 { 572 void *tablep; 573 int i; 574 575 tablep = config_tables; 576 pr_info(""); 577 for (i = 0; i < count; i++) { 578 efi_guid_t guid; 579 unsigned long table; 580 581 if (efi_enabled(EFI_64BIT)) { 582 u64 table64; 583 guid = ((efi_config_table_64_t *)tablep)->guid; 584 table64 = ((efi_config_table_64_t *)tablep)->table; 585 table = table64; 586 #ifndef CONFIG_64BIT 587 if (table64 >> 32) { 588 pr_cont("\n"); 589 pr_err("Table located above 4GB, disabling EFI.\n"); 590 return -EINVAL; 591 } 592 #endif 593 } else { 594 guid = ((efi_config_table_32_t *)tablep)->guid; 595 table = ((efi_config_table_32_t *)tablep)->table; 596 } 597 598 if (!match_config_table(&guid, table, common_tables)) 599 match_config_table(&guid, table, arch_tables); 600 601 tablep += sz; 602 } 603 pr_cont("\n"); 604 set_bit(EFI_CONFIG_TABLES, &efi.flags); 605 606 if (efi.rng_seed != EFI_INVALID_TABLE_ADDR) { 607 struct linux_efi_random_seed *seed; 608 u32 size = 0; 609 610 seed = early_memremap(efi.rng_seed, sizeof(*seed)); 611 if (seed != NULL) { 612 size = READ_ONCE(seed->size); 613 early_memunmap(seed, sizeof(*seed)); 614 } else { 615 pr_err("Could not map UEFI random seed!\n"); 616 } 617 if (size > 0) { 618 seed = early_memremap(efi.rng_seed, 619 sizeof(*seed) + size); 620 if (seed != NULL) { 621 pr_notice("seeding entropy pool\n"); 622 add_bootloader_randomness(seed->bits, size); 623 early_memunmap(seed, sizeof(*seed) + size); 624 } else { 625 pr_err("Could not map UEFI random seed!\n"); 626 } 627 } 628 } 629 630 if (efi_enabled(EFI_MEMMAP)) 631 efi_memattr_init(); 632 633 efi_tpm_eventlog_init(); 634 635 /* Parse the EFI Properties table if it exists */ 636 if (efi.properties_table != EFI_INVALID_TABLE_ADDR) { 637 efi_properties_table_t *tbl; 638 639 tbl = early_memremap(efi.properties_table, sizeof(*tbl)); 640 if (tbl == NULL) { 641 pr_err("Could not map Properties table!\n"); 642 return -ENOMEM; 643 } 644 645 if (tbl->memory_protection_attribute & 646 EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) 647 set_bit(EFI_NX_PE_DATA, &efi.flags); 648 649 early_memunmap(tbl, sizeof(*tbl)); 650 } 651 652 if (efi.mem_reserve != EFI_INVALID_TABLE_ADDR) { 653 unsigned long prsv = efi.mem_reserve; 654 655 while (prsv) { 656 struct linux_efi_memreserve *rsv; 657 u8 *p; 658 int i; 659 660 /* 661 * Just map a full page: that is what we will get 662 * anyway, and it permits us to map the entire entry 663 * before knowing its size. 664 */ 665 p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE), 666 PAGE_SIZE); 667 if (p == NULL) { 668 pr_err("Could not map UEFI memreserve entry!\n"); 669 return -ENOMEM; 670 } 671 672 rsv = (void *)(p + prsv % PAGE_SIZE); 673 674 /* reserve the entry itself */ 675 memblock_reserve(prsv, EFI_MEMRESERVE_SIZE(rsv->size)); 676 677 for (i = 0; i < atomic_read(&rsv->count); i++) { 678 memblock_reserve(rsv->entry[i].base, 679 rsv->entry[i].size); 680 } 681 682 prsv = rsv->next; 683 early_memunmap(p, PAGE_SIZE); 684 } 685 } 686 687 return 0; 688 } 689 690 int __init efi_config_init(efi_config_table_type_t *arch_tables) 691 { 692 void *config_tables; 693 int sz, ret; 694 695 if (efi.systab->nr_tables == 0) 696 return 0; 697 698 if (efi_enabled(EFI_64BIT)) 699 sz = sizeof(efi_config_table_64_t); 700 else 701 sz = sizeof(efi_config_table_32_t); 702 703 /* 704 * Let's see what config tables the firmware passed to us. 705 */ 706 config_tables = early_memremap(efi.systab->tables, 707 efi.systab->nr_tables * sz); 708 if (config_tables == NULL) { 709 pr_err("Could not map Configuration table!\n"); 710 return -ENOMEM; 711 } 712 713 ret = efi_config_parse_tables(config_tables, efi.systab->nr_tables, sz, 714 arch_tables); 715 716 early_memunmap(config_tables, efi.systab->nr_tables * sz); 717 return ret; 718 } 719 720 #ifdef CONFIG_EFI_VARS_MODULE 721 static int __init efi_load_efivars(void) 722 { 723 struct platform_device *pdev; 724 725 if (!efi_enabled(EFI_RUNTIME_SERVICES)) 726 return 0; 727 728 pdev = platform_device_register_simple("efivars", 0, NULL, 0); 729 return PTR_ERR_OR_ZERO(pdev); 730 } 731 device_initcall(efi_load_efivars); 732 #endif 733 734 #ifdef CONFIG_EFI_PARAMS_FROM_FDT 735 736 #define UEFI_PARAM(name, prop, field) \ 737 { \ 738 { name }, \ 739 { prop }, \ 740 offsetof(struct efi_fdt_params, field), \ 741 sizeof_field(struct efi_fdt_params, field) \ 742 } 743 744 struct params { 745 const char name[32]; 746 const char propname[32]; 747 int offset; 748 int size; 749 }; 750 751 static __initdata struct params fdt_params[] = { 752 UEFI_PARAM("System Table", "linux,uefi-system-table", system_table), 753 UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap), 754 UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size), 755 UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size), 756 UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver) 757 }; 758 759 static __initdata struct params xen_fdt_params[] = { 760 UEFI_PARAM("System Table", "xen,uefi-system-table", system_table), 761 UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap), 762 UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size), 763 UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size), 764 UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver) 765 }; 766 767 #define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params) 768 769 static __initdata struct { 770 const char *uname; 771 const char *subnode; 772 struct params *params; 773 } dt_params[] = { 774 { "hypervisor", "uefi", xen_fdt_params }, 775 { "chosen", NULL, fdt_params }, 776 }; 777 778 struct param_info { 779 int found; 780 void *params; 781 const char *missing; 782 }; 783 784 static int __init __find_uefi_params(unsigned long node, 785 struct param_info *info, 786 struct params *params) 787 { 788 const void *prop; 789 void *dest; 790 u64 val; 791 int i, len; 792 793 for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) { 794 prop = of_get_flat_dt_prop(node, params[i].propname, &len); 795 if (!prop) { 796 info->missing = params[i].name; 797 return 0; 798 } 799 800 dest = info->params + params[i].offset; 801 info->found++; 802 803 val = of_read_number(prop, len / sizeof(u32)); 804 805 if (params[i].size == sizeof(u32)) 806 *(u32 *)dest = val; 807 else 808 *(u64 *)dest = val; 809 810 if (efi_enabled(EFI_DBG)) 811 pr_info(" %s: 0x%0*llx\n", params[i].name, 812 params[i].size * 2, val); 813 } 814 815 return 1; 816 } 817 818 static int __init fdt_find_uefi_params(unsigned long node, const char *uname, 819 int depth, void *data) 820 { 821 struct param_info *info = data; 822 int i; 823 824 for (i = 0; i < ARRAY_SIZE(dt_params); i++) { 825 const char *subnode = dt_params[i].subnode; 826 827 if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) { 828 info->missing = dt_params[i].params[0].name; 829 continue; 830 } 831 832 if (subnode) { 833 int err = of_get_flat_dt_subnode_by_name(node, subnode); 834 835 if (err < 0) 836 return 0; 837 838 node = err; 839 } 840 841 return __find_uefi_params(node, info, dt_params[i].params); 842 } 843 844 return 0; 845 } 846 847 int __init efi_get_fdt_params(struct efi_fdt_params *params) 848 { 849 struct param_info info; 850 int ret; 851 852 pr_info("Getting EFI parameters from FDT:\n"); 853 854 info.found = 0; 855 info.params = params; 856 857 ret = of_scan_flat_dt(fdt_find_uefi_params, &info); 858 if (!info.found) 859 pr_info("UEFI not found.\n"); 860 else if (!ret) 861 pr_err("Can't find '%s' in device tree!\n", 862 info.missing); 863 864 return ret; 865 } 866 #endif /* CONFIG_EFI_PARAMS_FROM_FDT */ 867 868 static __initdata char memory_type_name[][20] = { 869 "Reserved", 870 "Loader Code", 871 "Loader Data", 872 "Boot Code", 873 "Boot Data", 874 "Runtime Code", 875 "Runtime Data", 876 "Conventional Memory", 877 "Unusable Memory", 878 "ACPI Reclaim Memory", 879 "ACPI Memory NVS", 880 "Memory Mapped I/O", 881 "MMIO Port Space", 882 "PAL Code", 883 "Persistent Memory", 884 }; 885 886 char * __init efi_md_typeattr_format(char *buf, size_t size, 887 const efi_memory_desc_t *md) 888 { 889 char *pos; 890 int type_len; 891 u64 attr; 892 893 pos = buf; 894 if (md->type >= ARRAY_SIZE(memory_type_name)) 895 type_len = snprintf(pos, size, "[type=%u", md->type); 896 else 897 type_len = snprintf(pos, size, "[%-*s", 898 (int)(sizeof(memory_type_name[0]) - 1), 899 memory_type_name[md->type]); 900 if (type_len >= size) 901 return buf; 902 903 pos += type_len; 904 size -= type_len; 905 906 attr = md->attribute; 907 if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT | 908 EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO | 909 EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP | 910 EFI_MEMORY_NV | EFI_MEMORY_SP | 911 EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE)) 912 snprintf(pos, size, "|attr=0x%016llx]", 913 (unsigned long long)attr); 914 else 915 snprintf(pos, size, 916 "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]", 917 attr & EFI_MEMORY_RUNTIME ? "RUN" : "", 918 attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "", 919 attr & EFI_MEMORY_SP ? "SP" : "", 920 attr & EFI_MEMORY_NV ? "NV" : "", 921 attr & EFI_MEMORY_XP ? "XP" : "", 922 attr & EFI_MEMORY_RP ? "RP" : "", 923 attr & EFI_MEMORY_WP ? "WP" : "", 924 attr & EFI_MEMORY_RO ? "RO" : "", 925 attr & EFI_MEMORY_UCE ? "UCE" : "", 926 attr & EFI_MEMORY_WB ? "WB" : "", 927 attr & EFI_MEMORY_WT ? "WT" : "", 928 attr & EFI_MEMORY_WC ? "WC" : "", 929 attr & EFI_MEMORY_UC ? "UC" : ""); 930 return buf; 931 } 932 933 /* 934 * IA64 has a funky EFI memory map that doesn't work the same way as 935 * other architectures. 936 */ 937 #ifndef CONFIG_IA64 938 /* 939 * efi_mem_attributes - lookup memmap attributes for physical address 940 * @phys_addr: the physical address to lookup 941 * 942 * Search in the EFI memory map for the region covering 943 * @phys_addr. Returns the EFI memory attributes if the region 944 * was found in the memory map, 0 otherwise. 945 */ 946 u64 efi_mem_attributes(unsigned long phys_addr) 947 { 948 efi_memory_desc_t *md; 949 950 if (!efi_enabled(EFI_MEMMAP)) 951 return 0; 952 953 for_each_efi_memory_desc(md) { 954 if ((md->phys_addr <= phys_addr) && 955 (phys_addr < (md->phys_addr + 956 (md->num_pages << EFI_PAGE_SHIFT)))) 957 return md->attribute; 958 } 959 return 0; 960 } 961 962 /* 963 * efi_mem_type - lookup memmap type for physical address 964 * @phys_addr: the physical address to lookup 965 * 966 * Search in the EFI memory map for the region covering @phys_addr. 967 * Returns the EFI memory type if the region was found in the memory 968 * map, -EINVAL otherwise. 969 */ 970 int efi_mem_type(unsigned long phys_addr) 971 { 972 const efi_memory_desc_t *md; 973 974 if (!efi_enabled(EFI_MEMMAP)) 975 return -ENOTSUPP; 976 977 for_each_efi_memory_desc(md) { 978 if ((md->phys_addr <= phys_addr) && 979 (phys_addr < (md->phys_addr + 980 (md->num_pages << EFI_PAGE_SHIFT)))) 981 return md->type; 982 } 983 return -EINVAL; 984 } 985 #endif 986 987 int efi_status_to_err(efi_status_t status) 988 { 989 int err; 990 991 switch (status) { 992 case EFI_SUCCESS: 993 err = 0; 994 break; 995 case EFI_INVALID_PARAMETER: 996 err = -EINVAL; 997 break; 998 case EFI_OUT_OF_RESOURCES: 999 err = -ENOSPC; 1000 break; 1001 case EFI_DEVICE_ERROR: 1002 err = -EIO; 1003 break; 1004 case EFI_WRITE_PROTECTED: 1005 err = -EROFS; 1006 break; 1007 case EFI_SECURITY_VIOLATION: 1008 err = -EACCES; 1009 break; 1010 case EFI_NOT_FOUND: 1011 err = -ENOENT; 1012 break; 1013 case EFI_ABORTED: 1014 err = -EINTR; 1015 break; 1016 default: 1017 err = -EINVAL; 1018 } 1019 1020 return err; 1021 } 1022 1023 static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock); 1024 static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init; 1025 1026 static int __init efi_memreserve_map_root(void) 1027 { 1028 if (efi.mem_reserve == EFI_INVALID_TABLE_ADDR) 1029 return -ENODEV; 1030 1031 efi_memreserve_root = memremap(efi.mem_reserve, 1032 sizeof(*efi_memreserve_root), 1033 MEMREMAP_WB); 1034 if (WARN_ON_ONCE(!efi_memreserve_root)) 1035 return -ENOMEM; 1036 return 0; 1037 } 1038 1039 static int efi_mem_reserve_iomem(phys_addr_t addr, u64 size) 1040 { 1041 struct resource *res, *parent; 1042 1043 res = kzalloc(sizeof(struct resource), GFP_ATOMIC); 1044 if (!res) 1045 return -ENOMEM; 1046 1047 res->name = "reserved"; 1048 res->flags = IORESOURCE_MEM; 1049 res->start = addr; 1050 res->end = addr + size - 1; 1051 1052 /* we expect a conflict with a 'System RAM' region */ 1053 parent = request_resource_conflict(&iomem_resource, res); 1054 return parent ? request_resource(parent, res) : 0; 1055 } 1056 1057 int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size) 1058 { 1059 struct linux_efi_memreserve *rsv; 1060 unsigned long prsv; 1061 int rc, index; 1062 1063 if (efi_memreserve_root == (void *)ULONG_MAX) 1064 return -ENODEV; 1065 1066 if (!efi_memreserve_root) { 1067 rc = efi_memreserve_map_root(); 1068 if (rc) 1069 return rc; 1070 } 1071 1072 /* first try to find a slot in an existing linked list entry */ 1073 for (prsv = efi_memreserve_root->next; prsv; prsv = rsv->next) { 1074 rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB); 1075 index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size); 1076 if (index < rsv->size) { 1077 rsv->entry[index].base = addr; 1078 rsv->entry[index].size = size; 1079 1080 memunmap(rsv); 1081 return efi_mem_reserve_iomem(addr, size); 1082 } 1083 memunmap(rsv); 1084 } 1085 1086 /* no slot found - allocate a new linked list entry */ 1087 rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC); 1088 if (!rsv) 1089 return -ENOMEM; 1090 1091 rc = efi_mem_reserve_iomem(__pa(rsv), SZ_4K); 1092 if (rc) { 1093 free_page((unsigned long)rsv); 1094 return rc; 1095 } 1096 1097 /* 1098 * The memremap() call above assumes that a linux_efi_memreserve entry 1099 * never crosses a page boundary, so let's ensure that this remains true 1100 * even when kexec'ing a 4k pages kernel from a >4k pages kernel, by 1101 * using SZ_4K explicitly in the size calculation below. 1102 */ 1103 rsv->size = EFI_MEMRESERVE_COUNT(SZ_4K); 1104 atomic_set(&rsv->count, 1); 1105 rsv->entry[0].base = addr; 1106 rsv->entry[0].size = size; 1107 1108 spin_lock(&efi_mem_reserve_persistent_lock); 1109 rsv->next = efi_memreserve_root->next; 1110 efi_memreserve_root->next = __pa(rsv); 1111 spin_unlock(&efi_mem_reserve_persistent_lock); 1112 1113 return efi_mem_reserve_iomem(addr, size); 1114 } 1115 1116 static int __init efi_memreserve_root_init(void) 1117 { 1118 if (efi_memreserve_root) 1119 return 0; 1120 if (efi_memreserve_map_root()) 1121 efi_memreserve_root = (void *)ULONG_MAX; 1122 return 0; 1123 } 1124 early_initcall(efi_memreserve_root_init); 1125 1126 #ifdef CONFIG_KEXEC 1127 static int update_efi_random_seed(struct notifier_block *nb, 1128 unsigned long code, void *unused) 1129 { 1130 struct linux_efi_random_seed *seed; 1131 u32 size = 0; 1132 1133 if (!kexec_in_progress) 1134 return NOTIFY_DONE; 1135 1136 seed = memremap(efi.rng_seed, sizeof(*seed), MEMREMAP_WB); 1137 if (seed != NULL) { 1138 size = min(seed->size, EFI_RANDOM_SEED_SIZE); 1139 memunmap(seed); 1140 } else { 1141 pr_err("Could not map UEFI random seed!\n"); 1142 } 1143 if (size > 0) { 1144 seed = memremap(efi.rng_seed, sizeof(*seed) + size, 1145 MEMREMAP_WB); 1146 if (seed != NULL) { 1147 seed->size = size; 1148 get_random_bytes(seed->bits, seed->size); 1149 memunmap(seed); 1150 } else { 1151 pr_err("Could not map UEFI random seed!\n"); 1152 } 1153 } 1154 return NOTIFY_DONE; 1155 } 1156 1157 static struct notifier_block efi_random_seed_nb = { 1158 .notifier_call = update_efi_random_seed, 1159 }; 1160 1161 static int register_update_efi_random_seed(void) 1162 { 1163 if (efi.rng_seed == EFI_INVALID_TABLE_ADDR) 1164 return 0; 1165 return register_reboot_notifier(&efi_random_seed_nb); 1166 } 1167 late_initcall(register_update_efi_random_seed); 1168 #endif 1169