image-host.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2013, Google Inc.
  3. *
  4. * (C) Copyright 2008 Semihalf
  5. *
  6. * (C) Copyright 2000-2006
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include "mkimage.h"
  28. #include <bootstage.h>
  29. #include <image.h>
  30. #include <sha1.h>
  31. #include <time.h>
  32. #include <u-boot/crc.h>
  33. #include <u-boot/md5.h>
  34. /**
  35. * fit_set_hashes - process FIT component image nodes and calculate hashes
  36. * @fit: pointer to the FIT format image header
  37. *
  38. * fit_set_hashes() adds hash values for all component images in the FIT blob.
  39. * Hashes are calculated for all component images which have hash subnodes
  40. * with algorithm property set to one of the supported hash algorithms.
  41. *
  42. * returns
  43. * 0, on success
  44. * libfdt error code, on failure
  45. */
  46. int fit_set_hashes(void *fit)
  47. {
  48. int images_noffset;
  49. int noffset;
  50. int ndepth;
  51. int ret;
  52. /* Find images parent node offset */
  53. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  54. if (images_noffset < 0) {
  55. printf("Can't find images parent node '%s' (%s)\n",
  56. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  57. return images_noffset;
  58. }
  59. /* Process its subnodes, print out component images details */
  60. for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
  61. (noffset >= 0) && (ndepth > 0);
  62. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  63. if (ndepth == 1) {
  64. /*
  65. * Direct child node of the images parent node,
  66. * i.e. component image node.
  67. */
  68. ret = fit_image_set_hashes(fit, noffset);
  69. if (ret)
  70. return ret;
  71. }
  72. }
  73. return 0;
  74. }
  75. /**
  76. * fit_image_process_hash - Process a single subnode of the images/ node
  77. *
  78. * Check each subnode and process accordingly. For hash nodes we generate
  79. * a hash of the supplised data and store it in the node.
  80. *
  81. * @fit: pointer to the FIT format image header
  82. * @image_name: name of image being processes (used to display errors)
  83. * @noffset: subnode offset
  84. * @data: data to process
  85. * @size: size of data in bytes
  86. * @return 0 if ok, -1 on error
  87. */
  88. static int fit_image_process_hash(void *fit, const char *image_name,
  89. int noffset, const void *data, size_t size)
  90. {
  91. uint8_t value[FIT_MAX_HASH_LEN];
  92. int value_len;
  93. char *algo;
  94. /*
  95. * Check subnode name, must be equal to "hash".
  96. * Multiple hash nodes require unique unit node
  97. * names, e.g. hash@1, hash@2, etc.
  98. */
  99. if (strncmp(fit_get_name(fit, noffset, NULL),
  100. FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME)) != 0)
  101. return 0;
  102. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  103. printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
  104. fit_get_name(fit, noffset, NULL), image_name);
  105. return -1;
  106. }
  107. if (calculate_hash(data, size, algo, value, &value_len)) {
  108. printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
  109. algo, fit_get_name(fit, noffset, NULL), image_name);
  110. return -1;
  111. }
  112. if (fit_image_hash_set_value(fit, noffset, value, value_len)) {
  113. printf("Can't set hash value for '%s' hash node in '%s' image node\n",
  114. fit_get_name(fit, noffset, NULL), image_name);
  115. return -1;
  116. }
  117. return 0;
  118. }
  119. /**
  120. * fit_image_set_hashes - calculate/set hashes for given component image node
  121. * @fit: pointer to the FIT format image header
  122. * @image_noffset: requested component image node
  123. *
  124. * fit_image_set_hashes() adds hash values for an component image node. All
  125. * existing hash subnodes are checked, if algorithm property is set to one of
  126. * the supported hash algorithms, hash value is computed and corresponding
  127. * hash node property is set, for example:
  128. *
  129. * Input component image node structure:
  130. *
  131. * o image@1 (at image_noffset)
  132. * | - data = [binary data]
  133. * o hash@1
  134. * |- algo = "sha1"
  135. *
  136. * Output component image node structure:
  137. *
  138. * o image@1 (at image_noffset)
  139. * | - data = [binary data]
  140. * o hash@1
  141. * |- algo = "sha1"
  142. * |- value = sha1(data)
  143. *
  144. * returns:
  145. * 0 on sucess
  146. * <0 on failure
  147. */
  148. int fit_image_set_hashes(void *fit, int image_noffset)
  149. {
  150. const void *data;
  151. size_t size;
  152. int noffset;
  153. int ndepth;
  154. const char *image_name;
  155. /* Get image data and data length */
  156. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  157. printf("Can't get image data/size\n");
  158. return -1;
  159. }
  160. image_name = fit_get_name(fit, image_noffset, NULL);
  161. /* Process all hash subnodes of the component image node */
  162. for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
  163. (noffset >= 0) && (ndepth > 0);
  164. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  165. if (ndepth == 1) {
  166. /* Direct child node of the component image node */
  167. if (fit_image_process_hash(fit, image_name, noffset,
  168. data, size))
  169. return -1;
  170. }
  171. }
  172. return 0;
  173. }
  174. /**
  175. * fit_image_hash_set_value - set hash value in requested has node
  176. * @fit: pointer to the FIT format image header
  177. * @noffset: hash node offset
  178. * @value: hash value to be set
  179. * @value_len: hash value length
  180. *
  181. * fit_image_hash_set_value() attempts to set hash value in a node at offset
  182. * given and returns operation status to the caller.
  183. *
  184. * returns
  185. * 0, on success
  186. * -1, on failure
  187. */
  188. int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
  189. int value_len)
  190. {
  191. int ret;
  192. ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
  193. if (ret) {
  194. printf("Can't set hash '%s' property for '%s' node(%s)\n",
  195. FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
  196. fdt_strerror(ret));
  197. return -1;
  198. }
  199. return 0;
  200. }