image-host.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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_hash_value - set hash value in requested has node
  36. * @fit: pointer to the FIT format image header
  37. * @noffset: hash node offset
  38. * @value: hash value to be set
  39. * @value_len: hash value length
  40. *
  41. * fit_set_hash_value() attempts to set hash value in a node at offset
  42. * given and returns operation status to the caller.
  43. *
  44. * returns
  45. * 0, on success
  46. * -1, on failure
  47. */
  48. static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
  49. int value_len)
  50. {
  51. int ret;
  52. ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
  53. if (ret) {
  54. printf("Can't set hash '%s' property for '%s' node(%s)\n",
  55. FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
  56. fdt_strerror(ret));
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. /**
  62. * fit_image_process_hash - Process a single subnode of the images/ node
  63. *
  64. * Check each subnode and process accordingly. For hash nodes we generate
  65. * a hash of the supplised data and store it in the node.
  66. *
  67. * @fit: pointer to the FIT format image header
  68. * @image_name: name of image being processes (used to display errors)
  69. * @noffset: subnode offset
  70. * @data: data to process
  71. * @size: size of data in bytes
  72. * @return 0 if ok, -1 on error
  73. */
  74. static int fit_image_process_hash(void *fit, const char *image_name,
  75. int noffset, const void *data, size_t size)
  76. {
  77. uint8_t value[FIT_MAX_HASH_LEN];
  78. const char *node_name;
  79. int value_len;
  80. char *algo;
  81. node_name = fit_get_name(fit, noffset, NULL);
  82. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  83. printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
  84. node_name, image_name);
  85. return -1;
  86. }
  87. if (calculate_hash(data, size, algo, value, &value_len)) {
  88. printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
  89. algo, node_name, image_name);
  90. return -1;
  91. }
  92. if (fit_set_hash_value(fit, noffset, value, value_len)) {
  93. printf("Can't set hash value for '%s' hash node in '%s' image node\n",
  94. node_name, image_name);
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. /**
  100. * fit_image_add_verification_data() - calculate/set hash data for image node
  101. *
  102. * This adds hash values for a component image node.
  103. *
  104. * All existing hash subnodes are checked, if algorithm property is set to
  105. * one of the supported hash algorithms, hash value is computed and
  106. * corresponding hash node property is set, for example:
  107. *
  108. * Input component image node structure:
  109. *
  110. * o image@1 (at image_noffset)
  111. * | - data = [binary data]
  112. * o hash@1
  113. * |- algo = "sha1"
  114. *
  115. * Output component image node structure:
  116. *
  117. * o image@1 (at image_noffset)
  118. * | - data = [binary data]
  119. * o hash@1
  120. * |- algo = "sha1"
  121. * |- value = sha1(data)
  122. *
  123. * For signature details, please see doc/uImage.FIT/signature.txt
  124. *
  125. * @fit: Pointer to the FIT format image header
  126. * @image_noffset: Requested component image node
  127. * @return: 0 on success, <0 on failure
  128. */
  129. int fit_image_add_verification_data(void *fit, int image_noffset)
  130. {
  131. const char *image_name;
  132. const void *data;
  133. size_t size;
  134. int noffset;
  135. /* Get image data and data length */
  136. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  137. printf("Can't get image data/size\n");
  138. return -1;
  139. }
  140. image_name = fit_get_name(fit, image_noffset, NULL);
  141. /* Process all hash subnodes of the component image node */
  142. for (noffset = fdt_first_subnode(fit, image_noffset);
  143. noffset >= 0;
  144. noffset = fdt_next_subnode(fit, noffset)) {
  145. const char *node_name;
  146. int ret = 0;
  147. /*
  148. * Check subnode name, must be equal to "hash" or "signature".
  149. * Multiple hash nodes require unique unit node
  150. * names, e.g. hash@1, hash@2, signature@1, etc.
  151. */
  152. node_name = fit_get_name(fit, noffset, NULL);
  153. if (!strncmp(node_name, FIT_HASH_NODENAME,
  154. strlen(FIT_HASH_NODENAME))) {
  155. ret = fit_image_process_hash(fit, image_name, noffset,
  156. data, size);
  157. }
  158. if (ret)
  159. return -1;
  160. }
  161. return 0;
  162. }
  163. int fit_add_verification_data(void *fit)
  164. {
  165. int images_noffset;
  166. int noffset;
  167. int ret;
  168. /* Find images parent node offset */
  169. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  170. if (images_noffset < 0) {
  171. printf("Can't find images parent node '%s' (%s)\n",
  172. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  173. return images_noffset;
  174. }
  175. /* Process its subnodes, print out component images details */
  176. for (noffset = fdt_first_subnode(fit, images_noffset);
  177. noffset >= 0;
  178. noffset = fdt_next_subnode(fit, noffset)) {
  179. /*
  180. * Direct child node of the images parent node,
  181. * i.e. component image node.
  182. */
  183. ret = fit_image_add_verification_data(fit, noffset);
  184. if (ret)
  185. return ret;
  186. }
  187. return 0;
  188. }