bbf3f54
From: Peter Jones <pjones@redhat.com>
bbf3f54
Date: Tue, 18 Nov 2014 10:18:22 -0500
bbf3f54
Subject: [PATCH] efi: Add esrt support.
bbf3f54
bbf3f54
Add sysfs files for the EFI System Resource Table (ESRT) under
bbf3f54
/sys/firmware/efi/esrt and for each EFI System Resource Entry under
bbf3f54
entries/ as a subdir.
bbf3f54
bbf3f54
The EFI System Resource Table (ESRT) provides a read-only catalog of
bbf3f54
system components for which the system accepts firmware upgrades via
bbf3f54
UEFI's "Capsule Update" feature.  This module allows userland utilities
bbf3f54
to evaluate what firmware updates can be applied to this system, and
bbf3f54
potentially arrange for those updates to occur.
bbf3f54
bbf3f54
The ESRT is described as part of the UEFI specification, in version 2.5
bbf3f54
which should be available from http://uefi.org/specifications in early
bbf3f54
2015.  If you're a member of the UEFI Forum, information about its
bbf3f54
addition to the standard is available as UEFI Mantis 1090.
bbf3f54
bbf3f54
For some hardware platforms, additional restrictions may be found at
bbf3f54
http://msdn.microsoft.com/en-us/library/windows/hardware/jj128256.aspx ,
bbf3f54
and additional documentation may be found at
bbf3f54
http://download.microsoft.com/download/5/F/5/5F5D16CD-2530-4289-8019-94C6A20BED3C/windows-uefi-firmware-update-platform.docx
bbf3f54
.
bbf3f54
bbf3f54
Signed-off-by: Peter Jones <pjones@redhat.com>
bbf3f54
---
bbf3f54
 Documentation/ABI/testing/sysfs-firmware-efi-esrt |  81 ++++
bbf3f54
 arch/x86/platform/efi/efi.c                       |   2 +
bbf3f54
 drivers/firmware/efi/Makefile                     |   2 +-
5fd2f45
 drivers/firmware/efi/efi.c                        |  82 +++-
02a7d0b
 drivers/firmware/efi/esrt.c                       | 464 ++++++++++++++++++++++
bbf3f54
 include/linux/efi.h                               |   8 +
5fd2f45
 6 files changed, 637 insertions(+), 2 deletions(-)
bbf3f54
 create mode 100644 Documentation/ABI/testing/sysfs-firmware-efi-esrt
bbf3f54
 create mode 100644 drivers/firmware/efi/esrt.c
bbf3f54
bbf3f54
diff --git a/Documentation/ABI/testing/sysfs-firmware-efi-esrt b/Documentation/ABI/testing/sysfs-firmware-efi-esrt
bbf3f54
new file mode 100644
2850957
index 000000000000..6e431d1a4e79
bbf3f54
--- /dev/null
bbf3f54
+++ b/Documentation/ABI/testing/sysfs-firmware-efi-esrt
bbf3f54
@@ -0,0 +1,81 @@
bbf3f54
+What:		/sys/firmware/efi/esrt/
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	Provides userland access to read the EFI System Resource Table
bbf3f54
+		(ESRT), a catalog of firmware for which can be updated with
bbf3f54
+		the UEFI UpdateCapsule mechanism described in section 7.5 of
bbf3f54
+		the UEFI Standard.
bbf3f54
+Users:		fwupdate - https://github.com/rhinstaller/fwupdate
bbf3f54
+
bbf3f54
+What:		/sys/firmware/efi/esrt/fw_resource_count
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	The number of entries in the ESRT
bbf3f54
+
bbf3f54
+What:		/sys/firmware/efi/esrt/fw_resource_count_max
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	The maximum number of entries that /could/ be registered
bbf3f54
+		in the allocation the table is currently in.  This is
bbf3f54
+		really only useful to the system firmware itself.
bbf3f54
+
bbf3f54
+What:		/sys/firmware/efi/esrt/fw_resource_version
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	The version of the ESRT structure provided by the firmware.
bbf3f54
+
bbf3f54
+What:		/sys/firmware/efi/esrt/entries/entry$N/
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	Each ESRT entry is identified by a GUID, and each gets a
bbf3f54
+		subdirectory under entries/ .
bbf3f54
+		example: /sys/firmware/efi/esrt/entries/entry0/
bbf3f54
+
bbf3f54
+What:		/sys/firmware/efi/esrt/entries/entry$N/fw_type
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	What kind of firmware entry this is:
bbf3f54
+		0 - Unknown
bbf3f54
+		1 - System Firmware
bbf3f54
+		2 - Device Firmware
bbf3f54
+		3 - UEFI Driver
bbf3f54
+
bbf3f54
+What:		/sys/firmware/efi/esrt/entries/entry$N/fw_class
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	This is the entry's guid, and will match the directory name.
bbf3f54
+
bbf3f54
+What:		/sys/firmware/efi/esrt/entries/entry$N/fw_version
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	The version of the firmware currently installed.  This is a
bbf3f54
+		32-bit unsigned integer.
bbf3f54
+
bbf3f54
+What:		/sys/firmware/efi/esrt/entries/entry$N/lowest_supported_fw_version
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	The lowest version of the firmware that can be installed.
bbf3f54
+
bbf3f54
+What:		/sys/firmware/efi/esrt/entries/entry$N/capsule_flags
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	Flags that must be passed to UpdateCapsule()
bbf3f54
+
bbf3f54
+What:		/sys/firmware/efi/esrt/entries/entry$N/last_attempt_version
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	The last firmware version for which an update was attempted.
bbf3f54
+
bbf3f54
+What:		/sys/firmware/efi/esrt/entries/entry$N/last_attempt_status
bbf3f54
+Date:		February 2015
bbf3f54
+Contact:	Peter Jones <pjones@redhat.com>
bbf3f54
+Description:	The result of the last firmware update attempt for the
bbf3f54
+		firmware resource entry.
bbf3f54
+		0 - Success
bbf3f54
+		1 - Insufficient resources
bbf3f54
+		2 - Incorrect version
bbf3f54
+		3 - Invalid format
bbf3f54
+		4 - Authentication error
bbf3f54
+		5 - AC power event
bbf3f54
+		6 - Battery power event
bbf3f54
+
bbf3f54
diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c
b18e6e7
index 02744df576d5..3b984c3aa1b0 100644
bbf3f54
--- a/arch/x86/platform/efi/efi.c
bbf3f54
+++ b/arch/x86/platform/efi/efi.c
b18e6e7
@@ -501,6 +501,8 @@ void __init efi_init(void)
bbf3f54
 
