mkeficapsule.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2018 Linaro Limited
  4. * Author: AKASHI Takahiro
  5. */
  6. #include <errno.h>
  7. #include <getopt.h>
  8. #include <malloc.h>
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <linux/types.h>
  15. #include <sys/mman.h>
  16. #include <sys/stat.h>
  17. #include <sys/types.h>
  18. #include "fdt_host.h"
  19. typedef __u8 u8;
  20. typedef __u16 u16;
  21. typedef __u32 u32;
  22. typedef __u64 u64;
  23. typedef __s16 s16;
  24. typedef __s32 s32;
  25. #define aligned_u64 __aligned_u64
  26. #define SIGNATURE_NODENAME "signature"
  27. #define OVERLAY_NODENAME "__overlay__"
  28. #ifndef __packed
  29. #define __packed __attribute__((packed))
  30. #endif
  31. #include <efi.h>
  32. #include <efi_api.h>
  33. static const char *tool_name = "mkeficapsule";
  34. efi_guid_t efi_guid_fm_capsule = EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
  35. efi_guid_t efi_guid_image_type_uboot_fit =
  36. EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID;
  37. efi_guid_t efi_guid_image_type_uboot_raw =
  38. EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID;
  39. static struct option options[] = {
  40. {"fit", required_argument, NULL, 'f'},
  41. {"raw", required_argument, NULL, 'r'},
  42. {"index", required_argument, NULL, 'i'},
  43. {"instance", required_argument, NULL, 'I'},
  44. {"dtb", required_argument, NULL, 'D'},
  45. {"public key", required_argument, NULL, 'K'},
  46. {"overlay", no_argument, NULL, 'O'},
  47. {"help", no_argument, NULL, 'h'},
  48. {NULL, 0, NULL, 0},
  49. };
  50. static void print_usage(void)
  51. {
  52. printf("Usage: %s [options] <output file>\n"
  53. "Options:\n"
  54. "\t--fit <fit image> new FIT image file\n"
  55. "\t--raw <raw image> new raw image file\n"
  56. "\t--index <index> update image index\n"
  57. "\t--instance <instance> update hardware instance\n"
  58. "\t--public-key <key file> public key esl file\n"
  59. "\t--dtb <dtb file> dtb file\n"
  60. "\t--overlay the dtb file is an overlay\n"
  61. "\t--help print a help message\n",
  62. tool_name);
  63. }
  64. static int fdt_add_pub_key_data(void *sptr, void *dptr, size_t key_size,
  65. bool overlay)
  66. {
  67. int parent;
  68. int ov_node;
  69. int frag_node;
  70. int ret = 0;
  71. if (overlay) {
  72. /*
  73. * The signature would be stored in the
  74. * first fragment node of the overlay
  75. */
  76. frag_node = fdt_first_subnode(dptr, 0);
  77. if (frag_node == -FDT_ERR_NOTFOUND) {
  78. fprintf(stderr,
  79. "Couldn't find the fragment node: %s\n",
  80. fdt_strerror(frag_node));
  81. goto done;
  82. }
  83. ov_node = fdt_subnode_offset(dptr, frag_node, OVERLAY_NODENAME);
  84. if (ov_node == -FDT_ERR_NOTFOUND) {
  85. fprintf(stderr,
  86. "Couldn't find the __overlay__ node: %s\n",
  87. fdt_strerror(ov_node));
  88. goto done;
  89. }
  90. } else {
  91. ov_node = 0;
  92. }
  93. parent = fdt_subnode_offset(dptr, ov_node, SIGNATURE_NODENAME);
  94. if (parent == -FDT_ERR_NOTFOUND) {
  95. parent = fdt_add_subnode(dptr, ov_node, SIGNATURE_NODENAME);
  96. if (parent < 0) {
  97. ret = parent;
  98. if (ret != -FDT_ERR_NOSPACE) {
  99. fprintf(stderr,
  100. "Couldn't create signature node: %s\n",
  101. fdt_strerror(parent));
  102. }
  103. }
  104. }
  105. if (ret)
  106. goto done;
  107. /* Write the key to the FDT node */
  108. ret = fdt_setprop(dptr, parent, "capsule-key",
  109. sptr, key_size);
  110. done:
  111. if (ret)
  112. ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
  113. return ret;
  114. }
  115. static int add_public_key(const char *pkey_file, const char *dtb_file,
  116. bool overlay)
  117. {
  118. int ret;
  119. int srcfd = 0;
  120. int destfd = 0;
  121. void *sptr = NULL;
  122. void *dptr = NULL;
  123. off_t src_size;
  124. struct stat pub_key;
  125. struct stat dtb;
  126. /* Find out the size of the public key */
  127. srcfd = open(pkey_file, O_RDONLY);
  128. if (srcfd == -1) {
  129. fprintf(stderr, "%s: Can't open %s: %s\n",
  130. __func__, pkey_file, strerror(errno));
  131. goto err;
  132. }
  133. ret = fstat(srcfd, &pub_key);
  134. if (ret == -1) {
  135. fprintf(stderr, "%s: Can't stat %s: %s\n",
  136. __func__, pkey_file, strerror(errno));
  137. goto err;
  138. }
  139. src_size = pub_key.st_size;
  140. /* mmap the public key esl file */
  141. sptr = mmap(0, src_size, PROT_READ, MAP_SHARED, srcfd, 0);
  142. if ((sptr == MAP_FAILED) || (errno != 0)) {
  143. fprintf(stderr, "%s: Failed to mmap %s:%s\n",
  144. __func__, pkey_file, strerror(errno));
  145. goto err;
  146. }
  147. /* Open the dest FDT */
  148. destfd = open(dtb_file, O_RDWR);
  149. if (destfd == -1) {
  150. fprintf(stderr, "%s: Can't open %s: %s\n",
  151. __func__, dtb_file, strerror(errno));
  152. goto err;
  153. }
  154. ret = fstat(destfd, &dtb);
  155. if (ret == -1) {
  156. fprintf(stderr, "%s: Can't stat %s: %s\n",
  157. __func__, dtb_file, strerror(errno));
  158. goto err;
  159. }
  160. dtb.st_size += src_size + 0x30;
  161. if (ftruncate(destfd, dtb.st_size)) {
  162. fprintf(stderr, "%s: Can't expand %s: %s\n",
  163. __func__, dtb_file, strerror(errno));
  164. goto err;;
  165. }
  166. errno = 0;
  167. /* mmap the dtb file */
  168. dptr = mmap(0, dtb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED,
  169. destfd, 0);
  170. if ((dptr == MAP_FAILED) || (errno != 0)) {
  171. fprintf(stderr, "%s: Failed to mmap %s:%s\n",
  172. __func__, dtb_file, strerror(errno));
  173. goto err;
  174. }
  175. if (fdt_check_header(dptr)) {
  176. fprintf(stderr, "%s: Invalid FDT header\n", __func__);
  177. goto err;
  178. }
  179. ret = fdt_open_into(dptr, dptr, dtb.st_size);
  180. if (ret) {
  181. fprintf(stderr, "%s: Cannot expand FDT: %s\n",
  182. __func__, fdt_strerror(ret));
  183. goto err;
  184. }
  185. /* Copy the esl file to the expanded FDT */
  186. ret = fdt_add_pub_key_data(sptr, dptr, src_size, overlay);
  187. if (ret < 0) {
  188. fprintf(stderr, "%s: Unable to add public key to the FDT\n",
  189. __func__);
  190. goto err;
  191. }
  192. return 0;
  193. err:
  194. if (sptr)
  195. munmap(sptr, src_size);
  196. if (dptr)
  197. munmap(dptr, dtb.st_size);
  198. if (srcfd >= 0)
  199. close(srcfd);
  200. if (destfd >= 0)
  201. close(destfd);
  202. return -1;
  203. }
  204. static int create_fwbin(char *path, char *bin, efi_guid_t *guid,
  205. unsigned long index, unsigned long instance)
  206. {
  207. struct efi_capsule_header header;
  208. struct efi_firmware_management_capsule_header capsule;
  209. struct efi_firmware_management_capsule_image_header image;
  210. FILE *f, *g;
  211. struct stat bin_stat;
  212. u8 *data;
  213. size_t size;
  214. u64 offset;
  215. #ifdef DEBUG
  216. printf("For output: %s\n", path);
  217. printf("\tbin: %s\n\ttype: %pUl\n" bin, guid);
  218. printf("\tindex: %ld\n\tinstance: %ld\n", index, instance);
  219. #endif
  220. g = fopen(bin, "r");
  221. if (!g) {
  222. printf("cannot open %s\n", bin);
  223. return -1;
  224. }
  225. if (stat(bin, &bin_stat) < 0) {
  226. printf("cannot determine the size of %s\n", bin);
  227. goto err_1;
  228. }
  229. data = malloc(bin_stat.st_size);
  230. if (!data) {
  231. printf("cannot allocate memory: %lx\n", bin_stat.st_size);
  232. goto err_1;
  233. }
  234. f = fopen(path, "w");
  235. if (!f) {
  236. printf("cannot open %s\n", path);
  237. goto err_2;
  238. }
  239. header.capsule_guid = efi_guid_fm_capsule;
  240. header.header_size = sizeof(header);
  241. /* TODO: The current implementation ignores flags */
  242. header.flags = CAPSULE_FLAGS_PERSIST_ACROSS_RESET;
  243. header.capsule_image_size = sizeof(header)
  244. + sizeof(capsule) + sizeof(u64)
  245. + sizeof(image)
  246. + bin_stat.st_size;
  247. size = fwrite(&header, 1, sizeof(header), f);
  248. if (size < sizeof(header)) {
  249. printf("write failed (%lx)\n", size);
  250. goto err_3;
  251. }
  252. capsule.version = 0x00000001;
  253. capsule.embedded_driver_count = 0;
  254. capsule.payload_item_count = 1;
  255. size = fwrite(&capsule, 1, sizeof(capsule), f);
  256. if (size < (sizeof(capsule))) {
  257. printf("write failed (%lx)\n", size);
  258. goto err_3;
  259. }
  260. offset = sizeof(capsule) + sizeof(u64);
  261. size = fwrite(&offset, 1, sizeof(offset), f);
  262. if (size < sizeof(offset)) {
  263. printf("write failed (%lx)\n", size);
  264. goto err_3;
  265. }
  266. image.version = 0x00000003;
  267. memcpy(&image.update_image_type_id, guid, sizeof(*guid));
  268. image.update_image_index = index;
  269. image.update_image_size = bin_stat.st_size;
  270. image.update_vendor_code_size = 0; /* none */
  271. image.update_hardware_instance = instance;
  272. image.image_capsule_support = 0;
  273. size = fwrite(&image, 1, sizeof(image), f);
  274. if (size < sizeof(image)) {
  275. printf("write failed (%lx)\n", size);
  276. goto err_3;
  277. }
  278. size = fread(data, 1, bin_stat.st_size, g);
  279. if (size < bin_stat.st_size) {
  280. printf("read failed (%lx)\n", size);
  281. goto err_3;
  282. }
  283. size = fwrite(data, 1, bin_stat.st_size, f);
  284. if (size < bin_stat.st_size) {
  285. printf("write failed (%lx)\n", size);
  286. goto err_3;
  287. }
  288. fclose(f);
  289. fclose(g);
  290. free(data);
  291. return 0;
  292. err_3:
  293. fclose(f);
  294. err_2:
  295. free(data);
  296. err_1:
  297. fclose(g);
  298. return -1;
  299. }
  300. /*
  301. * Usage:
  302. * $ mkeficapsule -f <firmware binary> <output file>
  303. */
  304. int main(int argc, char **argv)
  305. {
  306. char *file;
  307. char *pkey_file;
  308. char *dtb_file;
  309. efi_guid_t *guid;
  310. unsigned long index, instance;
  311. int c, idx;
  312. int ret;
  313. bool overlay = false;
  314. file = NULL;
  315. pkey_file = NULL;
  316. dtb_file = NULL;
  317. guid = NULL;
  318. index = 0;
  319. instance = 0;
  320. for (;;) {
  321. c = getopt_long(argc, argv, "f:r:i:I:v:D:K:Oh", options, &idx);
  322. if (c == -1)
  323. break;
  324. switch (c) {
  325. case 'f':
  326. if (file) {
  327. printf("Image already specified\n");
  328. return -1;
  329. }
  330. file = optarg;
  331. guid = &efi_guid_image_type_uboot_fit;
  332. break;
  333. case 'r':
  334. if (file) {
  335. printf("Image already specified\n");
  336. return -1;
  337. }
  338. file = optarg;
  339. guid = &efi_guid_image_type_uboot_raw;
  340. break;
  341. case 'i':
  342. index = strtoul(optarg, NULL, 0);
  343. break;
  344. case 'I':
  345. instance = strtoul(optarg, NULL, 0);
  346. break;
  347. case 'K':
  348. if (pkey_file) {
  349. printf("Public Key already specified\n");
  350. return -1;
  351. }
  352. pkey_file = optarg;
  353. break;
  354. case 'D':
  355. if (dtb_file) {
  356. printf("DTB file already specified\n");
  357. return -1;
  358. }
  359. dtb_file = optarg;
  360. break;
  361. case 'O':
  362. overlay = true;
  363. break;
  364. case 'h':
  365. print_usage();
  366. return 0;
  367. }
  368. }
  369. /* need a fit image file or raw image file */
  370. if (!file && !pkey_file && !dtb_file) {
  371. printf("%s: %d\n", __func__, __LINE__);
  372. print_usage();
  373. return -1;
  374. }
  375. if (pkey_file && dtb_file) {
  376. ret = add_public_key(pkey_file, dtb_file, overlay);
  377. if (ret == -1) {
  378. printf("Adding public key to the dtb failed\n");
  379. return -1;
  380. } else {
  381. return 0;
  382. }
  383. }
  384. if (create_fwbin(argv[optind], file, guid, index, instance)
  385. < 0) {
  386. printf("Creating firmware capsule failed\n");
  387. return -1;
  388. }
  389. return 0;
  390. }