0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0016
0017 #include <linux/kobject.h>
0018 #include <linux/module.h>
0019 #include <linux/init.h>
0020 #include <linux/debugfs.h>
0021 #include <linux/device.h>
0022 #include <linux/efi.h>
0023 #include <linux/of.h>
0024 #include <linux/io.h>
0025 #include <linux/kexec.h>
0026 #include <linux/platform_device.h>
0027 #include <linux/random.h>
0028 #include <linux/reboot.h>
0029 #include <linux/slab.h>
0030 #include <linux/acpi.h>
0031 #include <linux/ucs2_string.h>
0032 #include <linux/memblock.h>
0033 #include <linux/security.h>
0034
0035 #include <asm/early_ioremap.h>
0036
0037 struct efi __read_mostly efi = {
0038 .runtime_supported_mask = EFI_RT_SUPPORTED_ALL,
0039 .acpi = EFI_INVALID_TABLE_ADDR,
0040 .acpi20 = EFI_INVALID_TABLE_ADDR,
0041 .smbios = EFI_INVALID_TABLE_ADDR,
0042 .smbios3 = EFI_INVALID_TABLE_ADDR,
0043 .esrt = EFI_INVALID_TABLE_ADDR,
0044 .tpm_log = EFI_INVALID_TABLE_ADDR,
0045 .tpm_final_log = EFI_INVALID_TABLE_ADDR,
0046 #ifdef CONFIG_LOAD_UEFI_KEYS
0047 .mokvar_table = EFI_INVALID_TABLE_ADDR,
0048 #endif
0049 #ifdef CONFIG_EFI_COCO_SECRET
0050 .coco_secret = EFI_INVALID_TABLE_ADDR,
0051 #endif
0052 };
0053 EXPORT_SYMBOL(efi);
0054
0055 unsigned long __ro_after_init efi_rng_seed = EFI_INVALID_TABLE_ADDR;
0056 static unsigned long __initdata mem_reserve = EFI_INVALID_TABLE_ADDR;
0057 static unsigned long __initdata rt_prop = EFI_INVALID_TABLE_ADDR;
0058
0059 struct mm_struct efi_mm = {
0060 .mm_rb = RB_ROOT,
0061 .mm_users = ATOMIC_INIT(2),
0062 .mm_count = ATOMIC_INIT(1),
0063 .write_protect_seq = SEQCNT_ZERO(efi_mm.write_protect_seq),
0064 MMAP_LOCK_INITIALIZER(efi_mm)
0065 .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
0066 .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),
0067 .cpu_bitmap = { [BITS_TO_LONGS(NR_CPUS)] = 0},
0068 };
0069
0070 struct workqueue_struct *efi_rts_wq;
0071
0072 static bool disable_runtime = IS_ENABLED(CONFIG_EFI_DISABLE_RUNTIME);
0073 static int __init setup_noefi(char *arg)
0074 {
0075 disable_runtime = true;
0076 return 0;
0077 }
0078 early_param("noefi", setup_noefi);
0079
0080 bool efi_runtime_disabled(void)
0081 {
0082 return disable_runtime;
0083 }
0084
0085 bool __pure __efi_soft_reserve_enabled(void)
0086 {
0087 return !efi_enabled(EFI_MEM_NO_SOFT_RESERVE);
0088 }
0089
0090 static int __init parse_efi_cmdline(char *str)
0091 {
0092 if (!str) {
0093 pr_warn("need at least one option\n");
0094 return -EINVAL;
0095 }
0096
0097 if (parse_option_str(str, "debug"))
0098 set_bit(EFI_DBG, &efi.flags);
0099
0100 if (parse_option_str(str, "noruntime"))
0101 disable_runtime = true;
0102
0103 if (parse_option_str(str, "runtime"))
0104 disable_runtime = false;
0105
0106 if (parse_option_str(str, "nosoftreserve"))
0107 set_bit(EFI_MEM_NO_SOFT_RESERVE, &efi.flags);
0108
0109 return 0;
0110 }
0111 early_param("efi", parse_efi_cmdline);
0112
0113 struct kobject *efi_kobj;
0114
0115
0116
0117
0118
0119
0120
0121 static ssize_t systab_show(struct kobject *kobj,
0122 struct kobj_attribute *attr, char *buf)
0123 {
0124 char *str = buf;
0125
0126 if (!kobj || !buf)
0127 return -EINVAL;
0128
0129 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
0130 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
0131 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
0132 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
0133
0134
0135
0136
0137
0138 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
0139 str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
0140 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
0141 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
0142
0143 if (IS_ENABLED(CONFIG_IA64) || IS_ENABLED(CONFIG_X86))
0144 str = efi_systab_show_arch(str);
0145
0146 return str - buf;
0147 }
0148
0149 static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
0150
0151 static ssize_t fw_platform_size_show(struct kobject *kobj,
0152 struct kobj_attribute *attr, char *buf)
0153 {
0154 return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32);
0155 }
0156
0157 extern __weak struct kobj_attribute efi_attr_fw_vendor;
0158 extern __weak struct kobj_attribute efi_attr_runtime;
0159 extern __weak struct kobj_attribute efi_attr_config_table;
0160 static struct kobj_attribute efi_attr_fw_platform_size =
0161 __ATTR_RO(fw_platform_size);
0162
0163 static struct attribute *efi_subsys_attrs[] = {
0164 &efi_attr_systab.attr,
0165 &efi_attr_fw_platform_size.attr,
0166 &efi_attr_fw_vendor.attr,
0167 &efi_attr_runtime.attr,
0168 &efi_attr_config_table.attr,
0169 NULL,
0170 };
0171
0172 umode_t __weak efi_attr_is_visible(struct kobject *kobj, struct attribute *attr,
0173 int n)
0174 {
0175 return attr->mode;
0176 }
0177
0178 static const struct attribute_group efi_subsys_attr_group = {
0179 .attrs = efi_subsys_attrs,
0180 .is_visible = efi_attr_is_visible,
0181 };
0182
0183 static struct efivars generic_efivars;
0184 static struct efivar_operations generic_ops;
0185
0186 static int generic_ops_register(void)
0187 {
0188 generic_ops.get_variable = efi.get_variable;
0189 generic_ops.get_next_variable = efi.get_next_variable;
0190 generic_ops.query_variable_store = efi_query_variable_store;
0191
0192 if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE)) {
0193 generic_ops.set_variable = efi.set_variable;
0194 generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
0195 }
0196 return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
0197 }
0198
0199 static void generic_ops_unregister(void)
0200 {
0201 efivars_unregister(&generic_efivars);
0202 }
0203
0204 #ifdef CONFIG_EFI_CUSTOM_SSDT_OVERLAYS
0205 #define EFIVAR_SSDT_NAME_MAX 16UL
0206 static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
0207 static int __init efivar_ssdt_setup(char *str)
0208 {
0209 int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
0210
0211 if (ret)
0212 return ret;
0213
0214 if (strlen(str) < sizeof(efivar_ssdt))
0215 memcpy(efivar_ssdt, str, strlen(str));
0216 else
0217 pr_warn("efivar_ssdt: name too long: %s\n", str);
0218 return 1;
0219 }
0220 __setup("efivar_ssdt=", efivar_ssdt_setup);
0221
0222 static __init int efivar_ssdt_load(void)
0223 {
0224 unsigned long name_size = 256;
0225 efi_char16_t *name = NULL;
0226 efi_status_t status;
0227 efi_guid_t guid;
0228
0229 if (!efivar_ssdt[0])
0230 return 0;
0231
0232 name = kzalloc(name_size, GFP_KERNEL);
0233 if (!name)
0234 return -ENOMEM;
0235
0236 for (;;) {
0237 char utf8_name[EFIVAR_SSDT_NAME_MAX];
0238 unsigned long data_size = 0;
0239 void *data;
0240 int limit;
0241
0242 status = efi.get_next_variable(&name_size, name, &guid);
0243 if (status == EFI_NOT_FOUND) {
0244 break;
0245 } else if (status == EFI_BUFFER_TOO_SMALL) {
0246 name = krealloc(name, name_size, GFP_KERNEL);
0247 if (!name)
0248 return -ENOMEM;
0249 continue;
0250 }
0251
0252 limit = min(EFIVAR_SSDT_NAME_MAX, name_size);
0253 ucs2_as_utf8(utf8_name, name, limit - 1);
0254 if (strncmp(utf8_name, efivar_ssdt, limit) != 0)
0255 continue;
0256
0257 pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt, &guid);
0258
0259 status = efi.get_variable(name, &guid, NULL, &data_size, NULL);
0260 if (status != EFI_BUFFER_TOO_SMALL || !data_size)
0261 return -EIO;
0262
0263 data = kmalloc(data_size, GFP_KERNEL);
0264 if (!data)
0265 return -ENOMEM;
0266
0267 status = efi.get_variable(name, &guid, NULL, &data_size, data);
0268 if (status == EFI_SUCCESS) {
0269 acpi_status ret = acpi_load_table(data, NULL);
0270 if (ret)
0271 pr_err("failed to load table: %u\n", ret);
0272 } else {
0273 pr_err("failed to get var data: 0x%lx\n", status);
0274 }
0275 kfree(data);
0276 }
0277 return 0;
0278 }
0279 #else
0280 static inline int efivar_ssdt_load(void) { return 0; }
0281 #endif
0282
0283 #ifdef CONFIG_DEBUG_FS
0284
0285 #define EFI_DEBUGFS_MAX_BLOBS 32
0286
0287 static struct debugfs_blob_wrapper debugfs_blob[EFI_DEBUGFS_MAX_BLOBS];
0288
0289 static void __init efi_debugfs_init(void)
0290 {
0291 struct dentry *efi_debugfs;
0292 efi_memory_desc_t *md;
0293 char name[32];
0294 int type_count[EFI_BOOT_SERVICES_DATA + 1] = {};
0295 int i = 0;
0296
0297 efi_debugfs = debugfs_create_dir("efi", NULL);
0298 if (IS_ERR_OR_NULL(efi_debugfs))
0299 return;
0300
0301 for_each_efi_memory_desc(md) {
0302 switch (md->type) {
0303 case EFI_BOOT_SERVICES_CODE:
0304 snprintf(name, sizeof(name), "boot_services_code%d",
0305 type_count[md->type]++);
0306 break;
0307 case EFI_BOOT_SERVICES_DATA:
0308 snprintf(name, sizeof(name), "boot_services_data%d",
0309 type_count[md->type]++);
0310 break;
0311 default:
0312 continue;
0313 }
0314
0315 if (i >= EFI_DEBUGFS_MAX_BLOBS) {
0316 pr_warn("More then %d EFI boot service segments, only showing first %d in debugfs\n",
0317 EFI_DEBUGFS_MAX_BLOBS, EFI_DEBUGFS_MAX_BLOBS);
0318 break;
0319 }
0320
0321 debugfs_blob[i].size = md->num_pages << EFI_PAGE_SHIFT;
0322 debugfs_blob[i].data = memremap(md->phys_addr,
0323 debugfs_blob[i].size,
0324 MEMREMAP_WB);
0325 if (!debugfs_blob[i].data)
0326 continue;
0327
0328 debugfs_create_blob(name, 0400, efi_debugfs, &debugfs_blob[i]);
0329 i++;
0330 }
0331 }
0332 #else
0333 static inline void efi_debugfs_init(void) {}
0334 #endif
0335
0336
0337
0338
0339
0340
0341 static int __init efisubsys_init(void)
0342 {
0343 int error;
0344
0345 if (!efi_enabled(EFI_RUNTIME_SERVICES))
0346 efi.runtime_supported_mask = 0;
0347
0348 if (!efi_enabled(EFI_BOOT))
0349 return 0;
0350
0351 if (efi.runtime_supported_mask) {
0352
0353
0354
0355
0356
0357 efi_rts_wq = alloc_ordered_workqueue("efi_rts_wq", 0);
0358 if (!efi_rts_wq) {
0359 pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n");
0360 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
0361 efi.runtime_supported_mask = 0;
0362 return 0;
0363 }
0364 }
0365
0366 if (efi_rt_services_supported(EFI_RT_SUPPORTED_TIME_SERVICES))
0367 platform_device_register_simple("rtc-efi", 0, NULL, 0);
0368
0369
0370 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
0371 if (!efi_kobj) {
0372 pr_err("efi: Firmware registration failed.\n");
0373 destroy_workqueue(efi_rts_wq);
0374 return -ENOMEM;
0375 }
0376
0377 if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
0378 EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME)) {
0379 error = generic_ops_register();
0380 if (error)
0381 goto err_put;
0382 efivar_ssdt_load();
0383 platform_device_register_simple("efivars", 0, NULL, 0);
0384 }
0385
0386 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
0387 if (error) {
0388 pr_err("efi: Sysfs attribute export failed with error %d.\n",
0389 error);
0390 goto err_unregister;
0391 }
0392
0393 error = efi_runtime_map_init(efi_kobj);
0394 if (error)
0395 goto err_remove_group;
0396
0397
0398 error = sysfs_create_mount_point(efi_kobj, "efivars");
0399 if (error) {
0400 pr_err("efivars: Subsystem registration failed.\n");
0401 goto err_remove_group;
0402 }
0403
0404 if (efi_enabled(EFI_DBG) && efi_enabled(EFI_PRESERVE_BS_REGIONS))
0405 efi_debugfs_init();
0406
0407 #ifdef CONFIG_EFI_COCO_SECRET
0408 if (efi.coco_secret != EFI_INVALID_TABLE_ADDR)
0409 platform_device_register_simple("efi_secret", 0, NULL, 0);
0410 #endif
0411
0412 return 0;
0413
0414 err_remove_group:
0415 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
0416 err_unregister:
0417 if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
0418 EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME))
0419 generic_ops_unregister();
0420 err_put:
0421 kobject_put(efi_kobj);
0422 destroy_workqueue(efi_rts_wq);
0423 return error;
0424 }
0425
0426 subsys_initcall(efisubsys_init);
0427
0428 void __init efi_find_mirror(void)
0429 {
0430 efi_memory_desc_t *md;
0431 u64 mirror_size = 0, total_size = 0;
0432
0433 if (!efi_enabled(EFI_MEMMAP))
0434 return;
0435
0436 for_each_efi_memory_desc(md) {
0437 unsigned long long start = md->phys_addr;
0438 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
0439
0440 total_size += size;
0441 if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
0442 memblock_mark_mirror(start, size);
0443 mirror_size += size;
0444 }
0445 }
0446 if (mirror_size)
0447 pr_info("Memory: %lldM/%lldM mirrored memory\n",
0448 mirror_size>>20, total_size>>20);
0449 }
0450
0451
0452
0453
0454
0455
0456
0457 int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
0458 {
0459 efi_memory_desc_t *md;
0460
0461 if (!efi_enabled(EFI_MEMMAP)) {
0462 pr_err_once("EFI_MEMMAP is not enabled.\n");
0463 return -EINVAL;
0464 }
0465
0466 if (!out_md) {
0467 pr_err_once("out_md is null.\n");
0468 return -EINVAL;
0469 }
0470
0471 for_each_efi_memory_desc(md) {
0472 u64 size;
0473 u64 end;
0474
0475 size = md->num_pages << EFI_PAGE_SHIFT;
0476 end = md->phys_addr + size;
0477 if (phys_addr >= md->phys_addr && phys_addr < end) {
0478 memcpy(out_md, md, sizeof(*out_md));
0479 return 0;
0480 }
0481 }
0482 return -ENOENT;
0483 }
0484
0485
0486
0487
0488 u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
0489 {
0490 u64 size = md->num_pages << EFI_PAGE_SHIFT;
0491 u64 end = md->phys_addr + size;
0492 return end;
0493 }
0494
0495 void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509 void __init efi_mem_reserve(phys_addr_t addr, u64 size)
0510 {
0511 if (!memblock_is_region_reserved(addr, size))
0512 memblock_reserve(addr, size);
0513
0514
0515
0516
0517
0518
0519
0520
0521 efi_arch_mem_reserve(addr, size);
0522 }
0523
0524 static const efi_config_table_type_t common_tables[] __initconst = {
0525 {ACPI_20_TABLE_GUID, &efi.acpi20, "ACPI 2.0" },
0526 {ACPI_TABLE_GUID, &efi.acpi, "ACPI" },
0527 {SMBIOS_TABLE_GUID, &efi.smbios, "SMBIOS" },
0528 {SMBIOS3_TABLE_GUID, &efi.smbios3, "SMBIOS 3.0" },
0529 {EFI_SYSTEM_RESOURCE_TABLE_GUID, &efi.esrt, "ESRT" },
0530 {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, &efi_mem_attr_table, "MEMATTR" },
0531 {LINUX_EFI_RANDOM_SEED_TABLE_GUID, &efi_rng_seed, "RNG" },
0532 {LINUX_EFI_TPM_EVENT_LOG_GUID, &efi.tpm_log, "TPMEventLog" },
0533 {LINUX_EFI_TPM_FINAL_LOG_GUID, &efi.tpm_final_log, "TPMFinalLog" },
0534 {LINUX_EFI_MEMRESERVE_TABLE_GUID, &mem_reserve, "MEMRESERVE" },
0535 {EFI_RT_PROPERTIES_TABLE_GUID, &rt_prop, "RTPROP" },
0536 #ifdef CONFIG_EFI_RCI2_TABLE
0537 {DELLEMC_EFI_RCI2_TABLE_GUID, &rci2_table_phys },
0538 #endif
0539 #ifdef CONFIG_LOAD_UEFI_KEYS
0540 {LINUX_EFI_MOK_VARIABLE_TABLE_GUID, &efi.mokvar_table, "MOKvar" },
0541 #endif
0542 #ifdef CONFIG_EFI_COCO_SECRET
0543 {LINUX_EFI_COCO_SECRET_AREA_GUID, &efi.coco_secret, "CocoSecret" },
0544 #endif
0545 {},
0546 };
0547
0548 static __init int match_config_table(const efi_guid_t *guid,
0549 unsigned long table,
0550 const efi_config_table_type_t *table_types)
0551 {
0552 int i;
0553
0554 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
0555 if (!efi_guidcmp(*guid, table_types[i].guid)) {
0556 *(table_types[i].ptr) = table;
0557 if (table_types[i].name[0])
0558 pr_cont("%s=0x%lx ",
0559 table_types[i].name, table);
0560 return 1;
0561 }
0562 }
0563
0564 return 0;
0565 }
0566
0567 int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
0568 int count,
0569 const efi_config_table_type_t *arch_tables)
0570 {
0571 const efi_config_table_64_t *tbl64 = (void *)config_tables;
0572 const efi_config_table_32_t *tbl32 = (void *)config_tables;
0573 const efi_guid_t *guid;
0574 unsigned long table;
0575 int i;
0576
0577 pr_info("");
0578 for (i = 0; i < count; i++) {
0579 if (!IS_ENABLED(CONFIG_X86)) {
0580 guid = &config_tables[i].guid;
0581 table = (unsigned long)config_tables[i].table;
0582 } else if (efi_enabled(EFI_64BIT)) {
0583 guid = &tbl64[i].guid;
0584 table = tbl64[i].table;
0585
0586 if (IS_ENABLED(CONFIG_X86_32) &&
0587 tbl64[i].table > U32_MAX) {
0588 pr_cont("\n");
0589 pr_err("Table located above 4GB, disabling EFI.\n");
0590 return -EINVAL;
0591 }
0592 } else {
0593 guid = &tbl32[i].guid;
0594 table = tbl32[i].table;
0595 }
0596
0597 if (!match_config_table(guid, table, common_tables) && arch_tables)
0598 match_config_table(guid, table, arch_tables);
0599 }
0600 pr_cont("\n");
0601 set_bit(EFI_CONFIG_TABLES, &efi.flags);
0602
0603 if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) {
0604 struct linux_efi_random_seed *seed;
0605 u32 size = 0;
0606
0607 seed = early_memremap(efi_rng_seed, sizeof(*seed));
0608 if (seed != NULL) {
0609 size = READ_ONCE(seed->size);
0610 early_memunmap(seed, sizeof(*seed));
0611 } else {
0612 pr_err("Could not map UEFI random seed!\n");
0613 }
0614 if (size > 0) {
0615 seed = early_memremap(efi_rng_seed,
0616 sizeof(*seed) + size);
0617 if (seed != NULL) {
0618 pr_notice("seeding entropy pool\n");
0619 add_bootloader_randomness(seed->bits, size);
0620 early_memunmap(seed, sizeof(*seed) + size);
0621 } else {
0622 pr_err("Could not map UEFI random seed!\n");
0623 }
0624 }
0625 }
0626
0627 if (!IS_ENABLED(CONFIG_X86_32) && efi_enabled(EFI_MEMMAP))
0628 efi_memattr_init();
0629
0630 efi_tpm_eventlog_init();
0631
0632 if (mem_reserve != EFI_INVALID_TABLE_ADDR) {
0633 unsigned long prsv = mem_reserve;
0634
0635 while (prsv) {
0636 struct linux_efi_memreserve *rsv;
0637 u8 *p;
0638
0639
0640
0641
0642
0643
0644 p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE),
0645 PAGE_SIZE);
0646 if (p == NULL) {
0647 pr_err("Could not map UEFI memreserve entry!\n");
0648 return -ENOMEM;
0649 }
0650
0651 rsv = (void *)(p + prsv % PAGE_SIZE);
0652
0653
0654 memblock_reserve(prsv,
0655 struct_size(rsv, entry, rsv->size));
0656
0657 for (i = 0; i < atomic_read(&rsv->count); i++) {
0658 memblock_reserve(rsv->entry[i].base,
0659 rsv->entry[i].size);
0660 }
0661
0662 prsv = rsv->next;
0663 early_memunmap(p, PAGE_SIZE);
0664 }
0665 }
0666
0667 if (rt_prop != EFI_INVALID_TABLE_ADDR) {
0668 efi_rt_properties_table_t *tbl;
0669
0670 tbl = early_memremap(rt_prop, sizeof(*tbl));
0671 if (tbl) {
0672 efi.runtime_supported_mask &= tbl->runtime_services_supported;
0673 early_memunmap(tbl, sizeof(*tbl));
0674 }
0675 }
0676
0677 return 0;
0678 }
0679
0680 int __init efi_systab_check_header(const efi_table_hdr_t *systab_hdr,
0681 int min_major_version)
0682 {
0683 if (systab_hdr->signature != EFI_SYSTEM_TABLE_SIGNATURE) {
0684 pr_err("System table signature incorrect!\n");
0685 return -EINVAL;
0686 }
0687
0688 if ((systab_hdr->revision >> 16) < min_major_version)
0689 pr_err("Warning: System table version %d.%02d, expected %d.00 or greater!\n",
0690 systab_hdr->revision >> 16,
0691 systab_hdr->revision & 0xffff,
0692 min_major_version);
0693
0694 return 0;
0695 }
0696
0697 #ifndef CONFIG_IA64
0698 static const efi_char16_t *__init map_fw_vendor(unsigned long fw_vendor,
0699 size_t size)
0700 {
0701 const efi_char16_t *ret;
0702
0703 ret = early_memremap_ro(fw_vendor, size);
0704 if (!ret)
0705 pr_err("Could not map the firmware vendor!\n");
0706 return ret;
0707 }
0708
0709 static void __init unmap_fw_vendor(const void *fw_vendor, size_t size)
0710 {
0711 early_memunmap((void *)fw_vendor, size);
0712 }
0713 #else
0714 #define map_fw_vendor(p, s) __va(p)
0715 #define unmap_fw_vendor(v, s)
0716 #endif
0717
0718 void __init efi_systab_report_header(const efi_table_hdr_t *systab_hdr,
0719 unsigned long fw_vendor)
0720 {
0721 char vendor[100] = "unknown";
0722 const efi_char16_t *c16;
0723 size_t i;
0724
0725 c16 = map_fw_vendor(fw_vendor, sizeof(vendor) * sizeof(efi_char16_t));
0726 if (c16) {
0727 for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
0728 vendor[i] = c16[i];
0729 vendor[i] = '\0';
0730
0731 unmap_fw_vendor(c16, sizeof(vendor) * sizeof(efi_char16_t));
0732 }
0733
0734 pr_info("EFI v%u.%.02u by %s\n",
0735 systab_hdr->revision >> 16,
0736 systab_hdr->revision & 0xffff,
0737 vendor);
0738
0739 if (IS_ENABLED(CONFIG_X86_64) &&
0740 systab_hdr->revision > EFI_1_10_SYSTEM_TABLE_REVISION &&
0741 !strcmp(vendor, "Apple")) {
0742 pr_info("Apple Mac detected, using EFI v1.10 runtime services only\n");
0743 efi.runtime_version = EFI_1_10_SYSTEM_TABLE_REVISION;
0744 }
0745 }
0746
0747 static __initdata char memory_type_name[][13] = {
0748 "Reserved",
0749 "Loader Code",
0750 "Loader Data",
0751 "Boot Code",
0752 "Boot Data",
0753 "Runtime Code",
0754 "Runtime Data",
0755 "Conventional",
0756 "Unusable",
0757 "ACPI Reclaim",
0758 "ACPI Mem NVS",
0759 "MMIO",
0760 "MMIO Port",
0761 "PAL Code",
0762 "Persistent",
0763 };
0764
0765 char * __init efi_md_typeattr_format(char *buf, size_t size,
0766 const efi_memory_desc_t *md)
0767 {
0768 char *pos;
0769 int type_len;
0770 u64 attr;
0771
0772 pos = buf;
0773 if (md->type >= ARRAY_SIZE(memory_type_name))
0774 type_len = snprintf(pos, size, "[type=%u", md->type);
0775 else
0776 type_len = snprintf(pos, size, "[%-*s",
0777 (int)(sizeof(memory_type_name[0]) - 1),
0778 memory_type_name[md->type]);
0779 if (type_len >= size)
0780 return buf;
0781
0782 pos += type_len;
0783 size -= type_len;
0784
0785 attr = md->attribute;
0786 if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
0787 EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
0788 EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
0789 EFI_MEMORY_NV | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO |
0790 EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
0791 snprintf(pos, size, "|attr=0x%016llx]",
0792 (unsigned long long)attr);
0793 else
0794 snprintf(pos, size,
0795 "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
0796 attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
0797 attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
0798 attr & EFI_MEMORY_CPU_CRYPTO ? "CC" : "",
0799 attr & EFI_MEMORY_SP ? "SP" : "",
0800 attr & EFI_MEMORY_NV ? "NV" : "",
0801 attr & EFI_MEMORY_XP ? "XP" : "",
0802 attr & EFI_MEMORY_RP ? "RP" : "",
0803 attr & EFI_MEMORY_WP ? "WP" : "",
0804 attr & EFI_MEMORY_RO ? "RO" : "",
0805 attr & EFI_MEMORY_UCE ? "UCE" : "",
0806 attr & EFI_MEMORY_WB ? "WB" : "",
0807 attr & EFI_MEMORY_WT ? "WT" : "",
0808 attr & EFI_MEMORY_WC ? "WC" : "",
0809 attr & EFI_MEMORY_UC ? "UC" : "");
0810 return buf;
0811 }
0812
0813
0814
0815
0816
0817 #ifndef CONFIG_IA64
0818
0819
0820
0821
0822
0823
0824
0825
0826 u64 efi_mem_attributes(unsigned long phys_addr)
0827 {
0828 efi_memory_desc_t *md;
0829
0830 if (!efi_enabled(EFI_MEMMAP))
0831 return 0;
0832
0833 for_each_efi_memory_desc(md) {
0834 if ((md->phys_addr <= phys_addr) &&
0835 (phys_addr < (md->phys_addr +
0836 (md->num_pages << EFI_PAGE_SHIFT))))
0837 return md->attribute;
0838 }
0839 return 0;
0840 }
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850 int efi_mem_type(unsigned long phys_addr)
0851 {
0852 const efi_memory_desc_t *md;
0853
0854 if (!efi_enabled(EFI_MEMMAP))
0855 return -ENOTSUPP;
0856
0857 for_each_efi_memory_desc(md) {
0858 if ((md->phys_addr <= phys_addr) &&
0859 (phys_addr < (md->phys_addr +
0860 (md->num_pages << EFI_PAGE_SHIFT))))
0861 return md->type;
0862 }
0863 return -EINVAL;
0864 }
0865 #endif
0866
0867 int efi_status_to_err(efi_status_t status)
0868 {
0869 int err;
0870
0871 switch (status) {
0872 case EFI_SUCCESS:
0873 err = 0;
0874 break;
0875 case EFI_INVALID_PARAMETER:
0876 err = -EINVAL;
0877 break;
0878 case EFI_OUT_OF_RESOURCES:
0879 err = -ENOSPC;
0880 break;
0881 case EFI_DEVICE_ERROR:
0882 err = -EIO;
0883 break;
0884 case EFI_WRITE_PROTECTED:
0885 err = -EROFS;
0886 break;
0887 case EFI_SECURITY_VIOLATION:
0888 err = -EACCES;
0889 break;
0890 case EFI_NOT_FOUND:
0891 err = -ENOENT;
0892 break;
0893 case EFI_ABORTED:
0894 err = -EINTR;
0895 break;
0896 default:
0897 err = -EINVAL;
0898 }
0899
0900 return err;
0901 }
0902 EXPORT_SYMBOL_GPL(efi_status_to_err);
0903
0904 static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock);
0905 static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
0906
0907 static int __init efi_memreserve_map_root(void)
0908 {
0909 if (mem_reserve == EFI_INVALID_TABLE_ADDR)
0910 return -ENODEV;
0911
0912 efi_memreserve_root = memremap(mem_reserve,
0913 sizeof(*efi_memreserve_root),
0914 MEMREMAP_WB);
0915 if (WARN_ON_ONCE(!efi_memreserve_root))
0916 return -ENOMEM;
0917 return 0;
0918 }
0919
0920 static int efi_mem_reserve_iomem(phys_addr_t addr, u64 size)
0921 {
0922 struct resource *res, *parent;
0923 int ret;
0924
0925 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
0926 if (!res)
0927 return -ENOMEM;
0928
0929 res->name = "reserved";
0930 res->flags = IORESOURCE_MEM;
0931 res->start = addr;
0932 res->end = addr + size - 1;
0933
0934
0935 parent = request_resource_conflict(&iomem_resource, res);
0936 ret = parent ? request_resource(parent, res) : 0;
0937
0938
0939
0940
0941
0942
0943 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !ret)
0944 memblock_reserve(addr, size);
0945
0946 return ret;
0947 }
0948
0949 int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size)
0950 {
0951 struct linux_efi_memreserve *rsv;
0952 unsigned long prsv;
0953 int rc, index;
0954
0955 if (efi_memreserve_root == (void *)ULONG_MAX)
0956 return -ENODEV;
0957
0958 if (!efi_memreserve_root) {
0959 rc = efi_memreserve_map_root();
0960 if (rc)
0961 return rc;
0962 }
0963
0964
0965 for (prsv = efi_memreserve_root->next; prsv; ) {
0966 rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
0967 index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size);
0968 if (index < rsv->size) {
0969 rsv->entry[index].base = addr;
0970 rsv->entry[index].size = size;
0971
0972 memunmap(rsv);
0973 return efi_mem_reserve_iomem(addr, size);
0974 }
0975 prsv = rsv->next;
0976 memunmap(rsv);
0977 }
0978
0979
0980 rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC);
0981 if (!rsv)
0982 return -ENOMEM;
0983
0984 rc = efi_mem_reserve_iomem(__pa(rsv), SZ_4K);
0985 if (rc) {
0986 free_page((unsigned long)rsv);
0987 return rc;
0988 }
0989
0990
0991
0992
0993
0994
0995
0996 rsv->size = EFI_MEMRESERVE_COUNT(SZ_4K);
0997 atomic_set(&rsv->count, 1);
0998 rsv->entry[0].base = addr;
0999 rsv->entry[0].size = size;
1000
1001 spin_lock(&efi_mem_reserve_persistent_lock);
1002 rsv->next = efi_memreserve_root->next;
1003 efi_memreserve_root->next = __pa(rsv);
1004 spin_unlock(&efi_mem_reserve_persistent_lock);
1005
1006 return efi_mem_reserve_iomem(addr, size);
1007 }
1008
1009 static int __init efi_memreserve_root_init(void)
1010 {
1011 if (efi_memreserve_root)
1012 return 0;
1013 if (efi_memreserve_map_root())
1014 efi_memreserve_root = (void *)ULONG_MAX;
1015 return 0;
1016 }
1017 early_initcall(efi_memreserve_root_init);
1018
1019 #ifdef CONFIG_KEXEC
1020 static int update_efi_random_seed(struct notifier_block *nb,
1021 unsigned long code, void *unused)
1022 {
1023 struct linux_efi_random_seed *seed;
1024 u32 size = 0;
1025
1026 if (!kexec_in_progress)
1027 return NOTIFY_DONE;
1028
1029 seed = memremap(efi_rng_seed, sizeof(*seed), MEMREMAP_WB);
1030 if (seed != NULL) {
1031 size = min(seed->size, EFI_RANDOM_SEED_SIZE);
1032 memunmap(seed);
1033 } else {
1034 pr_err("Could not map UEFI random seed!\n");
1035 }
1036 if (size > 0) {
1037 seed = memremap(efi_rng_seed, sizeof(*seed) + size,
1038 MEMREMAP_WB);
1039 if (seed != NULL) {
1040 seed->size = size;
1041 get_random_bytes(seed->bits, seed->size);
1042 memunmap(seed);
1043 } else {
1044 pr_err("Could not map UEFI random seed!\n");
1045 }
1046 }
1047 return NOTIFY_DONE;
1048 }
1049
1050 static struct notifier_block efi_random_seed_nb = {
1051 .notifier_call = update_efi_random_seed,
1052 };
1053
1054 static int __init register_update_efi_random_seed(void)
1055 {
1056 if (efi_rng_seed == EFI_INVALID_TABLE_ADDR)
1057 return 0;
1058 return register_reboot_notifier(&efi_random_seed_nb);
1059 }
1060 late_initcall(register_update_efi_random_seed);
1061 #endif