]>
Commit | Line | Data |
---|---|---|
0bb54905 PJ |
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> | |
f58a37b2 | 19 | #include <linux/io.h> |
0bb54905 PJ |
20 | #include <linux/kernel.h> |
21 | #include <linux/kobject.h> | |
22 | #include <linux/list.h> | |
23 | #include <linux/memblock.h> | |
0bb54905 PJ |
24 | #include <linux/slab.h> |
25 | #include <linux/types.h> | |
26 | ||
27 | #include <asm/io.h> | |
28 | #include <asm/early_ioremap.h> | |
29 | ||
30 | struct 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 | */ | |
49 | struct 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 | ||
56 | static phys_addr_t esrt_data; | |
57 | static size_t esrt_data_size; | |
58 | ||
59 | static struct efi_system_resource_table *esrt; | |
60 | ||
61 | struct 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. */ | |
71 | static LIST_HEAD(entry_list); | |
72 | ||
73 | /* entry attribute */ | |
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); | |
79 | }; | |
80 | ||
81 | static struct esre_entry *to_entry(struct kobject *kobj) | |
82 | { | |
83 | return container_of(kobj, struct esre_entry, kobj); | |
84 | } | |
85 | ||
86 | static struct esre_attribute *to_attr(struct attribute *attr) | |
87 | { | |
88 | return container_of(attr, struct esre_attribute, attr); | |
89 | } | |
90 | ||
91 | static 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 | ||
104 | static const struct sysfs_ops esre_attr_ops = { | |
105 | .show = esre_attr_show, | |
106 | }; | |
107 | ||
108 | /* Generic ESRT Entry ("ESRE") support. */ | |
109 | static ssize_t esre_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 | ||
120 | static struct esre_attribute esre_fw_class = __ATTR(fw_class, 0400, | |
121 | esre_fw_class_show, NULL); | |
122 | ||
123 | #define esre_attr_decl(name, size, fmt) \ | |
124 | static ssize_t esre_##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 | \ | |
130 | static struct esre_attribute esre_##name = __ATTR(name, 0400, \ | |
131 | esre_##name##_show, NULL) | |
132 | ||
133 | esre_attr_decl(fw_type, 32, "%u"); | |
134 | esre_attr_decl(fw_version, 32, "%u"); | |
135 | esre_attr_decl(lowest_supported_fw_version, 32, "%u"); | |
136 | esre_attr_decl(capsule_flags, 32, "0x%x"); | |
137 | esre_attr_decl(last_attempt_version, 32, "%u"); | |
138 | esre_attr_decl(last_attempt_status, 32, "%u"); | |
139 | ||
140 | static struct attribute *esre1_attrs[] = { | |
141 | &esre_fw_class.attr, | |
142 | &esre_fw_type.attr, | |
143 | &esre_fw_version.attr, | |
144 | &esre_lowest_supported_fw_version.attr, | |
145 | &esre_capsule_flags.attr, | |
146 | &esre_last_attempt_version.attr, | |
147 | &esre_last_attempt_status.attr, | |
148 | NULL | |
149 | }; | |
150 | static void esre_release(struct kobject *kobj) | |
151 | { | |
152 | struct esre_entry *entry = to_entry(kobj); | |
153 | ||
154 | list_del(&entry->list); | |
155 | kfree(entry); | |
156 | } | |
157 | ||
158 | static struct kobj_type esre1_ktype = { | |
159 | .release = esre_release, | |
160 | .sysfs_ops = &esre_attr_ops, | |
161 | .default_attrs = esre1_attrs, | |
162 | }; | |
163 | ||
164 | ||
165 | static struct kobject *esrt_kobj; | |
166 | static struct kset *esrt_kset; | |
167 | ||
168 | static int esre_create_sysfs_entry(void *esre, int entry_num) | |
169 | { | |
0bb54905 | 170 | struct esre_entry *entry; |
0bb54905 PJ |
171 | |
172 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); | |
173 | if (!entry) | |
174 | return -ENOMEM; | |
175 | ||
0bb54905 PJ |
176 | entry->kobj.kset = esrt_kset; |
177 | ||
178 | if (esrt->fw_resource_version == 1) { | |
f2f6b587 PJ |
179 | int rc = 0; |
180 | ||
0bb54905 PJ |
181 | entry->esre.esre1 = esre; |
182 | rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL, | |
7f83773c | 183 | "entry%d", entry_num); |
f2f6b587 PJ |
184 | if (rc) { |
185 | kfree(entry); | |
186 | return rc; | |
187 | } | |
0bb54905 PJ |
188 | } |
189 | ||
190 | list_add_tail(&entry->list, &entry_list); | |
191 | return 0; | |
192 | } | |
193 | ||
194 | /* support for displaying ESRT fields at the top level */ | |
195 | #define esrt_attr_decl(name, size, fmt) \ | |
196 | static ssize_t esrt_##name##_show(struct kobject *kobj, \ | |
197 | struct kobj_attribute *attr, char *buf)\ | |
198 | { \ | |
199 | return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \ | |
200 | } \ | |
201 | \ | |
202 | static struct kobj_attribute esrt_##name = __ATTR(name, 0400, \ | |
203 | esrt_##name##_show, NULL) | |
204 | ||
205 | esrt_attr_decl(fw_resource_count, 32, "%u"); | |
206 | esrt_attr_decl(fw_resource_count_max, 32, "%u"); | |
207 | esrt_attr_decl(fw_resource_version, 64, "%llu"); | |
208 | ||
209 | static 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 | ||
216 | static 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 | ||
225 | static 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 | ||
f1a5b535 | 233 | static const struct attribute_group esrt_attr_group = { |
0bb54905 PJ |
234 | .attrs = esrt_attrs, |
235 | .is_visible = esrt_attr_is_visible, | |
236 | }; | |
237 | ||
238 | /* | |
8e80632f | 239 | * remap the table, validate it, mark it reserved and unmap it. |
0bb54905 PJ |
240 | */ |
241 | void __init efi_esrt_init(void) | |
242 | { | |
243 | void *va; | |
244 | struct efi_system_resource_table tmpesrt; | |
245 | struct efi_system_resource_entry_v1 *v1_entries; | |
246 | size_t size, max, entry_size, entries_size; | |
247 | efi_memory_desc_t md; | |
248 | int rc; | |
f2f6b587 | 249 | phys_addr_t end; |
0bb54905 PJ |
250 | |
251 | pr_debug("esrt-init: loading.\n"); | |
252 | if (!esrt_table_exists()) | |
253 | return; | |
254 | ||
255 | rc = efi_mem_desc_lookup(efi.esrt, &md); | |
256 | if (rc < 0) { | |
822f5845 | 257 | pr_warn("ESRT header is not in the memory map.\n"); |
0bb54905 PJ |
258 | return; |
259 | } | |
260 | ||
261 | max = efi_mem_desc_end(&md); | |
262 | if (max < efi.esrt) { | |
263 | pr_err("EFI memory descriptor is invalid. (esrt: %p max: %p)\n", | |
264 | (void *)efi.esrt, (void *)max); | |
265 | return; | |
266 | } | |
267 | ||
268 | size = sizeof(*esrt); | |
269 | max -= efi.esrt; | |
270 | ||
271 | if (max < size) { | |
e6688080 | 272 | pr_err("ESRT header doesn't fit on single memory map entry. (size: %zu max: %zu)\n", |
0bb54905 PJ |
273 | size, max); |
274 | return; | |
275 | } | |
276 | ||
277 | va = early_memremap(efi.esrt, size); | |
278 | if (!va) { | |
279 | pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt, | |
280 | size); | |
281 | return; | |
282 | } | |
283 | ||
284 | memcpy(&tmpesrt, va, sizeof(tmpesrt)); | |
285 | ||
286 | if (tmpesrt.fw_resource_version == 1) { | |
287 | entry_size = sizeof (*v1_entries); | |
288 | } else { | |
289 | pr_err("Unsupported ESRT version %lld.\n", | |
290 | tmpesrt.fw_resource_version); | |
291 | return; | |
292 | } | |
293 | ||
294 | if (tmpesrt.fw_resource_count > 0 && max - size < entry_size) { | |
295 | pr_err("ESRT memory map entry can only hold the header. (max: %zu size: %zu)\n", | |
296 | max - size, entry_size); | |
297 | goto err_memunmap; | |
298 | } | |
299 | ||
300 | /* | |
301 | * The format doesn't really give us any boundary to test here, | |
302 | * so I'm making up 128 as the max number of individually updatable | |
303 | * components we support. | |
304 | * 128 should be pretty excessive, but there's still some chance | |
305 | * somebody will do that someday and we'll need to raise this. | |
306 | */ | |
307 | if (tmpesrt.fw_resource_count > 128) { | |
308 | pr_err("ESRT says fw_resource_count has very large value %d.\n", | |
309 | tmpesrt.fw_resource_count); | |
310 | goto err_memunmap; | |
311 | } | |
312 | ||
313 | /* | |
314 | * We know it can't be larger than N * sizeof() here, and N is limited | |
315 | * by the previous test to a small number, so there's no overflow. | |
316 | */ | |
317 | entries_size = tmpesrt.fw_resource_count * entry_size; | |
318 | if (max < size + entries_size) { | |
319 | pr_err("ESRT does not fit on single memory map entry (size: %zu max: %zu)\n", | |
320 | size, max); | |
321 | goto err_memunmap; | |
322 | } | |
323 | ||
324 | /* remap it with our (plausible) new pages */ | |
325 | early_memunmap(va, size); | |
326 | size += entries_size; | |
327 | va = early_memremap(efi.esrt, size); | |
328 | if (!va) { | |
329 | pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt, | |
330 | size); | |
331 | return; | |
332 | } | |
333 | ||
334 | esrt_data = (phys_addr_t)efi.esrt; | |
335 | esrt_data_size = size; | |
336 | ||
f2f6b587 PJ |
337 | end = esrt_data + size; |
338 | pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end); | |
8e80632f | 339 | efi_mem_reserve(esrt_data, esrt_data_size); |
0bb54905 PJ |
340 | |
341 | pr_debug("esrt-init: loaded.\n"); | |
342 | err_memunmap: | |
343 | early_memunmap(va, size); | |
344 | } | |
345 | ||
346 | static int __init register_entries(void) | |
347 | { | |
348 | struct efi_system_resource_entry_v1 *v1_entries = (void *)esrt->entries; | |
349 | int i, rc; | |
350 | ||
351 | if (!esrt_table_exists()) | |
352 | return 0; | |
353 | ||
354 | for (i = 0; i < le32_to_cpu(esrt->fw_resource_count); i++) { | |
f2f6b587 | 355 | void *esre = NULL; |
0bb54905 | 356 | if (esrt->fw_resource_version == 1) { |
f2f6b587 PJ |
357 | esre = &v1_entries[i]; |
358 | } else { | |
359 | pr_err("Unsupported ESRT version %lld.\n", | |
360 | esrt->fw_resource_version); | |
361 | return -EINVAL; | |
0bb54905 | 362 | } |
f2f6b587 PJ |
363 | |
364 | rc = esre_create_sysfs_entry(esre, i); | |
0bb54905 PJ |
365 | if (rc < 0) { |
366 | pr_err("ESRT entry creation failed with error %d.\n", | |
367 | rc); | |
368 | return rc; | |
369 | } | |
370 | } | |
371 | return 0; | |
372 | } | |
373 | ||
374 | static void cleanup_entry_list(void) | |
375 | { | |
376 | struct esre_entry *entry, *next; | |
377 | ||
378 | list_for_each_entry_safe(entry, next, &entry_list, list) { | |
379 | kobject_put(&entry->kobj); | |
380 | } | |
381 | } | |
382 | ||
383 | static int __init esrt_sysfs_init(void) | |
384 | { | |
385 | int error; | |
0bb54905 PJ |
386 | |
387 | pr_debug("esrt-sysfs: loading.\n"); | |
388 | if (!esrt_data || !esrt_data_size) | |
389 | return -ENOSYS; | |
390 | ||
f58a37b2 | 391 | esrt = memremap(esrt_data, esrt_data_size, MEMREMAP_WB); |
8e80632f | 392 | if (!esrt) { |
f58a37b2 | 393 | pr_err("memremap(%pa, %zu) failed.\n", &esrt_data, |
0bb54905 PJ |
394 | esrt_data_size); |
395 | return -ENOMEM; | |
396 | } | |
397 | ||
0bb54905 PJ |
398 | esrt_kobj = kobject_create_and_add("esrt", efi_kobj); |
399 | if (!esrt_kobj) { | |
400 | pr_err("Firmware table registration failed.\n"); | |
401 | error = -ENOMEM; | |
402 | goto err; | |
403 | } | |
404 | ||
405 | error = sysfs_create_group(esrt_kobj, &esrt_attr_group); | |
406 | if (error) { | |
407 | pr_err("Sysfs attribute export failed with error %d.\n", | |
408 | error); | |
409 | goto err_remove_esrt; | |
410 | } | |
411 | ||
412 | esrt_kset = kset_create_and_add("entries", NULL, esrt_kobj); | |
413 | if (!esrt_kset) { | |
414 | pr_err("kset creation failed.\n"); | |
415 | error = -ENOMEM; | |
416 | goto err_remove_group; | |
417 | } | |
418 | ||
419 | error = register_entries(); | |
420 | if (error) | |
421 | goto err_cleanup_list; | |
422 | ||
0bb54905 PJ |
423 | pr_debug("esrt-sysfs: loaded.\n"); |
424 | ||
425 | return 0; | |
426 | err_cleanup_list: | |
427 | cleanup_entry_list(); | |
428 | kset_unregister(esrt_kset); | |
429 | err_remove_group: | |
430 | sysfs_remove_group(esrt_kobj, &esrt_attr_group); | |
431 | err_remove_esrt: | |
432 | kobject_put(esrt_kobj); | |
433 | err: | |
434 | kfree(esrt); | |
435 | esrt = NULL; | |
436 | return error; | |
437 | } | |
18aefbc5 | 438 | device_initcall(esrt_sysfs_init); |
0bb54905 | 439 | |
18aefbc5 | 440 | /* |
0bb54905 PJ |
441 | MODULE_AUTHOR("Peter Jones <pjones@redhat.com>"); |
442 | MODULE_DESCRIPTION("EFI System Resource Table support"); | |
443 | MODULE_LICENSE("GPL"); | |
18aefbc5 | 444 | */ |