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