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