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