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