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