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