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