xref: /rk3399_rockchip-uboot/common/image-fit.c (revision 92a42bc5fecfd4f2d8fba481875cd648d7428ec6)
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  * fit_image_hash_get_algo - get hash algorithm name
954  * @fit: pointer to the FIT format image header
955  * @noffset: hash node offset
956  * @algo: double pointer to char, will hold pointer to the algorithm name
957  *
958  * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
959  * If the property is found its data start address is returned to the caller.
960  *
961  * returns:
962  *     0, on success
963  *     -1, on failure
964  */
965 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
966 {
967 	int len;
968 
969 	*algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
970 	if (*algo == NULL) {
971 		fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
972 		return -1;
973 	}
974 
975 	return 0;
976 }
977 
978 /**
979  * fit_image_hash_get_value - get hash value and length
980  * @fit: pointer to the FIT format image header
981  * @noffset: hash node offset
982  * @value: double pointer to uint8_t, will hold address of a hash value data
983  * @value_len: pointer to an int, will hold hash data length
984  *
985  * fit_image_hash_get_value() finds hash value property in a given hash node.
986  * If the property is found its data start address and size are returned to
987  * the caller.
988  *
989  * returns:
990  *     0, on success
991  *     -1, on failure
992  */
993 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
994 				int *value_len)
995 {
996 	int len;
997 
998 	*value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
999 	if (*value == NULL) {
1000 		fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1001 		*value_len = 0;
1002 		return -1;
1003 	}
1004 
1005 	*value_len = len;
1006 	return 0;
1007 }
1008 
1009 /**
1010  * fit_image_hash_get_ignore - get hash ignore flag
1011  * @fit: pointer to the FIT format image header
1012  * @noffset: hash node offset
1013  * @ignore: pointer to an int, will hold hash ignore flag
1014  *
1015  * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1016  * If the property is found and non-zero, the hash algorithm is not verified by
1017  * u-boot automatically.
1018  *
1019  * returns:
1020  *     0, on ignore not found
1021  *     value, on ignore found
1022  */
1023 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1024 {
1025 	int len;
1026 	int *value;
1027 
1028 	value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1029 	if (value == NULL || len != sizeof(int))
1030 		*ignore = 0;
1031 	else
1032 		*ignore = *value;
1033 
1034 	return 0;
1035 }
1036 
1037 ulong fit_get_end(const void *fit)
1038 {
1039 	return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1040 }
1041 
1042 /**
1043  * fit_set_timestamp - set node timestamp property
1044  * @fit: pointer to the FIT format image header
1045  * @noffset: node offset
1046  * @timestamp: timestamp value to be set
1047  *
1048  * fit_set_timestamp() attempts to set timestamp property in the requested
1049  * node and returns operation status to the caller.
1050  *
1051  * returns:
1052  *     0, on success
1053  *     -ENOSPC if no space in device tree, -1 for other error
1054  */
1055 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1056 {
1057 	uint32_t t;
1058 	int ret;
1059 
1060 	t = cpu_to_uimage(timestamp);
1061 	ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1062 				sizeof(uint32_t));
1063 	if (ret) {
1064 		debug("Can't set '%s' property for '%s' node (%s)\n",
1065 		      FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1066 		      fdt_strerror(ret));
1067 		return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1068 	}
1069 
1070 	return 0;
1071 }
1072 
1073 /**
1074  * calculate_hash - calculate and return hash for provided input data
1075  * @data: pointer to the input data
1076  * @data_len: data length
1077  * @algo: requested hash algorithm
1078  * @value: pointer to the char, will hold hash value data (caller must
1079  * allocate enough free space)
1080  * value_len: length of the calculated hash
1081  *
1082  * calculate_hash() computes input data hash according to the requested
1083  * algorithm.
1084  * Resulting hash value is placed in caller provided 'value' buffer, length
1085  * of the calculated hash is returned via value_len pointer argument.
1086  *
1087  * returns:
1088  *     0, on success
1089  *    -1, when algo is unsupported
1090  */
1091 int calculate_hash_software(const void *data, int data_len,
1092 			    const char *algo, uint8_t *value,
1093 			    int *value_len)
1094 {
1095 	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1096 		*((uint32_t *)value) = crc32_wd(0, data, data_len,
1097 							CHUNKSZ_CRC32);
1098 		*((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1099 		*value_len = 4;
1100 	} else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1101 		sha1_csum_wd((unsigned char *)data, data_len,
1102 			     (unsigned char *)value, CHUNKSZ_SHA1);
1103 		*value_len = 20;
1104 	} else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1105 		sha256_csum_wd((unsigned char *)data, data_len,
1106 			       (unsigned char *)value, CHUNKSZ_SHA256);
1107 		*value_len = SHA256_SUM_LEN;
1108 	} else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1109 		md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1110 		*value_len = 16;
1111 	} else {
1112 		debug("Unsupported hash alogrithm\n");
1113 		return -1;
1114 	}
1115 	return 0;
1116 }
1117 
1118 #ifdef USE_HOSTCC
1119 int calculate_hash(const void *data, int data_len, const char *algo,
1120 		   uint8_t *value, int *value_len)
1121 {
1122 	return calculate_hash_software(data, data_len, algo, value, value_len);
1123 }
1124 #else
1125 #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO)
1126 static int crypto_csum(u32 cap, const char *data, int len, u8 *output)
1127 {
1128 	struct udevice *dev;
1129 	sha_context csha_ctx;
1130 
1131 	dev = crypto_get_device(cap);
1132 	if (!dev) {
1133 		printf("Can't find expected crypto device\n");
1134 		return -ENODEV;
1135 	}
1136 
1137 	csha_ctx.algo = cap;
1138 	csha_ctx.length = len;
1139 
1140 	return crypto_sha_csum(dev, &csha_ctx, (char *)data, len, output);
1141 }
1142 
1143 int calculate_hash(const void *data, int data_len, const char *algo,
1144 		   uint8_t *value, int *value_len)
1145 {
1146 	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1147 		*((uint32_t *)value) = crc32_wd(0, data, data_len,
1148 							CHUNKSZ_CRC32);
1149 		*((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1150 		*value_len = 4;
1151 	} else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1152 		crypto_csum(CRYPTO_SHA1, data, data_len, value);
1153 		*value_len = 20;
1154 	} else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1155 		crypto_csum(CRYPTO_SHA256, data, data_len, value);
1156 		*value_len = SHA256_SUM_LEN;
1157 	} else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1158 		crypto_csum(CRYPTO_MD5, data, data_len, value);
1159 		*value_len = 16;
1160 	} else {
1161 		debug("Unsupported hash alogrithm\n");
1162 		return -1;
1163 	}
1164 	return 0;
1165 }
1166 #else
1167 int calculate_hash(const void *data, int data_len, const char *algo,
1168 		   uint8_t *value, int *value_len)
1169 {
1170 	return calculate_hash_software(data, data_len, algo, value, value_len);
1171 }
1172 #endif
1173 #endif
1174 
1175 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1176 				size_t size, char **err_msgp)
1177 {
1178 	uint8_t value[FIT_MAX_HASH_LEN];
1179 	int value_len;
1180 	char *algo;
1181 	uint8_t *fit_value;
1182 	int fit_value_len;
1183 	int ignore;
1184 
1185 	*err_msgp = NULL;
1186 
1187 	if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1188 		*err_msgp = "Can't get hash algo property";
1189 		return -1;
1190 	}
1191 	printf("%s", algo);
1192 
1193 	if (IMAGE_ENABLE_IGNORE) {
1194 		fit_image_hash_get_ignore(fit, noffset, &ignore);
1195 		if (ignore) {
1196 			printf("-skipped ");
1197 			return 0;
1198 		}
1199 	}
1200 
1201 	if (fit_image_hash_get_value(fit, noffset, &fit_value,
1202 				     &fit_value_len)) {
1203 		*err_msgp = "Can't get hash value property";
1204 		return -1;
1205 	}
1206 
1207 	if (calculate_hash(data, size, algo, value, &value_len)) {
1208 		*err_msgp = "Unsupported hash algorithm";
1209 		return -1;
1210 	}
1211 
1212 	if (value_len != fit_value_len) {
1213 		*err_msgp = "Bad hash value len";
1214 		return -1;
1215 	} else if (memcmp(value, fit_value, value_len) != 0) {
1216 		*err_msgp = "Bad hash value";
1217 		return -1;
1218 	}
1219 
1220 	return 0;
1221 }
1222 
1223 int fit_image_verify_with_data(const void *fit, int image_noffset,
1224 			       const void *data, size_t size)
1225 {
1226 	int		noffset = 0;
1227 	char		*err_msg = "";
1228 	int verify_all = 1;
1229 	int ret;
1230 
1231 	/* Verify all required signatures */
1232 	if (IMAGE_ENABLE_VERIFY &&
1233 	    fit_image_verify_required_sigs(fit, image_noffset, data, size,
1234 					   gd_fdt_blob(), &verify_all)) {
1235 		err_msg = "Unable to verify required signature";
1236 		goto error;
1237 	}
1238 
1239 	/* Process all hash subnodes of the component image node */
1240 	fdt_for_each_subnode(noffset, fit, image_noffset) {
1241 		const char *name = fit_get_name(fit, noffset, NULL);
1242 
1243 		/*
1244 		 * Check subnode name, must be equal to "hash".
1245 		 * Multiple hash nodes require unique unit node
1246 		 * names, e.g. hash@1, hash@2, etc.
1247 		 */
1248 		if (!strncmp(name, FIT_HASH_NODENAME,
1249 			     strlen(FIT_HASH_NODENAME))) {
1250 			if (fit_image_check_hash(fit, noffset, data, size,
1251 						 &err_msg))
1252 				goto error;
1253 			puts("+ ");
1254 		} else if (IMAGE_ENABLE_VERIFY && verify_all &&
1255 				!strncmp(name, FIT_SIG_NODENAME,
1256 					strlen(FIT_SIG_NODENAME))) {
1257 			ret = fit_image_check_sig(fit, noffset, data,
1258 							size, -1, &err_msg);
1259 
1260 			/*
1261 			 * Show an indication on failure, but do not return
1262 			 * an error. Only keys marked 'required' can cause
1263 			 * an image validation failure. See the call to
1264 			 * fit_image_verify_required_sigs() above.
1265 			 */
1266 			if (ret)
1267 				puts("- ");
1268 			else
1269 				puts("+ ");
1270 		}
1271 	}
1272 
1273 	if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1274 		err_msg = "Corrupted or truncated tree";
1275 		goto error;
1276 	}
1277 
1278 	return 1;
1279 
1280 error:
1281 	printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1282 	       err_msg, fit_get_name(fit, noffset, NULL),
1283 	       fit_get_name(fit, image_noffset, NULL));
1284 	return 0;
1285 }
1286 
1287 /**
1288  * fit_image_verify - verify data integrity
1289  * @fit: pointer to the FIT format image header
1290  * @image_noffset: component image node offset
1291  *
1292  * fit_image_verify() goes over component image hash nodes,
1293  * re-calculates each data hash and compares with the value stored in hash
1294  * node.
1295  *
1296  * returns:
1297  *     1, if all hashes are valid
1298  *     0, otherwise (or on error)
1299  */
1300 int fit_image_verify(const void *fit, int image_noffset)
1301 {
1302 	const void	*data;
1303 	size_t		size;
1304 	int		noffset = 0;
1305 	char		*err_msg = "";
1306 
1307 	/* Get image data and data length */
1308 	if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1309 		err_msg = "Can't get image data/size";
1310 		printf("error!\n%s for '%s' hash node in '%s' image node\n",
1311 		       err_msg, fit_get_name(fit, noffset, NULL),
1312 		       fit_get_name(fit, image_noffset, NULL));
1313 		return 0;
1314 	}
1315 
1316 	return fit_image_verify_with_data(fit, image_noffset, data, size);
1317 }
1318 
1319 /**
1320  * fit_all_image_verify - verify data integrity for all images
1321  * @fit: pointer to the FIT format image header
1322  *
1323  * fit_all_image_verify() goes over all images in the FIT and
1324  * for every images checks if all it's hashes are valid.
1325  *
1326  * returns:
1327  *     1, if all hashes of all images are valid
1328  *     0, otherwise (or on error)
1329  */
1330 int fit_all_image_verify(const void *fit)
1331 {
1332 	int images_noffset;
1333 	int noffset;
1334 	int ndepth;
1335 	int count;
1336 
1337 	/* Find images parent node offset */
1338 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1339 	if (images_noffset < 0) {
1340 		printf("Can't find images parent node '%s' (%s)\n",
1341 		       FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1342 		return 0;
1343 	}
1344 
1345 	/* Process all image subnodes, check hashes for each */
1346 	printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1347 	       (ulong)fit);
1348 	for (ndepth = 0, count = 0,
1349 	     noffset = fdt_next_node(fit, images_noffset, &ndepth);
1350 			(noffset >= 0) && (ndepth > 0);
1351 			noffset = fdt_next_node(fit, noffset, &ndepth)) {
1352 		if (ndepth == 1) {
1353 			/*
1354 			 * Direct child node of the images parent node,
1355 			 * i.e. component image node.
1356 			 */
1357 			printf("   Hash(es) for Image %u (%s): ", count,
1358 			       fit_get_name(fit, noffset, NULL));
1359 			count++;
1360 
1361 			if (!fit_image_verify(fit, noffset))
1362 				return 0;
1363 			printf("\n");
1364 		}
1365 	}
1366 	return 1;
1367 }
1368 
1369 /**
1370  * fit_image_check_os - check whether image node is of a given os type
1371  * @fit: pointer to the FIT format image header
1372  * @noffset: component image node offset
1373  * @os: requested image os
1374  *
1375  * fit_image_check_os() reads image os property and compares its numeric
1376  * id with the requested os. Comparison result is returned to the caller.
1377  *
1378  * returns:
1379  *     1 if image is of given os type
1380  *     0 otherwise (or on error)
1381  */
1382 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1383 {
1384 	uint8_t image_os;
1385 
1386 	if (fit_image_get_os(fit, noffset, &image_os))
1387 		return 0;
1388 	return (os == image_os);
1389 }
1390 
1391 /**
1392  * fit_image_check_arch - check whether image node is of a given arch
1393  * @fit: pointer to the FIT format image header
1394  * @noffset: component image node offset
1395  * @arch: requested imagearch
1396  *
1397  * fit_image_check_arch() reads image arch property and compares its numeric
1398  * id with the requested arch. Comparison result is returned to the caller.
1399  *
1400  * returns:
1401  *     1 if image is of given arch
1402  *     0 otherwise (or on error)
1403  */
1404 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1405 {
1406 	uint8_t image_arch;
1407 	int aarch32_support = 0;
1408 
1409 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1410 	aarch32_support = 1;
1411 #endif
1412 
1413 	if (fit_image_get_arch(fit, noffset, &image_arch))
1414 		return 0;
1415 	return (arch == image_arch) ||
1416 		(arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1417 		(arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1418 		 aarch32_support);
1419 }
1420 
1421 /**
1422  * fit_image_check_type - check whether image node is of a given type
1423  * @fit: pointer to the FIT format image header
1424  * @noffset: component image node offset
1425  * @type: requested image type
1426  *
1427  * fit_image_check_type() reads image type property and compares its numeric
1428  * id with the requested type. Comparison result is returned to the caller.
1429  *
1430  * returns:
1431  *     1 if image is of given type
1432  *     0 otherwise (or on error)
1433  */
1434 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1435 {
1436 	uint8_t image_type;
1437 
1438 	if (fit_image_get_type(fit, noffset, &image_type))
1439 		return 0;
1440 	return (type == image_type);
1441 }
1442 
1443 /**
1444  * fit_image_check_comp - check whether image node uses given compression
1445  * @fit: pointer to the FIT format image header
1446  * @noffset: component image node offset
1447  * @comp: requested image compression type
1448  *
1449  * fit_image_check_comp() reads image compression property and compares its
1450  * numeric id with the requested compression type. Comparison result is
1451  * returned to the caller.
1452  *
1453  * returns:
1454  *     1 if image uses requested compression
1455  *     0 otherwise (or on error)
1456  */
1457 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1458 {
1459 	uint8_t image_comp;
1460 
1461 	if (fit_image_get_comp(fit, noffset, &image_comp))
1462 		return 0;
1463 	return (comp == image_comp);
1464 }
1465 
1466 /**
1467  * fit_check_format - sanity check FIT image format
1468  * @fit: pointer to the FIT format image header
1469  *
1470  * fit_check_format() runs a basic sanity FIT image verification.
1471  * Routine checks for mandatory properties, nodes, etc.
1472  *
1473  * returns:
1474  *     1, on success
1475  *     0, on failure
1476  */
1477 int fit_check_format(const void *fit)
1478 {
1479 	/* mandatory / node 'description' property */
1480 	if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1481 		debug("Wrong FIT format: no description\n");
1482 		return 0;
1483 	}
1484 
1485 	if (IMAGE_ENABLE_TIMESTAMP) {
1486 		/* mandatory / node 'timestamp' property */
1487 		if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1488 			debug("Wrong FIT format: no timestamp\n");
1489 			return 0;
1490 		}
1491 	}
1492 
1493 	/* mandatory subimages parent '/images' node */
1494 	if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1495 		debug("Wrong FIT format: no images parent node\n");
1496 		return 0;
1497 	}
1498 
1499 	return 1;
1500 }
1501 
1502 
1503 /**
1504  * fit_conf_find_compat
1505  * @fit: pointer to the FIT format image header
1506  * @fdt: pointer to the device tree to compare against
1507  *
1508  * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1509  * most compatible with the passed in device tree.
1510  *
1511  * Example:
1512  *
1513  * / o image-tree
1514  *   |-o images
1515  *   | |-o fdt@1
1516  *   | |-o fdt@2
1517  *   |
1518  *   |-o configurations
1519  *     |-o config@1
1520  *     | |-fdt = fdt@1
1521  *     |
1522  *     |-o config@2
1523  *       |-fdt = fdt@2
1524  *
1525  * / o U-Boot fdt
1526  *   |-compatible = "foo,bar", "bim,bam"
1527  *
1528  * / o kernel fdt1
1529  *   |-compatible = "foo,bar",
1530  *
1531  * / o kernel fdt2
1532  *   |-compatible = "bim,bam", "baz,biz"
1533  *
1534  * Configuration 1 would be picked because the first string in U-Boot's
1535  * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1536  * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1537  *
1538  * returns:
1539  *     offset to the configuration to use if one was found
1540  *     -1 otherwise
1541  */
1542 int fit_conf_find_compat(const void *fit, const void *fdt)
1543 {
1544 	int ndepth = 0;
1545 	int noffset, confs_noffset, images_noffset;
1546 	const void *fdt_compat;
1547 	int fdt_compat_len;
1548 	int best_match_offset = 0;
1549 	int best_match_pos = 0;
1550 
1551 	confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1552 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1553 	if (confs_noffset < 0 || images_noffset < 0) {
1554 		debug("Can't find configurations or images nodes.\n");
1555 		return -1;
1556 	}
1557 
1558 	fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1559 	if (!fdt_compat) {
1560 		debug("Fdt for comparison has no \"compatible\" property.\n");
1561 		return -1;
1562 	}
1563 
1564 	/*
1565 	 * Loop over the configurations in the FIT image.
1566 	 */
1567 	for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1568 			(noffset >= 0) && (ndepth > 0);
1569 			noffset = fdt_next_node(fit, noffset, &ndepth)) {
1570 		const void *kfdt;
1571 		const char *kfdt_name;
1572 		int kfdt_noffset;
1573 		const char *cur_fdt_compat;
1574 		int len;
1575 		size_t size;
1576 		int i;
1577 
1578 		if (ndepth > 1)
1579 			continue;
1580 
1581 		kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1582 		if (!kfdt_name) {
1583 			debug("No fdt property found.\n");
1584 			continue;
1585 		}
1586 		kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1587 						  kfdt_name);
1588 		if (kfdt_noffset < 0) {
1589 			debug("No image node named \"%s\" found.\n",
1590 			      kfdt_name);
1591 			continue;
1592 		}
1593 		/*
1594 		 * Get a pointer to this configuration's fdt.
1595 		 */
1596 		if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1597 			debug("Failed to get fdt \"%s\".\n", kfdt_name);
1598 			continue;
1599 		}
1600 
1601 		len = fdt_compat_len;
1602 		cur_fdt_compat = fdt_compat;
1603 		/*
1604 		 * Look for a match for each U-Boot compatibility string in
1605 		 * turn in this configuration's fdt.
1606 		 */
1607 		for (i = 0; len > 0 &&
1608 		     (!best_match_offset || best_match_pos > i); i++) {
1609 			int cur_len = strlen(cur_fdt_compat) + 1;
1610 
1611 			if (!fdt_node_check_compatible(kfdt, 0,
1612 						       cur_fdt_compat)) {
1613 				best_match_offset = noffset;
1614 				best_match_pos = i;
1615 				break;
1616 			}
1617 			len -= cur_len;
1618 			cur_fdt_compat += cur_len;
1619 		}
1620 	}
1621 	if (!best_match_offset) {
1622 		debug("No match found.\n");
1623 		return -1;
1624 	}
1625 
1626 	return best_match_offset;
1627 }
1628 
1629 /**
1630  * fit_conf_get_node - get node offset for configuration of a given unit name
1631  * @fit: pointer to the FIT format image header
1632  * @conf_uname: configuration node unit name
1633  *
1634  * fit_conf_get_node() finds a configuration (within the '/configurations'
1635  * parent node) of a provided unit name. If configuration is found its node
1636  * offset is returned to the caller.
1637  *
1638  * When NULL is provided in second argument fit_conf_get_node() will search
1639  * for a default configuration node instead. Default configuration node unit
1640  * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1641  * node.
1642  *
1643  * returns:
1644  *     configuration node offset when found (>=0)
1645  *     negative number on failure (FDT_ERR_* code)
1646  */
1647 int fit_conf_get_node(const void *fit, const char *conf_uname)
1648 {
1649 	int noffset, confs_noffset;
1650 	int len;
1651 	const char *s;
1652 	char *conf_uname_copy = NULL;
1653 
1654 	confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1655 	if (confs_noffset < 0) {
1656 		debug("Can't find configurations parent node '%s' (%s)\n",
1657 		      FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1658 		return confs_noffset;
1659 	}
1660 
1661 	if (conf_uname == NULL) {
1662 		/* get configuration unit name from the default property */
1663 		debug("No configuration specified, trying default...\n");
1664 		conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1665 						 FIT_DEFAULT_PROP, &len);
1666 		if (conf_uname == NULL) {
1667 			fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1668 				      len);
1669 			return len;
1670 		}
1671 		debug("Found default configuration: '%s'\n", conf_uname);
1672 	}
1673 
1674 	s = strchr(conf_uname, '#');
1675 	if (s) {
1676 		len = s - conf_uname;
1677 		conf_uname_copy = malloc(len + 1);
1678 		if (!conf_uname_copy) {
1679 			debug("Can't allocate uname copy: '%s'\n",
1680 					conf_uname);
1681 			return -ENOMEM;
1682 		}
1683 		memcpy(conf_uname_copy, conf_uname, len);
1684 		conf_uname_copy[len] = '\0';
1685 		conf_uname = conf_uname_copy;
1686 	}
1687 
1688 	noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1689 	if (noffset < 0) {
1690 		debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1691 		      conf_uname, fdt_strerror(noffset));
1692 	}
1693 
1694 	if (conf_uname_copy)
1695 		free(conf_uname_copy);
1696 
1697 	return noffset;
1698 }
1699 
1700 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1701 		const char *prop_name)
1702 {
1703 	return fdt_stringlist_count(fit, noffset, prop_name);
1704 }
1705 
1706 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1707 		const char *prop_name, int index)
1708 {
1709 	const char *uname;
1710 	int len;
1711 
1712 	/* get kernel image unit name from configuration kernel property */
1713 	uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1714 	if (uname == NULL)
1715 		return len;
1716 
1717 	return fit_image_get_node(fit, uname);
1718 }
1719 
1720 int fit_conf_get_prop_node(const void *fit, int noffset,
1721 		const char *prop_name)
1722 {
1723 	return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1724 }
1725 
1726 /**
1727  * fit_conf_print - prints out the FIT configuration details
1728  * @fit: pointer to the FIT format image header
1729  * @noffset: offset of the configuration node
1730  * @p: pointer to prefix string
1731  *
1732  * fit_conf_print() lists all mandatory properties for the processed
1733  * configuration node.
1734  *
1735  * returns:
1736  *     no returned results
1737  */
1738 void fit_conf_print(const void *fit, int noffset, const char *p)
1739 {
1740 	char *desc;
1741 	const char *uname;
1742 	int ret;
1743 	int fdt_index, loadables_index;
1744 
1745 	/* Mandatory properties */
1746 	ret = fit_get_desc(fit, noffset, &desc);
1747 	printf("%s  Description:  ", p);
1748 	if (ret)
1749 		printf("unavailable\n");
1750 	else
1751 		printf("%s\n", desc);
1752 
1753 	uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1754 	printf("%s  Kernel:       ", p);
1755 	if (uname == NULL)
1756 		printf("unavailable\n");
1757 	else
1758 		printf("%s\n", uname);
1759 
1760 	/* Optional properties */
1761 	uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1762 	if (uname)
1763 		printf("%s  Init Ramdisk: %s\n", p, uname);
1764 
1765 	uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
1766 	if (uname)
1767 		printf("%s  Firmware:     %s\n", p, uname);
1768 
1769 	for (fdt_index = 0;
1770 	     uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
1771 					fdt_index, NULL), uname;
1772 	     fdt_index++) {
1773 
1774 		if (fdt_index == 0)
1775 			printf("%s  FDT:          ", p);
1776 		else
1777 			printf("%s                ", p);
1778 		printf("%s\n", uname);
1779 	}
1780 
1781 	uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
1782 	if (uname)
1783 		printf("%s  FPGA:         %s\n", p, uname);
1784 
1785 	/* Print out all of the specified loadables */
1786 	for (loadables_index = 0;
1787 	     uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
1788 					loadables_index, NULL), uname;
1789 	     loadables_index++) {
1790 		if (loadables_index == 0) {
1791 			printf("%s  Loadables:    ", p);
1792 		} else {
1793 			printf("%s                ", p);
1794 		}
1795 		printf("%s\n", uname);
1796 	}
1797 }
1798 
1799 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1800 {
1801 	fit_image_print(fit, rd_noffset, "   ");
1802 
1803 	if (verify) {
1804 		puts("   Verifying Hash Integrity ... ");
1805 		if (!fit_image_verify(fit, rd_noffset)) {
1806 			puts("Bad Data Hash\n");
1807 			return -EACCES;
1808 		}
1809 		puts("OK\n");
1810 	}
1811 
1812 	return 0;
1813 }
1814 
1815 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1816 			ulong addr)
1817 {
1818 	int cfg_noffset;
1819 	void *fit_hdr;
1820 	int noffset;
1821 
1822 	debug("*  %s: using config '%s' from image at 0x%08lx\n",
1823 	      prop_name, images->fit_uname_cfg, addr);
1824 
1825 	/* Check whether configuration has this property defined */
1826 	fit_hdr = map_sysmem(addr, 0);
1827 	cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1828 	if (cfg_noffset < 0) {
1829 		debug("*  %s: no such config\n", prop_name);
1830 		return -EINVAL;
1831 	}
1832 
1833 	noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1834 	if (noffset < 0) {
1835 		debug("*  %s: no '%s' in config\n", prop_name, prop_name);
1836 		return -ENOENT;
1837 	}
1838 
1839 	return noffset;
1840 }
1841 
1842 /**
1843  * fit_get_image_type_property() - get property name for IH_TYPE_...
1844  *
1845  * @return the properly name where we expect to find the image in the
1846  * config node
1847  */
1848 static const char *fit_get_image_type_property(int type)
1849 {
1850 	/*
1851 	 * This is sort-of available in the uimage_type[] table in image.c
1852 	 * but we don't have access to the short name, and "fdt" is different
1853 	 * anyway. So let's just keep it here.
1854 	 */
1855 	switch (type) {
1856 	case IH_TYPE_FLATDT:
1857 		return FIT_FDT_PROP;
1858 	case IH_TYPE_KERNEL:
1859 		return FIT_KERNEL_PROP;
1860 	case IH_TYPE_RAMDISK:
1861 		return FIT_RAMDISK_PROP;
1862 	case IH_TYPE_FIRMWARE:
1863 		return FIT_FIRMWARE_PROP;
1864 	case IH_TYPE_X86_SETUP:
1865 		return FIT_SETUP_PROP;
1866 	case IH_TYPE_LOADABLE:
1867 		return FIT_LOADABLE_PROP;
1868 	case IH_TYPE_FPGA:
1869 		return FIT_FPGA_PROP;
1870 	}
1871 
1872 	return "unknown";
1873 }
1874 
1875 int fit_image_load_index(bootm_headers_t *images, ulong addr,
1876 			 const char **fit_unamep, const char **fit_uname_configp,
1877 			 int arch, int image_type, int image_index, int bootstage_id,
1878 			 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1879 {
1880 	int cfg_noffset, noffset;
1881 	const char *fit_uname;
1882 	const char *fit_uname_config;
1883 	const char *fit_base_uname_config;
1884 	const void *fit;
1885 	const void *buf;
1886 	size_t size;
1887 	int type_ok, os_ok;
1888 	ulong load, data, len;
1889 	uint8_t os;
1890 #ifndef USE_HOSTCC
1891 	uint8_t os_arch;
1892 #endif
1893 	const char *prop_name;
1894 	int ret;
1895 
1896 	fit = map_sysmem(addr, 0);
1897 	fit_uname = fit_unamep ? *fit_unamep : NULL;
1898 	fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1899 	fit_base_uname_config = NULL;
1900 	prop_name = fit_get_image_type_property(image_type);
1901 	printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1902 
1903 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1904 	if (!fit_check_format(fit)) {
1905 		printf("Bad FIT %s image format!\n", prop_name);
1906 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1907 		return -ENOEXEC;
1908 	}
1909 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1910 	if (fit_uname) {
1911 		/* get FIT component image node offset */
1912 		bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1913 		noffset = fit_image_get_node(fit, fit_uname);
1914 	} else {
1915 		/*
1916 		 * no image node unit name, try to get config
1917 		 * node first. If config unit node name is NULL
1918 		 * fit_conf_get_node() will try to find default config node
1919 		 */
1920 		bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1921 		if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1922 			cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1923 		} else {
1924 			cfg_noffset = fit_conf_get_node(fit,
1925 							fit_uname_config);
1926 		}
1927 		if (cfg_noffset < 0) {
1928 			puts("Could not find configuration node\n");
1929 			bootstage_error(bootstage_id +
1930 					BOOTSTAGE_SUB_NO_UNIT_NAME);
1931 			return -ENOENT;
1932 		}
1933 		fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1934 		printf("   Using '%s' configuration\n", fit_base_uname_config);
1935 		if (image_type == IH_TYPE_KERNEL) {
1936 			/* Remember (and possibly verify) this config */
1937 			images->fit_uname_cfg = fit_base_uname_config;
1938 			if (IMAGE_ENABLE_VERIFY && images->verify) {
1939 				puts("   Verifying Hash Integrity ... ");
1940 				if (fit_config_verify(fit, cfg_noffset)) {
1941 					puts("Bad Data Hash\n");
1942 					bootstage_error(bootstage_id +
1943 						BOOTSTAGE_SUB_HASH);
1944 					return -EACCES;
1945 				}
1946 				puts("OK\n");
1947 			}
1948 			bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1949 		}
1950 
1951 		noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
1952 						       prop_name, image_index);
1953 		fit_uname = fit_get_name(fit, noffset, NULL);
1954 	}
1955 	if (noffset < 0) {
1956 		puts("Could not find subimage node\n");
1957 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1958 		return -ENOENT;
1959 	}
1960 
1961 	printf("   Trying '%s' %s subimage\n", fit_uname, prop_name);
1962 
1963 	ret = fit_image_select(fit, noffset, images->verify);
1964 	if (ret) {
1965 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1966 		return ret;
1967 	}
1968 
1969 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1970 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1971 	if (!fit_image_check_target_arch(fit, noffset)) {
1972 		puts("Unsupported Architecture\n");
1973 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1974 		return -ENOEXEC;
1975 	}
1976 #endif
1977 
1978 #ifndef USE_HOSTCC
1979 	fit_image_get_arch(fit, noffset, &os_arch);
1980 	images->os.arch = os_arch;
1981 #endif
1982 
1983 	if (image_type == IH_TYPE_FLATDT &&
1984 	    !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1985 		puts("FDT image is compressed");
1986 		return -EPROTONOSUPPORT;
1987 	}
1988 
1989 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1990 	type_ok = fit_image_check_type(fit, noffset, image_type) ||
1991 		  fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1992 		  (image_type == IH_TYPE_KERNEL &&
1993 		   fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1994 
1995 	os_ok = image_type == IH_TYPE_FLATDT ||
1996 		image_type == IH_TYPE_FPGA ||
1997 		fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1998 		fit_image_check_os(fit, noffset, IH_OS_ARM_TRUSTED_FIRMWARE) ||
1999 		fit_image_check_os(fit, noffset, IH_OS_OP_TEE) ||
2000 		fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
2001 		fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
2002 
2003 	/*
2004 	 * If either of the checks fail, we should report an error, but
2005 	 * if the image type is coming from the "loadables" field, we
2006 	 * don't care what it is
2007 	 */
2008 	if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
2009 		fit_image_get_os(fit, noffset, &os);
2010 		printf("No %s %s %s Image\n",
2011 		       genimg_get_os_name(os),
2012 		       genimg_get_arch_name(arch),
2013 		       genimg_get_type_name(image_type));
2014 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2015 		return -EIO;
2016 	}
2017 
2018 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2019 
2020 	/* get image data address and length */
2021 	if (fit_image_get_data(fit, noffset, &buf, &size)) {
2022 		printf("Could not find %s subimage data!\n", prop_name);
2023 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2024 		return -ENOENT;
2025 	}
2026 
2027 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
2028 	/* perform any post-processing on the image data */
2029 	board_fit_image_post_process((void **)&buf, &size);
2030 #endif
2031 
2032 	len = (ulong)size;
2033 
2034 	/* verify that image data is a proper FDT blob */
2035 	if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
2036 		puts("Subimage data is not a FDT");
2037 		return -ENOEXEC;
2038 	}
2039 
2040 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2041 
2042 	/*
2043 	 * Work-around for eldk-4.2 which gives this warning if we try to
2044 	 * cast in the unmap_sysmem() call:
2045 	 * warning: initialization discards qualifiers from pointer target type
2046 	 */
2047 	{
2048 		void *vbuf = (void *)buf;
2049 
2050 		data = map_to_sysmem(vbuf);
2051 	}
2052 
2053 	if (load_op == FIT_LOAD_IGNORED) {
2054 		/* Don't load */
2055 	} else if (fit_image_get_load(fit, noffset, &load)) {
2056 		if (load_op == FIT_LOAD_REQUIRED) {
2057 			printf("Can't get %s subimage load address!\n",
2058 			       prop_name);
2059 			bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2060 			return -EBADF;
2061 		}
2062 	} else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
2063 		ulong image_start, image_end;
2064 		ulong load_end;
2065 		void *dst;
2066 
2067 		/*
2068 		 * move image data to the load address,
2069 		 * make sure we don't overwrite initial image
2070 		 */
2071 		image_start = addr;
2072 		image_end = addr + fit_get_size(fit);
2073 
2074 		load_end = load + len;
2075 		if (image_type != IH_TYPE_KERNEL &&
2076 		    load < image_end && load_end > image_start) {
2077 			printf("Error: %s overwritten\n", prop_name);
2078 			return -EXDEV;
2079 		}
2080 
2081 		printf("   Loading %s from 0x%08lx to 0x%08lx\n",
2082 		       prop_name, data, load);
2083 
2084 		dst = map_sysmem(load, len);
2085 		memmove(dst, buf, len);
2086 		data = load;
2087 	}
2088 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2089 
2090 	*datap = data;
2091 	*lenp = len;
2092 	if (fit_unamep)
2093 		*fit_unamep = (char *)fit_uname;
2094 	if (fit_uname_configp)
2095 		*fit_uname_configp = (char *)(fit_uname_config ? :
2096 					      fit_base_uname_config);
2097 
2098 	return noffset;
2099 }
2100 
2101 int fit_image_load(bootm_headers_t *images, ulong addr,
2102 		   const char **fit_unamep, const char **fit_uname_configp,
2103 		   int arch, int image_type, int bootstage_id,
2104 		   enum fit_load_op load_op, ulong *datap, ulong *lenp)
2105 {
2106 	return fit_image_load_index(images, addr,fit_unamep, fit_uname_configp,
2107 				    arch, image_type, 0, bootstage_id,
2108 				    load_op, datap, lenp);
2109 }
2110 
2111 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2112 			ulong *setup_start, ulong *setup_len)
2113 {
2114 	int noffset;
2115 	ulong addr;
2116 	ulong len;
2117 	int ret;
2118 
2119 	addr = map_to_sysmem(images->fit_hdr_os);
2120 	noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2121 	if (noffset < 0)
2122 		return noffset;
2123 
2124 	ret = fit_image_load(images, addr, NULL, NULL, arch,
2125 			     IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2126 			     FIT_LOAD_REQUIRED, setup_start, &len);
2127 
2128 	return ret;
2129 }
2130 
2131 #ifndef USE_HOSTCC
2132 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2133 		   const char **fit_unamep, const char **fit_uname_configp,
2134 		   int arch, ulong *datap, ulong *lenp)
2135 {
2136 	int fdt_noffset, cfg_noffset, count;
2137 	const void *fit;
2138 	const char *fit_uname = NULL;
2139 	const char *fit_uname_config = NULL;
2140 	char *fit_uname_config_copy = NULL;
2141 	char *next_config = NULL;
2142 	ulong load, len;
2143 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2144 	ulong image_start, image_end;
2145 	ulong ovload, ovlen;
2146 	const char *uconfig;
2147 	const char *uname;
2148 	void *base, *ov;
2149 	int i, err, noffset, ov_noffset;
2150 #endif
2151 
2152 	fit_uname = fit_unamep ? *fit_unamep : NULL;
2153 
2154 	if (fit_uname_configp && *fit_uname_configp) {
2155 		fit_uname_config_copy = strdup(*fit_uname_configp);
2156 		if (!fit_uname_config_copy)
2157 			return -ENOMEM;
2158 
2159 		next_config = strchr(fit_uname_config_copy, '#');
2160 		if (next_config)
2161 			*next_config++ = '\0';
2162 		if (next_config - 1 > fit_uname_config_copy)
2163 			fit_uname_config = fit_uname_config_copy;
2164 	}
2165 
2166 	fdt_noffset = fit_image_load(images,
2167 		addr, &fit_uname, &fit_uname_config,
2168 		arch, IH_TYPE_FLATDT,
2169 		BOOTSTAGE_ID_FIT_FDT_START,
2170 		FIT_LOAD_OPTIONAL, &load, &len);
2171 
2172 	if (fdt_noffset < 0)
2173 		goto out;
2174 
2175 	debug("fit_uname=%s, fit_uname_config=%s\n",
2176 			fit_uname ? fit_uname : "<NULL>",
2177 			fit_uname_config ? fit_uname_config : "<NULL>");
2178 
2179 	fit = map_sysmem(addr, 0);
2180 
2181 	cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2182 
2183 	/* single blob, or error just return as well */
2184 	count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2185 	if (count <= 1 && !next_config)
2186 		goto out;
2187 
2188 	/* we need to apply overlays */
2189 
2190 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2191 	image_start = addr;
2192 	image_end = addr + fit_get_size(fit);
2193 	/* verify that relocation took place by load address not being in fit */
2194 	if (load >= image_start && load < image_end) {
2195 		/* check is simplified; fit load checks for overlaps */
2196 		printf("Overlayed FDT requires relocation\n");
2197 		fdt_noffset = -EBADF;
2198 		goto out;
2199 	}
2200 
2201 	base = map_sysmem(load, len);
2202 
2203 	/* apply extra configs in FIT first, followed by args */
2204 	for (i = 1; ; i++) {
2205 		if (i < count) {
2206 			noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2207 							       FIT_FDT_PROP, i);
2208 			uname = fit_get_name(fit, noffset, NULL);
2209 			uconfig = NULL;
2210 		} else {
2211 			if (!next_config)
2212 				break;
2213 			uconfig = next_config;
2214 			next_config = strchr(next_config, '#');
2215 			if (next_config)
2216 				*next_config++ = '\0';
2217 			uname = NULL;
2218 		}
2219 
2220 		debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2221 
2222 		ov_noffset = fit_image_load(images,
2223 			addr, &uname, &uconfig,
2224 			arch, IH_TYPE_FLATDT,
2225 			BOOTSTAGE_ID_FIT_FDT_START,
2226 			FIT_LOAD_REQUIRED, &ovload, &ovlen);
2227 		if (ov_noffset < 0) {
2228 			printf("load of %s failed\n", uname);
2229 			continue;
2230 		}
2231 		debug("%s loaded at 0x%08lx len=0x%08lx\n",
2232 				uname, ovload, ovlen);
2233 		ov = map_sysmem(ovload, ovlen);
2234 
2235 		base = map_sysmem(load, len + ovlen);
2236 		err = fdt_open_into(base, base, len + ovlen);
2237 		if (err < 0) {
2238 			printf("failed on fdt_open_into\n");
2239 			fdt_noffset = err;
2240 			goto out;
2241 		}
2242 		/* the verbose method prints out messages on error */
2243 		err = fdt_overlay_apply_verbose(base, ov);
2244 		if (err < 0) {
2245 			fdt_noffset = err;
2246 			goto out;
2247 		}
2248 		fdt_pack(base);
2249 		len = fdt_totalsize(base);
2250 	}
2251 #else
2252 	printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2253 	fdt_noffset = -EBADF;
2254 #endif
2255 
2256 out:
2257 	if (datap)
2258 		*datap = load;
2259 	if (lenp)
2260 		*lenp = len;
2261 	if (fit_unamep)
2262 		*fit_unamep = fit_uname;
2263 	if (fit_uname_configp)
2264 		*fit_uname_configp = fit_uname_config;
2265 
2266 	if (fit_uname_config_copy)
2267 		free(fit_uname_config_copy);
2268 	return fdt_noffset;
2269 }
2270 #endif
2271