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