capsule.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * EFI capsule support.
  4. *
  5. * Copyright 2013 Intel Corporation; author Matt Fleming
  6. */
  7. #define pr_fmt(fmt) "efi: " fmt
  8. #include <linux/slab.h>
  9. #include <linux/mutex.h>
  10. #include <linux/highmem.h>
  11. #include <linux/efi.h>
  12. #include <linux/vmalloc.h>
  13. #include <asm/efi.h>
  14. #include <asm/io.h>
  15. typedef struct {
  16. u64 length;
  17. u64 data;
  18. } efi_capsule_block_desc_t;
  19. static bool capsule_pending;
  20. static bool stop_capsules;
  21. static int efi_reset_type = -1;
  22. /*
  23. * capsule_mutex serialises access to both capsule_pending and
  24. * efi_reset_type and stop_capsules.
  25. */
  26. static DEFINE_MUTEX(capsule_mutex);
  27. /**
  28. * efi_capsule_pending - has a capsule been passed to the firmware?
  29. * @reset_type: store the type of EFI reset if capsule is pending
  30. *
  31. * To ensure that the registered capsule is processed correctly by the
  32. * firmware we need to perform a specific type of reset. If a capsule is
  33. * pending return the reset type in @reset_type.
  34. *
  35. * This function will race with callers of efi_capsule_update(), for
  36. * example, calling this function while somebody else is in
  37. * efi_capsule_update() but hasn't reached efi_capsue_update_locked()
  38. * will miss the updates to capsule_pending and efi_reset_type after
  39. * efi_capsule_update_locked() completes.
  40. *
  41. * A non-racy use is from platform reboot code because we use
  42. * system_state to ensure no capsules can be sent to the firmware once
  43. * we're at SYSTEM_RESTART. See efi_capsule_update_locked().
  44. */
  45. bool efi_capsule_pending(int *reset_type)
  46. {
  47. if (!capsule_pending)
  48. return false;
  49. if (reset_type)
  50. *reset_type = efi_reset_type;
  51. return true;
  52. }
  53. /*
  54. * Whitelist of EFI capsule flags that we support.
  55. *
  56. * We do not handle EFI_CAPSULE_INITIATE_RESET because that would
  57. * require us to prepare the kernel for reboot. Refuse to load any
  58. * capsules with that flag and any other flags that we do not know how
  59. * to handle.
  60. */
  61. #define EFI_CAPSULE_SUPPORTED_FLAG_MASK \
  62. (EFI_CAPSULE_PERSIST_ACROSS_RESET | EFI_CAPSULE_POPULATE_SYSTEM_TABLE)
  63. /**
  64. * efi_capsule_supported - does the firmware support the capsule?
  65. * @guid: vendor guid of capsule
  66. * @flags: capsule flags
  67. * @size: size of capsule data
  68. * @reset: the reset type required for this capsule
  69. *
  70. * Check whether a capsule with @flags is supported by the firmware
  71. * and that @size doesn't exceed the maximum size for a capsule.
  72. *
  73. * No attempt is made to check @reset against the reset type required
  74. * by any pending capsules because of the races involved.
  75. */
  76. int efi_capsule_supported(efi_guid_t guid, u32 flags, size_t size, int *reset)
  77. {
  78. efi_capsule_header_t capsule;
  79. efi_capsule_header_t *cap_list[] = { &capsule };
  80. efi_status_t status;
  81. u64 max_size;
  82. if (flags & ~EFI_CAPSULE_SUPPORTED_FLAG_MASK)
  83. return -EINVAL;
  84. capsule.headersize = capsule.imagesize = sizeof(capsule);
  85. memcpy(&capsule.guid, &guid, sizeof(efi_guid_t));
  86. capsule.flags = flags;
  87. status = efi.query_capsule_caps(cap_list, 1, &max_size, reset);
  88. if (status != EFI_SUCCESS)
  89. return efi_status_to_err(status);
  90. if (size > max_size)
  91. return -ENOSPC;
  92. return 0;
  93. }
  94. EXPORT_SYMBOL_GPL(efi_capsule_supported);
  95. /*
  96. * Every scatter gather list (block descriptor) page must end with a
  97. * continuation pointer. The last continuation pointer of the last
  98. * page must be zero to mark the end of the chain.
  99. */
  100. #define SGLIST_PER_PAGE ((PAGE_SIZE / sizeof(efi_capsule_block_desc_t)) - 1)
  101. /*
  102. * How many scatter gather list (block descriptor) pages do we need
  103. * to map @count pages?
  104. */
  105. static inline unsigned int sg_pages_num(unsigned int count)
  106. {
  107. return DIV_ROUND_UP(count, SGLIST_PER_PAGE);
  108. }
  109. /**
  110. * efi_capsule_update_locked - pass a single capsule to the firmware
  111. * @capsule: capsule to send to the firmware
  112. * @sg_pages: array of scatter gather (block descriptor) pages
  113. * @reset: the reset type required for @capsule
  114. *
  115. * Since this function must be called under capsule_mutex check
  116. * whether efi_reset_type will conflict with @reset, and atomically
  117. * set it and capsule_pending if a capsule was successfully sent to
  118. * the firmware.
  119. *
  120. * We also check to see if the system is about to restart, and if so,
  121. * abort. This avoids races between efi_capsule_update() and
  122. * efi_capsule_pending().
  123. */
  124. static int
  125. efi_capsule_update_locked(efi_capsule_header_t *capsule,
  126. struct page **sg_pages, int reset)
  127. {
  128. efi_physical_addr_t sglist_phys;
  129. efi_status_t status;
  130. lockdep_assert_held(&capsule_mutex);
  131. /*
  132. * If someone has already registered a capsule that requires a
  133. * different reset type, we're out of luck and must abort.
  134. */
  135. if (efi_reset_type >= 0 && efi_reset_type != reset) {
  136. pr_err("Conflicting capsule reset type %d (%d).\n",
  137. reset, efi_reset_type);
  138. return -EINVAL;
  139. }
  140. /*
  141. * If the system is getting ready to restart it may have
  142. * called efi_capsule_pending() to make decisions (such as
  143. * whether to force an EFI reboot), and we're racing against
  144. * that call. Abort in that case.
  145. */
  146. if (unlikely(stop_capsules)) {
  147. pr_warn("Capsule update raced with reboot, aborting.\n");
  148. return -EINVAL;
  149. }
  150. sglist_phys = page_to_phys(sg_pages[0]);
  151. status = efi.update_capsule(&capsule, 1, sglist_phys);
  152. if (status == EFI_SUCCESS) {
  153. capsule_pending = true;
  154. efi_reset_type = reset;
  155. }
  156. return efi_status_to_err(status);
  157. }
  158. /**
  159. * efi_capsule_update - send a capsule to the firmware
  160. * @capsule: capsule to send to firmware
  161. * @pages: an array of capsule data pages
  162. *
  163. * Build a scatter gather list with EFI capsule block descriptors to
  164. * map the capsule described by @capsule with its data in @pages and
  165. * send it to the firmware via the UpdateCapsule() runtime service.
  166. *
  167. * @capsule must be a virtual mapping of the complete capsule update in the
  168. * kernel address space, as the capsule can be consumed immediately.
  169. * A capsule_header_t that describes the entire contents of the capsule
  170. * must be at the start of the first data page.
  171. *
  172. * Even though this function will validate that the firmware supports
  173. * the capsule guid, users will likely want to check that
  174. * efi_capsule_supported() returns true before calling this function
  175. * because it makes it easier to print helpful error messages.
  176. *
  177. * If the capsule is successfully submitted to the firmware, any
  178. * subsequent calls to efi_capsule_pending() will return true. @pages
  179. * must not be released or modified if this function returns
  180. * successfully.
  181. *
  182. * Callers must be prepared for this function to fail, which can
  183. * happen if we raced with system reboot or if there is already a
  184. * pending capsule that has a reset type that conflicts with the one
  185. * required by @capsule. Do NOT use efi_capsule_pending() to detect
  186. * this conflict since that would be racy. Instead, submit the capsule
  187. * to efi_capsule_update() and check the return value.
  188. *
  189. * Return 0 on success, a converted EFI status code on failure.
  190. */
  191. int efi_capsule_update(efi_capsule_header_t *capsule, phys_addr_t *pages)
  192. {
  193. u32 imagesize = capsule->imagesize;
  194. efi_guid_t guid = capsule->guid;
  195. unsigned int count, sg_count;
  196. u32 flags = capsule->flags;
  197. struct page **sg_pages;
  198. int rv, reset_type;
  199. int i, j;
  200. rv = efi_capsule_supported(guid, flags, imagesize, &reset_type);
  201. if (rv)
  202. return rv;
  203. count = DIV_ROUND_UP(imagesize, PAGE_SIZE);
  204. sg_count = sg_pages_num(count);
  205. sg_pages = kcalloc(sg_count, sizeof(*sg_pages), GFP_KERNEL);
  206. if (!sg_pages)
  207. return -ENOMEM;
  208. for (i = 0; i < sg_count; i++) {
  209. sg_pages[i] = alloc_page(GFP_KERNEL);
  210. if (!sg_pages[i]) {
  211. rv = -ENOMEM;
  212. goto out;
  213. }
  214. }
  215. for (i = 0; i < sg_count; i++) {
  216. efi_capsule_block_desc_t *sglist;
  217. sglist = kmap_atomic(sg_pages[i]);
  218. for (j = 0; j < SGLIST_PER_PAGE && count > 0; j++) {
  219. u64 sz = min_t(u64, imagesize,
  220. PAGE_SIZE - (u64)*pages % PAGE_SIZE);
  221. sglist[j].length = sz;
  222. sglist[j].data = *pages++;
  223. imagesize -= sz;
  224. count--;
  225. }
  226. /* Continuation pointer */
  227. sglist[j].length = 0;
  228. if (i + 1 == sg_count)
  229. sglist[j].data = 0;
  230. else
  231. sglist[j].data = page_to_phys(sg_pages[i + 1]);
  232. #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
  233. /*
  234. * At runtime, the firmware has no way to find out where the
  235. * sglist elements are mapped, if they are mapped in the first
  236. * place. Therefore, on architectures that can only perform
  237. * cache maintenance by virtual address, the firmware is unable
  238. * to perform this maintenance, and so it is up to the OS to do
  239. * it instead.
  240. */
  241. efi_capsule_flush_cache_range(sglist, PAGE_SIZE);
  242. #endif
  243. kunmap_atomic(sglist);
  244. }
  245. mutex_lock(&capsule_mutex);
  246. rv = efi_capsule_update_locked(capsule, sg_pages, reset_type);
  247. mutex_unlock(&capsule_mutex);
  248. out:
  249. for (i = 0; rv && i < sg_count; i++) {
  250. if (sg_pages[i])
  251. __free_page(sg_pages[i]);
  252. }
  253. kfree(sg_pages);
  254. return rv;
  255. }
  256. EXPORT_SYMBOL_GPL(efi_capsule_update);
  257. static int capsule_reboot_notify(struct notifier_block *nb, unsigned long event, void *cmd)
  258. {
  259. mutex_lock(&capsule_mutex);
  260. stop_capsules = true;
  261. mutex_unlock(&capsule_mutex);
  262. return NOTIFY_DONE;
  263. }
  264. static struct notifier_block capsule_reboot_nb = {
  265. .notifier_call = capsule_reboot_notify,
  266. };
  267. static int __init capsule_reboot_register(void)
  268. {
  269. return register_reboot_notifier(&capsule_reboot_nb);
  270. }
  271. core_initcall(capsule_reboot_register);