fit_image.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. static int fit_check_params(struct image_tool_params *params)
  139. {
  140. return ((params->dflag && (params->fflag || params->lflag)) ||
  141. (params->fflag && (params->dflag || params->lflag)) ||
  142. (params->lflag && (params->dflag || params->fflag)));
  143. }
  144. static struct image_type_params fitimage_params = {
  145. .name = "FIT Image support",
  146. .header_size = sizeof(image_header_t),
  147. .hdr = (void*)&header,
  148. .verify_header = fit_verify_header,
  149. .print_header = fit_print_contents,
  150. .check_image_type = fit_check_image_types,
  151. .fflag_handle = fit_handle_file,
  152. .set_header = NULL, /* FIT images use DTB header */
  153. .check_params = fit_check_params,
  154. };
  155. void init_fit_image_type (void)
  156. {
  157. register_image_type(&fitimage_params);
  158. }