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