xref: /rk3399_rockchip-uboot/common/image-fit.c (revision 94fbbf0f6db608f9349f507ed0734ea9befc8ec3)
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  * fit_image_get_load() - get load addr property for given component image node
719  * @fit: pointer to the FIT format image header
720  * @noffset: component image node offset
721  * @load: pointer to the uint32_t, will hold load address
722  *
723  * fit_image_get_load() finds load address property in a given component
724  * image node. If the property is found, its value is returned to the caller.
725  *
726  * returns:
727  *     0, on success
728  *     -1, on failure
729  */
730 int fit_image_get_load(const void *fit, int noffset, ulong *load)
731 {
732 	return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
733 }
734 
735 /**
736  * fit_image_get_entry() - get entry point address property
737  * @fit: pointer to the FIT format image header
738  * @noffset: component image node offset
739  * @entry: pointer to the uint32_t, will hold entry point address
740  *
741  * This gets the entry point address property for a given component image
742  * node.
743  *
744  * fit_image_get_entry() finds entry point address property in a given
745  * component image node.  If the property is found, its value is returned
746  * to the caller.
747  *
748  * returns:
749  *     0, on success
750  *     -1, on failure
751  */
752 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
753 {
754 	return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
755 }
756 
757 /**
758  * fit_image_get_data - get data property and its size for a given component image node
759  * @fit: pointer to the FIT format image header
760  * @noffset: component image node offset
761  * @data: double pointer to void, will hold data property's data address
762  * @size: pointer to size_t, will hold data property's data size
763  *
764  * fit_image_get_data() finds data property in a given component image node.
765  * If the property is found its data start address and size are returned to
766  * the caller.
767  *
768  * returns:
769  *     0, on success
770  *     -1, on failure
771  */
772 int fit_image_get_data(const void *fit, int noffset,
773 		const void **data, size_t *size)
774 {
775 	ulong data_off = 0;
776 	int data_sz = 0;
777 	int len;
778 
779 	*data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
780 	if (*data == NULL) {
781 		fit_image_get_data_offset(fit, noffset, (int *)&data_off);
782 		fit_image_get_data_size(fit, noffset, &data_sz);
783 		if (data_sz) {
784 			data_off += (ulong)fit;
785 			data_off += round_up(fdt_totalsize(fit), 4);
786 			*data = (void *)data_off;
787 			*size = data_sz;
788 			return 0;
789 		} else {
790 			fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
791 			*size = 0;
792 			return -1;
793 		}
794 	}
795 
796 	*size = len;
797 	return 0;
798 }
799 
800 /**
801  * Get 'data-offset' property from a given image node.
802  *
803  * @fit: pointer to the FIT image header
804  * @noffset: component image node offset
805  * @data_offset: holds the data-offset property
806  *
807  * returns:
808  *     0, on success
809  *     -ENOENT if the property could not be found
810  */
811 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
812 {
813 	const fdt32_t *val;
814 
815 	val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
816 	if (!val)
817 		return -ENOENT;
818 
819 	*data_offset = fdt32_to_cpu(*val);
820 
821 	return 0;
822 }
823 
824 /**
825  * Get 'data-size' property from a given image node.
826  *
827  * @fit: pointer to the FIT image header
828  * @noffset: component image node offset
829  * @data_size: holds the data-size property
830  *
831  * returns:
832  *     0, on success
833  *     -ENOENT if the property could not be found
834  */
835 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
836 {
837 	const fdt32_t *val;
838 
839 	val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
840 	if (!val)
841 		return -ENOENT;
842 
843 	*data_size = fdt32_to_cpu(*val);
844 
845 	return 0;
846 }
847 
848 /**
849  * fit_image_hash_get_algo - get hash algorithm name
850  * @fit: pointer to the FIT format image header
851  * @noffset: hash node offset
852  * @algo: double pointer to char, will hold pointer to the algorithm name
853  *
854  * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
855  * If the property is found its data start address is returned to the caller.
856  *
857  * returns:
858  *     0, on success
859  *     -1, on failure
860  */
861 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
862 {
863 	int len;
864 
865 	*algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
866 	if (*algo == NULL) {
867 		fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
868 		return -1;
869 	}
870 
871 	return 0;
872 }
873 
874 /**
875  * fit_image_hash_get_value - get hash value and length
876  * @fit: pointer to the FIT format image header
877  * @noffset: hash node offset
878  * @value: double pointer to uint8_t, will hold address of a hash value data
879  * @value_len: pointer to an int, will hold hash data length
880  *
881  * fit_image_hash_get_value() finds hash value property in a given hash node.
882  * If the property is found its data start address and size are returned to
883  * the caller.
884  *
885  * returns:
886  *     0, on success
887  *     -1, on failure
888  */
889 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
890 				int *value_len)
891 {
892 	int len;
893 
894 	*value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
895 	if (*value == NULL) {
896 		fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
897 		*value_len = 0;
898 		return -1;
899 	}
900 
901 	*value_len = len;
902 	return 0;
903 }
904 
905 /**
906  * fit_image_hash_get_ignore - get hash ignore flag
907  * @fit: pointer to the FIT format image header
908  * @noffset: hash node offset
909  * @ignore: pointer to an int, will hold hash ignore flag
910  *
911  * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
912  * If the property is found and non-zero, the hash algorithm is not verified by
913  * u-boot automatically.
914  *
915  * returns:
916  *     0, on ignore not found
917  *     value, on ignore found
918  */
919 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
920 {
921 	int len;
922 	int *value;
923 
924 	value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
925 	if (value == NULL || len != sizeof(int))
926 		*ignore = 0;
927 	else
928 		*ignore = *value;
929 
930 	return 0;
931 }
932 
933 ulong fit_get_end(const void *fit)
934 {
935 	return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
936 }
937 
938 /**
939  * fit_set_timestamp - set node timestamp property
940  * @fit: pointer to the FIT format image header
941  * @noffset: node offset
942  * @timestamp: timestamp value to be set
943  *
944  * fit_set_timestamp() attempts to set timestamp property in the requested
945  * node and returns operation status to the caller.
946  *
947  * returns:
948  *     0, on success
949  *     -ENOSPC if no space in device tree, -1 for other error
950  */
951 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
952 {
953 	uint32_t t;
954 	int ret;
955 
956 	t = cpu_to_uimage(timestamp);
957 	ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
958 				sizeof(uint32_t));
959 	if (ret) {
960 		debug("Can't set '%s' property for '%s' node (%s)\n",
961 		      FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
962 		      fdt_strerror(ret));
963 		return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
964 	}
965 
966 	return 0;
967 }
968 
969 /**
970  * calculate_hash - calculate and return hash for provided input data
971  * @data: pointer to the input data
972  * @data_len: data length
973  * @algo: requested hash algorithm
974  * @value: pointer to the char, will hold hash value data (caller must
975  * allocate enough free space)
976  * value_len: length of the calculated hash
977  *
978  * calculate_hash() computes input data hash according to the requested
979  * algorithm.
980  * Resulting hash value is placed in caller provided 'value' buffer, length
981  * of the calculated hash is returned via value_len pointer argument.
982  *
983  * returns:
984  *     0, on success
985  *    -1, when algo is unsupported
986  */
987 int calculate_hash(const void *data, int data_len, const char *algo,
988 			uint8_t *value, int *value_len)
989 {
990 	if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
991 		*((uint32_t *)value) = crc32_wd(0, data, data_len,
992 							CHUNKSZ_CRC32);
993 		*((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
994 		*value_len = 4;
995 	} else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
996 		sha1_csum_wd((unsigned char *)data, data_len,
997 			     (unsigned char *)value, CHUNKSZ_SHA1);
998 		*value_len = 20;
999 	} else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1000 		sha256_csum_wd((unsigned char *)data, data_len,
1001 			       (unsigned char *)value, CHUNKSZ_SHA256);
1002 		*value_len = SHA256_SUM_LEN;
1003 	} else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1004 		md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1005 		*value_len = 16;
1006 	} else {
1007 		debug("Unsupported hash alogrithm\n");
1008 		return -1;
1009 	}
1010 	return 0;
1011 }
1012 
1013 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1014 				size_t size, char **err_msgp)
1015 {
1016 	uint8_t value[FIT_MAX_HASH_LEN];
1017 	int value_len;
1018 	char *algo;
1019 	uint8_t *fit_value;
1020 	int fit_value_len;
1021 	int ignore;
1022 
1023 	*err_msgp = NULL;
1024 
1025 	if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1026 		*err_msgp = "Can't get hash algo property";
1027 		return -1;
1028 	}
1029 	printf("%s", algo);
1030 
1031 	if (IMAGE_ENABLE_IGNORE) {
1032 		fit_image_hash_get_ignore(fit, noffset, &ignore);
1033 		if (ignore) {
1034 			printf("-skipped ");
1035 			return 0;
1036 		}
1037 	}
1038 
1039 	if (fit_image_hash_get_value(fit, noffset, &fit_value,
1040 				     &fit_value_len)) {
1041 		*err_msgp = "Can't get hash value property";
1042 		return -1;
1043 	}
1044 
1045 	if (calculate_hash(data, size, algo, value, &value_len)) {
1046 		*err_msgp = "Unsupported hash algorithm";
1047 		return -1;
1048 	}
1049 
1050 	if (value_len != fit_value_len) {
1051 		*err_msgp = "Bad hash value len";
1052 		return -1;
1053 	} else if (memcmp(value, fit_value, value_len) != 0) {
1054 		*err_msgp = "Bad hash value";
1055 		return -1;
1056 	}
1057 
1058 	return 0;
1059 }
1060 
1061 /**
1062  * fit_image_verify - verify data integrity
1063  * @fit: pointer to the FIT format image header
1064  * @image_noffset: component image node offset
1065  *
1066  * fit_image_verify() goes over component image hash nodes,
1067  * re-calculates each data hash and compares with the value stored in hash
1068  * node.
1069  *
1070  * returns:
1071  *     1, if all hashes are valid
1072  *     0, otherwise (or on error)
1073  */
1074 int fit_image_verify(const void *fit, int image_noffset)
1075 {
1076 	const void	*data;
1077 	size_t		size;
1078 	int		noffset = 0;
1079 	char		*err_msg = "";
1080 	int verify_all = 1;
1081 	int ret;
1082 
1083 	/* Get image data and data length */
1084 	if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1085 		err_msg = "Can't get image data/size";
1086 		goto error;
1087 	}
1088 
1089 	/* Verify all required signatures */
1090 	if (IMAGE_ENABLE_VERIFY &&
1091 	    fit_image_verify_required_sigs(fit, image_noffset, data, size,
1092 					   gd_fdt_blob(), &verify_all)) {
1093 		err_msg = "Unable to verify required signature";
1094 		goto error;
1095 	}
1096 
1097 	/* Process all hash subnodes of the component image node */
1098 	fdt_for_each_subnode(noffset, fit, image_noffset) {
1099 		const char *name = fit_get_name(fit, noffset, NULL);
1100 
1101 		/*
1102 		 * Check subnode name, must be equal to "hash".
1103 		 * Multiple hash nodes require unique unit node
1104 		 * names, e.g. hash@1, hash@2, etc.
1105 		 */
1106 		if (!strncmp(name, FIT_HASH_NODENAME,
1107 			     strlen(FIT_HASH_NODENAME))) {
1108 			if (fit_image_check_hash(fit, noffset, data, size,
1109 						 &err_msg))
1110 				goto error;
1111 			puts("+ ");
1112 		} else if (IMAGE_ENABLE_VERIFY && verify_all &&
1113 				!strncmp(name, FIT_SIG_NODENAME,
1114 					strlen(FIT_SIG_NODENAME))) {
1115 			ret = fit_image_check_sig(fit, noffset, data,
1116 							size, -1, &err_msg);
1117 
1118 			/*
1119 			 * Show an indication on failure, but do not return
1120 			 * an error. Only keys marked 'required' can cause
1121 			 * an image validation failure. See the call to
1122 			 * fit_image_verify_required_sigs() above.
1123 			 */
1124 			if (ret)
1125 				puts("- ");
1126 			else
1127 				puts("+ ");
1128 		}
1129 	}
1130 
1131 	if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1132 		err_msg = "Corrupted or truncated tree";
1133 		goto error;
1134 	}
1135 
1136 	return 1;
1137 
1138 error:
1139 	printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1140 	       err_msg, fit_get_name(fit, noffset, NULL),
1141 	       fit_get_name(fit, image_noffset, NULL));
1142 	return 0;
1143 }
1144 
1145 /**
1146  * fit_all_image_verify - verify data integrity for all images
1147  * @fit: pointer to the FIT format image header
1148  *
1149  * fit_all_image_verify() goes over all images in the FIT and
1150  * for every images checks if all it's hashes are valid.
1151  *
1152  * returns:
1153  *     1, if all hashes of all images are valid
1154  *     0, otherwise (or on error)
1155  */
1156 int fit_all_image_verify(const void *fit)
1157 {
1158 	int images_noffset;
1159 	int noffset;
1160 	int ndepth;
1161 	int count;
1162 
1163 	/* Find images parent node offset */
1164 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1165 	if (images_noffset < 0) {
1166 		printf("Can't find images parent node '%s' (%s)\n",
1167 		       FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1168 		return 0;
1169 	}
1170 
1171 	/* Process all image subnodes, check hashes for each */
1172 	printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1173 	       (ulong)fit);
1174 	for (ndepth = 0, count = 0,
1175 	     noffset = fdt_next_node(fit, images_noffset, &ndepth);
1176 			(noffset >= 0) && (ndepth > 0);
1177 			noffset = fdt_next_node(fit, noffset, &ndepth)) {
1178 		if (ndepth == 1) {
1179 			/*
1180 			 * Direct child node of the images parent node,
1181 			 * i.e. component image node.
1182 			 */
1183 			printf("   Hash(es) for Image %u (%s): ", count,
1184 			       fit_get_name(fit, noffset, NULL));
1185 			count++;
1186 
1187 			if (!fit_image_verify(fit, noffset))
1188 				return 0;
1189 			printf("\n");
1190 		}
1191 	}
1192 	return 1;
1193 }
1194 
1195 /**
1196  * fit_image_check_os - check whether image node is of a given os type
1197  * @fit: pointer to the FIT format image header
1198  * @noffset: component image node offset
1199  * @os: requested image os
1200  *
1201  * fit_image_check_os() reads image os property and compares its numeric
1202  * id with the requested os. Comparison result is returned to the caller.
1203  *
1204  * returns:
1205  *     1 if image is of given os type
1206  *     0 otherwise (or on error)
1207  */
1208 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1209 {
1210 	uint8_t image_os;
1211 
1212 	if (fit_image_get_os(fit, noffset, &image_os))
1213 		return 0;
1214 	return (os == image_os);
1215 }
1216 
1217 /**
1218  * fit_image_check_arch - check whether image node is of a given arch
1219  * @fit: pointer to the FIT format image header
1220  * @noffset: component image node offset
1221  * @arch: requested imagearch
1222  *
1223  * fit_image_check_arch() reads image arch property and compares its numeric
1224  * id with the requested arch. Comparison result is returned to the caller.
1225  *
1226  * returns:
1227  *     1 if image is of given arch
1228  *     0 otherwise (or on error)
1229  */
1230 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1231 {
1232 	uint8_t image_arch;
1233 	int aarch32_support = 0;
1234 
1235 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1236 	aarch32_support = 1;
1237 #endif
1238 
1239 	if (fit_image_get_arch(fit, noffset, &image_arch))
1240 		return 0;
1241 	return (arch == image_arch) ||
1242 		(arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1243 		(arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1244 		 aarch32_support);
1245 }
1246 
1247 /**
1248  * fit_image_check_type - check whether image node is of a given type
1249  * @fit: pointer to the FIT format image header
1250  * @noffset: component image node offset
1251  * @type: requested image type
1252  *
1253  * fit_image_check_type() reads image type property and compares its numeric
1254  * id with the requested type. Comparison result is returned to the caller.
1255  *
1256  * returns:
1257  *     1 if image is of given type
1258  *     0 otherwise (or on error)
1259  */
1260 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1261 {
1262 	uint8_t image_type;
1263 
1264 	if (fit_image_get_type(fit, noffset, &image_type))
1265 		return 0;
1266 	return (type == image_type);
1267 }
1268 
1269 /**
1270  * fit_image_check_comp - check whether image node uses given compression
1271  * @fit: pointer to the FIT format image header
1272  * @noffset: component image node offset
1273  * @comp: requested image compression type
1274  *
1275  * fit_image_check_comp() reads image compression property and compares its
1276  * numeric id with the requested compression type. Comparison result is
1277  * returned to the caller.
1278  *
1279  * returns:
1280  *     1 if image uses requested compression
1281  *     0 otherwise (or on error)
1282  */
1283 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1284 {
1285 	uint8_t image_comp;
1286 
1287 	if (fit_image_get_comp(fit, noffset, &image_comp))
1288 		return 0;
1289 	return (comp == image_comp);
1290 }
1291 
1292 /**
1293  * fit_check_format - sanity check FIT image format
1294  * @fit: pointer to the FIT format image header
1295  *
1296  * fit_check_format() runs a basic sanity FIT image verification.
1297  * Routine checks for mandatory properties, nodes, etc.
1298  *
1299  * returns:
1300  *     1, on success
1301  *     0, on failure
1302  */
1303 int fit_check_format(const void *fit)
1304 {
1305 	/* mandatory / node 'description' property */
1306 	if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1307 		debug("Wrong FIT format: no description\n");
1308 		return 0;
1309 	}
1310 
1311 	if (IMAGE_ENABLE_TIMESTAMP) {
1312 		/* mandatory / node 'timestamp' property */
1313 		if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1314 			debug("Wrong FIT format: no timestamp\n");
1315 			return 0;
1316 		}
1317 	}
1318 
1319 	/* mandatory subimages parent '/images' node */
1320 	if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1321 		debug("Wrong FIT format: no images parent node\n");
1322 		return 0;
1323 	}
1324 
1325 	return 1;
1326 }
1327 
1328 
1329 /**
1330  * fit_conf_find_compat
1331  * @fit: pointer to the FIT format image header
1332  * @fdt: pointer to the device tree to compare against
1333  *
1334  * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1335  * most compatible with the passed in device tree.
1336  *
1337  * Example:
1338  *
1339  * / o image-tree
1340  *   |-o images
1341  *   | |-o fdt@1
1342  *   | |-o fdt@2
1343  *   |
1344  *   |-o configurations
1345  *     |-o config@1
1346  *     | |-fdt = fdt@1
1347  *     |
1348  *     |-o config@2
1349  *       |-fdt = fdt@2
1350  *
1351  * / o U-Boot fdt
1352  *   |-compatible = "foo,bar", "bim,bam"
1353  *
1354  * / o kernel fdt1
1355  *   |-compatible = "foo,bar",
1356  *
1357  * / o kernel fdt2
1358  *   |-compatible = "bim,bam", "baz,biz"
1359  *
1360  * Configuration 1 would be picked because the first string in U-Boot's
1361  * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1362  * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1363  *
1364  * returns:
1365  *     offset to the configuration to use if one was found
1366  *     -1 otherwise
1367  */
1368 int fit_conf_find_compat(const void *fit, const void *fdt)
1369 {
1370 	int ndepth = 0;
1371 	int noffset, confs_noffset, images_noffset;
1372 	const void *fdt_compat;
1373 	int fdt_compat_len;
1374 	int best_match_offset = 0;
1375 	int best_match_pos = 0;
1376 
1377 	confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1378 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1379 	if (confs_noffset < 0 || images_noffset < 0) {
1380 		debug("Can't find configurations or images nodes.\n");
1381 		return -1;
1382 	}
1383 
1384 	fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1385 	if (!fdt_compat) {
1386 		debug("Fdt for comparison has no \"compatible\" property.\n");
1387 		return -1;
1388 	}
1389 
1390 	/*
1391 	 * Loop over the configurations in the FIT image.
1392 	 */
1393 	for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1394 			(noffset >= 0) && (ndepth > 0);
1395 			noffset = fdt_next_node(fit, noffset, &ndepth)) {
1396 		const void *kfdt;
1397 		const char *kfdt_name;
1398 		int kfdt_noffset;
1399 		const char *cur_fdt_compat;
1400 		int len;
1401 		size_t size;
1402 		int i;
1403 
1404 		if (ndepth > 1)
1405 			continue;
1406 
1407 		kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1408 		if (!kfdt_name) {
1409 			debug("No fdt property found.\n");
1410 			continue;
1411 		}
1412 		kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1413 						  kfdt_name);
1414 		if (kfdt_noffset < 0) {
1415 			debug("No image node named \"%s\" found.\n",
1416 			      kfdt_name);
1417 			continue;
1418 		}
1419 		/*
1420 		 * Get a pointer to this configuration's fdt.
1421 		 */
1422 		if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1423 			debug("Failed to get fdt \"%s\".\n", kfdt_name);
1424 			continue;
1425 		}
1426 
1427 		len = fdt_compat_len;
1428 		cur_fdt_compat = fdt_compat;
1429 		/*
1430 		 * Look for a match for each U-Boot compatibility string in
1431 		 * turn in this configuration's fdt.
1432 		 */
1433 		for (i = 0; len > 0 &&
1434 		     (!best_match_offset || best_match_pos > i); i++) {
1435 			int cur_len = strlen(cur_fdt_compat) + 1;
1436 
1437 			if (!fdt_node_check_compatible(kfdt, 0,
1438 						       cur_fdt_compat)) {
1439 				best_match_offset = noffset;
1440 				best_match_pos = i;
1441 				break;
1442 			}
1443 			len -= cur_len;
1444 			cur_fdt_compat += cur_len;
1445 		}
1446 	}
1447 	if (!best_match_offset) {
1448 		debug("No match found.\n");
1449 		return -1;
1450 	}
1451 
1452 	return best_match_offset;
1453 }
1454 
1455 /**
1456  * fit_conf_get_node - get node offset for configuration of a given unit name
1457  * @fit: pointer to the FIT format image header
1458  * @conf_uname: configuration node unit name
1459  *
1460  * fit_conf_get_node() finds a configuration (within the '/configurations'
1461  * parent node) of a provided unit name. If configuration is found its node
1462  * offset is returned to the caller.
1463  *
1464  * When NULL is provided in second argument fit_conf_get_node() will search
1465  * for a default configuration node instead. Default configuration node unit
1466  * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1467  * node.
1468  *
1469  * returns:
1470  *     configuration node offset when found (>=0)
1471  *     negative number on failure (FDT_ERR_* code)
1472  */
1473 int fit_conf_get_node(const void *fit, const char *conf_uname)
1474 {
1475 	int noffset, confs_noffset;
1476 	int len;
1477 	const char *s;
1478 	char *conf_uname_copy = NULL;
1479 
1480 	confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1481 	if (confs_noffset < 0) {
1482 		debug("Can't find configurations parent node '%s' (%s)\n",
1483 		      FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1484 		return confs_noffset;
1485 	}
1486 
1487 	if (conf_uname == NULL) {
1488 		/* get configuration unit name from the default property */
1489 		debug("No configuration specified, trying default...\n");
1490 		conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1491 						 FIT_DEFAULT_PROP, &len);
1492 		if (conf_uname == NULL) {
1493 			fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1494 				      len);
1495 			return len;
1496 		}
1497 		debug("Found default configuration: '%s'\n", conf_uname);
1498 	}
1499 
1500 	s = strchr(conf_uname, '#');
1501 	if (s) {
1502 		len = s - conf_uname;
1503 		conf_uname_copy = malloc(len + 1);
1504 		if (!conf_uname_copy) {
1505 			debug("Can't allocate uname copy: '%s'\n",
1506 					conf_uname);
1507 			return -ENOMEM;
1508 		}
1509 		memcpy(conf_uname_copy, conf_uname, len);
1510 		conf_uname_copy[len] = '\0';
1511 		conf_uname = conf_uname_copy;
1512 	}
1513 
1514 	noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1515 	if (noffset < 0) {
1516 		debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1517 		      conf_uname, fdt_strerror(noffset));
1518 	}
1519 
1520 	if (conf_uname_copy)
1521 		free(conf_uname_copy);
1522 
1523 	return noffset;
1524 }
1525 
1526 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1527 		const char *prop_name)
1528 {
1529 	return fdt_stringlist_count(fit, noffset, prop_name);
1530 }
1531 
1532 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1533 		const char *prop_name, int index)
1534 {
1535 	const char *uname;
1536 	int len;
1537 
1538 	/* get kernel image unit name from configuration kernel property */
1539 	uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1540 	if (uname == NULL)
1541 		return len;
1542 
1543 	return fit_image_get_node(fit, uname);
1544 }
1545 
1546 int fit_conf_get_prop_node(const void *fit, int noffset,
1547 		const char *prop_name)
1548 {
1549 	return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1550 }
1551 
1552 /**
1553  * fit_conf_print - prints out the FIT configuration details
1554  * @fit: pointer to the FIT format image header
1555  * @noffset: offset of the configuration node
1556  * @p: pointer to prefix string
1557  *
1558  * fit_conf_print() lists all mandatory properties for the processed
1559  * configuration node.
1560  *
1561  * returns:
1562  *     no returned results
1563  */
1564 void fit_conf_print(const void *fit, int noffset, const char *p)
1565 {
1566 	char *desc;
1567 	const char *uname;
1568 	int ret;
1569 	int fdt_index, loadables_index;
1570 
1571 	/* Mandatory properties */
1572 	ret = fit_get_desc(fit, noffset, &desc);
1573 	printf("%s  Description:  ", p);
1574 	if (ret)
1575 		printf("unavailable\n");
1576 	else
1577 		printf("%s\n", desc);
1578 
1579 	uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1580 	printf("%s  Kernel:       ", p);
1581 	if (uname == NULL)
1582 		printf("unavailable\n");
1583 	else
1584 		printf("%s\n", uname);
1585 
1586 	/* Optional properties */
1587 	uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1588 	if (uname)
1589 		printf("%s  Init Ramdisk: %s\n", p, uname);
1590 
1591 	for (fdt_index = 0;
1592 	     uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
1593 					fdt_index, NULL), uname;
1594 	     fdt_index++) {
1595 
1596 		if (fdt_index == 0)
1597 			printf("%s  FDT:          ", p);
1598 		else
1599 			printf("%s                ", p);
1600 		printf("%s\n", uname);
1601 	}
1602 
1603 	uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
1604 	if (uname)
1605 		printf("%s  FPGA:         %s\n", p, uname);
1606 
1607 	/* Print out all of the specified loadables */
1608 	for (loadables_index = 0;
1609 	     uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
1610 					loadables_index, NULL), uname;
1611 	     loadables_index++) {
1612 		if (loadables_index == 0) {
1613 			printf("%s  Loadables:    ", p);
1614 		} else {
1615 			printf("%s                ", p);
1616 		}
1617 		printf("%s\n", uname);
1618 	}
1619 }
1620 
1621 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1622 {
1623 	fit_image_print(fit, rd_noffset, "   ");
1624 
1625 	if (verify) {
1626 		puts("   Verifying Hash Integrity ... ");
1627 		if (!fit_image_verify(fit, rd_noffset)) {
1628 			puts("Bad Data Hash\n");
1629 			return -EACCES;
1630 		}
1631 		puts("OK\n");
1632 	}
1633 
1634 	return 0;
1635 }
1636 
1637 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1638 			ulong addr)
1639 {
1640 	int cfg_noffset;
1641 	void *fit_hdr;
1642 	int noffset;
1643 
1644 	debug("*  %s: using config '%s' from image at 0x%08lx\n",
1645 	      prop_name, images->fit_uname_cfg, addr);
1646 
1647 	/* Check whether configuration has this property defined */
1648 	fit_hdr = map_sysmem(addr, 0);
1649 	cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1650 	if (cfg_noffset < 0) {
1651 		debug("*  %s: no such config\n", prop_name);
1652 		return -EINVAL;
1653 	}
1654 
1655 	noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1656 	if (noffset < 0) {
1657 		debug("*  %s: no '%s' in config\n", prop_name, prop_name);
1658 		return -ENOENT;
1659 	}
1660 
1661 	return noffset;
1662 }
1663 
1664 /**
1665  * fit_get_image_type_property() - get property name for IH_TYPE_...
1666  *
1667  * @return the properly name where we expect to find the image in the
1668  * config node
1669  */
1670 static const char *fit_get_image_type_property(int type)
1671 {
1672 	/*
1673 	 * This is sort-of available in the uimage_type[] table in image.c
1674 	 * but we don't have access to the short name, and "fdt" is different
1675 	 * anyway. So let's just keep it here.
1676 	 */
1677 	switch (type) {
1678 	case IH_TYPE_FLATDT:
1679 		return FIT_FDT_PROP;
1680 	case IH_TYPE_KERNEL:
1681 		return FIT_KERNEL_PROP;
1682 	case IH_TYPE_RAMDISK:
1683 		return FIT_RAMDISK_PROP;
1684 	case IH_TYPE_X86_SETUP:
1685 		return FIT_SETUP_PROP;
1686 	case IH_TYPE_LOADABLE:
1687 		return FIT_LOADABLE_PROP;
1688 	case IH_TYPE_FPGA:
1689 		return FIT_FPGA_PROP;
1690 	}
1691 
1692 	return "unknown";
1693 }
1694 
1695 int fit_image_load(bootm_headers_t *images, ulong addr,
1696 		   const char **fit_unamep, const char **fit_uname_configp,
1697 		   int arch, int image_type, int bootstage_id,
1698 		   enum fit_load_op load_op, ulong *datap, ulong *lenp)
1699 {
1700 	int cfg_noffset, noffset;
1701 	const char *fit_uname;
1702 	const char *fit_uname_config;
1703 	const char *fit_base_uname_config;
1704 	const void *fit;
1705 	const void *buf;
1706 	size_t size;
1707 	int type_ok, os_ok;
1708 	ulong load, data, len;
1709 	uint8_t os;
1710 #ifndef USE_HOSTCC
1711 	uint8_t os_arch;
1712 #endif
1713 	const char *prop_name;
1714 	int ret;
1715 
1716 	fit = map_sysmem(addr, 0);
1717 	fit_uname = fit_unamep ? *fit_unamep : NULL;
1718 	fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1719 	fit_base_uname_config = NULL;
1720 	prop_name = fit_get_image_type_property(image_type);
1721 	printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1722 
1723 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1724 	if (!fit_check_format(fit)) {
1725 		printf("Bad FIT %s image format!\n", prop_name);
1726 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1727 		return -ENOEXEC;
1728 	}
1729 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1730 	if (fit_uname) {
1731 		/* get FIT component image node offset */
1732 		bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1733 		noffset = fit_image_get_node(fit, fit_uname);
1734 	} else {
1735 		/*
1736 		 * no image node unit name, try to get config
1737 		 * node first. If config unit node name is NULL
1738 		 * fit_conf_get_node() will try to find default config node
1739 		 */
1740 		bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1741 		if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1742 			cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1743 		} else {
1744 			cfg_noffset = fit_conf_get_node(fit,
1745 							fit_uname_config);
1746 		}
1747 		if (cfg_noffset < 0) {
1748 			puts("Could not find configuration node\n");
1749 			bootstage_error(bootstage_id +
1750 					BOOTSTAGE_SUB_NO_UNIT_NAME);
1751 			return -ENOENT;
1752 		}
1753 		fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1754 		printf("   Using '%s' configuration\n", fit_base_uname_config);
1755 		if (image_type == IH_TYPE_KERNEL) {
1756 			/* Remember (and possibly verify) this config */
1757 			images->fit_uname_cfg = fit_base_uname_config;
1758 			if (IMAGE_ENABLE_VERIFY && images->verify) {
1759 				puts("   Verifying Hash Integrity ... ");
1760 				if (fit_config_verify(fit, cfg_noffset)) {
1761 					puts("Bad Data Hash\n");
1762 					bootstage_error(bootstage_id +
1763 						BOOTSTAGE_SUB_HASH);
1764 					return -EACCES;
1765 				}
1766 				puts("OK\n");
1767 			}
1768 			bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1769 		}
1770 
1771 		noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1772 						 prop_name);
1773 		fit_uname = fit_get_name(fit, noffset, NULL);
1774 	}
1775 	if (noffset < 0) {
1776 		puts("Could not find subimage node\n");
1777 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1778 		return -ENOENT;
1779 	}
1780 
1781 	printf("   Trying '%s' %s subimage\n", fit_uname, prop_name);
1782 
1783 	ret = fit_image_select(fit, noffset, images->verify);
1784 	if (ret) {
1785 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1786 		return ret;
1787 	}
1788 
1789 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1790 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1791 	if (!fit_image_check_target_arch(fit, noffset)) {
1792 		puts("Unsupported Architecture\n");
1793 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1794 		return -ENOEXEC;
1795 	}
1796 #endif
1797 
1798 #ifndef USE_HOSTCC
1799 	fit_image_get_arch(fit, noffset, &os_arch);
1800 	images->os.arch = os_arch;
1801 #endif
1802 
1803 	if (image_type == IH_TYPE_FLATDT &&
1804 	    !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1805 		puts("FDT image is compressed");
1806 		return -EPROTONOSUPPORT;
1807 	}
1808 
1809 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1810 	type_ok = fit_image_check_type(fit, noffset, image_type) ||
1811 		  fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1812 		  (image_type == IH_TYPE_KERNEL &&
1813 		   fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1814 
1815 	os_ok = image_type == IH_TYPE_FLATDT ||
1816 		image_type == IH_TYPE_FPGA ||
1817 		fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1818 		fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1819 		fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1820 
1821 	/*
1822 	 * If either of the checks fail, we should report an error, but
1823 	 * if the image type is coming from the "loadables" field, we
1824 	 * don't care what it is
1825 	 */
1826 	if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
1827 		fit_image_get_os(fit, noffset, &os);
1828 		printf("No %s %s %s Image\n",
1829 		       genimg_get_os_name(os),
1830 		       genimg_get_arch_name(arch),
1831 		       genimg_get_type_name(image_type));
1832 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1833 		return -EIO;
1834 	}
1835 
1836 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1837 
1838 	/* get image data address and length */
1839 	if (fit_image_get_data(fit, noffset, &buf, &size)) {
1840 		printf("Could not find %s subimage data!\n", prop_name);
1841 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1842 		return -ENOENT;
1843 	}
1844 
1845 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1846 	/* perform any post-processing on the image data */
1847 	board_fit_image_post_process((void **)&buf, &size);
1848 #endif
1849 
1850 	len = (ulong)size;
1851 
1852 	/* verify that image data is a proper FDT blob */
1853 	if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1854 		puts("Subimage data is not a FDT");
1855 		return -ENOEXEC;
1856 	}
1857 
1858 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1859 
1860 	/*
1861 	 * Work-around for eldk-4.2 which gives this warning if we try to
1862 	 * cast in the unmap_sysmem() call:
1863 	 * warning: initialization discards qualifiers from pointer target type
1864 	 */
1865 	{
1866 		void *vbuf = (void *)buf;
1867 
1868 		data = map_to_sysmem(vbuf);
1869 	}
1870 
1871 	if (load_op == FIT_LOAD_IGNORED) {
1872 		/* Don't load */
1873 	} else if (fit_image_get_load(fit, noffset, &load)) {
1874 		if (load_op == FIT_LOAD_REQUIRED) {
1875 			printf("Can't get %s subimage load address!\n",
1876 			       prop_name);
1877 			bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1878 			return -EBADF;
1879 		}
1880 	} else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1881 		ulong image_start, image_end;
1882 		ulong load_end;
1883 		void *dst;
1884 
1885 		/*
1886 		 * move image data to the load address,
1887 		 * make sure we don't overwrite initial image
1888 		 */
1889 		image_start = addr;
1890 		image_end = addr + fit_get_size(fit);
1891 
1892 		load_end = load + len;
1893 		if (image_type != IH_TYPE_KERNEL &&
1894 		    load < image_end && load_end > image_start) {
1895 			printf("Error: %s overwritten\n", prop_name);
1896 			return -EXDEV;
1897 		}
1898 
1899 		printf("   Loading %s from 0x%08lx to 0x%08lx\n",
1900 		       prop_name, data, load);
1901 
1902 		dst = map_sysmem(load, len);
1903 		memmove(dst, buf, len);
1904 		data = load;
1905 	}
1906 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1907 
1908 	*datap = data;
1909 	*lenp = len;
1910 	if (fit_unamep)
1911 		*fit_unamep = (char *)fit_uname;
1912 	if (fit_uname_configp)
1913 		*fit_uname_configp = (char *)(fit_uname_config ? :
1914 					      fit_base_uname_config);
1915 
1916 	return noffset;
1917 }
1918 
1919 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1920 			ulong *setup_start, ulong *setup_len)
1921 {
1922 	int noffset;
1923 	ulong addr;
1924 	ulong len;
1925 	int ret;
1926 
1927 	addr = map_to_sysmem(images->fit_hdr_os);
1928 	noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1929 	if (noffset < 0)
1930 		return noffset;
1931 
1932 	ret = fit_image_load(images, addr, NULL, NULL, arch,
1933 			     IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1934 			     FIT_LOAD_REQUIRED, setup_start, &len);
1935 
1936 	return ret;
1937 }
1938 
1939 #ifndef USE_HOSTCC
1940 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
1941 		   const char **fit_unamep, const char **fit_uname_configp,
1942 		   int arch, ulong *datap, ulong *lenp)
1943 {
1944 	int fdt_noffset, cfg_noffset, count;
1945 	const void *fit;
1946 	const char *fit_uname = NULL;
1947 	const char *fit_uname_config = NULL;
1948 	char *fit_uname_config_copy = NULL;
1949 	char *next_config = NULL;
1950 	ulong load, len;
1951 #ifdef CONFIG_OF_LIBFDT_OVERLAY
1952 	ulong image_start, image_end;
1953 	ulong ovload, ovlen;
1954 	const char *uconfig;
1955 	const char *uname;
1956 	void *base, *ov;
1957 	int i, err, noffset, ov_noffset;
1958 #endif
1959 
1960 	fit_uname = fit_unamep ? *fit_unamep : NULL;
1961 
1962 	if (fit_uname_configp && *fit_uname_configp) {
1963 		fit_uname_config_copy = strdup(*fit_uname_configp);
1964 		if (!fit_uname_config_copy)
1965 			return -ENOMEM;
1966 
1967 		next_config = strchr(fit_uname_config_copy, '#');
1968 		if (next_config)
1969 			*next_config++ = '\0';
1970 		if (next_config - 1 > fit_uname_config_copy)
1971 			fit_uname_config = fit_uname_config_copy;
1972 	}
1973 
1974 	fdt_noffset = fit_image_load(images,
1975 		addr, &fit_uname, &fit_uname_config,
1976 		arch, IH_TYPE_FLATDT,
1977 		BOOTSTAGE_ID_FIT_FDT_START,
1978 		FIT_LOAD_OPTIONAL, &load, &len);
1979 
1980 	if (fdt_noffset < 0)
1981 		goto out;
1982 
1983 	debug("fit_uname=%s, fit_uname_config=%s\n",
1984 			fit_uname ? fit_uname : "<NULL>",
1985 			fit_uname_config ? fit_uname_config : "<NULL>");
1986 
1987 	fit = map_sysmem(addr, 0);
1988 
1989 	cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
1990 
1991 	/* single blob, or error just return as well */
1992 	count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
1993 	if (count <= 1 && !next_config)
1994 		goto out;
1995 
1996 	/* we need to apply overlays */
1997 
1998 #ifdef CONFIG_OF_LIBFDT_OVERLAY
1999 	image_start = addr;
2000 	image_end = addr + fit_get_size(fit);
2001 	/* verify that relocation took place by load address not being in fit */
2002 	if (load >= image_start && load < image_end) {
2003 		/* check is simplified; fit load checks for overlaps */
2004 		printf("Overlayed FDT requires relocation\n");
2005 		fdt_noffset = -EBADF;
2006 		goto out;
2007 	}
2008 
2009 	base = map_sysmem(load, len);
2010 
2011 	/* apply extra configs in FIT first, followed by args */
2012 	for (i = 1; ; i++) {
2013 		if (i < count) {
2014 			noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2015 							       FIT_FDT_PROP, i);
2016 			uname = fit_get_name(fit, noffset, NULL);
2017 			uconfig = NULL;
2018 		} else {
2019 			if (!next_config)
2020 				break;
2021 			uconfig = next_config;
2022 			next_config = strchr(next_config, '#');
2023 			if (next_config)
2024 				*next_config++ = '\0';
2025 			uname = NULL;
2026 		}
2027 
2028 		debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2029 
2030 		ov_noffset = fit_image_load(images,
2031 			addr, &uname, &uconfig,
2032 			arch, IH_TYPE_FLATDT,
2033 			BOOTSTAGE_ID_FIT_FDT_START,
2034 			FIT_LOAD_REQUIRED, &ovload, &ovlen);
2035 		if (ov_noffset < 0) {
2036 			printf("load of %s failed\n", uname);
2037 			continue;
2038 		}
2039 		debug("%s loaded at 0x%08lx len=0x%08lx\n",
2040 				uname, ovload, ovlen);
2041 		ov = map_sysmem(ovload, ovlen);
2042 
2043 		base = map_sysmem(load, len + ovlen);
2044 		err = fdt_open_into(base, base, len + ovlen);
2045 		if (err < 0) {
2046 			printf("failed on fdt_open_into\n");
2047 			fdt_noffset = err;
2048 			goto out;
2049 		}
2050 		/* the verbose method prints out messages on error */
2051 		err = fdt_overlay_apply_verbose(base, ov);
2052 		if (err < 0) {
2053 			fdt_noffset = err;
2054 			goto out;
2055 		}
2056 		fdt_pack(base);
2057 		len = fdt_totalsize(base);
2058 	}
2059 #else
2060 	printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2061 	fdt_noffset = -EBADF;
2062 #endif
2063 
2064 out:
2065 	if (datap)
2066 		*datap = load;
2067 	if (lenp)
2068 		*lenp = len;
2069 	if (fit_unamep)
2070 		*fit_unamep = fit_uname;
2071 	if (fit_uname_configp)
2072 		*fit_uname_configp = fit_uname_config;
2073 
2074 	if (fit_uname_config_copy)
2075 		free(fit_uname_config_copy);
2076 	return fdt_noffset;
2077 }
2078 #endif
2079