xref: /openbmc/u-boot/common/image-fit.c (revision ed9072c7)
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_FIT_SPL_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 
546 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
547 
548 /**
549  * fit_get_desc - get node description property
550  * @fit: pointer to the FIT format image header
551  * @noffset: node offset
552  * @desc: double pointer to the char, will hold pointer to the description
553  *
554  * fit_get_desc() reads description property from a given node, if
555  * description is found pointer to it is returned in third call argument.
556  *
557  * returns:
558  *     0, on success
559  *     -1, on failure
560  */
561 int fit_get_desc(const void *fit, int noffset, char **desc)
562 {
563 	int len;
564 
565 	*desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
566 	if (*desc == NULL) {
567 		fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
568 		return -1;
569 	}
570 
571 	return 0;
572 }
573 
574 /**
575  * fit_get_timestamp - get node timestamp property
576  * @fit: pointer to the FIT format image header
577  * @noffset: node offset
578  * @timestamp: pointer to the time_t, will hold read timestamp
579  *
580  * fit_get_timestamp() reads timestamp property from given node, if timestamp
581  * is found and has a correct size its value is returned in third call
582  * argument.
583  *
584  * returns:
585  *     0, on success
586  *     -1, on property read failure
587  *     -2, on wrong timestamp size
588  */
589 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
590 {
591 	int len;
592 	const void *data;
593 
594 	data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
595 	if (data == NULL) {
596 		fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
597 		return -1;
598 	}
599 	if (len != sizeof(uint32_t)) {
600 		debug("FIT timestamp with incorrect size of (%u)\n", len);
601 		return -2;
602 	}
603 
604 	*timestamp = uimage_to_cpu(*((uint32_t *)data));
605 	return 0;
606 }
607 
608 /**
609  * fit_image_get_node - get node offset for component image of a given unit name
610  * @fit: pointer to the FIT format image header
611  * @image_uname: component image node unit name
612  *
613  * fit_image_get_node() finds a component image (within the '/images'
614  * node) of a provided unit name. If image is found its node offset is
615  * returned to the caller.
616  *
617  * returns:
618  *     image node offset when found (>=0)
619  *     negative number on failure (FDT_ERR_* code)
620  */
621 int fit_image_get_node(const void *fit, const char *image_uname)
622 {
623 	int noffset, images_noffset;
624 
625 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
626 	if (images_noffset < 0) {
627 		debug("Can't find images parent node '%s' (%s)\n",
628 		      FIT_IMAGES_PATH, fdt_strerror(images_noffset));
629 		return images_noffset;
630 	}
631 
632 	noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
633 	if (noffset < 0) {
634 		debug("Can't get node offset for image unit name: '%s' (%s)\n",
635 		      image_uname, fdt_strerror(noffset));
636 	}
637 
638 	return noffset;
639 }
640 
641 /**
642  * fit_image_get_os - get os id for a given component image node
643  * @fit: pointer to the FIT format image header
644  * @noffset: component image node offset
645  * @os: pointer to the uint8_t, will hold os numeric id
646  *
647  * fit_image_get_os() finds os property in a given component image node.
648  * If the property is found, its (string) value is translated to the numeric
649  * id which is returned to the caller.
650  *
651  * returns:
652  *     0, on success
653  *     -1, on failure
654  */
655 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
656 {
657 	int len;
658 	const void *data;
659 
660 	/* Get OS name from property data */
661 	data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
662 	if (data == NULL) {
663 		fit_get_debug(fit, noffset, FIT_OS_PROP, len);
664 		*os = -1;
665 		return -1;
666 	}
667 
668 	/* Translate OS name to id */
669 	*os = genimg_get_os_id(data);
670 	return 0;
671 }
672 
673 /**
674  * fit_image_get_arch - get arch id for a given component image node
675  * @fit: pointer to the FIT format image header
676  * @noffset: component image node offset
677  * @arch: pointer to the uint8_t, will hold arch numeric id
678  *
679  * fit_image_get_arch() finds arch property in a given component image node.
680  * If the property is found, its (string) value is translated to the numeric
681  * id which is returned to the caller.
682  *
683  * returns:
684  *     0, on success
685  *     -1, on failure
686  */
687 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
688 {
689 	int len;
690 	const void *data;
691 
692 	/* Get architecture name from property data */
693 	data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
694 	if (data == NULL) {
695 		fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
696 		*arch = -1;
697 		return -1;
698 	}
699 
700 	/* Translate architecture name to id */
701 	*arch = genimg_get_arch_id(data);
702 	return 0;
703 }
704 
705 /**
706  * fit_image_get_type - get type id for a given component image node
707  * @fit: pointer to the FIT format image header
708  * @noffset: component image node offset
709  * @type: pointer to the uint8_t, will hold type numeric id
710  *
711  * fit_image_get_type() finds type property in a given component image node.
712  * If the property is found, its (string) value is translated to the numeric
713  * id which is returned to the caller.
714  *
715  * returns:
716  *     0, on success
717  *     -1, on failure
718  */
719 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
720 {
721 	int len;
722 	const void *data;
723 
724 	/* Get image type name from property data */
725 	data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
726 	if (data == NULL) {
727 		fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
728 		*type = -1;
729 		return -1;
730 	}
731 
732 	/* Translate image type name to id */
733 	*type = genimg_get_type_id(data);
734 	return 0;
735 }
736 
737 /**
738  * fit_image_get_comp - get comp id for a given component image node
739  * @fit: pointer to the FIT format image header
740  * @noffset: component image node offset
741  * @comp: pointer to the uint8_t, will hold comp numeric id
742  *
743  * fit_image_get_comp() finds comp property in a given component image node.
744  * If the property is found, its (string) value is translated to the numeric
745  * id which is returned to the caller.
746  *
747  * returns:
748  *     0, on success
749  *     -1, on failure
750  */
751 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
752 {
753 	int len;
754 	const void *data;
755 
756 	/* Get compression name from property data */
757 	data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
758 	if (data == NULL) {
759 		fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
760 		*comp = -1;
761 		return -1;
762 	}
763 
764 	/* Translate compression name to id */
765 	*comp = genimg_get_comp_id(data);
766 	return 0;
767 }
768 
769 static int fit_image_get_address(const void *fit, int noffset, char *name,
770 			  ulong *load)
771 {
772 	int len, cell_len;
773 	const fdt32_t *cell;
774 	uint64_t load64 = 0;
775 
776 	cell = fdt_getprop(fit, noffset, name, &len);
777 	if (cell == NULL) {
778 		fit_get_debug(fit, noffset, name, len);
779 		return -1;
780 	}
781 
782 	if (len > sizeof(ulong)) {
783 		printf("Unsupported %s address size\n", name);
784 		return -1;
785 	}
786 
787 	cell_len = len >> 2;
788 	/* Use load64 to avoid compiling warning for 32-bit target */
789 	while (cell_len--) {
790 		load64 = (load64 << 32) | uimage_to_cpu(*cell);
791 		cell++;
792 	}
793 	*load = (ulong)load64;
794 
795 	return 0;
796 }
797 /**
798  * fit_image_get_load() - get load addr property for given component image node
799  * @fit: pointer to the FIT format image header
800  * @noffset: component image node offset
801  * @load: pointer to the uint32_t, will hold load address
802  *
803  * fit_image_get_load() finds load address property in a given component
804  * image node. If the property is found, its value is returned to the caller.
805  *
806  * returns:
807  *     0, on success
808  *     -1, on failure
809  */
810 int fit_image_get_load(const void *fit, int noffset, ulong *load)
811 {
812 	return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
813 }
814 
815 /**
816  * fit_image_get_entry() - get entry point address property
817  * @fit: pointer to the FIT format image header
818  * @noffset: component image node offset
819  * @entry: pointer to the uint32_t, will hold entry point address
820  *
821  * This gets the entry point address property for a given component image
822  * node.
823  *
824  * fit_image_get_entry() finds entry point address property in a given
825  * component image node.  If the property is found, its value is returned
826  * to the caller.
827  *
828  * returns:
829  *     0, on success
830  *     -1, on failure
831  */
832 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
833 {
834 	return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
835 }
836 
837 /**
838  * fit_image_get_data - get data property and its size for a given component image node
839  * @fit: pointer to the FIT format image header
840  * @noffset: component image node offset
841  * @data: double pointer to void, will hold data property's data address
842  * @size: pointer to size_t, will hold data property's data size
843  *
844  * fit_image_get_data() finds data property in a given component image node.
845  * If the property is found its data start address and size are returned to
846  * the caller.
847  *
848  * returns:
849  *     0, on success
850  *     -1, on failure
851  */
852 int fit_image_get_data(const void *fit, int noffset,
853 		const void **data, size_t *size)
854 {
855 	int len;
856 
857 	*data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
858 	if (*data == NULL) {
859 		fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
860 		*size = 0;
861 		return -1;
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 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1658 {
1659 	fit_image_print(fit, rd_noffset, "   ");
1660 
1661 	if (verify) {
1662 		puts("   Verifying Hash Integrity ... ");
1663 		if (!fit_image_verify(fit, rd_noffset)) {
1664 			puts("Bad Data Hash\n");
1665 			return -EACCES;
1666 		}
1667 		puts("OK\n");
1668 	}
1669 
1670 	return 0;
1671 }
1672 
1673 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1674 			ulong addr)
1675 {
1676 	int cfg_noffset;
1677 	void *fit_hdr;
1678 	int noffset;
1679 
1680 	debug("*  %s: using config '%s' from image at 0x%08lx\n",
1681 	      prop_name, images->fit_uname_cfg, addr);
1682 
1683 	/* Check whether configuration has this property defined */
1684 	fit_hdr = map_sysmem(addr, 0);
1685 	cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1686 	if (cfg_noffset < 0) {
1687 		debug("*  %s: no such config\n", prop_name);
1688 		return -EINVAL;
1689 	}
1690 
1691 	noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1692 	if (noffset < 0) {
1693 		debug("*  %s: no '%s' in config\n", prop_name, prop_name);
1694 		return -ENOENT;
1695 	}
1696 
1697 	return noffset;
1698 }
1699 
1700 /**
1701  * fit_get_image_type_property() - get property name for IH_TYPE_...
1702  *
1703  * @return the properly name where we expect to find the image in the
1704  * config node
1705  */
1706 static const char *fit_get_image_type_property(int type)
1707 {
1708 	/*
1709 	 * This is sort-of available in the uimage_type[] table in image.c
1710 	 * but we don't have access to the short name, and "fdt" is different
1711 	 * anyway. So let's just keep it here.
1712 	 */
1713 	switch (type) {
1714 	case IH_TYPE_FLATDT:
1715 		return FIT_FDT_PROP;
1716 	case IH_TYPE_KERNEL:
1717 		return FIT_KERNEL_PROP;
1718 	case IH_TYPE_RAMDISK:
1719 		return FIT_RAMDISK_PROP;
1720 	case IH_TYPE_X86_SETUP:
1721 		return FIT_SETUP_PROP;
1722 	case IH_TYPE_LOADABLE:
1723 		return FIT_LOADABLE_PROP;
1724 	case IH_TYPE_FPGA:
1725 		return FIT_FPGA_PROP;
1726 	}
1727 
1728 	return "unknown";
1729 }
1730 
1731 int fit_image_load(bootm_headers_t *images, ulong addr,
1732 		   const char **fit_unamep, const char **fit_uname_configp,
1733 		   int arch, int image_type, int bootstage_id,
1734 		   enum fit_load_op load_op, ulong *datap, ulong *lenp)
1735 {
1736 	int cfg_noffset, noffset;
1737 	const char *fit_uname;
1738 	const char *fit_uname_config;
1739 	const char *fit_base_uname_config;
1740 	const void *fit;
1741 	const void *buf;
1742 	size_t size;
1743 	int type_ok, os_ok;
1744 	ulong load, data, len;
1745 	uint8_t os;
1746 #ifndef USE_HOSTCC
1747 	uint8_t os_arch;
1748 #endif
1749 	const char *prop_name;
1750 	int ret;
1751 
1752 	fit = map_sysmem(addr, 0);
1753 	fit_uname = fit_unamep ? *fit_unamep : NULL;
1754 	fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1755 	fit_base_uname_config = NULL;
1756 	prop_name = fit_get_image_type_property(image_type);
1757 	printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1758 
1759 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1760 	if (!fit_check_format(fit)) {
1761 		printf("Bad FIT %s image format!\n", prop_name);
1762 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1763 		return -ENOEXEC;
1764 	}
1765 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1766 	if (fit_uname) {
1767 		/* get FIT component image node offset */
1768 		bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1769 		noffset = fit_image_get_node(fit, fit_uname);
1770 	} else {
1771 		/*
1772 		 * no image node unit name, try to get config
1773 		 * node first. If config unit node name is NULL
1774 		 * fit_conf_get_node() will try to find default config node
1775 		 */
1776 		bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1777 		if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1778 			cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1779 		} else {
1780 			cfg_noffset = fit_conf_get_node(fit,
1781 							fit_uname_config);
1782 		}
1783 		if (cfg_noffset < 0) {
1784 			puts("Could not find configuration node\n");
1785 			bootstage_error(bootstage_id +
1786 					BOOTSTAGE_SUB_NO_UNIT_NAME);
1787 			return -ENOENT;
1788 		}
1789 		fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1790 		printf("   Using '%s' configuration\n", fit_base_uname_config);
1791 		if (image_type == IH_TYPE_KERNEL) {
1792 			/* Remember (and possibly verify) this config */
1793 			images->fit_uname_cfg = fit_base_uname_config;
1794 			if (IMAGE_ENABLE_VERIFY && images->verify) {
1795 				puts("   Verifying Hash Integrity ... ");
1796 				if (fit_config_verify(fit, cfg_noffset)) {
1797 					puts("Bad Data Hash\n");
1798 					bootstage_error(bootstage_id +
1799 						BOOTSTAGE_SUB_HASH);
1800 					return -EACCES;
1801 				}
1802 				puts("OK\n");
1803 			}
1804 			bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1805 		}
1806 
1807 		noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1808 						 prop_name);
1809 		fit_uname = fit_get_name(fit, noffset, NULL);
1810 	}
1811 	if (noffset < 0) {
1812 		puts("Could not find subimage node\n");
1813 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1814 		return -ENOENT;
1815 	}
1816 
1817 	printf("   Trying '%s' %s subimage\n", fit_uname, prop_name);
1818 
1819 	ret = fit_image_select(fit, noffset, images->verify);
1820 	if (ret) {
1821 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1822 		return ret;
1823 	}
1824 
1825 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1826 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1827 	if (!fit_image_check_target_arch(fit, noffset)) {
1828 		puts("Unsupported Architecture\n");
1829 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1830 		return -ENOEXEC;
1831 	}
1832 #endif
1833 
1834 #ifndef USE_HOSTCC
1835 	fit_image_get_arch(fit, noffset, &os_arch);
1836 	images->os.arch = os_arch;
1837 #endif
1838 
1839 	if (image_type == IH_TYPE_FLATDT &&
1840 	    !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1841 		puts("FDT image is compressed");
1842 		return -EPROTONOSUPPORT;
1843 	}
1844 
1845 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1846 	type_ok = fit_image_check_type(fit, noffset, image_type) ||
1847 		  fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1848 		  (image_type == IH_TYPE_KERNEL &&
1849 		   fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1850 
1851 	os_ok = image_type == IH_TYPE_FLATDT ||
1852 		image_type == IH_TYPE_FPGA ||
1853 		fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1854 		fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1855 		fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1856 
1857 	/*
1858 	 * If either of the checks fail, we should report an error, but
1859 	 * if the image type is coming from the "loadables" field, we
1860 	 * don't care what it is
1861 	 */
1862 	if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
1863 		fit_image_get_os(fit, noffset, &os);
1864 		printf("No %s %s %s Image\n",
1865 		       genimg_get_os_name(os),
1866 		       genimg_get_arch_name(arch),
1867 		       genimg_get_type_name(image_type));
1868 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1869 		return -EIO;
1870 	}
1871 
1872 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1873 
1874 	/* get image data address and length */
1875 	if (fit_image_get_data(fit, noffset, &buf, &size)) {
1876 		printf("Could not find %s subimage data!\n", prop_name);
1877 		bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1878 		return -ENOENT;
1879 	}
1880 
1881 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1882 	/* perform any post-processing on the image data */
1883 	board_fit_image_post_process((void **)&buf, &size);
1884 #endif
1885 
1886 	len = (ulong)size;
1887 
1888 	/* verify that image data is a proper FDT blob */
1889 	if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1890 		puts("Subimage data is not a FDT");
1891 		return -ENOEXEC;
1892 	}
1893 
1894 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1895 
1896 	/*
1897 	 * Work-around for eldk-4.2 which gives this warning if we try to
1898 	 * cast in the unmap_sysmem() call:
1899 	 * warning: initialization discards qualifiers from pointer target type
1900 	 */
1901 	{
1902 		void *vbuf = (void *)buf;
1903 
1904 		data = map_to_sysmem(vbuf);
1905 	}
1906 
1907 	if (load_op == FIT_LOAD_IGNORED) {
1908 		/* Don't load */
1909 	} else if (fit_image_get_load(fit, noffset, &load)) {
1910 		if (load_op == FIT_LOAD_REQUIRED) {
1911 			printf("Can't get %s subimage load address!\n",
1912 			       prop_name);
1913 			bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1914 			return -EBADF;
1915 		}
1916 	} else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1917 		ulong image_start, image_end;
1918 		ulong load_end;
1919 		void *dst;
1920 
1921 		/*
1922 		 * move image data to the load address,
1923 		 * make sure we don't overwrite initial image
1924 		 */
1925 		image_start = addr;
1926 		image_end = addr + fit_get_size(fit);
1927 
1928 		load_end = load + len;
1929 		if (image_type != IH_TYPE_KERNEL &&
1930 		    load < image_end && load_end > image_start) {
1931 			printf("Error: %s overwritten\n", prop_name);
1932 			return -EXDEV;
1933 		}
1934 
1935 		printf("   Loading %s from 0x%08lx to 0x%08lx\n",
1936 		       prop_name, data, load);
1937 
1938 		dst = map_sysmem(load, len);
1939 		memmove(dst, buf, len);
1940 		data = load;
1941 	}
1942 	bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1943 
1944 	*datap = data;
1945 	*lenp = len;
1946 	if (fit_unamep)
1947 		*fit_unamep = (char *)fit_uname;
1948 	if (fit_uname_configp)
1949 		*fit_uname_configp = (char *)(fit_uname_config ? :
1950 					      fit_base_uname_config);
1951 
1952 	return noffset;
1953 }
1954 
1955 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1956 			ulong *setup_start, ulong *setup_len)
1957 {
1958 	int noffset;
1959 	ulong addr;
1960 	ulong len;
1961 	int ret;
1962 
1963 	addr = map_to_sysmem(images->fit_hdr_os);
1964 	noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1965 	if (noffset < 0)
1966 		return noffset;
1967 
1968 	ret = fit_image_load(images, addr, NULL, NULL, arch,
1969 			     IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1970 			     FIT_LOAD_REQUIRED, setup_start, &len);
1971 
1972 	return ret;
1973 }
1974 
1975 #ifndef USE_HOSTCC
1976 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
1977 		   const char **fit_unamep, const char **fit_uname_configp,
1978 		   int arch, ulong *datap, ulong *lenp)
1979 {
1980 	int fdt_noffset, cfg_noffset, count;
1981 	const void *fit;
1982 	const char *fit_uname = NULL;
1983 	const char *fit_uname_config = NULL;
1984 	char *fit_uname_config_copy = NULL;
1985 	char *next_config = NULL;
1986 	ulong load, len;
1987 #ifdef CONFIG_OF_LIBFDT_OVERLAY
1988 	ulong image_start, image_end;
1989 	ulong ovload, ovlen;
1990 	const char *uconfig;
1991 	const char *uname;
1992 	void *base, *ov;
1993 	int i, err, noffset, ov_noffset;
1994 #endif
1995 
1996 	fit_uname = fit_unamep ? *fit_unamep : NULL;
1997 
1998 	if (fit_uname_configp && *fit_uname_configp) {
1999 		fit_uname_config_copy = strdup(*fit_uname_configp);
2000 		if (!fit_uname_config_copy)
2001 			return -ENOMEM;
2002 
2003 		next_config = strchr(fit_uname_config_copy, '#');
2004 		if (next_config)
2005 			*next_config++ = '\0';
2006 		if (next_config - 1 > fit_uname_config_copy)
2007 			fit_uname_config = fit_uname_config_copy;
2008 	}
2009 
2010 	fdt_noffset = fit_image_load(images,
2011 		addr, &fit_uname, &fit_uname_config,
2012 		arch, IH_TYPE_FLATDT,
2013 		BOOTSTAGE_ID_FIT_FDT_START,
2014 		FIT_LOAD_OPTIONAL, &load, &len);
2015 
2016 	if (fdt_noffset < 0)
2017 		goto out;
2018 
2019 	debug("fit_uname=%s, fit_uname_config=%s\n",
2020 			fit_uname ? fit_uname : "<NULL>",
2021 			fit_uname_config ? fit_uname_config : "<NULL>");
2022 
2023 	fit = map_sysmem(addr, 0);
2024 
2025 	cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2026 
2027 	/* single blob, or error just return as well */
2028 	count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2029 	if (count <= 1 && !next_config)
2030 		goto out;
2031 
2032 	/* we need to apply overlays */
2033 
2034 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2035 	image_start = addr;
2036 	image_end = addr + fit_get_size(fit);
2037 	/* verify that relocation took place by load address not being in fit */
2038 	if (load >= image_start && load < image_end) {
2039 		/* check is simplified; fit load checks for overlaps */
2040 		printf("Overlayed FDT requires relocation\n");
2041 		fdt_noffset = -EBADF;
2042 		goto out;
2043 	}
2044 
2045 	base = map_sysmem(load, len);
2046 
2047 	/* apply extra configs in FIT first, followed by args */
2048 	for (i = 1; ; i++) {
2049 		if (i < count) {
2050 			noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2051 							       FIT_FDT_PROP, i);
2052 			uname = fit_get_name(fit, noffset, NULL);
2053 			uconfig = NULL;
2054 		} else {
2055 			if (!next_config)
2056 				break;
2057 			uconfig = next_config;
2058 			next_config = strchr(next_config, '#');
2059 			if (next_config)
2060 				*next_config++ = '\0';
2061 			uname = NULL;
2062 		}
2063 
2064 		debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2065 
2066 		ov_noffset = fit_image_load(images,
2067 			addr, &uname, &uconfig,
2068 			arch, IH_TYPE_FLATDT,
2069 			BOOTSTAGE_ID_FIT_FDT_START,
2070 			FIT_LOAD_REQUIRED, &ovload, &ovlen);
2071 		if (ov_noffset < 0) {
2072 			printf("load of %s failed\n", uname);
2073 			continue;
2074 		}
2075 		debug("%s loaded at 0x%08lx len=0x%08lx\n",
2076 				uname, ovload, ovlen);
2077 		ov = map_sysmem(ovload, ovlen);
2078 
2079 		base = map_sysmem(load, len + ovlen);
2080 		err = fdt_open_into(base, base, len + ovlen);
2081 		if (err < 0) {
2082 			printf("failed on fdt_open_into\n");
2083 			fdt_noffset = err;
2084 			goto out;
2085 		}
2086 		/* the verbose method prints out messages on error */
2087 		err = fdt_overlay_apply_verbose(base, ov);
2088 		if (err < 0) {
2089 			fdt_noffset = err;
2090 			goto out;
2091 		}
2092 		fdt_pack(base);
2093 		len = fdt_totalsize(base);
2094 	}
2095 #else
2096 	printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2097 	fdt_noffset = -EBADF;
2098 #endif
2099 
2100 out:
2101 	if (datap)
2102 		*datap = load;
2103 	if (lenp)
2104 		*lenp = len;
2105 	if (fit_unamep)
2106 		*fit_unamep = fit_uname;
2107 	if (fit_uname_configp)
2108 		*fit_uname_configp = fit_uname_config;
2109 
2110 	if (fit_uname_config_copy)
2111 		free(fit_uname_config_copy);
2112 	return fdt_noffset;
2113 }
2114 #endif
2115