]> git.sur5r.net Git - u-boot/blob - common/image.c
FIS: repare incorrect return value with ramdisk handling
[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 #ifndef USE_HOSTCC
27 #include <common.h>
28 #include <watchdog.h>
29
30 #ifdef CONFIG_SHOW_BOOT_PROGRESS
31 #include <status_led.h>
32 #endif
33
34 #ifdef CONFIG_HAS_DATAFLASH
35 #include <dataflash.h>
36 #endif
37
38 #ifdef CONFIG_LOGBUFFER
39 #include <logbuff.h>
40 #endif
41
42 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43 #include <rtc.h>
44 #endif
45
46 #include <image.h>
47
48 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
49 #include <fdt.h>
50 #include <libfdt.h>
51 #include <fdt_support.h>
52 #endif
53
54 #if defined(CONFIG_FIT)
55 #include <u-boot/md5.h>
56 #include <sha1.h>
57
58 static int fit_check_ramdisk (const void *fit, int os_noffset,
59                 uint8_t arch, int verify);
60 #endif
61
62 #ifdef CONFIG_CMD_BDI
63 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
64 #endif
65
66 DECLARE_GLOBAL_DATA_PTR;
67
68 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
69                                                 int verify);
70 #else
71 #include "mkimage.h"
72 #include <u-boot/md5.h>
73 #include <time.h>
74 #include <image.h>
75 #endif /* !USE_HOSTCC*/
76
77 typedef struct table_entry {
78         int     id;             /* as defined in image.h        */
79         char    *sname;         /* short (input) name           */
80         char    *lname;         /* long (output) name           */
81 } table_entry_t;
82
83 static table_entry_t uimage_arch[] = {
84         {       IH_ARCH_INVALID,        NULL,           "Invalid ARCH", },
85         {       IH_ARCH_ALPHA,          "alpha",        "Alpha",        },
86         {       IH_ARCH_ARM,            "arm",          "ARM",          },
87         {       IH_ARCH_I386,           "x86",          "Intel x86",    },
88         {       IH_ARCH_IA64,           "ia64",         "IA64",         },
89         {       IH_ARCH_M68K,           "m68k",         "M68K",         },
90         {       IH_ARCH_MICROBLAZE,     "microblaze",   "MicroBlaze",   },
91         {       IH_ARCH_MIPS,           "mips",         "MIPS",         },
92         {       IH_ARCH_MIPS64,         "mips64",       "MIPS 64 Bit",  },
93         {       IH_ARCH_NIOS,           "nios",         "NIOS",         },
94         {       IH_ARCH_NIOS2,          "nios2",        "NIOS II",      },
95         {       IH_ARCH_PPC,            "powerpc",      "PowerPC",      },
96         {       IH_ARCH_PPC,            "ppc",          "PowerPC",      },
97         {       IH_ARCH_S390,           "s390",         "IBM S390",     },
98         {       IH_ARCH_SH,             "sh",           "SuperH",       },
99         {       IH_ARCH_SPARC,          "sparc",        "SPARC",        },
100         {       IH_ARCH_SPARC64,        "sparc64",      "SPARC 64 Bit", },
101         {       IH_ARCH_BLACKFIN,       "blackfin",     "Blackfin",     },
102         {       IH_ARCH_AVR32,          "avr32",        "AVR32",        },
103         {       -1,                     "",             "",             },
104 };
105
106 static table_entry_t uimage_os[] = {
107         {       IH_OS_INVALID,  NULL,           "Invalid OS",           },
108 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
109         {       IH_OS_ARTOS,    "artos",        "ARTOS",                },
110 #endif
111         {       IH_OS_LINUX,    "linux",        "Linux",                },
112 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
113         {       IH_OS_LYNXOS,   "lynxos",       "LynxOS",               },
114 #endif
115         {       IH_OS_NETBSD,   "netbsd",       "NetBSD",               },
116         {       IH_OS_RTEMS,    "rtems",        "RTEMS",                },
117         {       IH_OS_U_BOOT,   "u-boot",       "U-Boot",               },
118 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
119         {       IH_OS_QNX,      "qnx",          "QNX",                  },
120         {       IH_OS_VXWORKS,  "vxworks",      "VxWorks",              },
121 #endif
122 #ifdef USE_HOSTCC
123         {       IH_OS_4_4BSD,   "4_4bsd",       "4_4BSD",               },
124         {       IH_OS_DELL,     "dell",         "Dell",                 },
125         {       IH_OS_ESIX,     "esix",         "Esix",                 },
126         {       IH_OS_FREEBSD,  "freebsd",      "FreeBSD",              },
127         {       IH_OS_IRIX,     "irix",         "Irix",                 },
128         {       IH_OS_NCR,      "ncr",          "NCR",                  },
129         {       IH_OS_OPENBSD,  "openbsd",      "OpenBSD",              },
130         {       IH_OS_PSOS,     "psos",         "pSOS",                 },
131         {       IH_OS_SCO,      "sco",          "SCO",                  },
132         {       IH_OS_SOLARIS,  "solaris",      "Solaris",              },
133         {       IH_OS_SVR4,     "svr4",         "SVR4",                 },
134 #endif
135         {       -1,             "",             "",                     },
136 };
137
138 static table_entry_t uimage_type[] = {
139         {       IH_TYPE_INVALID,    NULL,         "Invalid Image",      },
140         {       IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image",   },
141         {       IH_TYPE_FIRMWARE,   "firmware",   "Firmware",           },
142         {       IH_TYPE_KERNEL,     "kernel",     "Kernel Image",       },
143         {       IH_TYPE_MULTI,      "multi",      "Multi-File Image",   },
144         {       IH_TYPE_RAMDISK,    "ramdisk",    "RAMDisk Image",      },
145         {       IH_TYPE_SCRIPT,     "script",     "Script",             },
146         {       IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
147         {       IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",   },
148         {       -1,                 "",           "",                   },
149 };
150
151 static table_entry_t uimage_comp[] = {
152         {       IH_COMP_NONE,   "none",         "uncompressed",         },
153         {       IH_COMP_BZIP2,  "bzip2",        "bzip2 compressed",     },
154         {       IH_COMP_GZIP,   "gzip",         "gzip compressed",      },
155         {       -1,             "",             "",                     },
156 };
157
158 uint32_t crc32 (uint32_t, const unsigned char *, uint);
159 uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
160 static void genimg_print_size (uint32_t size);
161 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
162 static void genimg_print_time (time_t timestamp);
163 #endif
164
165 /*****************************************************************************/
166 /* Legacy format routines */
167 /*****************************************************************************/
168 int image_check_hcrc (image_header_t *hdr)
169 {
170         ulong hcrc;
171         ulong len = image_get_header_size ();
172         image_header_t header;
173
174         /* Copy header so we can blank CRC field for re-calculation */
175         memmove (&header, (char *)hdr, image_get_header_size ());
176         image_set_hcrc (&header, 0);
177
178         hcrc = crc32 (0, (unsigned char *)&header, len);
179
180         return (hcrc == image_get_hcrc (hdr));
181 }
182
183 int image_check_dcrc (image_header_t *hdr)
184 {
185         ulong data = image_get_data (hdr);
186         ulong len = image_get_data_size (hdr);
187         ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
188
189         return (dcrc == image_get_dcrc (hdr));
190 }
191
192
193 /**
194  * image_multi_count - get component (sub-image) count
195  * @hdr: pointer to the header of the multi component image
196  *
197  * image_multi_count() returns number of components in a multi
198  * component image.
199  *
200  * Note: no checking of the image type is done, caller must pass
201  * a valid multi component image.
202  *
203  * returns:
204  *     number of components
205  */
206 ulong image_multi_count (image_header_t *hdr)
207 {
208         ulong i, count = 0;
209         uint32_t *size;
210
211         /* get start of the image payload, which in case of multi
212          * component images that points to a table of component sizes */
213         size = (uint32_t *)image_get_data (hdr);
214
215         /* count non empty slots */
216         for (i = 0; size[i]; ++i)
217                 count++;
218
219         return count;
220 }
221
222 /**
223  * image_multi_getimg - get component data address and size
224  * @hdr: pointer to the header of the multi component image
225  * @idx: index of the requested component
226  * @data: pointer to a ulong variable, will hold component data address
227  * @len: pointer to a ulong variable, will hold component size
228  *
229  * image_multi_getimg() returns size and data address for the requested
230  * component in a multi component image.
231  *
232  * Note: no checking of the image type is done, caller must pass
233  * a valid multi component image.
234  *
235  * returns:
236  *     data address and size of the component, if idx is valid
237  *     0 in data and len, if idx is out of range
238  */
239 void image_multi_getimg (image_header_t *hdr, ulong idx,
240                         ulong *data, ulong *len)
241 {
242         int i;
243         uint32_t *size;
244         ulong offset, count, img_data;
245
246         /* get number of component */
247         count = image_multi_count (hdr);
248
249         /* get start of the image payload, which in case of multi
250          * component images that points to a table of component sizes */
251         size = (uint32_t *)image_get_data (hdr);
252
253         /* get address of the proper component data start, which means
254          * skipping sizes table (add 1 for last, null entry) */
255         img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
256
257         if (idx < count) {
258                 *len = uimage_to_cpu (size[idx]);
259                 offset = 0;
260
261                 /* go over all indices preceding requested component idx */
262                 for (i = 0; i < idx; i++) {
263                         /* add up i-th component size, rounding up to 4 bytes */
264                         offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
265                 }
266
267                 /* calculate idx-th component data address */
268                 *data = img_data + offset;
269         } else {
270                 *len = 0;
271                 *data = 0;
272         }
273 }
274
275 static void image_print_type (image_header_t *hdr)
276 {
277         const char *os, *arch, *type, *comp;
278
279         os = genimg_get_os_name (image_get_os (hdr));
280         arch = genimg_get_arch_name (image_get_arch (hdr));
281         type = genimg_get_type_name (image_get_type (hdr));
282         comp = genimg_get_comp_name (image_get_comp (hdr));
283
284         printf ("%s %s %s (%s)\n", arch, os, type, comp);
285 }
286
287 /**
288  * image_print_contents - prints out the contents of the legacy format image
289  * @hdr: pointer to the legacy format image header
290  * @p: pointer to prefix string
291  *
292  * image_print_contents() formats a multi line legacy image contents description.
293  * The routine prints out all header fields followed by the size/offset data
294  * for MULTI/SCRIPT images.
295  *
296  * returns:
297  *     no returned results
298  */
299 void image_print_contents (image_header_t *hdr)
300 {
301         const char *p;
302
303 #ifdef USE_HOSTCC
304         p = "";
305 #else
306         p = "   ";
307 #endif
308
309         printf ("%sImage Name:   %.*s\n", p, IH_NMLEN, image_get_name (hdr));
310 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
311         printf ("%sCreated:      ", p);
312         genimg_print_time ((time_t)image_get_time (hdr));
313 #endif
314         printf ("%sImage Type:   ", p);
315         image_print_type (hdr);
316         printf ("%sData Size:    ", p);
317         genimg_print_size (image_get_data_size (hdr));
318         printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
319         printf ("%sEntry Point:  %08x\n", p, image_get_ep (hdr));
320
321         if (image_check_type (hdr, IH_TYPE_MULTI) ||
322                         image_check_type (hdr, IH_TYPE_SCRIPT)) {
323                 int i;
324                 ulong data, len;
325                 ulong count = image_multi_count (hdr);
326
327                 printf ("%sContents:\n", p);
328                 for (i = 0; i < count; i++) {
329                         image_multi_getimg (hdr, i, &data, &len);
330
331                         printf ("%s   Image %d: ", p, i);
332                         genimg_print_size (len);
333
334                         if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
335                                 /*
336                                  * the user may need to know offsets
337                                  * if planning to do something with
338                                  * multiple files
339                                  */
340                                 printf ("%s    Offset = 0x%08lx\n", p, data);
341                         }
342                 }
343         }
344 }
345
346
347 #ifndef USE_HOSTCC
348 /**
349  * image_get_ramdisk - get and verify ramdisk image
350  * @rd_addr: ramdisk image start address
351  * @arch: expected ramdisk architecture
352  * @verify: checksum verification flag
353  *
354  * image_get_ramdisk() returns a pointer to the verified ramdisk image
355  * header. Routine receives image start address and expected architecture
356  * flag. Verification done covers data and header integrity and os/type/arch
357  * fields checking.
358  *
359  * If dataflash support is enabled routine checks for dataflash addresses
360  * and handles required dataflash reads.
361  *
362  * returns:
363  *     pointer to a ramdisk image header, if image was found and valid
364  *     otherwise, return NULL
365  */
366 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
367                                                 int verify)
368 {
369         image_header_t *rd_hdr = (image_header_t *)rd_addr;
370
371         if (!image_check_magic (rd_hdr)) {
372                 puts ("Bad Magic Number\n");
373                 show_boot_progress (-10);
374                 return NULL;
375         }
376
377         if (!image_check_hcrc (rd_hdr)) {
378                 puts ("Bad Header Checksum\n");
379                 show_boot_progress (-11);
380                 return NULL;
381         }
382
383         show_boot_progress (10);
384         image_print_contents (rd_hdr);
385
386         if (verify) {
387                 puts("   Verifying Checksum ... ");
388                 if (!image_check_dcrc (rd_hdr)) {
389                         puts ("Bad Data CRC\n");
390                         show_boot_progress (-12);
391                         return NULL;
392                 }
393                 puts("OK\n");
394         }
395
396         show_boot_progress (11);
397
398         if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
399             !image_check_arch (rd_hdr, arch) ||
400             !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
401                 printf ("No Linux %s Ramdisk Image\n",
402                                 genimg_get_arch_name(arch));
403                 show_boot_progress (-13);
404                 return NULL;
405         }
406
407         return rd_hdr;
408 }
409 #endif /* !USE_HOSTCC */
410
411 /*****************************************************************************/
412 /* Shared dual-format routines */
413 /*****************************************************************************/
414 #ifndef USE_HOSTCC
415 int getenv_yesno (char *var)
416 {
417         char *s = getenv (var);
418         return (s && (*s == 'n')) ? 0 : 1;
419 }
420
421 ulong getenv_bootm_low(void)
422 {
423         char *s = getenv ("bootm_low");
424         if (s) {
425                 ulong tmp = simple_strtoul (s, NULL, 16);
426                 return tmp;
427         }
428
429 #if defined(CFG_SDRAM_BASE)
430         return CFG_SDRAM_BASE;
431 #elif defined(CONFIG_ARM)
432         return gd->bd->bi_dram[0].start;
433 #else
434         return 0;
435 #endif
436 }
437
438 phys_size_t getenv_bootm_size(void)
439 {
440         char *s = getenv ("bootm_size");
441         if (s) {
442                 phys_size_t tmp;
443 #ifdef CFG_64BIT_STRTOUL
444                 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
445 #else
446                 tmp = (phys_size_t)simple_strtoul (s, NULL, 16);
447 #endif
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 1;
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 1;
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 1;
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 1;
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 1;
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 1;
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 1;
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 1;
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
1026 #ifdef CONFIG_LOGBUFFER
1027         /* Prevent initrd from overwriting logbuffer */
1028         lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1029 #endif
1030
1031         debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1032                         initrd_high, initrd_copy_to_ram);
1033
1034         if (rd_data) {
1035                 if (!initrd_copy_to_ram) {      /* zero-copy ramdisk support */
1036                         debug ("   in-place initrd\n");
1037                         *initrd_start = rd_data;
1038                         *initrd_end = rd_data + rd_len;
1039                         lmb_reserve(lmb, rd_data, rd_len);
1040                 } else {
1041                         if (initrd_high)
1042                                 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1043                         else
1044                                 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
1045
1046                         if (*initrd_start == 0) {
1047                                 puts ("ramdisk - allocation error\n");
1048                                 goto error;
1049                         }
1050                         show_boot_progress (12);
1051
1052                         *initrd_end = *initrd_start + rd_len;
1053                         printf ("   Loading Ramdisk to %08lx, end %08lx ... ",
1054                                         *initrd_start, *initrd_end);
1055
1056                         memmove_wd ((void *)*initrd_start,
1057                                         (void *)rd_data, rd_len, CHUNKSZ);
1058
1059                         puts ("OK\n");
1060                 }
1061         } else {
1062                 *initrd_start = 0;
1063                 *initrd_end = 0;
1064         }
1065         debug ("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1066                         *initrd_start, *initrd_end);
1067
1068         return 0;
1069
1070 error:
1071         return -1;
1072 }
1073
1074 /**
1075  * boot_get_cmdline - allocate and initialize kernel cmdline
1076  * @lmb: pointer to lmb handle, will be used for memory mgmt
1077  * @cmd_start: pointer to a ulong variable, will hold cmdline start
1078  * @cmd_end: pointer to a ulong variable, will hold cmdline end
1079  * @bootmap_base: ulong variable, holds offset in physical memory to
1080  * base of bootmap
1081  *
1082  * boot_get_cmdline() allocates space for kernel command line below
1083  * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1084  * variable is present its contents is copied to allocated kernel
1085  * command line.
1086  *
1087  * returns:
1088  *      0 - success
1089  *     -1 - failure
1090  */
1091 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1092                         ulong bootmap_base)
1093 {
1094         char *cmdline;
1095         char *s;
1096
1097         cmdline = (char *)(ulong)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1098                                          CFG_BOOTMAPSZ + bootmap_base);
1099
1100         if (cmdline == NULL)
1101                 return -1;
1102
1103         if ((s = getenv("bootargs")) == NULL)
1104                 s = "";
1105
1106         strcpy(cmdline, s);
1107
1108         *cmd_start = (ulong) & cmdline[0];
1109         *cmd_end = *cmd_start + strlen(cmdline);
1110
1111         debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1112
1113         return 0;
1114 }
1115
1116 /**
1117  * boot_get_kbd - allocate and initialize kernel copy of board info
1118  * @lmb: pointer to lmb handle, will be used for memory mgmt
1119  * @kbd: double pointer to board info data
1120  * @bootmap_base: ulong variable, holds offset in physical memory to
1121  * base of bootmap
1122  *
1123  * boot_get_kbd() allocates space for kernel copy of board info data below
1124  * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1125  * the current u-boot board info data.
1126  *
1127  * returns:
1128  *      0 - success
1129  *     -1 - failure
1130  */
1131 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1132 {
1133         *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1134                                       CFG_BOOTMAPSZ + bootmap_base);
1135         if (*kbd == NULL)
1136                 return -1;
1137
1138         **kbd = *(gd->bd);
1139
1140         debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1141
1142 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1143         do_bdinfo(NULL, 0, 0, NULL);
1144 #endif
1145
1146         return 0;
1147 }
1148 #endif /* CONFIG_PPC || CONFIG_M68K */
1149 #endif /* !USE_HOSTCC */
1150
1151 #if defined(CONFIG_FIT)
1152 /*****************************************************************************/
1153 /* New uImage format routines */
1154 /*****************************************************************************/
1155 #ifndef USE_HOSTCC
1156 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1157                 ulong *addr, const char **name)
1158 {
1159         const char *sep;
1160
1161         *addr = addr_curr;
1162         *name = NULL;
1163
1164         sep = strchr (spec, sepc);
1165         if (sep) {
1166                 if (sep - spec > 0)
1167                         *addr = simple_strtoul (spec, NULL, 16);
1168
1169                 *name = sep + 1;
1170                 return 1;
1171         }
1172
1173         return 0;
1174 }
1175
1176 /**
1177  * fit_parse_conf - parse FIT configuration spec
1178  * @spec: input string, containing configuration spec
1179  * @add_curr: current image address (to be used as a possible default)
1180  * @addr: pointer to a ulong variable, will hold FIT image address of a given
1181  * configuration
1182  * @conf_name double pointer to a char, will hold pointer to a configuration
1183  * unit name
1184  *
1185  * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1186  * where <addr> is a FIT image address that contains configuration
1187  * with a <conf> unit name.
1188  *
1189  * Address part is optional, and if omitted default add_curr will
1190  * be used instead.
1191  *
1192  * returns:
1193  *     1 if spec is a valid configuration string,
1194  *     addr and conf_name are set accordingly
1195  *     0 otherwise
1196  */
1197 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1198                 ulong *addr, const char **conf_name)
1199 {
1200         return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1201 }
1202
1203 /**
1204  * fit_parse_subimage - parse FIT subimage spec
1205  * @spec: input string, containing subimage spec
1206  * @add_curr: current image address (to be used as a possible default)
1207  * @addr: pointer to a ulong variable, will hold FIT image address of a given
1208  * subimage
1209  * @image_name: double pointer to a char, will hold pointer to a subimage name
1210  *
1211  * fit_parse_subimage() expects subimage spec in the for of
1212  * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1213  * subimage with a <subimg> unit name.
1214  *
1215  * Address part is optional, and if omitted default add_curr will
1216  * be used instead.
1217  *
1218  * returns:
1219  *     1 if spec is a valid subimage string,
1220  *     addr and image_name are set accordingly
1221  *     0 otherwise
1222  */
1223 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1224                 ulong *addr, const char **image_name)
1225 {
1226         return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1227 }
1228 #endif /* !USE_HOSTCC */
1229
1230 static void fit_get_debug (const void *fit, int noffset,
1231                 char *prop_name, int err)
1232 {
1233         debug ("Can't get '%s' property from FIT 0x%08lx, "
1234                 "node: offset %d, name %s (%s)\n",
1235                 prop_name, (ulong)fit, noffset,
1236                 fit_get_name (fit, noffset, NULL),
1237                 fdt_strerror (err));
1238 }
1239
1240 /**
1241  * fit_print_contents - prints out the contents of the FIT format image
1242  * @fit: pointer to the FIT format image header
1243  * @p: pointer to prefix string
1244  *
1245  * fit_print_contents() formats a multi line FIT image contents description.
1246  * The routine prints out FIT image properties (root node level) follwed by
1247  * the details of each component image.
1248  *
1249  * returns:
1250  *     no returned results
1251  */
1252 void fit_print_contents (const void *fit)
1253 {
1254         char *desc;
1255         char *uname;
1256         int images_noffset;
1257         int confs_noffset;
1258         int noffset;
1259         int ndepth;
1260         int count = 0;
1261         int ret;
1262         const char *p;
1263 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1264         time_t timestamp;
1265 #endif
1266
1267 #ifdef USE_HOSTCC
1268         p = "";
1269 #else
1270         p = "   ";
1271 #endif
1272
1273         /* Root node properties */
1274         ret = fit_get_desc (fit, 0, &desc);
1275         printf ("%sFIT description: ", p);
1276         if (ret)
1277                 printf ("unavailable\n");
1278         else
1279                 printf ("%s\n", desc);
1280
1281 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1282         ret = fit_get_timestamp (fit, 0, &timestamp);
1283         printf ("%sCreated:         ", p);
1284         if (ret)
1285                 printf ("unavailable\n");
1286         else
1287                 genimg_print_time (timestamp);
1288 #endif
1289
1290         /* Find images parent node offset */
1291         images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1292         if (images_noffset < 0) {
1293                 printf ("Can't find images parent node '%s' (%s)\n",
1294                         FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1295                 return;
1296         }
1297
1298         /* Process its subnodes, print out component images details */
1299         for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1300              (noffset >= 0) && (ndepth > 0);
1301              noffset = fdt_next_node (fit, noffset, &ndepth)) {
1302                 if (ndepth == 1) {
1303                         /*
1304                          * Direct child node of the images parent node,
1305                          * i.e. component image node.
1306                          */
1307                         printf ("%s Image %u (%s)\n", p, count++,
1308                                         fit_get_name(fit, noffset, NULL));
1309
1310                         fit_image_print (fit, noffset, p);
1311                 }
1312         }
1313
1314         /* Find configurations parent node offset */
1315         confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1316         if (confs_noffset < 0) {
1317                 debug ("Can't get configurations parent node '%s' (%s)\n",
1318                         FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1319                 return;
1320         }
1321
1322         /* get default configuration unit name from default property */
1323         uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1324         if (uname)
1325                 printf ("%s Default Configuration: '%s'\n", p, uname);
1326
1327         /* Process its subnodes, print out configurations details */
1328         for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1329              (noffset >= 0) && (ndepth > 0);
1330              noffset = fdt_next_node (fit, noffset, &ndepth)) {
1331                 if (ndepth == 1) {
1332                         /*
1333                          * Direct child node of the configurations parent node,
1334                          * i.e. configuration node.
1335                          */
1336                         printf ("%s Configuration %u (%s)\n", p, count++,
1337                                         fit_get_name(fit, noffset, NULL));
1338
1339                         fit_conf_print (fit, noffset, p);
1340                 }
1341         }
1342 }
1343
1344 /**
1345  * fit_image_print - prints out the FIT component image details
1346  * @fit: pointer to the FIT format image header
1347  * @image_noffset: offset of the component image node
1348  * @p: pointer to prefix string
1349  *
1350  * fit_image_print() lists all mandatory properies for the processed component
1351  * image. If present, hash nodes are printed out as well.
1352  *
1353  * returns:
1354  *     no returned results
1355  */
1356 void fit_image_print (const void *fit, int image_noffset, const char *p)
1357 {
1358         char *desc;
1359         uint8_t type, arch, os, comp;
1360         size_t size;
1361         ulong load, entry;
1362         const void *data;
1363         int noffset;
1364         int ndepth;
1365         int ret;
1366
1367         /* Mandatory properties */
1368         ret = fit_get_desc (fit, image_noffset, &desc);
1369         printf ("%s  Description:  ", p);
1370         if (ret)
1371                 printf ("unavailable\n");
1372         else
1373                 printf ("%s\n", desc);
1374
1375         fit_image_get_type (fit, image_noffset, &type);
1376         printf ("%s  Type:         %s\n", p, genimg_get_type_name (type));
1377
1378         fit_image_get_comp (fit, image_noffset, &comp);
1379         printf ("%s  Compression:  %s\n", p, genimg_get_comp_name (comp));
1380
1381         ret = fit_image_get_data (fit, image_noffset, &data, &size);
1382
1383 #ifndef USE_HOSTCC
1384         printf ("%s  Data Start:   ", p);
1385         if (ret)
1386                 printf ("unavailable\n");
1387         else
1388                 printf ("0x%08lx\n", (ulong)data);
1389 #endif
1390
1391         printf ("%s  Data Size:    ", p);
1392         if (ret)
1393                 printf ("unavailable\n");
1394         else
1395                 genimg_print_size (size);
1396
1397         /* Remaining, type dependent properties */
1398         if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1399             (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1400             (type == IH_TYPE_FLATDT)) {
1401                 fit_image_get_arch (fit, image_noffset, &arch);
1402                 printf ("%s  Architecture: %s\n", p, genimg_get_arch_name (arch));
1403         }
1404
1405         if (type == IH_TYPE_KERNEL) {
1406                 fit_image_get_os (fit, image_noffset, &os);
1407                 printf ("%s  OS:           %s\n", p, genimg_get_os_name (os));
1408         }
1409
1410         if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1411                 ret = fit_image_get_load (fit, image_noffset, &load);
1412                 printf ("%s  Load Address: ", p);
1413                 if (ret)
1414                         printf ("unavailable\n");
1415                 else
1416                         printf ("0x%08lx\n", load);
1417
1418                 fit_image_get_entry (fit, image_noffset, &entry);
1419                 printf ("%s  Entry Point:  ", p);
1420                 if (ret)
1421                         printf ("unavailable\n");
1422                 else
1423                         printf ("0x%08lx\n", entry);
1424         }
1425
1426         /* Process all hash subnodes of the component image node */
1427         for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1428              (noffset >= 0) && (ndepth > 0);
1429              noffset = fdt_next_node (fit, noffset, &ndepth)) {
1430                 if (ndepth == 1) {
1431                         /* Direct child node of the component image node */
1432                         fit_image_print_hash (fit, noffset, p);
1433                 }
1434         }
1435 }
1436
1437 /**
1438  * fit_image_print_hash - prints out the hash node details
1439  * @fit: pointer to the FIT format image header
1440  * @noffset: offset of the hash node
1441  * @p: pointer to prefix string
1442  *
1443  * fit_image_print_hash() lists properies for the processed hash node
1444  *
1445  * returns:
1446  *     no returned results
1447  */
1448 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1449 {
1450         char *algo;
1451         uint8_t *value;
1452         int value_len;
1453         int i, ret;
1454
1455         /*
1456          * Check subnode name, must be equal to "hash".
1457          * Multiple hash nodes require unique unit node
1458          * names, e.g. hash@1, hash@2, etc.
1459          */
1460         if (strncmp (fit_get_name(fit, noffset, NULL),
1461                         FIT_HASH_NODENAME,
1462                         strlen(FIT_HASH_NODENAME)) != 0)
1463                 return;
1464
1465         debug ("%s  Hash node:    '%s'\n", p,
1466                         fit_get_name (fit, noffset, NULL));
1467
1468         printf ("%s  Hash algo:    ", p);
1469         if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1470                 printf ("invalid/unsupported\n");
1471                 return;
1472         }
1473         printf ("%s\n", algo);
1474
1475         ret = fit_image_hash_get_value (fit, noffset, &value,
1476                                         &value_len);
1477         printf ("%s  Hash value:   ", p);
1478         if (ret) {
1479                 printf ("unavailable\n");
1480         } else {
1481                 for (i = 0; i < value_len; i++)
1482                         printf ("%02x", value[i]);
1483                 printf ("\n");
1484         }
1485
1486         debug  ("%s  Hash len:     %d\n", p, value_len);
1487 }
1488
1489 /**
1490  * fit_get_desc - get node description property
1491  * @fit: pointer to the FIT format image header
1492  * @noffset: node offset
1493  * @desc: double pointer to the char, will hold pointer to the descrption
1494  *
1495  * fit_get_desc() reads description property from a given node, if
1496  * description is found pointer to it is returened in third call argument.
1497  *
1498  * returns:
1499  *     0, on success
1500  *     -1, on failure
1501  */
1502 int fit_get_desc (const void *fit, int noffset, char **desc)
1503 {
1504         int len;
1505
1506         *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1507         if (*desc == NULL) {
1508                 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1509                 return -1;
1510         }
1511
1512         return 0;
1513 }
1514
1515 /**
1516  * fit_get_timestamp - get node timestamp property
1517  * @fit: pointer to the FIT format image header
1518  * @noffset: node offset
1519  * @timestamp: pointer to the time_t, will hold read timestamp
1520  *
1521  * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1522  * is found and has a correct size its value is retured in third call
1523  * argument.
1524  *
1525  * returns:
1526  *     0, on success
1527  *     -1, on property read failure
1528  *     -2, on wrong timestamp size
1529  */
1530 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1531 {
1532         int len;
1533         const void *data;
1534
1535         data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1536         if (data == NULL) {
1537                 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1538                 return -1;
1539         }
1540         if (len != sizeof (uint32_t)) {
1541                 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1542                 return -2;
1543         }
1544
1545         *timestamp = uimage_to_cpu (*((uint32_t *)data));
1546         return 0;
1547 }
1548
1549 /**
1550  * fit_image_get_node - get node offset for component image of a given unit name
1551  * @fit: pointer to the FIT format image header
1552  * @image_uname: component image node unit name
1553  *
1554  * fit_image_get_node() finds a component image (withing the '/images'
1555  * node) of a provided unit name. If image is found its node offset is
1556  * returned to the caller.
1557  *
1558  * returns:
1559  *     image node offset when found (>=0)
1560  *     negative number on failure (FDT_ERR_* code)
1561  */
1562 int fit_image_get_node (const void *fit, const char *image_uname)
1563 {
1564         int noffset, images_noffset;
1565
1566         images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1567         if (images_noffset < 0) {
1568                 debug ("Can't find images parent node '%s' (%s)\n",
1569                         FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1570                 return images_noffset;
1571         }
1572
1573         noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1574         if (noffset < 0) {
1575                 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1576                         image_uname, fdt_strerror (noffset));
1577         }
1578
1579         return noffset;
1580 }
1581
1582 /**
1583  * fit_image_get_os - get os id for a given component image node
1584  * @fit: pointer to the FIT format image header
1585  * @noffset: component image node offset
1586  * @os: pointer to the uint8_t, will hold os numeric id
1587  *
1588  * fit_image_get_os() finds os property in a given component image node.
1589  * If the property is found, its (string) value is translated to the numeric
1590  * id which is returned to the caller.
1591  *
1592  * returns:
1593  *     0, on success
1594  *     -1, on failure
1595  */
1596 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1597 {
1598         int len;
1599         const void *data;
1600
1601         /* Get OS name from property data */
1602         data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1603         if (data == NULL) {
1604                 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1605                 *os = -1;
1606                 return -1;
1607         }
1608
1609         /* Translate OS name to id */
1610         *os = genimg_get_os_id (data);
1611         return 0;
1612 }
1613
1614 /**
1615  * fit_image_get_arch - get arch id for a given component image node
1616  * @fit: pointer to the FIT format image header
1617  * @noffset: component image node offset
1618  * @arch: pointer to the uint8_t, will hold arch numeric id
1619  *
1620  * fit_image_get_arch() finds arch property in a given component image node.
1621  * If the property is found, its (string) value is translated to the numeric
1622  * id which is returned to the caller.
1623  *
1624  * returns:
1625  *     0, on success
1626  *     -1, on failure
1627  */
1628 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1629 {
1630         int len;
1631         const void *data;
1632
1633         /* Get architecture name from property data */
1634         data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1635         if (data == NULL) {
1636                 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1637                 *arch = -1;
1638                 return -1;
1639         }
1640
1641         /* Translate architecture name to id */
1642         *arch = genimg_get_arch_id (data);
1643         return 0;
1644 }
1645
1646 /**
1647  * fit_image_get_type - get type id for a given component image node
1648  * @fit: pointer to the FIT format image header
1649  * @noffset: component image node offset
1650  * @type: pointer to the uint8_t, will hold type numeric id
1651  *
1652  * fit_image_get_type() finds type property in a given component image node.
1653  * If the property is found, its (string) value is translated to the numeric
1654  * id which is returned to the caller.
1655  *
1656  * returns:
1657  *     0, on success
1658  *     -1, on failure
1659  */
1660 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1661 {
1662         int len;
1663         const void *data;
1664
1665         /* Get image type name from property data */
1666         data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1667         if (data == NULL) {
1668                 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1669                 *type = -1;
1670                 return -1;
1671         }
1672
1673         /* Translate image type name to id */
1674         *type = genimg_get_type_id (data);
1675         return 0;
1676 }
1677
1678 /**
1679  * fit_image_get_comp - get comp id for a given component image node
1680  * @fit: pointer to the FIT format image header
1681  * @noffset: component image node offset
1682  * @comp: pointer to the uint8_t, will hold comp numeric id
1683  *
1684  * fit_image_get_comp() finds comp property in a given component image node.
1685  * If the property is found, its (string) value is translated to the numeric
1686  * id which is returned to the caller.
1687  *
1688  * returns:
1689  *     0, on success
1690  *     -1, on failure
1691  */
1692 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1693 {
1694         int len;
1695         const void *data;
1696
1697         /* Get compression name from property data */
1698         data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1699         if (data == NULL) {
1700                 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1701                 *comp = -1;
1702                 return -1;
1703         }
1704
1705         /* Translate compression name to id */
1706         *comp = genimg_get_comp_id (data);
1707         return 0;
1708 }
1709
1710 /**
1711  * fit_image_get_load - get load address property for a given component image node
1712  * @fit: pointer to the FIT format image header
1713  * @noffset: component image node offset
1714  * @load: pointer to the uint32_t, will hold load address
1715  *
1716  * fit_image_get_load() finds load address property in a given component image node.
1717  * If the property is found, its value is returned to the caller.
1718  *
1719  * returns:
1720  *     0, on success
1721  *     -1, on failure
1722  */
1723 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1724 {
1725         int len;
1726         const uint32_t *data;
1727
1728         data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1729         if (data == NULL) {
1730                 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1731                 return -1;
1732         }
1733
1734         *load = uimage_to_cpu (*data);
1735         return 0;
1736 }
1737
1738 /**
1739  * fit_image_get_entry - get entry point address property for a given component image node
1740  * @fit: pointer to the FIT format image header
1741  * @noffset: component image node offset
1742  * @entry: pointer to the uint32_t, will hold entry point address
1743  *
1744  * fit_image_get_entry() finds entry point address property in a given component image node.
1745  * If the property is found, its value is returned to the caller.
1746  *
1747  * returns:
1748  *     0, on success
1749  *     -1, on failure
1750  */
1751 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1752 {
1753         int len;
1754         const uint32_t *data;
1755
1756         data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1757         if (data == NULL) {
1758                 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1759                 return -1;
1760         }
1761
1762         *entry = uimage_to_cpu (*data);
1763         return 0;
1764 }
1765
1766 /**
1767  * fit_image_get_data - get data property and its size for a given component image node
1768  * @fit: pointer to the FIT format image header
1769  * @noffset: component image node offset
1770  * @data: double pointer to void, will hold data property's data address
1771  * @size: pointer to size_t, will hold data property's data size
1772  *
1773  * fit_image_get_data() finds data property in a given component image node.
1774  * If the property is found its data start address and size are returned to
1775  * the caller.
1776  *
1777  * returns:
1778  *     0, on success
1779  *     -1, on failure
1780  */
1781 int fit_image_get_data (const void *fit, int noffset,
1782                 const void **data, size_t *size)
1783 {
1784         int len;
1785
1786         *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1787         if (*data == NULL) {
1788                 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1789                 *size = 0;
1790                 return -1;
1791         }
1792
1793         *size = len;
1794         return 0;
1795 }
1796
1797 /**
1798  * fit_image_hash_get_algo - get hash algorithm name
1799  * @fit: pointer to the FIT format image header
1800  * @noffset: hash node offset
1801  * @algo: double pointer to char, will hold pointer to the algorithm name
1802  *
1803  * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1804  * If the property is found its data start address is returned to the caller.
1805  *
1806  * returns:
1807  *     0, on success
1808  *     -1, on failure
1809  */
1810 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1811 {
1812         int len;
1813
1814         *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1815         if (*algo == NULL) {
1816                 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1817                 return -1;
1818         }
1819
1820         return 0;
1821 }
1822
1823 /**
1824  * fit_image_hash_get_value - get hash value and length
1825  * @fit: pointer to the FIT format image header
1826  * @noffset: hash node offset
1827  * @value: double pointer to uint8_t, will hold address of a hash value data
1828  * @value_len: pointer to an int, will hold hash data length
1829  *
1830  * fit_image_hash_get_value() finds hash value property in a given hash node.
1831  * If the property is found its data start address and size are returned to
1832  * the caller.
1833  *
1834  * returns:
1835  *     0, on success
1836  *     -1, on failure
1837  */
1838 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1839                                 int *value_len)
1840 {
1841         int len;
1842
1843         *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1844         if (*value == NULL) {
1845                 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1846                 *value_len = 0;
1847                 return -1;
1848         }
1849
1850         *value_len = len;
1851         return 0;
1852 }
1853
1854 /**
1855  * fit_set_timestamp - set node timestamp property
1856  * @fit: pointer to the FIT format image header
1857  * @noffset: node offset
1858  * @timestamp: timestamp value to be set
1859  *
1860  * fit_set_timestamp() attempts to set timestamp property in the requested
1861  * node and returns operation status to the caller.
1862  *
1863  * returns:
1864  *     0, on success
1865  *     -1, on property read failure
1866  */
1867 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1868 {
1869         uint32_t t;
1870         int ret;
1871
1872         t = cpu_to_uimage (timestamp);
1873         ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1874                                 sizeof (uint32_t));
1875         if (ret) {
1876                 printf ("Can't set '%s' property for '%s' node (%s)\n",
1877                         FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1878                         fdt_strerror (ret));
1879                 return -1;
1880         }
1881
1882         return 0;
1883 }
1884
1885 /**
1886  * calculate_hash - calculate and return hash for provided input data
1887  * @data: pointer to the input data
1888  * @data_len: data length
1889  * @algo: requested hash algorithm
1890  * @value: pointer to the char, will hold hash value data (caller must
1891  * allocate enough free space)
1892  * value_len: length of the calculated hash
1893  *
1894  * calculate_hash() computes input data hash according to the requested algorithm.
1895  * Resulting hash value is placed in caller provided 'value' buffer, length
1896  * of the calculated hash is returned via value_len pointer argument.
1897  *
1898  * returns:
1899  *     0, on success
1900  *    -1, when algo is unsupported
1901  */
1902 static int calculate_hash (const void *data, int data_len, const char *algo,
1903                         uint8_t *value, int *value_len)
1904 {
1905         if (strcmp (algo, "crc32") == 0 ) {
1906                 *((uint32_t *)value) = crc32_wd (0, data, data_len,
1907                                                         CHUNKSZ_CRC32);
1908                 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1909                 *value_len = 4;
1910         } else if (strcmp (algo, "sha1") == 0 ) {
1911                 sha1_csum_wd ((unsigned char *) data, data_len,
1912                                 (unsigned char *) value, CHUNKSZ_SHA1);
1913                 *value_len = 20;
1914         } else if (strcmp (algo, "md5") == 0 ) {
1915                 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1916                 *value_len = 16;
1917         } else {
1918                 debug ("Unsupported hash alogrithm\n");
1919                 return -1;
1920         }
1921         return 0;
1922 }
1923
1924 #ifdef USE_HOSTCC
1925 /**
1926  * fit_set_hashes - process FIT component image nodes and calculate hashes
1927  * @fit: pointer to the FIT format image header
1928  *
1929  * fit_set_hashes() adds hash values for all component images in the FIT blob.
1930  * Hashes are calculated for all component images which have hash subnodes
1931  * with algorithm property set to one of the supported hash algorithms.
1932  *
1933  * returns
1934  *     0, on success
1935  *     libfdt error code, on failure
1936  */
1937 int fit_set_hashes (void *fit)
1938 {
1939         int images_noffset;
1940         int noffset;
1941         int ndepth;
1942         int ret;
1943
1944         /* Find images parent node offset */
1945         images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1946         if (images_noffset < 0) {
1947                 printf ("Can't find images parent node '%s' (%s)\n",
1948                         FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1949                 return images_noffset;
1950         }
1951
1952         /* Process its subnodes, print out component images details */
1953         for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1954              (noffset >= 0) && (ndepth > 0);
1955              noffset = fdt_next_node (fit, noffset, &ndepth)) {
1956                 if (ndepth == 1) {
1957                         /*
1958                          * Direct child node of the images parent node,
1959                          * i.e. component image node.
1960                          */
1961                         ret = fit_image_set_hashes (fit, noffset);
1962                         if (ret)
1963                                 return ret;
1964                 }
1965         }
1966
1967         return 0;
1968 }
1969
1970 /**
1971  * fit_image_set_hashes - calculate/set hashes for given component image node
1972  * @fit: pointer to the FIT format image header
1973  * @image_noffset: requested component image node
1974  *
1975  * fit_image_set_hashes() adds hash values for an component image node. All
1976  * existing hash subnodes are checked, if algorithm property is set to one of
1977  * the supported hash algorithms, hash value is computed and corresponding
1978  * hash node property is set, for example:
1979  *
1980  * Input component image node structure:
1981  *
1982  * o image@1 (at image_noffset)
1983  *   | - data = [binary data]
1984  *   o hash@1
1985  *     |- algo = "sha1"
1986  *
1987  * Output component image node structure:
1988  *
1989  * o image@1 (at image_noffset)
1990  *   | - data = [binary data]
1991  *   o hash@1
1992  *     |- algo = "sha1"
1993  *     |- value = sha1(data)
1994  *
1995  * returns:
1996  *     0 on sucess
1997  *    <0 on failure
1998  */
1999 int fit_image_set_hashes (void *fit, int image_noffset)
2000 {
2001         const void *data;
2002         size_t size;
2003         char *algo;
2004         uint8_t value[FIT_MAX_HASH_LEN];
2005         int value_len;
2006         int noffset;
2007         int ndepth;
2008
2009         /* Get image data and data length */
2010         if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2011                 printf ("Can't get image data/size\n");
2012                 return -1;
2013         }
2014
2015         /* Process all hash subnodes of the component image node */
2016         for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2017              (noffset >= 0) && (ndepth > 0);
2018              noffset = fdt_next_node (fit, noffset, &ndepth)) {
2019                 if (ndepth == 1) {
2020                         /* Direct child node of the component image node */
2021
2022                         /*
2023                          * Check subnode name, must be equal to "hash".
2024                          * Multiple hash nodes require unique unit node
2025                          * names, e.g. hash@1, hash@2, etc.
2026                          */
2027                         if (strncmp (fit_get_name(fit, noffset, NULL),
2028                                                 FIT_HASH_NODENAME,
2029                                                 strlen(FIT_HASH_NODENAME)) != 0) {
2030                                 /* Not a hash subnode, skip it */
2031                                 continue;
2032                         }
2033
2034                         if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2035                                 printf ("Can't get hash algo property for "
2036                                         "'%s' hash node in '%s' image node\n",
2037                                         fit_get_name (fit, noffset, NULL),
2038                                         fit_get_name (fit, image_noffset, NULL));
2039                                 return -1;
2040                         }
2041
2042                         if (calculate_hash (data, size, algo, value, &value_len)) {
2043                                 printf ("Unsupported hash algorithm (%s) for "
2044                                         "'%s' hash node in '%s' image node\n",
2045                                         algo, fit_get_name (fit, noffset, NULL),
2046                                         fit_get_name (fit, image_noffset, NULL));
2047                                 return -1;
2048                         }
2049
2050                         if (fit_image_hash_set_value (fit, noffset, value,
2051                                                         value_len)) {
2052                                 printf ("Can't set hash value for "
2053                                         "'%s' hash node in '%s' image node\n",
2054                                         fit_get_name (fit, noffset, NULL),
2055                                         fit_get_name (fit, image_noffset, NULL));
2056                                 return -1;
2057                         }
2058                 }
2059         }
2060
2061         return 0;
2062 }
2063
2064 /**
2065  * fit_image_hash_set_value - set hash value in requested has node
2066  * @fit: pointer to the FIT format image header
2067  * @noffset: hash node offset
2068  * @value: hash value to be set
2069  * @value_len: hash value length
2070  *
2071  * fit_image_hash_set_value() attempts to set hash value in a node at offset
2072  * given and returns operation status to the caller.
2073  *
2074  * returns
2075  *     0, on success
2076  *     -1, on failure
2077  */
2078 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2079                                 int value_len)
2080 {
2081         int ret;
2082
2083         ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2084         if (ret) {
2085                 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2086                         FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2087                         fdt_strerror (ret));
2088                 return -1;
2089         }
2090
2091         return 0;
2092 }
2093 #endif /* USE_HOSTCC */
2094
2095 /**
2096  * fit_image_check_hashes - verify data intergity
2097  * @fit: pointer to the FIT format image header
2098  * @image_noffset: component image node offset
2099  *
2100  * fit_image_check_hashes() goes over component image hash nodes,
2101  * re-calculates each data hash and compares with the value stored in hash
2102  * node.
2103  *
2104  * returns:
2105  *     1, if all hashes are valid
2106  *     0, otherwise (or on error)
2107  */
2108 int fit_image_check_hashes (const void *fit, int image_noffset)
2109 {
2110         const void      *data;
2111         size_t          size;
2112         char            *algo;
2113         uint8_t         *fit_value;
2114         int             fit_value_len;
2115         uint8_t         value[FIT_MAX_HASH_LEN];
2116         int             value_len;
2117         int             noffset;
2118         int             ndepth;
2119         char            *err_msg = "";
2120
2121         /* Get image data and data length */
2122         if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2123                 printf ("Can't get image data/size\n");
2124                 return 0;
2125         }
2126
2127         /* Process all hash subnodes of the component image node */
2128         for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2129              (noffset >= 0) && (ndepth > 0);
2130              noffset = fdt_next_node (fit, noffset, &ndepth)) {
2131                 if (ndepth == 1) {
2132                         /* Direct child node of the component image node */
2133
2134                         /*
2135                          * Check subnode name, must be equal to "hash".
2136                          * Multiple hash nodes require unique unit node
2137                          * names, e.g. hash@1, hash@2, etc.
2138                          */
2139                         if (strncmp (fit_get_name(fit, noffset, NULL),
2140                                         FIT_HASH_NODENAME,
2141                                         strlen(FIT_HASH_NODENAME)) != 0)
2142                                 continue;
2143
2144                         if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2145                                 err_msg = "Can't get hash algo property";
2146                                 goto error;
2147                         }
2148                         printf ("%s", algo);
2149
2150                         if (fit_image_hash_get_value (fit, noffset, &fit_value,
2151                                                         &fit_value_len)) {
2152                                 err_msg = "Can't get hash value property";
2153                                 goto error;
2154                         }
2155
2156                         if (calculate_hash (data, size, algo, value, &value_len)) {
2157                                 err_msg = "Unsupported hash algorithm";
2158                                 goto error;
2159                         }
2160
2161                         if (value_len != fit_value_len) {
2162                                 err_msg = "Bad hash value len";
2163                                 goto error;
2164                         } else if (memcmp (value, fit_value, value_len) != 0) {
2165                                 err_msg = "Bad hash value";
2166                                 goto error;
2167                         }
2168                         printf ("+ ");
2169                 }
2170         }
2171
2172         return 1;
2173
2174 error:
2175         printf ("%s for '%s' hash node in '%s' image node\n",
2176                         err_msg, fit_get_name (fit, noffset, NULL),
2177                         fit_get_name (fit, image_noffset, NULL));
2178         return 0;
2179 }
2180
2181 /**
2182  * fit_image_check_os - check whether image node is of a given os type
2183  * @fit: pointer to the FIT format image header
2184  * @noffset: component image node offset
2185  * @os: requested image os
2186  *
2187  * fit_image_check_os() reads image os property and compares its numeric
2188  * id with the requested os. Comparison result is returned to the caller.
2189  *
2190  * returns:
2191  *     1 if image is of given os type
2192  *     0 otherwise (or on error)
2193  */
2194 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2195 {
2196         uint8_t image_os;
2197
2198         if (fit_image_get_os (fit, noffset, &image_os))
2199                 return 0;
2200         return (os == image_os);
2201 }
2202
2203 /**
2204  * fit_image_check_arch - check whether image node is of a given arch
2205  * @fit: pointer to the FIT format image header
2206  * @noffset: component image node offset
2207  * @arch: requested imagearch
2208  *
2209  * fit_image_check_arch() reads image arch property and compares its numeric
2210  * id with the requested arch. Comparison result is returned to the caller.
2211  *
2212  * returns:
2213  *     1 if image is of given arch
2214  *     0 otherwise (or on error)
2215  */
2216 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2217 {
2218         uint8_t image_arch;
2219
2220         if (fit_image_get_arch (fit, noffset, &image_arch))
2221                 return 0;
2222         return (arch == image_arch);
2223 }
2224
2225 /**
2226  * fit_image_check_type - check whether image node is of a given type
2227  * @fit: pointer to the FIT format image header
2228  * @noffset: component image node offset
2229  * @type: requested image type
2230  *
2231  * fit_image_check_type() reads image type property and compares its numeric
2232  * id with the requested type. Comparison result is returned to the caller.
2233  *
2234  * returns:
2235  *     1 if image is of given type
2236  *     0 otherwise (or on error)
2237  */
2238 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2239 {
2240         uint8_t image_type;
2241
2242         if (fit_image_get_type (fit, noffset, &image_type))
2243                 return 0;
2244         return (type == image_type);
2245 }
2246
2247 /**
2248  * fit_image_check_comp - check whether image node uses given compression
2249  * @fit: pointer to the FIT format image header
2250  * @noffset: component image node offset
2251  * @comp: requested image compression type
2252  *
2253  * fit_image_check_comp() reads image compression property and compares its
2254  * numeric id with the requested compression type. Comparison result is
2255  * returned to the caller.
2256  *
2257  * returns:
2258  *     1 if image uses requested compression
2259  *     0 otherwise (or on error)
2260  */
2261 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2262 {
2263         uint8_t image_comp;
2264
2265         if (fit_image_get_comp (fit, noffset, &image_comp))
2266                 return 0;
2267         return (comp == image_comp);
2268 }
2269
2270 /**
2271  * fit_check_format - sanity check FIT image format
2272  * @fit: pointer to the FIT format image header
2273  *
2274  * fit_check_format() runs a basic sanity FIT image verification.
2275  * Routine checks for mandatory properties, nodes, etc.
2276  *
2277  * returns:
2278  *     1, on success
2279  *     0, on failure
2280  */
2281 int fit_check_format (const void *fit)
2282 {
2283         /* mandatory / node 'description' property */
2284         if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2285                 debug ("Wrong FIT format: no description\n");
2286                 return 0;
2287         }
2288
2289 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2290         /* mandatory / node 'timestamp' property */
2291         if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2292                 debug ("Wrong FIT format: no description\n");
2293                 return 0;
2294         }
2295 #endif
2296
2297         /* mandatory subimages parent '/images' node */
2298         if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2299                 debug ("Wrong FIT format: no images parent node\n");
2300                 return 0;
2301         }
2302
2303         return 1;
2304 }
2305
2306 /**
2307  * fit_conf_get_node - get node offset for configuration of a given unit name
2308  * @fit: pointer to the FIT format image header
2309  * @conf_uname: configuration node unit name
2310  *
2311  * fit_conf_get_node() finds a configuration (withing the '/configurations'
2312  * parant node) of a provided unit name. If configuration is found its node offset
2313  * is returned to the caller.
2314  *
2315  * When NULL is provided in second argument fit_conf_get_node() will search
2316  * for a default configuration node instead. Default configuration node unit name
2317  * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2318  *
2319  * returns:
2320  *     configuration node offset when found (>=0)
2321  *     negative number on failure (FDT_ERR_* code)
2322  */
2323 int fit_conf_get_node (const void *fit, const char *conf_uname)
2324 {
2325         int noffset, confs_noffset;
2326         int len;
2327
2328         confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2329         if (confs_noffset < 0) {
2330                 debug ("Can't find configurations parent node '%s' (%s)\n",
2331                         FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2332                 return confs_noffset;
2333         }
2334
2335         if (conf_uname == NULL) {
2336                 /* get configuration unit name from the default property */
2337                 debug ("No configuration specified, trying default...\n");
2338                 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2339                 if (conf_uname == NULL) {
2340                         fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2341                         return len;
2342                 }
2343                 debug ("Found default configuration: '%s'\n", conf_uname);
2344         }
2345
2346         noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2347         if (noffset < 0) {
2348                 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2349                         conf_uname, fdt_strerror (noffset));
2350         }
2351
2352         return noffset;
2353 }
2354
2355 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2356                 const char *prop_name)
2357 {
2358         char *uname;
2359         int len;
2360
2361         /* get kernel image unit name from configuration kernel property */
2362         uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2363         if (uname == NULL)
2364                 return len;
2365
2366         return fit_image_get_node (fit, uname);
2367 }
2368
2369 /**
2370  * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2371  * a given configuration
2372  * @fit: pointer to the FIT format image header
2373  * @noffset: configuration node offset
2374  *
2375  * fit_conf_get_kernel_node() retrives kernel image node unit name from
2376  * configuration FIT_KERNEL_PROP property and translates it to the node
2377  * offset.
2378  *
2379  * returns:
2380  *     image node offset when found (>=0)
2381  *     negative number on failure (FDT_ERR_* code)
2382  */
2383 int fit_conf_get_kernel_node (const void *fit, int noffset)
2384 {
2385         return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2386 }
2387
2388 /**
2389  * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2390  * a given configuration
2391  * @fit: pointer to the FIT format image header
2392  * @noffset: configuration node offset
2393  *
2394  * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2395  * configuration FIT_KERNEL_PROP property and translates it to the node
2396  * offset.
2397  *
2398  * returns:
2399  *     image node offset when found (>=0)
2400  *     negative number on failure (FDT_ERR_* code)
2401  */
2402 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2403 {
2404         return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2405 }
2406
2407 /**
2408  * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2409  * a given configuration
2410  * @fit: pointer to the FIT format image header
2411  * @noffset: configuration node offset
2412  *
2413  * fit_conf_get_fdt_node() retrives fdt image node unit name from
2414  * configuration FIT_KERNEL_PROP property and translates it to the node
2415  * offset.
2416  *
2417  * returns:
2418  *     image node offset when found (>=0)
2419  *     negative number on failure (FDT_ERR_* code)
2420  */
2421 int fit_conf_get_fdt_node (const void *fit, int noffset)
2422 {
2423         return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2424 }
2425
2426 /**
2427  * fit_conf_print - prints out the FIT configuration details
2428  * @fit: pointer to the FIT format image header
2429  * @noffset: offset of the configuration node
2430  * @p: pointer to prefix string
2431  *
2432  * fit_conf_print() lists all mandatory properies for the processed
2433  * configuration node.
2434  *
2435  * returns:
2436  *     no returned results
2437  */
2438 void fit_conf_print (const void *fit, int noffset, const char *p)
2439 {
2440         char *desc;
2441         char *uname;
2442         int ret;
2443
2444         /* Mandatory properties */
2445         ret = fit_get_desc (fit, noffset, &desc);
2446         printf ("%s  Description:  ", p);
2447         if (ret)
2448                 printf ("unavailable\n");
2449         else
2450                 printf ("%s\n", desc);
2451
2452         uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2453         printf ("%s  Kernel:       ", p);
2454         if (uname == NULL)
2455                 printf ("unavailable\n");
2456         else
2457                 printf ("%s\n", uname);
2458
2459         /* Optional properties */
2460         uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2461         if (uname)
2462                 printf ("%s  Init Ramdisk: %s\n", p, uname);
2463
2464         uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2465         if (uname)
2466                 printf ("%s  FDT:          %s\n", p, uname);
2467 }
2468
2469 /**
2470  * fit_check_ramdisk - verify FIT format ramdisk subimage
2471  * @fit_hdr: pointer to the FIT ramdisk header
2472  * @rd_noffset: ramdisk subimage node offset within FIT image
2473  * @arch: requested ramdisk image architecture type
2474  * @verify: data CRC verification flag
2475  *
2476  * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2477  * specified FIT image.
2478  *
2479  * returns:
2480  *     1, on success
2481  *     0, on failure
2482  */
2483 #ifndef USE_HOSTCC
2484 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2485 {
2486         fit_image_print (fit, rd_noffset, "   ");
2487
2488         if (verify) {
2489                 puts ("   Verifying Hash Integrity ... ");
2490                 if (!fit_image_check_hashes (fit, rd_noffset)) {
2491                         puts ("Bad Data Hash\n");
2492                         show_boot_progress (-125);
2493                         return 0;
2494                 }
2495                 puts ("OK\n");
2496         }
2497
2498         show_boot_progress (126);
2499         if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2500             !fit_image_check_arch (fit, rd_noffset, arch) ||
2501             !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2502                 printf ("No Linux %s Ramdisk Image\n",
2503                                 genimg_get_arch_name(arch));
2504                 show_boot_progress (-126);
2505                 return 0;
2506         }
2507
2508         show_boot_progress (127);
2509         return 1;
2510 }
2511 #endif /* USE_HOSTCC */
2512 #endif /* CONFIG_FIT */