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