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