linux-efi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 1/2] efi/capsule: Add 'capsule' lookup support
@ 2017-02-28 17:45 Qiuxu Zhuo
       [not found] ` <20170228174528.23542-1-qiuxu.zhuo-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
  0 siblings, 1 reply; 3+ messages in thread
From: Qiuxu Zhuo @ 2017-02-28 17:45 UTC (permalink / raw)
  To: matt-mF/unelCI9GS6iBeEJttW/XRex20P6io,
	ard.biesheuvel-QSEj5FYQhm4dnm+yROfE0A
  Cc: linux-efi-u79uwXL29TY76Z2rM5mHXA,
	tony.luck-ral2JQCrhuEAvxtiuMwx3w, Qiuxu Zhuo, Matt Fleming

Add the interface to get the array of EFI capsules when
parsing the configuration tables.

This code is based on Matt Fleming previous work from:
https://git.kernel.org/cgit/linux/kernel/git/mfleming/efi.git/commit/?h=capsule-pstore&id=99c5f047133555aa0442f64064e85b7da2d4a45f

Signed-off-by: Qiuxu Zhuo <qiuxu.zhuo-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
Signed-off-by: Matt Fleming <matt.fleming-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
---
 drivers/firmware/efi/capsule.c | 107 +++++++++++++++++++++++++++++++++++++++++
 drivers/firmware/efi/efi.c     |   4 ++
 include/linux/efi.h            |   4 ++
 3 files changed, 115 insertions(+)

diff --git a/drivers/firmware/efi/capsule.c b/drivers/firmware/efi/capsule.c
index 6eedff4..51e1a43 100644
--- a/drivers/firmware/efi/capsule.c
+++ b/drivers/firmware/efi/capsule.c
@@ -24,6 +24,11 @@ typedef struct {
 static bool capsule_pending;
 static bool stop_capsules;
 static int efi_reset_type = -1;
+/*
+ * Information about capsules we pulled from the EFI System Table.
+ */
+static efi_capsule_header_t **prev_capsules;
+static u32 efi_capsule_num;
 
 /*
  * capsule_mutex serialises access to both capsule_pending and
@@ -288,6 +293,108 @@ int efi_capsule_update(efi_capsule_header_t *capsule, struct page **pages)
 }
 EXPORT_SYMBOL_GPL(efi_capsule_update);
 
+static int extract_capsules(void)
+{
+	void *capsule;
+	size_t size;
+
+	if (efi.capsule == EFI_INVALID_TABLE_ADDR)
+		return 0;
+
+	capsule = ioremap(efi.capsule, sizeof(efi_capsule_num));
+	if (!capsule)
+		return -ENOMEM;
+
+	/*
+	 * The array of capsules is prefixed with the number of
+	 * capsule entries in the array.
+	 */
+	efi_capsule_num = *(uint32_t *)capsule;
+	iounmap(capsule);
+
+	if (!efi_capsule_num) {
+		pr_info("No capsules on extraction\n");
+		return 0;
+	}
+
+	size = efi_capsule_num * sizeof(*capsule);
+	capsule = ioremap(efi.capsule, size);
+	if (!capsule)
+		return -ENOMEM;
+
+	capsule += sizeof(uint32_t);
+	prev_capsules = (efi_capsule_header_t **)capsule;
+	if (!*prev_capsules)
+		pr_err("Capsule array has no entries\n");
+
+	return 0;
+}
+
+/**
+ * efi_capsule_lookup - search capsule array for entries.
+ * @guid: the guid to search for.
+ * @nr_caps: the number of entries found.
+ *
+ * Map each capsule header into the kernel's virtual address space and
+ * inspect the guid. Build an array of capsule headers with every
+ * capsule that is found with @guid. If a match is found the capsule
+ * remains mapped, otherwise it is unmapped.
+ *
+ * Returns an array of capsule headers, each element of which has the
+ * guid @guid. The number of elements in the array is stored in
+ * @nr_caps. Returns %NULL if no capsules were found and stores zero
+ * in @nr_caps.
+ */
+efi_capsule_header_t **efi_capsule_lookup(efi_guid_t guid, uint32_t *nr_caps)
+{
+	efi_capsule_header_t **capsules = NULL, **tmp;
+	size_t capsule_size = 0;
+	int i, rv;
+
+	rv = extract_capsules();
+	if (rv)
+		return ERR_PTR(rv);
+
+	*nr_caps = 0;
+	for (i = 0; i < efi_capsule_num; i++) {
+		efi_capsule_header_t *c;
+		size_t size;
+
+		c = ioremap((resource_size_t)prev_capsules[i], sizeof(*c));
+		if (!c) {
+			pr_err("Failed to ioremap capsule\n");
+			continue;
+		}
+
+		size = c->imagesize;
+		iounmap(c);
+
+		c = ioremap((resource_size_t)prev_capsules[i], size);
+		if (!c) {
+			pr_err("Failed to ioremap header + data\n");
+			continue;
+		}
+
+		if (!efi_guidcmp(c->guid, guid)) {
+			capsule_size += sizeof(*capsules);
+			tmp = krealloc(capsules, capsule_size, GFP_KERNEL);
+			if (!tmp) {
+				kfree(capsules);
+				return ERR_PTR(-ENOMEM);
+			}
+
+			capsules = tmp;
+			capsules[(*nr_caps)++] = c;
+			continue;
+		}
+
+		iounmap(c);
+	}
+
+	return capsules;
+}
+EXPORT_SYMBOL_GPL(efi_capsule_lookup);
+
 static int capsule_reboot_notify(struct notifier_block *nb, unsigned long event, void *cmd)
 {
 	mutex_lock(&capsule_mutex);
diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c
index 9291480..03be9b8 100644
--- a/drivers/firmware/efi/efi.c
+++ b/drivers/firmware/efi/efi.c
@@ -52,6 +52,7 @@ struct efi __read_mostly efi = {
 	.properties_table	= EFI_INVALID_TABLE_ADDR,
 	.mem_attr_table		= EFI_INVALID_TABLE_ADDR,
 	.rng_seed		= EFI_INVALID_TABLE_ADDR,
+	.capsule		= EFI_INVALID_TABLE_ADDR,
 };
 EXPORT_SYMBOL(efi);
 
@@ -120,6 +121,8 @@ static ssize_t systab_show(struct kobject *kobj,
 		str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
 	if (efi.uga != EFI_INVALID_TABLE_ADDR)
 		str += sprintf(str, "UGA=0x%lx\n", efi.uga);
+	if (efi.capsule != EFI_INVALID_TABLE_ADDR)
+		str += sprintf(str, "CAPSULE=0x%lx\n", efi.capsule);
 
 	return str - buf;
 }
@@ -445,6 +448,7 @@ static __initdata efi_config_table_type_t common_tables[] = {
 	{EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table},
 	{EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table},
 	{LINUX_EFI_RANDOM_SEED_TABLE_GUID, "RNG", &efi.rng_seed},
+	{LINUX_EFI_CRASH_GUID, "CAPSULE", &efi.capsule},
 	{NULL_GUID, NULL, NULL},
 };
 
diff --git a/include/linux/efi.h b/include/linux/efi.h
index 5b1af30..3296724e 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -893,6 +893,7 @@ extern struct efi {
 	unsigned long properties_table;	/* properties table */
 	unsigned long mem_attr_table;	/* memory attributes table */
 	unsigned long rng_seed;		/* UEFI firmware random seed */
+	unsigned long capsule;		/* EFI capsule table */
 	efi_get_time_t *get_time;
 	efi_set_time_t *set_time;
 	efi_get_wakeup_time_t *get_wakeup_time;
@@ -1401,6 +1402,9 @@ extern int efi_capsule_supported(efi_guid_t guid, u32 flags,
 extern int efi_capsule_update(efi_capsule_header_t *capsule,
 			      struct page **pages);
 
+extern efi_capsule_header_t **efi_capsule_lookup(efi_guid_t guid,
+			      uint32_t *nr_caps);
+
 #ifdef CONFIG_EFI_RUNTIME_MAP
 int efi_runtime_map_init(struct kobject *);
 int efi_get_runtime_map_size(void);
-- 
2.9.0.GIT

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v1 1/2] efi/capsule: Add 'capsule' lookup support
       [not found] ` <20170228174528.23542-1-qiuxu.zhuo-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
