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