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