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