fit_image.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  23. * fit_handle_file - main FIT file processing function
  24. *
  25. * fit_handle_file() runs dtc to convert .its to .itb, includes
  26. * binary data, updates timestamp property and calculates hashes.
  27. *
  28. * datafile - .its file
  29. * imagefile - .itb file
  30. *
  31. * returns:
  32. * only on success, otherwise calls exit (EXIT_FAILURE);
  33. */
  34. static int fit_handle_file(struct image_tool_params *params)
  35. {
  36. char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
  37. char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
  38. int tfd, destfd = 0;
  39. void *dest_blob = NULL;
  40. struct stat sbuf;
  41. void *ptr;
  42. off_t destfd_size = 0;
  43. /* Flattened Image Tree (FIT) format handling */
  44. debug ("FIT format handling\n");
  45. /* call dtc to include binary properties into the tmp file */
  46. if (strlen (params->imagefile) +
  47. strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
  48. fprintf (stderr, "%s: Image file name (%s) too long, "
  49. "can't create tmpfile",
  50. params->imagefile, params->cmdname);
  51. return (EXIT_FAILURE);
  52. }
  53. sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
  54. /* We either compile the source file, or use the existing FIT image */
  55. if (params->datafile) {
  56. /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
  57. snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
  58. MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
  59. debug("Trying to execute \"%s\"\n", cmd);
  60. } else {
  61. snprintf(cmd, sizeof(cmd), "cp %s %s",
  62. params->imagefile, tmpfile);
  63. }
  64. if (system (cmd) == -1) {
  65. fprintf (stderr, "%s: system(%s) failed: %s\n",
  66. params->cmdname, cmd, strerror(errno));
  67. goto err_system;
  68. }
  69. if (params->keydest) {
  70. destfd = mmap_fdt(params->cmdname, params->keydest,
  71. &dest_blob, &sbuf, 1);
  72. if (destfd < 0)
  73. goto err_keydest;
  74. destfd_size = sbuf.st_size;
  75. }
  76. tfd = mmap_fdt(params->cmdname, tmpfile, &ptr, &sbuf, 1);
  77. if (tfd < 0)
  78. goto err_mmap;
  79. /* set hashes for images in the blob */
  80. if (fit_add_verification_data(params->keydir,
  81. dest_blob, ptr, params->comment,
  82. params->require_keys)) {
  83. fprintf(stderr, "%s Can't add hashes to FIT blob\n",
  84. params->cmdname);
  85. goto err_add_hashes;
  86. }
  87. /* for first image creation, add a timestamp at offset 0 i.e., root */
  88. if (params->datafile && fit_set_timestamp(ptr, 0, sbuf.st_mtime)) {
  89. fprintf (stderr, "%s: Can't add image timestamp\n",
  90. params->cmdname);
  91. goto err_add_timestamp;
  92. }
  93. debug ("Added timestamp successfully\n");
  94. munmap ((void *)ptr, sbuf.st_size);
  95. close (tfd);
  96. if (dest_blob) {
  97. munmap(dest_blob, destfd_size);
  98. close(destfd);
  99. }
  100. if (rename (tmpfile, params->imagefile) == -1) {
  101. fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
  102. params->cmdname, tmpfile, params->imagefile,
  103. strerror (errno));
  104. unlink (tmpfile);
  105. unlink (params->imagefile);
  106. return (EXIT_FAILURE);
  107. }
  108. return (EXIT_SUCCESS);
  109. err_add_timestamp:
  110. err_add_hashes:
  111. munmap(ptr, sbuf.st_size);
  112. err_mmap:
  113. if (dest_blob)
  114. munmap(dest_blob, destfd_size);
  115. err_keydest:
  116. err_system:
  117. unlink(tmpfile);
  118. return -1;
  119. }
  120. static int fit_check_params(struct image_tool_params *params)
  121. {
  122. return ((params->dflag && (params->fflag || params->lflag)) ||
  123. (params->fflag && (params->dflag || params->lflag)) ||
  124. (params->lflag && (params->dflag || params->fflag)));
  125. }
  126. static struct image_type_params fitimage_params = {
  127. .name = "FIT Image support",
  128. .header_size = sizeof(image_header_t),
  129. .hdr = (void*)&header,
  130. .verify_header = fit_verify_header,
  131. .print_header = fit_print_contents,
  132. .check_image_type = fit_check_image_types,
  133. .fflag_handle = fit_handle_file,
  134. .set_header = NULL, /* FIT images use DTB header */
  135. .check_params = fit_check_params,
  136. };
  137. void init_fit_image_type (void)
  138. {
  139. register_image_type(&fitimage_params);
  140. }