2 * Copyright (c) 2013, Google Inc.
4 * (C) Copyright 2008 Semihalf
6 * (C) Copyright 2000-2006
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include <bootstage.h>
33 #include <u-boot/crc.h>
34 #include <u-boot/md5.h>
37 * fit_set_hashes - process FIT component image nodes and calculate hashes
38 * @fit: pointer to the FIT format image header
40 * fit_set_hashes() adds hash values for all component images in the FIT blob.
41 * Hashes are calculated for all component images which have hash subnodes
42 * with algorithm property set to one of the supported hash algorithms.
46 * libfdt error code, on failure
48 int fit_set_hashes(void *fit)
55 /* Find images parent node offset */
56 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
57 if (images_noffset < 0) {
58 printf("Can't find images parent node '%s' (%s)\n",
59 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
60 return images_noffset;
63 /* Process its subnodes, print out component images details */
64 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
65 (noffset >= 0) && (ndepth > 0);
66 noffset = fdt_next_node(fit, noffset, &ndepth)) {
69 * Direct child node of the images parent node,
70 * i.e. component image node.
72 ret = fit_image_set_hashes(fit, noffset);
82 * fit_image_process_hash - Process a single subnode of the images/ node
84 * Check each subnode and process accordingly. For hash nodes we generate
85 * a hash of the supplised data and store it in the node.
87 * @fit: pointer to the FIT format image header
88 * @image_name: name of image being processes (used to display errors)
89 * @noffset: subnode offset
90 * @data: data to process
91 * @size: size of data in bytes
92 * @return 0 if ok, -1 on error
94 static int fit_image_process_hash(void *fit, const char *image_name,
95 int noffset, const void *data, size_t size)
97 uint8_t value[FIT_MAX_HASH_LEN];
102 * Check subnode name, must be equal to "hash".
103 * Multiple hash nodes require unique unit node
104 * names, e.g. hash@1, hash@2, etc.
106 if (strncmp(fit_get_name(fit, noffset, NULL),
107 FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME)) != 0)
110 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
111 printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
112 fit_get_name(fit, noffset, NULL), image_name);
116 if (calculate_hash(data, size, algo, value, &value_len)) {
117 printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
118 algo, fit_get_name(fit, noffset, NULL), image_name);
122 if (fit_image_hash_set_value(fit, noffset, value, value_len)) {
123 printf("Can't set hash value for '%s' hash node in '%s' image node\n",
124 fit_get_name(fit, noffset, NULL), image_name);
132 * fit_image_set_hashes - calculate/set hashes for given component image node
133 * @fit: pointer to the FIT format image header
134 * @image_noffset: requested component image node
136 * fit_image_set_hashes() adds hash values for an component image node. All
137 * existing hash subnodes are checked, if algorithm property is set to one of
138 * the supported hash algorithms, hash value is computed and corresponding
139 * hash node property is set, for example:
141 * Input component image node structure:
143 * o image@1 (at image_noffset)
144 * | - data = [binary data]
148 * Output component image node structure:
150 * o image@1 (at image_noffset)
151 * | - data = [binary data]
154 * |- value = sha1(data)
160 int fit_image_set_hashes(void *fit, int image_noffset)
166 const char *image_name;
168 /* Get image data and data length */
169 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
170 printf("Can't get image data/size\n");
174 image_name = fit_get_name(fit, image_noffset, NULL);
176 /* Process all hash subnodes of the component image node */
177 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
178 (noffset >= 0) && (ndepth > 0);
179 noffset = fdt_next_node(fit, noffset, &ndepth)) {
181 /* Direct child node of the component image node */
182 if (fit_image_process_hash(fit, image_name, noffset,
192 * fit_image_hash_set_value - set hash value in requested has node
193 * @fit: pointer to the FIT format image header
194 * @noffset: hash node offset
195 * @value: hash value to be set
196 * @value_len: hash value length
198 * fit_image_hash_set_value() attempts to set hash value in a node at offset
199 * given and returns operation status to the caller.
205 int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
210 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
212 printf("Can't set hash '%s' property for '%s' node(%s)\n",
213 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),