fit_image.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "mkimage.h"
  18. #include <image.h>
  19. #include <u-boot/crc.h>
  20. static image_header_t header;
  21. static int fit_verify_header (unsigned char *ptr, int image_size,
  22. struct image_tool_params *params)
  23. {
  24. return fdt_check_header(ptr);
  25. }
  26. static int fit_check_image_types (uint8_t type)
  27. {
  28. if (type == IH_TYPE_FLATDT)
  29. return EXIT_SUCCESS;
  30. else
  31. return EXIT_FAILURE;
  32. }
  33. int mmap_fdt(struct image_tool_params *params, const char *fname, void **blobp,
  34. struct stat *sbuf)
  35. {
  36. void *ptr;
  37. int fd;
  38. /* Load FIT blob into memory (we need to write hashes/signatures) */
  39. fd = open(fname, O_RDWR | O_BINARY);
  40. if (fd < 0) {
  41. fprintf(stderr, "%s: Can't open %s: %s\n",
  42. params->cmdname, fname, strerror(errno));
  43. unlink(fname);
  44. return -1;
  45. }
  46. if (fstat(fd, sbuf) < 0) {
  47. fprintf(stderr, "%s: Can't stat %s: %s\n",
  48. params->cmdname, fname, strerror(errno));
  49. unlink(fname);
  50. return -1;
  51. }
  52. ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  53. if (ptr == MAP_FAILED) {
  54. fprintf(stderr, "%s: Can't read %s: %s\n",
  55. params->cmdname, fname, strerror(errno));
  56. unlink(fname);
  57. return -1;
  58. }
  59. /* check if ptr has a valid blob */
  60. if (fdt_check_header(ptr)) {
  61. fprintf(stderr, "%s: Invalid FIT blob\n", params->cmdname);
  62. unlink(fname);
  63. return -1;
  64. }
  65. *blobp = ptr;
  66. return fd;
  67. }
  68. /**
  69. * fit_handle_file - main FIT file processing function
  70. *
  71. * fit_handle_file() runs dtc to convert .its to .itb, includes
  72. * binary data, updates timestamp property and calculates hashes.
  73. *
  74. * datafile - .its file
  75. * imagefile - .itb file
  76. *
  77. * returns:
  78. * only on success, otherwise calls exit (EXIT_FAILURE);
  79. */
  80. static int fit_handle_file(struct image_tool_params *params)
  81. {
  82. char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
  83. char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
  84. int tfd, destfd = 0;
  85. void *dest_blob = NULL;
  86. struct stat sbuf;
  87. void *ptr;
  88. off_t destfd_size = 0;
  89. /* Flattened Image Tree (FIT) format handling */
  90. debug ("FIT format handling\n");
  91. /* call dtc to include binary properties into the tmp file */
  92. if (strlen (params->imagefile) +
  93. strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
  94. fprintf (stderr, "%s: Image file name (%s) too long, "
  95. "can't create tmpfile",
  96. params->imagefile, params->cmdname);
  97. return (EXIT_FAILURE);
  98. }
  99. sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
  100. /* We either compile the source file, or use the existing FIT image */
  101. if (params->datafile) {
  102. /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
  103. snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
  104. MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
  105. debug("Trying to execute \"%s\"\n", cmd);
  106. } else {
  107. snprintf(cmd, sizeof(cmd), "cp %s %s",
  108. params->imagefile, tmpfile);
  109. }
  110. if (system (cmd) == -1) {
  111. fprintf (stderr, "%s: system(%s) failed: %s\n",
  112. params->cmdname, cmd, strerror(errno));
  113. goto err_system;
  114. }
  115. if (params->keydest) {
  116. destfd = mmap_fdt(params, params->keydest, &dest_blob, &sbuf);
  117. if (destfd < 0)
  118. goto err_keydest;
  119. destfd_size = sbuf.st_size;
  120. }
  121. tfd = mmap_fdt(params, tmpfile, &ptr, &sbuf);
  122. if (tfd < 0)
  123. goto err_mmap;
  124. /* set hashes for images in the blob */
  125. if (fit_add_verification_data(params->keydir,
  126. dest_blob, ptr, params->comment,
  127. params->require_keys)) {
  128. fprintf(stderr, "%s Can't add hashes to FIT blob\n",
  129. params->cmdname);
  130. goto err_add_hashes;
  131. }
  132. /* for first image creation, add a timestamp at offset 0 i.e., root */
  133. if (params->datafile && fit_set_timestamp(ptr, 0, sbuf.st_mtime)) {
  134. fprintf (stderr, "%s: Can't add image timestamp\n",
  135. params->cmdname);
  136. goto err_add_timestamp;
  137. }
  138. debug ("Added timestamp successfully\n");
  139. munmap ((void *)ptr, sbuf.st_size);
  140. close (tfd);
  141. if (dest_blob) {
  142. munmap(dest_blob, destfd_size);
  143. close(destfd);
  144. }
  145. if (rename (tmpfile, params->imagefile) == -1) {
  146. fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
  147. params->cmdname, tmpfile, params->imagefile,
  148. strerror (errno));
  149. unlink (tmpfile);
  150. unlink (params->imagefile);
  151. return (EXIT_FAILURE);
  152. }
  153. return (EXIT_SUCCESS);
  154. err_add_timestamp:
  155. err_add_hashes:
  156. munmap(ptr, sbuf.st_size);
  157. err_mmap:
  158. if (dest_blob)
  159. munmap(dest_blob, destfd_size);
  160. err_keydest:
  161. err_system:
  162. unlink(tmpfile);
  163. return -1;
  164. }
  165. static int fit_check_params(struct image_tool_params *params)
  166. {
  167. return ((params->dflag && (params->fflag || params->lflag)) ||
  168. (params->fflag && (params->dflag || params->lflag)) ||
  169. (params->lflag && (params->dflag || params->fflag)));
  170. }
  171. static struct image_type_params fitimage_params = {
  172. .name = "FIT Image support",
  173. .header_size = sizeof(image_header_t),
  174. .hdr = (void*)&header,
  175. .verify_header = fit_verify_header,
  176. .print_header = fit_print_contents,
  177. .check_image_type = fit_check_image_types,
  178. .fflag_handle = fit_handle_file,
  179. .set_header = NULL, /* FIT images use DTB header */
  180. .check_params = fit_check_params,
  181. };
  182. void init_fit_image_type (void)
  183. {
  184. register_image_type(&fitimage_params);
  185. }