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