Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * esrt.c
  4 *
  5 * This module exports EFI System Resource Table (ESRT) entries into userspace
  6 * through the sysfs file system. The ESRT provides a read-only catalog of
  7 * system components for which the system accepts firmware upgrades via UEFI's
  8 * "Capsule Update" feature. This module allows userland utilities to evaluate
  9 * what firmware updates can be applied to this system, and potentially arrange
 10 * for those updates to occur.
 11 *
 12 * Data is currently found below /sys/firmware/efi/esrt/...
 13 */
 14#define pr_fmt(fmt) "esrt: " fmt
 15
 16#include <linux/capability.h>
 17#include <linux/device.h>
 18#include <linux/efi.h>
 19#include <linux/init.h>
 20#include <linux/io.h>
 21#include <linux/kernel.h>
 22#include <linux/kobject.h>
 23#include <linux/list.h>
 24#include <linux/memblock.h>
 25#include <linux/slab.h>
 26#include <linux/types.h>
 27
 28#include <asm/io.h>
 29#include <asm/early_ioremap.h>
 30
 31struct efi_system_resource_entry_v1 {
 32	efi_guid_t	fw_class;
 33	u32		fw_type;
 34	u32		fw_version;
 35	u32		lowest_supported_fw_version;
 36	u32		capsule_flags;
 37	u32		last_attempt_version;
 38	u32		last_attempt_status;
 39};
 40
 41/*
 42 * _count and _version are what they seem like.  _max is actually just
 43 * accounting info for the firmware when creating the table; it should never
 44 * have been exposed to us.  To wit, the spec says:
 45 * The maximum number of resource array entries that can be within the
 46 * table without reallocating the table, must not be zero.
 47 * Since there's no guidance about what that means in terms of memory layout,
 48 * it means nothing to us.
 49 */
 50struct efi_system_resource_table {
 51	u32	fw_resource_count;
 52	u32	fw_resource_count_max;
 53	u64	fw_resource_version;
 54	u8	entries[];
 55};
 56
 57static phys_addr_t esrt_data;
 58static size_t esrt_data_size;
 59
 60static struct efi_system_resource_table *esrt;
 61
 62struct esre_entry {
 63	union {
 64		struct efi_system_resource_entry_v1 *esre1;
 65	} esre;
 66
 67	struct kobject kobj;
 68	struct list_head list;
 69};
 70
 71/* global list of esre_entry. */
 72static LIST_HEAD(entry_list);
 73
 74/* entry attribute */
 75struct esre_attribute {
 76	struct attribute attr;
 77	ssize_t (*show)(struct esre_entry *entry, char *buf);
 78	ssize_t (*store)(struct esre_entry *entry,
 79			 const char *buf, size_t count);
 80};
 81
 82static struct esre_entry *to_entry(struct kobject *kobj)
 83{
 84	return container_of(kobj, struct esre_entry, kobj);
 85}
 86
 87static struct esre_attribute *to_attr(struct attribute *attr)
 88{
 89	return container_of(attr, struct esre_attribute, attr);
 90}
 91
 92static ssize_t esre_attr_show(struct kobject *kobj,
 93			      struct attribute *_attr, char *buf)
 94{
 95	struct esre_entry *entry = to_entry(kobj);
 96	struct esre_attribute *attr = to_attr(_attr);
 97
 98	/* Don't tell normal users what firmware versions we've got... */
 99	if (!capable(CAP_SYS_ADMIN))
100		return -EACCES;
101
102	return attr->show(entry, buf);
103}
104
105static const struct sysfs_ops esre_attr_ops = {
106	.show = esre_attr_show,
107};
108
109/* Generic ESRT Entry ("ESRE") support. */
110static ssize_t fw_class_show(struct esre_entry *entry, char *buf)
111{
112	char *str = buf;
113
114	efi_guid_to_str(&entry->esre.esre1->fw_class, str);
115	str += strlen(str);
116	str += sprintf(str, "\n");
117
118	return str - buf;
119}
120
121static struct esre_attribute esre_fw_class = __ATTR_RO_MODE(fw_class, 0400);
 
