2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
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.
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.
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,
30 #ifdef CONFIG_SHOW_BOOT_PROGRESS
31 #include <status_led.h>
34 #ifdef CONFIG_HAS_DATAFLASH
35 #include <dataflash.h>
38 #ifdef CONFIG_LOGBUFFER
42 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
48 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
51 #include <fdt_support.h>
54 #if defined(CONFIG_FIT)
55 #include <u-boot/md5.h>
58 static int fit_check_ramdisk (const void *fit, int os_noffset,
59 uint8_t arch, int verify);
63 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
66 DECLARE_GLOBAL_DATA_PTR;
68 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
72 #include <u-boot/md5.h>
75 #endif /* !USE_HOSTCC*/
77 typedef struct table_entry {
78 int id; /* as defined in image.h */
79 char *sname; /* short (input) name */
80 char *lname; /* long (output) name */
83 static table_entry_t uimage_arch[] = {
84 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
85 { IH_ARCH_ALPHA, "alpha", "Alpha", },
86 { IH_ARCH_ARM, "arm", "ARM", },
87 { IH_ARCH_I386, "x86", "Intel x86", },
88 { IH_ARCH_IA64, "ia64", "IA64", },
89 { IH_ARCH_M68K, "m68k", "M68K", },
90 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
91 { IH_ARCH_MIPS, "mips", "MIPS", },
92 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
93 { IH_ARCH_NIOS, "nios", "NIOS", },
94 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
95 { IH_ARCH_PPC, "powerpc", "PowerPC", },
96 { IH_ARCH_PPC, "ppc", "PowerPC", },
97 { IH_ARCH_S390, "s390", "IBM S390", },
98 { IH_ARCH_SH, "sh", "SuperH", },
99 { IH_ARCH_SPARC, "sparc", "SPARC", },
100 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
101 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
102 { IH_ARCH_AVR32, "avr32", "AVR32", },
106 static table_entry_t uimage_os[] = {
107 { IH_OS_INVALID, NULL, "Invalid OS", },
108 { IH_OS_LINUX, "linux", "Linux", },
109 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
110 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
112 { IH_OS_NETBSD, "netbsd", "NetBSD", },
113 { IH_OS_RTEMS, "rtems", "RTEMS", },
114 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
115 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
116 { IH_OS_QNX, "qnx", "QNX", },
117 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
119 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
120 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
123 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
124 { IH_OS_DELL, "dell", "Dell", },
125 { IH_OS_ESIX, "esix", "Esix", },
126 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
127 { IH_OS_IRIX, "irix", "Irix", },
128 { IH_OS_NCR, "ncr", "NCR", },
129 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
130 { IH_OS_PSOS, "psos", "pSOS", },
131 { IH_OS_SCO, "sco", "SCO", },
132 { IH_OS_SOLARIS, "solaris", "Solaris", },
133 { IH_OS_SVR4, "svr4", "SVR4", },
138 static table_entry_t uimage_type[] = {
139 { IH_TYPE_INVALID, NULL, "Invalid Image", },
140 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
141 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
142 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
143 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
144 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
145 { IH_TYPE_SCRIPT, "script", "Script", },
146 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
147 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
151 static table_entry_t uimage_comp[] = {
152 { IH_COMP_NONE, "none", "uncompressed", },
153 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
154 { IH_COMP_GZIP, "gzip", "gzip compressed", },
155 { IH_COMP_LZMA, "lzma", "lzma compressed", },
159 uint32_t crc32 (uint32_t, const unsigned char *, uint);
160 uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
161 static void genimg_print_size (uint32_t size);
162 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
163 static void genimg_print_time (time_t timestamp);
166 /*****************************************************************************/
167 /* Legacy format routines */
168 /*****************************************************************************/
169 int image_check_hcrc (image_header_t *hdr)
172 ulong len = image_get_header_size ();
173 image_header_t header;
175 /* Copy header so we can blank CRC field for re-calculation */
176 memmove (&header, (char *)hdr, image_get_header_size ());
177 image_set_hcrc (&header, 0);
179 hcrc = crc32 (0, (unsigned char *)&header, len);
181 return (hcrc == image_get_hcrc (hdr));
184 int image_check_dcrc (image_header_t *hdr)
186 ulong data = image_get_data (hdr);
187 ulong len = image_get_data_size (hdr);
188 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
190 return (dcrc == image_get_dcrc (hdr));
194 * image_multi_count - get component (sub-image) count
195 * @hdr: pointer to the header of the multi component image
197 * image_multi_count() returns number of components in a multi
200 * Note: no checking of the image type is done, caller must pass
201 * a valid multi component image.
204 * number of components
206 ulong image_multi_count (image_header_t *hdr)
211 /* get start of the image payload, which in case of multi
212 * component images that points to a table of component sizes */
213 size = (uint32_t *)image_get_data (hdr);
215 /* count non empty slots */
216 for (i = 0; size[i]; ++i)
223 * image_multi_getimg - get component data address and size
224 * @hdr: pointer to the header of the multi component image
225 * @idx: index of the requested component
226 * @data: pointer to a ulong variable, will hold component data address
227 * @len: pointer to a ulong variable, will hold component size
229 * image_multi_getimg() returns size and data address for the requested
230 * component in a multi component image.
232 * Note: no checking of the image type is done, caller must pass
233 * a valid multi component image.
236 * data address and size of the component, if idx is valid
237 * 0 in data and len, if idx is out of range
239 void image_multi_getimg (image_header_t *hdr, ulong idx,
240 ulong *data, ulong *len)
244 ulong offset, count, img_data;
246 /* get number of component */
247 count = image_multi_count (hdr);
249 /* get start of the image payload, which in case of multi
250 * component images that points to a table of component sizes */
251 size = (uint32_t *)image_get_data (hdr);
253 /* get address of the proper component data start, which means
254 * skipping sizes table (add 1 for last, null entry) */
255 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
258 *len = uimage_to_cpu (size[idx]);
261 /* go over all indices preceding requested component idx */
262 for (i = 0; i < idx; i++) {
263 /* add up i-th component size, rounding up to 4 bytes */
264 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
267 /* calculate idx-th component data address */
268 *data = img_data + offset;
275 static void image_print_type (image_header_t *hdr)
277 const char *os, *arch, *type, *comp;
279 os = genimg_get_os_name (image_get_os (hdr));
280 arch = genimg_get_arch_name (image_get_arch (hdr));
281 type = genimg_get_type_name (image_get_type (hdr));
282 comp = genimg_get_comp_name (image_get_comp (hdr));
284 printf ("%s %s %s (%s)\n", arch, os, type, comp);
288 * image_print_contents - prints out the contents of the legacy format image
289 * @hdr: pointer to the legacy format image header
290 * @p: pointer to prefix string
292 * image_print_contents() formats a multi line legacy image contents description.
293 * The routine prints out all header fields followed by the size/offset data
294 * for MULTI/SCRIPT images.
297 * no returned results
299 void image_print_contents (image_header_t *hdr)
309 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
310 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
311 printf ("%sCreated: ", p);
312 genimg_print_time ((time_t)image_get_time (hdr));
314 printf ("%sImage Type: ", p);
315 image_print_type (hdr);
316 printf ("%sData Size: ", p);
317 genimg_print_size (image_get_data_size (hdr));
318 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
319 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
321 if (image_check_type (hdr, IH_TYPE_MULTI) ||
322 image_check_type (hdr, IH_TYPE_SCRIPT)) {
325 ulong count = image_multi_count (hdr);
327 printf ("%sContents:\n", p);
328 for (i = 0; i < count; i++) {
329 image_multi_getimg (hdr, i, &data, &len);
331 printf ("%s Image %d: ", p, i);
332 genimg_print_size (len);
334 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
336 * the user may need to know offsets
337 * if planning to do something with
340 printf ("%s Offset = 0x%08lx\n", p, data);
349 * image_get_ramdisk - get and verify ramdisk image
350 * @rd_addr: ramdisk image start address
351 * @arch: expected ramdisk architecture
352 * @verify: checksum verification flag
354 * image_get_ramdisk() returns a pointer to the verified ramdisk image
355 * header. Routine receives image start address and expected architecture
356 * flag. Verification done covers data and header integrity and os/type/arch
359 * If dataflash support is enabled routine checks for dataflash addresses
360 * and handles required dataflash reads.
363 * pointer to a ramdisk image header, if image was found and valid
364 * otherwise, return NULL
366 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
369 image_header_t *rd_hdr = (image_header_t *)rd_addr;
371 if (!image_check_magic (rd_hdr)) {
372 puts ("Bad Magic Number\n");
373 show_boot_progress (-10);
377 if (!image_check_hcrc (rd_hdr)) {
378 puts ("Bad Header Checksum\n");
379 show_boot_progress (-11);
383 show_boot_progress (10);
384 image_print_contents (rd_hdr);
387 puts(" Verifying Checksum ... ");
388 if (!image_check_dcrc (rd_hdr)) {
389 puts ("Bad Data CRC\n");
390 show_boot_progress (-12);
396 show_boot_progress (11);
398 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
399 !image_check_arch (rd_hdr, arch) ||
400 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
401 printf ("No Linux %s Ramdisk Image\n",
402 genimg_get_arch_name(arch));
403 show_boot_progress (-13);
409 #endif /* !USE_HOSTCC */
411 /*****************************************************************************/
412 /* Shared dual-format routines */
413 /*****************************************************************************/
415 int getenv_yesno (char *var)
417 char *s = getenv (var);
418 return (s && (*s == 'n')) ? 0 : 1;
421 ulong getenv_bootm_low(void)
423 char *s = getenv ("bootm_low");
425 ulong tmp = simple_strtoul (s, NULL, 16);
429 #if defined(CONFIG_SYS_SDRAM_BASE)
430 return CONFIG_SYS_SDRAM_BASE;
431 #elif defined(CONFIG_ARM)
432 return gd->bd->bi_dram[0].start;
438 phys_size_t getenv_bootm_size(void)
440 char *s = getenv ("bootm_size");
443 #ifdef CONFIG_SYS_64BIT_STRTOUL
444 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
446 tmp = (phys_size_t)simple_strtoul (s, NULL, 16);
451 #if defined(CONFIG_ARM)
452 return gd->bd->bi_dram[0].size;
454 return gd->bd->bi_memsize;
458 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
460 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
462 size_t tail = (len > chunksz) ? chunksz : len;
464 memmove (to, from, tail);
469 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
470 memmove (to, from, len);
471 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
473 #endif /* !USE_HOSTCC */
475 static void genimg_print_size (uint32_t size)
478 printf ("%d Bytes = ", size);
479 print_size (size, "\n");
481 printf ("%d Bytes = %.2f kB = %.2f MB\n",
482 size, (double)size / 1.024e3,
483 (double)size / 1.048576e6);
487 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
488 static void genimg_print_time (time_t timestamp)
493 to_tm (timestamp, &tm);
494 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
495 tm.tm_year, tm.tm_mon, tm.tm_mday,
496 tm.tm_hour, tm.tm_min, tm.tm_sec);
498 printf ("%s", ctime(×tamp));
501 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
504 * get_table_entry_name - translate entry id to long name
505 * @table: pointer to a translation table for entries of a specific type
506 * @msg: message to be returned when translation fails
507 * @id: entry id to be translated
509 * get_table_entry_name() will go over translation table trying to find
510 * entry that matches given id. If matching entry is found, its long
511 * name is returned to the caller.
514 * long entry name if translation succeeds
517 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
519 for (; table->id >= 0; ++table) {
521 return (table->lname);
526 const char *genimg_get_os_name (uint8_t os)
528 return (get_table_entry_name (uimage_os, "Unknown OS", os));
531 const char *genimg_get_arch_name (uint8_t arch)
533 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
536 const char *genimg_get_type_name (uint8_t type)
538 return (get_table_entry_name (uimage_type, "Unknown Image", type));
541 const char *genimg_get_comp_name (uint8_t comp)
543 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
547 * get_table_entry_id - translate short entry name to id
548 * @table: pointer to a translation table for entries of a specific type
549 * @table_name: to be used in case of error
550 * @name: entry short name to be translated
552 * get_table_entry_id() will go over translation table trying to find
553 * entry that matches given short name. If matching entry is found,
554 * its id returned to the caller.
557 * entry id if translation succeeds
560 static int get_table_entry_id (table_entry_t *table,
561 const char *table_name, const char *name)
567 for (t = table; t->id >= 0; ++t) {
568 if (t->sname && strcasecmp(t->sname, name) == 0)
572 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
573 for (t = table; t->id >= 0; ++t) {
574 if (t->sname == NULL)
576 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
579 fprintf (stderr, "\n");
581 for (t = table; t->id >= 0; ++t) {
582 if (t->sname && strcmp(t->sname, name) == 0)
585 debug ("Invalid %s Type: %s\n", table_name, name);
586 #endif /* USE_HOSTCC */
590 int genimg_get_os_id (const char *name)
592 return (get_table_entry_id (uimage_os, "OS", name));
595 int genimg_get_arch_id (const char *name)
597 return (get_table_entry_id (uimage_arch, "CPU", name));
600 int genimg_get_type_id (const char *name)
602 return (get_table_entry_id (uimage_type, "Image", name));
605 int genimg_get_comp_id (const char *name)
607 return (get_table_entry_id (uimage_comp, "Compression", name));
612 * genimg_get_format - get image format type
613 * @img_addr: image start address
615 * genimg_get_format() checks whether provided address points to a valid
616 * legacy or FIT image.
618 * New uImage format and FDT blob are based on a libfdt. FDT blob
619 * may be passed directly or embedded in a FIT image. In both situations
620 * genimg_get_format() must be able to dectect libfdt header.
623 * image format type or IMAGE_FORMAT_INVALID if no image is present
625 int genimg_get_format (void *img_addr)
627 ulong format = IMAGE_FORMAT_INVALID;
629 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
633 hdr = (image_header_t *)img_addr;
634 if (image_check_magic(hdr))
635 format = IMAGE_FORMAT_LEGACY;
636 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
638 fit_hdr = (char *)img_addr;
639 if (fdt_check_header (fit_hdr) == 0)
640 format = IMAGE_FORMAT_FIT;
648 * genimg_get_image - get image from special storage (if necessary)
649 * @img_addr: image start address
651 * genimg_get_image() checks if provided image start adddress is located
652 * in a dataflash storage. If so, image is moved to a system RAM memory.
655 * image start address after possible relocation from special storage
657 ulong genimg_get_image (ulong img_addr)
659 ulong ram_addr = img_addr;
661 #ifdef CONFIG_HAS_DATAFLASH
662 ulong h_size, d_size;
664 if (addr_dataflash (img_addr)){
665 /* ger RAM address */
666 ram_addr = CONFIG_SYS_LOAD_ADDR;
668 /* get header size */
669 h_size = image_get_header_size ();
670 #if defined(CONFIG_FIT)
671 if (sizeof(struct fdt_header) > h_size)
672 h_size = sizeof(struct fdt_header);
676 debug (" Reading image header from dataflash address "
677 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
679 read_dataflash (img_addr, h_size, (char *)ram_addr);
682 switch (genimg_get_format ((void *)ram_addr)) {
683 case IMAGE_FORMAT_LEGACY:
684 d_size = image_get_data_size ((image_header_t *)ram_addr);
685 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
688 #if defined(CONFIG_FIT)
689 case IMAGE_FORMAT_FIT:
690 d_size = fit_get_size ((const void *)ram_addr) - h_size;
691 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
696 printf (" No valid image found at 0x%08lx\n", img_addr);
700 /* read in image data */
701 debug (" Reading image remaining data from dataflash address "
702 "%08lx to RAM address %08lx\n", img_addr + h_size,
705 read_dataflash (img_addr + h_size, d_size,
706 (char *)(ram_addr + h_size));
709 #endif /* CONFIG_HAS_DATAFLASH */
715 * fit_has_config - check if there is a valid FIT configuration
716 * @images: pointer to the bootm command headers structure
718 * fit_has_config() checks if there is a FIT configuration in use
719 * (if FTI support is present).
722 * 0, no FIT support or no configuration found
723 * 1, configuration found
725 int genimg_has_config (bootm_headers_t *images)
727 #if defined(CONFIG_FIT)
728 if (images->fit_uname_cfg)
735 * boot_get_ramdisk - main ramdisk handling routine
736 * @argc: command argument count
737 * @argv: command argument list
738 * @images: pointer to the bootm images structure
739 * @arch: expected ramdisk architecture
740 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
741 * @rd_end: pointer to a ulong variable, will hold ramdisk end
743 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
744 * Curently supported are the following ramdisk sources:
745 * - multicomponent kernel/ramdisk image,
746 * - commandline provided address of decicated ramdisk image.
749 * 0, if ramdisk image was found and valid, or skiped
750 * rd_start and rd_end are set to ramdisk start/end addresses if
751 * ramdisk image is found and valid
753 * 1, if ramdisk image is found but corrupted, or invalid
754 * rd_start and rd_end are set to 0 if no ramdisk exists
756 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
757 uint8_t arch, ulong *rd_start, ulong *rd_end)
759 ulong rd_addr, rd_load;
760 ulong rd_data, rd_len;
761 image_header_t *rd_hdr;
762 #if defined(CONFIG_FIT)
764 const char *fit_uname_config = NULL;
765 const char *fit_uname_ramdisk = NULL;
777 * Look for a '-' which indicates to ignore the
780 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
781 debug ("## Skipping init Ramdisk\n");
782 rd_len = rd_data = 0;
783 } else if (argc >= 3 || genimg_has_config (images)) {
784 #if defined(CONFIG_FIT)
787 * If the init ramdisk comes from the FIT image and
788 * the FIT image address is omitted in the command
789 * line argument, try to use os FIT image address or
790 * default load address.
792 if (images->fit_uname_os)
793 default_addr = (ulong)images->fit_hdr_os;
795 default_addr = load_addr;
797 if (fit_parse_conf (argv[2], default_addr,
798 &rd_addr, &fit_uname_config)) {
799 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
800 fit_uname_config, rd_addr);
801 } else if (fit_parse_subimage (argv[2], default_addr,
802 &rd_addr, &fit_uname_ramdisk)) {
803 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
804 fit_uname_ramdisk, rd_addr);
808 rd_addr = simple_strtoul(argv[2], NULL, 16);
809 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
812 #if defined(CONFIG_FIT)
814 /* use FIT configuration provided in first bootm
817 rd_addr = (ulong)images->fit_hdr_os;
818 fit_uname_config = images->fit_uname_cfg;
819 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
820 fit_uname_config, rd_addr);
823 * Check whether configuration has ramdisk defined,
824 * if not, don't try to use it, quit silently.
826 fit_hdr = (void *)rd_addr;
827 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
828 if (cfg_noffset < 0) {
829 debug ("* ramdisk: no such config\n");
833 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
834 if (rd_noffset < 0) {
835 debug ("* ramdisk: no ramdisk in config\n");
841 /* copy from dataflash if needed */
842 rd_addr = genimg_get_image (rd_addr);
845 * Check if there is an initrd image at the
846 * address provided in the second bootm argument
847 * check image type, for FIT images get FIT node.
849 switch (genimg_get_format ((void *)rd_addr)) {
850 case IMAGE_FORMAT_LEGACY:
851 printf ("## Loading init Ramdisk from Legacy "
852 "Image at %08lx ...\n", rd_addr);
854 show_boot_progress (9);
855 rd_hdr = image_get_ramdisk (rd_addr, arch,
861 rd_data = image_get_data (rd_hdr);
862 rd_len = image_get_data_size (rd_hdr);
863 rd_load = image_get_load (rd_hdr);
865 #if defined(CONFIG_FIT)
866 case IMAGE_FORMAT_FIT:
867 fit_hdr = (void *)rd_addr;
868 printf ("## Loading init Ramdisk from FIT "
869 "Image at %08lx ...\n", rd_addr);
871 show_boot_progress (120);
872 if (!fit_check_format (fit_hdr)) {
873 puts ("Bad FIT ramdisk image format!\n");
874 show_boot_progress (-120);
877 show_boot_progress (121);
879 if (!fit_uname_ramdisk) {
881 * no ramdisk image node unit name, try to get config
882 * node first. If config unit node name is NULL
883 * fit_conf_get_node() will try to find default config node
885 show_boot_progress (122);
886 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
887 if (cfg_noffset < 0) {
888 puts ("Could not find configuration node\n");
889 show_boot_progress (-122);
892 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
893 printf (" Using '%s' configuration\n", fit_uname_config);
895 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
896 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
898 /* get ramdisk component image node offset */
899 show_boot_progress (123);
900 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
902 if (rd_noffset < 0) {
903 puts ("Could not find subimage node\n");
904 show_boot_progress (-124);
908 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
910 show_boot_progress (125);
911 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
914 /* get ramdisk image data address and length */
915 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
916 puts ("Could not find ramdisk subimage data!\n");
917 show_boot_progress (-127);
920 show_boot_progress (128);
922 rd_data = (ulong)data;
925 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
926 puts ("Can't get ramdisk subimage load address!\n");
927 show_boot_progress (-129);
930 show_boot_progress (129);
932 images->fit_hdr_rd = fit_hdr;
933 images->fit_uname_rd = fit_uname_ramdisk;
934 images->fit_noffset_rd = rd_noffset;
938 puts ("Wrong Ramdisk Image Format\n");
939 rd_data = rd_len = rd_load = 0;
943 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
945 * We need to copy the ramdisk to SRAM to let Linux boot
948 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
951 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
953 } else if (images->legacy_hdr_valid &&
954 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
956 * Now check if we have a legacy mult-component image,
957 * get second entry data start address and len.
959 show_boot_progress (13);
960 printf ("## Loading init Ramdisk from multi component "
961 "Legacy Image at %08lx ...\n",
962 (ulong)images->legacy_hdr_os);
964 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
969 show_boot_progress (14);
970 rd_len = rd_data = 0;
974 debug ("## No init Ramdisk\n");
977 *rd_end = rd_data + rd_len;
979 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
985 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
987 * boot_ramdisk_high - relocate init ramdisk
988 * @lmb: pointer to lmb handle, will be used for memory mgmt
989 * @rd_data: ramdisk data start address
990 * @rd_len: ramdisk data length
991 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
992 * start address (after possible relocation)
993 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
994 * end address (after possible relocation)
996 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
997 * variable and if requested ramdisk data is moved to a specified location.
999 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1000 * start/end addresses if ramdisk image start and len were provided,
1001 * otherwise set initrd_start and initrd_end set to zeros.
1007 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1008 ulong *initrd_start, ulong *initrd_end)
1012 int initrd_copy_to_ram = 1;
1014 if ((s = getenv ("initrd_high")) != NULL) {
1015 /* a value of "no" or a similar string will act like 0,
1016 * turning the "load high" feature off. This is intentional.
1018 initrd_high = simple_strtoul (s, NULL, 16);
1019 if (initrd_high == ~0)
1020 initrd_copy_to_ram = 0;
1022 /* not set, no restrictions to load high */
1027 #ifdef CONFIG_LOGBUFFER
1028 /* Prevent initrd from overwriting logbuffer */
1029 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1032 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1033 initrd_high, initrd_copy_to_ram);
1036 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1037 debug (" in-place initrd\n");
1038 *initrd_start = rd_data;
1039 *initrd_end = rd_data + rd_len;
1040 lmb_reserve(lmb, rd_data, rd_len);
1043 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1045 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
1047 if (*initrd_start == 0) {
1048 puts ("ramdisk - allocation error\n");
1051 show_boot_progress (12);
1053 *initrd_end = *initrd_start + rd_len;
1054 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1055 *initrd_start, *initrd_end);
1057 memmove_wd ((void *)*initrd_start,
1058 (void *)rd_data, rd_len, CHUNKSZ);
1066 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1067 *initrd_start, *initrd_end);
1074 #endif /* defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC) */
1076 #ifdef CONFIG_OF_LIBFDT
1077 static void fdt_error (const char *msg)
1081 puts (" - must RESET the board to recover.\n");
1084 static image_header_t *image_get_fdt (ulong fdt_addr)
1086 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
1088 image_print_contents (fdt_hdr);
1090 puts (" Verifying Checksum ... ");
1091 if (!image_check_hcrc (fdt_hdr)) {
1092 fdt_error ("fdt header checksum invalid");
1096 if (!image_check_dcrc (fdt_hdr)) {
1097 fdt_error ("fdt checksum invalid");
1102 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1103 fdt_error ("uImage is not a fdt");
1106 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1107 fdt_error ("uImage is compressed");
1110 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1111 fdt_error ("uImage data is not a fdt");
1118 * fit_check_fdt - verify FIT format FDT subimage
1119 * @fit_hdr: pointer to the FIT header
1120 * fdt_noffset: FDT subimage node offset within FIT image
1121 * @verify: data CRC verification flag
1123 * fit_check_fdt() verifies integrity of the FDT subimage and from
1124 * specified FIT image.
1130 #if defined(CONFIG_FIT)
1131 static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1133 fit_image_print (fit, fdt_noffset, " ");
1136 puts (" Verifying Hash Integrity ... ");
1137 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1138 fdt_error ("Bad Data Hash");
1144 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1145 fdt_error ("Not a FDT image");
1149 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1150 fdt_error ("FDT image is compressed");
1156 #endif /* CONFIG_FIT */
1158 #ifndef CONFIG_SYS_FDT_PAD
1159 #define CONFIG_SYS_FDT_PAD 0x3000
1163 * boot_relocate_fdt - relocate flat device tree
1164 * @lmb: pointer to lmb handle, will be used for memory mgmt
1165 * @bootmap_base: base address of the bootmap region
1166 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1167 * @of_size: pointer to a ulong variable, will hold fdt length
1169 * boot_relocate_fdt() determines if the of_flat_tree address is within
1170 * the bootmap and if not relocates it into that region
1172 * of_flat_tree and of_size are set to final (after relocation) values
1178 int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
1179 char **of_flat_tree, ulong *of_size)
1181 char *fdt_blob = *of_flat_tree;
1189 if (fdt_check_header (fdt_blob) != 0) {
1190 fdt_error ("image is not a fdt");
1194 #ifndef CONFIG_SYS_NO_FLASH
1195 /* move the blob if it is in flash (set relocate) */
1196 if (addr2info ((ulong)fdt_blob) != NULL)
1201 * The blob needs to be inside the boot mapping.
1203 if (fdt_blob < (char *)bootmap_base)
1206 if ((fdt_blob + *of_size + CONFIG_SYS_FDT_PAD) >=
1207 ((char *)CONFIG_SYS_BOOTMAPSZ + bootmap_base))
1210 /* move flattend device tree if needed */
1215 /* position on a 4K boundary before the alloc_current */
1216 /* Pad the FDT by a specified amount */
1217 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1218 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
1219 (CONFIG_SYS_BOOTMAPSZ + bootmap_base));
1221 if (of_start == 0) {
1222 puts("device tree - allocation error\n");
1226 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
1227 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
1230 printf (" Loading Device Tree to %08lx, end %08lx ... ",
1231 of_start, of_start + of_len - 1);
1233 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
1235 fdt_error ("fdt move failed");
1240 *of_flat_tree = (char *)of_start;
1243 *of_flat_tree = fdt_blob;
1244 of_len = (CONFIG_SYS_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
1245 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
1246 fdt_set_totalsize(*of_flat_tree, of_len);
1251 set_working_fdt_addr(*of_flat_tree);
1259 * boot_get_fdt - main fdt handling routine
1260 * @argc: command argument count
1261 * @argv: command argument list
1262 * @images: pointer to the bootm images structure
1263 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1264 * @of_size: pointer to a ulong variable, will hold fdt length
1266 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1267 * Curently supported are the following ramdisk sources:
1268 * - multicomponent kernel/ramdisk image,
1269 * - commandline provided address of decicated ramdisk image.
1272 * 0, if fdt image was found and valid, or skipped
1273 * of_flat_tree and of_size are set to fdt start address and length if
1274 * fdt image is found and valid
1276 * 1, if fdt image is found but corrupted
1277 * of_flat_tree and of_size are set to 0 if no fdt exists
1279 int boot_get_fdt (int flag, int argc, char *argv[], bootm_headers_t *images,
1280 char **of_flat_tree, ulong *of_size)
1283 image_header_t *fdt_hdr;
1284 char *fdt_blob = NULL;
1285 ulong image_start, image_end;
1286 ulong load_start, load_end;
1287 #if defined(CONFIG_FIT)
1289 const char *fit_uname_config = NULL;
1290 const char *fit_uname_fdt = NULL;
1298 *of_flat_tree = NULL;
1301 if (argc > 3 || genimg_has_config (images)) {
1302 #if defined(CONFIG_FIT)
1305 * If the FDT blob comes from the FIT image and the
1306 * FIT image address is omitted in the command line
1307 * argument, try to use ramdisk or os FIT image
1308 * address or default load address.
1310 if (images->fit_uname_rd)
1311 default_addr = (ulong)images->fit_hdr_rd;
1312 else if (images->fit_uname_os)
1313 default_addr = (ulong)images->fit_hdr_os;
1315 default_addr = load_addr;
1317 if (fit_parse_conf (argv[3], default_addr,
1318 &fdt_addr, &fit_uname_config)) {
1319 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1320 fit_uname_config, fdt_addr);
1321 } else if (fit_parse_subimage (argv[3], default_addr,
1322 &fdt_addr, &fit_uname_fdt)) {
1323 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1324 fit_uname_fdt, fdt_addr);
1328 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1329 debug ("* fdt: cmdline image address = 0x%08lx\n",
1332 #if defined(CONFIG_FIT)
1334 /* use FIT configuration provided in first bootm
1337 fdt_addr = (ulong)images->fit_hdr_os;
1338 fit_uname_config = images->fit_uname_cfg;
1339 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1340 fit_uname_config, fdt_addr);
1343 * Check whether configuration has FDT blob defined,
1344 * if not quit silently.
1346 fit_hdr = (void *)fdt_addr;
1347 cfg_noffset = fit_conf_get_node (fit_hdr,
1349 if (cfg_noffset < 0) {
1350 debug ("* fdt: no such config\n");
1354 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1356 if (fdt_noffset < 0) {
1357 debug ("* fdt: no fdt in config\n");
1363 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1366 /* copy from dataflash if needed */
1367 fdt_addr = genimg_get_image (fdt_addr);
1370 * Check if there is an FDT image at the
1371 * address provided in the second bootm argument
1372 * check image type, for FIT images get a FIT node.
1374 switch (genimg_get_format ((void *)fdt_addr)) {
1375 case IMAGE_FORMAT_LEGACY:
1376 /* verify fdt_addr points to a valid image header */
1377 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1379 fdt_hdr = image_get_fdt (fdt_addr);
1384 * move image data to the load address,
1385 * make sure we don't overwrite initial image
1387 image_start = (ulong)fdt_hdr;
1388 image_end = image_get_image_end (fdt_hdr);
1390 load_start = image_get_load (fdt_hdr);
1391 load_end = load_start + image_get_data_size (fdt_hdr);
1393 if ((load_start < image_end) && (load_end > image_start)) {
1394 fdt_error ("fdt overwritten");
1398 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1399 image_get_data (fdt_hdr), load_start);
1401 memmove ((void *)load_start,
1402 (void *)image_get_data (fdt_hdr),
1403 image_get_data_size (fdt_hdr));
1405 fdt_blob = (char *)load_start;
1407 case IMAGE_FORMAT_FIT:
1409 * This case will catch both: new uImage format
1410 * (libfdt based) and raw FDT blob (also libfdt
1413 #if defined(CONFIG_FIT)
1414 /* check FDT blob vs FIT blob */
1415 if (fit_check_format ((const void *)fdt_addr)) {
1419 fit_hdr = (void *)fdt_addr;
1420 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1423 if (!fit_uname_fdt) {
1425 * no FDT blob image node unit name,
1426 * try to get config node first. If
1427 * config unit node name is NULL
1428 * fit_conf_get_node() will try to
1429 * find default config node
1431 cfg_noffset = fit_conf_get_node (fit_hdr,
1434 if (cfg_noffset < 0) {
1435 fdt_error ("Could not find configuration node\n");
1439 fit_uname_config = fdt_get_name (fit_hdr,
1441 printf (" Using '%s' configuration\n",
1444 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1446 fit_uname_fdt = fit_get_name (fit_hdr,
1449 /* get FDT component image node offset */
1450 fdt_noffset = fit_image_get_node (fit_hdr,
1453 if (fdt_noffset < 0) {
1454 fdt_error ("Could not find subimage node\n");
1458 printf (" Trying '%s' FDT blob subimage\n",
1461 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1465 /* get ramdisk image data address and length */
1466 if (fit_image_get_data (fit_hdr, fdt_noffset,
1468 fdt_error ("Could not find FDT subimage data");
1472 /* verift that image data is a proper FDT blob */
1473 if (fdt_check_header ((char *)data) != 0) {
1474 fdt_error ("Subimage data is not a FTD");
1479 * move image data to the load address,
1480 * make sure we don't overwrite initial image
1482 image_start = (ulong)fit_hdr;
1483 image_end = fit_get_end (fit_hdr);
1485 if (fit_image_get_load (fit_hdr, fdt_noffset,
1486 &load_start) == 0) {
1487 load_end = load_start + size;
1489 if ((load_start < image_end) &&
1490 (load_end > image_start)) {
1491 fdt_error ("FDT overwritten");
1495 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1496 (ulong)data, load_start);
1498 memmove ((void *)load_start,
1499 (void *)data, size);
1501 fdt_blob = (char *)load_start;
1503 fdt_blob = (char *)data;
1506 images->fit_hdr_fdt = fit_hdr;
1507 images->fit_uname_fdt = fit_uname_fdt;
1508 images->fit_noffset_fdt = fdt_noffset;
1516 fdt_blob = (char *)fdt_addr;
1517 debug ("* fdt: raw FDT blob\n");
1518 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1522 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
1526 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1528 } else if (images->legacy_hdr_valid &&
1529 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1531 ulong fdt_data, fdt_len;
1534 * Now check if we have a legacy multi-component image,
1535 * get second entry data start address and len.
1537 printf ("## Flattened Device Tree from multi "
1538 "component Image at %08lX\n",
1539 (ulong)images->legacy_hdr_os);
1541 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1544 fdt_blob = (char *)fdt_data;
1545 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1547 if (fdt_check_header (fdt_blob) != 0) {
1548 fdt_error ("image is not a fdt");
1552 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
1553 fdt_error ("fdt size != image size");
1557 debug ("## No Flattened Device Tree\n");
1561 debug ("## No Flattened Device Tree\n");
1565 *of_flat_tree = fdt_blob;
1566 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
1567 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
1568 (ulong)*of_flat_tree, *of_size);
1577 #endif /* CONFIG_OF_LIBFDT */
1579 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
1581 * boot_get_cmdline - allocate and initialize kernel cmdline
1582 * @lmb: pointer to lmb handle, will be used for memory mgmt
1583 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1584 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1585 * @bootmap_base: ulong variable, holds offset in physical memory to
1588 * boot_get_cmdline() allocates space for kernel command line below
1589 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1590 * variable is present its contents is copied to allocated kernel
1597 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1603 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1604 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
1606 if (cmdline == NULL)
1609 if ((s = getenv("bootargs")) == NULL)
1614 *cmd_start = (ulong) & cmdline[0];
1615 *cmd_end = *cmd_start + strlen(cmdline);
1617 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1623 * boot_get_kbd - allocate and initialize kernel copy of board info
1624 * @lmb: pointer to lmb handle, will be used for memory mgmt
1625 * @kbd: double pointer to board info data
1626 * @bootmap_base: ulong variable, holds offset in physical memory to
1629 * boot_get_kbd() allocates space for kernel copy of board info data below
1630 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1631 * the current u-boot board info data.
1637 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1639 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1640 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
1646 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1648 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1649 do_bdinfo(NULL, 0, 0, NULL);
1654 #endif /* CONFIG_PPC || CONFIG_M68K */
1655 #endif /* !USE_HOSTCC */
1657 #if defined(CONFIG_FIT)
1658 /*****************************************************************************/
1659 /* New uImage format routines */
1660 /*****************************************************************************/
1662 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1663 ulong *addr, const char **name)
1670 sep = strchr (spec, sepc);
1673 *addr = simple_strtoul (spec, NULL, 16);
1683 * fit_parse_conf - parse FIT configuration spec
1684 * @spec: input string, containing configuration spec
1685 * @add_curr: current image address (to be used as a possible default)
1686 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1688 * @conf_name double pointer to a char, will hold pointer to a configuration
1691 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1692 * where <addr> is a FIT image address that contains configuration
1693 * with a <conf> unit name.
1695 * Address part is optional, and if omitted default add_curr will
1699 * 1 if spec is a valid configuration string,
1700 * addr and conf_name are set accordingly
1703 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1704 ulong *addr, const char **conf_name)
1706 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1710 * fit_parse_subimage - parse FIT subimage spec
1711 * @spec: input string, containing subimage spec
1712 * @add_curr: current image address (to be used as a possible default)
1713 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1715 * @image_name: double pointer to a char, will hold pointer to a subimage name
1717 * fit_parse_subimage() expects subimage spec in the for of
1718 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1719 * subimage with a <subimg> unit name.
1721 * Address part is optional, and if omitted default add_curr will
1725 * 1 if spec is a valid subimage string,
1726 * addr and image_name are set accordingly
1729 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1730 ulong *addr, const char **image_name)
1732 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1734 #endif /* !USE_HOSTCC */
1736 static void fit_get_debug (const void *fit, int noffset,
1737 char *prop_name, int err)
1739 debug ("Can't get '%s' property from FIT 0x%08lx, "
1740 "node: offset %d, name %s (%s)\n",
1741 prop_name, (ulong)fit, noffset,
1742 fit_get_name (fit, noffset, NULL),
1743 fdt_strerror (err));
1747 * fit_print_contents - prints out the contents of the FIT format image
1748 * @fit: pointer to the FIT format image header
1749 * @p: pointer to prefix string
1751 * fit_print_contents() formats a multi line FIT image contents description.
1752 * The routine prints out FIT image properties (root node level) follwed by
1753 * the details of each component image.
1756 * no returned results
1758 void fit_print_contents (const void *fit)
1769 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1779 /* Root node properties */
1780 ret = fit_get_desc (fit, 0, &desc);
1781 printf ("%sFIT description: ", p);
1783 printf ("unavailable\n");
1785 printf ("%s\n", desc);
1787 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1788 ret = fit_get_timestamp (fit, 0, ×tamp);
1789 printf ("%sCreated: ", p);
1791 printf ("unavailable\n");
1793 genimg_print_time (timestamp);
1796 /* Find images parent node offset */
1797 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1798 if (images_noffset < 0) {
1799 printf ("Can't find images parent node '%s' (%s)\n",
1800 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1804 /* Process its subnodes, print out component images details */
1805 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1806 (noffset >= 0) && (ndepth > 0);
1807 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1810 * Direct child node of the images parent node,
1811 * i.e. component image node.
1813 printf ("%s Image %u (%s)\n", p, count++,
1814 fit_get_name(fit, noffset, NULL));
1816 fit_image_print (fit, noffset, p);
1820 /* Find configurations parent node offset */
1821 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1822 if (confs_noffset < 0) {
1823 debug ("Can't get configurations parent node '%s' (%s)\n",
1824 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1828 /* get default configuration unit name from default property */
1829 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1831 printf ("%s Default Configuration: '%s'\n", p, uname);
1833 /* Process its subnodes, print out configurations details */
1834 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1835 (noffset >= 0) && (ndepth > 0);
1836 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1839 * Direct child node of the configurations parent node,
1840 * i.e. configuration node.
1842 printf ("%s Configuration %u (%s)\n", p, count++,
1843 fit_get_name(fit, noffset, NULL));
1845 fit_conf_print (fit, noffset, p);
1851 * fit_image_print - prints out the FIT component image details
1852 * @fit: pointer to the FIT format image header
1853 * @image_noffset: offset of the component image node
1854 * @p: pointer to prefix string
1856 * fit_image_print() lists all mandatory properies for the processed component
1857 * image. If present, hash nodes are printed out as well. Load
1858 * address for images of type firmware is also printed out. Since the load
1859 * address is not mandatory for firmware images, it will be output as
1860 * "unavailable" when not present.
1863 * no returned results
1865 void fit_image_print (const void *fit, int image_noffset, const char *p)
1868 uint8_t type, arch, os, comp;
1876 /* Mandatory properties */
1877 ret = fit_get_desc (fit, image_noffset, &desc);
1878 printf ("%s Description: ", p);
1880 printf ("unavailable\n");
1882 printf ("%s\n", desc);
1884 fit_image_get_type (fit, image_noffset, &type);
1885 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1887 fit_image_get_comp (fit, image_noffset, &comp);
1888 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1890 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1893 printf ("%s Data Start: ", p);
1895 printf ("unavailable\n");
1897 printf ("0x%08lx\n", (ulong)data);
1900 printf ("%s Data Size: ", p);
1902 printf ("unavailable\n");
1904 genimg_print_size (size);
1906 /* Remaining, type dependent properties */
1907 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1908 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1909 (type == IH_TYPE_FLATDT)) {
1910 fit_image_get_arch (fit, image_noffset, &arch);
1911 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1914 if (type == IH_TYPE_KERNEL) {
1915 fit_image_get_os (fit, image_noffset, &os);
1916 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1919 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1920 (type == IH_TYPE_FIRMWARE)) {
1921 ret = fit_image_get_load (fit, image_noffset, &load);
1922 printf ("%s Load Address: ", p);
1924 printf ("unavailable\n");
1926 printf ("0x%08lx\n", load);
1929 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1930 fit_image_get_entry (fit, image_noffset, &entry);
1931 printf ("%s Entry Point: ", p);
1933 printf ("unavailable\n");
1935 printf ("0x%08lx\n", entry);
1938 /* Process all hash subnodes of the component image node */
1939 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1940 (noffset >= 0) && (ndepth > 0);
1941 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1943 /* Direct child node of the component image node */
1944 fit_image_print_hash (fit, noffset, p);
1950 * fit_image_print_hash - prints out the hash node details
1951 * @fit: pointer to the FIT format image header
1952 * @noffset: offset of the hash node
1953 * @p: pointer to prefix string
1955 * fit_image_print_hash() lists properies for the processed hash node
1958 * no returned results
1960 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1968 * Check subnode name, must be equal to "hash".
1969 * Multiple hash nodes require unique unit node
1970 * names, e.g. hash@1, hash@2, etc.
1972 if (strncmp (fit_get_name(fit, noffset, NULL),
1974 strlen(FIT_HASH_NODENAME)) != 0)
1977 debug ("%s Hash node: '%s'\n", p,
1978 fit_get_name (fit, noffset, NULL));
1980 printf ("%s Hash algo: ", p);
1981 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1982 printf ("invalid/unsupported\n");
1985 printf ("%s\n", algo);
1987 ret = fit_image_hash_get_value (fit, noffset, &value,
1989 printf ("%s Hash value: ", p);
1991 printf ("unavailable\n");
1993 for (i = 0; i < value_len; i++)
1994 printf ("%02x", value[i]);
1998 debug ("%s Hash len: %d\n", p, value_len);
2002 * fit_get_desc - get node description property
2003 * @fit: pointer to the FIT format image header
2004 * @noffset: node offset
2005 * @desc: double pointer to the char, will hold pointer to the descrption
2007 * fit_get_desc() reads description property from a given node, if
2008 * description is found pointer to it is returened in third call argument.
2014 int fit_get_desc (const void *fit, int noffset, char **desc)
2018 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2019 if (*desc == NULL) {
2020 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2028 * fit_get_timestamp - get node timestamp property
2029 * @fit: pointer to the FIT format image header
2030 * @noffset: node offset
2031 * @timestamp: pointer to the time_t, will hold read timestamp
2033 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2034 * is found and has a correct size its value is retured in third call
2039 * -1, on property read failure
2040 * -2, on wrong timestamp size
2042 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2047 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2049 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2052 if (len != sizeof (uint32_t)) {
2053 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2057 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2062 * fit_image_get_node - get node offset for component image of a given unit name
2063 * @fit: pointer to the FIT format image header
2064 * @image_uname: component image node unit name
2066 * fit_image_get_node() finds a component image (withing the '/images'
2067 * node) of a provided unit name. If image is found its node offset is
2068 * returned to the caller.
2071 * image node offset when found (>=0)
2072 * negative number on failure (FDT_ERR_* code)
2074 int fit_image_get_node (const void *fit, const char *image_uname)
2076 int noffset, images_noffset;
2078 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2079 if (images_noffset < 0) {
2080 debug ("Can't find images parent node '%s' (%s)\n",
2081 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2082 return images_noffset;
2085 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2087 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2088 image_uname, fdt_strerror (noffset));
2095 * fit_image_get_os - get os id for a given component image node
2096 * @fit: pointer to the FIT format image header
2097 * @noffset: component image node offset
2098 * @os: pointer to the uint8_t, will hold os numeric id
2100 * fit_image_get_os() finds os property in a given component image node.
2101 * If the property is found, its (string) value is translated to the numeric
2102 * id which is returned to the caller.
2108 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2113 /* Get OS name from property data */
2114 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2116 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2121 /* Translate OS name to id */
2122 *os = genimg_get_os_id (data);
2127 * fit_image_get_arch - get arch id for a given component image node
2128 * @fit: pointer to the FIT format image header
2129 * @noffset: component image node offset
2130 * @arch: pointer to the uint8_t, will hold arch numeric id
2132 * fit_image_get_arch() finds arch property in a given component image node.
2133 * If the property is found, its (string) value is translated to the numeric
2134 * id which is returned to the caller.
2140 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2145 /* Get architecture name from property data */
2146 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2148 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2153 /* Translate architecture name to id */
2154 *arch = genimg_get_arch_id (data);
2159 * fit_image_get_type - get type id for a given component image node
2160 * @fit: pointer to the FIT format image header
2161 * @noffset: component image node offset
2162 * @type: pointer to the uint8_t, will hold type numeric id
2164 * fit_image_get_type() finds type property in a given component image node.
2165 * If the property is found, its (string) value is translated to the numeric
2166 * id which is returned to the caller.
2172 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2177 /* Get image type name from property data */
2178 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2180 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2185 /* Translate image type name to id */
2186 *type = genimg_get_type_id (data);
2191 * fit_image_get_comp - get comp id for a given component image node
2192 * @fit: pointer to the FIT format image header
2193 * @noffset: component image node offset
2194 * @comp: pointer to the uint8_t, will hold comp numeric id
2196 * fit_image_get_comp() finds comp property in a given component image node.
2197 * If the property is found, its (string) value is translated to the numeric
2198 * id which is returned to the caller.
2204 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2209 /* Get compression name from property data */
2210 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2212 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2217 /* Translate compression name to id */
2218 *comp = genimg_get_comp_id (data);
2223 * fit_image_get_load - get load address property for a given component image node
2224 * @fit: pointer to the FIT format image header
2225 * @noffset: component image node offset
2226 * @load: pointer to the uint32_t, will hold load address
2228 * fit_image_get_load() finds load address property in a given component image node.
2229 * If the property is found, its value is returned to the caller.
2235 int fit_image_get_load (const void *fit, int noffset, ulong *load)
2238 const uint32_t *data;
2240 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2242 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2246 *load = uimage_to_cpu (*data);
2251 * fit_image_get_entry - get entry point address property for a given component image node
2252 * @fit: pointer to the FIT format image header
2253 * @noffset: component image node offset
2254 * @entry: pointer to the uint32_t, will hold entry point address
2256 * fit_image_get_entry() finds entry point address property in a given component image node.
2257 * If the property is found, its value is returned to the caller.
2263 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2266 const uint32_t *data;
2268 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2270 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2274 *entry = uimage_to_cpu (*data);
2279 * fit_image_get_data - get data property and its size for a given component image node
2280 * @fit: pointer to the FIT format image header
2281 * @noffset: component image node offset
2282 * @data: double pointer to void, will hold data property's data address
2283 * @size: pointer to size_t, will hold data property's data size
2285 * fit_image_get_data() finds data property in a given component image node.
2286 * If the property is found its data start address and size are returned to
2293 int fit_image_get_data (const void *fit, int noffset,
2294 const void **data, size_t *size)
2298 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2299 if (*data == NULL) {
2300 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2310 * fit_image_hash_get_algo - get hash algorithm name
2311 * @fit: pointer to the FIT format image header
2312 * @noffset: hash node offset
2313 * @algo: double pointer to char, will hold pointer to the algorithm name
2315 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2316 * If the property is found its data start address is returned to the caller.
2322 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2326 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2327 if (*algo == NULL) {
2328 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2336 * fit_image_hash_get_value - get hash value and length
2337 * @fit: pointer to the FIT format image header
2338 * @noffset: hash node offset
2339 * @value: double pointer to uint8_t, will hold address of a hash value data
2340 * @value_len: pointer to an int, will hold hash data length
2342 * fit_image_hash_get_value() finds hash value property in a given hash node.
2343 * If the property is found its data start address and size are returned to
2350 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2355 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2356 if (*value == NULL) {
2357 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2367 * fit_set_timestamp - set node timestamp property
2368 * @fit: pointer to the FIT format image header
2369 * @noffset: node offset
2370 * @timestamp: timestamp value to be set
2372 * fit_set_timestamp() attempts to set timestamp property in the requested
2373 * node and returns operation status to the caller.
2377 * -1, on property read failure
2379 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2384 t = cpu_to_uimage (timestamp);
2385 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2388 printf ("Can't set '%s' property for '%s' node (%s)\n",
2389 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2390 fdt_strerror (ret));
2398 * calculate_hash - calculate and return hash for provided input data
2399 * @data: pointer to the input data
2400 * @data_len: data length
2401 * @algo: requested hash algorithm
2402 * @value: pointer to the char, will hold hash value data (caller must
2403 * allocate enough free space)
2404 * value_len: length of the calculated hash
2406 * calculate_hash() computes input data hash according to the requested algorithm.
2407 * Resulting hash value is placed in caller provided 'value' buffer, length
2408 * of the calculated hash is returned via value_len pointer argument.
2412 * -1, when algo is unsupported
2414 static int calculate_hash (const void *data, int data_len, const char *algo,
2415 uint8_t *value, int *value_len)
2417 if (strcmp (algo, "crc32") == 0 ) {
2418 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2420 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2422 } else if (strcmp (algo, "sha1") == 0 ) {
2423 sha1_csum_wd ((unsigned char *) data, data_len,
2424 (unsigned char *) value, CHUNKSZ_SHA1);
2426 } else if (strcmp (algo, "md5") == 0 ) {
2427 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2430 debug ("Unsupported hash alogrithm\n");
2438 * fit_set_hashes - process FIT component image nodes and calculate hashes
2439 * @fit: pointer to the FIT format image header
2441 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2442 * Hashes are calculated for all component images which have hash subnodes
2443 * with algorithm property set to one of the supported hash algorithms.
2447 * libfdt error code, on failure
2449 int fit_set_hashes (void *fit)
2456 /* Find images parent node offset */
2457 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2458 if (images_noffset < 0) {
2459 printf ("Can't find images parent node '%s' (%s)\n",
2460 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2461 return images_noffset;
2464 /* Process its subnodes, print out component images details */
2465 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2466 (noffset >= 0) && (ndepth > 0);
2467 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2470 * Direct child node of the images parent node,
2471 * i.e. component image node.
2473 ret = fit_image_set_hashes (fit, noffset);
2483 * fit_image_set_hashes - calculate/set hashes for given component image node
2484 * @fit: pointer to the FIT format image header
2485 * @image_noffset: requested component image node
2487 * fit_image_set_hashes() adds hash values for an component image node. All
2488 * existing hash subnodes are checked, if algorithm property is set to one of
2489 * the supported hash algorithms, hash value is computed and corresponding
2490 * hash node property is set, for example:
2492 * Input component image node structure:
2494 * o image@1 (at image_noffset)
2495 * | - data = [binary data]
2499 * Output component image node structure:
2501 * o image@1 (at image_noffset)
2502 * | - data = [binary data]
2505 * |- value = sha1(data)
2511 int fit_image_set_hashes (void *fit, int image_noffset)
2516 uint8_t value[FIT_MAX_HASH_LEN];
2521 /* Get image data and data length */
2522 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2523 printf ("Can't get image data/size\n");
2527 /* Process all hash subnodes of the component image node */
2528 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2529 (noffset >= 0) && (ndepth > 0);
2530 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2532 /* Direct child node of the component image node */
2535 * Check subnode name, must be equal to "hash".
2536 * Multiple hash nodes require unique unit node
2537 * names, e.g. hash@1, hash@2, etc.
2539 if (strncmp (fit_get_name(fit, noffset, NULL),
2541 strlen(FIT_HASH_NODENAME)) != 0) {
2542 /* Not a hash subnode, skip it */
2546 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2547 printf ("Can't get hash algo property for "
2548 "'%s' hash node in '%s' image node\n",
2549 fit_get_name (fit, noffset, NULL),
2550 fit_get_name (fit, image_noffset, NULL));
2554 if (calculate_hash (data, size, algo, value, &value_len)) {
2555 printf ("Unsupported hash algorithm (%s) for "
2556 "'%s' hash node in '%s' image node\n",
2557 algo, fit_get_name (fit, noffset, NULL),
2558 fit_get_name (fit, image_noffset, NULL));
2562 if (fit_image_hash_set_value (fit, noffset, value,
2564 printf ("Can't set hash value for "
2565 "'%s' hash node in '%s' image node\n",
2566 fit_get_name (fit, noffset, NULL),
2567 fit_get_name (fit, image_noffset, NULL));
2577 * fit_image_hash_set_value - set hash value in requested has node
2578 * @fit: pointer to the FIT format image header
2579 * @noffset: hash node offset
2580 * @value: hash value to be set
2581 * @value_len: hash value length
2583 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2584 * given and returns operation status to the caller.
2590 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2595 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2597 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2598 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2599 fdt_strerror (ret));
2605 #endif /* USE_HOSTCC */
2608 * fit_image_check_hashes - verify data intergity
2609 * @fit: pointer to the FIT format image header
2610 * @image_noffset: component image node offset
2612 * fit_image_check_hashes() goes over component image hash nodes,
2613 * re-calculates each data hash and compares with the value stored in hash
2617 * 1, if all hashes are valid
2618 * 0, otherwise (or on error)
2620 int fit_image_check_hashes (const void *fit, int image_noffset)
2627 uint8_t value[FIT_MAX_HASH_LEN];
2633 /* Get image data and data length */
2634 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2635 printf ("Can't get image data/size\n");
2639 /* Process all hash subnodes of the component image node */
2640 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2641 (noffset >= 0) && (ndepth > 0);
2642 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2644 /* Direct child node of the component image node */
2647 * Check subnode name, must be equal to "hash".
2648 * Multiple hash nodes require unique unit node
2649 * names, e.g. hash@1, hash@2, etc.
2651 if (strncmp (fit_get_name(fit, noffset, NULL),
2653 strlen(FIT_HASH_NODENAME)) != 0)
2656 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2657 err_msg = " error!\nCan't get hash algo "
2661 printf ("%s", algo);
2663 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2665 err_msg = " error!\nCan't get hash value "
2670 if (calculate_hash (data, size, algo, value, &value_len)) {
2671 err_msg = " error!\nUnsupported hash algorithm";
2675 if (value_len != fit_value_len) {
2676 err_msg = " error !\nBad hash value len";
2678 } else if (memcmp (value, fit_value, value_len) != 0) {
2679 err_msg = " error!\nBad hash value";
2689 printf ("%s for '%s' hash node in '%s' image node\n",
2690 err_msg, fit_get_name (fit, noffset, NULL),
2691 fit_get_name (fit, image_noffset, NULL));
2696 * fit_all_image_check_hashes - verify data intergity for all images
2697 * @fit: pointer to the FIT format image header
2699 * fit_all_image_check_hashes() goes over all images in the FIT and
2700 * for every images checks if all it's hashes are valid.
2703 * 1, if all hashes of all images are valid
2704 * 0, otherwise (or on error)
2706 int fit_all_image_check_hashes (const void *fit)
2713 /* Find images parent node offset */
2714 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2715 if (images_noffset < 0) {
2716 printf ("Can't find images parent node '%s' (%s)\n",
2717 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2721 /* Process all image subnodes, check hashes for each */
2722 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2724 for (ndepth = 0, count = 0,
2725 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2726 (noffset >= 0) && (ndepth > 0);
2727 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2730 * Direct child node of the images parent node,
2731 * i.e. component image node.
2733 printf (" Hash(es) for Image %u (%s): ", count++,
2734 fit_get_name (fit, noffset, NULL));
2736 if (!fit_image_check_hashes (fit, noffset))
2745 * fit_image_check_os - check whether image node is of a given os type
2746 * @fit: pointer to the FIT format image header
2747 * @noffset: component image node offset
2748 * @os: requested image os
2750 * fit_image_check_os() reads image os property and compares its numeric
2751 * id with the requested os. Comparison result is returned to the caller.
2754 * 1 if image is of given os type
2755 * 0 otherwise (or on error)
2757 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2761 if (fit_image_get_os (fit, noffset, &image_os))
2763 return (os == image_os);
2767 * fit_image_check_arch - check whether image node is of a given arch
2768 * @fit: pointer to the FIT format image header
2769 * @noffset: component image node offset
2770 * @arch: requested imagearch
2772 * fit_image_check_arch() reads image arch property and compares its numeric
2773 * id with the requested arch. Comparison result is returned to the caller.
2776 * 1 if image is of given arch
2777 * 0 otherwise (or on error)
2779 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2783 if (fit_image_get_arch (fit, noffset, &image_arch))
2785 return (arch == image_arch);
2789 * fit_image_check_type - check whether image node is of a given type
2790 * @fit: pointer to the FIT format image header
2791 * @noffset: component image node offset
2792 * @type: requested image type
2794 * fit_image_check_type() reads image type property and compares its numeric
2795 * id with the requested type. Comparison result is returned to the caller.
2798 * 1 if image is of given type
2799 * 0 otherwise (or on error)
2801 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2805 if (fit_image_get_type (fit, noffset, &image_type))
2807 return (type == image_type);
2811 * fit_image_check_comp - check whether image node uses given compression
2812 * @fit: pointer to the FIT format image header
2813 * @noffset: component image node offset
2814 * @comp: requested image compression type
2816 * fit_image_check_comp() reads image compression property and compares its
2817 * numeric id with the requested compression type. Comparison result is
2818 * returned to the caller.
2821 * 1 if image uses requested compression
2822 * 0 otherwise (or on error)
2824 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2828 if (fit_image_get_comp (fit, noffset, &image_comp))
2830 return (comp == image_comp);
2834 * fit_check_format - sanity check FIT image format
2835 * @fit: pointer to the FIT format image header
2837 * fit_check_format() runs a basic sanity FIT image verification.
2838 * Routine checks for mandatory properties, nodes, etc.
2844 int fit_check_format (const void *fit)
2846 /* mandatory / node 'description' property */
2847 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2848 debug ("Wrong FIT format: no description\n");
2852 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2853 /* mandatory / node 'timestamp' property */
2854 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2855 debug ("Wrong FIT format: no timestamp\n");
2860 /* mandatory subimages parent '/images' node */
2861 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2862 debug ("Wrong FIT format: no images parent node\n");
2870 * fit_conf_get_node - get node offset for configuration of a given unit name
2871 * @fit: pointer to the FIT format image header
2872 * @conf_uname: configuration node unit name
2874 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2875 * parant node) of a provided unit name. If configuration is found its node offset
2876 * is returned to the caller.
2878 * When NULL is provided in second argument fit_conf_get_node() will search
2879 * for a default configuration node instead. Default configuration node unit name
2880 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2883 * configuration node offset when found (>=0)
2884 * negative number on failure (FDT_ERR_* code)
2886 int fit_conf_get_node (const void *fit, const char *conf_uname)
2888 int noffset, confs_noffset;
2891 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2892 if (confs_noffset < 0) {
2893 debug ("Can't find configurations parent node '%s' (%s)\n",
2894 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2895 return confs_noffset;
2898 if (conf_uname == NULL) {
2899 /* get configuration unit name from the default property */
2900 debug ("No configuration specified, trying default...\n");
2901 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2902 if (conf_uname == NULL) {
2903 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2906 debug ("Found default configuration: '%s'\n", conf_uname);
2909 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2911 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2912 conf_uname, fdt_strerror (noffset));
2918 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2919 const char *prop_name)
2924 /* get kernel image unit name from configuration kernel property */
2925 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2929 return fit_image_get_node (fit, uname);
2933 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2934 * a given configuration
2935 * @fit: pointer to the FIT format image header
2936 * @noffset: configuration node offset
2938 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2939 * configuration FIT_KERNEL_PROP property and translates it to the node
2943 * image node offset when found (>=0)
2944 * negative number on failure (FDT_ERR_* code)
2946 int fit_conf_get_kernel_node (const void *fit, int noffset)
2948 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2952 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2953 * a given configuration
2954 * @fit: pointer to the FIT format image header
2955 * @noffset: configuration node offset
2957 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2958 * configuration FIT_KERNEL_PROP property and translates it to the node
2962 * image node offset when found (>=0)
2963 * negative number on failure (FDT_ERR_* code)
2965 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2967 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2971 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2972 * a given configuration
2973 * @fit: pointer to the FIT format image header
2974 * @noffset: configuration node offset
2976 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2977 * configuration FIT_KERNEL_PROP property and translates it to the node
2981 * image node offset when found (>=0)
2982 * negative number on failure (FDT_ERR_* code)
2984 int fit_conf_get_fdt_node (const void *fit, int noffset)
2986 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2990 * fit_conf_print - prints out the FIT configuration details
2991 * @fit: pointer to the FIT format image header
2992 * @noffset: offset of the configuration node
2993 * @p: pointer to prefix string
2995 * fit_conf_print() lists all mandatory properies for the processed
2996 * configuration node.
2999 * no returned results
3001 void fit_conf_print (const void *fit, int noffset, const char *p)
3007 /* Mandatory properties */
3008 ret = fit_get_desc (fit, noffset, &desc);
3009 printf ("%s Description: ", p);
3011 printf ("unavailable\n");
3013 printf ("%s\n", desc);
3015 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3016 printf ("%s Kernel: ", p);
3018 printf ("unavailable\n");
3020 printf ("%s\n", uname);
3022 /* Optional properties */
3023 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3025 printf ("%s Init Ramdisk: %s\n", p, uname);
3027 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3029 printf ("%s FDT: %s\n", p, uname);
3033 * fit_check_ramdisk - verify FIT format ramdisk subimage
3034 * @fit_hdr: pointer to the FIT ramdisk header
3035 * @rd_noffset: ramdisk subimage node offset within FIT image
3036 * @arch: requested ramdisk image architecture type
3037 * @verify: data CRC verification flag
3039 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3040 * specified FIT image.
3047 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3049 fit_image_print (fit, rd_noffset, " ");
3052 puts (" Verifying Hash Integrity ... ");
3053 if (!fit_image_check_hashes (fit, rd_noffset)) {
3054 puts ("Bad Data Hash\n");
3055 show_boot_progress (-125);
3061 show_boot_progress (126);
3062 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3063 !fit_image_check_arch (fit, rd_noffset, arch) ||
3064 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3065 printf ("No Linux %s Ramdisk Image\n",
3066 genimg_get_arch_name(arch));
3067 show_boot_progress (-126);
3071 show_boot_progress (127);
3074 #endif /* USE_HOSTCC */
3075 #endif /* CONFIG_FIT */