esrt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. #include <linux/capability.h>
  16. #include <linux/device.h>
  17. #include <linux/efi.h>
  18. #include <linux/init.h>
  19. #include <linux/io.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>
  26. #include <asm/io.h>
  27. #include <asm/early_ioremap.h>
  28. struct efi_system_resource_entry_v1 {
  29. efi_guid_t fw_class;
  30. u32 fw_type;
  31. u32 fw_version;
  32. u32 lowest_supported_fw_version;
  33. u32 capsule_flags;
  34. u32 last_attempt_version;
  35. u32 last_attempt_status;
  36. };
  37. /*
  38. * _count and _version are what they seem like. _max is actually just
  39. * accounting info for the firmware when creating the table; it should never
  40. * have been exposed to us. To wit, the spec says:
  41. * The maximum number of resource array entries that can be within the
  42. * table without reallocating the table, must not be zero.
  43. * Since there's no guidance about what that means in terms of memory layout,
  44. * it means nothing to us.
  45. */
  46. struct efi_system_resource_table {
  47. u32 fw_resource_count;
  48. u32 fw_resource_count_max;
  49. u64 fw_resource_version;
  50. u8 entries[];
  51. };
  52. static phys_addr_t esrt_data;
  53. static size_t esrt_data_size;
  54. static struct efi_system_resource_table *esrt;
  55. struct esre_entry {
  56. union {
  57. struct efi_system_resource_entry_v1 *esre1;
  58. } esre;
  59. struct kobject kobj;
  60. struct list_head list;
  61. };
  62. /* global list of esre_entry. */
  63. static LIST_HEAD(entry_list);
  64. /* entry attribute */
  65. struct esre_attribute {
  66. struct attribute attr;
  67. ssize_t (*show)(struct esre_entry *entry, char *buf);
  68. ssize_t (*store)(struct esre_entry *entry,
  69. const char *buf, size_t count);
  70. };
  71. static struct esre_entry *to_entry(struct kobject *kobj)
  72. {
  73. return container_of(kobj, struct esre_entry, kobj);
  74. }
  75. static struct esre_attribute *to_attr(struct attribute *attr)
  76. {
  77. return container_of(attr, struct esre_attribute, attr);
  78. }
  79. static ssize_t esre_attr_show(struct kobject *kobj,
  80. struct attribute *_attr, char *buf)
  81. {
  82. struct esre_entry *entry = to_entry(kobj);
  83. struct esre_attribute *attr = to_attr(_attr);
  84. return attr->show(entry, buf);
  85. }
  86. static const struct sysfs_ops esre_attr_ops = {
  87. .show = esre_attr_show,
  88. };
  89. /* Generic ESRT Entry ("ESRE") support. */
  90. static ssize_t fw_class_show(struct esre_entry *entry, char *buf)
  91. {
  92. char *str = buf;
  93. efi_guid_to_str(&entry->esre.esre1->fw_class, str);
  94. str += strlen(str);
  95. str += sprintf(str, "\n");
  96. return str - buf;
  97. }
  98. static struct esre_attribute esre_fw_class = __ATTR_RO_MODE(fw_class, 0400);
  99. #define esre_attr_decl(name, size, fmt) \
  100. static ssize_t name##_show(struct esre_entry *entry, char *buf) \
  101. { \
  102. return sprintf(buf, fmt "\n", \
  103. le##size##_to_cpu(entry->esre.esre1->name)); \
  104. } \
  105. \
  106. static struct esre_attribute esre_##name = __ATTR_RO_MODE(name, 0400)
  107. esre_attr_decl(fw_type, 32, "%u");
  108. esre_attr_decl(fw_version, 32, "%u");
  109. esre_attr_decl(lowest_supported_fw_version, 32, "%u");
  110. esre_attr_decl(capsule_flags, 32, "0x%x");
  111. esre_attr_decl(last_attempt_version, 32, "%u");
  112. esre_attr_decl(last_attempt_status, 32, "%u");
  113. static struct attribute *esre1_attrs[] = {
  114. &esre_fw_class.attr,
  115. &esre_fw_type.attr,
  116. &esre_fw_version.attr,
  117. &esre_lowest_supported_fw_version.attr,
  118. &esre_capsule_flags.attr,
  119. &esre_last_attempt_version.attr,
  120. &esre_last_attempt_status.attr,
  121. NULL
  122. };
  123. ATTRIBUTE_GROUPS(esre1);
  124. static void esre_release(struct kobject *kobj)
  125. {
  126. struct esre_entry *entry = to_entry(kobj);
  127. list_del(&entry->list);
  128. kfree(entry);
  129. }
  130. static const struct kobj_type esre1_ktype = {
  131. .release = esre_release,
  132. .sysfs_ops = &esre_attr_ops,
  133. .default_groups = esre1_groups,
  134. };
  135. static struct kobject *esrt_kobj;
  136. static struct kset *esrt_kset;
  137. static int esre_create_sysfs_entry(void *esre, int entry_num)
  138. {
  139. struct esre_entry *entry;
  140. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  141. if (!entry)
  142. return -ENOMEM;
  143. entry->kobj.kset = esrt_kset;
  144. if (esrt->fw_resource_version == 1) {
  145. int rc = 0;
  146. entry->esre.esre1 = esre;
  147. rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
  148. "entry%d", entry_num);
  149. if (rc) {
  150. kobject_put(&entry->kobj);
  151. return rc;
  152. }
  153. }
  154. list_add_tail(&entry->list, &entry_list);
  155. return 0;
  156. }
  157. /* support for displaying ESRT fields at the top level */
  158. #define esrt_attr_decl(name, size, fmt) \
  159. static ssize_t name##_show(struct kobject *kobj, \
  160. struct kobj_attribute *attr, char *buf)\
  161. { \
  162. return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \
  163. } \
  164. \
  165. static struct kobj_attribute esrt_##name = __ATTR_RO_MODE(name, 0400)
  166. esrt_attr_decl(fw_resource_count, 32, "%u");
  167. esrt_attr_decl(fw_resource_count_max, 32, "%u");
  168. esrt_attr_decl(fw_resource_version, 64, "%llu");
  169. static struct attribute *esrt_attrs[] = {
  170. &esrt_fw_resource_count.attr,
  171. &esrt_fw_resource_count_max.attr,
  172. &esrt_fw_resource_version.attr,
  173. NULL,
  174. };
  175. static inline int esrt_table_exists(void)
  176. {
  177. if (!efi_enabled(EFI_CONFIG_TABLES))
  178. return 0;
  179. if (efi.esrt == EFI_INVALID_TABLE_ADDR)
  180. return 0;
  181. return 1;
  182. }
  183. static umode_t esrt_attr_is_visible(struct kobject *kobj,
  184. struct attribute *attr, int n)
  185. {
  186. if (!esrt_table_exists())
  187. return 0;
  188. return attr->mode;
  189. }
  190. static const struct attribute_group esrt_attr_group = {
  191. .attrs = esrt_attrs,
  192. .is_visible = esrt_attr_is_visible,
  193. };
  194. /*
  195. * remap the table, validate it, mark it reserved and unmap it.
  196. */
  197. void __init efi_esrt_init(void)
  198. {
  199. void *va;
  200. struct efi_system_resource_table tmpesrt;
  201. size_t size, max, entry_size, entries_size;
  202. efi_memory_desc_t md;
  203. int rc;
  204. phys_addr_t end;
  205. if (!efi_enabled(EFI_MEMMAP) && !efi_enabled(EFI_PARAVIRT))
  206. return;
  207. pr_debug("esrt-init: loading.\n");
  208. if (!esrt_table_exists())
  209. return;
  210. rc = efi_mem_desc_lookup(efi.esrt, &md);
  211. if (rc < 0 ||
  212. (!(md.attribute & EFI_MEMORY_RUNTIME) &&
  213. md.type != EFI_BOOT_SERVICES_DATA &&
  214. md.type != EFI_RUNTIME_SERVICES_DATA &&
  215. md.type != EFI_ACPI_RECLAIM_MEMORY &&
  216. md.type != EFI_ACPI_MEMORY_NVS)) {
  217. pr_warn("ESRT header is not in the memory map.\n");
  218. return;
  219. }
  220. max = efi_mem_desc_end(&md) - efi.esrt;
  221. size = sizeof(*esrt);
  222. if (max < size) {
  223. pr_err("ESRT header doesn't fit on single memory map entry. (size: %zu max: %zu)\n",
  224. size, max);
  225. return;
  226. }
  227. va = early_memremap(efi.esrt, size);
  228. if (!va) {
  229. pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
  230. size);
  231. return;
  232. }
  233. memcpy(&tmpesrt, va, sizeof(tmpesrt));
  234. early_memunmap(va, size);
  235. if (tmpesrt.fw_resource_version != 1) {
  236. pr_err("Unsupported ESRT version %lld.\n",
  237. tmpesrt.fw_resource_version);
  238. return;
  239. }
  240. entry_size = sizeof(struct efi_system_resource_entry_v1);
  241. if (tmpesrt.fw_resource_count > 0 && max - size < entry_size) {
  242. pr_err("ESRT memory map entry can only hold the header. (max: %zu size: %zu)\n",
  243. max - size, entry_size);
  244. return;
  245. }
  246. /*
  247. * The format doesn't really give us any boundary to test here,
  248. * so I'm making up 128 as the max number of individually updatable
  249. * components we support.
  250. * 128 should be pretty excessive, but there's still some chance
  251. * somebody will do that someday and we'll need to raise this.
  252. */
  253. if (tmpesrt.fw_resource_count > 128) {
  254. pr_err("ESRT says fw_resource_count has very large value %d.\n",
  255. tmpesrt.fw_resource_count);
  256. return;
  257. }
  258. /*
  259. * We know it can't be larger than N * sizeof() here, and N is limited
  260. * by the previous test to a small number, so there's no overflow.
  261. */
  262. entries_size = tmpesrt.fw_resource_count * entry_size;
  263. if (max < size + entries_size) {
  264. pr_err("ESRT does not fit on single memory map entry (size: %zu max: %zu)\n",
  265. size, max);
  266. return;
  267. }
  268. size += entries_size;
  269. esrt_data = (phys_addr_t)efi.esrt;
  270. esrt_data_size = size;
  271. end = esrt_data + size;
  272. pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end);
  273. if (md.type == EFI_BOOT_SERVICES_DATA)
  274. efi_mem_reserve(esrt_data, esrt_data_size);
  275. pr_debug("esrt-init: loaded.\n");
  276. }
  277. static int __init register_entries(void)
  278. {
  279. struct efi_system_resource_entry_v1 *v1_entries = (void *)esrt->entries;
  280. int i, rc;
  281. if (!esrt_table_exists())
  282. return 0;
  283. for (i = 0; i < le32_to_cpu(esrt->fw_resource_count); i++) {
  284. void *esre = NULL;
  285. if (esrt->fw_resource_version == 1) {
  286. esre = &v1_entries[i];
  287. } else {
  288. pr_err("Unsupported ESRT version %lld.\n",
  289. esrt->fw_resource_version);
  290. return -EINVAL;
  291. }
  292. rc = esre_create_sysfs_entry(esre, i);
  293. if (rc < 0) {
  294. pr_err("ESRT entry creation failed with error %d.\n",
  295. rc);
  296. return rc;
  297. }
  298. }
  299. return 0;
  300. }
  301. static void cleanup_entry_list(void)
  302. {
  303. struct esre_entry *entry, *next;
  304. list_for_each_entry_safe(entry, next, &entry_list, list) {
  305. kobject_put(&entry->kobj);
  306. }
  307. }
  308. static int __init esrt_sysfs_init(void)
  309. {
  310. int error;
  311. pr_debug("esrt-sysfs: loading.\n");
  312. if (!esrt_data || !esrt_data_size)
  313. return -ENOSYS;
  314. esrt = memremap(esrt_data, esrt_data_size, MEMREMAP_WB);
  315. if (!esrt) {
  316. pr_err("memremap(%pa, %zu) failed.\n", &esrt_data,
  317. esrt_data_size);
  318. return -ENOMEM;
  319. }
  320. esrt_kobj = kobject_create_and_add("esrt", efi_kobj);
  321. if (!esrt_kobj) {
  322. pr_err("Firmware table registration failed.\n");
  323. error = -ENOMEM;
  324. goto err;
  325. }
  326. error = sysfs_create_group(esrt_kobj, &esrt_attr_group);
  327. if (error) {
  328. pr_err("Sysfs attribute export failed with error %d.\n",
  329. error);
  330. goto err_remove_esrt;
  331. }
  332. esrt_kset = kset_create_and_add("entries", NULL, esrt_kobj);
  333. if (!esrt_kset) {
  334. pr_err("kset creation failed.\n");
  335. error = -ENOMEM;
  336. goto err_remove_group;
  337. }
  338. error = register_entries();
  339. if (error)
  340. goto err_cleanup_list;
  341. pr_debug("esrt-sysfs: loaded.\n");
  342. return 0;
  343. err_cleanup_list:
  344. cleanup_entry_list();
  345. kset_unregister(esrt_kset);
  346. err_remove_group:
  347. sysfs_remove_group(esrt_kobj, &esrt_attr_group);
  348. err_remove_esrt:
  349. kobject_put(esrt_kobj);
  350. err:
  351. memunmap(esrt);
  352. esrt = NULL;
  353. return error;
  354. }
  355. device_initcall(esrt_sysfs_init);
  356. /*
  357. MODULE_AUTHOR("Peter Jones <pjones@redhat.com>");
  358. MODULE_DESCRIPTION("EFI System Resource Table support");
  359. MODULE_LICENSE("GPL");
  360. */