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