xref: /rk3399_rockchip-uboot/common/image-fit.c (revision c48a3a80d2169914a6b90ad1a3895e69b7797cae)
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  * SPDX-License-Identifier:	GPL-2.0+
10  */
11 
12 #ifdef USE_HOSTCC
13 #include "mkimage.h"
14 #include <time.h>
15 #else
16 #include <linux/compiler.h>
17 #include <linux/kconfig.h>
18 #include <common.h>
19 #include <errno.h>
20 #include <mapmem.h>
21 #include <asm/io.h>
22 #include <malloc.h>
23 #include <crypto.h>
24 
25 DECLARE_GLOBAL_DATA_PTR;
26 #endif /* !USE_HOSTCC*/
27 
28 #include <image.h>
29 #include <bootstage.h>
30 #include <u-boot/crc.h>
31 #include <u-boot/md5.h>
32 #include <u-boot/sha1.h>
33 #include <u-boot/sha256.h>
34 
35 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
36 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
37 
38 /*****************************************************************************/
39 /* New uImage format routines */
40 /*****************************************************************************/
41 #ifndef USE_HOSTCC
42 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
43 		ulong *addr, const char **name)
44 {
45 	const char *sep;
46 
47 	*addr = addr_curr;
48 	*name = NULL;
49 
50 	sep = strchr(spec, sepc);
51 	if (sep) {
52 		if (sep - spec > 0)
53 			*addr = simple_strtoul(spec, NULL, 16);
54 
55 		*name = sep + 1;
56 		return 1;
57 	}
58 
59 	return 0;
60 }
61 
62 /**
63  * fit_parse_conf - parse FIT configuration spec
64  * @spec: input string, containing configuration spec
65  * @add_curr: current image address (to be used as a possible default)
66  * @addr: pointer to a ulong variable, will hold FIT image address of a given
67  * configuration
68  * @conf_name double pointer to a char, will hold pointer to a configuration
69  * unit name
70  *
71  * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
72  * where <addr> is a FIT image address that contains configuration
73  * with a <conf> unit name.
74  *
75  * Address part is optional, and if omitted default add_curr will
76  * be used instead.
77  *
78  * returns:
79  *     1 if spec is a valid configuration string,
80  *     addr and conf_name are set accordingly
81  *     0 otherwise
82  */
83 int fit_parse_conf(const char *spec, ulong addr_curr,
84 		ulong *addr, const char **conf_name)
85 {
86 	return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
87 }
88 
89 /**
90  * fit_parse_subimage - parse FIT subimage spec
91  * @spec: input string, containing subimage spec
92  * @add_curr: current image address (to be used as a possible default)
93  * @addr: pointer to a ulong variable, will hold FIT image address of a given
94  * subimage
95  * @image_name: double pointer to a char, will hold pointer to a subimage name
96  *
97  * fit_parse_subimage() expects subimage spec in the form of
98  * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
99  * subimage with a <subimg> unit name.
100  *
101  * Address part is optional, and if omitted default add_curr will
102  * be used instead.
103  *
104  * returns:
105  *     1 if spec is a valid subimage string,
106  *     addr and image_name are set accordingly
107  *     0 otherwise
108  */
109 int fit_parse_subimage(const char *spec, ulong addr_curr,
110 		ulong *addr, const char **image_name)
111 {
112 	return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
113 }
114 #endif /* !USE_HOSTCC */
115 
116 static void fit_get_debug(const void *fit, int noffset,
117 		char *prop_name, int err)
118 {
119 	debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
120 	      prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
121 	      fdt_strerror(err));
122 }
123 
124 /**
125  * fit_get_subimage_count - get component (sub-image) count
126  * @fit: pointer to the FIT format image header
127  * @images_noffset: offset of images node
128  *
129  * returns:
130  *     number of image components
131  */
132 int fit_get_subimage_count(const void *fit, int images_noffset)
133 {
134 	int noffset;
135 	int ndepth;
136 	int count = 0;
137 
138 	/* Process its subnodes, print out component images details */
139 	for (ndepth = 0, count = 0,
140 		noffset = fdt_next_node(fit, images_noffset, &ndepth);
141 	     (noffset >= 0) && (ndepth > 0);
142 	     noffset = fdt_next_node(fit, noffset, &ndepth)) {
143 		if (ndepth == 1) {
144 			count++;
145 		}
146 	}
147 
148 	return count;
149 }
150 
151 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
152 /**
153  * fit_print_contents - prints out the contents of the FIT format image
154  * @fit: pointer to the FIT format image header
155  * @p: pointer to prefix string
156  *
157  * fit_print_contents() formats a multi line FIT image contents description.
158  * The routine prints out FIT image properties (root node level) followed by
159  * the details of each component image.
160  *
161  * returns:
162  *     no returned results
163  */
164 void fit_print_contents(const void *fit)
165 {
166 	char *desc;
167 	char *uname;
168 	int images_noffset;
169 	int confs_noffset;
170 	int noffset;
171 	int ndepth;
172 	int count = 0;
173 	int ret;
174 	const char *p;
175 	time_t timestamp;
176 
177 	/* Indent string is defined in header image.h */
178 	p = IMAGE_INDENT_STRING;
179 
180 	/* Root node properties */
181 	ret = fit_get_desc(fit, 0, &desc);
182 	printf("%sFIT description: ", p);
183 	if (ret)
184 		printf("unavailable\n");
185 	else
186 		printf("%s\n", desc);
187 
188 	if (IMAGE_ENABLE_TIMESTAMP) {
189 		ret = fit_get_timestamp(fit, 0, &timestamp);
190 		printf("%sCreated:         ", p);
191 		if (ret)
192 			printf("unavailable\n");
193 		else
194 			genimg_print_time(timestamp);
195 	}
196 
197 	/* Find images parent node offset */
198 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
199 	if (images_noffset < 0) {
200 		printf("Can't find images parent node '%s' (%s)\n",
201 		       FIT_IMAGES_PATH, fdt_strerror(images_noffset));
202 		return;
203 	}
204 
205 	/* Process its subnodes, print out component images details */
206 	for (ndepth = 0, count = 0,
207 		noffset = fdt_next_node(fit, images_noffset, &ndepth);
208 	     (noffset >= 0) && (ndepth > 0);
209 	     noffset = fdt_next_node(fit, noffset, &ndepth)) {
210 		if (ndepth == 1) {
211 			/*
212 			 * Direct child node of the images parent node,
213 			 * i.e. component image node.
214 			 */
215 			printf("%s Image %u (%s)\n", p, count++,
216 			       fit_get_name(fit, noffset, NULL));
217 
218 			fit_image_print(fit, noffset, p);
219 		}
220 	}
221 
222 	/* Find configurations parent node offset */
223 	confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
224 	if (confs_noffset < 0) {
225 		debug("Can't get configurations parent node '%s' (%s)\n",
226 		      FIT_CONFS_PATH, fdt_strerror(confs_noffset));
227 		return;
228 	}
229 
230 	/* get default configuration unit name from default property */
231 	uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
232 	if (uname)
233 		printf("%s Default Configuration: '%s'\n", p, uname);
234 
235 	/* Process its subnodes, print out configurations details */
236 	for (ndepth = 0, count = 0,
237 		noffset = fdt_next_node(fit, confs_noffset, &ndepth);
238 	     (noffset >= 0) && (ndepth > 0);
239 	     noffset = fdt_next_node(fit, noffset, &ndepth)) {
240 		if (ndepth == 1) {
241 			/*
242 			 * Direct child node of the configurations parent node,
243 			 * i.e. configuration node.
244 			 */
245 			printf("%s Configuration %u (%s)\n", p, count++,
246 			       fit_get_name(fit, noffset, NULL));
247 
248 			fit_conf_print(fit, noffset, p);
249 		}
250 	}
251 }
252 
253 /**
254  * fit_image_print_data() - prints out the hash node details
255  * @fit: pointer to the FIT format image header
256  * @noffset: offset of the hash node
257  * @p: pointer to prefix string
258  * @type: Type of information to print ("hash" or "sign")
259  *
260  * fit_image_print_data() lists properties for the processed hash node
261  *
262  * This function avoid using puts() since it prints a newline on the host
263  * but does not in U-Boot.
264  *
265  * returns:
266  *     no returned results
267  */
268 static void fit_image_print_data(const void *fit, int noffset, const char *p,
269 				 const char *type)
270 {
271 	const char *keyname;
272 	uint8_t *value;
273 	int value_len;
274 	char *algo;
275 	int required;
276 	int ret, i;
277 
278 	debug("%s  %s node:    '%s'\n", p, type,
279 	      fit_get_name(fit, noffset, NULL));
280 	printf("%s  %s algo:    ", p, type);
281 	if (fit_image_hash_get_algo(fit, noffset, &algo)) {
282 		printf("invalid/unsupported\n");
283 		return;
284 	}
285 	printf("%s", algo);
286 	keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
287 	required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
288 	if (keyname)
289 		printf(":%s", keyname);
290 	if (required)
291 		printf(" (required)");
292 	printf("\n");
293 
294 	ret = fit_image_hash_get_value(fit, noffset, &value,
295 					&value_len);
296 	printf("%s  %s value:   ", p, type);
297 	if (ret) {
298 		printf("unavailable\n");
299 	} else {
300 		for (i = 0; i < value_len; i++)
301 			printf("%02x", value[i]);
302 		printf("\n");
303 	}
304 
305 	debug("%s  %s len:     %d\n", p, type, value_len);
306 
307 	/* Signatures have a time stamp */
308 	if (IMAGE_ENABLE_TIMESTAMP && keyname) {
309 		time_t timestamp;
310 
311 		printf("%s  Timestamp:    ", p);
312 		if (fit_get_timestamp(fit, noffset, &timestamp))
313 			printf("unavailable\n");
314 		else
315 			genimg_print_time(timestamp);
316 	}
317 }
318 
319 /**
320  * fit_image_print_verification_data() - prints out the hash/signature details
321  * @fit: pointer to the FIT format image header
322  * @noffset: offset of the hash or signature node
323  * @p: pointer to prefix string
324  *
325  * This lists properties for the processed hash node
326  *
327  * returns:
328  *     no returned results
329  */
330 static void fit_image_print_verification_data(const void *fit, int noffset,
331 				       const char *p)
332 {
333 	const char *name;
334 
335 	/*
336 	 * Check subnode name, must be equal to "hash" or "signature".
337 	 * Multiple hash/signature nodes require unique unit node
338 	 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
339 	 */
340 	name = fit_get_name(fit, noffset, NULL);
341 	if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
342 		fit_image_print_data(fit, noffset, p, "Hash");
343 	} else if (!strncmp(name, FIT_SIG_NODENAME,
344 				strlen(FIT_SIG_NODENAME))) {
345 		fit_image_print_data(fit, noffset, p, "Sign");
346 	}
347 }
348 
349 /**
350  * fit_image_print - prints out the FIT component image details
351  * @fit: pointer to the FIT format image header
352  * @image_noffset: offset of the component image node
353  * @p: pointer to prefix string
354  *
355  * fit_image_print() lists all mandatory properties for the processed component
356  * image. If present, hash nodes are printed out as well. Load
357  * address for images of type firmware is also printed out. Since the load
358  * address is not mandatory for firmware images, it will be output as
359  * "unavailable" when not present.
360  *
361  * returns:
362  *     no returned results
363  */
364 void fit_image_print(const void *fit, int image_noffset, const char *p)
365 {
366 	char *desc;
367 	uint8_t type, arch, os, comp;
368 	size_t size;
369 	ulong load, entry;
370 	const void *data;
371 	int noffset;
372 	int ndepth;
373 	int ret;
374 
375 	/* Mandatory properties */
376 	ret = fit_get_desc(fit, image_noffset, &desc);
377 	printf("%s  Description:  ", p);
378 	if (ret)
379 		printf("unavailable\n");
380 	else
381 		printf("%s\n", desc);
382 
383 	if (IMAGE_ENABLE_TIMESTAMP) {
384 		time_t timestamp;
385 
386 		ret = fit_get_timestamp(fit, 0, &timestamp);
387 		printf("%s  Created:      ", p);
388 		if (ret)
389 			printf("unavailable\n");
390 		else
391 			genimg_print_time(timestamp);
392 	}
393 
394 	fit_image_get_type(fit, image_noffset, &type);
395 	printf("%s  Type:         %s\n", p, genimg_get_type_name(type));
396 
397 	fit_image_get_comp(fit, image_noffset, &comp);
398 	printf("%s  Compression:  %s\n", p, genimg_get_comp_name(comp));
399 
400 	ret = fit_image_get_data(fit, image_noffset, &data, &size);
401 
402 #ifndef USE_HOSTCC
403 	printf("%s  Data Start:   ", p);
404 	if (ret) {
405 		printf("unavailable\n");
406 	} else {
407 		void *vdata = (void *)data;
408 
409 		printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
410 	}
411 #endif
412 
413 	printf("%s  Data Size:    ", p);
414 	if (ret)
415 		printf("unavailable\n");
416 	else
417 		genimg_print_size(size);
418 
419 	/* Remaining, type dependent properties */
420 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
421 	    (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
422 	    (type == IH_TYPE_FLATDT)) {
423 		fit_image_get_arch(fit, image_noffset, &arch);
424 		printf("%s  Architecture: %s\n", p, genimg_get_arch_name(arch));
425 	}
426 
427 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
428 		fit_image_get_os(fit, image_noffset, &os);
429 		printf("%s  OS:           %s\n", p, genimg_get_os_name(os));
430 	}
431 
432 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
433 	    (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
434 	    (type == IH_TYPE_FPGA)) {
435 		ret = fit_image_get_load(fit, image_noffset, &load);
436 		printf("%s  Load Address: ", p);
437 		if (ret)
438 			printf("unavailable\n");
439 		else
440 			printf("0x%08lx\n", load);
441 	}
442 
443 	/* optional load address for FDT */
444 	if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
445 		printf("%s  Load Address: 0x%08lx\n", p, load);
446 
447 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
448 	    (type == IH_TYPE_RAMDISK)) {
449 		ret = fit_image_get_entry(fit, image_noffset, &entry);
450 		printf("%s  Entry Point:  ", p);
451 		if (ret)
452 			printf("unavailable\n");
453 		else
454 			printf("0x%08lx\n", entry);
455 	}
456 
457 	/* Process all hash subnodes of the component image node */
458 	for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
459 	     (noffset >= 0) && (ndepth > 0);
460 	     noffset = fdt_next_node(fit, noffset, &ndepth)) {
461 		if (ndepth == 1) {
462 			/* Direct child node of the component image node */
463 			fit_image_print_verification_data(fit, noffset, p);
464 		}
465 	}
466 }
467 
468 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
469 
470 /**
471  * fit_get_desc - get node description property
472  * @fit: pointer to the FIT format image header
473  * @noffset: node offset
474  * @desc: double pointer to the char, will hold pointer to the description
475  *
476  * fit_get_desc() reads description property from a given node, if
477  * description is found pointer to it is returned in third call argument.
478  *
479  * returns:
480  *     0, on success
481  *     -1, on failure
482  */
483 int fit_get_desc(const void *fit, int noffset, char **desc)
484 {
485 	int len;
486 
487 	*desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
488 	if (*desc == NULL) {
489 		fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
490 		return -1;
491 	}
492 
493 	return 0;
494 }
495 
496 /**
497  * fit_get_timestamp - get node timestamp property
498  * @fit: pointer to the FIT format image header
499  * @noffset: node offset
500  * @timestamp: pointer to the time_t, will hold read timestamp
501  *
502  * fit_get_timestamp() reads timestamp property from given node, if timestamp
503  * is found and has a correct size its value is returned in third call
504  * argument.
505  *
506  * returns:
507  *     0, on success
508  *     -1, on property read failure
509  *     -2, on wrong timestamp size
510  */
511 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
512 {
513 	int len;
514 	const void *data;
515 
516 	data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
517 	if (data == NULL) {
518 		fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
519 		return -1;
520 	}
521 	if (len != sizeof(uint32_t)) {
522 		debug("FIT timestamp with incorrect size of (%u)\n", len);
523 		return -2;
524 	}
525 
526 	*timestamp = uimage_to_cpu(*((uint32_t *)data));
527 	return 0;
528 }
529 
530 /**
531  * fit_image_get_node - get node offset for component image of a given unit name
532  * @fit: pointer to the FIT format image header
533  * @image_uname: component image node unit name
534  *
535  * fit_image_get_node() finds a component image (within the '/images'
536  * node) of a provided unit name. If image is found its node offset is
537  * returned to the caller.
538  *
539  * returns:
540  *     image node offset when found (>=0)
541  *     negative number on failure (FDT_ERR_* code)
542  */
543 int fit_image_get_node(const void *fit, const char *image_uname)
544 {
545 	int noffset, images_noffset;
546 
547 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
548 	if (images_noffset < 0) {
549 		debug("Can't find images parent node '%s' (%s)\n",
550 		      FIT_IMAGES_PATH, fdt_strerror(images_noffset));
551 		return images_noffset;
552 	}
553 
554 	noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
555 	if (noffset < 0) {
556 		debug("Can't get node offset for image unit name: '%s' (%s)\n",
557 		      image_uname, fdt_strerror(noffset));
558 	}
559 
560 	return noffset;
561 }
562 
563 /**
564  * fit_image_get_os - get os id for a given component image node
565  * @fit: pointer to the FIT format image header
566  * @noffset: component image node offset
567  * @os: pointer to the uint8_t, will hold os numeric id
568  *
569  * fit_image_get_os() finds os property in a given component image node.
570  * If the property is found, its (string) value is translated to the numeric
571  * id which is returned to the caller.
572  *
573  * returns:
574  *     0, on success
575  *     -1, on failure
576  */
577 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
578 {
579 	int len;
580 	const void *data;
581 
582 	/* Get OS name from property data */
583 	data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
584 	if (data == NULL) {
585 		fit_get_debug(fit, noffset, FIT_OS_PROP, len);
586 		*os = -1;
587 		return -1;
588 	}
589 
590 	/* Translate OS name to id */
591 	*os = genimg_get_os_id(data);
592 	return 0;
593 }
594 
595 /**
596  * fit_image_get_arch - get arch id for a given component image node
597  * @fit: pointer to the FIT format image header
598  * @noffset: component image node offset
599  * @arch: pointer to the uint8_t, will hold arch numeric id
600  *
601  * fit_image_get_arch() finds arch property in a given component image node.
602  * If the property is found, its (string) value is translated to the numeric
603  * id which is returned to the caller.
604  *
605  * returns:
606  *     0, on success
607  *     -1, on failure
608  */
609 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
610 {
611 	int len;
612 	const void *data;
613 
614 	/* Get architecture name from property data */
615 	data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
616 	if (data == NULL) {
617 		fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
618 		*arch = -1;
619 		return -1;
620 	}
621 
622 	/* Translate architecture name to id */
623 	*arch = genimg_get_arch_id(data);
624 	return 0;
625 }
626 
627 /**
628  * fit_image_get_type - get type id for a given component image node
629  * @fit: pointer to the FIT format image header
630  * @noffset: component image node offset
631  * @type: pointer to the uint8_t, will hold type numeric id
632  *
633  * fit_image_get_type() finds type property in a given component image node.
634  * If the property is found, its (string) value is translated to the numeric
635  * id which is returned to the caller.
636  *
637  * returns:
638  *     0, on success
639  *     -1, on failure
640  */
641 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
642 {
643 	int len;
644 	const void *data;
645 
646 	/* Get image type name from property data */
647 	data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
648 	if (data == NULL) {
649 		fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
650 		*type = -1;
651 		return -1;
652 	}
653 
654 	/* Translate image type name to id */
655 	*type = genimg_get_type_id(data);
656 	return 0;
657 }
658 
659 /**
660  * fit_image_get_comp - get comp id for a given component image node
661  * @fit: pointer to the FIT format image header
662  * @noffset: component image node offset
663  * @comp: pointer to the uint8_t, will hold comp numeric id
664  *
665  * fit_image_get_comp() finds comp property in a given component image node.
666  * If the property is found, its (string) value is translated to the numeric
667  * id which is returned to the caller.
668  *
669  * returns:
670  *     0, on success
671  *     -1, on failure
672  */
673 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
674 {
675 	int len;
676 	const void *data;
677 
678 	/* Get compression name from property data */
679 	data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
680 	if (data == NULL) {
681 		fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
682 		*comp = -1;
683 		return -1;
684 	}
685 
686 	/* Translate compression name to id */
687 	*comp = genimg_get_comp_id(data);
688 	return 0;
689 }
690 
691 static int fit_image_get_address(const void *fit, int noffset, char *name,
692 			  ulong *load)
693 {
694 	int len, cell_len;
695 	const fdt32_t *cell;
696 	uint64_t load64 = 0;
697 
698 	cell = fdt_getprop(fit, noffset, name, &len);
699 	if (cell == NULL) {
700 		fit_get_debug(fit, noffset, name, len);
701 		return -1;
702 	}
703 
704 	if (len > sizeof(ulong)) {
705 		printf("Unsupported %s address size\n", name);
706 		return -1;
707 	}
708 
709 	cell_len = len >> 2;
710 	/* Use load64 to avoid compiling warning for 32-bit target */
711 	while (cell_len--) {
712 		load64 = (load64 << 32) | uimage_to_cpu(*cell);
713 		cell++;
714 	}
715 	*load = (ulong)load64;
716 
717 	return 0;
718 }
719 
720 static int fit_image_set_address(const void *fit, int noffset, char *name,
721 				 ulong addr)
722 {
723 	int len, cell_len;
724 	const fdt32_t *cell;
725 
726 	cell = fdt_getprop(fit, noffset, name, &len);
727 	if (cell == NULL) {
728 		fit_get_debug(fit, noffset, name, len);
729 		return -1;
730 	}
731 
732 	if (len > sizeof(ulong)) {
733 		printf("Unsupported %s address size\n", name);
734 		return -1;
735 	}
736 
737 	cell_len = len >> 2;
738 	/* Use load64 to avoid compiling warning for 32-bit target */
739 	while (cell_len--) {
740 		*(fdt32_t *)cell = cpu_to_uimage(addr >> (32 * cell_len));
741 		cell++;
742 	}
743 
744 	return 0;
745 }
746 
747 /**
748  * fit_image_get_load() - get load addr property for given component image node
749  * @fit: pointer to the FIT format image header
750  * @noffset: component image node offset
751  * @load: pointer to the uint32_t, will hold load address
752  *
753  * fit_image_get_load() finds load address property in a given component
754  * image node. If the property is found, its value is returned to the caller.
755  *
756  * returns:
757  *     0, on success
758  *     -1, on failure
759  */
760 int fit_image_get_load(const void *fit, int noffset, ulong *load)
761 {
762 	return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
763 }
764 
765 /**
766  * fit_image_set_load() - set load addr property for given component image node
767  * @fit: pointer to the FIT format image header
768  * @noffset: component image node offset
769  * @load: uint32_t value, will hold load address
770  *
771  * fit_image_set_load() finds and set load address property in a given component
772  * image node. If the property is found, its value is returned to the caller.
773  *
774  * returns:
775  *     0, on success
776  *     -1, on failure
777  */
778 int fit_image_set_load(const void *fit, int noffset, ulong load)
779 {
780 	return fit_image_set_address(fit, noffset, FIT_LOAD_PROP, load);
781 }
782 
783 /**
784  * fit_image_get_entry() - get entry point address property
785  * @fit: pointer to the FIT format image header
786  * @noffset: component image node offset
787  * @entry: pointer to the uint32_t, will hold entry point address
788  *
789  * This gets the entry point address property for a given component image
790  * node.
791  *
792  * fit_image_get_entry() finds entry point address property in a given
793  * component image node.  If the property is found, its value is returned
794  * to the caller.
795  *
796  * returns:
797  *     0, on success
798  *     -1, on failure
799  */
800 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
801 {
802 	return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
803 }
804 
805 /**
806  * fit_image_set_entry() - set entry point address property
807  * @fit: pointer to the FIT format image header
808  * @noffset: component image node offset
809  * @entry: uint32_t value, will hold entry point address
810  *
811  * This sets the entry point address property for a given component image
812  * node.
813  *
814  * fit_image_set_entry() finds and set entry point address property in a given
815  * component image node.  If the property is found, its value is returned
816  * to the caller.
817  *
818  * returns:
819  *     0, on success
820  *     -1, on failure
821  */
822 int fit_image_set_entry(const void *fit, int noffset, ulong entry)
823 {
824 	return fit_image_set_address(fit, noffset, FIT_ENTRY_PROP, entry);
825 }
826 
827 /**
828  * fit_image_get_data - get data property and its size for a given component image node
829  * @fit: pointer to the FIT format image header
830  * @noffset: component image node offset
831  * @data: double pointer to void, will hold data property's data address
832  * @size: pointer to size_t, will hold data property's data size
833  *
834  * fit_image_get_data() finds data property in a given component image node.
835  * If the property is found its data start address and size are returned to
836  * the caller.
837  *
838  * returns:
839  *     0, on success
840  *     -1, on failure
841  */
842 int fit_image_get_data(const void *fit, int noffset,
843 		const void **data, size_t *size)
844 {
845 	ulong data_off = 0;
846 	ulong data_pos = 0;
847 	int len;
848 
849 	/* data */
850 	*data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
851 	if (*data) {
852 		*size = len;
853 		return 0;
854 	}
855 
856 	/* data-size */
857 	if (fit_image_get_data_size(fit, noffset, &len))
858 		return -ENOENT;
859 
860 	/* data-offset */
861 	if (!fit_image_get_data_offset(fit, noffset, (int *)&data_off)) {
862 		data_off += (ulong)fit + FIT_ALIGN(fdt_totalsize(fit));
863 		*data = (void *)data_off;
864 		*size = len;
865 		return 0;
866 	}
867 
868 	/* data-position */
869 	if (!fit_image_get_data_position(fit, noffset, (int *)&data_pos)) {
870 		*data = (void *)(data_pos + (ulong)fit);
871 		*size = len;
872 		return 0;
873 	}
874 
875 	*size = 0;
876 	return -1;
877 }
878 
879 /**
880  * Get 'data-offset' property from a given image node.
881  *
882  * @fit: pointer to the FIT image header
883  * @noffset: component image node offset
884  * @data_offset: holds the data-offset property
885  *
886  * returns:
887  *     0, on success
888  *     -ENOENT if the property could not be found
889  */
890 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
891 {
892 	const fdt32_t *val;
893 
894 	val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
895 	if (!val)
896 		return -ENOENT;
897 
898 	*data_offset = fdt32_to_cpu(*val);
899 
900 	return 0;
901 }
902 
903 /**
904  * Get 'data-position' property from a given image node.
905  *
906  * @fit: pointer to the FIT image header
907  * @noffset: component image node offset
908  * @data_position: holds the data-position property
909  *
910  * returns:
911  *     0, on success
912  *     -ENOENT if the property could not be found
913  */
914 int fit_image_get_data_position(const void *fit, int noffset,
915 				int *data_position)
916 {
917 	const fdt32_t *val;
918 
919 	val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
920 	if (!val)
921 		return -ENOENT;
922 
923 	*data_position = fdt32_to_cpu(*val);
924 
925 	return 0;
926 }
927 
928 /**
929  * Get 'data-size' property from a given image node.
930  *
931  * @fit: pointer to the FIT image header
932  * @noffset: component image node offset
933  * @data_size: holds the data-size property
934  *
935  * returns:
936  *     0, on success
937  *     -ENOENT if the property could not be found
938  */
939 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
940 {
941 	const fdt32_t *val;
942 
943 	val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
944 	if (!val)
945 		return -ENOENT;
946 
947 	*data_size = fdt32_to_cpu(*val);
948 
949 	return 0;
950 }
951 
952 /**
953  * Get 'rollback-index' property from a given image node.
954  *
955  * @fit: pointer to the FIT image header
956  * @noffset: component image node offset
957  * @index: holds the rollback-index property
958  *
959  * returns:
960  *     0, on success
961  *     -ENOENT if the property could not be found
962  */
963 int fit_image_get_rollback_index(const void *fit, int noffset, uint32_t *index)
964 {
965 	const fdt32_t *val;
966 
967 	val = fdt_getprop(fit, noffset, FIT_ROLLBACK_PROP, NULL);
968 	if (!val)
969 		return -ENOENT;
970 
971 	*index = fdt32_to_cpu(*val);
972 
973 	return 0;
974 }
975 
976 /**
977  * fit_image_hash_get_algo - get hash algorithm name
978  * @fit: pointer to the FIT format image header
979  * @noffset: hash node offset
980  * @algo: double pointer to char, will hold pointer to the algorithm name
981  *
982  * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
983  * If the property is found its data start address is returned to the caller.
984  *
985  * returns:
986  *     0, on success
987  *     -1, on failure
988  */
989 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
990 {
991 	int len;
992 
993 	*algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
994 	if (*algo == NULL) {
995 		fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
996 		return -1;
997 	}
998 
999 	return 0;
1000 }
1001 
1002 /**
1003  * fit_image_hash_get_value - get hash value and length
1004  * @fit: pointer to the FIT format image header
1005  * @noffset: hash node offset
1006  * @value: double pointer to uint8_t, will hold address of a hash value data
1007  * @value_len: pointer to an int, will hold hash data length
1008  *
1009  * fit_image_hash_get_value() finds hash value property in a given hash node.
1010  * If the property is found its data start address and size are returned to
1011  * the caller.
1012  *
1013  * returns:
1014  *     0, on success
1015  *     -1, on failure
1016  */
1017 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1018 				int *value_len)
1019 {
1020 	int len;
1021 
1022 	*value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1023 	if (*value == NULL) {
1024 		fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1025 		*value_len = 0;
1026 		return -1;
1027 	}
1028 
1029 	*value_len = len;
1030 	return 0;
1031 }
1032 
1033 /**
1034  * fit_image_hash_get_ignore - get hash ignore flag
1035  * @fit: pointer to the FIT format image header
1036  * @noffset: hash node offset
1037  * @ignore: pointer to an int, will hold hash ignore flag
1038  *
1039  * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1040  * If the property is found and non-zero, the hash algorithm is not verified by
1041  * u-boot automatically.
1042  *
1043  * returns:
1044  *     0, on ignore not found
1045  *     value, on ignore found
1046  */
1047 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1048 {
1049 	int len;
1050 	int *value;
1051 
1052 	value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1053 	if (value == NULL || len != sizeof(int))
1054 		*ignore = 0;
1055 	else
1056 		*ignore = *value;
1057 
1058 	return 0;
1059 }
1060 
1061 ulong fit_get_end(const void *fit)
1062 {
1063 	return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1064 }
1065 
1066 /**
1067  * fit_set_timestamp - set node timestamp property
1068  * @fit: pointer to the FIT format image header
1069  * @noffset: node offset
1070  * @timestamp: timestamp value to be set
1071  *
1072  * fit_set_timestamp() attempts to set timestamp property in the requested
1073  * node and returns operation status to the caller.
1074  *
1075  * returns:
1076  *     0, on success
1077  *     -ENOSPC if no space in device tree, -1 for other error
1078  */
1079 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1080 {
1081 	uint32_t t;
1082 	int ret;
1083 
1084 	t = cpu_to_uimage(timestamp);
1085 	ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1086 				sizeof(uint32_t));
1087 	if (ret) {
1088 		debug("Can't set '%s' property for '%s' node (%s)\n",
1089 		      FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1090 		      fdt_strerror(ret));
1091 		return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1092 	}
1093 
1094 	return 0;
1095 }
1096 
1097 int fit_set_totalsize(void *fit, int noffset, int totalsize)
1098 {
1099 	uint32_t t;
1100 	int ret;
1101 
1102 	t = cpu_to_uimage(totalsize);
1103 	ret = fdt_setprop(fit, noffset, FIT_TOTALSIZE_PROP, &t,
1104 				sizeof(uint32_t));
1105 	if (ret)
1106 		return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1107 
1108 	return 0;
1109 }
1110 
1111 int fit_get_image_defconf_node(const void *fit, int *images_noffset, int *def_noffset)
1112 {
1113 	int images_node, confs_node, defconf_node;
1114 	const char *def_name;
1115 
1116 	images_node = fdt_path_offset(fit, FIT_IMAGES_PATH);
1117 	if (images_node < 0)
1118 		return images_node;
1119 
1120 	confs_node = fdt_path_offset(fit, FIT_CONFS_PATH);
1121 	if (confs_node < 0)
1122 		return confs_node;
1123 
1124 	def_name = fdt_getprop(fit, confs_node, FIT_DEFAULT_PROP, NULL);
1125 	if (!def_name)
1126 		return -ENOENT;
1127 
1128 	defconf_node = fdt_subnode_offset(fit, confs_node, def_name);
1129 	if (defconf_node < 0)
1130 		return defconf_node;
1131 
1132 	*images_noffset = images_node;
1133 	*def_noffset = defconf_node;
1134 
1135 	return 0;
1136 }
1137 
1138 /**
1139  * calculate_hash - calculate and return hash for provided input data
1140  * @data: pointer to the input data
1141  * @data_len: data length
1142  * @algo: requested hash algorithm
1143  * @value: pointer to the char, will hold hash value data (caller must
1144  * allocate enough free space)
1145  * value_len: length of the calculated hash
1146  *
1147  * calculate_hash() computes input data hash according to the requested
1148  * algorithm.
1149  * Resulting hash value is placed in caller provided 'value' buffer, length
1150  * of the calculated hash is returned via value_len pointer argument.
1151  *
1152  * returns:
1153  *     0, on success
1154  *    -1, when algo is unsupported
1155  */
1156 int calculate_hash_software(const void *data, int data_len,
1157 			    const char *algo, uint8_t *value,
1158 			    int *value_len)
1159 {
1160 	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1161 		*((uint32_t *)value) = crc32_wd(0, data, data_len,
1162 							CHUNKSZ_CRC32);
1163 		*((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1164 		*value_len = 4;
1165 	} else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1166 		sha1_csum_wd((unsigned char *)data, data_len,
1167 			     (unsigned char *)value, CHUNKSZ_SHA1);
1168 		*value_len = 20;
1169 	} else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1170 		sha256_csum_wd((unsigned char *)data, data_len,
1171 			       (unsigned char *)value, CHUNKSZ_SHA256);
1172 		*value_len = SHA256_SUM_LEN;
1173 	} else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1174 		md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1175 		*value_len = 16;
1176 	} else {
1177 		debug("Unsupported hash alogrithm\n");
1178 		return -1;
1179 	}
1180 	return 0;
1181 }
1182 
1183 #ifdef USE_HOSTCC
1184 int calculate_hash(const void *data, int data_len, const char *algo,
1185 		   uint8_t *value, int *value_len)
1186 {
1187 	return calculate_hash_software(data, data_len, algo, value, value_len);
1188 }
1189 #else
1190 #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO)
1191 static int crypto_csum(u32 cap, const char *data, int len, u8 *output)
1192 {
1193 	struct udevice *dev;
1194 	sha_context csha_ctx;
1195 
1196 	dev = crypto_get_device(cap);
1197 	if (!dev) {
1198 		printf("Can't find expected crypto device\n");
1199 		return -ENODEV;
1200 	}
1201 
1202 	csha_ctx.algo = cap;
1203 	csha_ctx.length = len;
1204 
1205 	return crypto_sha_csum(dev, &csha_ctx, (char *)data, len, output);
1206 }
1207 
1208 int calculate_hash(const void *data, int data_len, const char *algo,
1209 		   uint8_t *value, int *value_len)
1210 {
1211 	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1212 		*((uint32_t *)value) = crc32_wd(0, data, data_len,
1213 							CHUNKSZ_CRC32);
1214 		*((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1215 		*value_len = 4;
1216 	} else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1217 		crypto_csum(CRYPTO_SHA1, data, data_len, value);
1218 		*value_len = 20;
1219 	} else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1220 		crypto_csum(CRYPTO_SHA256, data, data_len, value);
1221 		*value_len = SHA256_SUM_LEN;
1222 	} else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1223 		crypto_csum(CRYPTO_MD5, data, data_len, value);
1224 		*value_len = 16;
1225 	} else {
1226 		debug("Unsupported hash alogrithm\n");
1227 		return -1;
1228 	}
1229 	return 0;
1230 }
1231 #else
1232 int calculate_hash(const void *data, int data_len, const char *algo,
1233 		   uint8_t *value, int *value_len)
1234 {
1235 	return calculate_hash_software(data, data_len, algo, value, value_len);
1236 }
1237 #endif
1238 #endif
1239 
1240 int fit_image_check_hash(const void *fit, int noffset, const void *data,
1241 			 size_t size, char **err_msgp)
1242 {
1243 	uint8_t value[FIT_MAX_HASH_LEN];
1244 	int value_len;
1245 	char *algo;
1246 	uint8_t *fit_value;
1247 	int fit_value_len;
1248 	int ignore;
1249 
1250 	*err_msgp = NULL;
1251 
1252 	if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1253 		*err_msgp = "Can't get hash algo property";
1254 		return -1;
1255 	}
1256 	printf("%s", algo);
1257 
1258 	if (IMAGE_ENABLE_IGNORE) {
1259 		fit_image_hash_get_ignore(fit, noffset, &ignore);
1260 		if (ignore) {
1261 			printf("-skipped ");
1262 			return 0;
1263 		}
1264 	}
1265 
1266 	if (fit_image_hash_get_value(fit, noffset, &fit_value,
1267 				     &fit_value_len)) {
1268 		*err_msgp = "Can't get hash value property";
1269 		return -1;
1270 	}
1271 
1272 	if (calculate_hash(data, size, algo, value, &value_len)) {
1273 		*err_msgp = "Unsupported hash algorithm";
1274 		return -1;
1275 	}
1276 
1277 	if (value_len != fit_value_len) {
1278 		*err_msgp = "Bad hash value len";
1279 		return -1;
1280 	} else if (memcmp(value, fit_value, value_len) != 0) {
1281 		*err_msgp = "Bad hash value";
1282 		return -1;
1283 	}
1284 
1285 	return 0;
1286 }
1287 
1288 int fit_image_verify_with_data(const void *fit, int image_noffset,
1289 			       const void *data, size_t size)
1290 {
1291 	int		noffset = 0;
1292 	char		*err_msg = "";
1293 	int verify_all = 1;
1294 	int ret;
1295 
1296 	/* Verify all required signatures */
1297 	if (IMAGE_ENABLE_VERIFY &&
1298 	    fit_image_verify_required_sigs(fit, image_noffset, data, size,
1299 					   gd_fdt_blob(), &verify_all)) {
1300 		err_msg = "Unable to verify required signature";
1301 		goto error;
1302 	}
1303 
1304 	/* Process all hash subnodes of the component image node */
1305 	fdt_for_each_subnode(noffset, fit, image_noffset) {
1306 		const char *name = fit_get_name(fit, noffset, NULL);
1307 
1308 		/*
1309 		 * Check subnode name, must be equal to "hash".
1310 		 * Multiple hash nodes require unique unit node
1311 		 * names, e.g. hash@1, hash@2, etc.
1312 		 */
1313 		if (!strncmp(name, FIT_HASH_NODENAME,
1314 			     strlen(FIT_HASH_NODENAME))) {
1315 			if (fit_image_check_hash(fit, noffset, data, size,
1316 						 &err_msg))
1317 				goto error;
1318 			puts("+ ");
1319 		} else if (IMAGE_ENABLE_VERIFY && verify_all &&
1320 				!strncmp(name, FIT_SIG_NODENAME,
1321 					strlen(FIT_SIG_NODENAME))) {
1322 			ret = fit_image_check_sig(fit, noffset, data,
1323 							size, -1, &err_msg);
1324 
1325 			/*
1326 			 * Show an indication on failure, but do not return
1327 			 * an error. Only keys marked 'required' can cause
1328 			 * an image validation failure. See the call to
1329 			 * fit_image_verify_required_sigs() above.
1330 			 */
1331 			if (ret)
1332 				puts("- ");
1333 			else
1334 				puts("+ ");
1335 		}
1336 	}
1337 
1338 	if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1339 		err_msg = "Corrupted or truncated tree";
1340 		goto error;
1341 	}
1342 
1343 	return 1; /* success */
1344 
1345 error:
1346 	printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1347 	       err_msg, fit_get_name(fit, noffset, NULL),
1348 	       fit_get_name(fit, image_noffset, NULL));
1349 	return 0;
1350 }
1351 
1352 /**
1353  * fit_image_verify - verify data integrity
1354  * @fit: pointer to the FIT format image header
1355  * @image_noffset: component image node offset
1356  *
1357  * fit_image_verify() goes over component image hash nodes,
1358  * re-calculates each data hash and compares with the value stored in hash
1359  * node.
1360  *
1361  * returns:
1362  *     1, if all hashes are valid
1363  *     0, otherwise (or on error)
1364  */
1365 int fit_image_verify(const void *fit, int image_noffset)
1366 {
1367 	const void	*data;
1368 	size_t		size;
1369 	int		noffset = 0;
1370 	char		*err_msg = "";
1371 
1372 	/* Get image data and data length */
1373 	if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1374 		err_msg = "Can't get image data/size";
1375 		printf("error!\n%s for '%s' hash node in '%s' image node\n",
1376 		       err_msg, fit_get_name(fit, noffset, NULL),
1377 		       fit_get_name(fit, image_noffset, NULL));
1378 		return 0;
1379 	}
1380 
1381 	return fit_image_verify_with_data(fit, image_noffset, data, size);
1382 }
1383 
1384 /**
1385  * fit_all_image_verify - verify data integrity for all images
1386  * @fit: pointer to the FIT format image header
1387  *
1388  * fit_all_image_verify() goes over all images in the FIT and
1389  * for every images checks if all it's hashes are valid.
1390  *
1391  * returns:
1392  *     1, if all hashes of all images are valid
1393  *     0, otherwise (or on error)
1394  */
1395 int fit_all_image_verify(const void *fit)
1396 {
1397 	int images_noffset;
1398 	int noffset;
1399 	int ndepth;
1400 	int count;
1401 
1402 	/* Find images parent node offset */
1403 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1404 	if (images_noffset < 0) {
1405 		printf("Can't find images parent node '%s' (%s)\n",
1406 		       FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1407 		return 0;
1408 	}
1409 
1410 	/* Process all image subnodes, check hashes for each */
1411 	printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1412 	       (ulong)fit);
1413 	for (ndepth = 0, count = 0,
1414 	     noffset = fdt_next_node(fit, images_noffset, &ndepth);
1415 			(noffset >= 0) && (ndepth > 0);
1416 			noffset = fdt_next_node(fit, noffset, &ndepth)) {
1417 		if (ndepth == 1) {
1418 			/*
1419 			 * Direct child node of the images parent node,
1420 			 * i.e. component image node.
1421 			 */
1422 			printf("   Hash(es) for Image %u (%s): ", count,
1423 			       fit_get_name(fit, noffset, NULL));
1424 			count++;
1425 
1426 			if (!fit_image_verify(fit, noffset))
1427 				return 0;
1428 			printf("\n");
1429 		}
1430 	}
1431 	return 1;
1432 }
1433 
1434 /**
1435  * fit_image_check_os - check whether image node is of a given os type
1436  * @fit: pointer to the FIT format image header
1437  * @noffset: component image node offset
1438  * @os: requested image os
1439  *
1440  * fit_image_check_os() reads image os property and compares its numeric
1441  * id with the requested os. Comparison result is returned to the caller.
1442  *
1443  * returns:
1444  *     1 if image is of given os type
1445  *     0 otherwise (or on error)
1446  */
1447 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1448 {
1449 	uint8_t image_os;
1450 
1451 	if (fit_image_get_os(fit, noffset, &image_os))
1452 		return 0;
1453 	return (os == image_os);
1454 }
1455 
1456 /**
1457  * fit_image_check_arch - check whether image node is of a given arch
1458  * @fit: pointer to the FIT format image header
1459  * @noffset: component image node offset
1460  * @arch: requested imagearch
1461  *
1462  * fit_image_check_arch() reads image arch property and compares its numeric
1463  * id with the requested arch. Comparison result is returned to the caller.
1464  *
1465  * returns:
1466  *     1 if image is of given arch
1467  *     0 otherwise (or on error)
1468  */
1469 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1470 {
1471 	uint8_t image_arch;
1472 	int aarch32_support = 0;
1473 
1474 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1475 	aarch32_support = 1;
1476 #endif
1477 
1478 	if (fit_image_get_arch(fit, noffset, &image_arch))
1479 		return 0;
1480 	return (arch == image_arch) ||
1481 		(arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1482 		(arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1483 		 aarch32_support);
1484 }
1485 
1486 /**
1487  * fit_image_check_type - check whether image node is of a given type
1488  * @fit: pointer to the FIT format image header
1489  * @noffset: component image node offset
1490  * @type: requested image type
1491  *
1492  * fit_image_check_type() reads image type property and compares its numeric
1493  * id with the requested type. Comparison result is returned to the caller.
1494  *
1495  * returns:
1496  *     1 if image is of given type
1497  *     0 otherwise (or on error)
1498  */
1499 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1500 {
1501 	uint8_t image_type;
1502 
1503 	if (fit_image_get_type(fit, noffset, &image_type))
1504 		return 0;
1505 	return (type == image_type);
1506 }
1507 
1508 /**
1509  * fit_image_check_comp - check whether image node uses given compression
1510  * @fit: pointer to the FIT format image header
1511  * @noffset: component image node offset
1512  * @comp: requested image compression type
1513  *
1514  * fit_image_check_comp() reads image compression property and compares its
1515  * numeric id with the requested compression type. Comparison result is
1516  * returned to the caller.
1517  *
1518  * returns:
1519  *     1 if image uses requested compression
1520  *     0 otherwise (or on error)
1521  */
1522 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1523 {
1524 	uint8_t image_comp;
1525 
1526 	if (fit_image_get_comp(fit, noffset, &image_comp))
1527 		return 0;
1528 	return (comp == image_comp);
1529 }
1530 
1531 /**
1532  * fit_check_format - sanity check FIT image format
1533  * @fit: pointer to the FIT format image header
1534  *
1535  * fit_check_format() runs a basic sanity FIT image verification.
1536  * Routine checks for mandatory properties, nodes, etc.
1537  *
1538  * returns:
1539  *     1, on success
1540  *     0, on failure
1541  */
1542 int fit_check_format(const void *fit)
1543 {
1544 	/* mandatory / node 'description' property */
1545 	if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1546 		debug("Wrong FIT format: no description\n");
1547 		return 0;
1548 	}
1549 
1550 	if (IMAGE_ENABLE_TIMESTAMP) {
1551 		/* mandatory / node 'timestamp' property */
1552 		if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1553 			debug("Wrong FIT format: no timestamp\n");
1554 			return 0;
1555 		}
1556 	}
1557 
1558 	/* mandatory subimages parent '/images' node */
1559 	if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1560 		debug("Wrong FIT format: no images parent node\n");
1561 		return 0;
1562 	}
1563 
1564 	return 1;
1565 }
1566 
1567 
1568 /**
1569  * fit_conf_find_compat
1570  * @fit: pointer to the FIT format image header
1571  * @fdt: pointer to the device tree to compare against
1572  *
1573  * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1574  * most compatible with the passed in device tree.
1575  *
1576  * Example:
1577  *
1578  * / o image-tree
1579  *   |-o images
1580  *   | |-o fdt@1
1581  *   | |-o fdt@2
1582  *   |
1583  *   |-o configurations
1584  *     |-o config@1
1585  *     | |-fdt = fdt@1
1586  *     |
1587  *     |-o config@2
1588  *       |-fdt = fdt@2
1589  *
1590  * / o U-Boot fdt
1591  *   |-compatible = "foo,bar", "bim,bam"
1592  *
1593  * / o kernel fdt1
1594  *   |-compatible = "foo,bar",
1595  *
1596  * / o kernel fdt2
1597  *   |-compatible = "bim,bam", "baz,biz"
1598  *
1599  * Configuration 1 would be picked because the first string in U-Boot's
1600  * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1601  * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1602  *
1603  * returns:
1604  *     offset to the configuration to use if one was found
1605  *     -1 otherwise
1606  */
1607 int fit_conf_find_compat(const void *fit, const void *fdt)
1608 {
1609 	int ndepth = 0;
1610 	int noffset, confs_noffset, images_noffset;
1611 	const void *fdt_compat;
1612 	int fdt_compat_len;
1613 	int best_match_offset = 0;
1614 	int best_match_pos = 0;
1615 
1616 	confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1617 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1618 	if (confs_noffset < 0 || images_noffset < 0) {
1619 		debug("Can't find configurations or images nodes.\n");
1620 		return -1;
1621 	}
1622 
1623 	fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1624 	if (!fdt_compat) {
1625 		debug("Fdt for comparison has no \"compatible\" property.\n");
1626 		return -1;
1627 	}
1628 
1629 	/*
1630 	 * Loop over the configurations in the FIT image.
1631 	 */
1632 	for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1633 			(noffset >= 0) && (ndepth > 0);
1634 			noffset = fdt_next_node(fit, noffset, &ndepth)) {
1635 		const void *kfdt;
1636 		const char *kfdt_name;
1637 		int kfdt_noffset;
1638 		const char *cur_fdt_compat;
1639 		int len;
1640 		size_t size;
1641 		int i;
1642 
1643 		if (ndepth > 1)
1644 			continue;
1645 
1646 		kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1647 		if (!kfdt_name) {
1648 			debug("No fdt property found.\n");
1649 			continue;
1650 		}
1651 		kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1652 						  kfdt_name);
1653 		if (kfdt_noffset < 0) {
1654 			debug("No image node named \"%s\" found.\n",
1655 			      kfdt_name);
1656 			continue;
1657 		}
1658 		/*
1659 		 * Get a pointer to this configuration's fdt.
1660 		 */
1661 		if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1662 			debug("Failed to get fdt \"%s\".\n", kfdt_name);
1663 			continue;
1664 		}
1665 
1666 		len = fdt_compat_len;
1667 		cur_fdt_compat = fdt_compat;
1668 		/*
1669 		 * Look for a match for each U-Boot compatibility string in
1670 		 * turn in this configuration's fdt.
1671 		 */
1672 		for (i = 0; len > 0 &&
1673 		     (!best_match_offset || best_match_pos > i); i++) {
1674 			int cur_len = strlen(cur_fdt_compat) + 1;
1675 
1676 			if (!fdt_node_check_compatible(kfdt, 0,
1677 						       cur_fdt_compat)) {
1678 				best_match_offset = noffset;
1679 				best_match_pos = i;
1680 				break;
1681 			}
1682 			len -= cur_len;
1683 			cur_fdt_compat += cur_len;
1684 		}
1685 	}
1686 	if (!best_match_offset) {
1687 		debug("No match found.\n");
1688 		return -1;
1689 	}
1690 
1691 	return best_match_offset;
1692 }
1693 
1694 /**
1695  * fit_conf_get_node - get node offset for configuration of a given unit name
1696  * @fit: pointer to the FIT format image header
1697  * @conf_uname: configuration node unit name
1698  *
1699  * fit_conf_get_node() finds a configuration (within the '/configurations'
1700  * parent node) of a provided unit name. If configuration is found its node
1701  * offset is returned to the caller.
1702  *
1703  * When NULL is provided in second argument fit_conf_get_node() will search
1704  * for a default configuration node instead. Default configuration node unit
1705  * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1706  * node.
1707  *
1708  * returns:
1709  *     configuration node offset when found (>=0)
1710  *     negative number on failure (FDT_ERR_* code)
1711  */
1712 int fit_conf_get_node(const void *fit, const char *conf_uname)
1713 {
1714 	int noffset, confs_noffset;
1715 	int len;
1716 	const char *s;
1717 	char *conf_uname_copy = NULL;
1718 
1719 	confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1720 	if (confs_noffset < 0) {
1721 		debug("Can't find configurations parent node '%s' (%s)\n",
1722 		      FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1723 		return confs_noffset;
1724 	}
1725 
1726 	if (conf_uname == NULL) {
1727 		/* get configuration unit name from the default property */
1728 		debug("No configuration specified, trying default...\n");
1729 		conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1730 						 FIT_DEFAULT_PROP, &len);
1731 		if (conf_uname == NULL) {
1732 			fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1733 				      len);
1734 			return len;
1735 		}
1736 		debug("Found default configuration: '%s'\n", conf_uname);
1737 	}
1738 
1739 	s = strchr(conf_uname, '#');
1740 	if (s) {
1741 		len = s - conf_uname;
1742 		conf_uname_copy = malloc(len + 1);
1743 		if (!conf_uname_copy) {
1744 			debug("Can't allocate uname copy: '%s'\n",
1745 					conf_uname);
1746 			return -ENOMEM;
1747 		}
1748 		memcpy(conf_uname_copy, conf_uname, len);
1749 		conf_uname_copy[len] = '\0';
1750 		conf_uname = conf_uname_copy;
1751 	}
1752 
1753 	noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1754 	if (noffset < 0) {
1755 		debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1756 		      conf_uname, fdt_strerror(noffset));
1757 	}
1758 
1759 	if (conf_uname_copy)
1760 		free(conf_uname_copy);
1761 
1762 	return noffset;
1763 }
1764 
1765 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1766 		const char *prop_name)
1767 {
1768 	return fdt_stringlist_count(fit, noffset, prop_name);
1769 }
1770 
1771 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1772 		const char *prop_name, int index)
1773 {
1774 	const char *uname;
1775 	int len;
1776 
1777 	/* get kernel image unit name from configuration kernel property */
1778 	uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1779 	if (uname == NULL)
1780 		return len;
1781 
1782 	return fit_image_get_node(fit, uname);
1783 }
1784 
1785 int fit_conf_get_prop_node(const void *fit, int noffset,
1786 		const char *prop_name)
1787 {
1788 	return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1789 }
1790 
1791 /**
1792  * fit_conf_print - prints out the FIT configuration details
1793  * @fit: pointer to the FIT format image header
1794  * @noffset: offset of the configuration node
1795  * @p: pointer to prefix string
1796  *
1797  * fit_conf_print() lists all mandatory properties for the processed
1798  * configuration node.
1799  *
1800  * returns:
1801  *     no returned results
1802  */
1803 void fit_conf_print(const void *fit, int noffset, const char *p)
1804 {
1805 	char *desc;
1806 	const char *uname;
1807 	int ret;
1808 	int fdt_index, loadables_index;
1809 
1810 	/* Mandatory properties */
1811 	ret = fit_get_desc(fit, noffset, &desc);
1812 	printf("%s  Description:  ", p);
1813 	if (ret)
1814 		printf("unavailable\n");
1815 	else
1816 		printf("%s\n", desc);
1817 
1818 	uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1819 	printf("%s  Kernel:       ", p);
1820 	if (uname == NULL)
1821 		printf("unavailable\n");
1822 	else
1823 		printf("%s\n", uname);
1824 
1825 	/* Optional properties */
1826 	uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1827 	if (uname)
1828 		printf("%s  Init Ramdisk: %s\n", p, uname);
1829 
1830 	uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
1831 	if (uname)
1832 		printf("%s  Firmware:     %s\n", p, uname);
1833 
1834 	for (fdt_index = 0;
1835 	     uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
1836 					fdt_index, NULL), uname;
1837 	     fdt_index++) {
1838 
1839 		if (fdt_index == 0)
1840 			printf("%s  FDT:          ", p);
1841 		else
1842 			printf("%s                ", p);
1843 		printf("%s\n", uname);
1844 	}
1845 
1846 	uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
1847 	if (uname)
1848 		printf("%s  FPGA:         %s\n", p, uname);
1849 
1850 	/* Print out all of the specified loadables */
1851 	for (loadables_index = 0;
1852 	     uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
1853 					loadables_index, NULL), uname;
1854 	     loadables_index++) {
1855 		if (loadables_index == 0) {
1856 			printf("%s  Loadables:    ", p);
1857 		} else {
1858 			printf("%s                ", p);
1859 		}
1860 		printf("%s\n", uname);
1861 	}
1862 }
1863 
1864 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1865 {
1866 	fit_image_print(fit, rd_noffset, "   ");
1867 
1868 	if (verify) {
1869 		puts("   Verifying Hash Integrity ... ");
1870 		if (!fit_image_verify(fit, rd_noffset)) {
1871 			puts("Bad Data Hash\n");
1872 			return -EACCES;
1873 		}
1874 		puts("OK\n");
1875 	}
1876 
1877 	return 0;
1878 }
1879 
1880 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1881 			ulong addr)
1882 {
1883 	int cfg_noffset;
1884 	void *fit_hdr;
1885 	int noffset;
1886 
1887 	debug("*  %s: using config '%s' from image at 0x%08lx\n",
1888 	      prop_name, images->fit_uname_cfg, addr);
1889 
1890 	/* Check whether configuration has this property defined */
1891 	fit_hdr = map_sysmem(addr, 0);
1892 	cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1893 	if (cfg_noffset < 0) {
1894 		debug("*  %s: no such config\n", prop_name);
1895 		return -EINVAL;
1896 	}
1897 
1898 	noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1899 	if (noffset < 0) {
1900 		debug("*  %s: no '%s' in config\n", prop_name, prop_name);
1901 		return -ENOENT;
1902 	}
1903 
1904 	return noffset;
1905 }
1906 
1907 /**
1908  * fit_get_image_type_property() - get property name for IH_TYPE_...
1909  *
1910  * @return the properly name where we expect to find the image in the
1911  * config node
1912  */
1913 static const char *fit_get_image_type_property(int type)
1914 {
1915 	/*
1916 	 * This is sort-of available in the uimage_type[] table in image.c
1917 	 * but we don't have access to the short name, and "fdt" is different
1918 	 * anyway. So let's just keep it here.
1919 	 */
1920 	switch (type) {
1921 	case IH_TYPE_FLATDT:
1922 		return FIT_FDT_PROP;
1923 	case IH_TYPE_KERNEL:
1924 		return FIT_KERNEL_PROP;
1925 	case IH_TYPE_RAMDISK:
1926 		return FIT_RAMDISK_PROP;
1927 	case IH_TYPE_FIRMWARE:
1928 		return FIT_FIRMWARE_PROP;
1929 	case IH_TYPE_X86_SETUP:
1930 		return FIT_SETUP_PROP;
1931 	case IH_TYPE_LOADABLE:
1932 		return FIT_LOADABLE_PROP;
1933 	case IH_TYPE_FPGA:
1934 		return FIT_FPGA_PROP;
1935 	case IH_TYPE_STANDALONE:
1936 		return FIT_STANDALONE_PROP;
1937 	}
1938 
1939 	return "unknown";
1940 }
1941 
1942 #ifndef USE_HOSTCC
1943 __weak int fit_board_verify_required_sigs(void)
1944 {
1945 	return 0;
1946 }
1947 #endif
1948 
1949 int fit_image_load_index(bootm_headers_t *images, ulong addr,
1950 			 const char **fit_unamep, const char **fit_uname_configp,
1951 			 int arch, int image_type, int image_index, int bootstage_id,
1952 			 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1953 {
1954 	int cfg_noffset, noffset;
1955 	const char *fit_uname;
1956 	const char *fit_uname_config;
1957 	const char *fit_base_uname_config;
1958 	const void *fit;
1959 	const void *buf;
1960 	size_t size;
1961 	int type_ok, os_ok;
1962 	ulong load, data, len;
1963 	uint8_t os;
1964 #ifndef USE_HOSTCC
1965 	uint8_t os_arch;
1966 #endif
1967 	const char *prop_name;
1968 	int ret;
1969 
1970 	fit = map_sysmem(addr, 0);
1971 	fit_uname = fit_unamep ? *fit_unamep : NULL;
1972 	fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1973 	fit_base_uname_config = NULL;
1974 	prop_name = fit_get_image_type_property(image_type);
1975 	printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1976 
1977 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1978 	if (!fit_check_format(fit)) {
1979 		printf("Bad FIT %s image format!\n", prop_name);
1980 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1981 		return -ENOEXEC;
1982 	}
1983 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1984 	if (fit_uname) {
1985 		/* get FIT component image node offset */
1986 		bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1987 		noffset = fit_image_get_node(fit, fit_uname);
1988 	} else {
1989 		/*
1990 		 * no image node unit name, try to get config
1991 		 * node first. If config unit node name is NULL
1992 		 * fit_conf_get_node() will try to find default config node
1993 		 */
1994 		bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1995 		if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1996 			cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1997 		} else {
1998 			cfg_noffset = fit_conf_get_node(fit,
1999 							fit_uname_config);
2000 		}
2001 		if (cfg_noffset < 0) {
2002 			puts("Could not find configuration node\n");
2003 			bootstage_error(bootstage_id +
2004 					BOOTSTAGE_SUB_NO_UNIT_NAME);
2005 			return -ENOENT;
2006 		}
2007 		fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
2008 		printf("   Using '%s' configuration\n", fit_base_uname_config);
2009 		if (image_type == IH_TYPE_KERNEL) {
2010 #ifndef USE_HOSTCC
2011 			/* If board required sigs, check self */
2012 			if (fit_board_verify_required_sigs() &&
2013 			    !IS_ENABLED(CONFIG_FIT_SIGNATURE)) {
2014 				printf("Verified-boot requires CONFIG_FIT_SIGNATURE enabled\n");
2015 				hang();
2016 			}
2017 #endif
2018 			/* Remember (and possibly verify) this config */
2019 			images->fit_uname_cfg = fit_base_uname_config;
2020 			if (IMAGE_ENABLE_VERIFY) {
2021 				puts("   Verifying Hash Integrity ... ");
2022 				if (fit_config_verify(fit, cfg_noffset)) {
2023 					puts("Bad Data Hash\n");
2024 					bootstage_error(bootstage_id +
2025 						BOOTSTAGE_SUB_HASH);
2026 					return -EACCES;
2027 				}
2028 				puts("OK\n");
2029 
2030 #ifdef CONFIG_FIT_ROLLBACK_PROTECT
2031 				uint32_t this_index, min_index;
2032 
2033 				puts("   Verifying Rollback-index ... ");
2034 				if (fit_rollback_index_verify(fit,
2035 						FIT_ROLLBACK_INDEX,
2036 						&this_index, &min_index)) {
2037 					puts("Failed to get index\n");
2038 					return ret;
2039 				} else if (this_index < min_index) {
2040 					printf("Reject index %d < %d(min)\n",
2041 					       this_index, min_index);
2042 					return -EINVAL;
2043 				}
2044 
2045 				printf("%d >= %d, OK\n", this_index, min_index);
2046 #endif
2047 			}
2048 			bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
2049 		}
2050 
2051 		noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2052 						       prop_name, image_index);
2053 		fit_uname = fit_get_name(fit, noffset, NULL);
2054 	}
2055 	if (noffset < 0) {
2056 		puts("Could not find subimage node\n");
2057 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
2058 		return -ENOENT;
2059 	}
2060 
2061 	printf("   Trying '%s' %s subimage\n", fit_uname, prop_name);
2062 
2063 	ret = fit_image_select(fit, noffset, images->verify);
2064 	if (ret) {
2065 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2066 		return ret;
2067 	}
2068 
2069 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2070 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
2071 	if (!fit_image_check_target_arch(fit, noffset)) {
2072 		puts("Unsupported Architecture\n");
2073 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2074 		return -ENOEXEC;
2075 	}
2076 #endif
2077 
2078 #ifndef USE_HOSTCC
2079 	fit_image_get_arch(fit, noffset, &os_arch);
2080 	images->os.arch = os_arch;
2081 #endif
2082 
2083 	if (image_type == IH_TYPE_FLATDT &&
2084 	    !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
2085 		puts("FDT image is compressed");
2086 		return -EPROTONOSUPPORT;
2087 	}
2088 
2089 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2090 	type_ok = fit_image_check_type(fit, noffset, image_type) ||
2091 		  fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
2092 		  (image_type == IH_TYPE_KERNEL &&
2093 		   fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
2094 
2095 	os_ok = image_type == IH_TYPE_FLATDT ||
2096 		image_type == IH_TYPE_FPGA ||
2097 		fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
2098 		fit_image_check_os(fit, noffset, IH_OS_ARM_TRUSTED_FIRMWARE) ||
2099 		fit_image_check_os(fit, noffset, IH_OS_OP_TEE) ||
2100 		fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
2101 		fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
2102 
2103 	/*
2104 	 * If either of the checks fail, we should report an error, but
2105 	 * if the image type is coming from the "loadables" field, we
2106 	 * don't care what it is
2107 	 */
2108 	if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
2109 		fit_image_get_os(fit, noffset, &os);
2110 		printf("No %s %s %s Image\n",
2111 		       genimg_get_os_name(os),
2112 		       genimg_get_arch_name(arch),
2113 		       genimg_get_type_name(image_type));
2114 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2115 		return -EIO;
2116 	}
2117 
2118 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2119 
2120 	/* get image data address and length */
2121 	if (fit_image_get_data(fit, noffset, &buf, &size)) {
2122 		printf("Could not find %s subimage data!\n", prop_name);
2123 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2124 		return -ENOENT;
2125 	}
2126 
2127 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
2128 	/* perform any post-processing on the image data */
2129 	board_fit_image_post_process((void **)&buf, &size);
2130 #endif
2131 
2132 	len = (ulong)size;
2133 
2134 	/* verify that image data is a proper FDT blob */
2135 	if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
2136 		puts("Subimage data is not a FDT");
2137 		return -ENOEXEC;
2138 	}
2139 
2140 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2141 
2142 	/*
2143 	 * Work-around for eldk-4.2 which gives this warning if we try to
2144 	 * cast in the unmap_sysmem() call:
2145 	 * warning: initialization discards qualifiers from pointer target type
2146 	 */
2147 	{
2148 		void *vbuf = (void *)buf;
2149 
2150 		data = map_to_sysmem(vbuf);
2151 	}
2152 
2153 	if (load_op == FIT_LOAD_IGNORED) {
2154 		/* Don't load */
2155 	} else if (fit_image_get_load(fit, noffset, &load)) {
2156 		if (load_op == FIT_LOAD_REQUIRED) {
2157 			printf("Can't get %s subimage load address!\n",
2158 			       prop_name);
2159 			bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2160 			return -EBADF;
2161 		}
2162 	} else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
2163 		ulong image_start, image_end;
2164 		ulong load_end;
2165 		void *dst;
2166 
2167 		/*
2168 		 * move image data to the load address,
2169 		 * make sure we don't overwrite initial image
2170 		 */
2171 		image_start = addr;
2172 		image_end = addr + fit_get_size(fit);
2173 
2174 		load_end = load + len;
2175 		if (image_type != IH_TYPE_KERNEL &&
2176 		    load < image_end && load_end > image_start) {
2177 			printf("Error: %s overwritten\n", prop_name);
2178 			return -EXDEV;
2179 		}
2180 
2181 		printf("   Loading %s from 0x%08lx to 0x%08lx\n",
2182 		       prop_name, data, load);
2183 
2184 		dst = map_sysmem(load, len);
2185 		memmove(dst, buf, len);
2186 		data = load;
2187 	}
2188 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2189 
2190 	*datap = data;
2191 	*lenp = len;
2192 	if (fit_unamep)
2193 		*fit_unamep = (char *)fit_uname;
2194 	if (fit_uname_configp)
2195 		*fit_uname_configp = (char *)(fit_uname_config ? :
2196 					      fit_base_uname_config);
2197 
2198 	return noffset;
2199 }
2200 
2201 int fit_image_load(bootm_headers_t *images, ulong addr,
2202 		   const char **fit_unamep, const char **fit_uname_configp,
2203 		   int arch, int image_type, int bootstage_id,
2204 		   enum fit_load_op load_op, ulong *datap, ulong *lenp)
2205 {
2206 	return fit_image_load_index(images, addr,fit_unamep, fit_uname_configp,
2207 				    arch, image_type, 0, bootstage_id,
2208 				    load_op, datap, lenp);
2209 }
2210 
2211 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2212 			ulong *setup_start, ulong *setup_len)
2213 {
2214 	int noffset;
2215 	ulong addr;
2216 	ulong len;
2217 	int ret;
2218 
2219 	addr = map_to_sysmem(images->fit_hdr_os);
2220 	noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2221 	if (noffset < 0)
2222 		return noffset;
2223 
2224 	ret = fit_image_load(images, addr, NULL, NULL, arch,
2225 			     IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2226 			     FIT_LOAD_REQUIRED, setup_start, &len);
2227 
2228 	return ret;
2229 }
2230 
2231 #ifndef USE_HOSTCC
2232 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2233 		   const char **fit_unamep, const char **fit_uname_configp,
2234 		   int arch, ulong *datap, ulong *lenp)
2235 {
2236 	int fdt_noffset, cfg_noffset, count;
2237 	const void *fit;
2238 	const char *fit_uname = NULL;
2239 	const char *fit_uname_config = NULL;
2240 	char *fit_uname_config_copy = NULL;
2241 	char *next_config = NULL;
2242 	ulong load, len;
2243 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2244 	ulong image_start, image_end;
2245 	ulong ovload, ovlen;
2246 	const char *uconfig;
2247 	const char *uname;
2248 	void *base, *ov;
2249 	int i, err, noffset, ov_noffset;
2250 #endif
2251 
2252 	fit_uname = fit_unamep ? *fit_unamep : NULL;
2253 
2254 	if (fit_uname_configp && *fit_uname_configp) {
2255 		fit_uname_config_copy = strdup(*fit_uname_configp);
2256 		if (!fit_uname_config_copy)
2257 			return -ENOMEM;
2258 
2259 		next_config = strchr(fit_uname_config_copy, '#');
2260 		if (next_config)
2261 			*next_config++ = '\0';
2262 		if (next_config - 1 > fit_uname_config_copy)
2263 			fit_uname_config = fit_uname_config_copy;
2264 	}
2265 
2266 	fdt_noffset = fit_image_load(images,
2267 		addr, &fit_uname, &fit_uname_config,
2268 		arch, IH_TYPE_FLATDT,
2269 		BOOTSTAGE_ID_FIT_FDT_START,
2270 		FIT_LOAD_OPTIONAL, &load, &len);
2271 
2272 	if (fdt_noffset < 0)
2273 		goto out;
2274 
2275 	debug("fit_uname=%s, fit_uname_config=%s\n",
2276 			fit_uname ? fit_uname : "<NULL>",
2277 			fit_uname_config ? fit_uname_config : "<NULL>");
2278 
2279 	fit = map_sysmem(addr, 0);
2280 
2281 	cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2282 
2283 	/* single blob, or error just return as well */
2284 	count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2285 	if (count <= 1 && !next_config)
2286 		goto out;
2287 
2288 	/* we need to apply overlays */
2289 
2290 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2291 	image_start = addr;
2292 	image_end = addr + fit_get_size(fit);
2293 	/* verify that relocation took place by load address not being in fit */
2294 	if (load >= image_start && load < image_end) {
2295 		/* check is simplified; fit load checks for overlaps */
2296 		printf("Overlayed FDT requires relocation\n");
2297 		fdt_noffset = -EBADF;
2298 		goto out;
2299 	}
2300 
2301 	base = map_sysmem(load, len);
2302 
2303 	/* apply extra configs in FIT first, followed by args */
2304 	for (i = 1; ; i++) {
2305 		if (i < count) {
2306 			noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2307 							       FIT_FDT_PROP, i);
2308 			uname = fit_get_name(fit, noffset, NULL);
2309 			uconfig = NULL;
2310 		} else {
2311 			if (!next_config)
2312 				break;
2313 			uconfig = next_config;
2314 			next_config = strchr(next_config, '#');
2315 			if (next_config)
2316 				*next_config++ = '\0';
2317 			uname = NULL;
2318 		}
2319 
2320 		debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2321 
2322 		ov_noffset = fit_image_load(images,
2323 			addr, &uname, &uconfig,
2324 			arch, IH_TYPE_FLATDT,
2325 			BOOTSTAGE_ID_FIT_FDT_START,
2326 			FIT_LOAD_REQUIRED, &ovload, &ovlen);
2327 		if (ov_noffset < 0) {
2328 			printf("load of %s failed\n", uname);
2329 			continue;
2330 		}
2331 		debug("%s loaded at 0x%08lx len=0x%08lx\n",
2332 				uname, ovload, ovlen);
2333 		ov = map_sysmem(ovload, ovlen);
2334 
2335 		base = map_sysmem(load, len + ovlen);
2336 		err = fdt_open_into(base, base, len + ovlen);
2337 		if (err < 0) {
2338 			printf("failed on fdt_open_into\n");
2339 			fdt_noffset = err;
2340 			goto out;
2341 		}
2342 		/* the verbose method prints out messages on error */
2343 		err = fdt_overlay_apply_verbose(base, ov);
2344 		if (err < 0) {
2345 			fdt_noffset = err;
2346 			goto out;
2347 		}
2348 		fdt_pack(base);
2349 		len = fdt_totalsize(base);
2350 	}
2351 #else
2352 	printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2353 	fdt_noffset = -EBADF;
2354 #endif
2355 
2356 out:
2357 	if (datap)
2358 		*datap = load;
2359 	if (lenp)
2360 		*lenp = len;
2361 	if (fit_unamep)
2362 		*fit_unamep = fit_uname;
2363 	if (fit_uname_configp)
2364 		*fit_uname_configp = fit_uname_config;
2365 
2366 	if (fit_uname_config_copy)
2367 		free(fit_uname_config_copy);
2368 	return fdt_noffset;
2369 }
2370 #endif
2371