2 * efi.c - EFI subsystem
4 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
5 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
6 * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
8 * This code registers /sys/firmware/efi{,/efivars} when EFI is supported,
9 * allowing the efivarfs to be mounted or the efivars module to be loaded.
10 * The existance of /sys/firmware/efi may also be used by userspace to
11 * determine that the system supports EFI.
13 * This file is released under the GPLv2.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/kobject.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/efi.h>
24 #include <linux/of_fdt.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/acpi.h>
29 #include <linux/ucs2_string.h>
31 #include <asm/early_ioremap.h>
33 struct efi __read_mostly efi = {
34 .mps = EFI_INVALID_TABLE_ADDR,
35 .acpi = EFI_INVALID_TABLE_ADDR,
36 .acpi20 = EFI_INVALID_TABLE_ADDR,
37 .smbios = EFI_INVALID_TABLE_ADDR,
38 .smbios3 = EFI_INVALID_TABLE_ADDR,
39 .sal_systab = EFI_INVALID_TABLE_ADDR,
40 .boot_info = EFI_INVALID_TABLE_ADDR,
41 .hcdp = EFI_INVALID_TABLE_ADDR,
42 .uga = EFI_INVALID_TABLE_ADDR,
43 .uv_systab = EFI_INVALID_TABLE_ADDR,
44 .fw_vendor = EFI_INVALID_TABLE_ADDR,
45 .runtime = EFI_INVALID_TABLE_ADDR,
46 .config_table = EFI_INVALID_TABLE_ADDR,
47 .esrt = EFI_INVALID_TABLE_ADDR,
48 .properties_table = EFI_INVALID_TABLE_ADDR,
49 .mem_attr_table = EFI_INVALID_TABLE_ADDR,
53 static bool disable_runtime;
54 static int __init setup_noefi(char *arg)
56 disable_runtime = true;
59 early_param("noefi", setup_noefi);
61 bool efi_runtime_disabled(void)
63 return disable_runtime;
66 static int __init parse_efi_cmdline(char *str)
69 pr_warn("need at least one option\n");
73 if (parse_option_str(str, "debug"))
74 set_bit(EFI_DBG, &efi.flags);
76 if (parse_option_str(str, "noruntime"))
77 disable_runtime = true;
81 early_param("efi", parse_efi_cmdline);
83 struct kobject *efi_kobj;
86 * Let's not leave out systab information that snuck into
89 static ssize_t systab_show(struct kobject *kobj,
90 struct kobj_attribute *attr, char *buf)
97 if (efi.mps != EFI_INVALID_TABLE_ADDR)
98 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
99 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
100 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
101 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
102 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
104 * If both SMBIOS and SMBIOS3 entry points are implemented, the
105 * SMBIOS3 entry point shall be preferred, so we list it first to
106 * let applications stop parsing after the first match.
108 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
109 str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
110 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
111 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
112 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
113 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
114 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
115 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
116 if (efi.uga != EFI_INVALID_TABLE_ADDR)
117 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
122 static struct kobj_attribute efi_attr_systab =
123 __ATTR(systab, 0400, systab_show, NULL);
125 #define EFI_FIELD(var) efi.var
127 #define EFI_ATTR_SHOW(name) \
128 static ssize_t name##_show(struct kobject *kobj, \
129 struct kobj_attribute *attr, char *buf) \
131 return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
134 EFI_ATTR_SHOW(fw_vendor);
135 EFI_ATTR_SHOW(runtime);
136 EFI_ATTR_SHOW(config_table);
138 static ssize_t fw_platform_size_show(struct kobject *kobj,
139 struct kobj_attribute *attr, char *buf)
141 return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32);
144 static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
145 static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
146 static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
147 static struct kobj_attribute efi_attr_fw_platform_size =
148 __ATTR_RO(fw_platform_size);
150 static struct attribute *efi_subsys_attrs[] = {
151 &efi_attr_systab.attr,
152 &efi_attr_fw_vendor.attr,
153 &efi_attr_runtime.attr,
154 &efi_attr_config_table.attr,
155 &efi_attr_fw_platform_size.attr,
159 static umode_t efi_attr_is_visible(struct kobject *kobj,
160 struct attribute *attr, int n)
162 if (attr == &efi_attr_fw_vendor.attr) {
163 if (efi_enabled(EFI_PARAVIRT) ||
164 efi.fw_vendor == EFI_INVALID_TABLE_ADDR)
166 } else if (attr == &efi_attr_runtime.attr) {
167 if (efi.runtime == EFI_INVALID_TABLE_ADDR)
169 } else if (attr == &efi_attr_config_table.attr) {
170 if (efi.config_table == EFI_INVALID_TABLE_ADDR)
177 static struct attribute_group efi_subsys_attr_group = {
178 .attrs = efi_subsys_attrs,
179 .is_visible = efi_attr_is_visible,
182 static struct efivars generic_efivars;
183 static struct efivar_operations generic_ops;
185 static int generic_ops_register(void)
187 generic_ops.get_variable = efi.get_variable;
188 generic_ops.set_variable = efi.set_variable;
189 generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
190 generic_ops.get_next_variable = efi.get_next_variable;
191 generic_ops.query_variable_store = efi_query_variable_store;
193 return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
196 static void generic_ops_unregister(void)
198 efivars_unregister(&generic_efivars);
201 #if IS_ENABLED(CONFIG_ACPI)
202 #define EFIVAR_SSDT_NAME_MAX 16
203 static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
204 static int __init efivar_ssdt_setup(char *str)
206 if (strlen(str) < sizeof(efivar_ssdt))
207 memcpy(efivar_ssdt, str, strlen(str));
209 pr_warn("efivar_ssdt: name too long: %s\n", str);
212 __setup("efivar_ssdt=", efivar_ssdt_setup);
214 static __init int efivar_ssdt_iter(efi_char16_t *name, efi_guid_t vendor,
215 unsigned long name_size, void *data)
217 struct efivar_entry *entry;
218 struct list_head *list = data;
219 char utf8_name[EFIVAR_SSDT_NAME_MAX];
220 int limit = min_t(unsigned long, EFIVAR_SSDT_NAME_MAX, name_size);
222 ucs2_as_utf8(utf8_name, name, limit - 1);
223 if (strncmp(utf8_name, efivar_ssdt, limit) != 0)
226 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
230 memcpy(entry->var.VariableName, name, name_size);
231 memcpy(&entry->var.VendorGuid, &vendor, sizeof(efi_guid_t));
233 efivar_entry_add(entry, list);
238 static __init int efivar_ssdt_load(void)
241 struct efivar_entry *entry, *aux;
246 ret = efivar_init(efivar_ssdt_iter, &entries, true, &entries);
248 list_for_each_entry_safe(entry, aux, &entries, list) {
249 pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt,
250 &entry->var.VendorGuid);
252 list_del(&entry->list);
254 ret = efivar_entry_size(entry, &size);
256 pr_err("failed to get var size\n");
260 data = kmalloc(size, GFP_KERNEL);
264 ret = efivar_entry_get(entry, NULL, &size, data);
266 pr_err("failed to get var data\n");
270 ret = acpi_load_table(data);
272 pr_err("failed to load table: %d\n", ret);
288 static inline int efivar_ssdt_load(void) { return 0; }
292 * We register the efi subsystem with the firmware subsystem and the
293 * efivars subsystem with the efi subsystem, if the system was booted with
296 static int __init efisubsys_init(void)
300 if (!efi_enabled(EFI_BOOT))
303 /* We register the efi directory at /sys/firmware/efi */
304 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
306 pr_err("efi: Firmware registration failed.\n");
310 error = generic_ops_register();
314 if (efi_enabled(EFI_RUNTIME_SERVICES))
317 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
319 pr_err("efi: Sysfs attribute export failed with error %d.\n",
324 error = efi_runtime_map_init(efi_kobj);
326 goto err_remove_group;
328 /* and the standard mountpoint for efivarfs */
329 error = sysfs_create_mount_point(efi_kobj, "efivars");
331 pr_err("efivars: Subsystem registration failed.\n");
332 goto err_remove_group;
338 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
340 generic_ops_unregister();
342 kobject_put(efi_kobj);
346 subsys_initcall(efisubsys_init);
349 * Find the efi memory descriptor for a given physical address. Given a
350 * physicall address, determine if it exists within an EFI Memory Map entry,
351 * and if so, populate the supplied memory descriptor with the appropriate
354 int __init efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
356 struct efi_memory_map *map = &efi.memmap;
359 if (!efi_enabled(EFI_MEMMAP)) {
360 pr_err_once("EFI_MEMMAP is not enabled.\n");
365 pr_err_once("efi.memmap is not set.\n");
369 pr_err_once("out_md is null.\n");
372 if (WARN_ON_ONCE(!map->phys_map))
374 if (WARN_ON_ONCE(map->nr_map == 0) || WARN_ON_ONCE(map->desc_size == 0))
377 e = map->phys_map + map->nr_map * map->desc_size;
378 for (p = map->phys_map; p < e; p += map->desc_size) {
379 efi_memory_desc_t *md;
384 * If a driver calls this after efi_free_boot_services,
385 * ->map will be NULL, and the target may also not be mapped.
386 * So just always get our own virtual map on the CPU.
389 md = early_memremap(p, sizeof (*md));
391 pr_err_once("early_memremap(%pa, %zu) failed.\n",
396 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
397 md->type != EFI_BOOT_SERVICES_DATA &&
398 md->type != EFI_RUNTIME_SERVICES_DATA) {
399 early_memunmap(md, sizeof (*md));
403 size = md->num_pages << EFI_PAGE_SHIFT;
404 end = md->phys_addr + size;
405 if (phys_addr >= md->phys_addr && phys_addr < end) {
406 memcpy(out_md, md, sizeof(*out_md));
407 early_memunmap(md, sizeof (*md));
411 early_memunmap(md, sizeof (*md));
413 pr_err_once("requested map not found.\n");
418 * Calculate the highest address of an efi memory descriptor.
420 u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
422 u64 size = md->num_pages << EFI_PAGE_SHIFT;
423 u64 end = md->phys_addr + size;
427 static __initdata efi_config_table_type_t common_tables[] = {
428 {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
429 {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
430 {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
431 {MPS_TABLE_GUID, "MPS", &efi.mps},
432 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
433 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
434 {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3},
435 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
436 {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt},
437 {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table},
438 {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table},
439 {NULL_GUID, NULL, NULL},
442 static __init int match_config_table(efi_guid_t *guid,
444 efi_config_table_type_t *table_types)
449 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
450 if (!efi_guidcmp(*guid, table_types[i].guid)) {
451 *(table_types[i].ptr) = table;
452 if (table_types[i].name)
453 pr_cont(" %s=0x%lx ",
454 table_types[i].name, table);
463 int __init efi_config_parse_tables(void *config_tables, int count, int sz,
464 efi_config_table_type_t *arch_tables)
469 tablep = config_tables;
471 for (i = 0; i < count; i++) {
475 if (efi_enabled(EFI_64BIT)) {
477 guid = ((efi_config_table_64_t *)tablep)->guid;
478 table64 = ((efi_config_table_64_t *)tablep)->table;
483 pr_err("Table located above 4GB, disabling EFI.\n");
488 guid = ((efi_config_table_32_t *)tablep)->guid;
489 table = ((efi_config_table_32_t *)tablep)->table;
492 if (!match_config_table(&guid, table, common_tables))
493 match_config_table(&guid, table, arch_tables);
498 set_bit(EFI_CONFIG_TABLES, &efi.flags);
500 /* Parse the EFI Properties table if it exists */
501 if (efi.properties_table != EFI_INVALID_TABLE_ADDR) {
502 efi_properties_table_t *tbl;
504 tbl = early_memremap(efi.properties_table, sizeof(*tbl));
506 pr_err("Could not map Properties table!\n");
510 if (tbl->memory_protection_attribute &
511 EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
512 set_bit(EFI_NX_PE_DATA, &efi.flags);
514 early_memunmap(tbl, sizeof(*tbl));
520 int __init efi_config_init(efi_config_table_type_t *arch_tables)
525 if (efi_enabled(EFI_64BIT))
526 sz = sizeof(efi_config_table_64_t);
528 sz = sizeof(efi_config_table_32_t);
531 * Let's see what config tables the firmware passed to us.
533 config_tables = early_memremap(efi.systab->tables,
534 efi.systab->nr_tables * sz);
535 if (config_tables == NULL) {
536 pr_err("Could not map Configuration table!\n");
540 ret = efi_config_parse_tables(config_tables, efi.systab->nr_tables, sz,
543 early_memunmap(config_tables, efi.systab->nr_tables * sz);
547 #ifdef CONFIG_EFI_VARS_MODULE
548 static int __init efi_load_efivars(void)
550 struct platform_device *pdev;
552 if (!efi_enabled(EFI_RUNTIME_SERVICES))
555 pdev = platform_device_register_simple("efivars", 0, NULL, 0);
556 return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
558 device_initcall(efi_load_efivars);
561 #ifdef CONFIG_EFI_PARAMS_FROM_FDT
563 #define UEFI_PARAM(name, prop, field) \
567 offsetof(struct efi_fdt_params, field), \
568 FIELD_SIZEOF(struct efi_fdt_params, field) \
573 const char propname[32];
578 static __initdata struct params fdt_params[] = {
579 UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
580 UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
581 UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
582 UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
583 UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver)
586 static __initdata struct params xen_fdt_params[] = {
587 UEFI_PARAM("System Table", "xen,uefi-system-table", system_table),
588 UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap),
589 UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size),
590 UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size),
591 UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver)
594 #define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params)
596 static __initdata struct {
599 struct params *params;
601 { "hypervisor", "uefi", xen_fdt_params },
602 { "chosen", NULL, fdt_params },
611 static int __init __find_uefi_params(unsigned long node,
612 struct param_info *info,
613 struct params *params)
620 for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) {
621 prop = of_get_flat_dt_prop(node, params[i].propname, &len);
623 info->missing = params[i].name;
627 dest = info->params + params[i].offset;
630 val = of_read_number(prop, len / sizeof(u32));
632 if (params[i].size == sizeof(u32))
637 if (efi_enabled(EFI_DBG))
638 pr_info(" %s: 0x%0*llx\n", params[i].name,
639 params[i].size * 2, val);
645 static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
646 int depth, void *data)
648 struct param_info *info = data;
651 for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
652 const char *subnode = dt_params[i].subnode;
654 if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) {
655 info->missing = dt_params[i].params[0].name;
660 node = of_get_flat_dt_subnode_by_name(node, subnode);
665 return __find_uefi_params(node, info, dt_params[i].params);
671 int __init efi_get_fdt_params(struct efi_fdt_params *params)
673 struct param_info info;
676 pr_info("Getting EFI parameters from FDT:\n");
679 info.params = params;
681 ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
683 pr_info("UEFI not found.\n");
685 pr_err("Can't find '%s' in device tree!\n",
690 #endif /* CONFIG_EFI_PARAMS_FROM_FDT */
692 static __initdata char memory_type_name[][20] = {
700 "Conventional Memory",
702 "ACPI Reclaim Memory",
710 char * __init efi_md_typeattr_format(char *buf, size_t size,
711 const efi_memory_desc_t *md)
718 if (md->type >= ARRAY_SIZE(memory_type_name))
719 type_len = snprintf(pos, size, "[type=%u", md->type);
721 type_len = snprintf(pos, size, "[%-*s",
722 (int)(sizeof(memory_type_name[0]) - 1),
723 memory_type_name[md->type]);
724 if (type_len >= size)
730 attr = md->attribute;
731 if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
732 EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
733 EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
735 EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
736 snprintf(pos, size, "|attr=0x%016llx]",
737 (unsigned long long)attr);
740 "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
741 attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
742 attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
743 attr & EFI_MEMORY_NV ? "NV" : "",
744 attr & EFI_MEMORY_XP ? "XP" : "",
745 attr & EFI_MEMORY_RP ? "RP" : "",
746 attr & EFI_MEMORY_WP ? "WP" : "",
747 attr & EFI_MEMORY_RO ? "RO" : "",
748 attr & EFI_MEMORY_UCE ? "UCE" : "",
749 attr & EFI_MEMORY_WB ? "WB" : "",
750 attr & EFI_MEMORY_WT ? "WT" : "",
751 attr & EFI_MEMORY_WC ? "WC" : "",
752 attr & EFI_MEMORY_UC ? "UC" : "");
757 * efi_mem_attributes - lookup memmap attributes for physical address
758 * @phys_addr: the physical address to lookup
760 * Search in the EFI memory map for the region covering
761 * @phys_addr. Returns the EFI memory attributes if the region
762 * was found in the memory map, 0 otherwise.
764 * Despite being marked __weak, most architectures should *not*
765 * override this function. It is __weak solely for the benefit
766 * of ia64 which has a funky EFI memory map that doesn't work
767 * the same way as other architectures.
769 u64 __weak efi_mem_attributes(unsigned long phys_addr)
771 efi_memory_desc_t *md;
773 if (!efi_enabled(EFI_MEMMAP))
776 for_each_efi_memory_desc(md) {
777 if ((md->phys_addr <= phys_addr) &&
778 (phys_addr < (md->phys_addr +
779 (md->num_pages << EFI_PAGE_SHIFT))))
780 return md->attribute;
785 int efi_status_to_err(efi_status_t status)
793 case EFI_INVALID_PARAMETER:
796 case EFI_OUT_OF_RESOURCES:
799 case EFI_DEVICE_ERROR:
802 case EFI_WRITE_PROTECTED:
805 case EFI_SECURITY_VIOLATION: