fit_image.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2004
  5. * DENX Software Engineering
  6. * Wolfgang Denk, wd@denx.de
  7. *
  8. * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
  9. * FIT image specific code abstracted from mkimage.c
  10. * some functions added to address abstraction
  11. *
  12. * All rights reserved.
  13. *
  14. * SPDX-License-Identifier: GPL-2.0+
  15. */
  16. #include "imagetool.h"
  17. #include "fit_common.h"
  18. #include "mkimage.h"
  19. #include <image.h>
  20. #include <u-boot/crc.h>
  21. static image_header_t header;
  22. static int fit_add_file_data(struct image_tool_params *params, size_t size_inc,
  23. const char *tmpfile)
  24. {
  25. int tfd, destfd = 0;
  26. void *dest_blob = NULL;
  27. off_t destfd_size = 0;
  28. struct stat sbuf;
  29. void *ptr;
  30. int ret = 0;
  31. tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true);
  32. if (tfd < 0)
  33. return -EIO;
  34. if (params->keydest) {
  35. struct stat dest_sbuf;
  36. destfd = mmap_fdt(params->cmdname, params->keydest, size_inc,
  37. &dest_blob, &dest_sbuf, false);
  38. if (destfd < 0) {
  39. ret = -EIO;
  40. goto err_keydest;
  41. }
  42. destfd_size = dest_sbuf.st_size;
  43. }
  44. /* for first image creation, add a timestamp at offset 0 i.e., root */
  45. if (params->datafile)
  46. ret = fit_set_timestamp(ptr, 0, sbuf.st_mtime);
  47. if (!ret) {
  48. ret = fit_add_verification_data(params->keydir, dest_blob, ptr,
  49. params->comment,
  50. params->require_keys);
  51. }
  52. if (dest_blob) {
  53. munmap(dest_blob, destfd_size);
  54. close(destfd);
  55. }
  56. err_keydest:
  57. munmap(ptr, sbuf.st_size);
  58. close(tfd);
  59. return ret;
  60. }
  61. /**
  62. * fit_handle_file - main FIT file processing function
  63. *
  64. * fit_handle_file() runs dtc to convert .its to .itb, includes
  65. * binary data, updates timestamp property and calculates hashes.
  66. *
  67. * datafile - .its file
  68. * imagefile - .itb file
  69. *
  70. * returns:
  71. * only on success, otherwise calls exit (EXIT_FAILURE);
  72. */
  73. static int fit_handle_file(struct image_tool_params *params)
  74. {
  75. char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
  76. char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
  77. size_t size_inc;
  78. int ret;
  79. /* Flattened Image Tree (FIT) format handling */
  80. debug ("FIT format handling\n");
  81. /* call dtc to include binary properties into the tmp file */
  82. if (strlen (params->imagefile) +
  83. strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
  84. fprintf (stderr, "%s: Image file name (%s) too long, "
  85. "can't create tmpfile",
  86. params->imagefile, params->cmdname);
  87. return (EXIT_FAILURE);
  88. }
  89. sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
  90. /* We either compile the source file, or use the existing FIT image */
  91. if (params->datafile) {
  92. /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
  93. snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
  94. MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
  95. debug("Trying to execute \"%s\"\n", cmd);
  96. } else {
  97. snprintf(cmd, sizeof(cmd), "cp %s %s",
  98. params->imagefile, tmpfile);
  99. }
  100. if (system (cmd) == -1) {
  101. fprintf (stderr, "%s: system(%s) failed: %s\n",
  102. params->cmdname, cmd, strerror(errno));
  103. goto err_system;
  104. }
  105. /*
  106. * Set hashes for images in the blob. Unfortunately we may need more
  107. * space in either FDT, so keep trying until we succeed.
  108. *
  109. * Note: this is pretty inefficient for signing, since we must
  110. * calculate the signature every time. It would be better to calculate
  111. * all the data and then store it in a separate step. However, this
  112. * would be considerably more complex to implement. Generally a few
  113. * steps of this loop is enough to sign with several keys.
  114. */
  115. for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
  116. ret = fit_add_file_data(params, size_inc, tmpfile);
  117. if (!ret || ret != -ENOSPC)
  118. break;
  119. }
  120. if (ret) {
  121. fprintf(stderr, "%s Can't add hashes to FIT blob\n",
  122. params->cmdname);
  123. goto err_system;
  124. }
  125. if (rename (tmpfile, params->imagefile) == -1) {
  126. fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
  127. params->cmdname, tmpfile, params->imagefile,
  128. strerror (errno));
  129. unlink (tmpfile);
  130. unlink (params->imagefile);
  131. return EXIT_FAILURE;
  132. }
  133. return EXIT_SUCCESS;
  134. err_system:
  135. unlink(tmpfile);
  136. return -1;
  137. }
  138. /**
  139. * fit_image_extract - extract a FIT component image
  140. * @fit: pointer to the FIT format image header
  141. * @image_noffset: offset of the component image node
  142. * @file_name: name of the file to store the FIT sub-image
  143. *
  144. * returns:
  145. * zero in case of success or a negative value if fail.
  146. */
  147. static int fit_image_extract(
  148. const void *fit,
  149. int image_noffset,
  150. const char *file_name)
  151. {
  152. const void *file_data;
  153. size_t file_size = 0;
  154. /* get the "data" property of component at offset "image_noffset" */
  155. fit_image_get_data(fit, image_noffset, &file_data, &file_size);
  156. /* save the "file_data" into the file specified by "file_name" */
  157. return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
  158. }
  159. /**
  160. * fit_extract_contents - retrieve a sub-image component from the FIT image
  161. * @ptr: pointer to the FIT format image header
  162. * @params: command line parameters
  163. *
  164. * returns:
  165. * zero in case of success or a negative value if fail.
  166. */
  167. static int fit_extract_contents(void *ptr, struct image_tool_params *params)
  168. {
  169. int images_noffset;
  170. int noffset;
  171. int ndepth;
  172. const void *fit = ptr;
  173. int count = 0;
  174. const char *p;
  175. /* Indent string is defined in header image.h */
  176. p = IMAGE_INDENT_STRING;
  177. if (!fit_check_format(fit)) {
  178. printf("Bad FIT image format\n");
  179. return -1;
  180. }
  181. /* Find images parent node offset */
  182. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  183. if (images_noffset < 0) {
  184. printf("Can't find images parent node '%s' (%s)\n",
  185. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  186. return -1;
  187. }
  188. /* Avoid any overrun */
  189. count = fit_get_subimage_count(fit, images_noffset);
  190. if ((params->pflag < 0) || (count <= params->pflag)) {
  191. printf("No such component at '%d'\n", params->pflag);
  192. return -1;
  193. }
  194. /* Process its subnodes, extract the desired component from image */
  195. for (ndepth = 0, count = 0,
  196. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  197. (noffset >= 0) && (ndepth > 0);
  198. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  199. if (ndepth == 1) {
  200. /*
  201. * Direct child node of the images parent node,
  202. * i.e. component image node.
  203. */
  204. if (params->pflag == count) {
  205. printf("Extracted:\n%s Image %u (%s)\n", p,
  206. count, fit_get_name(fit, noffset, NULL));
  207. fit_image_print(fit, noffset, p);
  208. return fit_image_extract(fit, noffset,
  209. params->outfile);
  210. }
  211. count++;
  212. }
  213. }
  214. return 0;
  215. }
  216. static int fit_check_params(struct image_tool_params *params)
  217. {
  218. return ((params->dflag && (params->fflag || params->lflag)) ||
  219. (params->fflag && (params->dflag || params->lflag)) ||
  220. (params->lflag && (params->dflag || params->fflag)));
  221. }
  222. U_BOOT_IMAGE_TYPE(
  223. fitimage,
  224. "FIT Image support",
  225. sizeof(image_header_t),
  226. (void *)&header,
  227. fit_check_params,
  228. fit_verify_header,
  229. fit_print_contents,
  230. NULL,
  231. fit_extract_contents,
  232. fit_check_image_types,
  233. fit_handle_file,
  234. NULL /* FIT images use DTB header */
  235. );