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