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>
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>
27 #include <asm/early_ioremap.h>
29 struct efi_system_resource_entry_v1 {
33 u32 lowest_supported_fw_version;
35 u32 last_attempt_version;
36 u32 last_attempt_status;
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.
48 struct efi_system_resource_table {
49 u32 fw_resource_count;
50 u32 fw_resource_count_max;
51 u64 fw_resource_version;
55 static phys_addr_t esrt_data;
56 static size_t esrt_data_size;
58 static struct efi_system_resource_table *esrt;
62 struct efi_system_resource_entry_v1 *esre1;
66 struct list_head list;
69 /* global list of esre_entry. */
70 static LIST_HEAD(entry_list);
73 struct 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);
80 static struct esre_entry *to_entry(struct kobject *kobj)
82 return container_of(kobj, struct esre_entry, kobj);
85 static struct esre_attribute *to_attr(struct attribute *attr)
87 return container_of(attr, struct esre_attribute, attr);
90 static ssize_t esre_attr_show(struct kobject *kobj,
91 struct attribute *_attr, char *buf)
93 struct esre_entry *entry = to_entry(kobj);
94 struct esre_attribute *attr = to_attr(_attr);
96 /* Don't tell normal users what firmware versions we've got... */
97 if (!capable(CAP_SYS_ADMIN))
100 return attr->show(entry, buf);
103 static const struct sysfs_ops esre_attr_ops = {
104 .show = esre_attr_show,
107 /* Generic ESRT Entry ("ESRE") support. */
108 static ssize_t esre_fw_class_show(struct esre_entry *entry, char *buf)
112 efi_guid_to_str(&entry->esre.esre1->fw_class, str);
114 str += sprintf(str, "\n");
119 static struct esre_attribute esre_fw_class = __ATTR(fw_class, 0400,
120 esre_fw_class_show, NULL);
122 #define esre_attr_decl(name, size, fmt) \
123 static ssize_t esre_##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(name, 0400, \
130 esre_##name##_show, NULL)
132 esre_attr_decl(fw_type, 32, "%u");
133 esre_attr_decl(fw_version, 32, "%u");
134 esre_attr_decl(lowest_supported_fw_version, 32, "%u");
135 esre_attr_decl(capsule_flags, 32, "0x%x");
136 esre_attr_decl(last_attempt_version, 32, "%u");
137 esre_attr_decl(last_attempt_status, 32, "%u");
139 static struct attribute *esre1_attrs[] = {
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,
149 static void esre_release(struct kobject *kobj)
151 struct esre_entry *entry = to_entry(kobj);
153 list_del(&entry->list);
157 static struct kobj_type esre1_ktype = {
158 .release = esre_release,
159 .sysfs_ops = &esre_attr_ops,
160 .default_attrs = esre1_attrs,
164 static struct kobject *esrt_kobj;
165 static struct kset *esrt_kset;
167 static int esre_create_sysfs_entry(void *esre, int entry_num)
169 struct esre_entry *entry;
171 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
175 entry->kobj.kset = esrt_kset;
177 if (esrt->fw_resource_version == 1) {
180 entry->esre.esre1 = esre;
181 rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
182 "entry%d", entry_num);
189 list_add_tail(&entry->list, &entry_list);
193 /* support for displaying ESRT fields at the top level */
194 #define esrt_attr_decl(name, size, fmt) \
195 static ssize_t esrt_##name##_show(struct kobject *kobj, \
196 struct kobj_attribute *attr, char *buf)\
198 return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \
201 static struct kobj_attribute esrt_##name = __ATTR(name, 0400, \
202 esrt_##name##_show, NULL)
204 esrt_attr_decl(fw_resource_count, 32, "%u");
205 esrt_attr_decl(fw_resource_count_max, 32, "%u");
206 esrt_attr_decl(fw_resource_version, 64, "%llu");
208 static struct attribute *esrt_attrs[] = {
209 &esrt_fw_resource_count.attr,
210 &esrt_fw_resource_count_max.attr,
211 &esrt_fw_resource_version.attr,
215 static inline int esrt_table_exists(void)
217 if (!efi_enabled(EFI_CONFIG_TABLES))
219 if (efi.esrt == EFI_INVALID_TABLE_ADDR)
224 static umode_t esrt_attr_is_visible(struct kobject *kobj,
225 struct attribute *attr, int n)
227 if (!esrt_table_exists())
232 static struct attribute_group esrt_attr_group = {
234 .is_visible = esrt_attr_is_visible,
238 * remap the table, copy it to kmalloced pages, and unmap it.
240 void __init efi_esrt_init(void)
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;
250 pr_debug("esrt-init: loading.\n");
251 if (!esrt_table_exists())
254 rc = efi_mem_desc_lookup(efi.esrt, &md);
256 pr_err("ESRT header is not in the memory map.\n");
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);
267 size = sizeof(*esrt);
271 pr_err("ESRT header doen't fit on single memory map entry. (size: %zu max: %zu)\n",
276 va = early_memremap(efi.esrt, size);
278 pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
283 memcpy(&tmpesrt, va, sizeof(tmpesrt));
285 if (tmpesrt.fw_resource_version == 1) {
286 entry_size = sizeof (*v1_entries);
288 pr_err("Unsupported ESRT version %lld.\n",
289 tmpesrt.fw_resource_version);
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);
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.
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);
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.
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",
323 /* remap it with our (plausible) new pages */
324 early_memunmap(va, size);
325 size += entries_size;
326 va = early_memremap(efi.esrt, size);
328 pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
333 esrt_data = (phys_addr_t)efi.esrt;
334 esrt_data_size = size;
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);
340 pr_debug("esrt-init: loaded.\n");
342 early_memunmap(va, size);
345 static int __init register_entries(void)
347 struct efi_system_resource_entry_v1 *v1_entries = (void *)esrt->entries;
350 if (!esrt_table_exists())
353 for (i = 0; i < le32_to_cpu(esrt->fw_resource_count); i++) {
355 if (esrt->fw_resource_version == 1) {
356 esre = &v1_entries[i];
358 pr_err("Unsupported ESRT version %lld.\n",
359 esrt->fw_resource_version);
363 rc = esre_create_sysfs_entry(esre, i);
365 pr_err("ESRT entry creation failed with error %d.\n",
373 static void cleanup_entry_list(void)
375 struct esre_entry *entry, *next;
377 list_for_each_entry_safe(entry, next, &entry_list, list) {
378 kobject_put(&entry->kobj);
382 static int __init esrt_sysfs_init(void)
385 struct efi_system_resource_table __iomem *ioesrt;
387 pr_debug("esrt-sysfs: loading.\n");
388 if (!esrt_data || !esrt_data_size)
391 ioesrt = ioremap(esrt_data, esrt_data_size);
393 pr_err("ioremap(%pa, %zu) failed.\n", &esrt_data,
398 esrt = kmalloc(esrt_data_size, GFP_KERNEL);
400 pr_err("kmalloc failed. (wanted %zu bytes)\n", esrt_data_size);
405 memcpy_fromio(esrt, ioesrt, esrt_data_size);
407 esrt_kobj = kobject_create_and_add("esrt", efi_kobj);
409 pr_err("Firmware table registration failed.\n");
414 error = sysfs_create_group(esrt_kobj, &esrt_attr_group);
416 pr_err("Sysfs attribute export failed with error %d.\n",
418 goto err_remove_esrt;
421 esrt_kset = kset_create_and_add("entries", NULL, esrt_kobj);
423 pr_err("kset creation failed.\n");
425 goto err_remove_group;
428 error = register_entries();
430 goto err_cleanup_list;
432 memblock_remove(esrt_data, esrt_data_size);
434 pr_debug("esrt-sysfs: loaded.\n");
438 cleanup_entry_list();
439 kset_unregister(esrt_kset);
441 sysfs_remove_group(esrt_kobj, &esrt_attr_group);
443 kobject_put(esrt_kobj);
449 device_initcall(esrt_sysfs_init);
452 MODULE_AUTHOR("Peter Jones <pjones@redhat.com>");
453 MODULE_DESCRIPTION("EFI System Resource Table support");
454 MODULE_LICENSE("GPL");