b18e6e7
 	if (efi_enabled(EFI_DBG))
b18e6e7
 		print_efi_memmap();
bbf3f54
+
bbf3f54
+	efi_esrt_init();
bbf3f54
 }
bbf3f54
 
bbf3f54
 void __init efi_late_init(void)
bbf3f54
diff --git a/drivers/firmware/efi/Makefile b/drivers/firmware/efi/Makefile
2850957
index d8be608a9f3b..26eabbc55341 100644
bbf3f54
--- a/drivers/firmware/efi/Makefile
bbf3f54
+++ b/drivers/firmware/efi/Makefile
bbf3f54
@@ -1,7 +1,7 @@
bbf3f54
 #
bbf3f54
 # Makefile for linux kernel
bbf3f54
 #
bbf3f54
-obj-$(CONFIG_EFI)			+= efi.o vars.o reboot.o
bbf3f54
+obj-$(CONFIG_EFI)			+= efi.o esrt.o vars.o reboot.o
bbf3f54
 obj-$(CONFIG_EFI_VARS)			+= efivars.o
bbf3f54
 obj-$(CONFIG_EFI_VARS_PSTORE)		+= efi-pstore.o
bbf3f54
 obj-$(CONFIG_UEFI_CPER)			+= cper.o
bbf3f54
diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c
2850957
index 3061bb8629dc..48b4c356740f 100644
bbf3f54
--- a/drivers/firmware/efi/efi.c
bbf3f54
+++ b/drivers/firmware/efi/efi.c
bbf3f54
@@ -39,6 +39,7 @@ struct efi __read_mostly efi = {
bbf3f54
 	.fw_vendor  = EFI_INVALID_TABLE_ADDR,
bbf3f54
 	.runtime    = EFI_INVALID_TABLE_ADDR,
bbf3f54
 	.config_table  = EFI_INVALID_TABLE_ADDR,
bbf3f54
+	.esrt       = EFI_INVALID_TABLE_ADDR,
bbf3f54
 };
bbf3f54
 EXPORT_SYMBOL(efi);
bbf3f54
 
bbf3f54
@@ -64,7 +65,7 @@ static int __init parse_efi_cmdline(char *str)
bbf3f54
 }
bbf3f54
 early_param("efi", parse_efi_cmdline);
bbf3f54
 
bbf3f54
-static struct kobject *efi_kobj;
bbf3f54
+struct kobject *efi_kobj;
bbf3f54
 static struct kobject *efivars_kobj;
bbf3f54
 
