xref: /OK3568_Linux_fs/u-boot/tools/image-host.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2013, Google Inc.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * (C) Copyright 2008 Semihalf
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * (C) Copyright 2000-2006
7*4882a593Smuzhiyun  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include "mkimage.h"
13*4882a593Smuzhiyun #include <bootm.h>
14*4882a593Smuzhiyun #include <image.h>
15*4882a593Smuzhiyun #include <version.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * fit_set_hash_value - set hash value in requested has node
19*4882a593Smuzhiyun  * @fit: pointer to the FIT format image header
20*4882a593Smuzhiyun  * @noffset: hash node offset
21*4882a593Smuzhiyun  * @value: hash value to be set
22*4882a593Smuzhiyun  * @value_len: hash value length
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * fit_set_hash_value() attempts to set hash value in a node at offset
25*4882a593Smuzhiyun  * given and returns operation status to the caller.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * returns
28*4882a593Smuzhiyun  *     0, on success
29*4882a593Smuzhiyun  *     -1, on failure
30*4882a593Smuzhiyun  */
fit_set_hash_value(void * fit,int noffset,uint8_t * value,int value_len)31*4882a593Smuzhiyun static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
32*4882a593Smuzhiyun 				int value_len)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun 	int ret;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
37*4882a593Smuzhiyun 	if (ret) {
38*4882a593Smuzhiyun 		printf("Can't set hash '%s' property for '%s' node(%s)\n",
39*4882a593Smuzhiyun 		       FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
40*4882a593Smuzhiyun 		       fdt_strerror(ret));
41*4882a593Smuzhiyun 		return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
42*4882a593Smuzhiyun 	}
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	return 0;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /**
48*4882a593Smuzhiyun  * fit_image_process_hash - Process a single subnode of the images/ node
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * Check each subnode and process accordingly. For hash nodes we generate
51*4882a593Smuzhiyun  * a hash of the supplised data and store it in the node.
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * @fit:	pointer to the FIT format image header
54*4882a593Smuzhiyun  * @image_name:	name of image being processes (used to display errors)
55*4882a593Smuzhiyun  * @noffset:	subnode offset
56*4882a593Smuzhiyun  * @data:	data to process
57*4882a593Smuzhiyun  * @size:	size of data in bytes
58*4882a593Smuzhiyun  * @return 0 if ok, -1 on error
59*4882a593Smuzhiyun  */
fit_image_process_hash(void * fit,const char * image_name,int noffset,const void * data,size_t size)60*4882a593Smuzhiyun static int fit_image_process_hash(void *fit, const char *image_name,
61*4882a593Smuzhiyun 		int noffset, const void *data, size_t size)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	uint8_t value[FIT_MAX_HASH_LEN];
64*4882a593Smuzhiyun 	const char *node_name;
65*4882a593Smuzhiyun 	int value_len;
66*4882a593Smuzhiyun 	char *algo;
67*4882a593Smuzhiyun 	int ret;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	node_name = fit_get_name(fit, noffset, NULL);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	if (fit_image_hash_get_algo(fit, noffset, &algo)) {
72*4882a593Smuzhiyun 		printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
73*4882a593Smuzhiyun 		       node_name, image_name);
74*4882a593Smuzhiyun 		return -ENOENT;
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	if (calculate_hash(data, size, algo, value, &value_len)) {
78*4882a593Smuzhiyun 		printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
79*4882a593Smuzhiyun 		       algo, node_name, image_name);
80*4882a593Smuzhiyun 		return -EPROTONOSUPPORT;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	ret = fit_set_hash_value(fit, noffset, value, value_len);
84*4882a593Smuzhiyun 	if (ret) {
85*4882a593Smuzhiyun 		printf("Can't set hash value for '%s' hash node in '%s' image node\n",
86*4882a593Smuzhiyun 		       node_name, image_name);
87*4882a593Smuzhiyun 		return ret;
88*4882a593Smuzhiyun 	}
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	return 0;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /**
94*4882a593Smuzhiyun  * fit_image_write_sig() - write the signature to a FIT
95*4882a593Smuzhiyun  *
96*4882a593Smuzhiyun  * This writes the signature and signer data to the FIT.
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * @fit: pointer to the FIT format image header
99*4882a593Smuzhiyun  * @noffset: hash node offset
100*4882a593Smuzhiyun  * @value: signature value to be set
101*4882a593Smuzhiyun  * @value_len: signature value length
102*4882a593Smuzhiyun  * @comment: Text comment to write (NULL for none)
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * returns
105*4882a593Smuzhiyun  *     0, on success
106*4882a593Smuzhiyun  *     -FDT_ERR_..., on failure
107*4882a593Smuzhiyun  */
fit_image_write_sig(void * fit,int noffset,uint8_t * value,int value_len,const char * comment,const char * region_prop,int region_proplen)108*4882a593Smuzhiyun static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
109*4882a593Smuzhiyun 		int value_len, const char *comment, const char *region_prop,
110*4882a593Smuzhiyun 		int region_proplen)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	int string_size;
113*4882a593Smuzhiyun 	int ret;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	/*
116*4882a593Smuzhiyun 	 * Get the current string size, before we update the FIT and add
117*4882a593Smuzhiyun 	 * more
118*4882a593Smuzhiyun 	 */
119*4882a593Smuzhiyun 	string_size = fdt_size_dt_strings(fit);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
122*4882a593Smuzhiyun 	if (!ret) {
123*4882a593Smuzhiyun 		ret = fdt_setprop_string(fit, noffset, "signer-name",
124*4882a593Smuzhiyun 					 "mkimage");
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 	if (!ret) {
127*4882a593Smuzhiyun 		ret = fdt_setprop_string(fit, noffset, "signer-version",
128*4882a593Smuzhiyun 				  PLAIN_VERSION);
129*4882a593Smuzhiyun 	}
130*4882a593Smuzhiyun 	if (comment && !ret)
131*4882a593Smuzhiyun 		ret = fdt_setprop_string(fit, noffset, "comment", comment);
132*4882a593Smuzhiyun 	if (!ret)
133*4882a593Smuzhiyun 		ret = fit_set_timestamp(fit, noffset, time(NULL));
134*4882a593Smuzhiyun 	if (region_prop && !ret) {
135*4882a593Smuzhiyun 		uint32_t strdata[2];
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 		ret = fdt_setprop(fit, noffset, "hashed-nodes",
138*4882a593Smuzhiyun 				   region_prop, region_proplen);
139*4882a593Smuzhiyun 		strdata[0] = 0;
140*4882a593Smuzhiyun 		strdata[1] = cpu_to_fdt32(string_size);
141*4882a593Smuzhiyun 		if (!ret) {
142*4882a593Smuzhiyun 			ret = fdt_setprop(fit, noffset, "hashed-strings",
143*4882a593Smuzhiyun 					  strdata, sizeof(strdata));
144*4882a593Smuzhiyun 		}
145*4882a593Smuzhiyun 	}
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	return ret;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
fit_image_setup_sig(struct image_sign_info * info,const char * keydir,void * fit,const char * image_name,int noffset,const char * require_keys,const char * engine_id)150*4882a593Smuzhiyun static int fit_image_setup_sig(struct image_sign_info *info,
151*4882a593Smuzhiyun 		const char *keydir, void *fit, const char *image_name,
152*4882a593Smuzhiyun 		int noffset, const char *require_keys, const char *engine_id)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	const char *node_name;
155*4882a593Smuzhiyun 	char *algo_name;
156*4882a593Smuzhiyun 	const char *padding_name;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	node_name = fit_get_name(fit, noffset, NULL);
159*4882a593Smuzhiyun 	if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
160*4882a593Smuzhiyun 		printf("Can't get algo property for '%s' signature node in '%s' image node\n",
161*4882a593Smuzhiyun 		       node_name, image_name);
162*4882a593Smuzhiyun 		return -1;
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	padding_name = fdt_getprop(fit, noffset, "padding", NULL);
166*4882a593Smuzhiyun 	if (!padding_name)
167*4882a593Smuzhiyun 		padding_name = "pkcs-1.5";
168*4882a593Smuzhiyun 	memset(info, '\0', sizeof(*info));
169*4882a593Smuzhiyun 	info->keydir = keydir;
170*4882a593Smuzhiyun 	info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
171*4882a593Smuzhiyun 	info->fit = fit;
172*4882a593Smuzhiyun 	info->node_offset = noffset;
173*4882a593Smuzhiyun 	info->name = algo_name;
174*4882a593Smuzhiyun 	info->checksum = image_get_checksum_algo(algo_name);
175*4882a593Smuzhiyun 	info->crypto = image_get_crypto_algo(algo_name);
176*4882a593Smuzhiyun 	info->padding = image_get_padding_algo(padding_name);
177*4882a593Smuzhiyun 	info->require_keys = require_keys;
178*4882a593Smuzhiyun 	info->engine_id = engine_id;
179*4882a593Smuzhiyun 	if (!info->checksum || !info->crypto) {
180*4882a593Smuzhiyun 		printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
181*4882a593Smuzhiyun 		       algo_name, node_name, image_name);
182*4882a593Smuzhiyun 		return -1;
183*4882a593Smuzhiyun 	}
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	return 0;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun /**
189*4882a593Smuzhiyun  * fit_image_process_sig- Process a single subnode of the images/ node
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * Check each subnode and process accordingly. For signature nodes we
192*4882a593Smuzhiyun  * generate a signed hash of the supplised data and store it in the node.
193*4882a593Smuzhiyun  *
194*4882a593Smuzhiyun  * @keydir:	Directory containing keys to use for signing
195*4882a593Smuzhiyun  * @keydest:	Destination FDT blob to write public keys into
196*4882a593Smuzhiyun  * @fit:	pointer to the FIT format image header
197*4882a593Smuzhiyun  * @image_name:	name of image being processes (used to display errors)
198*4882a593Smuzhiyun  * @noffset:	subnode offset
199*4882a593Smuzhiyun  * @data:	data to process
200*4882a593Smuzhiyun  * @size:	size of data in bytes
201*4882a593Smuzhiyun  * @comment:	Comment to add to signature nodes
202*4882a593Smuzhiyun  * @require_keys: Mark all keys as 'required'
203*4882a593Smuzhiyun  * @engine_id:	Engine to use for signing
204*4882a593Smuzhiyun  * @return 0 if ok, -1 on error
205*4882a593Smuzhiyun  */
fit_image_process_sig(const char * keydir,void * keydest,void * fit,const char * image_name,int noffset,const void * data,size_t size,const char * comment,int require_keys,const char * engine_id)206*4882a593Smuzhiyun static int fit_image_process_sig(const char *keydir, void *keydest,
207*4882a593Smuzhiyun 		void *fit, const char *image_name,
208*4882a593Smuzhiyun 		int noffset, const void *data, size_t size,
209*4882a593Smuzhiyun 		const char *comment, int require_keys, const char *engine_id)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun 	struct image_sign_info info;
212*4882a593Smuzhiyun 	struct image_region region;
213*4882a593Smuzhiyun 	const char *node_name;
214*4882a593Smuzhiyun 	uint8_t *value;
215*4882a593Smuzhiyun 	uint value_len;
216*4882a593Smuzhiyun 	int ret;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset,
219*4882a593Smuzhiyun 				require_keys ? "image" : NULL, engine_id))
220*4882a593Smuzhiyun 		return -1;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	node_name = fit_get_name(fit, noffset, NULL);
223*4882a593Smuzhiyun 	region.data = data;
224*4882a593Smuzhiyun 	region.size = size;
225*4882a593Smuzhiyun 	ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
226*4882a593Smuzhiyun 	if (ret) {
227*4882a593Smuzhiyun 		printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
228*4882a593Smuzhiyun 		       node_name, image_name, ret);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 		/* We allow keys to be missing */
231*4882a593Smuzhiyun 		if (ret == -ENOENT)
232*4882a593Smuzhiyun 			return 0;
233*4882a593Smuzhiyun 		return -1;
234*4882a593Smuzhiyun 	}
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
237*4882a593Smuzhiyun 			NULL, 0);
238*4882a593Smuzhiyun 	if (ret) {
239*4882a593Smuzhiyun 		if (ret == -FDT_ERR_NOSPACE)
240*4882a593Smuzhiyun 			return -ENOSPC;
241*4882a593Smuzhiyun 		printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
242*4882a593Smuzhiyun 		       node_name, image_name, fdt_strerror(ret));
243*4882a593Smuzhiyun 		return -1;
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 	free(value);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/* Get keyname again, as FDT has changed and invalidated our pointer */
248*4882a593Smuzhiyun 	info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	if (keydest)
251*4882a593Smuzhiyun 		ret = info.crypto->add_verify_data(&info, keydest);
252*4882a593Smuzhiyun 	else
253*4882a593Smuzhiyun 		return -1;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	/*
256*4882a593Smuzhiyun 	 * Write the public key into the supplied FDT file; this might fail
257*4882a593Smuzhiyun 	 * several times, since we try signing with successively increasing
258*4882a593Smuzhiyun 	 * size values
259*4882a593Smuzhiyun 	 */
260*4882a593Smuzhiyun 	if (keydest && ret)
261*4882a593Smuzhiyun 		return ret;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	return 0;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun /**
267*4882a593Smuzhiyun  * fit_image_add_verification_data() - calculate/set verig. data for image node
268*4882a593Smuzhiyun  *
269*4882a593Smuzhiyun  * This adds hash and signature values for an component image node.
270*4882a593Smuzhiyun  *
271*4882a593Smuzhiyun  * All existing hash subnodes are checked, if algorithm property is set to
272*4882a593Smuzhiyun  * one of the supported hash algorithms, hash value is computed and
273*4882a593Smuzhiyun  * corresponding hash node property is set, for example:
274*4882a593Smuzhiyun  *
275*4882a593Smuzhiyun  * Input component image node structure:
276*4882a593Smuzhiyun  *
277*4882a593Smuzhiyun  * o image@1 (at image_noffset)
278*4882a593Smuzhiyun  *   | - data = [binary data]
279*4882a593Smuzhiyun  *   o hash@1
280*4882a593Smuzhiyun  *     |- algo = "sha1"
281*4882a593Smuzhiyun  *
282*4882a593Smuzhiyun  * Output component image node structure:
283*4882a593Smuzhiyun  *
284*4882a593Smuzhiyun  * o image@1 (at image_noffset)
285*4882a593Smuzhiyun  *   | - data = [binary data]
286*4882a593Smuzhiyun  *   o hash@1
287*4882a593Smuzhiyun  *     |- algo = "sha1"
288*4882a593Smuzhiyun  *     |- value = sha1(data)
289*4882a593Smuzhiyun  *
290*4882a593Smuzhiyun  * For signature details, please see doc/uImage.FIT/signature.txt
291*4882a593Smuzhiyun  *
292*4882a593Smuzhiyun  * @keydir	Directory containing *.key and *.crt files (or NULL)
293*4882a593Smuzhiyun  * @keydest	FDT Blob to write public keys into (NULL if none)
294*4882a593Smuzhiyun  * @fit:	Pointer to the FIT format image header
295*4882a593Smuzhiyun  * @image_noffset: Requested component image node
296*4882a593Smuzhiyun  * @comment:	Comment to add to signature nodes
297*4882a593Smuzhiyun  * @require_keys: Mark all keys as 'required'
298*4882a593Smuzhiyun  * @engine_id:	Engine to use for signing
299*4882a593Smuzhiyun  * @return: 0 on success, <0 on failure
300*4882a593Smuzhiyun  */
fit_image_add_verification_data(const char * keydir,void * keydest,void * fit,int image_noffset,const char * comment,int require_keys,const char * engine_id)301*4882a593Smuzhiyun int fit_image_add_verification_data(const char *keydir, void *keydest,
302*4882a593Smuzhiyun 		void *fit, int image_noffset, const char *comment,
303*4882a593Smuzhiyun 		int require_keys, const char *engine_id)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	const char *image_name;
306*4882a593Smuzhiyun 	const void *data;
307*4882a593Smuzhiyun 	size_t size;
308*4882a593Smuzhiyun 	int noffset;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	/* Get image data and data length */
311*4882a593Smuzhiyun 	if (fit_image_get_data(fit, image_noffset, &data, &size)) {
312*4882a593Smuzhiyun 		printf("Can't get image data/size\n");
313*4882a593Smuzhiyun 		return -1;
314*4882a593Smuzhiyun 	}
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	image_name = fit_get_name(fit, image_noffset, NULL);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	/* Process all hash subnodes of the component image node */
319*4882a593Smuzhiyun 	for (noffset = fdt_first_subnode(fit, image_noffset);
320*4882a593Smuzhiyun 	     noffset >= 0;
321*4882a593Smuzhiyun 	     noffset = fdt_next_subnode(fit, noffset)) {
322*4882a593Smuzhiyun 		const char *node_name;
323*4882a593Smuzhiyun 		int ret = 0;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 		/*
326*4882a593Smuzhiyun 		 * Check subnode name, must be equal to "hash" or "signature".
327*4882a593Smuzhiyun 		 * Multiple hash nodes require unique unit node
328*4882a593Smuzhiyun 		 * names, e.g. hash@1, hash@2, signature@1, etc.
329*4882a593Smuzhiyun 		 */
330*4882a593Smuzhiyun 		node_name = fit_get_name(fit, noffset, NULL);
331*4882a593Smuzhiyun 		if (!strncmp(node_name, FIT_HASH_NODENAME,
332*4882a593Smuzhiyun 			     strlen(FIT_HASH_NODENAME))) {
333*4882a593Smuzhiyun 			ret = fit_image_process_hash(fit, image_name, noffset,
334*4882a593Smuzhiyun 						data, size);
335*4882a593Smuzhiyun 		} else if (IMAGE_ENABLE_SIGN && keydir &&
336*4882a593Smuzhiyun 			   !strncmp(node_name, FIT_SIG_NODENAME,
337*4882a593Smuzhiyun 				strlen(FIT_SIG_NODENAME))) {
338*4882a593Smuzhiyun 			ret = fit_image_process_sig(keydir, keydest,
339*4882a593Smuzhiyun 				fit, image_name, noffset, data, size,
340*4882a593Smuzhiyun 				comment, require_keys, engine_id);
341*4882a593Smuzhiyun 		}
342*4882a593Smuzhiyun 		if (ret)
343*4882a593Smuzhiyun 			return ret;
344*4882a593Smuzhiyun 	}
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	return 0;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun struct strlist {
350*4882a593Smuzhiyun 	int count;
351*4882a593Smuzhiyun 	char **strings;
352*4882a593Smuzhiyun };
353*4882a593Smuzhiyun 
strlist_init(struct strlist * list)354*4882a593Smuzhiyun static void strlist_init(struct strlist *list)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun 	memset(list, '\0', sizeof(*list));
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun 
strlist_free(struct strlist * list)359*4882a593Smuzhiyun static void strlist_free(struct strlist *list)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun 	int i;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	for (i = 0; i < list->count; i++)
364*4882a593Smuzhiyun 		free(list->strings[i]);
365*4882a593Smuzhiyun 	free(list->strings);
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun 
strlist_add(struct strlist * list,const char * str)368*4882a593Smuzhiyun static int strlist_add(struct strlist *list, const char *str)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	char *dup;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	dup = strdup(str);
373*4882a593Smuzhiyun 	list->strings = realloc(list->strings,
374*4882a593Smuzhiyun 				(list->count + 1) * sizeof(char *));
375*4882a593Smuzhiyun 	if (!list || !str)
376*4882a593Smuzhiyun 		return -1;
377*4882a593Smuzhiyun 	list->strings[list->count++] = dup;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	return 0;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun 
fit_config_get_image_list(void * fit,int noffset,int * lenp,int * allow_missingp)382*4882a593Smuzhiyun static const char *fit_config_get_image_list(void *fit, int noffset,
383*4882a593Smuzhiyun 		int *lenp, int *allow_missingp)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun 	static const char default_list[] = FIT_KERNEL_PROP "\0"
386*4882a593Smuzhiyun 			FIT_FDT_PROP;
387*4882a593Smuzhiyun 	const char *prop;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	/* If there is an "image" property, use that */
390*4882a593Smuzhiyun 	prop = fdt_getprop(fit, noffset, "sign-images", lenp);
391*4882a593Smuzhiyun 	if (prop) {
392*4882a593Smuzhiyun 		*allow_missingp = 0;
393*4882a593Smuzhiyun 		return *lenp ? prop : NULL;
394*4882a593Smuzhiyun 	}
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	/* Default image list */
397*4882a593Smuzhiyun 	*allow_missingp = 1;
398*4882a593Smuzhiyun 	*lenp = sizeof(default_list);
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	return default_list;
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun 
fit_config_get_hash_list(void * fit,int conf_noffset,int sig_offset,struct strlist * node_inc)403*4882a593Smuzhiyun static int fit_config_get_hash_list(void *fit, int conf_noffset,
404*4882a593Smuzhiyun 				    int sig_offset, struct strlist *node_inc)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun 	int allow_missing;
407*4882a593Smuzhiyun 	const char *prop, *iname, *end;
408*4882a593Smuzhiyun 	const char *conf_name, *sig_name;
409*4882a593Smuzhiyun 	char name[200], path[200];
410*4882a593Smuzhiyun 	int image_count;
411*4882a593Smuzhiyun 	int ret, len;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	conf_name = fit_get_name(fit, conf_noffset, NULL);
414*4882a593Smuzhiyun 	sig_name = fit_get_name(fit, sig_offset, NULL);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	/*
417*4882a593Smuzhiyun 	 * Build a list of nodes we need to hash. We always need the root
418*4882a593Smuzhiyun 	 * node and the configuration.
419*4882a593Smuzhiyun 	 */
420*4882a593Smuzhiyun 	strlist_init(node_inc);
421*4882a593Smuzhiyun 	snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
422*4882a593Smuzhiyun 	if (strlist_add(node_inc, "/") ||
423*4882a593Smuzhiyun 	    strlist_add(node_inc, FIT_CONFS_PATH) ||
424*4882a593Smuzhiyun 	    strlist_add(node_inc, name))
425*4882a593Smuzhiyun 		goto err_mem;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	/* Get a list of images that we intend to sign */
428*4882a593Smuzhiyun 	prop = fit_config_get_image_list(fit, sig_offset, &len,
429*4882a593Smuzhiyun 					&allow_missing);
430*4882a593Smuzhiyun 	if (!prop)
431*4882a593Smuzhiyun 		return 0;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	/* Locate the images */
434*4882a593Smuzhiyun 	end = prop + len;
435*4882a593Smuzhiyun 	image_count = 0;
436*4882a593Smuzhiyun 	for (iname = prop; iname < end; iname += strlen(iname) + 1) {
437*4882a593Smuzhiyun 		int noffset;
438*4882a593Smuzhiyun 		int image_noffset;
439*4882a593Smuzhiyun 		int hash_count;
440*4882a593Smuzhiyun 		int i, max_index;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 		max_index = fdt_stringlist_count(fit, conf_noffset, iname);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 		for (i = 0; i < max_index; i++) {
445*4882a593Smuzhiyun 			image_noffset =
446*4882a593Smuzhiyun 				fit_conf_get_prop_node_index(fit, conf_noffset,
447*4882a593Smuzhiyun 							     iname, i);
448*4882a593Smuzhiyun 			if (image_noffset < 0) {
449*4882a593Smuzhiyun 				if (i > 0)
450*4882a593Smuzhiyun 					break;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 				printf("Failed to find image '%s' in  configuration '%s/%s'\n",
453*4882a593Smuzhiyun 				       iname, conf_name, sig_name);
454*4882a593Smuzhiyun 				if (allow_missing)
455*4882a593Smuzhiyun 					continue;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 				return -ENOENT;
458*4882a593Smuzhiyun 			}
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 			ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
461*4882a593Smuzhiyun 			if (ret < 0)
462*4882a593Smuzhiyun 				goto err_path;
463*4882a593Smuzhiyun 			if (strlist_add(node_inc, path))
464*4882a593Smuzhiyun 				goto err_mem;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 			snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH,
467*4882a593Smuzhiyun 				 conf_name);
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 			/* Add all this image's hashes */
470*4882a593Smuzhiyun 			hash_count = 0;
471*4882a593Smuzhiyun 			for (noffset = fdt_first_subnode(fit, image_noffset);
472*4882a593Smuzhiyun 			     noffset >= 0;
473*4882a593Smuzhiyun 			     noffset = fdt_next_subnode(fit, noffset)) {
474*4882a593Smuzhiyun 				const char *name = fit_get_name(fit, noffset, NULL);
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 				if (strncmp(name, FIT_HASH_NODENAME,
477*4882a593Smuzhiyun 					    strlen(FIT_HASH_NODENAME)))
478*4882a593Smuzhiyun 					continue;
479*4882a593Smuzhiyun 				ret = fdt_get_path(fit, noffset, path, sizeof(path));
480*4882a593Smuzhiyun 				if (ret < 0)
481*4882a593Smuzhiyun 					goto err_path;
482*4882a593Smuzhiyun 				if (strlist_add(node_inc, path))
483*4882a593Smuzhiyun 					goto err_mem;
484*4882a593Smuzhiyun 				hash_count++;
485*4882a593Smuzhiyun 			}
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 			if (!hash_count) {
488*4882a593Smuzhiyun 				printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
489*4882a593Smuzhiyun 				       conf_name, sig_name, iname);
490*4882a593Smuzhiyun 				return -ENOMSG;
491*4882a593Smuzhiyun 			}
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 			image_count++;
494*4882a593Smuzhiyun 		}
495*4882a593Smuzhiyun 	}
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	if (!image_count) {
498*4882a593Smuzhiyun 		printf("Failed to find any images for configuration '%s/%s'\n",
499*4882a593Smuzhiyun 		       conf_name, sig_name);
500*4882a593Smuzhiyun 		return -ENOMSG;
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	return 0;
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun err_mem:
506*4882a593Smuzhiyun 	printf("Out of memory processing configuration '%s/%s'\n", conf_name,
507*4882a593Smuzhiyun 	       sig_name);
508*4882a593Smuzhiyun 	return -ENOMEM;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun err_path:
511*4882a593Smuzhiyun 	printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
512*4882a593Smuzhiyun 	       iname, conf_name, sig_name, fdt_strerror(ret));
513*4882a593Smuzhiyun 	return -ENOENT;
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun 
fit_config_get_data(void * fit,int conf_noffset,int noffset,struct image_region ** regionp,int * region_countp,char ** region_propp,int * region_proplen)516*4882a593Smuzhiyun static int fit_config_get_data(void *fit, int conf_noffset, int noffset,
517*4882a593Smuzhiyun 		struct image_region **regionp, int *region_countp,
518*4882a593Smuzhiyun 		char **region_propp, int *region_proplen)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun 	char * const exc_prop[] = {"data"};
521*4882a593Smuzhiyun 	struct strlist node_inc;
522*4882a593Smuzhiyun 	struct image_region *region;
523*4882a593Smuzhiyun 	struct fdt_region fdt_regions[100];
524*4882a593Smuzhiyun 	const char *conf_name, *sig_name;
525*4882a593Smuzhiyun 	char path[200];
526*4882a593Smuzhiyun 	int count, i;
527*4882a593Smuzhiyun 	char *region_prop;
528*4882a593Smuzhiyun 	int ret, len;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	conf_name = fit_get_name(fit, conf_noffset, NULL);
531*4882a593Smuzhiyun 	sig_name = fit_get_name(fit, noffset, NULL);
532*4882a593Smuzhiyun 	debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	/* Get a list of nodes we want to hash */
535*4882a593Smuzhiyun 	ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc);
536*4882a593Smuzhiyun 	if (ret)
537*4882a593Smuzhiyun 		return ret;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	/* Get a list of regions to hash */
540*4882a593Smuzhiyun 	count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
541*4882a593Smuzhiyun 			exc_prop, ARRAY_SIZE(exc_prop),
542*4882a593Smuzhiyun 			fdt_regions, ARRAY_SIZE(fdt_regions),
543*4882a593Smuzhiyun 			path, sizeof(path), 1);
544*4882a593Smuzhiyun 	if (count < 0) {
545*4882a593Smuzhiyun 		printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
546*4882a593Smuzhiyun 		       sig_name, fdt_strerror(ret));
547*4882a593Smuzhiyun 		return -EIO;
548*4882a593Smuzhiyun 	}
549*4882a593Smuzhiyun 	if (count == 0) {
550*4882a593Smuzhiyun 		printf("No data to hash for configuration '%s/%s': %s\n",
551*4882a593Smuzhiyun 		       conf_name, sig_name, fdt_strerror(ret));
552*4882a593Smuzhiyun 		return -EINVAL;
553*4882a593Smuzhiyun 	}
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	/* Build our list of data blocks */
556*4882a593Smuzhiyun 	region = fit_region_make_list(fit, fdt_regions, count, NULL);
557*4882a593Smuzhiyun 	if (!region) {
558*4882a593Smuzhiyun 		printf("Out of memory hashing configuration '%s/%s'\n",
559*4882a593Smuzhiyun 		       conf_name, sig_name);
560*4882a593Smuzhiyun 		return -ENOMEM;
561*4882a593Smuzhiyun 	}
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	/* Create a list of all hashed properties */
564*4882a593Smuzhiyun 	debug("Hash nodes:\n");
565*4882a593Smuzhiyun 	for (i = len = 0; i < node_inc.count; i++) {
566*4882a593Smuzhiyun 		debug("   %s\n", node_inc.strings[i]);
567*4882a593Smuzhiyun 		len += strlen(node_inc.strings[i]) + 1;
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 	region_prop = malloc(len);
570*4882a593Smuzhiyun 	if (!region_prop) {
571*4882a593Smuzhiyun 		printf("Out of memory setting up regions for configuration '%s/%s'\n",
572*4882a593Smuzhiyun 		       conf_name, sig_name);
573*4882a593Smuzhiyun 		return -ENOMEM;
574*4882a593Smuzhiyun 	}
575*4882a593Smuzhiyun 	for (i = len = 0; i < node_inc.count;
576*4882a593Smuzhiyun 	     len += strlen(node_inc.strings[i]) + 1, i++)
577*4882a593Smuzhiyun 		strcpy(region_prop + len, node_inc.strings[i]);
578*4882a593Smuzhiyun 	strlist_free(&node_inc);
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	*region_countp = count;
581*4882a593Smuzhiyun 	*regionp = region;
582*4882a593Smuzhiyun 	*region_propp = region_prop;
583*4882a593Smuzhiyun 	*region_proplen = len;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	return 0;
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun 
fit_config_process_sig(const char * keydir,void * keydest,void * fit,const char * conf_name,int conf_noffset,int noffset,const char * comment,int require_keys,const char * engine_id)588*4882a593Smuzhiyun static int fit_config_process_sig(const char *keydir, void *keydest,
589*4882a593Smuzhiyun 		void *fit, const char *conf_name, int conf_noffset,
590*4882a593Smuzhiyun 		int noffset, const char *comment, int require_keys,
591*4882a593Smuzhiyun 		const char *engine_id)
592*4882a593Smuzhiyun {
593*4882a593Smuzhiyun 	struct image_sign_info info;
594*4882a593Smuzhiyun 	const char *node_name;
595*4882a593Smuzhiyun 	struct image_region *region;
596*4882a593Smuzhiyun 	char *region_prop;
597*4882a593Smuzhiyun 	int region_proplen;
598*4882a593Smuzhiyun 	int region_count;
599*4882a593Smuzhiyun 	uint8_t *value;
600*4882a593Smuzhiyun 	uint value_len;
601*4882a593Smuzhiyun 	int ret;
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	node_name = fit_get_name(fit, noffset, NULL);
604*4882a593Smuzhiyun 	if (fit_config_get_data(fit, conf_noffset, noffset, &region,
605*4882a593Smuzhiyun 				&region_count, &region_prop, &region_proplen))
606*4882a593Smuzhiyun 		return -1;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset,
609*4882a593Smuzhiyun 				require_keys ? "conf" : NULL, engine_id))
610*4882a593Smuzhiyun 		return -1;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	ret = info.crypto->sign(&info, region, region_count, &value,
613*4882a593Smuzhiyun 				&value_len);
614*4882a593Smuzhiyun 	free(region);
615*4882a593Smuzhiyun 	if (ret) {
616*4882a593Smuzhiyun 		printf("Failed to sign '%s' signature node in '%s' conf node\n",
617*4882a593Smuzhiyun 		       node_name, conf_name);
618*4882a593Smuzhiyun 		return -1;
619*4882a593Smuzhiyun 	}
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
622*4882a593Smuzhiyun 				region_prop, region_proplen);
623*4882a593Smuzhiyun 	if (ret) {
624*4882a593Smuzhiyun 		if (ret == -FDT_ERR_NOSPACE)
625*4882a593Smuzhiyun 			return -ENOSPC;
626*4882a593Smuzhiyun 		printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
627*4882a593Smuzhiyun 		       node_name, conf_name, fdt_strerror(ret));
628*4882a593Smuzhiyun 		return -1;
629*4882a593Smuzhiyun 	}
630*4882a593Smuzhiyun 	free(value);
631*4882a593Smuzhiyun 	free(region_prop);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	/* Get keyname again, as FDT has changed and invalidated our pointer */
634*4882a593Smuzhiyun 	info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	/* Write the public key into the supplied FDT file */
637*4882a593Smuzhiyun 	if (keydest) {
638*4882a593Smuzhiyun 		ret = info.crypto->add_verify_data(&info, keydest);
639*4882a593Smuzhiyun 		if (ret == -ENOSPC)
640*4882a593Smuzhiyun 			return -ENOSPC;
641*4882a593Smuzhiyun 		if (ret) {
642*4882a593Smuzhiyun 			printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
643*4882a593Smuzhiyun 			       node_name, conf_name);
644*4882a593Smuzhiyun 		}
645*4882a593Smuzhiyun 		return ret;
646*4882a593Smuzhiyun 	}
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	return 0;
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun 
fit_config_add_verification_data(const char * keydir,void * keydest,void * fit,int conf_noffset,const char * comment,int require_keys,const char * engine_id)651*4882a593Smuzhiyun static int fit_config_add_verification_data(const char *keydir, void *keydest,
652*4882a593Smuzhiyun 		void *fit, int conf_noffset, const char *comment,
653*4882a593Smuzhiyun 		int require_keys, const char *engine_id)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun 	const char *conf_name;
656*4882a593Smuzhiyun 	int noffset;
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 	conf_name = fit_get_name(fit, conf_noffset, NULL);
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	/* Process all hash subnodes of the configuration node */
661*4882a593Smuzhiyun 	for (noffset = fdt_first_subnode(fit, conf_noffset);
662*4882a593Smuzhiyun 	     noffset >= 0;
663*4882a593Smuzhiyun 	     noffset = fdt_next_subnode(fit, noffset)) {
664*4882a593Smuzhiyun 		const char *node_name;
665*4882a593Smuzhiyun 		int ret = 0;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 		node_name = fit_get_name(fit, noffset, NULL);
668*4882a593Smuzhiyun 		if (!strncmp(node_name, FIT_SIG_NODENAME,
669*4882a593Smuzhiyun 			     strlen(FIT_SIG_NODENAME))) {
670*4882a593Smuzhiyun 			ret = fit_config_process_sig(keydir, keydest,
671*4882a593Smuzhiyun 				fit, conf_name, conf_noffset, noffset, comment,
672*4882a593Smuzhiyun 				require_keys, engine_id);
673*4882a593Smuzhiyun 		}
674*4882a593Smuzhiyun 		if (ret)
675*4882a593Smuzhiyun 			return ret;
676*4882a593Smuzhiyun 	}
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	return 0;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun 
fit_add_verification_data(const char * keydir,void * keydest,void * fit,const char * comment,int require_keys,const char * engine_id)681*4882a593Smuzhiyun int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
682*4882a593Smuzhiyun 			      const char *comment, int require_keys,
683*4882a593Smuzhiyun 			      const char *engine_id)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	int images_noffset, confs_noffset;
686*4882a593Smuzhiyun 	int noffset;
687*4882a593Smuzhiyun 	int ret;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	/* Find images parent node offset */
690*4882a593Smuzhiyun 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
691*4882a593Smuzhiyun 	if (images_noffset < 0) {
692*4882a593Smuzhiyun 		printf("Can't find images parent node '%s' (%s)\n",
693*4882a593Smuzhiyun 		       FIT_IMAGES_PATH, fdt_strerror(images_noffset));
694*4882a593Smuzhiyun 		return images_noffset;
695*4882a593Smuzhiyun 	}
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	/* Process its subnodes, print out component images details */
698*4882a593Smuzhiyun 	for (noffset = fdt_first_subnode(fit, images_noffset);
699*4882a593Smuzhiyun 	     noffset >= 0;
700*4882a593Smuzhiyun 	     noffset = fdt_next_subnode(fit, noffset)) {
701*4882a593Smuzhiyun 		/*
702*4882a593Smuzhiyun 		 * Direct child node of the images parent node,
703*4882a593Smuzhiyun 		 * i.e. component image node.
704*4882a593Smuzhiyun 		 */
705*4882a593Smuzhiyun 		ret = fit_image_add_verification_data(keydir, keydest,
706*4882a593Smuzhiyun 				fit, noffset, comment, require_keys, engine_id);
707*4882a593Smuzhiyun 		if (ret)
708*4882a593Smuzhiyun 			return ret;
709*4882a593Smuzhiyun 	}
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	/* If there are no keys, we can't sign configurations */
712*4882a593Smuzhiyun 	if (!IMAGE_ENABLE_SIGN || !keydir)
713*4882a593Smuzhiyun 		return 0;
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	/* Find configurations parent node offset */
716*4882a593Smuzhiyun 	confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
717*4882a593Smuzhiyun 	if (confs_noffset < 0) {
718*4882a593Smuzhiyun 		printf("Can't find images parent node '%s' (%s)\n",
719*4882a593Smuzhiyun 		       FIT_CONFS_PATH, fdt_strerror(confs_noffset));
720*4882a593Smuzhiyun 		return -ENOENT;
721*4882a593Smuzhiyun 	}
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	/* Process its subnodes, print out component images details */
724*4882a593Smuzhiyun 	for (noffset = fdt_first_subnode(fit, confs_noffset);
725*4882a593Smuzhiyun 	     noffset >= 0;
726*4882a593Smuzhiyun 	     noffset = fdt_next_subnode(fit, noffset)) {
727*4882a593Smuzhiyun 		ret = fit_config_add_verification_data(keydir, keydest,
728*4882a593Smuzhiyun 						       fit, noffset, comment,
729*4882a593Smuzhiyun 						       require_keys,
730*4882a593Smuzhiyun 						       engine_id);
731*4882a593Smuzhiyun 		if (ret)
732*4882a593Smuzhiyun 			return ret;
733*4882a593Smuzhiyun 	}
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	return 0;
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun #ifdef CONFIG_FIT_SIGNATURE
fit_check_sign(const void * fit,const void * key,int is_spl)739*4882a593Smuzhiyun int fit_check_sign(const void *fit, const void *key, int is_spl)
740*4882a593Smuzhiyun {
741*4882a593Smuzhiyun 	int cfg_noffset;
742*4882a593Smuzhiyun 	int ret;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	cfg_noffset = fit_conf_get_node(fit, NULL);
745*4882a593Smuzhiyun 	if (!cfg_noffset)
746*4882a593Smuzhiyun 		return -1;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	printf("Verifying Hash Integrity ... ");
749*4882a593Smuzhiyun 	ret = fit_config_verify(fit, cfg_noffset);
750*4882a593Smuzhiyun 	if (ret)
751*4882a593Smuzhiyun 		return ret;
752*4882a593Smuzhiyun 	ret = bootm_host_load_images(fit, cfg_noffset, is_spl);
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	return ret;
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun #endif
757