capsule-loader.c 8.7 KB

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