capsule-loader.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. pr_info("Successfully upload 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. return 0;
  121. }
  122. /**
  123. * efi_capsule_write - store the capsule binary and pass it to
  124. * efi_capsule_update() API
  125. * @file: file pointer
  126. * @buff: buffer pointer
  127. * @count: number of bytes in @buff
  128. * @offp: not used
  129. *
  130. * Expectation:
  131. * - A user space tool should start at the beginning of capsule binary and
  132. * pass data in sequentially.
  133. * - Users should close and re-open this file note in order to upload more
  134. * capsules.
  135. * - After an error returned, user should close the file and restart the
  136. * operation for the next try otherwise -EIO will be returned until the
  137. * file is closed.
  138. * - An EFI capsule header must be located at the beginning of capsule
  139. * binary file and passed in as first block data of write operation.
  140. **/
  141. static ssize_t efi_capsule_write(struct file *file, const char __user *buff,
  142. size_t count, loff_t *offp)
  143. {
  144. int ret = 0;
  145. struct capsule_info *cap_info = file->private_data;
  146. struct page *page;
  147. void *kbuff = NULL;
  148. size_t write_byte;
  149. if (count == 0)
  150. return 0;
  151. /* Return error while NO_FURTHER_WRITE_ACTION is flagged */
  152. if (cap_info->index < 0)
  153. return -EIO;
  154. /* Only alloc a new page when previous page is full */
  155. if (!cap_info->page_bytes_remain) {
  156. page = alloc_page(GFP_KERNEL);
  157. if (!page) {
  158. ret = -ENOMEM;
  159. goto failed;
  160. }
  161. cap_info->pages[cap_info->index] = page;
  162. cap_info->phys[cap_info->index] = page_to_phys(page);
  163. cap_info->page_bytes_remain = PAGE_SIZE;
  164. cap_info->index++;
  165. } else {
  166. page = cap_info->pages[cap_info->index - 1];
  167. }
  168. kbuff = kmap(page);
  169. kbuff += PAGE_SIZE - cap_info->page_bytes_remain;
  170. /* Copy capsule binary data from user space to kernel space buffer */
  171. write_byte = min_t(size_t, count, cap_info->page_bytes_remain);
  172. if (copy_from_user(kbuff, buff, write_byte)) {
  173. ret = -EFAULT;
  174. goto fail_unmap;
  175. }
  176. cap_info->page_bytes_remain -= write_byte;
  177. /* Setup capsule binary info structure */
  178. if (cap_info->header.headersize == 0) {
  179. ret = efi_capsule_setup_info(cap_info, kbuff - cap_info->count,
  180. cap_info->count + write_byte);
  181. if (ret)
  182. goto fail_unmap;
  183. }
  184. cap_info->count += write_byte;
  185. kunmap(page);
  186. /* Submit the full binary to efi_capsule_update() API */
  187. if (cap_info->header.headersize > 0 &&
  188. cap_info->count >= cap_info->total_size) {
  189. if (cap_info->count > cap_info->total_size) {
  190. pr_err("capsule upload size exceeded header defined size\n");
  191. ret = -EINVAL;
  192. goto failed;
  193. }
  194. ret = efi_capsule_submit_update(cap_info);
  195. if (ret)
  196. goto failed;
  197. }
  198. return write_byte;
  199. fail_unmap:
  200. kunmap(page);
  201. failed:
  202. efi_free_all_buff_pages(cap_info);
  203. return ret;
  204. }
  205. /**
  206. * efi_capsule_flush - called by file close or file flush
  207. * @file: file pointer
  208. * @id: not used
  209. *
  210. * If a capsule is being partially uploaded then calling this function
  211. * will be treated as upload termination and will free those completed
  212. * buffer pages and -ECANCELED will be returned.
  213. **/
  214. static int efi_capsule_flush(struct file *file, fl_owner_t id)
  215. {
  216. int ret = 0;
  217. struct capsule_info *cap_info = file->private_data;
  218. if (cap_info->index > 0) {
  219. pr_err("capsule upload not complete\n");
  220. efi_free_all_buff_pages(cap_info);
  221. ret = -ECANCELED;
  222. }
  223. return ret;
  224. }
  225. /**
  226. * efi_capsule_release - called by file close
  227. * @inode: not used
  228. * @file: file pointer
  229. *
  230. * We will not free successfully submitted pages since efi update
  231. * requires data to be maintained across system reboot.
  232. **/
  233. static int efi_capsule_release(struct inode *inode, struct file *file)
  234. {
  235. struct capsule_info *cap_info = file->private_data;
  236. kfree(cap_info->pages);
  237. kfree(cap_info->phys);
  238. kfree(file->private_data);
  239. file->private_data = NULL;
  240. return 0;
  241. }
  242. /**
  243. * efi_capsule_open - called by file open
  244. * @inode: not used
  245. * @file: file pointer
  246. *
  247. * Will allocate each capsule_info memory for each file open call.
  248. * This provided the capability to support multiple file open feature
  249. * where user is not needed to wait for others to finish in order to
  250. * upload their capsule binary.
  251. **/
  252. static int efi_capsule_open(struct inode *inode, struct file *file)
  253. {
  254. struct capsule_info *cap_info;
  255. cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL);
  256. if (!cap_info)
  257. return -ENOMEM;
  258. cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL);
  259. if (!cap_info->pages) {
  260. kfree(cap_info);
  261. return -ENOMEM;
  262. }
  263. cap_info->phys = kzalloc(sizeof(void *), GFP_KERNEL);
  264. if (!cap_info->phys) {
  265. kfree(cap_info->pages);
  266. kfree(cap_info);
  267. return -ENOMEM;
  268. }
  269. file->private_data = cap_info;
  270. return 0;
  271. }
  272. static const struct file_operations efi_capsule_fops = {
  273. .owner = THIS_MODULE,
  274. .open = efi_capsule_open,
  275. .write = efi_capsule_write,
  276. .flush = efi_capsule_flush,
  277. .release = efi_capsule_release,
  278. .llseek = no_llseek,
  279. };
  280. static struct miscdevice efi_capsule_misc = {
  281. .minor = MISC_DYNAMIC_MINOR,
  282. .name = "efi_capsule_loader",
  283. .fops = &efi_capsule_fops,
  284. };
  285. static int __init efi_capsule_loader_init(void)
  286. {
  287. int ret;
  288. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  289. return -ENODEV;
  290. ret = misc_register(&efi_capsule_misc);
  291. if (ret)
  292. pr_err("Unable to register capsule loader device\n");
  293. return ret;
  294. }
  295. module_init(efi_capsule_loader_init);
  296. static void __exit efi_capsule_loader_exit(void)
  297. {
  298. misc_deregister(&efi_capsule_misc);
  299. }
  300. module_exit(efi_capsule_loader_exit);
  301. MODULE_DESCRIPTION("EFI capsule firmware binary loader");
  302. MODULE_LICENSE("GPL v2");