capsule-loader.c 9.1 KB

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