fit_image.c 5.2 KB

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