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.
11 * Data is currently found below /sys/firmware/efi/esrt/...
13 #define pr_fmt(fmt) "esrt: " fmt
15 #include <linux/capability.h>
16 #include <linux/device.h>
17 #include <linux/efi.h>
18 #include <linux/init.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>
28 #include <asm/early_ioremap.h>
30 struct efi_system_resource_entry_v1
{
34 u32 lowest_supported_fw_version
;
36 u32 last_attempt_version
;
37 u32 last_attempt_status
;
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.
49 struct efi_system_resource_table
{
50 u32 fw_resource_count
;
51 u32 fw_resource_count_max
;
52 u64 fw_resource_version
;
56 static phys_addr_t esrt_data
;
57 static size_t esrt_data_size
;
59 static struct efi_system_resource_table
*esrt
;
63 struct efi_system_resource_entry_v1
*esre1
;
67 struct list_head list
;
70 /* global list of esre_entry. */
71 static LIST_HEAD(entry_list
);
74 struct 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
);
81 static struct esre_entry
*to_entry(struct kobject
*kobj
)
83 return container_of(kobj
, struct esre_entry
, kobj
);
86 static struct esre_attribute
*to_attr(struct attribute
*attr
)
88 return container_of(attr
, struct esre_attribute
, attr
);
91 static ssize_t
esre_attr_show(struct kobject
*kobj
,
92 struct attribute
*_attr
, char *buf
)
94 struct esre_entry
*entry
= to_entry(kobj
);
95 struct esre_attribute
*attr
= to_attr(_attr
);
97 /* Don't tell normal users what firmware versions we've got... */
98 if (!capable(CAP_SYS_ADMIN
))
101 return attr
->show(entry
, buf
);
104 static const struct sysfs_ops esre_attr_ops
= {
105 .show
= esre_attr_show
,
108 /* Generic ESRT Entry ("ESRE") support. */
109 static ssize_t
fw_class_show(struct esre_entry
*entry
, char *buf
)
113 efi_guid_to_str(&entry
->esre
.esre1
->fw_class
, str
);
115 str
+= sprintf(str
, "\n");
120 static struct esre_attribute esre_fw_class
= __ATTR_RO_MODE(fw_class
, 0400);
122 #define esre_attr_decl(name, size, fmt) \
123 static ssize_t name##_show(struct esre_entry *entry, char *buf) \
125 return sprintf(buf, fmt "\n", \
126 le##size##_to_cpu(entry->esre.esre1->name)); \
129 static struct esre_attribute esre_##name = __ATTR_RO_MODE(name, 0400)
131 esre_attr_decl(fw_type
, 32, "%u");
132 esre_attr_decl(fw_version
, 32, "%u");
133 esre_attr_decl(lowest_supported_fw_version
, 32, "%u");
134 esre_attr_decl(capsule_flags
, 32, "0x%x");
135 esre_attr_decl(last_attempt_version
, 32, "%u");
136 esre_attr_decl(last_attempt_status
, 32, "%u");
138 static struct attribute
*esre1_attrs
[] = {
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
,
148 static void esre_release(struct kobject
*kobj
)
150 struct esre_entry
*entry
= to_entry(kobj
);
152 list_del(&entry
->list
);
156 static struct kobj_type esre1_ktype
= {
157 .release
= esre_release
,
158 .sysfs_ops
= &esre_attr_ops
,
159 .default_attrs
= esre1_attrs
,
163 static struct kobject
*esrt_kobj
;
164 static struct kset
*esrt_kset
;
166 static int esre_create_sysfs_entry(void *esre
, int entry_num
)
168 struct esre_entry
*entry
;
170 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
174 entry
->kobj
.kset
= esrt_kset
;
176 if (esrt
->fw_resource_version
== 1) {
179 entry
->esre
.esre1
= esre
;
180 rc
= kobject_init_and_add(&entry
->kobj
, &esre1_ktype
, NULL
,
181 "entry%d", entry_num
);
188 list_add_tail(&entry
->list
, &entry_list
);
192 /* support for displaying ESRT fields at the top level */
193 #define esrt_attr_decl(name, size, fmt) \
194 static ssize_t name##_show(struct kobject *kobj, \
195 struct kobj_attribute *attr, char *buf)\
197 return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \
200 static struct kobj_attribute esrt_##name = __ATTR_RO_MODE(name, 0400)
202 esrt_attr_decl(fw_resource_count
, 32, "%u");
203 esrt_attr_decl(fw_resource_count_max
, 32, "%u");
204 esrt_attr_decl(fw_resource_version
, 64, "%llu");
206 static struct attribute
*esrt_attrs
[] = {
207 &esrt_fw_resource_count
.attr
,
208 &esrt_fw_resource_count_max
.attr
,
209 &esrt_fw_resource_version
.attr
,
213 static inline int esrt_table_exists(void)
215 if (!efi_enabled(EFI_CONFIG_TABLES
))
217 if (efi
.esrt
== EFI_INVALID_TABLE_ADDR
)
222 static umode_t
esrt_attr_is_visible(struct kobject
*kobj
,
223 struct attribute
*attr
, int n
)
225 if (!esrt_table_exists())
230 static const struct attribute_group esrt_attr_group
= {
232 .is_visible
= esrt_attr_is_visible
,
236 * remap the table, validate it, mark it reserved and unmap it.
238 void __init
efi_esrt_init(void)
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
;
248 pr_debug("esrt-init: loading.\n");
249 if (!esrt_table_exists())
252 rc
= efi_mem_desc_lookup(efi
.esrt
, &md
);
254 pr_warn("ESRT header is not in the memory map.\n");
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
);
265 size
= sizeof(*esrt
);
269 pr_err("ESRT header doesn't fit on single memory map entry. (size: %zu max: %zu)\n",
274 va
= early_memremap(efi
.esrt
, size
);
276 pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi
.esrt
,
281 memcpy(&tmpesrt
, va
, sizeof(tmpesrt
));
282 early_memunmap(va
, size
);
284 if (tmpesrt
.fw_resource_version
== 1) {
285 entry_size
= sizeof (*v1_entries
);
287 pr_err("Unsupported ESRT version %lld.\n",
288 tmpesrt
.fw_resource_version
);
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
);
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.
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
);
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.
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",
322 size
+= entries_size
;
324 esrt_data
= (phys_addr_t
)efi
.esrt
;
325 esrt_data_size
= size
;
327 end
= esrt_data
+ size
;
328 pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data
, &end
);
329 if (md
.type
== EFI_BOOT_SERVICES_DATA
)
330 efi_mem_reserve(esrt_data
, esrt_data_size
);
332 pr_debug("esrt-init: loaded.\n");
335 static int __init
register_entries(void)
337 struct efi_system_resource_entry_v1
*v1_entries
= (void *)esrt
->entries
;
340 if (!esrt_table_exists())
343 for (i
= 0; i
< le32_to_cpu(esrt
->fw_resource_count
); i
++) {
345 if (esrt
->fw_resource_version
== 1) {
346 esre
= &v1_entries
[i
];
348 pr_err("Unsupported ESRT version %lld.\n",
349 esrt
->fw_resource_version
);
353 rc
= esre_create_sysfs_entry(esre
, i
);
355 pr_err("ESRT entry creation failed with error %d.\n",
363 static void cleanup_entry_list(void)
365 struct esre_entry
*entry
, *next
;
367 list_for_each_entry_safe(entry
, next
, &entry_list
, list
) {
368 kobject_put(&entry
->kobj
);
372 static int __init
esrt_sysfs_init(void)
376 pr_debug("esrt-sysfs: loading.\n");
377 if (!esrt_data
|| !esrt_data_size
)
380 esrt
= memremap(esrt_data
, esrt_data_size
, MEMREMAP_WB
);
382 pr_err("memremap(%pa, %zu) failed.\n", &esrt_data
,
387 esrt_kobj
= kobject_create_and_add("esrt", efi_kobj
);
389 pr_err("Firmware table registration failed.\n");
394 error
= sysfs_create_group(esrt_kobj
, &esrt_attr_group
);
396 pr_err("Sysfs attribute export failed with error %d.\n",
398 goto err_remove_esrt
;
401 esrt_kset
= kset_create_and_add("entries", NULL
, esrt_kobj
);
403 pr_err("kset creation failed.\n");
405 goto err_remove_group
;
408 error
= register_entries();
410 goto err_cleanup_list
;
412 pr_debug("esrt-sysfs: loaded.\n");
416 cleanup_entry_list();
417 kset_unregister(esrt_kset
);
419 sysfs_remove_group(esrt_kobj
, &esrt_attr_group
);
421 kobject_put(esrt_kobj
);
427 device_initcall(esrt_sysfs_init
);
430 MODULE_AUTHOR("Peter Jones <pjones@redhat.com>");
431 MODULE_DESCRIPTION("EFI System Resource Table support");
432 MODULE_LICENSE("GPL");