fit_image.c 5.2 KB

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