122
123#define esre_attr_decl(name, size, fmt) \
124static ssize_t name##_show(struct esre_entry *entry, char *buf) \
125{ \
126	return sprintf(buf, fmt "\n", \
127		       le##size##_to_cpu(entry->esre.esre1->name)); \
128} \
129\
130static struct esre_attribute esre_##name = __ATTR_RO_MODE(name, 0400)
 
131
132esre_attr_decl(fw_type, 32, "%u");
133esre_attr_decl(fw_version, 32, "%u");
134esre_attr_decl(lowest_supported_fw_version, 32, "%u");
135esre_attr_decl(capsule_flags, 32, "0x%x");
136esre_attr_decl(last_attempt_version, 32, "%u");
137esre_attr_decl(last_attempt_status, 32, "%u");
138
139static struct attribute *esre1_attrs[] = {
140	&esre_fw_class.attr,
141	&esre_fw_type.attr,
142	&esre_fw_version.attr,
143	&esre_lowest_supported_fw_version.attr,
144	&esre_capsule_flags.attr,
145	&esre_last_attempt_version.attr,
146	&esre_last_attempt_status.attr,
147	NULL
148};
149static void esre_release(struct kobject *kobj)
150{
151	struct esre_entry *entry = to_entry(kobj);
152
153	list_del(&entry->list);
154	kfree(entry);
155}
156
157static struct kobj_type esre1_ktype = {
158	.release = esre_release,
159	.sysfs_ops = &esre_attr_ops,
160	.default_attrs = esre1_attrs,
161};
162
163
164static struct kobject *esrt_kobj;
165static struct kset *esrt_kset;
166
167static int esre_create_sysfs_entry(void *esre, int entry_num)
168{
169	struct esre_entry *entry;
170
171	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
172	if (!entry)
173		return -ENOMEM;
174
175	entry->kobj.kset = esrt_kset;
176
177	if (esrt->fw_resource_version == 1) {
178		int rc = 0;
179
180		entry->esre.esre1 = esre;
181		rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
182					  "entry%d", entry_num);
183		if (rc) {
184			kobject_put(&entry->kobj);
185			return rc;
186		}
187	}
188
189	list_add_tail(&entry->list, &entry_list);
190	return 0;
191}
192
193/* support for displaying ESRT fields at the top level */
194#define esrt_attr_decl(name, size, fmt) \
195static ssize_t name##_show(struct kobject *kobj, \
196				  struct kobj_attribute *attr, char *buf)\
197{ \
198	return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \
199} \
200\
201static struct kobj_attribute esrt_##name = __ATTR_RO_MODE(name, 0400)
 
