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