Loading...
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};
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 return attr->show(entry, buf);
97}
98
99static const struct sysfs_ops esre_attr_ops = {
100 .show = esre_attr_show,
101};
102
103/* Generic ESRT Entry ("ESRE") support. */
104static ssize_t fw_class_show(struct esre_entry *entry, char *buf)
105{
106 char *str = buf;
107
108 efi_guid_to_str(&entry->esre.esre1->fw_class, str);
109 str += strlen(str);
110 str += sprintf(str, "\n");
111
112 return str - buf;
113}
114
115static struct esre_attribute esre_fw_class = __ATTR_RO_MODE(fw_class, 0400);
116
117#define esre_attr_decl(name, size, fmt) \
118static ssize_t name##_show(struct esre_entry *entry, char *buf) \
119{ \
120 return sprintf(buf, fmt "\n", \
121 le##size##_to_cpu(entry->esre.esre1->name)); \
122} \
123\
124static struct esre_attribute esre_##name = __ATTR_RO_MODE(name, 0400)
125
126esre_attr_decl(fw_type, 32, "%u");
127esre_attr_decl(fw_version, 32, "%u");
128esre_attr_decl(lowest_supported_fw_version, 32, "%u");
129esre_attr_decl(capsule_flags, 32, "0x%x");
130esre_attr_decl(last_attempt_version, 32, "%u");
131esre_attr_decl(last_attempt_status, 32, "%u");
132
133static struct attribute *esre1_attrs[] = {
134 &esre_fw_class.attr,
135 &esre_fw_type.attr,
136 &esre_fw_version.attr,
137 &esre_lowest_supported_fw_version.attr,
138 &esre_capsule_flags.attr,
139 &esre_last_attempt_version.attr,
140 &esre_last_attempt_status.attr,
141 NULL
142};
143ATTRIBUTE_GROUPS(esre1);
144
145static void esre_release(struct kobject *kobj)
146{
147 struct esre_entry *entry = to_entry(kobj);
148
149 list_del(&entry->list);
150 kfree(entry);
151}
152
153static const struct kobj_type esre1_ktype = {
154 .release = esre_release,
155 .sysfs_ops = &esre_attr_ops,
156 .default_groups = esre1_groups,
157};
158
159
160static struct kobject *esrt_kobj;
161static struct kset *esrt_kset;
162
163static int esre_create_sysfs_entry(void *esre, int entry_num)
164{
165 struct esre_entry *entry;
166
167 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
168 if (!entry)
169 return -ENOMEM;
170
171 entry->kobj.kset = esrt_kset;
172
173 if (esrt->fw_resource_version == 1) {
174 int rc = 0;
175
176 entry->esre.esre1 = esre;
177 rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
178 "entry%d", entry_num);
179 if (rc) {
180 kobject_put(&entry->kobj);
181 return rc;
182 }
183 }
184
185 list_add_tail(&entry->list, &entry_list);
186 return 0;
187}
188
189/* support for displaying ESRT fields at the top level */
190#define esrt_attr_decl(name, size, fmt) \
191static ssize_t name##_show(struct kobject *kobj, \
192 struct kobj_attribute *attr, char *buf)\
193{ \
194 return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \
195} \
196\
197static struct kobj_attribute esrt_##name = __ATTR_RO_MODE(name, 0400)
198
199esrt_attr_decl(fw_resource_count, 32, "%u");
200esrt_attr_decl(fw_resource_count_max, 32, "%u");
201esrt_attr_decl(fw_resource_version, 64, "%llu");
202
203static struct attribute *esrt_attrs[] = {
204 &esrt_fw_resource_count.attr,
205 &esrt_fw_resource_count_max.attr,
206 &esrt_fw_resource_version.attr,
207 NULL,
208};
209
210static inline int esrt_table_exists(void)
211{
212 if (!efi_enabled(EFI_CONFIG_TABLES))
213 return 0;
214 if (efi.esrt == EFI_INVALID_TABLE_ADDR)
215 return 0;
216 return 1;
217}
218
219static umode_t esrt_attr_is_visible(struct kobject *kobj,
220 struct attribute *attr, int n)
221{
222 if (!esrt_table_exists())
223 return 0;
224 return attr->mode;
225}
226
227static const struct attribute_group esrt_attr_group = {
228 .attrs = esrt_attrs,
229 .is_visible = esrt_attr_is_visible,
230};
231
232/*
233 * remap the table, validate it, mark it reserved and unmap it.
234 */
235void __init efi_esrt_init(void)
236{
237 void *va;
238 struct efi_system_resource_table tmpesrt;
239 size_t size, max, entry_size, entries_size;
240 efi_memory_desc_t md;
241 int rc;
242 phys_addr_t end;
243
244 if (!efi_enabled(EFI_MEMMAP) && !efi_enabled(EFI_PARAVIRT))
245 return;
246
247 pr_debug("esrt-init: loading.\n");
248 if (!esrt_table_exists())
249 return;
250
251 rc = efi_mem_desc_lookup(efi.esrt, &md);
252 if (rc < 0 ||
253 (!(md.attribute & EFI_MEMORY_RUNTIME) &&
254 md.type != EFI_BOOT_SERVICES_DATA &&
255 md.type != EFI_RUNTIME_SERVICES_DATA &&
256 md.type != EFI_ACPI_RECLAIM_MEMORY &&
257 md.type != EFI_ACPI_MEMORY_NVS)) {
258 pr_warn("ESRT header is not in the memory map.\n");
259 return;
260 }
261
262 max = efi_mem_desc_end(&md) - efi.esrt;
263 size = sizeof(*esrt);
264
265 if (max < size) {
266 pr_err("ESRT header doesn't fit on single memory map entry. (size: %zu max: %zu)\n",
267 size, max);
268 return;
269 }
270
271 va = early_memremap(efi.esrt, size);
272 if (!va) {
273 pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
274 size);
275 return;
276 }
277
278 memcpy(&tmpesrt, va, sizeof(tmpesrt));
279 early_memunmap(va, size);
280
281 if (tmpesrt.fw_resource_version != 1) {
282 pr_err("Unsupported ESRT version %lld.\n",
283 tmpesrt.fw_resource_version);
284 return;
285 }
286
287 entry_size = sizeof(struct efi_system_resource_entry_v1);
288 if (tmpesrt.fw_resource_count > 0 && max - size < entry_size) {
289 pr_err("ESRT memory map entry can only hold the header. (max: %zu size: %zu)\n",
290 max - size, entry_size);
291 return;
292 }
293
294 /*
295 * The format doesn't really give us any boundary to test here,
296 * so I'm making up 128 as the max number of individually updatable
297 * components we support.
298 * 128 should be pretty excessive, but there's still some chance
299 * somebody will do that someday and we'll need to raise this.
300 */
301 if (tmpesrt.fw_resource_count > 128) {
302 pr_err("ESRT says fw_resource_count has very large value %d.\n",
303 tmpesrt.fw_resource_count);
304 return;
305 }
306
307 /*
308 * We know it can't be larger than N * sizeof() here, and N is limited
309 * by the previous test to a small number, so there's no overflow.
310 */
311 entries_size = tmpesrt.fw_resource_count * entry_size;
312 if (max < size + entries_size) {
313 pr_err("ESRT does not fit on single memory map entry (size: %zu max: %zu)\n",
314 size, max);
315 return;
316 }
317
318 size += entries_size;
319
320 esrt_data = (phys_addr_t)efi.esrt;
321 esrt_data_size = size;
322
323 end = esrt_data + size;
324 pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end);
325 if (md.type == EFI_BOOT_SERVICES_DATA)
326 efi_mem_reserve(esrt_data, esrt_data_size);
327
328 pr_debug("esrt-init: loaded.\n");
329}
330
331static int __init register_entries(void)
332{
333 struct efi_system_resource_entry_v1 *v1_entries = (void *)esrt->entries;
334 int i, rc;
335
336 if (!esrt_table_exists())
337 return 0;
338
339 for (i = 0; i < le32_to_cpu(esrt->fw_resource_count); i++) {
340 void *esre = NULL;
341 if (esrt->fw_resource_version == 1) {
342 esre = &v1_entries[i];
343 } else {
344 pr_err("Unsupported ESRT version %lld.\n",
345 esrt->fw_resource_version);
346 return -EINVAL;
347 }
348
349 rc = esre_create_sysfs_entry(esre, i);
350 if (rc < 0) {
351 pr_err("ESRT entry creation failed with error %d.\n",
352 rc);
353 return rc;
354 }
355 }
356 return 0;
357}
358
359static void cleanup_entry_list(void)
360{
361 struct esre_entry *entry, *next;
362
363 list_for_each_entry_safe(entry, next, &entry_list, list) {
364 kobject_put(&entry->kobj);
365 }
366}
367
368static int __init esrt_sysfs_init(void)
369{
370 int error;
371
372 pr_debug("esrt-sysfs: loading.\n");
373 if (!esrt_data || !esrt_data_size)
374 return -ENOSYS;
375
376 esrt = memremap(esrt_data, esrt_data_size, MEMREMAP_WB);
377 if (!esrt) {
378 pr_err("memremap(%pa, %zu) failed.\n", &esrt_data,
379 esrt_data_size);
380 return -ENOMEM;
381 }
382
383 esrt_kobj = kobject_create_and_add("esrt", efi_kobj);
384 if (!esrt_kobj) {
385 pr_err("Firmware table registration failed.\n");
386 error = -ENOMEM;
387 goto err;
388 }
389
390 error = sysfs_create_group(esrt_kobj, &esrt_attr_group);
391 if (error) {
392 pr_err("Sysfs attribute export failed with error %d.\n",
393 error);
394 goto err_remove_esrt;
395 }
396
397 esrt_kset = kset_create_and_add("entries", NULL, esrt_kobj);
398 if (!esrt_kset) {
399 pr_err("kset creation failed.\n");
400 error = -ENOMEM;
401 goto err_remove_group;
402 }
403
404 error = register_entries();
405 if (error)
406 goto err_cleanup_list;
407
408 pr_debug("esrt-sysfs: loaded.\n");
409
410 return 0;
411err_cleanup_list:
412 cleanup_entry_list();
413 kset_unregister(esrt_kset);
414err_remove_group:
415 sysfs_remove_group(esrt_kobj, &esrt_attr_group);
416err_remove_esrt:
417 kobject_put(esrt_kobj);
418err:
419 memunmap(esrt);
420 esrt = NULL;
421 return error;
422}
423device_initcall(esrt_sysfs_init);
424
425/*
426MODULE_AUTHOR("Peter Jones <pjones@redhat.com>");
427MODULE_DESCRIPTION("EFI System Resource Table support");
428MODULE_LICENSE("GPL");
429*/
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 return attr->show(entry, buf);
99}
100
101static const struct sysfs_ops esre_attr_ops = {
102 .show = esre_attr_show,
103};
104
105/* Generic ESRT Entry ("ESRE") support. */
106static ssize_t fw_class_show(struct esre_entry *entry, char *buf)
107{
108 char *str = buf;
109
110 efi_guid_to_str(&entry->esre.esre1->fw_class, str);
111 str += strlen(str);
112 str += sprintf(str, "\n");
113
114 return str - buf;
115}
116
117static struct esre_attribute esre_fw_class = __ATTR_RO_MODE(fw_class, 0400);
118
119#define esre_attr_decl(name, size, fmt) \
120static ssize_t name##_show(struct esre_entry *entry, char *buf) \
121{ \
122 return sprintf(buf, fmt "\n", \
123 le##size##_to_cpu(entry->esre.esre1->name)); \
124} \
125\
126static struct esre_attribute esre_##name = __ATTR_RO_MODE(name, 0400)
127
128esre_attr_decl(fw_type, 32, "%u");
129esre_attr_decl(fw_version, 32, "%u");
130esre_attr_decl(lowest_supported_fw_version, 32, "%u");
131esre_attr_decl(capsule_flags, 32, "0x%x");
132esre_attr_decl(last_attempt_version, 32, "%u");
133esre_attr_decl(last_attempt_status, 32, "%u");
134
135static struct attribute *esre1_attrs[] = {
136 &esre_fw_class.attr,
137 &esre_fw_type.attr,
138 &esre_fw_version.attr,
139 &esre_lowest_supported_fw_version.attr,
140 &esre_capsule_flags.attr,
141 &esre_last_attempt_version.attr,
142 &esre_last_attempt_status.attr,
143 NULL
144};
145ATTRIBUTE_GROUPS(esre1);
146
147static void esre_release(struct kobject *kobj)
148{
149 struct esre_entry *entry = to_entry(kobj);
150
151 list_del(&entry->list);
152 kfree(entry);
153}
154
155static const struct kobj_type esre1_ktype = {
156 .release = esre_release,
157 .sysfs_ops = &esre_attr_ops,
158 .default_groups = esre1_groups,
159};
160
161
162static struct kobject *esrt_kobj;
163static struct kset *esrt_kset;
164
165static int esre_create_sysfs_entry(void *esre, int entry_num)
166{
167 struct esre_entry *entry;
168
169 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
170 if (!entry)
171 return -ENOMEM;
172
173 entry->kobj.kset = esrt_kset;
174
175 if (esrt->fw_resource_version == 1) {
176 int rc = 0;
177
178 entry->esre.esre1 = esre;
179 rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
180 "entry%d", entry_num);
181 if (rc) {
182 kobject_put(&entry->kobj);
183 return rc;
184 }
185 }
186
187 list_add_tail(&entry->list, &entry_list);
188 return 0;
189}
190
191/* support for displaying ESRT fields at the top level */
192#define esrt_attr_decl(name, size, fmt) \
193static ssize_t name##_show(struct kobject *kobj, \
194 struct kobj_attribute *attr, char *buf)\
195{ \
196 return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \
197} \
198\
199static struct kobj_attribute esrt_##name = __ATTR_RO_MODE(name, 0400)
200
201esrt_attr_decl(fw_resource_count, 32, "%u");
202esrt_attr_decl(fw_resource_count_max, 32, "%u");
203esrt_attr_decl(fw_resource_version, 64, "%llu");
204
205static struct attribute *esrt_attrs[] = {
206 &esrt_fw_resource_count.attr,
207 &esrt_fw_resource_count_max.attr,
208 &esrt_fw_resource_version.attr,
209 NULL,
210};
211
212static inline int esrt_table_exists(void)
213{
214 if (!efi_enabled(EFI_CONFIG_TABLES))
215 return 0;
216 if (efi.esrt == EFI_INVALID_TABLE_ADDR)
217 return 0;
218 return 1;
219}
220
221static umode_t esrt_attr_is_visible(struct kobject *kobj,
222 struct attribute *attr, int n)
223{
224 if (!esrt_table_exists())
225 return 0;
226 return attr->mode;
227}
228
229static const struct attribute_group esrt_attr_group = {
230 .attrs = esrt_attrs,
231 .is_visible = esrt_attr_is_visible,
232};
233
234/*
235 * remap the table, validate it, mark it reserved and unmap it.
236 */
237void __init efi_esrt_init(void)
238{
239 void *va;
240 struct efi_system_resource_table tmpesrt;
241 size_t size, max, entry_size, entries_size;
242 efi_memory_desc_t md;
243 int rc;
244 phys_addr_t end;
245
246 if (!efi_enabled(EFI_MEMMAP) && !efi_enabled(EFI_PARAVIRT))
247 return;
248
249 pr_debug("esrt-init: loading.\n");
250 if (!esrt_table_exists())
251 return;
252
253 rc = efi_mem_desc_lookup(efi.esrt, &md);
254 if (rc < 0 ||
255 (!(md.attribute & EFI_MEMORY_RUNTIME) &&
256 md.type != EFI_BOOT_SERVICES_DATA &&
257 md.type != EFI_RUNTIME_SERVICES_DATA &&
258 md.type != EFI_ACPI_RECLAIM_MEMORY &&
259 md.type != EFI_ACPI_MEMORY_NVS)) {
260 pr_warn("ESRT header is not in the memory map.\n");
261 return;
262 }
263
264 max = efi_mem_desc_end(&md) - efi.esrt;
265 size = sizeof(*esrt);
266
267 if (max < size) {
268 pr_err("ESRT header doesn't fit on single memory map entry. (size: %zu max: %zu)\n",
269 size, max);
270 return;
271 }
272
273 va = early_memremap(efi.esrt, size);
274 if (!va) {
275 pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
276 size);
277 return;
278 }
279
280 memcpy(&tmpesrt, va, sizeof(tmpesrt));
281 early_memunmap(va, size);
282
283 if (tmpesrt.fw_resource_version != 1) {
284 pr_err("Unsupported ESRT version %lld.\n",
285 tmpesrt.fw_resource_version);
286 return;
287 }
288
289 entry_size = sizeof(struct efi_system_resource_entry_v1);
290 if (tmpesrt.fw_resource_count > 0 && max - size < entry_size) {
291 pr_err("ESRT memory map entry can only hold the header. (max: %zu size: %zu)\n",
292 max - size, entry_size);
293 return;
294 }
295
296 /*
297 * The format doesn't really give us any boundary to test here,
298 * so I'm making up 128 as the max number of individually updatable
299 * components we support.
300 * 128 should be pretty excessive, but there's still some chance
301 * somebody will do that someday and we'll need to raise this.
302 */
303 if (tmpesrt.fw_resource_count > 128) {
304 pr_err("ESRT says fw_resource_count has very large value %d.\n",
305 tmpesrt.fw_resource_count);
306 return;
307 }
308
309 /*
310 * We know it can't be larger than N * sizeof() here, and N is limited
311 * by the previous test to a small number, so there's no overflow.
312 */
313 entries_size = tmpesrt.fw_resource_count * entry_size;
314 if (max < size + entries_size) {
315 pr_err("ESRT does not fit on single memory map entry (size: %zu max: %zu)\n",
316 size, max);
317 return;
318 }
319
320 size += entries_size;
321
322 esrt_data = (phys_addr_t)efi.esrt;
323 esrt_data_size = size;
324
325 end = esrt_data + size;
326 pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end);
327 if (md.type == EFI_BOOT_SERVICES_DATA)
328 efi_mem_reserve(esrt_data, esrt_data_size);
329
330 pr_debug("esrt-init: loaded.\n");
331}
332
333static int __init register_entries(void)
334{
335 struct efi_system_resource_entry_v1 *v1_entries = (void *)esrt->entries;
336 int i, rc;
337
338 if (!esrt_table_exists())
339 return 0;
340
341 for (i = 0; i < le32_to_cpu(esrt->fw_resource_count); i++) {
342 void *esre = NULL;
343 if (esrt->fw_resource_version == 1) {
344 esre = &v1_entries[i];
345 } else {
346 pr_err("Unsupported ESRT version %lld.\n",
347 esrt->fw_resource_version);
348 return -EINVAL;
349 }
350
351 rc = esre_create_sysfs_entry(esre, i);
352 if (rc < 0) {
353 pr_err("ESRT entry creation failed with error %d.\n",
354 rc);
355 return rc;
356 }
357 }
358 return 0;
359}
360
361static void cleanup_entry_list(void)
362{
363 struct esre_entry *entry, *next;
364
365 list_for_each_entry_safe(entry, next, &entry_list, list) {
366 kobject_put(&entry->kobj);
367 }
368}
369
370static int __init esrt_sysfs_init(void)
371{
372 int error;
373
374 pr_debug("esrt-sysfs: loading.\n");
375 if (!esrt_data || !esrt_data_size)
376 return -ENOSYS;
377
378 esrt = memremap(esrt_data, esrt_data_size, MEMREMAP_WB);
379 if (!esrt) {
380 pr_err("memremap(%pa, %zu) failed.\n", &esrt_data,
381 esrt_data_size);
382 return -ENOMEM;
383 }
384
385 esrt_kobj = kobject_create_and_add("esrt", efi_kobj);
386 if (!esrt_kobj) {
387 pr_err("Firmware table registration failed.\n");
388 error = -ENOMEM;
389 goto err;
390 }
391
392 error = sysfs_create_group(esrt_kobj, &esrt_attr_group);
393 if (error) {
394 pr_err("Sysfs attribute export failed with error %d.\n",
395 error);
396 goto err_remove_esrt;
397 }
398
399 esrt_kset = kset_create_and_add("entries", NULL, esrt_kobj);
400 if (!esrt_kset) {
401 pr_err("kset creation failed.\n");
402 error = -ENOMEM;
403 goto err_remove_group;
404 }
405
406 error = register_entries();
407 if (error)
408 goto err_cleanup_list;
409
410 pr_debug("esrt-sysfs: loaded.\n");
411
412 return 0;
413err_cleanup_list:
414 cleanup_entry_list();
415 kset_unregister(esrt_kset);
416err_remove_group:
417 sysfs_remove_group(esrt_kobj, &esrt_attr_group);
418err_remove_esrt:
419 kobject_put(esrt_kobj);
420err:
421 memunmap(esrt);
422 esrt = NULL;
423 return error;
424}
425device_initcall(esrt_sysfs_init);
426
427/*
428MODULE_AUTHOR("Peter Jones <pjones@redhat.com>");
429MODULE_DESCRIPTION("EFI System Resource Table support");
430MODULE_LICENSE("GPL");
431*/