202
203esrt_attr_decl(fw_resource_count, 32, "%u");
204esrt_attr_decl(fw_resource_count_max, 32, "%u");
205esrt_attr_decl(fw_resource_version, 64, "%llu");
206
207static struct attribute *esrt_attrs[] = {
208	&esrt_fw_resource_count.attr,
209	&esrt_fw_resource_count_max.attr,
210	&esrt_fw_resource_version.attr,
211	NULL,
212};
213
214static inline int esrt_table_exists(void)
215{
216	if (!efi_enabled(EFI_CONFIG_TABLES))
217		return 0;
218	if (efi.esrt == EFI_INVALID_TABLE_ADDR)
219		return 0;
220	return 1;
221}
222
223static umode_t esrt_attr_is_visible(struct kobject *kobj,
224				    struct attribute *attr, int n)
225{
226	if (!esrt_table_exists())
227		return 0;
228	return attr->mode;
229}
230
231static const struct attribute_group esrt_attr_group = {
232	.attrs = esrt_attrs,
233	.is_visible = esrt_attr_is_visible,
234};
235
236/*
237 * remap the table, validate it, mark it reserved and unmap it.
238 */
239void __init efi_esrt_init(void)
240{
241	void *va;
242	struct efi_system_resource_table tmpesrt;
 
243	size_t size, max, entry_size, entries_size;
244	efi_memory_desc_t md;
245	int rc;
246	phys_addr_t end;
247
248	if (!efi_enabled(EFI_MEMMAP))
249		return;
250
251	pr_debug("esrt-init: loading.\n");
252	if (!esrt_table_exists())
253		return;
254
255	rc = efi_mem_desc_lookup(efi.esrt, &md);
256	if (rc < 0 ||
257	    (!(md.attribute & EFI_MEMORY_RUNTIME) &&
258	     md.type != EFI_BOOT_SERVICES_DATA &&
259	     md.type != EFI_RUNTIME_SERVICES_DATA)) {
260		pr_warn("ESRT header is not in the memory map.\n");
261		return;
262	}
263
264	max = efi_mem_desc_end(&md);
265	if (max < efi.esrt) {
266		pr_err("EFI memory descriptor is invalid. (esrt: %p max: %p)\n",
267		       (void *)efi.esrt, (void *)max);
268		return;
269	}
270
271	size = sizeof(*esrt);
272	max -= efi.esrt;
273
274	if (max < size) {
275		pr_err("ESRT header doesn't fit on single memory map entry. (size: %zu max: %zu)\n",
276		       size, max);
277		return;
278	}
279
280	va = early_memremap(efi.esrt, size);
281	if (!va) {
282		pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
283		       size);
284		return;
285	}
286
287	memcpy(&tmpesrt, va, sizeof(tmpesrt));
288	early_memunmap(va, size);
289
290	if (tmpesrt.fw_resource_version != 1) {
 
 
291		pr_err("Unsupported ESRT version %lld.\n",
292		       tmpesrt.fw_resource_version);
293		return;
294	}
295
296	entry_size = sizeof(struct efi_system_resource_entry_v1);
297	if (tmpesrt.fw_resource_count > 0 && max - size < entry_size) {
298		pr_err("ESRT memory map entry can only hold the header. (max: %zu size: %zu)\n",
299		       max - size, entry_size);
300		return;
301	}
302
303	/*
304	 * The format doesn't really give us any boundary to test here,
305	 * so I'm making up 128 as the max number of individually updatable
306	 * components we support.
307	 * 128 should be pretty excessive, but there's still some chance
308	 * somebody will do that someday and we'll need to raise this.
309	 */
310	if (tmpesrt.fw_resource_count > 128) {
311		pr_err("ESRT says fw_resource_count has very large value %d.\n",
312		       tmpesrt.fw_resource_count);
313		return;
314	}
315
316	/*
317	 * We know it can't be larger than N * sizeof() here, and N is limited
318	 * by the previous test to a small number, so there's no overflow.
319	 */
320	entries_size = tmpesrt.fw_resource_count * entry_size;
321	if (max < size + entries_size) {
322		pr_err("ESRT does not fit on single memory map entry (size: %zu max: %zu)\n",
323		       size, max);
324		return;
325	}
326
 
 
327	size += entries_size;
 
 
 
 
 
 
328
329	esrt_data = (phys_addr_t)efi.esrt;
330	esrt_data_size = size;
331
332	end = esrt_data + size;
333	pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end);
334	if (md.type == EFI_BOOT_SERVICES_DATA)
335		efi_mem_reserve(esrt_data, esrt_data_size);
336
337	pr_debug("esrt-init: loaded.\n");
 
 
338}
339
340static int __init register_entries(void)
341{
342	struct efi_system_resource_entry_v1 *v1_entries = (void *)esrt->entries;
343	int i, rc;
344
345	if (!esrt_table_exists())
346		return 0;
347
348	for (i = 0; i < le32_to_cpu(esrt->fw_resource_count); i++) {
349		void *esre = NULL;
350		if (esrt->fw_resource_version == 1) {
351			esre = &v1_entries[i];
352		} else {
353			pr_err("Unsupported ESRT version %lld.\n",
354			       esrt->fw_resource_version);
355			return -EINVAL;
356		}
357
358		rc = esre_create_sysfs_entry(esre, i);
359		if (rc < 0) {
360			pr_err("ESRT entry creation failed with error %d.\n",
361			       rc);
362			return rc;
363		}
364	}
365	return 0;
366}
367
368static void cleanup_entry_list(void)
369{
370	struct esre_entry *entry, *next;
371
372	list_for_each_entry_safe(entry, next, &entry_list, list) {
373		kobject_put(&entry->kobj);
374	}
375}
376
377static int __init esrt_sysfs_init(void)
378{
379	int error;
 
380
381	pr_debug("esrt-sysfs: loading.\n");
382	if (!esrt_data || !esrt_data_size)
383		return -ENOSYS;
384
385	esrt = memremap(esrt_data, esrt_data_size, MEMREMAP_WB);
386	if (!esrt) {
387		pr_err("memremap(%pa, %zu) failed.\n", &esrt_data,
388		       esrt_data_size);
389		return -ENOMEM;
390	}
391
 
 
 
 
 
 
 
 
 
392	esrt_kobj = kobject_create_and_add("esrt", efi_kobj);
393	if (!esrt_kobj) {
394		pr_err("Firmware table registration failed.\n");
395		error = -ENOMEM;
396		goto err;
397	}
398
399	error = sysfs_create_group(esrt_kobj, &esrt_attr_group);
400	if (error) {
401		pr_err("Sysfs attribute export failed with error %d.\n",
402		       error);
403		goto err_remove_esrt;
404	}
405
406	esrt_kset = kset_create_and_add("entries", NULL, esrt_kobj);
407	if (!esrt_kset) {
408		pr_err("kset creation failed.\n");
409		error = -ENOMEM;
410		goto err_remove_group;
411	}
412
413	error = register_entries();
414	if (error)
415		goto err_cleanup_list;
416
 
 
417	pr_debug("esrt-sysfs: loaded.\n");
418
419	return 0;
420err_cleanup_list:
421	cleanup_entry_list();
422	kset_unregister(esrt_kset);
423err_remove_group:
424	sysfs_remove_group(esrt_kobj, &esrt_attr_group);
425err_remove_esrt:
426	kobject_put(esrt_kobj);
427err:
428	memunmap(esrt);
429	esrt = NULL;
430	return error;
431}
432device_initcall(esrt_sysfs_init);
433
434/*
435MODULE_AUTHOR("Peter Jones <pjones@redhat.com>");
436MODULE_DESCRIPTION("EFI System Resource Table support");
437MODULE_LICENSE("GPL");
438*/
v4.6
 
  1/*
  2 * esrt.c
  3 *
  4 * This module exports EFI System Resource Table (ESRT) entries into userspace
  5 * through the sysfs file system. The ESRT provides a read-only catalog of
  6 * system components for which the system accepts firmware upgrades via UEFI's
  7 * "Capsule Update" feature. This module allows userland utilities to evaluate
  8 * what firmware updates can be applied to this system, and potentially arrange
  9 * for those updates to occur.
 10 *
 11 * Data is currently found below /sys/firmware/efi/esrt/...
 12 */
 13#define pr_fmt(fmt) "esrt: " fmt
 14
 15#include <linux/capability.h>
 16#include <linux/device.h>
 17#include <linux/efi.h>
 18#include <linux/init.h>
 
 19#include <linux/kernel.h>
 20#include <linux/kobject.h>
 21#include <linux/list.h>
 22#include <linux/memblock.h>
 23#include <linux/slab.h>
 24#include <linux/types.h>
 25
 26#include <asm/io.h>
 27#include <asm/early_ioremap.h>
 28
 29struct efi_system_resource_entry_v1 {
 30	efi_guid_t	fw_class;
 31	u32		fw_type;
 32	u32		fw_version;
 33	u32		lowest_supported_fw_version;
 34	u32		capsule_flags;
 35	u32		last_attempt_version;
 36	u32		last_attempt_status;
 37};
 38
 39/*
 40 * _count and _version are what they seem like.  _max is actually just
 41 * accounting info for the firmware when creating the table; it should never
 42 * have been exposed to us.  To wit, the spec says:
 43 * The maximum number of resource array entries that can be within the
 44 * table without reallocating the table, must not be zero.
 45 * Since there's no guidance about what that means in terms of memory layout,
 46 * it means nothing to us.
 47 */
 48struct efi_system_resource_table {
 49	u32	fw_resource_count;
 50	u32	fw_resource_count_max;
 51	u64	fw_resource_version;
 52	u8	entries[];
 53};
 54
 55static phys_addr_t esrt_data;
 56static size_t esrt_data_size;
 57
 58static struct efi_system_resource_table *esrt;
 59
 60struct esre_entry {
 61	union {
 62		struct efi_system_resource_entry_v1 *esre1;
 63	} esre;
 64
 65	struct kobject kobj;
 66	struct list_head list;
 67};
 68
 69/* global list of esre_entry. */
 70static LIST_HEAD(entry_list);
 71
 72/* entry attribute */
 73struct esre_attribute {
 74	struct attribute attr;
 75	ssize_t (*show)(struct esre_entry *entry, char *buf);
 76	ssize_t (*store)(struct esre_entry *entry,
 77			 const char *buf, size_t count);
 78};
 79
 80static struct esre_entry *to_entry(struct kobject *kobj)
 81{
 82	return container_of(kobj, struct esre_entry, kobj);
 83}
 84
 85static struct esre_attribute *to_attr(struct attribute *attr)
 86{
 87	return container_of(attr, struct esre_attribute, attr);
 88}
 89
 90static ssize_t esre_attr_show(struct kobject *kobj,
 91			      struct attribute *_attr, char *buf)
 92{
 93	struct esre_entry *entry = to_entry(kobj);
 94	struct esre_attribute *attr = to_attr(_attr);
 95
 96	/* Don't tell normal users what firmware versions we've got... */
 97	if (!capable(CAP_SYS_ADMIN))
 98		return -EACCES;
 99
100	return attr->show(entry, buf);
101}
102
103static const struct sysfs_ops esre_attr_ops = {
104	.show = esre_attr_show,
105};
106
107/* Generic ESRT Entry ("ESRE") support. */
108static ssize_t esre_fw_class_show(struct esre_entry *entry, char *buf)
109{
110	char *str = buf;
111
112	efi_guid_to_str(&entry->esre.esre1->fw_class, str);
113	str += strlen(str);
114	str += sprintf(str, "\n");
115
116	return str - buf;
117}
118
119static struct esre_attribute esre_fw_class = __ATTR(fw_class, 0400,
120	esre_fw_class_show, NULL);
121
122#define esre_attr_decl(name, size, fmt) \
123static ssize_t esre_##name##_show(struct esre_entry *entry, char *buf) \
124{ \
125	return sprintf(buf, fmt "\n", \
126		       le##size##_to_cpu(entry->esre.esre1->name)); \
127} \
128\
129static struct esre_attribute esre_##name = __ATTR(name, 0400, \
130	esre_##name##_show, NULL)
131
132esre_attr_decl(fw_type, 32, "%u");
133esre_attr_decl(fw_version, 32, "%u");
134esre_attr_decl(lowest_supported_fw_version, 32, "%u");
135esre_attr_decl(capsule_flags, 32, "0x%x");
136esre_attr_decl(last_attempt_version, 32, "%u");
137esre_attr_decl(last_attempt_status, 32, "%u");
138
139static struct attribute *esre1_attrs[] = {
140	&esre_fw_class.attr,
141	&esre_fw_type.attr,
142	&esre_fw_version.attr,
143	&esre_lowest_supported_fw_version.attr,
144	&esre_capsule_flags.attr,
145	&esre_last_attempt_version.attr,
146	&esre_last_attempt_status.attr,
147	NULL
148};
149static void esre_release(struct kobject *kobj)
150{
151	struct esre_entry *entry = to_entry(kobj);
152
153	list_del(&entry->list);
154	kfree(entry);
155}
156
157static struct kobj_type esre1_ktype = {
158	.release = esre_release,
159	.sysfs_ops = &esre_attr_ops,
160	.default_attrs = esre1_attrs,
161};
162
163
164static struct kobject *esrt_kobj;
165static struct kset *esrt_kset;
166
167static int esre_create_sysfs_entry(void *esre, int entry_num)
168{
169	struct esre_entry *entry;
170
171	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
172	if (!entry)
173		return -ENOMEM;
174
175	entry->kobj.kset = esrt_kset;
176
177	if (esrt->fw_resource_version == 1) {
178		int rc = 0;
179
180		entry->esre.esre1 = esre;
181		rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
182					  "entry%d", entry_num);
183		if (rc) {
184			kfree(entry);
185			return rc;
186		}
187	}
188
189	list_add_tail(&entry->list, &entry_list);
190	return 0;
191}
192
193/* support for displaying ESRT fields at the top level */
194#define esrt_attr_decl(name, size, fmt) \
195static ssize_t esrt_##name##_show(struct kobject *kobj, \
196				  struct kobj_attribute *attr, char *buf)\
197{ \
198	return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \
199} \
200\
201static struct kobj_attribute esrt_##name = __ATTR(name, 0400, \
202	esrt_##name##_show, NULL)
203
204esrt_attr_decl(fw_resource_count, 32, "%u");
205esrt_attr_decl(fw_resource_count_max, 32, "%u");
206esrt_attr_decl(fw_resource_version, 64, "%llu");
207
208static struct attribute *esrt_attrs[] = {
209	&esrt_fw_resource_count.attr,
210	&esrt_fw_resource_count_max.attr,
211	&esrt_fw_resource_version.attr,
212	NULL,
213};
214
215static inline int esrt_table_exists(void)
216{
217	if (!efi_enabled(EFI_CONFIG_TABLES))
218		return 0;
219	if (efi.esrt == EFI_INVALID_TABLE_ADDR)
220		return 0;
221	return 1;
222}
223
224static umode_t esrt_attr_is_visible(struct kobject *kobj,
225				    struct attribute *attr, int n)
226{
227	if (!esrt_table_exists())
228		return 0;
229	return attr->mode;
230}
231
232static struct attribute_group esrt_attr_group = {
233	.attrs = esrt_attrs,
234	.is_visible = esrt_attr_is_visible,
235};
236
237/*
238 * remap the table, copy it to kmalloced pages, and unmap it.
239 */
240void __init efi_esrt_init(void)
241{
242	void *va;
243	struct efi_system_resource_table tmpesrt;
244	struct efi_system_resource_entry_v1 *v1_entries;
245	size_t size, max, entry_size, entries_size;
246	efi_memory_desc_t md;
247	int rc;
248	phys_addr_t end;
249
 
 
 
250	pr_debug("esrt-init: loading.\n");
251	if (!esrt_table_exists())
252		return;
253
254	rc = efi_mem_desc_lookup(efi.esrt, &md);
255	if (rc < 0) {
256		pr_err("ESRT header is not in the memory map.\n");
 
 
 
257		return;
258	}
259
260	max = efi_mem_desc_end(&md);
261	if (max < efi.esrt) {
262		pr_err("EFI memory descriptor is invalid. (esrt: %p max: %p)\n",
263		       (void *)efi.esrt, (void *)max);
264		return;
265	}
266
267	size = sizeof(*esrt);
268	max -= efi.esrt;
269
270	if (max < size) {
271		pr_err("ESRT header doen't fit on single memory map entry. (size: %zu max: %zu)\n",
272		       size, max);
273		return;
274	}
275
276	va = early_memremap(efi.esrt, size);
277	if (!va) {
278		pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
279		       size);
280		return;
281	}
282
283	memcpy(&tmpesrt, va, sizeof(tmpesrt));
 
