Linux Audio

Check our new training course

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