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