@ 2017-03-01 20:44   ` Ard Biesheuvel
  2017-03-01 20:46   ` Matt Fleming
  1 sibling, 0 replies; 3+ messages in thread
From: Ard Biesheuvel @ 2017-03-01 20:44 UTC (permalink / raw)
  To: Qiuxu Zhuo
  Cc: Matt Fleming, linux-efi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	Tony Luck, Matt Fleming

On 28 February 2017 at 17:45, Qiuxu Zhuo <qiuxu.zhuo-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org> wrote:
> Add the interface to get the array of EFI capsules when
> parsing the configuration tables.
>
> This code is based on Matt Fleming previous work from:
> https://git.kernel.org/cgit/linux/kernel/git/mfleming/efi.git/commit/?h=capsule-pstore&id=99c5f047133555aa0442f64064e85b7da2d4a45f
>
> Signed-off-by: Qiuxu Zhuo <qiuxu.zhuo-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
> Signed-off-by: Matt Fleming <matt.fleming-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
> ---
>  drivers/firmware/efi/capsule.c | 107 +++++++++++++++++++++++++++++++++++++++++
>  drivers/firmware/efi/efi.c     |   4 ++
>  include/linux/efi.h            |   4 ++
>  3 files changed, 115 insertions(+)
>
> diff --git a/drivers/firmware/efi/capsule.c b/drivers/firmware/efi/capsule.c
> index 6eedff4..51e1a43 100644
> --- a/drivers/firmware/efi/capsule.c
> +++ b/drivers/firmware/efi/capsule.c
> @@ -24,6 +24,11 @@ typedef struct {
>  static bool capsule_pending;
>  static bool stop_capsules;
>  static int efi_reset_type = -1;
> +/*
> + * Information about capsules we pulled from the EFI System Table.
> + */
> +static efi_capsule_header_t **prev_capsules;
> +static u32 efi_capsule_num;
>
>  /*
>   * capsule_mutex serialises access to both capsule_pending and
> @@ -288,6 +293,108 @@ int efi_capsule_update(efi_capsule_header_t *capsule, struct page **pages)
>  }
>  EXPORT_SYMBOL_GPL(efi_capsule_update);
>
> +static int extract_capsules(void)
> +{
> +       void *capsule;
> +       size_t size;
> +
> +       if (efi.capsule == EFI_INVALID_TABLE_ADDR)
> +               return 0;
> +
> +       capsule = ioremap(efi.capsule, sizeof(efi_capsule_num));

Please use memremap not ioremap when mapping firmware tables.

> +       if (!capsule)
> +               return -ENOMEM;
> +
> +       /*
> +        * The array of capsules is prefixed with the number of
> +        * capsule entries in the array.
> +        */
> +       efi_capsule_num = *(uint32_t *)capsule;
> +       iounmap(capsule);
> +
> +       if (!efi_capsule_num) {
> +               pr_info("No capsules on extraction\n");
> +               return 0;
> +       }
> +
> +       size = efi_capsule_num * sizeof(*capsule);
> +       capsule = ioremap(efi.capsule, size);
> +       if (!capsule)
> +               return -ENOMEM;
> +
> +       capsule += sizeof(uint32_t);
> +       prev_capsules = (efi_capsule_header_t **)capsule;
> +       if (!*prev_capsules)
> +               pr_err("Capsule array has no entries\n");
> +
> +       return 0;
> +}
> +
> +/**
> + * efi_capsule_lookup - search capsule array for entries.
> + * @guid: the guid to search for.
> + * @nr_caps: the number of entries found.
> + *
> + * Map each capsule header into the kernel's virtual address space and
> + * inspect the guid. Build an array of capsule headers with every
> + * capsule that is found with @guid. If a match is found the capsule
> + * remains mapped, otherwise it is unmapped.
> + *
> + * Returns an array of capsule headers, each element of which has the
> + * guid @guid. The number of elements in the array is stored in
> + * @nr_caps. Returns %NULL if no capsules were found and stores zero
> + * in @nr_caps.
> + */
> +efi_capsule_header_t **efi_capsule_lookup(efi_guid_t guid, uint32_t *nr_caps)
> +{
> +       efi_capsule_header_t **capsules = NULL, **tmp;
> +       size_t capsule_size = 0;
> +       int i, rv;
> +
> +       rv = extract_capsules();
> +       if (rv)
> +               return ERR_PTR(rv);
> +
> +       *nr_caps = 0;
> +       for (i = 0; i < efi_capsule_num; i++) {
> +               efi_capsule_header_t *c;
> +               size_t size;
> +
> +               c = ioremap((resource_size_t)prev_capsules[i], sizeof(*c));
> +               if (!c) {
> +                       pr_err("Failed to ioremap capsule\n");
> +                       continue;
> +               }
> +
> +               size = c->imagesize;
> +               iounmap(c);
> +
> +               c = ioremap((resource_size_t)prev_capsules[i], size);
> +               if (!c) {
> +                       pr_err("Failed to ioremap header + data\n");
> +                       continue;
> +               }
> +
> +               if (!efi_guidcmp(c->guid, guid)) {
> +                       capsule_size += sizeof(*capsules);
> +                       tmp = krealloc(capsules, capsule_size, GFP_KERNEL);
> +                       if (!tmp) {
> +                               kfree(capsules);
> +                               return ERR_PTR(-ENOMEM);
> +                       }
> +
> +                       capsules = tmp;
> +                       capsules[(*nr_caps)++] = c;
> +                       continue;
> +               }
> +
> +               iounmap(c);
> +       }
> +
> +       return capsules;
> +}
> +EXPORT_SYMBOL_GPL(efi_capsule_lookup);
> +
>  static int capsule_reboot_notify(struct notifier_block *nb, unsigned long event, void *cmd)
>  {
>         mutex_lock(&capsule_mutex);
> diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c
> index 9291480..03be9b8 100644
> --- a/drivers/firmware/efi/efi.c
> +++ b/drivers/firmware/efi/efi.c
> @@ -52,6 +52,7 @@ struct efi __read_mostly efi = {
>         .properties_table       = EFI_INVALID_TABLE_ADDR,
>         .mem_attr_table         = EFI_INVALID_TABLE_ADDR,
>         .rng_seed               = EFI_INVALID_TABLE_ADDR,
> +       .capsule                = EFI_INVALID_TABLE_ADDR,
>  };
>  EXPORT_SYMBOL(efi);
>
> @@ -120,6 +121,8 @@ static ssize_t systab_show(struct kobject *kobj,
>                 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
>         if (efi.uga != EFI_INVALID_TABLE_ADDR)
>                 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
> +       if (efi.capsule != EFI_INVALID_TABLE_ADDR)
> +               str += sprintf(str, "CAPSULE=0x%lx\n", efi.capsule);
>
>         return str - buf;
>  }
> @@ -445,6 +448,7 @@ static __initdata efi_config_table_type_t common_tables[] = {
>         {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table},
>         {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table},
>         {LINUX_EFI_RANDOM_SEED_TABLE_GUID, "RNG", &efi.rng_seed},
> +       {LINUX_EFI_CRASH_GUID, "CAPSULE", &efi.capsule},

LINUX_EFI_CRASH_GUID ??

>         {NULL_GUID, NULL, NULL},
>  };
>
> diff --git a/include/linux/efi.h b/include/linux/efi.h
> index 5b1af30..3296724e 100644
> --- a/include/linux/efi.h
> +++ b/include/linux/efi.h
> @@ -893,6 +893,7 @@ extern struct efi {
>         unsigned long properties_table; /* properties table */
>         unsigned long mem_attr_table;   /* memory attributes table */
>         unsigned long rng_seed;         /* UEFI firmware random seed */
> +       unsigned long capsule;          /* EFI capsule table */
>         efi_get_time_t *get_time;
>         efi_set_time_t *set_time;
>         efi_get_wakeup_time_t *get_wakeup_time;
> @@ -1401,6 +1402,9 @@ extern int efi_capsule_supported(efi_guid_t guid, u32 flags,
>  extern int efi_capsule_update(efi_capsule_header_t *capsule,
>                               struct page **pages);
>
> +extern efi_capsule_header_t **efi_capsule_lookup(efi_guid_t guid,
> +                             uint32_t *nr_caps);
> +
>  #ifdef CONFIG_EFI_RUNTIME_MAP
>  int efi_runtime_map_init(struct kobject *);
>  int efi_get_runtime_map_size(void);
> --
> 2.9.0.GIT
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v1 1/2] efi/capsule: Add 'capsule' lookup support
       [not found] ` <20170228174528.23542-1-qiuxu.zhuo-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
  2017-03-01 20:44   ` Ard Biesheuvel
@ 2017-03-01 20:46   ` Matt Fleming
  1 sibling, 0 replies; 3+ messages in thread
From: Matt Fleming @ 2017-03-01 20:46 UTC (permalink / raw)
  To: Qiuxu Zhuo
  Cc: ard.biesheuvel-QSEj5FYQhm4dnm+yROfE0A,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	tony.luck-ral2JQCrhuEAvxtiuMwx3w

On Wed, 01 Mar, at 01:45:27AM, Qiuxu Zhuo wrote:
> Add the interface to get the array of EFI capsules when
> parsing the configuration tables.
> 
> This code is based on Matt Fleming previous work from:
> https://git.kernel.org/cgit/linux/kernel/git/mfleming/efi.git/commit/?h=capsule-pstore&id=99c5f047133555aa0442f64064e85b7da2d4a45f
> 
> Signed-off-by: Qiuxu Zhuo <qiuxu.zhuo-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
> Signed-off-by: Matt Fleming <matt.fleming-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
> ---
>  drivers/firmware/efi/capsule.c | 107 +++++++++++++++++++++++++++++++++++++++++
>  drivers/firmware/efi/efi.c     |   4 ++
>  include/linux/efi.h            |   4 ++
>  3 files changed, 115 insertions(+)

Please remove this SoB line. You've modified the code enough that my
SoB doesn't belong here.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-03-01 20:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-02-28 17:45 [PATCH v1 1/2] efi/capsule: Add 'capsule' lookup support Qiuxu Zhuo
     [not found] ` <20170228174528.23542-1-qiuxu.zhuo-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2017-03-01 20:44   ` Ard Biesheuvel
2017-03-01 20:46   ` Matt Fleming

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).