blob: 22c5285f77050f27d2d96a22b5ade945bae54316 [file] [log] [blame]
Peter Jones0bb54902015-04-28 18:44:31 -04001/*
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>
Peter Jones0bb54902015-04-28 18:44:31 -040023#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{
Peter Jones0bb54902015-04-28 18:44:31 -0400169 struct esre_entry *entry;
170 char name[20];
171
172 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
173 if (!entry)
174 return -ENOMEM;
175
176 sprintf(name, "entry%d", entry_num);
177
178 entry->kobj.kset = esrt_kset;
179
180 if (esrt->fw_resource_version == 1) {
Peter Jonesf2f6b582015-04-30 10:49:20 -0400181 int rc = 0;
182
Peter Jones0bb54902015-04-28 18:44:31 -0400183 entry->esre.esre1 = esre;
184 rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
185 "%s", name);
Peter Jonesf2f6b582015-04-30 10:49:20 -0400186 if (rc) {
187 kfree(entry);
188 return rc;
189 }
Peter Jones0bb54902015-04-28 18:44:31 -0400190 }
191
192 list_add_tail(&entry->list, &entry_list);
193 return 0;
194}
195
196/* support for displaying ESRT fields at the top level */
197#define esrt_attr_decl(name, size, fmt) \
198static ssize_t esrt_##name##_show(struct kobject *kobj, \
199 struct kobj_attribute *attr, char *buf)\
200{ \
201 return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \
202} \
203\
204static struct kobj_attribute esrt_##name = __ATTR(name, 0400, \
205 esrt_##name##_show, NULL)
206
207esrt_attr_decl(fw_resource_count, 32, "%u");
208esrt_attr_decl(fw_resource_count_max, 32, "%u");
209esrt_attr_decl(fw_resource_version, 64, "%llu");
210
211static struct attribute *esrt_attrs[] = {
212 &esrt_fw_resource_count.attr,
213 &esrt_fw_resource_count_max.attr,
214 &esrt_fw_resource_version.attr,
215 NULL,
216};
217
218static inline int esrt_table_exists(void)
219{
220 if (!efi_enabled(EFI_CONFIG_TABLES))
221 return 0;
222 if (efi.esrt == EFI_INVALID_TABLE_ADDR)
223 return 0;
224 return 1;
225}
226
227static umode_t esrt_attr_is_visible(struct kobject *kobj,
228 struct attribute *attr, int n)
229{
230 if (!esrt_table_exists())
231 return 0;
232 return attr->mode;
233}
234
235static struct attribute_group esrt_attr_group = {
236 .attrs = esrt_attrs,
237 .is_visible = esrt_attr_is_visible,
238};
239
240/*
241 * remap the table, copy it to kmalloced pages, and unmap it.
242 */
243void __init efi_esrt_init(void)
244{
245 void *va;
246 struct efi_system_resource_table tmpesrt;
247 struct efi_system_resource_entry_v1 *v1_entries;
248 size_t size, max, entry_size, entries_size;
249 efi_memory_desc_t md;
250 int rc;
Peter Jonesf2f6b582015-04-30 10:49:20 -0400251 phys_addr_t end;
Peter Jones0bb54902015-04-28 18:44:31 -0400252
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 pr_err("ESRT header is not in the memory map.\n");
260 return;
261 }
262
263 max = efi_mem_desc_end(&md);
264 if (max < efi.esrt) {
265 pr_err("EFI memory descriptor is invalid. (esrt: %p max: %p)\n",
266 (void *)efi.esrt, (void *)max);
267 return;
268 }
269
270 size = sizeof(*esrt);
271 max -= efi.esrt;
272
273 if (max < size) {
274 pr_err("ESRT header doen't fit on single memory map entry. (size: %zu max: %zu)\n",
275 size, max);
276 return;
277 }
278
279 va = early_memremap(efi.esrt, size);
280 if (!va) {
281 pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
282 size);
283 return;
284 }
285
286 memcpy(&tmpesrt, va, sizeof(tmpesrt));
287
288 if (tmpesrt.fw_resource_version == 1) {
289 entry_size = sizeof (*v1_entries);
290 } else {
291 pr_err("Unsupported ESRT version %lld.\n",
292 tmpesrt.fw_resource_version);
293 return;
294 }
295
296 if (tmpesrt.fw_resource_count > 0 && max - size < entry_size) {
297 pr_err("ESRT memory map entry can only hold the header. (max: %zu size: %zu)\n",
298 max - size, entry_size);
299 goto err_memunmap;
300 }
301
302 /*
303 * The format doesn't really give us any boundary to test here,
304 * so I'm making up 128 as the max number of individually updatable
305 * components we support.
306 * 128 should be pretty excessive, but there's still some chance
307 * somebody will do that someday and we'll need to raise this.
308 */
309 if (tmpesrt.fw_resource_count > 128) {
310 pr_err("ESRT says fw_resource_count has very large value %d.\n",
311 tmpesrt.fw_resource_count);
312 goto err_memunmap;
313 }
314
315 /*
316 * We know it can't be larger than N * sizeof() here, and N is limited
317 * by the previous test to a small number, so there's no overflow.
318 */
319 entries_size = tmpesrt.fw_resource_count * entry_size;
320 if (max < size + entries_size) {
321 pr_err("ESRT does not fit on single memory map entry (size: %zu max: %zu)\n",
322 size, max);
323 goto err_memunmap;
324 }
325
326 /* remap it with our (plausible) new pages */
327 early_memunmap(va, size);
328 size += entries_size;
329 va = early_memremap(efi.esrt, size);
330 if (!va) {
331 pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
332 size);
333 return;
334 }
335
336 esrt_data = (phys_addr_t)efi.esrt;
337 esrt_data_size = size;
338
Peter Jonesf2f6b582015-04-30 10:49:20 -0400339 end = esrt_data + size;
340 pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end);
Peter Jones0bb54902015-04-28 18:44:31 -0400341 memblock_reserve(esrt_data, esrt_data_size);
342
343 pr_debug("esrt-init: loaded.\n");
344err_memunmap:
345 early_memunmap(va, size);
346}
347
348static int __init register_entries(void)
349{
350 struct efi_system_resource_entry_v1 *v1_entries = (void *)esrt->entries;
351 int i, rc;
352
353 if (!esrt_table_exists())
354 return 0;
355
356 for (i = 0; i < le32_to_cpu(esrt->fw_resource_count); i++) {
Peter Jonesf2f6b582015-04-30 10:49:20 -0400357 void *esre = NULL;
Peter Jones0bb54902015-04-28 18:44:31 -0400358 if (esrt->fw_resource_version == 1) {
Peter Jonesf2f6b582015-04-30 10:49:20 -0400359 esre = &v1_entries[i];
360 } else {
361 pr_err("Unsupported ESRT version %lld.\n",
362 esrt->fw_resource_version);
363 return -EINVAL;
Peter Jones0bb54902015-04-28 18:44:31 -0400364 }
Peter Jonesf2f6b582015-04-30 10:49:20 -0400365
366 rc = esre_create_sysfs_entry(esre, i);
Peter Jones0bb54902015-04-28 18:44:31 -0400367 if (rc < 0) {
368 pr_err("ESRT entry creation failed with error %d.\n",
369 rc);
370 return rc;
371 }
372 }
373 return 0;
374}
375
376static void cleanup_entry_list(void)
377{
378 struct esre_entry *entry, *next;
379
380 list_for_each_entry_safe(entry, next, &entry_list, list) {
381 kobject_put(&entry->kobj);
382 }
383}
384
385static int __init esrt_sysfs_init(void)
386{
387 int error;
388 struct efi_system_resource_table __iomem *ioesrt;
389
390 pr_debug("esrt-sysfs: loading.\n");
391 if (!esrt_data || !esrt_data_size)
392 return -ENOSYS;
393
394 ioesrt = ioremap(esrt_data, esrt_data_size);
395 if (!ioesrt) {
Peter Jonesf2f6b582015-04-30 10:49:20 -0400396 pr_err("ioremap(%pa, %zu) failed.\n", &esrt_data,
Peter Jones0bb54902015-04-28 18:44:31 -0400397 esrt_data_size);
398 return -ENOMEM;
399 }
400
401 esrt = kmalloc(esrt_data_size, GFP_KERNEL);
402 if (!esrt) {
403 pr_err("kmalloc failed. (wanted %zu bytes)\n", esrt_data_size);
404 iounmap(ioesrt);
405 return -ENOMEM;
406 }
407
408 memcpy_fromio(esrt, ioesrt, esrt_data_size);
409
410 esrt_kobj = kobject_create_and_add("esrt", efi_kobj);
411 if (!esrt_kobj) {
412 pr_err("Firmware table registration failed.\n");
413 error = -ENOMEM;
414 goto err;
415 }
416
417 error = sysfs_create_group(esrt_kobj, &esrt_attr_group);
418 if (error) {
419 pr_err("Sysfs attribute export failed with error %d.\n",
420 error);
421 goto err_remove_esrt;
422 }
423
424 esrt_kset = kset_create_and_add("entries", NULL, esrt_kobj);
425 if (!esrt_kset) {
426 pr_err("kset creation failed.\n");
427 error = -ENOMEM;
428 goto err_remove_group;
429 }
430
431 error = register_entries();
432 if (error)
433 goto err_cleanup_list;
434
435 memblock_remove(esrt_data, esrt_data_size);
436
437 pr_debug("esrt-sysfs: loaded.\n");
438
439 return 0;
440err_cleanup_list:
441 cleanup_entry_list();
442 kset_unregister(esrt_kset);
443err_remove_group:
444 sysfs_remove_group(esrt_kobj, &esrt_attr_group);
445err_remove_esrt:
446 kobject_put(esrt_kobj);
447err:
448 kfree(esrt);
449 esrt = NULL;
450 return error;
451}
Paul Gortmaker18aefbc2015-08-25 19:00:48 -0400452device_initcall(esrt_sysfs_init);
Peter Jones0bb54902015-04-28 18:44:31 -0400453
Paul Gortmaker18aefbc2015-08-25 19:00:48 -0400454/*
Peter Jones0bb54902015-04-28 18:44:31 -0400455MODULE_AUTHOR("Peter Jones <pjones@redhat.com>");
456MODULE_DESCRIPTION("EFI System Resource Table support");
457MODULE_LICENSE("GPL");
Paul Gortmaker18aefbc2015-08-25 19:00:48 -0400458*/