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