bbf3f54
 /*
5fd2f45
@@ -232,6 +233,84 @@ err_put:
bbf3f54
 
bbf3f54
 subsys_initcall(efisubsys_init);
bbf3f54
 
bbf3f54
+/*
bbf3f54
+ * Find the efi memory descriptor for a given physical address.  Given a
bbf3f54
+ * physicall address, determine if it exists within an EFI Memory Map entry,
bbf3f54
+ * and if so, populate the supplied memory descriptor with the appropriate
bbf3f54
+ * data.
bbf3f54
+ */
bbf3f54
+int __init efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
bbf3f54
+{
bbf3f54
+	struct efi_memory_map *map = efi.memmap;
bbf3f54
+	void *p, *e;
bbf3f54
+
bbf3f54
+	if (!efi_enabled(EFI_MEMMAP)) {
bbf3f54
+		pr_err_once("EFI_MEMMAP is not enabled.\n");
bbf3f54
+		return -EINVAL;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	if (!map) {
bbf3f54
+		pr_err_once("efi.memmap is not set.\n");
bbf3f54
+		return -EINVAL;
bbf3f54
+	}
bbf3f54
+	if (!out_md) {
bbf3f54
+		pr_err_once("out_md is null.\n");
bbf3f54
+		return -EINVAL;
bbf3f54
+        }
bbf3f54
+	if (WARN_ON_ONCE(!map->phys_map))
bbf3f54
+		return -EINVAL;
bbf3f54
+	if (WARN_ON_ONCE(map->nr_map == 0) || WARN_ON_ONCE(map->desc_size == 0))
bbf3f54
+		return -EINVAL;
bbf3f54
+
bbf3f54
+	e = map->phys_map + map->nr_map * map->desc_size;
bbf3f54
+	for (p = map->phys_map; p < e; p += map->desc_size) {
5fd2f45
+		efi_memory_desc_t *md;
5fd2f45
+		u64 size;
5fd2f45
+		u64 end;
5fd2f45
+
bbf3f54
+		/*
bbf3f54
+		 * If a driver calls this after efi_free_boot_services,
5fd2f45
+		 * ->map will be NULL, and the target may also not be mapped.
bbf3f54
+		 * So just always get our own virtual map on the CPU.
5fd2f45
+		 *
bbf3f54
+		 */
5fd2f45
+		md = early_memremap((phys_addr_t)p, sizeof (*md));
5fd2f45
+		if (!md) {
5fd2f45
+			pr_err_once("early_memremap(%p, %zu) failed.\n",
5fd2f45
+				    p, sizeof (*md));
5fd2f45
+			return -ENOMEM;
5fd2f45
+		}
bbf3f54
+
bbf3f54
+		if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
bbf3f54
+		    md->type != EFI_BOOT_SERVICES_DATA &&
5fd2f45
+		    md->type != EFI_RUNTIME_SERVICES_DATA) {
5fd2f45
+			early_memunmap(md, sizeof (*md));
bbf3f54
+			continue;
5fd2f45
+		}
5fd2f45
+
5fd2f45
+		size = md->num_pages << EFI_PAGE_SHIFT;
5fd2f45
+		end = md->phys_addr + size;
bbf3f54
+		if (phys_addr >= md->phys_addr && phys_addr < end) {
bbf3f54
+			memcpy(out_md, md, sizeof(*out_md));
5fd2f45
+			early_memunmap(md, sizeof (*md));
bbf3f54
+			return 0;
bbf3f54
+		}
5fd2f45
+
5fd2f45
+		early_memunmap(md, sizeof (*md));
bbf3f54
+	}
bbf3f54
+	pr_err_once("requested map not found.\n");
bbf3f54
+	return -ENOENT;
bbf3f54
+}
bbf3f54
+
bbf3f54
+/*
bbf3f54
+ * Calculate the highest address of an efi memory descriptor.
bbf3f54
+ */
bbf3f54
+u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
bbf3f54
+{
bbf3f54
+	u64 size = md->num_pages << EFI_PAGE_SHIFT;
bbf3f54
+	u64 end = md->phys_addr + size;
bbf3f54
+	return end;
bbf3f54
+}
bbf3f54
 
