capsule-loader.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * EFI capsule loader driver.
  4. *
  5. * Copyright 2015 Intel Corporation
  6. */
  7. #define pr_fmt(fmt) "efi: " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/miscdevice.h>
  11. #include <linux/highmem.h>
  12. #include <linux/io.h>
  13. #include <linux/slab.h>
  14. #include <linux/mutex.h>
  15. #include <linux/efi.h>
  16. #include <linux/vmalloc.h>
  17. #define NO_FURTHER_WRITE_ACTION -1
  18. /**
  19. * efi_free_all_buff_pages - free all previous allocated buffer pages
  20. * @cap_info: pointer to current instance of capsule_info structure
  21. *
  22. * In addition to freeing buffer pages, it flags NO_FURTHER_WRITE_ACTION
  23. * to cease processing data in subsequent write(2) calls until close(2)
  24. * is called.
  25. **/
  26. static void efi_free_all_buff_pages(struct capsule_info *cap_info)
  27. {
  28. while (cap_info->index > 0)
  29. __free_page(cap_info->pages[--cap_info->index]);
  30. cap_info->index = NO_FURTHER_WRITE_ACTION;
  31. }
  32. int __efi_capsule_setup_info(struct capsule_info *cap_info)
  33. {
  34. size_t pages_needed;
  35. int ret;
  36. void *temp_page;
  37. pages_needed = ALIGN(cap_info->total_size, PAGE_SIZE) / PAGE_SIZE;
  38. if (pages_needed == 0) {
  39. pr_err("invalid capsule size\n");
  40. return -EINVAL;
  41. }
  42. /* Check if the capsule binary supported */
  43. ret = efi_capsule_supported(cap_info->header.guid,
  44. cap_info->header.flags,
  45. cap_info->header.imagesize,
  46. &cap_info->reset_type);
  47. if (ret) {
  48. pr_err("capsule not supported\n");
  49. return ret;
  50. }
  51. temp_page = krealloc(cap_info->pages,
  52. pages_needed * sizeof(void *),
  53. GFP_KERNEL | __GFP_ZERO);
  54. if (!temp_page)
  55. return -ENOMEM;
  56. cap_info->pages = temp_page;
  57. temp_page = krealloc(cap_info->phys,
  58. pages_needed * sizeof(phys_addr_t *),
  59. GFP_KERNEL | __GFP_ZERO);
  60. if (!temp_page)
  61. return -ENOMEM;
  62. cap_info->phys = temp_page;
  63. return 0;
  64. }
  65. /**
  66. * efi_capsule_setup_info - obtain the efi capsule header in the binary and
  67. * setup capsule_info structure
  68. * @cap_info: pointer to current instance of capsule_info structure
  69. * @kbuff: a mapped first page buffer pointer
  70. * @hdr_bytes: the total received number of bytes for efi header
  71. *
  72. * Platforms with non-standard capsule update mechanisms can override
  73. * this __weak function so they can perform any required capsule
  74. * image munging. See quark_quirk_function() for an example.
  75. **/
  76. int __weak efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
  77. size_t hdr_bytes)
  78. {
  79. /* Only process data block that is larger than efi header size */
  80. if (hdr_bytes < sizeof(efi_capsule_header_t))
  81. return 0;
  82. memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));
  83. cap_info->total_size = cap_info->header.imagesize;
  84. return __efi_capsule_setup_info(cap_info);
  85. }
  86. /**
  87. * efi_capsule_submit_update - invoke the efi_capsule_update API once binary
  88. * upload done
  89. * @cap_info: pointer to current instance of capsule_info structure
  90. **/
  91. static ssize_t efi_capsule_submit_update(struct capsule_info *cap_info)
  92. {
  93. bool do_vunmap = false;
  94. int ret;
  95. /*
  96. * cap_info->capsule may have been assigned already by a quirk
  97. * handler, so only overwrite it if it is NULL
  98. */
  99. if (!cap_info->capsule) {
  100. cap_info->capsule = vmap(cap_info->pages, cap_info->index,
  101. VM_MAP, PAGE_KERNEL);
  102. if (!cap_info->capsule)
  103. return -ENOMEM;
  104. do_vunmap = true;
  105. }
  106. ret = efi_capsule_update(cap_info->capsule, cap_info->phys);
  107. if (do_vunmap)
  108. vunmap(cap_info->capsule);
  109. if (ret) {
  110. pr_err("capsule update failed\n");
  111. return ret;
  112. }
  113. /* Indicate capsule binary uploading is done */
  114. cap_info->index = NO_FURTHER_WRITE_ACTION;
  115. if (cap_info->header.flags & EFI_CAPSULE_PERSIST_ACROSS_RESET) {
  116. pr_info("Successfully uploaded capsule file with reboot type '%s'\n",
  117. !cap_info->reset_type ? "RESET_COLD" :
  118. cap_info->reset_type == 1 ? "RESET_WARM" :
  119. "RESET_SHUTDOWN");
  120. } else {
  121. pr_info("Successfully processed capsule file\n");
  122. }
  123. return 0;
  124. }
  125. /**
  126. * efi_capsule_write - store the capsule binary and pass it to
  127. * efi_capsule_update() API
  128. * @file: file pointer
  129. * @buff: buffer pointer
  130. * @count: number of bytes in @buff
  131. * @offp: not used
  132. *
  133. * Expectation:
  134. * - A user space tool should start at the beginning of capsule binary and
  135. * pass data in sequentially.
  136. * - Users should close and re-open this file note in order to upload more
  137. * capsules.
  138. * - After an error returned, user should close the file and restart the
  139. * operation for the next try otherwise -EIO will be returned until the
  140. * file is closed.
  141. * - An EFI capsule header must be located at the beginning of capsule
  142. * binary file and passed in as first block data of write operation.
  143. **/
  144. static ssize_t efi_capsule_write(struct file *file, const char __user *buff,
  145. size_t count, loff_t *offp)
  146. {
  147. int ret;
  148. struct capsule_info *cap_info = file->private_data;
  149. struct page *page;
  150. void *kbuff = NULL;
  151. size_t write_byte;
  152. if (count == 0)
  153. return 0;
  154. /* Return error while NO_FURTHER_WRITE_ACTION is flagged */
  155. if (cap_info->index < 0)
  156. return -EIO;
  157. /* Only alloc a new page when previous page is full */
  158. if (!cap_info->page_bytes_remain) {
  159. page = alloc_page(GFP_KERNEL);
  160. if (!page) {
  161. ret = -ENOMEM;
  162. goto failed;
  163. }
  164. cap_info->pages[cap_info->index] = page;
  165. cap_info->phys[cap_info->index] = page_to_phys(page);
  166. cap_info->page_bytes_remain = PAGE_SIZE;
  167. cap_info->index++;
  168. } else {
  169. page = cap_info->pages[cap_info->index - 1];
  170. }
  171. kbuff = kmap(page);
  172. kbuff += PAGE_SIZE - cap_info->page_bytes_remain;
  173. /* Copy capsule binary data from user space to kernel space buffer */
  174. write_byte = min_t(size_t, count, cap_info->page_bytes_remain);
  175. if (copy_from_user(kbuff, buff, write_byte)) {
  176. ret = -EFAULT;
  177. goto fail_unmap;
  178. }
  179. cap_info->page_bytes_remain -= write_byte;
  180. /* Setup capsule binary info structure */
  181. if (cap_info->header.headersize == 0) {
  182. ret = efi_capsule_setup_info(cap_info, kbuff - cap_info->count,
  183. cap_info->count + write_byte);
  184. if (ret)
  185. goto fail_unmap;
  186. }
  187. cap_info->count += write_byte;
  188. kunmap(page);
  189. /* Submit the full binary to efi_capsule_update() API */
  190. if (cap_info->header.headersize > 0 &&
  191. cap_info->count >= cap_info->total_size) {
  192. if (cap_info->count > cap_info->total_size) {
  193. pr_err("capsule upload size exceeded header defined size\n");
  194. ret = -EINVAL;
  195. goto failed;
  196. }
  197. ret = efi_capsule_submit_update(cap_info);
  198. if (ret)
  199. goto failed;
  200. }
  201. return write_byte;
  202. fail_unmap:
  203. kunmap(page);
  204. failed:
  205. efi_free_all_buff_pages(cap_info);
  206. return ret;
  207. }
  208. /**
  209. * efi_capsule_flush - called by file close or file flush
  210. * @file: file pointer
  211. * @id: not used
  212. *
  213. * If a capsule is being partially uploaded then calling this function
  214. * will be treated as upload termination and will free those completed
  215. * buffer pages and -ECANCELED will be returned.
  216. **/
  217. static int efi_capsule_flush(struct file *file, fl_owner_t id)
  218. {
  219. int ret = 0;
  220. struct capsule_info *cap_info = file->private_data;
  221. if (cap_info->index > 0) {
  222. pr_err("capsule upload not complete\n");
  223. efi_free_all_buff_pages(cap_info);
  224. ret = -ECANCELED;
  225. }
  226. return ret;
  227. }
  228. /**
  229. * efi_capsule_release - called by file close
  230. * @inode: not used
  231. * @file: file pointer
  232. *
  233. * We will not free successfully submitted pages since efi update
  234. * requires data to be maintained across system reboot.
  235. **/
  236. static int efi_capsule_release(struct inode *inode, struct file *file)
  237. {
  238. struct capsule_info *cap_info = file->private_data;
  239. kfree(cap_info->pages);
  240. kfree(cap_info->phys);
  241. kfree(file->private_data);
  242. file->private_data = NULL;
  243. return 0;
  244. }
  245. /**
  246. * efi_capsule_open - called by file open
  247. * @inode: not used
  248. * @file: file pointer
  249. *
  250. * Will allocate each capsule_info memory for each file open call.
  251. * This provided the capability to support multiple file open feature
  252. * where user is not needed to wait for others to finish in order to
  253. * upload their capsule binary.
  254. **/
  255. static int efi_capsule_open(struct inode *inode, struct file *file)
  256. {
  257. struct capsule_info *cap_info;
  258. cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL);
  259. if (!cap_info)
  260. return -ENOMEM;
  261. cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL);
  262. if (!cap_info->pages) {
  263. kfree(cap_info);
  264. return -ENOMEM;
  265. }
  266. cap_info->phys = kzalloc(sizeof(void *), GFP_KERNEL);
  267. if (!cap_info->phys) {
  268. kfree(cap_info->pages);
  269. kfree(cap_info);
  270. return -ENOMEM;
  271. }
  272. file->private_data = cap_info;
  273. return 0;
  274. }
  275. static const struct file_operations efi_capsule_fops = {
  276. .owner = THIS_MODULE,
  277. .open = efi_capsule_open,
  278. .write = efi_capsule_write,
  279. .flush = efi_capsule_flush,
  280. .release = efi_capsule_release,
  281. .llseek = no_llseek,
  282. };
  283. static struct miscdevice efi_capsule_misc = {
  284. .minor = MISC_DYNAMIC_MINOR,
  285. .name = "efi_capsule_loader",
  286. .fops = &efi_capsule_fops,
  287. };
  288. static int __init efi_capsule_loader_init(void)
  289. {
  290. int ret;
  291. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  292. return -ENODEV;
  293. ret = misc_register(&efi_capsule_misc);
  294. if (ret)
  295. pr_err("Unable to register capsule loader device\n");
  296. return ret;
  297. }
  298. module_init(efi_capsule_loader_init);
  299. static void __exit efi_capsule_loader_exit(void)
  300. {
  301. misc_deregister(&efi_capsule_misc);
  302. }
  303. module_exit(efi_capsule_loader_exit);
  304. MODULE_DESCRIPTION("EFI capsule firmware binary loader");
  305. MODULE_LICENSE("GPL v2");