fit_image.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 void fit_set_header (void *ptr, struct stat *sbuf, int ifd,
  141. struct mkimage_params *params)
  142. {
  143. uint32_t checksum;
  144. image_header_t * hdr = (image_header_t *)ptr;
  145. checksum = crc32 (0,
  146. (const unsigned char *)(ptr +
  147. sizeof(image_header_t)),
  148. sbuf->st_size - sizeof(image_header_t));
  149. /* Build new header */
  150. image_set_magic (hdr, IH_MAGIC);
  151. image_set_time (hdr, sbuf->st_mtime);
  152. image_set_size (hdr, sbuf->st_size - sizeof(image_header_t));
  153. image_set_load (hdr, params->addr);
  154. image_set_ep (hdr, params->ep);
  155. image_set_dcrc (hdr, checksum);
  156. image_set_os (hdr, params->os);
  157. image_set_arch (hdr, params->arch);
  158. image_set_type (hdr, params->type);
  159. image_set_comp (hdr, params->comp);
  160. image_set_name (hdr, params->imagename);
  161. checksum = crc32 (0, (const unsigned char *)hdr,
  162. sizeof(image_header_t));
  163. image_set_hcrc (hdr, checksum);
  164. }
  165. static int fit_check_params (struct mkimage_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 = fit_set_header,
  180. .check_params = fit_check_params,
  181. };
  182. void init_fit_image_type (void)
  183. {
  184. mkimage_register (&fitimage_params);
  185. }