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