esrt.c 11 KB

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