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