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