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