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