284
285	if (tmpesrt.fw_resource_version == 1) {
286		entry_size = sizeof (*v1_entries);
287	} else {
288		pr_err("Unsupported ESRT version %lld.\n",
289		       tmpesrt.fw_resource_version);
290		return;
291	}
292
 
293	if (tmpesrt.fw_resource_count > 0 && max - size < entry_size) {
294		pr_err("ESRT memory map entry can only hold the header. (max: %zu size: %zu)\n",
295		       max - size, entry_size);
296		goto err_memunmap;
297	}
298
299	/*
300	 * The format doesn't really give us any boundary to test here,
301	 * so I'm making up 128 as the max number of individually updatable
302	 * components we support.
303	 * 128 should be pretty excessive, but there's still some chance
304	 * somebody will do that someday and we'll need to raise this.
305	 */
306	if (tmpesrt.fw_resource_count > 128) {
307		pr_err("ESRT says fw_resource_count has very large value %d.\n",
308		       tmpesrt.fw_resource_count);
309		goto err_memunmap;
310	}
311
312	/*
313	 * We know it can't be larger than N * sizeof() here, and N is limited
314	 * by the previous test to a small number, so there's no overflow.
315	 */
316	entries_size = tmpesrt.fw_resource_count * entry_size;
317	if (max < size + entries_size) {
318		pr_err("ESRT does not fit on single memory map entry (size: %zu max: %zu)\n",
319		       size, max);
320		goto err_memunmap;
321	}
322
323	/* remap it with our (plausible) new pages */
324	early_memunmap(va, size);
325	size += entries_size;
326	va = early_memremap(efi.esrt, size);
327	if (!va) {
328		pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
329		       size);
330		return;
331	}
332
333	esrt_data = (phys_addr_t)efi.esrt;
334	esrt_data_size = size;
335
336	end = esrt_data + size;
337	pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end);
338	memblock_reserve(esrt_data, esrt_data_size);
 
