capsule.c 8.7 KB

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