]> git.sur5r.net Git - u-boot/blob - common/image.c
[new uImage] Use show_boot_progress() for new uImage format
[u-boot] / common / image.c
1 /*
2  * (C) Copyright 2008 Semihalf
3  *
4  * (C) Copyright 2000-2006
5  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25
26 #define DEBUG
27
28 #ifndef USE_HOSTCC
29 #include <common.h>
30 #include <watchdog.h>
31
32 #ifdef CONFIG_SHOW_BOOT_PROGRESS
33 #include <status_led.h>
34 #endif
35
36 #ifdef CONFIG_HAS_DATAFLASH
37 #include <dataflash.h>
38 #endif
39
40 #ifdef CONFIG_LOGBUFFER
41 #include <logbuff.h>
42 #endif
43
44 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
45 #include <rtc.h>
46 #endif
47
48 #include <image.h>
49
50 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
51 #include <fdt.h>
52 #include <libfdt.h>
53 #include <fdt_support.h>
54 #endif
55
56 #if defined(CONFIG_FIT)
57 #include <sha1.h>
58
59 static int fit_check_ramdisk (const void *fit, int os_noffset,
60                 uint8_t arch, int verify);
61 #endif
62
63 #ifdef CONFIG_CMD_BDI
64 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
65 #endif
66
67 DECLARE_GLOBAL_DATA_PTR;
68
69 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
70                                                 int verify);
71 #else
72 #include "mkimage.h"
73 #include <time.h>
74 #include <image.h>
75 #endif /* !USE_HOSTCC*/
76
77 typedef struct table_entry {
78         int     id;             /* as defined in image.h        */
79         char    *sname;         /* short (input) name           */
80         char    *lname;         /* long (output) name           */
81 } table_entry_t;
82
83 static table_entry_t uimage_arch[] = {
84         {       IH_ARCH_INVALID,        NULL,           "Invalid ARCH", },
85         {       IH_ARCH_ALPHA,          "alpha",        "Alpha",        },
86         {       IH_ARCH_ARM,            "arm",          "ARM",          },
87         {       IH_ARCH_I386,           "x86",          "Intel x86",    },
88         {       IH_ARCH_IA64,           "ia64",         "IA64",         },
89         {       IH_ARCH_M68K,           "m68k",         "M68K",         },
90         {       IH_ARCH_MICROBLAZE,     "microblaze",   "MicroBlaze",   },
91         {       IH_ARCH_MIPS,           "mips",         "MIPS",         },
92         {       IH_ARCH_MIPS64,         "mips64",       "MIPS 64 Bit",  },
93         {       IH_ARCH_NIOS,           "nios",         "NIOS",         },
94         {       IH_ARCH_NIOS2,          "nios2",        "NIOS II",      },
95         {       IH_ARCH_PPC,            "ppc",          "PowerPC",      },
96         {       IH_ARCH_S390,           "s390",         "IBM S390",     },
97         {       IH_ARCH_SH,             "sh",           "SuperH",       },
98         {       IH_ARCH_SPARC,          "sparc",        "SPARC",        },
99         {       IH_ARCH_SPARC64,        "sparc64",      "SPARC 64 Bit", },
100         {       IH_ARCH_BLACKFIN,       "blackfin",     "Blackfin",     },
101         {       IH_ARCH_AVR32,          "avr32",        "AVR32",        },
102         {       -1,                     "",             "",             },
103 };
104
105 static table_entry_t uimage_os[] = {
106         {       IH_OS_INVALID,  NULL,           "Invalid OS",           },
107 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
108         {       IH_OS_ARTOS,    "artos",        "ARTOS",                },
109 #endif
110         {       IH_OS_LINUX,    "linux",        "Linux",                },
111 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
112         {       IH_OS_LYNXOS,   "lynxos",       "LynxOS",               },
113 #endif
114         {       IH_OS_NETBSD,   "netbsd",       "NetBSD",               },
115         {       IH_OS_RTEMS,    "rtems",        "RTEMS",                },
116         {       IH_OS_U_BOOT,   "u-boot",       "U-Boot",               },
117 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
118         {       IH_OS_QNX,      "qnx",          "QNX",                  },
119         {       IH_OS_VXWORKS,  "vxworks",      "VxWorks",              },
120 #endif
121 #ifdef USE_HOSTCC
122         {       IH_OS_4_4BSD,   "4_4bsd",       "4_4BSD",               },
123         {       IH_OS_DELL,     "dell",         "Dell",                 },
124         {       IH_OS_ESIX,     "esix",         "Esix",                 },
125         {       IH_OS_FREEBSD,  "freebsd",      "FreeBSD",              },
126         {       IH_OS_IRIX,     "irix",         "Irix",                 },
127         {       IH_OS_NCR,      "ncr",          "NCR",                  },
128         {       IH_OS_OPENBSD,  "openbsd",      "OpenBSD",              },
129         {       IH_OS_PSOS,     "psos",         "pSOS",                 },
130         {       IH_OS_SCO,      "sco",          "SCO",                  },
131         {       IH_OS_SOLARIS,  "solaris",      "Solaris",              },
132         {       IH_OS_SVR4,     "svr4",         "SVR4",                 },
133 #endif
134         {       -1,             "",             "",                     },
135 };
136
137 static table_entry_t uimage_type[] = {
138         {       IH_TYPE_INVALID,    NULL,         "Invalid Image",      },
139         {       IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image",   },
140         {       IH_TYPE_FIRMWARE,   "firmware",   "Firmware",           },
141         {       IH_TYPE_KERNEL,     "kernel",     "Kernel Image",       },
142         {       IH_TYPE_MULTI,      "multi",      "Multi-File Image",   },
143         {       IH_TYPE_RAMDISK,    "ramdisk",    "RAMDisk Image",      },
144         {       IH_TYPE_SCRIPT,     "script",     "Script",             },
145         {       IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
146         {       IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",   },
147         {       -1,                 "",           "",                   },
148 };
149
150 static table_entry_t uimage_comp[] = {
151         {       IH_COMP_NONE,   "none",         "uncompressed",         },
152         {       IH_COMP_BZIP2,  "bzip2",        "bzip2 compressed",     },
153         {       IH_COMP_GZIP,   "gzip",         "gzip compressed",      },
154         {       -1,             "",             "",                     },
155 };
156
157 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
158 static void genimg_print_size (uint32_t size);
159 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
160 static void genimg_print_time (time_t timestamp);
161 #endif
162
163 /*****************************************************************************/
164 /* Legacy format routines */
165 /*****************************************************************************/
166 int image_check_hcrc (image_header_t *hdr)
167 {
168         ulong hcrc;
169         ulong len = image_get_header_size ();
170         image_header_t header;
171
172         /* Copy header so we can blank CRC field for re-calculation */
173         memmove (&header, (char *)hdr, image_get_header_size ());
174         image_set_hcrc (&header, 0);
175
176         hcrc = crc32 (0, (unsigned char *)&header, len);
177
178         return (hcrc == image_get_hcrc (hdr));
179 }
180
181 int image_check_dcrc (image_header_t *hdr)
182 {
183         ulong data = image_get_data (hdr);
184         ulong len = image_get_data_size (hdr);
185         ulong dcrc = crc32 (0, (unsigned char *)data, len);
186
187         return (dcrc == image_get_dcrc (hdr));
188 }
189
190 #ifndef USE_HOSTCC
191 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
192 {
193         ulong dcrc = 0;
194         ulong len = image_get_data_size (hdr);
195         ulong data = image_get_data (hdr);
196
197 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
198         ulong cdata = data;
199         ulong edata = cdata + len;
200
201         while (cdata < edata) {
202                 ulong chunk = edata - cdata;
203
204                 if (chunk > chunksz)
205                         chunk = chunksz;
206                 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
207                 cdata += chunk;
208
209                 WATCHDOG_RESET ();
210         }
211 #else
212         dcrc = crc32 (0, (unsigned char *)data, len);
213 #endif
214
215         return (dcrc == image_get_dcrc (hdr));
216 }
217 #endif /* !USE_HOSTCC */
218
219 /**
220  * image_multi_count - get component (sub-image) count
221  * @hdr: pointer to the header of the multi component image
222  *
223  * image_multi_count() returns number of components in a multi
224  * component image.
225  *
226  * Note: no checking of the image type is done, caller must pass
227  * a valid multi component image.
228  *
229  * returns:
230  *     number of components
231  */
232 ulong image_multi_count (image_header_t *hdr)
233 {
234         ulong i, count = 0;
235         uint32_t *size;
236
237         /* get start of the image payload, which in case of multi
238          * component images that points to a table of component sizes */
239         size = (uint32_t *)image_get_data (hdr);
240
241         /* count non empty slots */
242         for (i = 0; size[i]; ++i)
243                 count++;
244
245         return count;
246 }
247
248 /**
249  * image_multi_getimg - get component data address and size
250  * @hdr: pointer to the header of the multi component image
251  * @idx: index of the requested component
252  * @data: pointer to a ulong variable, will hold component data address
253  * @len: pointer to a ulong variable, will hold component size
254  *
255  * image_multi_getimg() returns size and data address for the requested
256  * component in a multi component image.
257  *
258  * Note: no checking of the image type is done, caller must pass
259  * a valid multi component image.
260  *
261  * returns:
262  *     data address and size of the component, if idx is valid
263  *     0 in data and len, if idx is out of range
264  */
265 void image_multi_getimg (image_header_t *hdr, ulong idx,
266                         ulong *data, ulong *len)
267 {
268         int i;
269         uint32_t *size;
270         ulong offset, tail, count, img_data;
271
272         /* get number of component */
273         count = image_multi_count (hdr);
274
275         /* get start of the image payload, which in case of multi
276          * component images that points to a table of component sizes */
277         size = (uint32_t *)image_get_data (hdr);
278
279         /* get address of the proper component data start, which means
280          * skipping sizes table (add 1 for last, null entry) */
281         img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
282
283         if (idx < count) {
284                 *len = uimage_to_cpu (size[idx]);
285                 offset = 0;
286                 tail = 0;
287
288                 /* go over all indices preceding requested component idx */
289                 for (i = 0; i < idx; i++) {
290                         /* add up i-th component size */
291                         offset += uimage_to_cpu (size[i]);
292
293                         /* add up alignment for i-th component */
294                         tail += (4 - uimage_to_cpu (size[i]) % 4);
295                 }
296
297                 /* calculate idx-th component data address */
298                 *data = img_data + offset + tail;
299         } else {
300                 *len = 0;
301                 *data = 0;
302         }
303 }
304
305 static void image_print_type (image_header_t *hdr)
306 {
307         const char *os, *arch, *type, *comp;
308
309         os = genimg_get_os_name (image_get_os (hdr));
310         arch = genimg_get_arch_name (image_get_arch (hdr));
311         type = genimg_get_type_name (image_get_type (hdr));
312         comp = genimg_get_comp_name (image_get_comp (hdr));
313
314         printf ("%s %s %s (%s)\n", arch, os, type, comp);
315 }
316
317 /**
318  * __image_print_contents - prints out the contents of the legacy format image
319  * @hdr: pointer to the legacy format image header
320  * @p: pointer to prefix string
321  *
322  * __image_print_contents() formats a multi line legacy image contents description.
323  * The routine prints out all header fields followed by the size/offset data
324  * for MULTI/SCRIPT images.
325  *
326  * returns:
327  *     no returned results
328  */
329 static void __image_print_contents (image_header_t *hdr, const char *p)
330 {
331         printf ("%sImage Name:   %.*s\n", p, IH_NMLEN, image_get_name (hdr));
332 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
333         printf ("%sCreated:      ", p);
334         genimg_print_time ((time_t)image_get_time (hdr));
335 #endif
336         printf ("%sImage Type:   ", p);
337         image_print_type (hdr);
338         printf ("%sData Size:    ", p);
339         genimg_print_size (image_get_data_size (hdr));
340         printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
341         printf ("%sEntry Point:  %08x\n", p, image_get_ep (hdr));
342
343         if (image_check_type (hdr, IH_TYPE_MULTI) ||
344                         image_check_type (hdr, IH_TYPE_SCRIPT)) {
345                 int i;
346                 ulong data, len;
347                 ulong count = image_multi_count (hdr);
348
349                 printf ("%sContents:\n", p);
350                 for (i = 0; i < count; i++) {
351                         image_multi_getimg (hdr, i, &data, &len);
352
353                         printf ("%s   Image %d: ", p, i);
354                         genimg_print_size (len);
355
356                         if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
357                                 /*
358                                  * the user may need to know offsets
359                                  * if planning to do something with
360                                  * multiple files
361                                  */
362                                 printf ("%s    Offset = 0x%08lx\n", p, data);
363                         }
364                 }
365         }
366 }
367
368 inline void image_print_contents (image_header_t *hdr)
369 {
370         __image_print_contents (hdr, "   ");
371 }
372
373 inline void image_print_contents_noindent (image_header_t *hdr)
374 {
375         __image_print_contents (hdr, "");
376 }
377
378 #ifndef USE_HOSTCC
379 /**
380  * image_get_ramdisk - get and verify ramdisk image
381  * @rd_addr: ramdisk image start address
382  * @arch: expected ramdisk architecture
383  * @verify: checksum verification flag
384  *
385  * image_get_ramdisk() returns a pointer to the verified ramdisk image
386  * header. Routine receives image start address and expected architecture
387  * flag. Verification done covers data and header integrity and os/type/arch
388  * fields checking.
389  *
390  * If dataflash support is enabled routine checks for dataflash addresses
391  * and handles required dataflash reads.
392  *
393  * returns:
394  *     pointer to a ramdisk image header, if image was found and valid
395  *     otherwise, return NULL
396  */
397 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
398                                                 int verify)
399 {
400         image_header_t *rd_hdr = (image_header_t *)rd_addr;
401
402         if (!image_check_magic (rd_hdr)) {
403                 puts ("Bad Magic Number\n");
404                 show_boot_progress (-10);
405                 return NULL;
406         }
407
408         if (!image_check_hcrc (rd_hdr)) {
409                 puts ("Bad Header Checksum\n");
410                 show_boot_progress (-11);
411                 return NULL;
412         }
413
414         show_boot_progress (10);
415         image_print_contents (rd_hdr);
416
417         if (verify) {
418                 puts("   Verifying Checksum ... ");
419                 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
420                         puts ("Bad Data CRC\n");
421                         show_boot_progress (-12);
422                         return NULL;
423                 }
424                 puts("OK\n");
425         }
426
427         show_boot_progress (11);
428
429         if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
430             !image_check_arch (rd_hdr, arch) ||
431             !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
432                 printf ("No Linux %s Ramdisk Image\n",
433                                 genimg_get_arch_name(arch));
434                 show_boot_progress (-13);
435                 return NULL;
436         }
437
438         return rd_hdr;
439 }
440 #endif /* !USE_HOSTCC */
441
442 /*****************************************************************************/
443 /* Shared dual-format routines */
444 /*****************************************************************************/
445 #ifndef USE_HOSTCC
446 int getenv_verify (void)
447 {
448         char *s = getenv ("verify");
449         return (s && (*s == 'n')) ? 0 : 1;
450 }
451
452 int getenv_autostart (void)
453 {
454         char *s = getenv ("autostart");
455         return (s && (*s == 'n')) ? 0 : 1;
456 }
457
458 ulong getenv_bootm_low(void)
459 {
460         char *s = getenv ("bootm_low");
461         if (s) {
462                 ulong tmp = simple_strtoul (s, NULL, 16);
463                 return tmp;
464         }
465
466 #ifdef CFG_SDRAM_BASE
467         return CFG_SDRAM_BASE;
468 #else
469         return 0;
470 #endif
471 }
472
473 ulong getenv_bootm_size(void)
474 {
475         char *s = getenv ("bootm_size");
476         if (s) {
477                 ulong tmp = simple_strtoul (s, NULL, 16);
478                 return tmp;
479         }
480
481         return gd->bd->bi_memsize;
482 }
483
484 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
485 {
486 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
487         while (len > 0) {
488                 size_t tail = (len > chunksz) ? chunksz : len;
489                 WATCHDOG_RESET ();
490                 memmove (to, from, tail);
491                 to += tail;
492                 from += tail;
493                 len -= tail;
494         }
495 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
496         memmove (to, from, len);
497 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
498 }
499 #endif /* !USE_HOSTCC */
500
501 static void genimg_print_size (uint32_t size)
502 {
503 #ifndef USE_HOSTCC
504         printf ("%d Bytes = ", size);
505         print_size (size, "\n");
506 #else
507         printf ("%d Bytes = %.2f kB = %.2f MB\n",
508                         size, (double)size / 1.024e3,
509                         (double)size / 1.048576e6);
510 #endif
511 }
512
513 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
514 static void genimg_print_time (time_t timestamp)
515 {
516 #ifndef USE_HOSTCC
517         struct rtc_time tm;
518
519         to_tm (timestamp, &tm);
520         printf ("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
521                         tm.tm_year, tm.tm_mon, tm.tm_mday,
522                         tm.tm_hour, tm.tm_min, tm.tm_sec);
523 #else
524         printf ("%s", ctime(&timestamp));
525 #endif
526 }
527 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
528
529 /**
530  * get_table_entry_name - translate entry id to long name
531  * @table: pointer to a translation table for entries of a specific type
532  * @msg: message to be returned when translation fails
533  * @id: entry id to be translated
534  *
535  * get_table_entry_name() will go over translation table trying to find
536  * entry that matches given id. If matching entry is found, its long
537  * name is returned to the caller.
538  *
539  * returns:
540  *     long entry name if translation succeeds
541  *     msg otherwise
542  */
543 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
544 {
545         for (; table->id >= 0; ++table) {
546                 if (table->id == id)
547                         return (table->lname);
548         }
549         return (msg);
550 }
551
552 const char *genimg_get_os_name (uint8_t os)
553 {
554         return (get_table_entry_name (uimage_os, "Unknown OS", os));
555 }
556
557 const char *genimg_get_arch_name (uint8_t arch)
558 {
559         return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
560 }
561
562 const char *genimg_get_type_name (uint8_t type)
563 {
564         return (get_table_entry_name (uimage_type, "Unknown Image", type));
565 }
566
567 const char *genimg_get_comp_name (uint8_t comp)
568 {
569         return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
570 }
571
572 /**
573  * get_table_entry_id - translate short entry name to id
574  * @table: pointer to a translation table for entries of a specific type
575  * @table_name: to be used in case of error
576  * @name: entry short name to be translated
577  *
578  * get_table_entry_id() will go over translation table trying to find
579  * entry that matches given short name. If matching entry is found,
580  * its id returned to the caller.
581  *
582  * returns:
583  *     entry id if translation succeeds
584  *     -1 otherwise
585  */
586 static int get_table_entry_id (table_entry_t *table,
587                 const char *table_name, const char *name)
588 {
589         table_entry_t *t;
590 #ifdef USE_HOSTCC
591         int first = 1;
592
593         for (t = table; t->id >= 0; ++t) {
594                 if (t->sname && strcasecmp(t->sname, name) == 0)
595                         return (t->id);
596         }
597
598         fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
599         for (t = table; t->id >= 0; ++t) {
600                 if (t->sname == NULL)
601                         continue;
602                 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
603                 first = 0;
604         }
605         fprintf (stderr, "\n");
606 #else
607         for (t = table; t->id >= 0; ++t) {
608                 if (t->sname && strcmp(t->sname, name) == 0)
609                         return (t->id);
610         }
611         debug ("Invalid %s Type: %s\n", table_name, name);
612 #endif /* USE_HOSTCC */
613         return (-1);
614 }
615
616 int genimg_get_os_id (const char *name)
617 {
618         return (get_table_entry_id (uimage_os, "OS", name));
619 }
620
621 int genimg_get_arch_id (const char *name)
622 {
623         return (get_table_entry_id (uimage_arch, "CPU", name));
624 }
625
626 int genimg_get_type_id (const char *name)
627 {
628         return (get_table_entry_id (uimage_type, "Image", name));
629 }
630
631 int genimg_get_comp_id (const char *name)
632 {
633         return (get_table_entry_id (uimage_comp, "Compression", name));
634 }
635
636 #ifndef USE_HOSTCC
637 /**
638  * genimg_get_format - get image format type
639  * @img_addr: image start address
640  *
641  * genimg_get_format() checks whether provided address points to a valid
642  * legacy or FIT image.
643  *
644  * New uImage format and FDT blob are based on a libfdt. FDT blob
645  * may be passed directly or embedded in a FIT image. In both situations
646  * genimg_get_format() must be able to dectect libfdt header.
647  *
648  * returns:
649  *     image format type or IMAGE_FORMAT_INVALID if no image is present
650  */
651 int genimg_get_format (void *img_addr)
652 {
653         ulong           format = IMAGE_FORMAT_INVALID;
654         image_header_t  *hdr;
655 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
656         char            *fit_hdr;
657 #endif
658
659         hdr = (image_header_t *)img_addr;
660         if (image_check_magic(hdr))
661                 format = IMAGE_FORMAT_LEGACY;
662 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
663         else {
664                 fit_hdr = (char *)img_addr;
665                 if (fdt_check_header (fit_hdr) == 0)
666                         format = IMAGE_FORMAT_FIT;
667         }
668 #endif
669
670         return format;
671 }
672
673 /**
674  * genimg_get_image - get image from special storage (if necessary)
675  * @img_addr: image start address
676  *
677  * genimg_get_image() checks if provided image start adddress is located
678  * in a dataflash storage. If so, image is moved to a system RAM memory.
679  *
680  * returns:
681  *     image start address after possible relocation from special storage
682  */
683 ulong genimg_get_image (ulong img_addr)
684 {
685         ulong ram_addr = img_addr;
686
687 #ifdef CONFIG_HAS_DATAFLASH
688         ulong h_size, d_size;
689
690         if (addr_dataflash (img_addr)){
691                 /* ger RAM address */
692                 ram_addr = CFG_LOAD_ADDR;
693
694                 /* get header size */
695                 h_size = image_get_header_size ();
696 #if defined(CONFIG_FIT)
697                 if (sizeof(struct fdt_header) > h_size)
698                         h_size = sizeof(struct fdt_header);
699 #endif
700
701                 /* read in header */
702                 debug ("   Reading image header from dataflash address "
703                         "%08lx to RAM address %08lx\n", img_addr, ram_addr);
704
705                 read_dataflash (img_addr, h_size, (char *)ram_addr);
706
707                 /* get data size */
708                 switch (genimg_get_format ((void *)ram_addr)) {
709                 case IMAGE_FORMAT_LEGACY:
710                         d_size = image_get_data_size ((image_header_t *)ram_addr);
711                         debug ("   Legacy format image found at 0x%08lx, size 0x%08lx\n",
712                                         ram_addr, d_size);
713                         break;
714 #if defined(CONFIG_FIT)
715                 case IMAGE_FORMAT_FIT:
716                         d_size = fit_get_size ((const void *)ram_addr) - h_size;
717                         debug ("   FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
718                                         ram_addr, d_size);
719                         break;
720 #endif
721                 default:
722                         printf ("   No valid image found at 0x%08lx\n", img_addr);
723                         return ram_addr;
724                 }
725
726                 /* read in image data */
727                 debug ("   Reading image remaining data from dataflash address "
728                         "%08lx to RAM address %08lx\n", img_addr + h_size,
729                         ram_addr + h_size);
730
731                 read_dataflash (img_addr + h_size, d_size,
732                                 (char *)(ram_addr + h_size));
733
734         }
735 #endif /* CONFIG_HAS_DATAFLASH */
736
737         return ram_addr;
738 }
739
740 /**
741  * boot_get_ramdisk - main ramdisk handling routine
742  * @argc: command argument count
743  * @argv: command argument list
744  * @images: pointer to the bootm images structure
745  * @arch: expected ramdisk architecture
746  * @rd_start: pointer to a ulong variable, will hold ramdisk start address
747  * @rd_end: pointer to a ulong variable, will hold ramdisk end
748  *
749  * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
750  * Curently supported are the following ramdisk sources:
751  *      - multicomponent kernel/ramdisk image,
752  *      - commandline provided address of decicated ramdisk image.
753  *
754  * returns:
755  *     0, if ramdisk image was found and valid, or skiped
756  *     rd_start and rd_end are set to ramdisk start/end addresses if
757  *     ramdisk image is found and valid
758  *
759  *     1, if ramdisk image is found but corrupted
760  *     rd_start and rd_end are set to 0 if no ramdisk exists
761  */
762 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
763                 uint8_t arch, ulong *rd_start, ulong *rd_end)
764 {
765         ulong rd_addr, rd_load;
766         ulong rd_data, rd_len;
767         image_header_t *rd_hdr;
768 #if defined(CONFIG_FIT)
769         void            *fit_hdr;
770         const char      *fit_uname_config = NULL;
771         const char      *fit_uname_ramdisk = NULL;
772         ulong           default_addr;
773         int             rd_noffset;
774         int             conf_noffset;
775         const void      *data;
776         size_t          size;
777 #endif
778
779         *rd_start = 0;
780         *rd_end = 0;
781
782         /*
783          * Look for a '-' which indicates to ignore the
784          * ramdisk argument
785          */
786         if ((argc >= 3) && (strcmp(argv[2], "-") ==  0)) {
787                 debug ("## Skipping init Ramdisk\n");
788                 rd_len = rd_data = 0;
789         } else if (argc >= 3) {
790 #if defined(CONFIG_FIT)
791                 /*
792                  * If the init ramdisk comes from the FIT image and the FIT image
793                  * address is omitted in the command line argument, try to use
794                  * os FIT image address or default load address.
795                  */
796                 if (images->fit_uname_os)
797                         default_addr = (ulong)images->fit_hdr_os;
798                 else
799                         default_addr = load_addr;
800
801                 if (fit_parse_conf (argv[2], default_addr,
802                                         &rd_addr, &fit_uname_config)) {
803                         debug ("*  ramdisk: config '%s' from image at 0x%08lx\n",
804                                         fit_uname_config, rd_addr);
805                 } else if (fit_parse_subimage (argv[2], default_addr,
806                                         &rd_addr, &fit_uname_ramdisk)) {
807                         debug ("*  ramdisk: subimage '%s' from image at 0x%08lx\n",
808                                         fit_uname_ramdisk, rd_addr);
809                 } else
810 #endif
811                 {
812                         rd_addr = simple_strtoul(argv[2], NULL, 16);
813                         debug ("*  ramdisk: cmdline image address = 0x%08lx\n",
814                                         rd_addr);
815                 }
816
817                 /* copy from dataflash if needed */
818                 rd_addr = genimg_get_image (rd_addr);
819
820                 /*
821                  * Check if there is an initrd image at the
822                  * address provided in the second bootm argument
823                  * check image type, for FIT images get FIT node.
824                  */
825                 switch (genimg_get_format ((void *)rd_addr)) {
826                 case IMAGE_FORMAT_LEGACY:
827                         printf ("## Loading init Ramdisk from Legacy "
828                                         "Image at %08lx ...\n", rd_addr);
829
830                         show_boot_progress (9);
831                         rd_hdr = image_get_ramdisk (rd_addr, arch,
832                                                         images->verify);
833
834                         if (rd_hdr == NULL)
835                                 return 1;
836
837                         rd_data = image_get_data (rd_hdr);
838                         rd_len = image_get_data_size (rd_hdr);
839                         rd_load = image_get_load (rd_hdr);
840                         break;
841 #if defined(CONFIG_FIT)
842                 case IMAGE_FORMAT_FIT:
843                         fit_hdr = (void *)rd_addr;
844                         printf ("## Loading init Ramdisk from FIT "
845                                         "Image at %08lx ...\n", rd_addr);
846
847                         show_boot_progress (120);
848                         if (!fit_check_format (fit_hdr)) {
849                                 puts ("Bad FIT ramdisk image format!\n");
850                                 show_boot_progress (-120);
851                                 return 0;
852                         }
853                         show_boot_progress (121);
854
855                         if (!fit_uname_ramdisk) {
856                                 /*
857                                  * no ramdisk image node unit name, try to get config
858                                  * node first. If config unit node name is NULL
859                                  * fit_conf_get_node() will try to find default config node
860                                  */
861                                 show_boot_progress (122);
862                                 conf_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
863                                 if (conf_noffset < 0) {
864                                         show_boot_progress (-122);
865                                         return 0;
866                                 }
867
868                                 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, conf_noffset);
869                                 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
870                         } else {
871                                 /* get ramdisk component image node offset */
872                                 show_boot_progress (123);
873                                 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
874                         }
875                         if (rd_noffset < 0) {
876                                 show_boot_progress (-124);
877                                 return 0;
878                         }
879
880                         printf ("   Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
881
882                         show_boot_progress (125);
883                         if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
884                                 return 0;
885
886                         /* get ramdisk image data address and length */
887                         if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
888                                 puts ("Could not find ramdisk subimage data!\n");
889                                 show_boot_progress (-127);
890                                 return 0;
891                         }
892                         show_boot_progress (128);
893
894                         rd_data = (ulong)data;
895                         rd_len = size;
896
897                         if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
898                                 puts ("Can't get ramdisk subimage load address!\n");
899                                 show_boot_progress (-129);
900                                 return 0;
901                         }
902                         show_boot_progress (129);
903
904                         images->fit_hdr_rd = fit_hdr;
905                         images->fit_uname_rd = fit_uname_ramdisk;
906                         images->fit_noffset_rd = rd_noffset;
907                         break;
908 #endif
909                 default:
910                         puts ("Wrong Ramdisk Image Format\n");
911                         rd_data = rd_len = rd_load = 0;
912                 }
913
914 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
915                 /*
916                  * We need to copy the ramdisk to SRAM to let Linux boot
917                  */
918                 if (rd_data) {
919                         memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
920                         rd_data = rd_load;
921                 }
922 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
923
924         } else if (images->legacy_hdr_valid &&
925                         image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
926                 /*
927                  * Now check if we have a legacy mult-component image,
928                  * get second entry data start address and len.
929                  */
930                 show_boot_progress (13);
931                 printf ("## Loading init Ramdisk from multi component "
932                                 "Legacy Image at %08lx ...\n",
933                                 (ulong)images->legacy_hdr_os);
934
935                 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
936         } else {
937                 /*
938                  * no initrd image
939                  */
940                 show_boot_progress (14);
941                 rd_len = rd_data = 0;
942         }
943
944         if (!rd_data) {
945                 debug ("## No init Ramdisk\n");
946         } else {
947                 *rd_start = rd_data;
948                 *rd_end = rd_data + rd_len;
949         }
950         debug ("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
951                         *rd_start, *rd_end);
952
953         return 0;
954 }
955
956 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
957 /**
958  * boot_ramdisk_high - relocate init ramdisk
959  * @lmb: pointer to lmb handle, will be used for memory mgmt
960  * @rd_data: ramdisk data start address
961  * @rd_len: ramdisk data length
962  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
963  *      start address (after possible relocation)
964  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
965  *      end address (after possible relocation)
966  *
967  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
968  * variable and if requested ramdisk data is moved to a specified location.
969  *
970  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
971  * start/end addresses if ramdisk image start and len were provided,
972  * otherwise set initrd_start and initrd_end set to zeros.
973  *
974  * returns:
975  *      0 - success
976  *     -1 - failure
977  */
978 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
979                   ulong *initrd_start, ulong *initrd_end)
980 {
981         char    *s;
982         ulong   initrd_high;
983         int     initrd_copy_to_ram = 1;
984
985         if ((s = getenv ("initrd_high")) != NULL) {
986                 /* a value of "no" or a similar string will act like 0,
987                  * turning the "load high" feature off. This is intentional.
988                  */
989                 initrd_high = simple_strtoul (s, NULL, 16);
990                 if (initrd_high == ~0)
991                         initrd_copy_to_ram = 0;
992         } else {
993                 /* not set, no restrictions to load high */
994                 initrd_high = ~0;
995         }
996
997         debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
998                         initrd_high, initrd_copy_to_ram);
999
1000         if (rd_data) {
1001                 if (!initrd_copy_to_ram) {      /* zero-copy ramdisk support */
1002                         debug ("   in-place initrd\n");
1003                         *initrd_start = rd_data;
1004                         *initrd_end = rd_data + rd_len;
1005                         lmb_reserve(lmb, rd_data, rd_len);
1006                 } else {
1007                         if (initrd_high)
1008                                 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1009                         else
1010                                 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
1011
1012                         if (*initrd_start == 0) {
1013                                 puts ("ramdisk - allocation error\n");
1014                                 goto error;
1015                         }
1016                         show_boot_progress (12);
1017
1018                         *initrd_end = *initrd_start + rd_len;
1019                         printf ("   Loading Ramdisk to %08lx, end %08lx ... ",
1020                                         *initrd_start, *initrd_end);
1021
1022                         memmove_wd ((void *)*initrd_start,
1023                                         (void *)rd_data, rd_len, CHUNKSZ);
1024
1025                         puts ("OK\n");
1026                 }
1027         } else {
1028                 *initrd_start = 0;
1029                 *initrd_end = 0;
1030         }
1031         debug ("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1032                         *initrd_start, *initrd_end);
1033
1034         return 0;
1035
1036 error:
1037         return -1;
1038 }
1039
1040 /**
1041  * boot_get_cmdline - allocate and initialize kernel cmdline
1042  * @lmb: pointer to lmb handle, will be used for memory mgmt
1043  * @cmd_start: pointer to a ulong variable, will hold cmdline start
1044  * @cmd_end: pointer to a ulong variable, will hold cmdline end
1045  * @bootmap_base: ulong variable, holds offset in physical memory to
1046  * base of bootmap
1047  *
1048  * boot_get_cmdline() allocates space for kernel command line below
1049  * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1050  * variable is present its contents is copied to allocated kernel
1051  * command line.
1052  *
1053  * returns:
1054  *      0 - success
1055  *     -1 - failure
1056  */
1057 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1058                         ulong bootmap_base)
1059 {
1060         char *cmdline;
1061         char *s;
1062
1063         cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1064                                          CFG_BOOTMAPSZ + bootmap_base);
1065
1066         if (cmdline == NULL)
1067                 return -1;
1068
1069         if ((s = getenv("bootargs")) == NULL)
1070                 s = "";
1071
1072         strcpy(cmdline, s);
1073
1074         *cmd_start = (ulong) & cmdline[0];
1075         *cmd_end = *cmd_start + strlen(cmdline);
1076
1077         debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1078
1079         return 0;
1080 }
1081
1082 /**
1083  * boot_get_kbd - allocate and initialize kernel copy of board info
1084  * @lmb: pointer to lmb handle, will be used for memory mgmt
1085  * @kbd: double pointer to board info data
1086  * @bootmap_base: ulong variable, holds offset in physical memory to
1087  * base of bootmap
1088  *
1089  * boot_get_kbd() allocates space for kernel copy of board info data below
1090  * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1091  * the current u-boot board info data.
1092  *
1093  * returns:
1094  *      0 - success
1095  *     -1 - failure
1096  */
1097 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1098 {
1099         *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1100                                       CFG_BOOTMAPSZ + bootmap_base);
1101         if (*kbd == NULL)
1102                 return -1;
1103
1104         **kbd = *(gd->bd);
1105
1106         debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1107
1108 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1109         do_bdinfo(NULL, 0, 0, NULL);
1110 #endif
1111
1112         return 0;
1113 }
1114 #endif /* CONFIG_PPC || CONFIG_M68K */
1115 #endif /* !USE_HOSTCC */
1116
1117 #if defined(CONFIG_FIT)
1118 /*****************************************************************************/
1119 /* New uImage format routines */
1120 /*****************************************************************************/
1121 #ifndef USE_HOSTCC
1122 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1123                 ulong *addr, const char **name)
1124 {
1125         const char *sep;
1126
1127         *addr = addr_curr;
1128         *name = NULL;
1129
1130         sep = strchr (spec, sepc);
1131         if (sep) {
1132                 if (sep - spec > 0)
1133                         *addr = simple_strtoul (spec, NULL, 16);
1134
1135                 *name = sep + 1;
1136                 return 1;
1137         }
1138
1139         return 0;
1140 }
1141
1142 /**
1143  * fit_parse_conf - parse FIT configuration spec
1144  * @spec: input string, containing configuration spec
1145  * @add_curr: current image address (to be used as a possible default)
1146  * @addr: pointer to a ulong variable, will hold FIT image address of a given
1147  * configuration
1148  * @conf_name double pointer to a char, will hold pointer to a configuration
1149  * unit name
1150  *
1151  * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1152  * where <addr> is a FIT image address that contains configuration
1153  * with a <conf> unit name.
1154  *
1155  * Address part is optional, and if omitted default add_curr will
1156  * be used instead.
1157  *
1158  * returns:
1159  *     1 if spec is a valid configuration string,
1160  *     addr and conf_name are set accordingly
1161  *     0 otherwise
1162  */
1163 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1164                 ulong *addr, const char **conf_name)
1165 {
1166         return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1167 }
1168
1169 /**
1170  * fit_parse_subimage - parse FIT subimage spec
1171  * @spec: input string, containing subimage spec
1172  * @add_curr: current image address (to be used as a possible default)
1173  * @addr: pointer to a ulong variable, will hold FIT image address of a given
1174  * subimage
1175  * @image_name: double pointer to a char, will hold pointer to a subimage name
1176  *
1177  * fit_parse_subimage() expects subimage spec in the for of
1178  * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1179  * subimage with a <subimg> unit name.
1180  *
1181  * Address part is optional, and if omitted default add_curr will
1182  * be used instead.
1183  *
1184  * returns:
1185  *     1 if spec is a valid subimage string,
1186  *     addr and image_name are set accordingly
1187  *     0 otherwise
1188  */
1189 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1190                 ulong *addr, const char **image_name)
1191 {
1192         return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1193 }
1194 #endif /* !USE_HOSTCC */
1195
1196 static void fit_get_debug (const void *fit, int noffset,
1197                 char *prop_name, int err)
1198 {
1199         debug ("Can't get '%s' property from FIT 0x%08lx, "
1200                 "node: offset %d, name %s (%s)\n",
1201                 prop_name, (ulong)fit, noffset,
1202                 fit_get_name (fit, noffset, NULL),
1203                 fdt_strerror (err));
1204 }
1205
1206 /**
1207  * __fit_print_contents - prints out the contents of the FIT format image
1208  * @fit: pointer to the FIT format image header
1209  * @p: pointer to prefix string
1210  *
1211  * __fit_print_contents() formats a multi line FIT image contents description.
1212  * The routine prints out FIT image properties (root node level) follwed by
1213  * the details of each component image.
1214  *
1215  * returns:
1216  *     no returned results
1217  */
1218 static void __fit_print_contents (const void *fit, const char *p)
1219 {
1220         char *desc;
1221         char *uname;
1222         int images_noffset;
1223         int confs_noffset;
1224         int noffset;
1225         int ndepth;
1226         int count = 0;
1227         int ret;
1228 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1229         time_t timestamp;
1230 #endif
1231
1232         /* Root node properties */
1233         ret = fit_get_desc (fit, 0, &desc);
1234         printf ("%sFIT description: ", p);
1235         if (ret)
1236                 printf ("unavailable\n");
1237         else
1238                 printf ("%s\n", desc);
1239
1240 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1241         ret = fit_get_timestamp (fit, 0, &timestamp);
1242         printf ("%sCreated:         ", p);
1243         if (ret)
1244                 printf ("unavailable\n");
1245         else
1246                 genimg_print_time (timestamp);
1247 #endif
1248
1249         /* Find images parent node offset */
1250         images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1251         if (images_noffset < 0) {
1252                 printf ("Can't find images parent node '%s' (%s)\n",
1253                         FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1254                 return;
1255         }
1256
1257         /* Process its subnodes, print out component images details */
1258         for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1259              (noffset >= 0) && (ndepth > 0);
1260              noffset = fdt_next_node (fit, noffset, &ndepth)) {
1261                 if (ndepth == 1) {
1262                         /*
1263                          * Direct child node of the images parent node,
1264                          * i.e. component image node.
1265                          */
1266                         printf ("%s Image %u (%s)\n", p, count++,
1267                                         fit_get_name(fit, noffset, NULL));
1268
1269                         fit_image_print (fit, noffset, p);
1270                 }
1271         }
1272
1273         /* Find configurations parent node offset */
1274         confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1275         if (confs_noffset < 0) {
1276                 debug ("Can't get configurations parent node '%s' (%s)\n",
1277                         FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1278                 return;
1279         }
1280
1281         /* get default configuration unit name from default property */
1282         uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1283         if (uname)
1284                 printf ("%s Default Configuration: '%s'\n", p, uname);
1285
1286         /* Process its subnodes, print out configurations details */
1287         for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1288              (noffset >= 0) && (ndepth > 0);
1289              noffset = fdt_next_node (fit, noffset, &ndepth)) {
1290                 if (ndepth == 1) {
1291                         /*
1292                          * Direct child node of the configurations parent node,
1293                          * i.e. configuration node.
1294                          */
1295                         printf ("%s Configuration %u (%s)\n", p, count++,
1296                                         fit_get_name(fit, noffset, NULL));
1297
1298                         fit_conf_print (fit, noffset, p);
1299                 }
1300         }
1301 }
1302
1303 inline void fit_print_contents (const void *fit)
1304 {
1305         __fit_print_contents (fit, "   ");
1306 }
1307
1308 inline void fit_print_contents_noindent (const void *fit)
1309 {
1310         __fit_print_contents (fit, "");
1311 }
1312
1313 /**
1314  * fit_image_print - prints out the FIT component image details
1315  * @fit: pointer to the FIT format image header
1316  * @image_noffset: offset of the component image node
1317  * @p: pointer to prefix string
1318  *
1319  * fit_image_print() lists all mandatory properies for the processed component
1320  * image. If present, hash nodes are printed out as well.
1321  *
1322  * returns:
1323  *     no returned results
1324  */
1325 void fit_image_print (const void *fit, int image_noffset, const char *p)
1326 {
1327         char *desc;
1328         uint8_t type, arch, os, comp;
1329         size_t size;
1330         ulong load, entry;
1331         const void *data;
1332         int noffset;
1333         int ndepth;
1334         int ret;
1335
1336         /* Mandatory properties */
1337         ret = fit_get_desc (fit, image_noffset, &desc);
1338         printf ("%s  Description:  ", p);
1339         if (ret)
1340                 printf ("unavailable\n");
1341         else
1342                 printf ("%s\n", desc);
1343
1344         fit_image_get_type (fit, image_noffset, &type);
1345         printf ("%s  Type:         %s\n", p, genimg_get_type_name (type));
1346
1347         fit_image_get_comp (fit, image_noffset, &comp);
1348         printf ("%s  Compression:  %s\n", p, genimg_get_comp_name (comp));
1349
1350         ret = fit_image_get_data (fit, image_noffset, &data, &size);
1351
1352 #ifndef USE_HOSTCC
1353         printf ("%s  Data Start:   ", p);
1354         if (ret)
1355                 printf ("unavailable\n");
1356         else
1357                 printf ("0x%08lx\n", (ulong)data);
1358 #endif
1359
1360         printf ("%s  Data Size:    ", p);
1361         if (ret)
1362                 printf ("unavailable\n");
1363         else
1364                 genimg_print_size (size);
1365
1366         /* Remaining, type dependent properties */
1367         if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1368             (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1369             (type == IH_TYPE_FLATDT)) {
1370                 fit_image_get_arch (fit, image_noffset, &arch);
1371                 printf ("%s  Architecture: %s\n", p, genimg_get_arch_name (arch));
1372         }
1373
1374         if (type == IH_TYPE_KERNEL) {
1375                 fit_image_get_os (fit, image_noffset, &os);
1376                 printf ("%s  OS:           %s\n", p, genimg_get_os_name (os));
1377         }
1378
1379         if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1380                 ret = fit_image_get_load (fit, image_noffset, &load);
1381                 printf ("%s  Load Address: ", p);
1382                 if (ret)
1383                         printf ("unavailable\n");
1384                 else
1385                         printf ("0x%08lx\n", load);
1386
1387                 fit_image_get_entry (fit, image_noffset, &entry);
1388                 printf ("%s  Entry Point:  ", p);
1389                 if (ret)
1390                         printf ("unavailable\n");
1391                 else
1392                         printf ("0x%08lx\n", entry);
1393         }
1394
1395         /* Process all hash subnodes of the component image node */
1396         for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1397              (noffset >= 0) && (ndepth > 0);
1398              noffset = fdt_next_node (fit, noffset, &ndepth)) {
1399                 if (ndepth == 1) {
1400                         /* Direct child node of the component image node */
1401                         fit_image_print_hash (fit, noffset, p);
1402                 }
1403         }
1404 }
1405
1406 /**
1407  * fit_image_print_hash - prints out the hash node details
1408  * @fit: pointer to the FIT format image header
1409  * @noffset: offset of the hash node
1410  * @p: pointer to prefix string
1411  *
1412  * fit_image_print_hash() lists properies for the processed hash node
1413  *
1414  * returns:
1415  *     no returned results
1416  */
1417 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1418 {
1419         char *algo;
1420         uint8_t *value;
1421         int value_len;
1422         int i, ret;
1423
1424         /*
1425          * Check subnode name, must be equal to "hash".
1426          * Multiple hash nodes require unique unit node
1427          * names, e.g. hash@1, hash@2, etc.
1428          */
1429         if (strncmp (fit_get_name(fit, noffset, NULL),
1430                         FIT_HASH_NODENAME,
1431                         strlen(FIT_HASH_NODENAME)) != 0)
1432                 return;
1433
1434         debug ("%s  Hash node:    '%s'\n", p,
1435                         fit_get_name (fit, noffset, NULL));
1436
1437         printf ("%s  Hash algo:    ", p);
1438         if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1439                 printf ("invalid/unsupported\n");
1440                 return;
1441         }
1442         printf ("%s\n", algo);
1443
1444         ret = fit_image_hash_get_value (fit, noffset, &value,
1445                                         &value_len);
1446         printf ("%s  Hash value:   ", p);
1447         if (ret) {
1448                 printf ("unavailable\n");
1449         } else {
1450                 for (i = 0; i < value_len; i++)
1451                         printf ("%02x", value[i]);
1452                 printf ("\n");
1453         }
1454
1455         debug  ("%s  Hash len:     %d\n", p, value_len);
1456 }
1457
1458 /**
1459  * fit_get_desc - get node description property
1460  * @fit: pointer to the FIT format image header
1461  * @noffset: node offset
1462  * @desc: double pointer to the char, will hold pointer to the descrption
1463  *
1464  * fit_get_desc() reads description property from a given node, if
1465  * description is found pointer to it is returened in third call argument.
1466  *
1467  * returns:
1468  *     0, on success
1469  *     -1, on failure
1470  */
1471 int fit_get_desc (const void *fit, int noffset, char **desc)
1472 {
1473         int len;
1474
1475         *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1476         if (*desc == NULL) {
1477                 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1478                 return -1;
1479         }
1480
1481         return 0;
1482 }
1483
1484 /**
1485  * fit_get_timestamp - get node timestamp property
1486  * @fit: pointer to the FIT format image header
1487  * @noffset: node offset
1488  * @timestamp: pointer to the time_t, will hold read timestamp
1489  *
1490  * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1491  * is found and has a correct size its value is retured in third call
1492  * argument.
1493  *
1494  * returns:
1495  *     0, on success
1496  *     -1, on property read failure
1497  *     -2, on wrong timestamp size
1498  */
1499 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1500 {
1501         int len;
1502         const void *data;
1503
1504         data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1505         if (data == NULL) {
1506                 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1507                 return -1;
1508         }
1509         if (len != sizeof (uint32_t)) {
1510                 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1511                 return -2;
1512         }
1513
1514         *timestamp = uimage_to_cpu (*((uint32_t *)data));
1515         return 0;
1516 }
1517
1518 /**
1519  * fit_image_get_node - get node offset for component image of a given unit name
1520  * @fit: pointer to the FIT format image header
1521  * @image_uname: component image node unit name
1522  *
1523  * fit_image_get_node() finds a component image (withing the '/images'
1524  * node) of a provided unit name. If image is found its node offset is
1525  * returned to the caller.
1526  *
1527  * returns:
1528  *     image node offset when found (>=0)
1529  *     negative number on failure (FDT_ERR_* code)
1530  */
1531 int fit_image_get_node (const void *fit, const char *image_uname)
1532 {
1533         int noffset, images_noffset;
1534
1535         images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1536         if (images_noffset < 0) {
1537                 debug ("Can't find images parent node '%s' (%s)\n",
1538                         FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1539                 return images_noffset;
1540         }
1541
1542         noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1543         if (noffset < 0) {
1544                 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1545                         image_uname, fdt_strerror (noffset));
1546         }
1547
1548         return noffset;
1549 }
1550
1551 /**
1552  * fit_image_get_os - get os id for a given component image node
1553  * @fit: pointer to the FIT format image header
1554  * @noffset: component image node offset
1555  * @os: pointer to the uint8_t, will hold os numeric id
1556  *
1557  * fit_image_get_os() finds os property in a given component image node.
1558  * If the property is found, its (string) value is translated to the numeric
1559  * id which is returned to the caller.
1560  *
1561  * returns:
1562  *     0, on success
1563  *     -1, on failure
1564  */
1565 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1566 {
1567         int len;
1568         const void *data;
1569
1570         /* Get OS name from property data */
1571         data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1572         if (data == NULL) {
1573                 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1574                 *os = -1;
1575                 return -1;
1576         }
1577
1578         /* Translate OS name to id */
1579         *os = genimg_get_os_id (data);
1580         return 0;
1581 }
1582
1583 /**
1584  * fit_image_get_arch - get arch id for a given component image node
1585  * @fit: pointer to the FIT format image header
1586  * @noffset: component image node offset
1587  * @arch: pointer to the uint8_t, will hold arch numeric id
1588  *
1589  * fit_image_get_arch() finds arch property in a given component image node.
1590  * If the property is found, its (string) value is translated to the numeric
1591  * id which is returned to the caller.
1592  *
1593  * returns:
1594  *     0, on success
1595  *     -1, on failure
1596  */
1597 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1598 {
1599         int len;
1600         const void *data;
1601
1602         /* Get architecture name from property data */
1603         data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1604         if (data == NULL) {
1605                 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1606                 *arch = -1;
1607                 return -1;
1608         }
1609
1610         /* Translate architecture name to id */
1611         *arch = genimg_get_arch_id (data);
1612         return 0;
1613 }
1614
1615 /**
1616  * fit_image_get_type - get type id for a given component image node
1617  * @fit: pointer to the FIT format image header
1618  * @noffset: component image node offset
1619  * @type: pointer to the uint8_t, will hold type numeric id
1620  *
1621  * fit_image_get_type() finds type property in a given component image node.
1622  * If the property is found, its (string) value is translated to the numeric
1623  * id which is returned to the caller.
1624  *
1625  * returns:
1626  *     0, on success
1627  *     -1, on failure
1628  */
1629 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1630 {
1631         int len;
1632         const void *data;
1633
1634         /* Get image type name from property data */
1635         data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1636         if (data == NULL) {
1637                 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1638                 *type = -1;
1639                 return -1;
1640         }
1641
1642         /* Translate image type name to id */
1643         *type = genimg_get_type_id (data);
1644         return 0;
1645 }
1646
1647 /**
1648  * fit_image_get_comp - get comp id for a given component image node
1649  * @fit: pointer to the FIT format image header
1650  * @noffset: component image node offset
1651  * @comp: pointer to the uint8_t, will hold comp numeric id
1652  *
1653  * fit_image_get_comp() finds comp property in a given component image node.
1654  * If the property is found, its (string) value is translated to the numeric
1655  * id which is returned to the caller.
1656  *
1657  * returns:
1658  *     0, on success
1659  *     -1, on failure
1660  */
1661 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1662 {
1663         int len;
1664         const void *data;
1665
1666         /* Get compression name from property data */
1667         data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1668         if (data == NULL) {
1669                 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1670                 *comp = -1;
1671                 return -1;
1672         }
1673
1674         /* Translate compression name to id */
1675         *comp = genimg_get_comp_id (data);
1676         return 0;
1677 }
1678
1679 /**
1680  * fit_image_get_load - get load address property for a given component image node
1681  * @fit: pointer to the FIT format image header
1682  * @noffset: component image node offset
1683  * @load: pointer to the uint32_t, will hold load address
1684  *
1685  * fit_image_get_load() finds load address property in a given component image node.
1686  * If the property is found, its value is returned to the caller.
1687  *
1688  * returns:
1689  *     0, on success
1690  *     -1, on failure
1691  */
1692 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1693 {
1694         int len;
1695         const uint32_t *data;
1696
1697         data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1698         if (data == NULL) {
1699                 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1700                 return -1;
1701         }
1702
1703         *load = uimage_to_cpu (*data);
1704         return 0;
1705 }
1706
1707 /**
1708  * fit_image_get_entry - get entry point address property for a given component image node
1709  * @fit: pointer to the FIT format image header
1710  * @noffset: component image node offset
1711  * @entry: pointer to the uint32_t, will hold entry point address
1712  *
1713  * fit_image_get_entry() finds entry point address property in a given component image node.
1714  * If the property is found, its value is returned to the caller.
1715  *
1716  * returns:
1717  *     0, on success
1718  *     -1, on failure
1719  */
1720 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1721 {
1722         int len;
1723         const uint32_t *data;
1724
1725         data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1726         if (data == NULL) {
1727                 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1728                 return -1;
1729         }
1730
1731         *entry = uimage_to_cpu (*data);
1732         return 0;
1733 }
1734
1735 /**
1736  * fit_image_get_data - get data property and its size for a given component image node
1737  * @fit: pointer to the FIT format image header
1738  * @noffset: component image node offset
1739  * @data: double pointer to void, will hold data property's data address
1740  * @size: pointer to size_t, will hold data property's data size
1741  *
1742  * fit_image_get_data() finds data property in a given component image node.
1743  * If the property is found its data start address and size are returned to
1744  * the caller.
1745  *
1746  * returns:
1747  *     0, on success
1748  *     -1, on failure
1749  */
1750 int fit_image_get_data (const void *fit, int noffset,
1751                 const void **data, size_t *size)
1752 {
1753         int len;
1754
1755         *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1756         if (*data == NULL) {
1757                 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1758                 *size = 0;
1759                 return -1;
1760         }
1761
1762         *size = len;
1763         return 0;
1764 }
1765
1766 /**
1767  * fit_image_hash_get_algo - get hash algorithm name
1768  * @fit: pointer to the FIT format image header
1769  * @noffset: hash node offset
1770  * @algo: double pointer to char, will hold pointer to the algorithm name
1771  *
1772  * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1773  * If the property is found its data start address is returned to the caller.
1774  *
1775  * returns:
1776  *     0, on success
1777  *     -1, on failure
1778  */
1779 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1780 {
1781         int len;
1782
1783         *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1784         if (*algo == NULL) {
1785                 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1786                 return -1;
1787         }
1788
1789         return 0;
1790 }
1791
1792 /**
1793  * fit_image_hash_get_value - get hash value and length
1794  * @fit: pointer to the FIT format image header
1795  * @noffset: hash node offset
1796  * @value: double pointer to uint8_t, will hold address of a hash value data
1797  * @value_len: pointer to an int, will hold hash data length
1798  *
1799  * fit_image_hash_get_value() finds hash value property in a given hash node.
1800  * If the property is found its data start address and size are returned to
1801  * the caller.
1802  *
1803  * returns:
1804  *     0, on success
1805  *     -1, on failure
1806  */
1807 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1808                                 int *value_len)
1809 {
1810         int len;
1811
1812         *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1813         if (*value == NULL) {
1814                 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1815                 *value_len = 0;
1816                 return -1;
1817         }
1818
1819         *value_len = len;
1820         return 0;
1821 }
1822
1823 /**
1824  * fit_set_timestamp - set node timestamp property
1825  * @fit: pointer to the FIT format image header
1826  * @noffset: node offset
1827  * @timestamp: timestamp value to be set
1828  *
1829  * fit_set_timestamp() attempts to set timestamp property in the requested
1830  * node and returns operation status to the caller.
1831  *
1832  * returns:
1833  *     0, on success
1834  *     -1, on property read failure
1835  */
1836 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1837 {
1838         uint32_t t;
1839         int ret;
1840
1841         t = cpu_to_uimage (timestamp);
1842         ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1843                                 sizeof (uint32_t));
1844         if (ret) {
1845                 printf ("Can't set '%s' property for '%s' node (%s)\n",
1846                         FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1847                         fdt_strerror (ret));
1848                 return -1;
1849         }
1850
1851         return 0;
1852 }
1853
1854 /**
1855  * calculate_hash - calculate and return hash for provided input data
1856  * @data: pointer to the input data
1857  * @data_len: data length
1858  * @algo: requested hash algorithm
1859  * @value: pointer to the char, will hold hash value data (caller must
1860  * allocate enough free space)
1861  * value_len: length of the calculated hash
1862  *
1863  * calculate_hash() computes input data hash according to the requested algorithm.
1864  * Resulting hash value is placed in caller provided 'value' buffer, length
1865  * of the calculated hash is returned via value_len pointer argument.
1866  *
1867  * returns:
1868  *     0, on success
1869  *    -1, when algo is unsupported
1870  */
1871 static int calculate_hash (const void *data, int data_len, const char *algo,
1872                         uint8_t *value, int *value_len)
1873 {
1874         if (strcmp (algo, "crc32") == 0 ) {
1875                 *((uint32_t *)value) = crc32 (0, data, data_len);
1876                 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1877                 *value_len = 4;
1878         } else if (strcmp (algo, "sha1") == 0 ) {
1879                 sha1_csum ((unsigned char *) data, data_len,
1880                                 (unsigned char *) value);
1881                 *value_len = 20;
1882         } else if (strcmp (algo, "md5") == 0 ) {
1883                 printf ("MD5 not supported\n");
1884                 *value_len = 0;
1885         } else {
1886                 debug ("Unsupported hash alogrithm\n");
1887                 return -1;
1888         }
1889         return 0;
1890 }
1891
1892 #ifdef USE_HOSTCC
1893 /**
1894  * fit_set_hashes - process FIT component image nodes and calculate hashes
1895  * @fit: pointer to the FIT format image header
1896  *
1897  * fit_set_hashes() adds hash values for all component images in the FIT blob.
1898  * Hashes are calculated for all component images which have hash subnodes
1899  * with algorithm property set to one of the supported hash algorithms.
1900  *
1901  * returns
1902  *     0, on success
1903  *     libfdt error code, on failure
1904  */
1905 int fit_set_hashes (void *fit)
1906 {
1907         int images_noffset;
1908         int noffset;
1909         int ndepth;
1910         int ret;
1911
1912         /* Find images parent node offset */
1913         images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1914         if (images_noffset < 0) {
1915                 printf ("Can't find images parent node '%s' (%s)\n",
1916                         FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1917                 return images_noffset;
1918         }
1919
1920         /* Process its subnodes, print out component images details */
1921         for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1922              (noffset >= 0) && (ndepth > 0);
1923              noffset = fdt_next_node (fit, noffset, &ndepth)) {
1924                 if (ndepth == 1) {
1925                         /*
1926                          * Direct child node of the images parent node,
1927                          * i.e. component image node.
1928                          */
1929                         ret = fit_image_set_hashes (fit, noffset);
1930                         if (ret)
1931                                 return ret;
1932                 }
1933         }
1934
1935         return 0;
1936 }
1937
1938 /**
1939  * fit_image_set_hashes - calculate/set hashes for given component image node
1940  * @fit: pointer to the FIT format image header
1941  * @image_noffset: requested component image node
1942  *
1943  * fit_image_set_hashes() adds hash values for an component image node. All
1944  * existing hash subnodes are checked, if algorithm property is set to one of
1945  * the supported hash algorithms, hash value is computed and corresponding
1946  * hash node property is set, for example:
1947  *
1948  * Input component image node structure:
1949  *
1950  * o image@1 (at image_noffset)
1951  *   | - data = [binary data]
1952  *   o hash@1
1953  *     |- algo = "sha1"
1954  *
1955  * Output component image node structure:
1956  *
1957  * o image@1 (at image_noffset)
1958  *   | - data = [binary data]
1959  *   o hash@1
1960  *     |- algo = "sha1"
1961  *     |- value = sha1(data)
1962  *
1963  * returns:
1964  *     0 on sucess
1965  *    <0 on failure
1966  */
1967 int fit_image_set_hashes (void *fit, int image_noffset)
1968 {
1969         const void *data;
1970         size_t size;
1971         char *algo;
1972         uint8_t value[FIT_MAX_HASH_LEN];
1973         int value_len;
1974         int noffset;
1975         int ndepth;
1976
1977         /* Get image data and data length */
1978         if (fit_image_get_data (fit, image_noffset, &data, &size)) {
1979                 printf ("Can't get image data/size\n");
1980                 return -1;
1981         }
1982
1983         /* Process all hash subnodes of the component image node */
1984         for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1985              (noffset >= 0) && (ndepth > 0);
1986              noffset = fdt_next_node (fit, noffset, &ndepth)) {
1987                 if (ndepth == 1) {
1988                         /* Direct child node of the component image node */
1989
1990                         /*
1991                          * Check subnode name, must be equal to "hash".
1992                          * Multiple hash nodes require unique unit node
1993                          * names, e.g. hash@1, hash@2, etc.
1994                          */
1995                         if (strncmp (fit_get_name(fit, noffset, NULL),
1996                                                 FIT_HASH_NODENAME,
1997                                                 strlen(FIT_HASH_NODENAME)) != 0) {
1998                                 /* Not a hash subnode, skip it */
1999                                 continue;
2000                         }
2001
2002                         if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2003                                 printf ("Can't get hash algo property for "
2004                                         "'%s' hash node in '%s' image node\n",
2005                                         fit_get_name (fit, noffset, NULL),
2006                                         fit_get_name (fit, image_noffset, NULL));
2007                                 return -1;
2008                         }
2009
2010                         if (calculate_hash (data, size, algo, value, &value_len)) {
2011                                 printf ("Unsupported hash algorithm (%s) for "
2012                                         "'%s' hash node in '%s' image node\n",
2013                                         algo, fit_get_name (fit, noffset, NULL),
2014                                         fit_get_name (fit, image_noffset, NULL));
2015                                 return -1;
2016                         }
2017
2018                         if (fit_image_hash_set_value (fit, noffset, value,
2019                                                         value_len)) {
2020                                 printf ("Can't set hash value for "
2021                                         "'%s' hash node in '%s' image node\n",
2022                                         fit_get_name (fit, noffset, NULL),
2023                                         fit_get_name (fit, image_noffset, NULL));
2024                                 return -1;
2025                         }
2026                 }
2027         }
2028
2029         return 0;
2030 }
2031
2032 /**
2033  * fit_image_hash_set_value - set hash value in requested has node
2034  * @fit: pointer to the FIT format image header
2035  * @noffset: hash node offset
2036  * @value: hash value to be set
2037  * @value_len: hash value length
2038  *
2039  * fit_image_hash_set_value() attempts to set hash value in a node at offset
2040  * given and returns operation status to the caller.
2041  *
2042  * returns
2043  *     0, on success
2044  *     -1, on failure
2045  */
2046 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2047                                 int value_len)
2048 {
2049         int ret;
2050
2051         ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2052         if (ret) {
2053                 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2054                         FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2055                         fdt_strerror (ret));
2056                 return -1;
2057         }
2058
2059         return 0;
2060 }
2061 #endif /* USE_HOSTCC */
2062
2063 /**
2064  * fit_image_check_hashes - verify data intergity
2065  * @fit: pointer to the FIT format image header
2066  * @image_noffset: component image node offset
2067  *
2068  * fit_image_check_hashes() goes over component image hash nodes,
2069  * re-calculates each data hash and compares with the value stored in hash
2070  * node.
2071  *
2072  * returns:
2073  *     1, if all hashes are valid
2074  *     0, otherwise (or on error)
2075  */
2076 int fit_image_check_hashes (const void *fit, int image_noffset)
2077 {
2078         const void      *data;
2079         size_t          size;
2080         char            *algo;
2081         uint8_t         *fit_value;
2082         int             fit_value_len;
2083         uint8_t         value[FIT_MAX_HASH_LEN];
2084         int             value_len;
2085         int             noffset;
2086         int             ndepth;
2087         char            *err_msg = "";
2088
2089         /* Get image data and data length */
2090         if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2091                 printf ("Can't get image data/size\n");
2092                 return 0;
2093         }
2094
2095         /* Process all hash subnodes of the component image node */
2096         for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2097              (noffset >= 0) && (ndepth > 0);
2098              noffset = fdt_next_node (fit, noffset, &ndepth)) {
2099                 if (ndepth == 1) {
2100                         /* Direct child node of the component image node */
2101
2102                         /*
2103                          * Check subnode name, must be equal to "hash".
2104                          * Multiple hash nodes require unique unit node
2105                          * names, e.g. hash@1, hash@2, etc.
2106                          */
2107                         if (strncmp (fit_get_name(fit, noffset, NULL),
2108                                         FIT_HASH_NODENAME,
2109                                         strlen(FIT_HASH_NODENAME)) != 0)
2110                                 continue;
2111
2112                         if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2113                                 err_msg = "Can't get hash algo property";
2114                                 goto error;
2115                         }
2116                         printf ("%s", algo);
2117
2118                         if (fit_image_hash_get_value (fit, noffset, &fit_value,
2119                                                         &fit_value_len)) {
2120                                 err_msg = "Can't get hash value property";
2121                                 goto error;
2122                         }
2123
2124                         if (calculate_hash (data, size, algo, value, &value_len)) {
2125                                 err_msg = "Unsupported hash algorithm";
2126                                 goto error;
2127                         }
2128
2129                         if (value_len != fit_value_len) {
2130                                 err_msg = "Bad hash value len";
2131                                 goto error;
2132                         } else if (memcmp (value, fit_value, value_len) != 0) {
2133                                 err_msg = "Bad hash value";
2134                                 goto error;
2135                         }
2136                         printf ("+ ");
2137                 }
2138         }
2139
2140         return 1;
2141
2142 error:
2143         printf ("%s for '%s' hash node in '%s' image node\n",
2144                         err_msg, fit_get_name (fit, noffset, NULL),
2145                         fit_get_name (fit, image_noffset, NULL));
2146         return 0;
2147 }
2148
2149 /**
2150  * fit_image_check_os - check whether image node is of a given os type
2151  * @fit: pointer to the FIT format image header
2152  * @noffset: component image node offset
2153  * @os: requested image os
2154  *
2155  * fit_image_check_os() reads image os property and compares its numeric
2156  * id with the requested os. Comparison result is returned to the caller.
2157  *
2158  * returns:
2159  *     1 if image is of given os type
2160  *     0 otherwise (or on error)
2161  */
2162 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2163 {
2164         uint8_t image_os;
2165
2166         if (fit_image_get_os (fit, noffset, &image_os))
2167                 return 0;
2168         return (os == image_os);
2169 }
2170
2171 /**
2172  * fit_image_check_arch - check whether image node is of a given arch
2173  * @fit: pointer to the FIT format image header
2174  * @noffset: component image node offset
2175  * @arch: requested imagearch
2176  *
2177  * fit_image_check_arch() reads image arch property and compares its numeric
2178  * id with the requested arch. Comparison result is returned to the caller.
2179  *
2180  * returns:
2181  *     1 if image is of given arch
2182  *     0 otherwise (or on error)
2183  */
2184 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2185 {
2186         uint8_t image_arch;
2187
2188         if (fit_image_get_arch (fit, noffset, &image_arch))
2189                 return 0;
2190         return (arch == image_arch);
2191 }
2192
2193 /**
2194  * fit_image_check_type - check whether image node is of a given type
2195  * @fit: pointer to the FIT format image header
2196  * @noffset: component image node offset
2197  * @type: requested image type
2198  *
2199  * fit_image_check_type() reads image type property and compares its numeric
2200  * id with the requested type. Comparison result is returned to the caller.
2201  *
2202  * returns:
2203  *     1 if image is of given type
2204  *     0 otherwise (or on error)
2205  */
2206 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2207 {
2208         uint8_t image_type;
2209
2210         if (fit_image_get_type (fit, noffset, &image_type))
2211                 return 0;
2212         return (type == image_type);
2213 }
2214
2215 /**
2216  * fit_image_check_comp - check whether image node uses given compression
2217  * @fit: pointer to the FIT format image header
2218  * @noffset: component image node offset
2219  * @comp: requested image compression type
2220  *
2221  * fit_image_check_comp() reads image compression property and compares its
2222  * numeric id with the requested compression type. Comparison result is
2223  * returned to the caller.
2224  *
2225  * returns:
2226  *     1 if image uses requested compression
2227  *     0 otherwise (or on error)
2228  */
2229 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2230 {
2231         uint8_t image_comp;
2232
2233         if (fit_image_get_comp (fit, noffset, &image_comp))
2234                 return 0;
2235         return (comp == image_comp);
2236 }
2237
2238 /**
2239  * fit_check_format - sanity check FIT image format
2240  * @fit: pointer to the FIT format image header
2241  *
2242  * fit_check_format() runs a basic sanity FIT image verification.
2243  * Routine checks for mandatory properties, nodes, etc.
2244  *
2245  * returns:
2246  *     1, on success
2247  *     0, on failure
2248  */
2249 int fit_check_format (const void *fit)
2250 {
2251         /* mandatory / node 'description' property */
2252         if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2253                 debug ("Wrong FIT format: no description\n");
2254                 return 0;
2255         }
2256
2257 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2258         /* mandatory / node 'timestamp' property */
2259         if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2260                 debug ("Wrong FIT format: no description\n");
2261                 return 0;
2262         }
2263 #endif
2264
2265         /* mandatory subimages parent '/images' node */
2266         if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2267                 debug ("Wrong FIT format: no images parent node\n");
2268                 return 0;
2269         }
2270
2271         return 1;
2272 }
2273
2274 /**
2275  * fit_conf_get_node - get node offset for configuration of a given unit name
2276  * @fit: pointer to the FIT format image header
2277  * @conf_uname: configuration node unit name
2278  *
2279  * fit_conf_get_node() finds a configuration (withing the '/configurations'
2280  * parant node) of a provided unit name. If configuration is found its node offset
2281  * is returned to the caller.
2282  *
2283  * When NULL is provided in second argument fit_conf_get_node() will search
2284  * for a default configuration node instead. Default configuration node unit name
2285  * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2286  *
2287  * returns:
2288  *     configuration node offset when found (>=0)
2289  *     negative number on failure (FDT_ERR_* code)
2290  */
2291 int fit_conf_get_node (const void *fit, const char *conf_uname)
2292 {
2293         int noffset, confs_noffset;
2294         int len;
2295
2296         confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2297         if (confs_noffset < 0) {
2298                 debug ("Can't find configurations parent node '%s' (%s)\n",
2299                         FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2300                 return confs_noffset;
2301         }
2302
2303         if (conf_uname == NULL) {
2304                 /* get configuration unit name from the default property */
2305                 debug ("No configuration specified, trying default...\n");
2306                 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2307                 if (conf_uname == NULL) {
2308                         fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2309                         return len;
2310                 }
2311                 debug ("Found default configuration: '%s'\n", conf_uname);
2312         }
2313
2314         noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2315         if (noffset < 0) {
2316                 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2317                         conf_uname, fdt_strerror (noffset));
2318         }
2319
2320         return noffset;
2321 }
2322
2323 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2324                 const char *prop_name)
2325 {
2326         char *uname;
2327         int len;
2328
2329         /* get kernel image unit name from configuration kernel property */
2330         uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2331         if (uname == NULL)
2332                 return len;
2333
2334         return fit_image_get_node (fit, uname);
2335 }
2336
2337 /**
2338  * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2339  * a given configuration
2340  * @fit: pointer to the FIT format image header
2341  * @noffset: configuration node offset
2342  *
2343  * fit_conf_get_kernel_node() retrives kernel image node unit name from
2344  * configuration FIT_KERNEL_PROP property and translates it to the node
2345  * offset.
2346  *
2347  * returns:
2348  *     image node offset when found (>=0)
2349  *     negative number on failure (FDT_ERR_* code)
2350  */
2351 int fit_conf_get_kernel_node (const void *fit, int noffset)
2352 {
2353         return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2354 }
2355
2356 /**
2357  * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2358  * a given configuration
2359  * @fit: pointer to the FIT format image header
2360  * @noffset: configuration node offset
2361  *
2362  * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2363  * configuration FIT_KERNEL_PROP property and translates it to the node
2364  * offset.
2365  *
2366  * returns:
2367  *     image node offset when found (>=0)
2368  *     negative number on failure (FDT_ERR_* code)
2369  */
2370 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2371 {
2372         return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2373 }
2374
2375 /**
2376  * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2377  * a given configuration
2378  * @fit: pointer to the FIT format image header
2379  * @noffset: configuration node offset
2380  *
2381  * fit_conf_get_fdt_node() retrives fdt image node unit name from
2382  * configuration FIT_KERNEL_PROP property and translates it to the node
2383  * offset.
2384  *
2385  * returns:
2386  *     image node offset when found (>=0)
2387  *     negative number on failure (FDT_ERR_* code)
2388  */
2389 int fit_conf_get_fdt_node (const void *fit, int noffset)
2390 {
2391         return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2392 }
2393
2394 /**
2395  * fit_conf_print - prints out the FIT configuration details
2396  * @fit: pointer to the FIT format image header
2397  * @conf_noffset: offset of the configuration node
2398  * @p: pointer to prefix string
2399  *
2400  * fit_conf_print() lists all mandatory properies for the processed
2401  * configuration node.
2402  *
2403  * returns:
2404  *     no returned results
2405  */
2406 void fit_conf_print (const void *fit, int noffset, const char *p)
2407 {
2408         char *desc;
2409         char *uname;
2410         int ret;
2411
2412         /* Mandatory properties */
2413         ret = fit_get_desc (fit, noffset, &desc);
2414         printf ("%s  Description:  ", p);
2415         if (ret)
2416                 printf ("unavailable\n");
2417         else
2418                 printf ("%s\n", desc);
2419
2420         uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2421         printf ("%s  Kernel:       ", p);
2422         if (uname == NULL)
2423                 printf ("unavailable\n");
2424         else
2425                 printf ("%s\n", uname);
2426
2427         /* Optional properties */
2428         uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2429         if (uname)
2430                 printf ("%s  Init Ramdisk: %s\n", p, uname);
2431
2432         uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2433         if (uname)
2434                 printf ("%s  FDT:          %s\n", p, uname);
2435 }
2436
2437 /**
2438  * fit_check_ramdisk - verify FIT format ramdisk subimage
2439  * @fit_hdr: pointer to the FIT ramdisk header
2440  * @rd_noffset: ramdisk subimage node offset within FIT image
2441  * @arch: requested ramdisk image architecture type
2442  * @verify: data CRC verification flag
2443  *
2444  * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2445  * specified FIT image.
2446  *
2447  * returns:
2448  *     1, on success
2449  *     0, on failure
2450  */
2451 #ifndef USE_HOSTCC
2452 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2453 {
2454         fit_image_print (fit, rd_noffset, "   ");
2455
2456         if (verify) {
2457                 puts ("   Verifying Hash Integrity ... ");
2458                 if (!fit_image_check_hashes (fit, rd_noffset)) {
2459                         puts ("Bad Data Hash\n");
2460                         show_boot_progress (-125);
2461                         return 0;
2462                 }
2463                 puts ("OK\n");
2464         }
2465
2466         show_boot_progress (126);
2467         if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2468             !fit_image_check_arch (fit, rd_noffset, arch) ||
2469             !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2470                 printf ("No Linux %s Ramdisk Image\n",
2471                                 genimg_get_arch_name(arch));
2472                 show_boot_progress (-126);
2473                 return 0;
2474         }
2475
2476         show_boot_progress (127);
2477         return 1;
2478 }
2479 #endif /* USE_HOSTCC */
2480 #endif /* CONFIG_FIT */