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