339
340	pr_debug("esrt-init: loaded.\n");
341err_memunmap:
342	early_memunmap(va, size);
343}
344
345static int __init register_entries(void)
346{
347	struct efi_system_resource_entry_v1 *v1_entries = (void *)esrt->entries;
348	int i, rc;
349
350	if (!esrt_table_exists())
351		return 0;
352
353	for (i = 0; i < le32_to_cpu(esrt->fw_resource_count); i++) {
354		void *esre = NULL;
355		if (esrt->fw_resource_version == 1) {
356			esre = &v1_entries[i];
357		} else {
358			pr_err("Unsupported ESRT version %lld.\n",
359			       esrt->fw_resource_version);
360			return -EINVAL;
361		}
362
363		rc = esre_create_sysfs_entry(esre, i);
364		if (rc < 0) {
365			pr_err("ESRT entry creation failed with error %d.\n",
366			       rc);
367			return rc;
368		}
369	}
370	return 0;
371}
372
373static void cleanup_entry_list(void)
374{
375	struct esre_entry *entry, *next;
376
377	list_for_each_entry_safe(entry, next, &entry_list, list) {
378		kobject_put(&entry->kobj);
379	}
380}
381
382static int __init esrt_sysfs_init(void)
383{
384	int error;
385	struct efi_system_resource_table __iomem *ioesrt;
386
387	pr_debug("esrt-sysfs: loading.\n");
388	if (!esrt_data || !esrt_data_size)
389		return -ENOSYS;
390
391	ioesrt = ioremap(esrt_data, esrt_data_size);
392	if (!ioesrt) {
393		pr_err("ioremap(%pa, %zu) failed.\n", &esrt_data,
394		       esrt_data_size);
395		return -ENOMEM;
396	}
397
398	esrt = kmalloc(esrt_data_size, GFP_KERNEL);
399	if (!esrt) {
400		pr_err("kmalloc failed. (wanted %zu bytes)\n", esrt_data_size);
401		iounmap(ioesrt);
402		return -ENOMEM;
403	}
404
405	memcpy_fromio(esrt, ioesrt, esrt_data_size);
406
407	esrt_kobj = kobject_create_and_add("esrt", efi_kobj);
408	if (!esrt_kobj) {
409		pr_err("Firmware table registration failed.\n");
410		error = -ENOMEM;
411		goto err;
412	}
413
414	error = sysfs_create_group(esrt_kobj, &esrt_attr_group);
415	if (error) {
416		pr_err("Sysfs attribute export failed with error %d.\n",
417		       error);
418		goto err_remove_esrt;
419	}
420
421	esrt_kset = kset_create_and_add("entries", NULL, esrt_kobj);
422	if (!esrt_kset) {
423		pr_err("kset creation failed.\n");
424		error = -ENOMEM;
425		goto err_remove_group;
426	}
427
428	error = register_entries();
429	if (error)
430		goto err_cleanup_list;
431
432	memblock_remove(esrt_data, esrt_data_size);
433
434	pr_debug("esrt-sysfs: loaded.\n");
435
436	return 0;
437err_cleanup_list:
438	cleanup_entry_list();
439	kset_unregister(esrt_kset);
440err_remove_group:
441	sysfs_remove_group(esrt_kobj, &esrt_attr_group);
442err_remove_esrt:
443	kobject_put(esrt_kobj);
444err:
445	kfree(esrt);
446	esrt = NULL;
447	return error;
448}
449device_initcall(esrt_sysfs_init);
450
451/*
452MODULE_AUTHOR("Peter Jones <pjones@redhat.com>");
453MODULE_DESCRIPTION("EFI System Resource Table support");
454MODULE_LICENSE("GPL");
455*/