bbf3f54
 /*
bbf3f54
  * We can't ioremap data in EFI boot services RAM, because we've already mapped
5fd2f45
@@ -274,6 +353,7 @@ static __initdata efi_config_table_type_t common_tables[] = {
bbf3f54
 	{SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
bbf3f54
 	{SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3},
bbf3f54
 	{UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
bbf3f54
+	{EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt},
bbf3f54
 	{NULL_GUID, NULL, NULL},
bbf3f54
 };
bbf3f54
 
bbf3f54
diff --git a/drivers/firmware/efi/esrt.c b/drivers/firmware/efi/esrt.c
bbf3f54
new file mode 100644
2850957
index 000000000000..20c0cbdb1c60
bbf3f54
--- /dev/null
bbf3f54
+++ b/drivers/firmware/efi/esrt.c
02a7d0b
@@ -0,0 +1,464 @@
bbf3f54
+/*
bbf3f54
+ * esrt.c
bbf3f54
+ *
bbf3f54
+ * This module exports EFI System Resource Table (ESRT) entries into userspace
bbf3f54
+ * through the sysfs file system. The ESRT provides a read-only catalog of
bbf3f54
+ * system components for which the system accepts firmware upgrades via UEFI's
bbf3f54
+ * "Capsule Update" feature. This module allows userland utilities to evaluate
bbf3f54
+ * what firmware updates can be applied to this system, and potentially arrange
bbf3f54
+ * for those updates to occur.
bbf3f54
+ *
bbf3f54
+ * Data is currently found below /sys/firmware/efi/esrt/...
bbf3f54
+ */
bbf3f54
+#define pr_fmt(fmt) "esrt: " fmt
bbf3f54
+
bbf3f54
+#include <linux/capability.h>
bbf3f54
+#include <linux/device.h>
bbf3f54
+#include <linux/efi.h>
bbf3f54
+#include <linux/init.h>
bbf3f54
+#include <linux/kernel.h>
bbf3f54
+#include <linux/kobject.h>
bbf3f54
+#include <linux/list.h>
bbf3f54
+#include <linux/memblock.h>
bbf3f54
+#include <linux/module.h>
bbf3f54
+#include <linux/slab.h>
bbf3f54
+#include <linux/types.h>
bbf3f54
+
02a7d0b
+#include <asm/io.h>
02a7d0b
+#include <asm/early_ioremap.h>
02a7d0b
+
bbf3f54
+struct efi_system_resource_entry_v1 {
bbf3f54
+	efi_guid_t	fw_class;
bbf3f54
+	u32		fw_type;
bbf3f54
+	u32		fw_version;
bbf3f54
+	u32		lowest_supported_fw_version;
bbf3f54
+	u32		capsule_flags;
bbf3f54
+	u32		last_attempt_version;
bbf3f54
+	u32		last_attempt_status;
bbf3f54
+};
bbf3f54
+
bbf3f54
+/*
bbf3f54
+ * _count and _version are what they seem like.  _max is actually just
bbf3f54
+ * accounting info for the firmware when creating the table; it should never
bbf3f54
+ * have been exposed to us.  To wit, the spec says:
bbf3f54
+ * The maximum number of resource array entries that can be within the
bbf3f54
+ * table without reallocating the table, must not be zero.
bbf3f54
+ * Since there's no guidance about what that means in terms of memory layout,
bbf3f54
+ * it means nothing to us.
bbf3f54
+ */
bbf3f54
+struct efi_system_resource_table {
bbf3f54
+	u32	fw_resource_count;
bbf3f54
+	u32	fw_resource_count_max;
bbf3f54
+	u64	fw_resource_version;
bbf3f54
+	u8	entries[];
bbf3f54
+};
bbf3f54
+
bbf3f54
+static phys_addr_t esrt_data;
bbf3f54
+static size_t esrt_data_size;
bbf3f54
+
bbf3f54
+static struct efi_system_resource_table *esrt;
bbf3f54
+
bbf3f54
+struct esre_entry {
bbf3f54
+	union {
bbf3f54
+		struct efi_system_resource_entry_v1 *esre1;
bbf3f54
+	} esre;
bbf3f54
+
bbf3f54
+	struct kobject kobj;
bbf3f54
+	struct list_head list;
bbf3f54
+};
bbf3f54
+
bbf3f54
+/* global list of esre_entry. */
bbf3f54
+static LIST_HEAD(entry_list);
bbf3f54
+
bbf3f54
+/* entry attribute */
bbf3f54
+struct esre_attribute {
bbf3f54
+	struct attribute attr;
bbf3f54
+	ssize_t (*show)(struct esre_entry *entry, char *buf);
bbf3f54
+	ssize_t (*store)(struct esre_entry *entry,
bbf3f54
+			 const char *buf, size_t count);
bbf3f54
+};
bbf3f54
+
bbf3f54
+static struct esre_entry *to_entry(struct kobject *kobj)
bbf3f54
+{
bbf3f54
+	return container_of(kobj, struct esre_entry, kobj);
bbf3f54
+}
bbf3f54
+
bbf3f54
+static struct esre_attribute *to_attr(struct attribute *attr)
bbf3f54
+{
bbf3f54
+	return container_of(attr, struct esre_attribute, attr);
bbf3f54
+}
bbf3f54
+
bbf3f54
+static ssize_t esre_attr_show(struct kobject *kobj,
bbf3f54
+			      struct attribute *_attr, char *buf)
bbf3f54
+{
bbf3f54
+	struct esre_entry *entry = to_entry(kobj);
bbf3f54
+	struct esre_attribute *attr = to_attr(_attr);
bbf3f54
+
bbf3f54
+	/* Don't tell normal users what firmware versions we've got... */
bbf3f54
+	if (!capable(CAP_SYS_ADMIN))
bbf3f54
+		return -EACCES;
bbf3f54
+
bbf3f54
+	return attr->show(entry, buf);
bbf3f54
+}
bbf3f54
+
bbf3f54
+static const struct sysfs_ops esre_attr_ops = {
bbf3f54
+	.show = esre_attr_show,
bbf3f54
+};
bbf3f54
+
bbf3f54
+/* Generic ESRT Entry ("ESRE") support. */
bbf3f54
+static ssize_t esre_fw_class_show(struct esre_entry *entry, char *buf)
bbf3f54
+{
bbf3f54
+	char *str = buf;
bbf3f54
+
bbf3f54
+	efi_guid_to_str(&entry->esre.esre1->fw_class, str);
bbf3f54
+	str += strlen(str);
bbf3f54
+	str += sprintf(str, "\n");
bbf3f54
+
bbf3f54
+	return str - buf;
bbf3f54
+}
bbf3f54
+
bbf3f54
+static struct esre_attribute esre_fw_class = __ATTR(fw_class, 0400,
bbf3f54
+	esre_fw_class_show, NULL);
bbf3f54
+
bbf3f54
+#define esre_attr_decl(name, size, fmt) \
bbf3f54
+static ssize_t esre_##name##_show(struct esre_entry *entry, char *buf) \
bbf3f54
+{ \
bbf3f54
+	return sprintf(buf, fmt "\n", \
bbf3f54
+		       le##size##_to_cpu(entry->esre.esre1->name)); \
bbf3f54
+} \
bbf3f54
+\
bbf3f54
+static struct esre_attribute esre_##name = __ATTR(name, 0400, \
bbf3f54
+	esre_##name##_show, NULL)
bbf3f54
+
bbf3f54
+esre_attr_decl(fw_type, 32, "%u");
bbf3f54
+esre_attr_decl(fw_version, 32, "%u");
bbf3f54
+esre_attr_decl(lowest_supported_fw_version, 32, "%u");
bbf3f54
+esre_attr_decl(capsule_flags, 32, "0x%x");
bbf3f54
+esre_attr_decl(last_attempt_version, 32, "%u");
bbf3f54
+esre_attr_decl(last_attempt_status, 32, "%u");
bbf3f54
+
bbf3f54
+static struct attribute *esre1_attrs[] = {
bbf3f54
+	&esre_fw_class.attr,
bbf3f54
+	&esre_fw_type.attr,
bbf3f54
+	&esre_fw_version.attr,
bbf3f54
+	&esre_lowest_supported_fw_version.attr,
bbf3f54
+	&esre_capsule_flags.attr,
bbf3f54
+	&esre_last_attempt_version.attr,
bbf3f54
+	&esre_last_attempt_status.attr,
bbf3f54
+	NULL
bbf3f54
+};
bbf3f54
+static void esre_release(struct kobject *kobj)
bbf3f54
+{
bbf3f54
+	struct esre_entry *entry = to_entry(kobj);
bbf3f54
+
bbf3f54
+	list_del(&entry->list);
bbf3f54
+	kfree(entry);
bbf3f54
+}
bbf3f54
+
bbf3f54
+static struct kobj_type esre1_ktype = {
bbf3f54
+	.release = esre_release,
bbf3f54
+	.sysfs_ops = &esre_attr_ops,
bbf3f54
+	.default_attrs = esre1_attrs,
bbf3f54
+};
bbf3f54
+
bbf3f54
+
bbf3f54
+static struct kobject *esrt_kobj;
bbf3f54
+static struct kset *esrt_kset;
bbf3f54
+
bbf3f54
+static int esre_create_sysfs_entry(void *esre, int entry_num)
bbf3f54
+{
bbf3f54
+	int rc = 0;
bbf3f54
+	struct esre_entry *entry;
bbf3f54
+	char name[20];
bbf3f54
+
bbf3f54
+	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
bbf3f54
+	if (!entry)
bbf3f54
+		return -ENOMEM;
bbf3f54
+
bbf3f54
+	sprintf(name, "entry%d", entry_num);
bbf3f54
+
bbf3f54
+	entry->kobj.kset = esrt_kset;
bbf3f54
+
bbf3f54
+	if (esrt->fw_resource_version == 1) {
bbf3f54
+		entry->esre.esre1 = esre;
bbf3f54
+		rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
bbf3f54
+					  "%s", name);
bbf3f54
+	}
bbf3f54
+	if (rc) {
bbf3f54
+		kfree(entry);
bbf3f54
+		return rc;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	list_add_tail(&entry->list, &entry_list);
bbf3f54
+	return 0;
bbf3f54
+}
bbf3f54
+
bbf3f54
+/* support for displaying ESRT fields at the top level */
bbf3f54
+#define esrt_attr_decl(name, size, fmt) \
bbf3f54
+static ssize_t esrt_##name##_show(struct kobject *kobj, \
bbf3f54
+				  struct kobj_attribute *attr, char *buf)\
bbf3f54
+{ \
bbf3f54
+	return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \
bbf3f54
+} \
bbf3f54
+\
bbf3f54
+static struct kobj_attribute esrt_##name = __ATTR(name, 0400, \
bbf3f54
+	esrt_##name##_show, NULL)
bbf3f54
+
bbf3f54
+esrt_attr_decl(fw_resource_count, 32, "%u");
bbf3f54
+esrt_attr_decl(fw_resource_count_max, 32, "%u");
bbf3f54
+esrt_attr_decl(fw_resource_version, 64, "%llu");
bbf3f54
+
bbf3f54
+static struct attribute *esrt_attrs[] = {
bbf3f54
+	&esrt_fw_resource_count.attr,
bbf3f54
+	&esrt_fw_resource_count_max.attr,
bbf3f54
+	&esrt_fw_resource_version.attr,
bbf3f54
+	NULL,
bbf3f54
+};
bbf3f54
+
bbf3f54
+static inline int esrt_table_exists(void)
bbf3f54
+{
bbf3f54
+	if (!efi_enabled(EFI_CONFIG_TABLES))
bbf3f54
+		return 0;
bbf3f54
+	if (efi.esrt == EFI_INVALID_TABLE_ADDR)
bbf3f54
+		return 0;
bbf3f54
+	return 1;
bbf3f54
+}
bbf3f54
+
bbf3f54
+static umode_t esrt_attr_is_visible(struct kobject *kobj,
bbf3f54
+				    struct attribute *attr, int n)
bbf3f54
+{
bbf3f54
+	if (!esrt_table_exists())
bbf3f54
+		return 0;
bbf3f54
+	return attr->mode;
bbf3f54
+}
bbf3f54
+
bbf3f54
+static struct attribute_group esrt_attr_group = {
bbf3f54
+	.attrs = esrt_attrs,
bbf3f54
+	.is_visible = esrt_attr_is_visible,
bbf3f54
+};
bbf3f54
+
bbf3f54
+/*
bbf3f54
+ * remap the table, copy it to kmalloced pages, and unmap it.
bbf3f54
+ */
bbf3f54
+void __init efi_esrt_init(void)
bbf3f54
+{
bbf3f54
+	void *va;
bbf3f54
+	struct efi_system_resource_table tmpesrt;
bbf3f54
+	struct efi_system_resource_entry_v1 *v1_entries;
bbf3f54
+	size_t size, max, entry_size, entries_size;
bbf3f54
+	efi_memory_desc_t md;
bbf3f54
+	int rc;
bbf3f54
+
bbf3f54
+	pr_debug("esrt-init: loading.\n");
bbf3f54
+	if (!esrt_table_exists())
bbf3f54
+		return;
bbf3f54
+
bbf3f54
+	rc = efi_mem_desc_lookup(efi.esrt, &md);
bbf3f54
+	if (rc < 0) {
bbf3f54
+		pr_err("ESRT header is not in the memory map.\n");
bbf3f54
+		return;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	max = efi_mem_desc_end(&md);
bbf3f54
+	if (max < efi.esrt) {
bbf3f54
+		pr_err("EFI memory descriptor is invalid. (esrt: %p max: %p)\n",
bbf3f54
+		       (void *)efi.esrt, (void *)max);
bbf3f54
+		return;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	size = sizeof(*esrt);
bbf3f54
+	max -= efi.esrt;
bbf3f54
+
bbf3f54
+	if (max < size) {
bbf3f54
+		pr_err("ESRT header doen't fit on single memory map entry. (size: %zu max: %zu)\n",
bbf3f54
+		       size, max);
bbf3f54
+		return;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	va = early_memremap(efi.esrt, size);
bbf3f54
+	if (!va) {
bbf3f54
+		pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
bbf3f54
+		       size);
bbf3f54
+		return;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	memcpy(&tmpesrt, va, sizeof(tmpesrt));
bbf3f54
+
bbf3f54
+	if (tmpesrt.fw_resource_version == 1) {
bbf3f54
+		entry_size = sizeof (*v1_entries);
bbf3f54
+	} else {
bbf3f54
+		pr_err("Unsupported ESRT version %lld.\n",
bbf3f54
+		       tmpesrt.fw_resource_version);
bbf3f54
+		return;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	if (tmpesrt.fw_resource_count > 0 && max - size < entry_size) {
bbf3f54
+		pr_err("ESRT memory map entry can only hold the header. (max: %zu size: %zu)\n",
bbf3f54
+		       max - size, entry_size);
bbf3f54
+		goto err_memunmap;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	/*
bbf3f54
+	 * The format doesn't really give us any boundary to test here,
bbf3f54
+	 * so I'm making up 128 as the max number of individually updatable
bbf3f54
+	 * components we support.
bbf3f54
+	 * 128 should be pretty excessive, but there's still some chance
bbf3f54
+	 * somebody will do that someday and we'll need to raise this.
bbf3f54
+	 */
bbf3f54
+	if (tmpesrt.fw_resource_count > 128) {
bbf3f54
+		pr_err("ESRT says fw_resource_count has very large value %d.\n",
bbf3f54
+		       tmpesrt.fw_resource_count);
bbf3f54
+		goto err_memunmap;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	/*
bbf3f54
+	 * We know it can't be larger than N * sizeof() here, and N is limited
bbf3f54
+	 * by the previous test to a small number, so there's no overflow.
bbf3f54
+	 */
bbf3f54
+	entries_size = tmpesrt.fw_resource_count * entry_size;
bbf3f54
+	if (max < size + entries_size) {
bbf3f54
+		pr_err("ESRT does not fit on single memory map entry (size: %zu max: %zu)\n",
bbf3f54
+		       size, max);
bbf3f54
+		goto err_memunmap;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	/* remap it with our (plausible) new pages */
bbf3f54
+	early_memunmap(va, size);
bbf3f54
+	size += entries_size;
bbf3f54
+	va = early_memremap(efi.esrt, size);
bbf3f54
+	if (!va) {
bbf3f54
+		pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
bbf3f54
+		       size);
bbf3f54
+		return;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	esrt_data = (phys_addr_t)efi.esrt;
bbf3f54
+	esrt_data_size = size;
bbf3f54
+
bbf3f54
+	pr_info("Reserving ESRT space from %p to %p.\n", (void *)esrt_data,
bbf3f54
+		(char *)esrt_data + size);
bbf3f54
+	memblock_reserve(esrt_data, esrt_data_size);
bbf3f54
+
bbf3f54
+	pr_debug("esrt-init: loaded.\n");
bbf3f54
+err_memunmap:
bbf3f54
+	early_memunmap(va, size);
bbf3f54
+}
bbf3f54
+
bbf3f54
+static int __init register_entries(void)
bbf3f54
+{
bbf3f54
+	struct efi_system_resource_entry_v1 *v1_entries = (void *)esrt->entries;
bbf3f54
+	int i, rc;
bbf3f54
+
bbf3f54
+	if (!esrt_table_exists())
bbf3f54
+		return 0;
bbf3f54
+
bbf3f54
+	for (i = 0; i < le32_to_cpu(esrt->fw_resource_count); i++) {
bbf3f54
+		void *entry;
bbf3f54
+		if (esrt->fw_resource_version == 1) {
bbf3f54
+			entry = &v1_entries[i];
bbf3f54
+		}
bbf3f54
+		rc = esre_create_sysfs_entry(entry, i);
bbf3f54
+		if (rc < 0) {
bbf3f54
+			pr_err("ESRT entry creation failed with error %d.\n",
bbf3f54
+			       rc);
bbf3f54
+			return rc;
bbf3f54
+		}
bbf3f54
+	}
bbf3f54
+	return 0;
bbf3f54
+}
bbf3f54
+
bbf3f54
+static void cleanup_entry_list(void)
bbf3f54
+{
bbf3f54
+	struct esre_entry *entry, *next;
bbf3f54
+
bbf3f54
+	list_for_each_entry_safe(entry, next, &entry_list, list) {
bbf3f54
+		kobject_put(&entry->kobj);
bbf3f54
+	}
bbf3f54
+}
bbf3f54
+
bbf3f54
+static int __init esrt_sysfs_init(void)
bbf3f54
+{
bbf3f54
+	int error;
bbf3f54
+	struct efi_system_resource_table __iomem *ioesrt;
bbf3f54
+
bbf3f54
+	pr_debug("esrt-sysfs: loading.\n");
bbf3f54
+	if (!esrt_data || !esrt_data_size)
bbf3f54
+		return -ENOSYS;
bbf3f54
+
bbf3f54
+	ioesrt = ioremap(esrt_data, esrt_data_size);
bbf3f54
+	if (!ioesrt) {
bbf3f54
+		pr_err("ioremap(%p, %zu) failed.\n", (void *)esrt_data,
bbf3f54
+		       esrt_data_size);
bbf3f54
+		return -ENOMEM;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	esrt = kmalloc(esrt_data_size, GFP_KERNEL);
bbf3f54
+	if (!esrt) {
bbf3f54
+		pr_err("kmalloc failed. (wanted %zu bytes)\n", esrt_data_size);
bbf3f54
+		iounmap(ioesrt);
bbf3f54
+		return -ENOMEM;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	memcpy_fromio(esrt, ioesrt, esrt_data_size);
bbf3f54
+
bbf3f54
+	esrt_kobj = kobject_create_and_add("esrt", efi_kobj);
bbf3f54
+	if (!esrt_kobj) {
bbf3f54
+		pr_err("Firmware table registration failed.\n");
bbf3f54
+		error = -ENOMEM;
bbf3f54
+		goto err;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	error = sysfs_create_group(esrt_kobj, &esrt_attr_group);
bbf3f54
+	if (error) {
bbf3f54
+		pr_err("Sysfs attribute export failed with error %d.\n",
bbf3f54
+		       error);
bbf3f54
+		goto err_remove_esrt;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	esrt_kset = kset_create_and_add("entries", NULL, esrt_kobj);
bbf3f54
+	if (!esrt_kset) {
bbf3f54
+		pr_err("kset creation failed.\n");
bbf3f54
+		error = -ENOMEM;
bbf3f54
+		goto err_remove_group;
bbf3f54
+	}
bbf3f54
+
bbf3f54
+	error = register_entries();
bbf3f54
+	if (error)
bbf3f54
+		goto err_cleanup_list;
bbf3f54
+
bbf3f54
+	memblock_remove(esrt_data, esrt_data_size);
bbf3f54
+
bbf3f54
+	pr_debug("esrt-sysfs: loaded.\n");
bbf3f54
+
bbf3f54
+	return 0;
bbf3f54
+err_cleanup_list:
bbf3f54
+	cleanup_entry_list();
bbf3f54
+	kset_unregister(esrt_kset);
bbf3f54
+err_remove_group:
bbf3f54
+	sysfs_remove_group(esrt_kobj, &esrt_attr_group);
bbf3f54
+err_remove_esrt:
bbf3f54
+	kobject_put(esrt_kobj);
bbf3f54
+err:
bbf3f54
+	kfree(esrt);
bbf3f54
+	esrt = NULL;
bbf3f54
+	return error;
bbf3f54
+}
bbf3f54
+
bbf3f54
+static void __exit esrt_sysfs_exit(void)
bbf3f54
+{
bbf3f54
+	pr_debug("esrt-sysfs: unloading.\n");
bbf3f54
+	cleanup_entry_list();
bbf3f54
+	kset_unregister(esrt_kset);
bbf3f54
+	sysfs_remove_group(esrt_kobj, &esrt_attr_group);
bbf3f54
+	kfree(esrt);
bbf3f54
+	esrt = NULL;
bbf3f54
+	kobject_del(esrt_kobj);
bbf3f54
+	kobject_put(esrt_kobj);
bbf3f54
+}
bbf3f54
+
bbf3f54
+module_init(esrt_sysfs_init);
bbf3f54
+module_exit(esrt_sysfs_exit);
bbf3f54
+
bbf3f54
+MODULE_AUTHOR("Peter Jones <pjones@redhat.com>");
bbf3f54
+MODULE_DESCRIPTION("EFI System Resource Table support");
bbf3f54
+MODULE_LICENSE("GPL");
bbf3f54
diff --git a/include/linux/efi.h b/include/linux/efi.h
b18e6e7
index 9cc069579351..fa3546989ab0 100644
bbf3f54
--- a/include/linux/efi.h
bbf3f54
+++ b/include/linux/efi.h
5fd2f45
@@ -583,6 +583,9 @@ void efi_native_runtime_setup(void);
5fd2f45
 #define EFI_FILE_INFO_ID \
5fd2f45
     EFI_GUID(  0x9576e92, 0x6d3f, 0x11d2, 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b )
bbf3f54
 
bbf3f54
+#define EFI_SYSTEM_RESOURCE_TABLE_GUID \
bbf3f54
+    EFI_GUID(  0xb122a263, 0x3661, 0x4f68, 0x99, 0x29, 0x78, 0xf8, 0xb0, 0xd6, 0x21, 0x80 )
bbf3f54
+
5fd2f45
 #define EFI_FILE_SYSTEM_GUID \
5fd2f45
     EFI_GUID(  0x964e5b22, 0x6459, 0x11d2, 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b )
bbf3f54
 
2850957
@@ -849,6 +852,7 @@ extern struct efi {
bbf3f54
 	unsigned long fw_vendor;	/* fw_vendor */
bbf3f54
 	unsigned long runtime;		/* runtime table */
bbf3f54
 	unsigned long config_table;	/* config tables */
bbf3f54
+	unsigned long esrt;		/* ESRT table */
bbf3f54
 	efi_get_time_t *get_time;
bbf3f54
 	efi_set_time_t *set_time;
bbf3f54
 	efi_get_wakeup_time_t *get_wakeup_time;
2850957
@@ -901,6 +905,7 @@ static inline efi_status_t efi_query_variable_store(u32 attributes, unsigned lon
bbf3f54
 #endif
bbf3f54
 extern void __iomem *efi_lookup_mapped_addr(u64 phys_addr);
bbf3f54
 extern int efi_config_init(efi_config_table_type_t *arch_tables);
bbf3f54
+extern void __init efi_esrt_init(void);
bbf3f54
 extern int efi_config_parse_tables(void *config_tables, int count, int sz,
bbf3f54
 				   efi_config_table_type_t *arch_tables);
bbf3f54
 extern u64 efi_get_iobase (void);
2850957
@@ -908,12 +913,15 @@ extern u32 efi_mem_type (unsigned long phys_addr);
bbf3f54
 extern u64 efi_mem_attributes (unsigned long phys_addr);
bbf3f54
 extern u64 efi_mem_attribute (unsigned long phys_addr, unsigned long size);
bbf3f54
 extern int __init efi_uart_console_only (void);
bbf3f54
+extern u64 efi_mem_desc_end(efi_memory_desc_t *md);
bbf3f54
+extern int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md);
bbf3f54
 extern void efi_initialize_iomem_resources(struct resource *code_resource,
bbf3f54
 		struct resource *data_resource, struct resource *bss_resource);
bbf3f54
 extern void efi_get_time(struct timespec *now);
bbf3f54
 extern void efi_reserve_boot_services(void);
bbf3f54
 extern int efi_get_fdt_params(struct efi_fdt_params *params, int verbose);
bbf3f54
 extern struct efi_memory_map memmap;
bbf3f54
+extern struct kobject *efi_kobj;
bbf3f54
 
bbf3f54
 extern int efi_reboot_quirk_mode;
bbf3f54
 extern bool efi_poweroff_required(void);