fit_image.c 5.0 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. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include "mkimage.h"
  30. #include <image.h>
  31. #include <u-boot/crc.h>
  32. static image_header_t header;
  33. static int fit_verify_header (unsigned char *ptr, int image_size,
  34. struct mkimage_params *params)
  35. {
  36. return fdt_check_header ((void *)ptr);
  37. }
  38. static int fit_check_image_types (uint8_t type)
  39. {
  40. if (type == IH_TYPE_FLATDT)
  41. return EXIT_SUCCESS;
  42. else
  43. return EXIT_FAILURE;
  44. }
  45. /**
  46. * fit_handle_file - main FIT file processing function
  47. *
  48. * fit_handle_file() runs dtc to convert .its to .itb, includes
  49. * binary data, updates timestamp property and calculates hashes.
  50. *
  51. * datafile - .its file
  52. * imagefile - .itb file
  53. *
  54. * returns:
  55. * only on success, otherwise calls exit (EXIT_FAILURE);
  56. */
  57. static int fit_handle_file (struct mkimage_params *params)
  58. {
  59. char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
  60. char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
  61. int tfd;
  62. struct stat sbuf;
  63. unsigned char *ptr;
  64. /* Flattened Image Tree (FIT) format handling */
  65. debug ("FIT format handling\n");
  66. /* call dtc to include binary properties into the tmp file */
  67. if (strlen (params->imagefile) +
  68. strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
  69. fprintf (stderr, "%s: Image file name (%s) too long, "
  70. "can't create tmpfile",
  71. params->imagefile, params->cmdname);
  72. return (EXIT_FAILURE);
  73. }
  74. sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
  75. /* dtc -I dts -O -p 200 datafile > tmpfile */
  76. sprintf (cmd, "%s %s %s > %s",
  77. MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
  78. debug ("Trying to execute \"%s\"\n", cmd);
  79. if (system (cmd) == -1) {
  80. fprintf (stderr, "%s: system(%s) failed: %s\n",
  81. params->cmdname, cmd, strerror(errno));
  82. unlink (tmpfile);
  83. return (EXIT_FAILURE);
  84. }
  85. /* load FIT blob into memory */
  86. tfd = open (tmpfile, O_RDWR|O_BINARY);
  87. if (tfd < 0) {
  88. fprintf (stderr, "%s: Can't open %s: %s\n",
  89. params->cmdname, tmpfile, strerror(errno));
  90. unlink (tmpfile);
  91. return (EXIT_FAILURE);
  92. }
  93. if (fstat (tfd, &sbuf) < 0) {
  94. fprintf (stderr, "%s: Can't stat %s: %s\n",
  95. params->cmdname, tmpfile, strerror(errno));
  96. unlink (tmpfile);
  97. return (EXIT_FAILURE);
  98. }
  99. ptr = mmap (0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
  100. tfd, 0);
  101. if (ptr == MAP_FAILED) {
  102. fprintf (stderr, "%s: Can't read %s: %s\n",
  103. params->cmdname, tmpfile, strerror(errno));
  104. unlink (tmpfile);
  105. return (EXIT_FAILURE);
  106. }
  107. /* check if ptr has a valid blob */
  108. if (fdt_check_header (ptr)) {
  109. fprintf (stderr, "%s: Invalid FIT blob\n", params->cmdname);
  110. unlink (tmpfile);
  111. return (EXIT_FAILURE);
  112. }
  113. /* set hashes for images in the blob */
  114. if (fit_set_hashes (ptr)) {
  115. fprintf (stderr, "%s Can't add hashes to FIT blob",
  116. params->cmdname);
  117. unlink (tmpfile);
  118. return (EXIT_FAILURE);
  119. }
  120. /* add a timestamp at offset 0 i.e., root */
  121. if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) {
  122. fprintf (stderr, "%s: Can't add image timestamp\n",
  123. params->cmdname);
  124. unlink (tmpfile);
  125. return (EXIT_FAILURE);
  126. }
  127. debug ("Added timestamp successfully\n");
  128. munmap ((void *)ptr, sbuf.st_size);
  129. close (tfd);
  130. if (rename (tmpfile, params->imagefile) == -1) {
  131. fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
  132. params->cmdname, tmpfile, params->imagefile,
  133. strerror (errno));
  134. unlink (tmpfile);
  135. unlink (params->imagefile);
  136. return (EXIT_FAILURE);
  137. }
  138. return (EXIT_SUCCESS);
  139. }
  140. static int fit_check_params (struct mkimage_params *params)
  141. {
  142. return ((params->dflag && (params->fflag || params->lflag)) ||
  143. (params->fflag && (params->dflag || params->lflag)) ||
  144. (params->lflag && (params->dflag || params->fflag)));
  145. }
  146. static struct image_type_params fitimage_params = {
  147. .name = "FIT Image support",
  148. .header_size = sizeof(image_header_t),
  149. .hdr = (void*)&header,
  150. .verify_header = fit_verify_header,
  151. .print_header = fit_print_contents,
  152. .check_image_type = fit_check_image_types,
  153. .fflag_handle = fit_handle_file,
  154. .set_header = NULL, /* FIT images use DTB header */
  155. .check_params = fit_check_params,
  156. };
  157. void init_fit_image_type (void)
  158. {
  159. mkimage_register (&fitimage_params);
  160. }