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 const image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
72 #include <u-boot/md5.h>
75 #endif /* !USE_HOSTCC*/
77 static table_entry_t uimage_arch[] = {
78 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
79 { IH_ARCH_ALPHA, "alpha", "Alpha", },
80 { IH_ARCH_ARM, "arm", "ARM", },
81 { IH_ARCH_I386, "x86", "Intel x86", },
82 { IH_ARCH_IA64, "ia64", "IA64", },
83 { IH_ARCH_M68K, "m68k", "M68K", },
84 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
85 { IH_ARCH_MIPS, "mips", "MIPS", },
86 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
87 { IH_ARCH_NIOS, "nios", "NIOS", },
88 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
89 { IH_ARCH_PPC, "powerpc", "PowerPC", },
90 { IH_ARCH_PPC, "ppc", "PowerPC", },
91 { IH_ARCH_S390, "s390", "IBM S390", },
92 { IH_ARCH_SH, "sh", "SuperH", },
93 { IH_ARCH_SPARC, "sparc", "SPARC", },
94 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
95 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
96 { IH_ARCH_AVR32, "avr32", "AVR32", },
100 static table_entry_t uimage_os[] = {
101 { IH_OS_INVALID, NULL, "Invalid OS", },
102 { IH_OS_LINUX, "linux", "Linux", },
103 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
104 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
106 { IH_OS_NETBSD, "netbsd", "NetBSD", },
107 { IH_OS_RTEMS, "rtems", "RTEMS", },
108 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
109 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
110 { IH_OS_QNX, "qnx", "QNX", },
111 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
113 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
114 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
117 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
118 { IH_OS_DELL, "dell", "Dell", },
119 { IH_OS_ESIX, "esix", "Esix", },
120 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
121 { IH_OS_IRIX, "irix", "Irix", },
122 { IH_OS_NCR, "ncr", "NCR", },
123 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
124 { IH_OS_PSOS, "psos", "pSOS", },
125 { IH_OS_SCO, "sco", "SCO", },
126 { IH_OS_SOLARIS, "solaris", "Solaris", },
127 { IH_OS_SVR4, "svr4", "SVR4", },
132 static table_entry_t uimage_type[] = {
133 { IH_TYPE_INVALID, NULL, "Invalid Image", },
134 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
135 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
136 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
137 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
138 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
139 { IH_TYPE_SCRIPT, "script", "Script", },
140 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
141 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
142 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
143 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
147 static table_entry_t uimage_comp[] = {
148 { IH_COMP_NONE, "none", "uncompressed", },
149 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
150 { IH_COMP_GZIP, "gzip", "gzip compressed", },
151 { IH_COMP_LZMA, "lzma", "lzma compressed", },
152 { IH_COMP_LZO, "lzo", "lzo compressed", },
156 uint32_t crc32 (uint32_t, const unsigned char *, uint);
157 uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
158 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
159 static void genimg_print_time (time_t timestamp);
162 /*****************************************************************************/
163 /* Legacy format routines */
164 /*****************************************************************************/
165 int image_check_hcrc (const image_header_t *hdr)
168 ulong len = image_get_header_size ();
169 image_header_t header;
171 /* Copy header so we can blank CRC field for re-calculation */
172 memmove (&header, (char *)hdr, image_get_header_size ());
173 image_set_hcrc (&header, 0);
175 hcrc = crc32 (0, (unsigned char *)&header, len);
177 return (hcrc == image_get_hcrc (hdr));
180 int image_check_dcrc (const image_header_t *hdr)
182 ulong data = image_get_data (hdr);
183 ulong len = image_get_data_size (hdr);
184 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
186 return (dcrc == image_get_dcrc (hdr));
190 * image_multi_count - get component (sub-image) count
191 * @hdr: pointer to the header of the multi component image
193 * image_multi_count() returns number of components in a multi
196 * Note: no checking of the image type is done, caller must pass
197 * a valid multi component image.
200 * number of components
202 ulong image_multi_count (const image_header_t *hdr)
207 /* get start of the image payload, which in case of multi
208 * component images that points to a table of component sizes */
209 size = (uint32_t *)image_get_data (hdr);
211 /* count non empty slots */
212 for (i = 0; size[i]; ++i)
219 * image_multi_getimg - get component data address and size
220 * @hdr: pointer to the header of the multi component image
221 * @idx: index of the requested component
222 * @data: pointer to a ulong variable, will hold component data address
223 * @len: pointer to a ulong variable, will hold component size
225 * image_multi_getimg() returns size and data address for the requested
226 * component in a multi component image.
228 * Note: no checking of the image type is done, caller must pass
229 * a valid multi component image.
232 * data address and size of the component, if idx is valid
233 * 0 in data and len, if idx is out of range
235 void image_multi_getimg (const image_header_t *hdr, ulong idx,
236 ulong *data, ulong *len)
240 ulong offset, count, img_data;
242 /* get number of component */
243 count = image_multi_count (hdr);
245 /* get start of the image payload, which in case of multi
246 * component images that points to a table of component sizes */
247 size = (uint32_t *)image_get_data (hdr);
249 /* get address of the proper component data start, which means
250 * skipping sizes table (add 1 for last, null entry) */
251 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
254 *len = uimage_to_cpu (size[idx]);
257 /* go over all indices preceding requested component idx */
258 for (i = 0; i < idx; i++) {
259 /* add up i-th component size, rounding up to 4 bytes */
260 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
263 /* calculate idx-th component data address */
264 *data = img_data + offset;
271 static void image_print_type (const image_header_t *hdr)
273 const char *os, *arch, *type, *comp;
275 os = genimg_get_os_name (image_get_os (hdr));
276 arch = genimg_get_arch_name (image_get_arch (hdr));
277 type = genimg_get_type_name (image_get_type (hdr));
278 comp = genimg_get_comp_name (image_get_comp (hdr));
280 printf ("%s %s %s (%s)\n", arch, os, type, comp);
284 * image_print_contents - prints out the contents of the legacy format image
285 * @ptr: pointer to the legacy format image header
286 * @p: pointer to prefix string
288 * image_print_contents() formats a multi line legacy image contents description.
289 * The routine prints out all header fields followed by the size/offset data
290 * for MULTI/SCRIPT images.
293 * no returned results
295 void image_print_contents (const void *ptr)
297 const image_header_t *hdr = (const image_header_t *)ptr;
306 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
307 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
308 printf ("%sCreated: ", p);
309 genimg_print_time ((time_t)image_get_time (hdr));
311 printf ("%sImage Type: ", p);
312 image_print_type (hdr);
313 printf ("%sData Size: ", p);
314 genimg_print_size (image_get_data_size (hdr));
315 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
316 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
318 if (image_check_type (hdr, IH_TYPE_MULTI) ||
319 image_check_type (hdr, IH_TYPE_SCRIPT)) {
322 ulong count = image_multi_count (hdr);
324 printf ("%sContents:\n", p);
325 for (i = 0; i < count; i++) {
326 image_multi_getimg (hdr, i, &data, &len);
328 printf ("%s Image %d: ", p, i);
329 genimg_print_size (len);
331 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
333 * the user may need to know offsets
334 * if planning to do something with
337 printf ("%s Offset = 0x%08lx\n", p, data);
346 * image_get_ramdisk - get and verify ramdisk image
347 * @rd_addr: ramdisk image start address
348 * @arch: expected ramdisk architecture
349 * @verify: checksum verification flag
351 * image_get_ramdisk() returns a pointer to the verified ramdisk image
352 * header. Routine receives image start address and expected architecture
353 * flag. Verification done covers data and header integrity and os/type/arch
356 * If dataflash support is enabled routine checks for dataflash addresses
357 * and handles required dataflash reads.
360 * pointer to a ramdisk image header, if image was found and valid
361 * otherwise, return NULL
363 static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
366 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
368 if (!image_check_magic (rd_hdr)) {
369 puts ("Bad Magic Number\n");
370 show_boot_progress (-10);
374 if (!image_check_hcrc (rd_hdr)) {
375 puts ("Bad Header Checksum\n");
376 show_boot_progress (-11);
380 show_boot_progress (10);
381 image_print_contents (rd_hdr);
384 puts(" Verifying Checksum ... ");
385 if (!image_check_dcrc (rd_hdr)) {
386 puts ("Bad Data CRC\n");
387 show_boot_progress (-12);
393 show_boot_progress (11);
395 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
396 !image_check_arch (rd_hdr, arch) ||
397 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
398 printf ("No Linux %s Ramdisk Image\n",
399 genimg_get_arch_name(arch));
400 show_boot_progress (-13);
406 #endif /* !USE_HOSTCC */
408 /*****************************************************************************/
409 /* Shared dual-format routines */
410 /*****************************************************************************/
412 int getenv_yesno (char *var)
414 char *s = getenv (var);
415 return (s && (*s == 'n')) ? 0 : 1;
418 ulong getenv_bootm_low(void)
420 char *s = getenv ("bootm_low");
422 ulong tmp = simple_strtoul (s, NULL, 16);
426 #if defined(CONFIG_SYS_SDRAM_BASE)
427 return CONFIG_SYS_SDRAM_BASE;
428 #elif defined(CONFIG_ARM)
429 return gd->bd->bi_dram[0].start;
435 phys_size_t getenv_bootm_size(void)
437 char *s = getenv ("bootm_size");
440 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
444 #if defined(CONFIG_ARM)
445 return gd->bd->bi_dram[0].size;
447 return gd->bd->bi_memsize;
451 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
456 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
458 size_t tail = (len > chunksz) ? chunksz : len;
460 memmove (to, from, tail);
465 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
466 memmove (to, from, len);
467 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
469 #endif /* !USE_HOSTCC */
471 void genimg_print_size (uint32_t size)
474 printf ("%d Bytes = ", size);
475 print_size (size, "\n");
477 printf ("%d Bytes = %.2f kB = %.2f MB\n",
478 size, (double)size / 1.024e3,
479 (double)size / 1.048576e6);
483 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
484 static void genimg_print_time (time_t timestamp)
489 to_tm (timestamp, &tm);
490 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
491 tm.tm_year, tm.tm_mon, tm.tm_mday,
492 tm.tm_hour, tm.tm_min, tm.tm_sec);
494 printf ("%s", ctime(×tamp));
497 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
500 * get_table_entry_name - translate entry id to long name
501 * @table: pointer to a translation table for entries of a specific type
502 * @msg: message to be returned when translation fails
503 * @id: entry id to be translated
505 * get_table_entry_name() will go over translation table trying to find
506 * entry that matches given id. If matching entry is found, its long
507 * name is returned to the caller.
510 * long entry name if translation succeeds
513 char *get_table_entry_name (table_entry_t *table, char *msg, int id)
515 for (; table->id >= 0; ++table) {
517 #if defined(USE_HOSTCC) || defined(CONFIG_RELOC_FIXUP_WORKS)
520 return table->lname + gd->reloc_off;
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 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 #ifdef CONFIG_RELOC_FIXUP_WORKS
583 if (t->sname && strcmp(t->sname, name) == 0)
585 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
589 debug ("Invalid %s Type: %s\n", table_name, name);
590 #endif /* USE_HOSTCC */
594 int genimg_get_os_id (const char *name)
596 return (get_table_entry_id (uimage_os, "OS", name));
599 int genimg_get_arch_id (const char *name)
601 return (get_table_entry_id (uimage_arch, "CPU", name));
604 int genimg_get_type_id (const char *name)
606 return (get_table_entry_id (uimage_type, "Image", name));
609 int genimg_get_comp_id (const char *name)
611 return (get_table_entry_id (uimage_comp, "Compression", name));
616 * genimg_get_format - get image format type
617 * @img_addr: image start address
619 * genimg_get_format() checks whether provided address points to a valid
620 * legacy or FIT image.
622 * New uImage format and FDT blob are based on a libfdt. FDT blob
623 * may be passed directly or embedded in a FIT image. In both situations
624 * genimg_get_format() must be able to dectect libfdt header.
627 * image format type or IMAGE_FORMAT_INVALID if no image is present
629 int genimg_get_format (void *img_addr)
631 ulong format = IMAGE_FORMAT_INVALID;
632 const image_header_t *hdr;
633 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
637 hdr = (const image_header_t *)img_addr;
638 if (image_check_magic(hdr))
639 format = IMAGE_FORMAT_LEGACY;
640 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
642 fit_hdr = (char *)img_addr;
643 if (fdt_check_header (fit_hdr) == 0)
644 format = IMAGE_FORMAT_FIT;
652 * genimg_get_image - get image from special storage (if necessary)
653 * @img_addr: image start address
655 * genimg_get_image() checks if provided image start adddress is located
656 * in a dataflash storage. If so, image is moved to a system RAM memory.
659 * image start address after possible relocation from special storage
661 ulong genimg_get_image (ulong img_addr)
663 ulong ram_addr = img_addr;
665 #ifdef CONFIG_HAS_DATAFLASH
666 ulong h_size, d_size;
668 if (addr_dataflash (img_addr)){
669 /* ger RAM address */
670 ram_addr = CONFIG_SYS_LOAD_ADDR;
672 /* get header size */
673 h_size = image_get_header_size ();
674 #if defined(CONFIG_FIT)
675 if (sizeof(struct fdt_header) > h_size)
676 h_size = sizeof(struct fdt_header);
680 debug (" Reading image header from dataflash address "
681 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
683 read_dataflash (img_addr, h_size, (char *)ram_addr);
686 switch (genimg_get_format ((void *)ram_addr)) {
687 case IMAGE_FORMAT_LEGACY:
688 d_size = image_get_data_size ((const image_header_t *)ram_addr);
689 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
692 #if defined(CONFIG_FIT)
693 case IMAGE_FORMAT_FIT:
694 d_size = fit_get_size ((const void *)ram_addr) - h_size;
695 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
700 printf (" No valid image found at 0x%08lx\n", img_addr);
704 /* read in image data */
705 debug (" Reading image remaining data from dataflash address "
706 "%08lx to RAM address %08lx\n", img_addr + h_size,
709 read_dataflash (img_addr + h_size, d_size,
710 (char *)(ram_addr + h_size));
713 #endif /* CONFIG_HAS_DATAFLASH */
719 * fit_has_config - check if there is a valid FIT configuration
720 * @images: pointer to the bootm command headers structure
722 * fit_has_config() checks if there is a FIT configuration in use
723 * (if FTI support is present).
726 * 0, no FIT support or no configuration found
727 * 1, configuration found
729 int genimg_has_config (bootm_headers_t *images)
731 #if defined(CONFIG_FIT)
732 if (images->fit_uname_cfg)
739 * boot_get_ramdisk - main ramdisk handling routine
740 * @argc: command argument count
741 * @argv: command argument list
742 * @images: pointer to the bootm images structure
743 * @arch: expected ramdisk architecture
744 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
745 * @rd_end: pointer to a ulong variable, will hold ramdisk end
747 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
748 * Curently supported are the following ramdisk sources:
749 * - multicomponent kernel/ramdisk image,
750 * - commandline provided address of decicated ramdisk image.
753 * 0, if ramdisk image was found and valid, or skiped
754 * rd_start and rd_end are set to ramdisk start/end addresses if
755 * ramdisk image is found and valid
757 * 1, if ramdisk image is found but corrupted, or invalid
758 * rd_start and rd_end are set to 0 if no ramdisk exists
760 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
761 uint8_t arch, ulong *rd_start, ulong *rd_end)
763 ulong rd_addr, rd_load;
764 ulong rd_data, rd_len;
765 const image_header_t *rd_hdr;
766 #if defined(CONFIG_FIT)
768 const char *fit_uname_config = NULL;
769 const char *fit_uname_ramdisk = NULL;
781 * Look for a '-' which indicates to ignore the
784 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
785 debug ("## Skipping init Ramdisk\n");
786 rd_len = rd_data = 0;
787 } else if (argc >= 3 || genimg_has_config (images)) {
788 #if defined(CONFIG_FIT)
791 * If the init ramdisk comes from the FIT image and
792 * the FIT image address is omitted in the command
793 * line argument, try to use os FIT image address or
794 * default load address.
796 if (images->fit_uname_os)
797 default_addr = (ulong)images->fit_hdr_os;
799 default_addr = load_addr;
801 if (fit_parse_conf (argv[2], default_addr,
802 &rd_addr, &fit_uname_config)) {
803 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
804 fit_uname_config, rd_addr);
805 } else if (fit_parse_subimage (argv[2], default_addr,
806 &rd_addr, &fit_uname_ramdisk)) {
807 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
808 fit_uname_ramdisk, rd_addr);
812 rd_addr = simple_strtoul(argv[2], NULL, 16);
813 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
816 #if defined(CONFIG_FIT)
818 /* use FIT configuration provided in first bootm
821 rd_addr = (ulong)images->fit_hdr_os;
822 fit_uname_config = images->fit_uname_cfg;
823 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
824 fit_uname_config, rd_addr);
827 * Check whether configuration has ramdisk defined,
828 * if not, don't try to use it, quit silently.
830 fit_hdr = (void *)rd_addr;
831 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
832 if (cfg_noffset < 0) {
833 debug ("* ramdisk: no such config\n");
837 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
838 if (rd_noffset < 0) {
839 debug ("* ramdisk: no ramdisk in config\n");
845 /* copy from dataflash if needed */
846 rd_addr = genimg_get_image (rd_addr);
849 * Check if there is an initrd image at the
850 * address provided in the second bootm argument
851 * check image type, for FIT images get FIT node.
853 switch (genimg_get_format ((void *)rd_addr)) {
854 case IMAGE_FORMAT_LEGACY:
855 printf ("## Loading init Ramdisk from Legacy "
856 "Image at %08lx ...\n", rd_addr);
858 show_boot_progress (9);
859 rd_hdr = image_get_ramdisk (rd_addr, arch,
865 rd_data = image_get_data (rd_hdr);
866 rd_len = image_get_data_size (rd_hdr);
867 rd_load = image_get_load (rd_hdr);
869 #if defined(CONFIG_FIT)
870 case IMAGE_FORMAT_FIT:
871 fit_hdr = (void *)rd_addr;
872 printf ("## Loading init Ramdisk from FIT "
873 "Image at %08lx ...\n", rd_addr);
875 show_boot_progress (120);
876 if (!fit_check_format (fit_hdr)) {
877 puts ("Bad FIT ramdisk image format!\n");
878 show_boot_progress (-120);
881 show_boot_progress (121);
883 if (!fit_uname_ramdisk) {
885 * no ramdisk image node unit name, try to get config
886 * node first. If config unit node name is NULL
887 * fit_conf_get_node() will try to find default config node
889 show_boot_progress (122);
890 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
891 if (cfg_noffset < 0) {
892 puts ("Could not find configuration node\n");
893 show_boot_progress (-122);
896 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
897 printf (" Using '%s' configuration\n", fit_uname_config);
899 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
900 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
902 /* get ramdisk component image node offset */
903 show_boot_progress (123);
904 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
906 if (rd_noffset < 0) {
907 puts ("Could not find subimage node\n");
908 show_boot_progress (-124);
912 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
914 show_boot_progress (125);
915 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
918 /* get ramdisk image data address and length */
919 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
920 puts ("Could not find ramdisk subimage data!\n");
921 show_boot_progress (-127);
924 show_boot_progress (128);
926 rd_data = (ulong)data;
929 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
930 puts ("Can't get ramdisk subimage load address!\n");
931 show_boot_progress (-129);
934 show_boot_progress (129);
936 images->fit_hdr_rd = fit_hdr;
937 images->fit_uname_rd = fit_uname_ramdisk;
938 images->fit_noffset_rd = rd_noffset;
942 puts ("Wrong Ramdisk Image Format\n");
943 rd_data = rd_len = rd_load = 0;
947 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
949 * We need to copy the ramdisk to SRAM to let Linux boot
952 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
955 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
957 } else if (images->legacy_hdr_valid &&
958 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
960 * Now check if we have a legacy mult-component image,
961 * get second entry data start address and len.
963 show_boot_progress (13);
964 printf ("## Loading init Ramdisk from multi component "
965 "Legacy Image at %08lx ...\n",
966 (ulong)images->legacy_hdr_os);
968 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
973 show_boot_progress (14);
974 rd_len = rd_data = 0;
978 debug ("## No init Ramdisk\n");
981 *rd_end = rd_data + rd_len;
983 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
989 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
991 * boot_ramdisk_high - relocate init ramdisk
992 * @lmb: pointer to lmb handle, will be used for memory mgmt
993 * @rd_data: ramdisk data start address
994 * @rd_len: ramdisk data length
995 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
996 * start address (after possible relocation)
997 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
998 * end address (after possible relocation)
1000 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1001 * variable and if requested ramdisk data is moved to a specified location.
1003 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1004 * start/end addresses if ramdisk image start and len were provided,
1005 * otherwise set initrd_start and initrd_end set to zeros.
1011 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1012 ulong *initrd_start, ulong *initrd_end)
1016 int initrd_copy_to_ram = 1;
1018 if ((s = getenv ("initrd_high")) != NULL) {
1019 /* a value of "no" or a similar string will act like 0,
1020 * turning the "load high" feature off. This is intentional.
1022 initrd_high = simple_strtoul (s, NULL, 16);
1023 if (initrd_high == ~0)
1024 initrd_copy_to_ram = 0;
1026 /* not set, no restrictions to load high */
1031 #ifdef CONFIG_LOGBUFFER
1032 /* Prevent initrd from overwriting logbuffer */
1033 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1036 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1037 initrd_high, initrd_copy_to_ram);
1040 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1041 debug (" in-place initrd\n");
1042 *initrd_start = rd_data;
1043 *initrd_end = rd_data + rd_len;
1044 lmb_reserve(lmb, rd_data, rd_len);
1047 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1049 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
1051 if (*initrd_start == 0) {
1052 puts ("ramdisk - allocation error\n");
1055 show_boot_progress (12);
1057 *initrd_end = *initrd_start + rd_len;
1058 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1059 *initrd_start, *initrd_end);
1061 memmove_wd ((void *)*initrd_start,
1062 (void *)rd_data, rd_len, CHUNKSZ);
1070 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1071 *initrd_start, *initrd_end);
1078 #endif /* defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC) */
1080 #ifdef CONFIG_OF_LIBFDT
1081 static void fdt_error (const char *msg)
1085 puts (" - must RESET the board to recover.\n");
1088 static const image_header_t *image_get_fdt (ulong fdt_addr)
1090 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1092 image_print_contents (fdt_hdr);
1094 puts (" Verifying Checksum ... ");
1095 if (!image_check_hcrc (fdt_hdr)) {
1096 fdt_error ("fdt header checksum invalid");
1100 if (!image_check_dcrc (fdt_hdr)) {
1101 fdt_error ("fdt checksum invalid");
1106 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1107 fdt_error ("uImage is not a fdt");
1110 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1111 fdt_error ("uImage is compressed");
1114 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1115 fdt_error ("uImage data is not a fdt");
1122 * fit_check_fdt - verify FIT format FDT subimage
1123 * @fit_hdr: pointer to the FIT header
1124 * fdt_noffset: FDT subimage node offset within FIT image
1125 * @verify: data CRC verification flag
1127 * fit_check_fdt() verifies integrity of the FDT subimage and from
1128 * specified FIT image.
1134 #if defined(CONFIG_FIT)
1135 static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1137 fit_image_print (fit, fdt_noffset, " ");
1140 puts (" Verifying Hash Integrity ... ");
1141 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1142 fdt_error ("Bad Data Hash");
1148 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1149 fdt_error ("Not a FDT image");
1153 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1154 fdt_error ("FDT image is compressed");
1160 #endif /* CONFIG_FIT */
1162 #ifndef CONFIG_SYS_FDT_PAD
1163 #define CONFIG_SYS_FDT_PAD 0x3000
1167 * boot_relocate_fdt - relocate flat device tree
1168 * @lmb: pointer to lmb handle, will be used for memory mgmt
1169 * @bootmap_base: base address of the bootmap region
1170 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1171 * @of_size: pointer to a ulong variable, will hold fdt length
1173 * boot_relocate_fdt() determines if the of_flat_tree address is within
1174 * the bootmap and if not relocates it into that region
1176 * of_flat_tree and of_size are set to final (after relocation) values
1182 int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
1183 char **of_flat_tree, ulong *of_size)
1185 char *fdt_blob = *of_flat_tree;
1193 if (fdt_check_header (fdt_blob) != 0) {
1194 fdt_error ("image is not a fdt");
1198 #ifndef CONFIG_SYS_NO_FLASH
1199 /* move the blob if it is in flash (set relocate) */
1200 if (addr2info ((ulong)fdt_blob) != NULL)
1205 * The blob needs to be inside the boot mapping.
1207 if (fdt_blob < (char *)bootmap_base)
1210 if ((fdt_blob + *of_size + CONFIG_SYS_FDT_PAD) >=
1211 ((char *)CONFIG_SYS_BOOTMAPSZ + bootmap_base))
1214 /* move flattend device tree if needed */
1219 /* position on a 4K boundary before the alloc_current */
1220 /* Pad the FDT by a specified amount */
1221 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1222 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
1223 (CONFIG_SYS_BOOTMAPSZ + bootmap_base));
1225 if (of_start == 0) {
1226 puts("device tree - allocation error\n");
1230 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
1231 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
1234 printf (" Loading Device Tree to %08lx, end %08lx ... ",
1235 of_start, of_start + of_len - 1);
1237 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
1239 fdt_error ("fdt move failed");
1244 *of_flat_tree = (char *)of_start;
1247 *of_flat_tree = fdt_blob;
1248 of_len = (CONFIG_SYS_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
1249 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
1250 fdt_set_totalsize(*of_flat_tree, of_len);
1255 set_working_fdt_addr(*of_flat_tree);
1263 * boot_get_fdt - main fdt handling routine
1264 * @argc: command argument count
1265 * @argv: command argument list
1266 * @images: pointer to the bootm images structure
1267 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1268 * @of_size: pointer to a ulong variable, will hold fdt length
1270 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1271 * Curently supported are the following ramdisk sources:
1272 * - multicomponent kernel/ramdisk image,
1273 * - commandline provided address of decicated ramdisk image.
1276 * 0, if fdt image was found and valid, or skipped
1277 * of_flat_tree and of_size are set to fdt start address and length if
1278 * fdt image is found and valid
1280 * 1, if fdt image is found but corrupted
1281 * of_flat_tree and of_size are set to 0 if no fdt exists
1283 int boot_get_fdt (int flag, int argc, char *argv[], bootm_headers_t *images,
1284 char **of_flat_tree, ulong *of_size)
1286 const image_header_t *fdt_hdr;
1288 char *fdt_blob = NULL;
1289 ulong image_start, image_end;
1290 ulong load_start, load_end;
1291 #if defined(CONFIG_FIT)
1293 const char *fit_uname_config = NULL;
1294 const char *fit_uname_fdt = NULL;
1302 *of_flat_tree = NULL;
1305 if (argc > 3 || genimg_has_config (images)) {
1306 #if defined(CONFIG_FIT)
1309 * If the FDT blob comes from the FIT image and the
1310 * FIT image address is omitted in the command line
1311 * argument, try to use ramdisk or os FIT image
1312 * address or default load address.
1314 if (images->fit_uname_rd)
1315 default_addr = (ulong)images->fit_hdr_rd;
1316 else if (images->fit_uname_os)
1317 default_addr = (ulong)images->fit_hdr_os;
1319 default_addr = load_addr;
1321 if (fit_parse_conf (argv[3], default_addr,
1322 &fdt_addr, &fit_uname_config)) {
1323 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1324 fit_uname_config, fdt_addr);
1325 } else if (fit_parse_subimage (argv[3], default_addr,
1326 &fdt_addr, &fit_uname_fdt)) {
1327 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1328 fit_uname_fdt, fdt_addr);
1332 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1333 debug ("* fdt: cmdline image address = 0x%08lx\n",
1336 #if defined(CONFIG_FIT)
1338 /* use FIT configuration provided in first bootm
1341 fdt_addr = (ulong)images->fit_hdr_os;
1342 fit_uname_config = images->fit_uname_cfg;
1343 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1344 fit_uname_config, fdt_addr);
1347 * Check whether configuration has FDT blob defined,
1348 * if not quit silently.
1350 fit_hdr = (void *)fdt_addr;
1351 cfg_noffset = fit_conf_get_node (fit_hdr,
1353 if (cfg_noffset < 0) {
1354 debug ("* fdt: no such config\n");
1358 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1360 if (fdt_noffset < 0) {
1361 debug ("* fdt: no fdt in config\n");
1367 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1370 /* copy from dataflash if needed */
1371 fdt_addr = genimg_get_image (fdt_addr);
1374 * Check if there is an FDT image at the
1375 * address provided in the second bootm argument
1376 * check image type, for FIT images get a FIT node.
1378 switch (genimg_get_format ((void *)fdt_addr)) {
1379 case IMAGE_FORMAT_LEGACY:
1380 /* verify fdt_addr points to a valid image header */
1381 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1383 fdt_hdr = image_get_fdt (fdt_addr);
1388 * move image data to the load address,
1389 * make sure we don't overwrite initial image
1391 image_start = (ulong)fdt_hdr;
1392 image_end = image_get_image_end (fdt_hdr);
1394 load_start = image_get_load (fdt_hdr);
1395 load_end = load_start + image_get_data_size (fdt_hdr);
1397 if ((load_start < image_end) && (load_end > image_start)) {
1398 fdt_error ("fdt overwritten");
1402 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1403 image_get_data (fdt_hdr), load_start);
1405 memmove ((void *)load_start,
1406 (void *)image_get_data (fdt_hdr),
1407 image_get_data_size (fdt_hdr));
1409 fdt_blob = (char *)load_start;
1411 case IMAGE_FORMAT_FIT:
1413 * This case will catch both: new uImage format
1414 * (libfdt based) and raw FDT blob (also libfdt
1417 #if defined(CONFIG_FIT)
1418 /* check FDT blob vs FIT blob */
1419 if (fit_check_format ((const void *)fdt_addr)) {
1423 fit_hdr = (void *)fdt_addr;
1424 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1427 if (!fit_uname_fdt) {
1429 * no FDT blob image node unit name,
1430 * try to get config node first. If
1431 * config unit node name is NULL
1432 * fit_conf_get_node() will try to
1433 * find default config node
1435 cfg_noffset = fit_conf_get_node (fit_hdr,
1438 if (cfg_noffset < 0) {
1439 fdt_error ("Could not find configuration node\n");
1443 fit_uname_config = fdt_get_name (fit_hdr,
1445 printf (" Using '%s' configuration\n",
1448 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1450 fit_uname_fdt = fit_get_name (fit_hdr,
1453 /* get FDT component image node offset */
1454 fdt_noffset = fit_image_get_node (fit_hdr,
1457 if (fdt_noffset < 0) {
1458 fdt_error ("Could not find subimage node\n");
1462 printf (" Trying '%s' FDT blob subimage\n",
1465 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1469 /* get ramdisk image data address and length */
1470 if (fit_image_get_data (fit_hdr, fdt_noffset,
1472 fdt_error ("Could not find FDT subimage data");
1476 /* verift that image data is a proper FDT blob */
1477 if (fdt_check_header ((char *)data) != 0) {
1478 fdt_error ("Subimage data is not a FTD");
1483 * move image data to the load address,
1484 * make sure we don't overwrite initial image
1486 image_start = (ulong)fit_hdr;
1487 image_end = fit_get_end (fit_hdr);
1489 if (fit_image_get_load (fit_hdr, fdt_noffset,
1490 &load_start) == 0) {
1491 load_end = load_start + size;
1493 if ((load_start < image_end) &&
1494 (load_end > image_start)) {
1495 fdt_error ("FDT overwritten");
1499 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1500 (ulong)data, load_start);
1502 memmove ((void *)load_start,
1503 (void *)data, size);
1505 fdt_blob = (char *)load_start;
1507 fdt_blob = (char *)data;
1510 images->fit_hdr_fdt = fit_hdr;
1511 images->fit_uname_fdt = fit_uname_fdt;
1512 images->fit_noffset_fdt = fdt_noffset;
1520 fdt_blob = (char *)fdt_addr;
1521 debug ("* fdt: raw FDT blob\n");
1522 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1526 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
1530 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1532 } else if (images->legacy_hdr_valid &&
1533 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1535 ulong fdt_data, fdt_len;
1538 * Now check if we have a legacy multi-component image,
1539 * get second entry data start address and len.
1541 printf ("## Flattened Device Tree from multi "
1542 "component Image at %08lX\n",
1543 (ulong)images->legacy_hdr_os);
1545 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1548 fdt_blob = (char *)fdt_data;
1549 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1551 if (fdt_check_header (fdt_blob) != 0) {
1552 fdt_error ("image is not a fdt");
1556 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
1557 fdt_error ("fdt size != image size");
1561 debug ("## No Flattened Device Tree\n");
1565 debug ("## No Flattened Device Tree\n");
1569 *of_flat_tree = fdt_blob;
1570 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
1571 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
1572 (ulong)*of_flat_tree, *of_size);
1581 #endif /* CONFIG_OF_LIBFDT */
1583 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
1585 * boot_get_cmdline - allocate and initialize kernel cmdline
1586 * @lmb: pointer to lmb handle, will be used for memory mgmt
1587 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1588 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1589 * @bootmap_base: ulong variable, holds offset in physical memory to
1592 * boot_get_cmdline() allocates space for kernel command line below
1593 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1594 * variable is present its contents is copied to allocated kernel
1601 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1607 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1608 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
1610 if (cmdline == NULL)
1613 if ((s = getenv("bootargs")) == NULL)
1618 *cmd_start = (ulong) & cmdline[0];
1619 *cmd_end = *cmd_start + strlen(cmdline);
1621 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1627 * boot_get_kbd - allocate and initialize kernel copy of board info
1628 * @lmb: pointer to lmb handle, will be used for memory mgmt
1629 * @kbd: double pointer to board info data
1630 * @bootmap_base: ulong variable, holds offset in physical memory to
1633 * boot_get_kbd() allocates space for kernel copy of board info data below
1634 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1635 * the current u-boot board info data.
1641 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1643 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1644 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
1650 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1652 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1653 do_bdinfo(NULL, 0, 0, NULL);
1658 #endif /* CONFIG_PPC || CONFIG_M68K */
1659 #endif /* !USE_HOSTCC */
1661 #if defined(CONFIG_FIT)
1662 /*****************************************************************************/
1663 /* New uImage format routines */
1664 /*****************************************************************************/
1666 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1667 ulong *addr, const char **name)
1674 sep = strchr (spec, sepc);
1677 *addr = simple_strtoul (spec, NULL, 16);
1687 * fit_parse_conf - parse FIT configuration spec
1688 * @spec: input string, containing configuration spec
1689 * @add_curr: current image address (to be used as a possible default)
1690 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1692 * @conf_name double pointer to a char, will hold pointer to a configuration
1695 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1696 * where <addr> is a FIT image address that contains configuration
1697 * with a <conf> unit name.
1699 * Address part is optional, and if omitted default add_curr will
1703 * 1 if spec is a valid configuration string,
1704 * addr and conf_name are set accordingly
1707 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1708 ulong *addr, const char **conf_name)
1710 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1714 * fit_parse_subimage - parse FIT subimage spec
1715 * @spec: input string, containing subimage spec
1716 * @add_curr: current image address (to be used as a possible default)
1717 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1719 * @image_name: double pointer to a char, will hold pointer to a subimage name
1721 * fit_parse_subimage() expects subimage spec in the for of
1722 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1723 * subimage with a <subimg> unit name.
1725 * Address part is optional, and if omitted default add_curr will
1729 * 1 if spec is a valid subimage string,
1730 * addr and image_name are set accordingly
1733 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1734 ulong *addr, const char **image_name)
1736 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1738 #endif /* !USE_HOSTCC */
1740 static void fit_get_debug (const void *fit, int noffset,
1741 char *prop_name, int err)
1743 debug ("Can't get '%s' property from FIT 0x%08lx, "
1744 "node: offset %d, name %s (%s)\n",
1745 prop_name, (ulong)fit, noffset,
1746 fit_get_name (fit, noffset, NULL),
1747 fdt_strerror (err));
1751 * fit_print_contents - prints out the contents of the FIT format image
1752 * @fit: pointer to the FIT format image header
1753 * @p: pointer to prefix string
1755 * fit_print_contents() formats a multi line FIT image contents description.
1756 * The routine prints out FIT image properties (root node level) follwed by
1757 * the details of each component image.
1760 * no returned results
1762 void fit_print_contents (const void *fit)
1773 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1783 /* Root node properties */
1784 ret = fit_get_desc (fit, 0, &desc);
1785 printf ("%sFIT description: ", p);
1787 printf ("unavailable\n");
1789 printf ("%s\n", desc);
1791 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1792 ret = fit_get_timestamp (fit, 0, ×tamp);
1793 printf ("%sCreated: ", p);
1795 printf ("unavailable\n");
1797 genimg_print_time (timestamp);
1800 /* Find images parent node offset */
1801 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1802 if (images_noffset < 0) {
1803 printf ("Can't find images parent node '%s' (%s)\n",
1804 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1808 /* Process its subnodes, print out component images details */
1809 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1810 (noffset >= 0) && (ndepth > 0);
1811 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1814 * Direct child node of the images parent node,
1815 * i.e. component image node.
1817 printf ("%s Image %u (%s)\n", p, count++,
1818 fit_get_name(fit, noffset, NULL));
1820 fit_image_print (fit, noffset, p);
1824 /* Find configurations parent node offset */
1825 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1826 if (confs_noffset < 0) {
1827 debug ("Can't get configurations parent node '%s' (%s)\n",
1828 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1832 /* get default configuration unit name from default property */
1833 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1835 printf ("%s Default Configuration: '%s'\n", p, uname);
1837 /* Process its subnodes, print out configurations details */
1838 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1839 (noffset >= 0) && (ndepth > 0);
1840 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1843 * Direct child node of the configurations parent node,
1844 * i.e. configuration node.
1846 printf ("%s Configuration %u (%s)\n", p, count++,
1847 fit_get_name(fit, noffset, NULL));
1849 fit_conf_print (fit, noffset, p);
1855 * fit_image_print - prints out the FIT component image details
1856 * @fit: pointer to the FIT format image header
1857 * @image_noffset: offset of the component image node
1858 * @p: pointer to prefix string
1860 * fit_image_print() lists all mandatory properies for the processed component
1861 * image. If present, hash nodes are printed out as well. Load
1862 * address for images of type firmware is also printed out. Since the load
1863 * address is not mandatory for firmware images, it will be output as
1864 * "unavailable" when not present.
1867 * no returned results
1869 void fit_image_print (const void *fit, int image_noffset, const char *p)
1872 uint8_t type, arch, os, comp;
1880 /* Mandatory properties */
1881 ret = fit_get_desc (fit, image_noffset, &desc);
1882 printf ("%s Description: ", p);
1884 printf ("unavailable\n");
1886 printf ("%s\n", desc);
1888 fit_image_get_type (fit, image_noffset, &type);
1889 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1891 fit_image_get_comp (fit, image_noffset, &comp);
1892 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1894 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1897 printf ("%s Data Start: ", p);
1899 printf ("unavailable\n");
1901 printf ("0x%08lx\n", (ulong)data);
1904 printf ("%s Data Size: ", p);
1906 printf ("unavailable\n");
1908 genimg_print_size (size);
1910 /* Remaining, type dependent properties */
1911 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1912 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1913 (type == IH_TYPE_FLATDT)) {
1914 fit_image_get_arch (fit, image_noffset, &arch);
1915 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1918 if (type == IH_TYPE_KERNEL) {
1919 fit_image_get_os (fit, image_noffset, &os);
1920 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1923 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1924 (type == IH_TYPE_FIRMWARE)) {
1925 ret = fit_image_get_load (fit, image_noffset, &load);
1926 printf ("%s Load Address: ", p);
1928 printf ("unavailable\n");
1930 printf ("0x%08lx\n", load);
1933 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1934 fit_image_get_entry (fit, image_noffset, &entry);
1935 printf ("%s Entry Point: ", p);
1937 printf ("unavailable\n");
1939 printf ("0x%08lx\n", entry);
1942 /* Process all hash subnodes of the component image node */
1943 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1944 (noffset >= 0) && (ndepth > 0);
1945 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1947 /* Direct child node of the component image node */
1948 fit_image_print_hash (fit, noffset, p);
1954 * fit_image_print_hash - prints out the hash node details
1955 * @fit: pointer to the FIT format image header
1956 * @noffset: offset of the hash node
1957 * @p: pointer to prefix string
1959 * fit_image_print_hash() lists properies for the processed hash node
1962 * no returned results
1964 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1972 * Check subnode name, must be equal to "hash".
1973 * Multiple hash nodes require unique unit node
1974 * names, e.g. hash@1, hash@2, etc.
1976 if (strncmp (fit_get_name(fit, noffset, NULL),
1978 strlen(FIT_HASH_NODENAME)) != 0)
1981 debug ("%s Hash node: '%s'\n", p,
1982 fit_get_name (fit, noffset, NULL));
1984 printf ("%s Hash algo: ", p);
1985 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1986 printf ("invalid/unsupported\n");
1989 printf ("%s\n", algo);
1991 ret = fit_image_hash_get_value (fit, noffset, &value,
1993 printf ("%s Hash value: ", p);
1995 printf ("unavailable\n");
1997 for (i = 0; i < value_len; i++)
1998 printf ("%02x", value[i]);
2002 debug ("%s Hash len: %d\n", p, value_len);
2006 * fit_get_desc - get node description property
2007 * @fit: pointer to the FIT format image header
2008 * @noffset: node offset
2009 * @desc: double pointer to the char, will hold pointer to the descrption
2011 * fit_get_desc() reads description property from a given node, if
2012 * description is found pointer to it is returened in third call argument.
2018 int fit_get_desc (const void *fit, int noffset, char **desc)
2022 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2023 if (*desc == NULL) {
2024 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2032 * fit_get_timestamp - get node timestamp property
2033 * @fit: pointer to the FIT format image header
2034 * @noffset: node offset
2035 * @timestamp: pointer to the time_t, will hold read timestamp
2037 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2038 * is found and has a correct size its value is retured in third call
2043 * -1, on property read failure
2044 * -2, on wrong timestamp size
2046 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2051 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2053 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2056 if (len != sizeof (uint32_t)) {
2057 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2061 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2066 * fit_image_get_node - get node offset for component image of a given unit name
2067 * @fit: pointer to the FIT format image header
2068 * @image_uname: component image node unit name
2070 * fit_image_get_node() finds a component image (withing the '/images'
2071 * node) of a provided unit name. If image is found its node offset is
2072 * returned to the caller.
2075 * image node offset when found (>=0)
2076 * negative number on failure (FDT_ERR_* code)
2078 int fit_image_get_node (const void *fit, const char *image_uname)
2080 int noffset, images_noffset;
2082 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2083 if (images_noffset < 0) {
2084 debug ("Can't find images parent node '%s' (%s)\n",
2085 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2086 return images_noffset;
2089 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2091 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2092 image_uname, fdt_strerror (noffset));
2099 * fit_image_get_os - get os id for a given component image node
2100 * @fit: pointer to the FIT format image header
2101 * @noffset: component image node offset
2102 * @os: pointer to the uint8_t, will hold os numeric id
2104 * fit_image_get_os() finds os property in a given component image node.
2105 * If the property is found, its (string) value is translated to the numeric
2106 * id which is returned to the caller.
2112 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2117 /* Get OS name from property data */
2118 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2120 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2125 /* Translate OS name to id */
2126 *os = genimg_get_os_id (data);
2131 * fit_image_get_arch - get arch id for a given component image node
2132 * @fit: pointer to the FIT format image header
2133 * @noffset: component image node offset
2134 * @arch: pointer to the uint8_t, will hold arch numeric id
2136 * fit_image_get_arch() finds arch property in a given component image node.
2137 * If the property is found, its (string) value is translated to the numeric
2138 * id which is returned to the caller.
2144 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2149 /* Get architecture name from property data */
2150 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2152 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2157 /* Translate architecture name to id */
2158 *arch = genimg_get_arch_id (data);
2163 * fit_image_get_type - get type id for a given component image node
2164 * @fit: pointer to the FIT format image header
2165 * @noffset: component image node offset
2166 * @type: pointer to the uint8_t, will hold type numeric id
2168 * fit_image_get_type() finds type property in a given component image node.
2169 * If the property is found, its (string) value is translated to the numeric
2170 * id which is returned to the caller.
2176 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2181 /* Get image type name from property data */
2182 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2184 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2189 /* Translate image type name to id */
2190 *type = genimg_get_type_id (data);
2195 * fit_image_get_comp - get comp id for a given component image node
2196 * @fit: pointer to the FIT format image header
2197 * @noffset: component image node offset
2198 * @comp: pointer to the uint8_t, will hold comp numeric id
2200 * fit_image_get_comp() finds comp property in a given component image node.
2201 * If the property is found, its (string) value is translated to the numeric
2202 * id which is returned to the caller.
2208 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2213 /* Get compression name from property data */
2214 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2216 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2221 /* Translate compression name to id */
2222 *comp = genimg_get_comp_id (data);
2227 * fit_image_get_load - get load address property for a given component image node
2228 * @fit: pointer to the FIT format image header
2229 * @noffset: component image node offset
2230 * @load: pointer to the uint32_t, will hold load address
2232 * fit_image_get_load() finds load address property in a given component image node.
2233 * If the property is found, its value is returned to the caller.
2239 int fit_image_get_load (const void *fit, int noffset, ulong *load)
2242 const uint32_t *data;
2244 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2246 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2250 *load = uimage_to_cpu (*data);
2255 * fit_image_get_entry - get entry point address property for a given component image node
2256 * @fit: pointer to the FIT format image header
2257 * @noffset: component image node offset
2258 * @entry: pointer to the uint32_t, will hold entry point address
2260 * fit_image_get_entry() finds entry point address property in a given component image node.
2261 * If the property is found, its value is returned to the caller.
2267 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2270 const uint32_t *data;
2272 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2274 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2278 *entry = uimage_to_cpu (*data);
2283 * fit_image_get_data - get data property and its size for a given component image node
2284 * @fit: pointer to the FIT format image header
2285 * @noffset: component image node offset
2286 * @data: double pointer to void, will hold data property's data address
2287 * @size: pointer to size_t, will hold data property's data size
2289 * fit_image_get_data() finds data property in a given component image node.
2290 * If the property is found its data start address and size are returned to
2297 int fit_image_get_data (const void *fit, int noffset,
2298 const void **data, size_t *size)
2302 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2303 if (*data == NULL) {
2304 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2314 * fit_image_hash_get_algo - get hash algorithm name
2315 * @fit: pointer to the FIT format image header
2316 * @noffset: hash node offset
2317 * @algo: double pointer to char, will hold pointer to the algorithm name
2319 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2320 * If the property is found its data start address is returned to the caller.
2326 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2330 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2331 if (*algo == NULL) {
2332 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2340 * fit_image_hash_get_value - get hash value and length
2341 * @fit: pointer to the FIT format image header
2342 * @noffset: hash node offset
2343 * @value: double pointer to uint8_t, will hold address of a hash value data
2344 * @value_len: pointer to an int, will hold hash data length
2346 * fit_image_hash_get_value() finds hash value property in a given hash node.
2347 * If the property is found its data start address and size are returned to
2354 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2359 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2360 if (*value == NULL) {
2361 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2371 * fit_set_timestamp - set node timestamp property
2372 * @fit: pointer to the FIT format image header
2373 * @noffset: node offset
2374 * @timestamp: timestamp value to be set
2376 * fit_set_timestamp() attempts to set timestamp property in the requested
2377 * node and returns operation status to the caller.
2381 * -1, on property read failure
2383 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2388 t = cpu_to_uimage (timestamp);
2389 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2392 printf ("Can't set '%s' property for '%s' node (%s)\n",
2393 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2394 fdt_strerror (ret));
2402 * calculate_hash - calculate and return hash for provided input data
2403 * @data: pointer to the input data
2404 * @data_len: data length
2405 * @algo: requested hash algorithm
2406 * @value: pointer to the char, will hold hash value data (caller must
2407 * allocate enough free space)
2408 * value_len: length of the calculated hash
2410 * calculate_hash() computes input data hash according to the requested algorithm.
2411 * Resulting hash value is placed in caller provided 'value' buffer, length
2412 * of the calculated hash is returned via value_len pointer argument.
2416 * -1, when algo is unsupported
2418 static int calculate_hash (const void *data, int data_len, const char *algo,
2419 uint8_t *value, int *value_len)
2421 if (strcmp (algo, "crc32") == 0 ) {
2422 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2424 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2426 } else if (strcmp (algo, "sha1") == 0 ) {
2427 sha1_csum_wd ((unsigned char *) data, data_len,
2428 (unsigned char *) value, CHUNKSZ_SHA1);
2430 } else if (strcmp (algo, "md5") == 0 ) {
2431 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2434 debug ("Unsupported hash alogrithm\n");
2442 * fit_set_hashes - process FIT component image nodes and calculate hashes
2443 * @fit: pointer to the FIT format image header
2445 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2446 * Hashes are calculated for all component images which have hash subnodes
2447 * with algorithm property set to one of the supported hash algorithms.
2451 * libfdt error code, on failure
2453 int fit_set_hashes (void *fit)
2460 /* Find images parent node offset */
2461 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2462 if (images_noffset < 0) {
2463 printf ("Can't find images parent node '%s' (%s)\n",
2464 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2465 return images_noffset;
2468 /* Process its subnodes, print out component images details */
2469 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2470 (noffset >= 0) && (ndepth > 0);
2471 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2474 * Direct child node of the images parent node,
2475 * i.e. component image node.
2477 ret = fit_image_set_hashes (fit, noffset);
2487 * fit_image_set_hashes - calculate/set hashes for given component image node
2488 * @fit: pointer to the FIT format image header
2489 * @image_noffset: requested component image node
2491 * fit_image_set_hashes() adds hash values for an component image node. All
2492 * existing hash subnodes are checked, if algorithm property is set to one of
2493 * the supported hash algorithms, hash value is computed and corresponding
2494 * hash node property is set, for example:
2496 * Input component image node structure:
2498 * o image@1 (at image_noffset)
2499 * | - data = [binary data]
2503 * Output component image node structure:
2505 * o image@1 (at image_noffset)
2506 * | - data = [binary data]
2509 * |- value = sha1(data)
2515 int fit_image_set_hashes (void *fit, int image_noffset)
2520 uint8_t value[FIT_MAX_HASH_LEN];
2525 /* Get image data and data length */
2526 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2527 printf ("Can't get image data/size\n");
2531 /* Process all hash subnodes of the component image node */
2532 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2533 (noffset >= 0) && (ndepth > 0);
2534 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2536 /* Direct child node of the component image node */
2539 * Check subnode name, must be equal to "hash".
2540 * Multiple hash nodes require unique unit node
2541 * names, e.g. hash@1, hash@2, etc.
2543 if (strncmp (fit_get_name(fit, noffset, NULL),
2545 strlen(FIT_HASH_NODENAME)) != 0) {
2546 /* Not a hash subnode, skip it */
2550 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2551 printf ("Can't get hash algo property for "
2552 "'%s' hash node in '%s' image node\n",
2553 fit_get_name (fit, noffset, NULL),
2554 fit_get_name (fit, image_noffset, NULL));
2558 if (calculate_hash (data, size, algo, value, &value_len)) {
2559 printf ("Unsupported hash algorithm (%s) for "
2560 "'%s' hash node in '%s' image node\n",
2561 algo, fit_get_name (fit, noffset, NULL),
2562 fit_get_name (fit, image_noffset, NULL));
2566 if (fit_image_hash_set_value (fit, noffset, value,
2568 printf ("Can't set hash value for "
2569 "'%s' hash node in '%s' image node\n",
2570 fit_get_name (fit, noffset, NULL),
2571 fit_get_name (fit, image_noffset, NULL));
2581 * fit_image_hash_set_value - set hash value in requested has node
2582 * @fit: pointer to the FIT format image header
2583 * @noffset: hash node offset
2584 * @value: hash value to be set
2585 * @value_len: hash value length
2587 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2588 * given and returns operation status to the caller.
2594 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2599 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2601 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2602 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2603 fdt_strerror (ret));
2609 #endif /* USE_HOSTCC */
2612 * fit_image_check_hashes - verify data intergity
2613 * @fit: pointer to the FIT format image header
2614 * @image_noffset: component image node offset
2616 * fit_image_check_hashes() goes over component image hash nodes,
2617 * re-calculates each data hash and compares with the value stored in hash
2621 * 1, if all hashes are valid
2622 * 0, otherwise (or on error)
2624 int fit_image_check_hashes (const void *fit, int image_noffset)
2631 uint8_t value[FIT_MAX_HASH_LEN];
2637 /* Get image data and data length */
2638 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2639 printf ("Can't get image data/size\n");
2643 /* Process all hash subnodes of the component image node */
2644 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2645 (noffset >= 0) && (ndepth > 0);
2646 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2648 /* Direct child node of the component image node */
2651 * Check subnode name, must be equal to "hash".
2652 * Multiple hash nodes require unique unit node
2653 * names, e.g. hash@1, hash@2, etc.
2655 if (strncmp (fit_get_name(fit, noffset, NULL),
2657 strlen(FIT_HASH_NODENAME)) != 0)
2660 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2661 err_msg = " error!\nCan't get hash algo "
2665 printf ("%s", algo);
2667 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2669 err_msg = " error!\nCan't get hash value "
2674 if (calculate_hash (data, size, algo, value, &value_len)) {
2675 err_msg = " error!\nUnsupported hash algorithm";
2679 if (value_len != fit_value_len) {
2680 err_msg = " error !\nBad hash value len";
2682 } else if (memcmp (value, fit_value, value_len) != 0) {
2683 err_msg = " error!\nBad hash value";
2693 printf ("%s for '%s' hash node in '%s' image node\n",
2694 err_msg, fit_get_name (fit, noffset, NULL),
2695 fit_get_name (fit, image_noffset, NULL));
2700 * fit_all_image_check_hashes - verify data intergity for all images
2701 * @fit: pointer to the FIT format image header
2703 * fit_all_image_check_hashes() goes over all images in the FIT and
2704 * for every images checks if all it's hashes are valid.
2707 * 1, if all hashes of all images are valid
2708 * 0, otherwise (or on error)
2710 int fit_all_image_check_hashes (const void *fit)
2717 /* Find images parent node offset */
2718 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2719 if (images_noffset < 0) {
2720 printf ("Can't find images parent node '%s' (%s)\n",
2721 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2725 /* Process all image subnodes, check hashes for each */
2726 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2728 for (ndepth = 0, count = 0,
2729 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2730 (noffset >= 0) && (ndepth > 0);
2731 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2734 * Direct child node of the images parent node,
2735 * i.e. component image node.
2737 printf (" Hash(es) for Image %u (%s): ", count++,
2738 fit_get_name (fit, noffset, NULL));
2740 if (!fit_image_check_hashes (fit, noffset))
2749 * fit_image_check_os - check whether image node is of a given os type
2750 * @fit: pointer to the FIT format image header
2751 * @noffset: component image node offset
2752 * @os: requested image os
2754 * fit_image_check_os() reads image os property and compares its numeric
2755 * id with the requested os. Comparison result is returned to the caller.
2758 * 1 if image is of given os type
2759 * 0 otherwise (or on error)
2761 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2765 if (fit_image_get_os (fit, noffset, &image_os))
2767 return (os == image_os);
2771 * fit_image_check_arch - check whether image node is of a given arch
2772 * @fit: pointer to the FIT format image header
2773 * @noffset: component image node offset
2774 * @arch: requested imagearch
2776 * fit_image_check_arch() reads image arch property and compares its numeric
2777 * id with the requested arch. Comparison result is returned to the caller.
2780 * 1 if image is of given arch
2781 * 0 otherwise (or on error)
2783 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2787 if (fit_image_get_arch (fit, noffset, &image_arch))
2789 return (arch == image_arch);
2793 * fit_image_check_type - check whether image node is of a given type
2794 * @fit: pointer to the FIT format image header
2795 * @noffset: component image node offset
2796 * @type: requested image type
2798 * fit_image_check_type() reads image type property and compares its numeric
2799 * id with the requested type. Comparison result is returned to the caller.
2802 * 1 if image is of given type
2803 * 0 otherwise (or on error)
2805 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2809 if (fit_image_get_type (fit, noffset, &image_type))
2811 return (type == image_type);
2815 * fit_image_check_comp - check whether image node uses given compression
2816 * @fit: pointer to the FIT format image header
2817 * @noffset: component image node offset
2818 * @comp: requested image compression type
2820 * fit_image_check_comp() reads image compression property and compares its
2821 * numeric id with the requested compression type. Comparison result is
2822 * returned to the caller.
2825 * 1 if image uses requested compression
2826 * 0 otherwise (or on error)
2828 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2832 if (fit_image_get_comp (fit, noffset, &image_comp))
2834 return (comp == image_comp);
2838 * fit_check_format - sanity check FIT image format
2839 * @fit: pointer to the FIT format image header
2841 * fit_check_format() runs a basic sanity FIT image verification.
2842 * Routine checks for mandatory properties, nodes, etc.
2848 int fit_check_format (const void *fit)
2850 /* mandatory / node 'description' property */
2851 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2852 debug ("Wrong FIT format: no description\n");
2856 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2857 /* mandatory / node 'timestamp' property */
2858 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2859 debug ("Wrong FIT format: no timestamp\n");
2864 /* mandatory subimages parent '/images' node */
2865 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2866 debug ("Wrong FIT format: no images parent node\n");
2874 * fit_conf_get_node - get node offset for configuration of a given unit name
2875 * @fit: pointer to the FIT format image header
2876 * @conf_uname: configuration node unit name
2878 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2879 * parant node) of a provided unit name. If configuration is found its node offset
2880 * is returned to the caller.
2882 * When NULL is provided in second argument fit_conf_get_node() will search
2883 * for a default configuration node instead. Default configuration node unit name
2884 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2887 * configuration node offset when found (>=0)
2888 * negative number on failure (FDT_ERR_* code)
2890 int fit_conf_get_node (const void *fit, const char *conf_uname)
2892 int noffset, confs_noffset;
2895 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2896 if (confs_noffset < 0) {
2897 debug ("Can't find configurations parent node '%s' (%s)\n",
2898 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2899 return confs_noffset;
2902 if (conf_uname == NULL) {
2903 /* get configuration unit name from the default property */
2904 debug ("No configuration specified, trying default...\n");
2905 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2906 if (conf_uname == NULL) {
2907 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2910 debug ("Found default configuration: '%s'\n", conf_uname);
2913 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2915 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2916 conf_uname, fdt_strerror (noffset));
2922 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2923 const char *prop_name)
2928 /* get kernel image unit name from configuration kernel property */
2929 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2933 return fit_image_get_node (fit, uname);
2937 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2938 * a given configuration
2939 * @fit: pointer to the FIT format image header
2940 * @noffset: configuration node offset
2942 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2943 * configuration FIT_KERNEL_PROP property and translates it to the node
2947 * image node offset when found (>=0)
2948 * negative number on failure (FDT_ERR_* code)
2950 int fit_conf_get_kernel_node (const void *fit, int noffset)
2952 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2956 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2957 * a given configuration
2958 * @fit: pointer to the FIT format image header
2959 * @noffset: configuration node offset
2961 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2962 * configuration FIT_KERNEL_PROP property and translates it to the node
2966 * image node offset when found (>=0)
2967 * negative number on failure (FDT_ERR_* code)
2969 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2971 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2975 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2976 * a given configuration
2977 * @fit: pointer to the FIT format image header
2978 * @noffset: configuration node offset
2980 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2981 * configuration FIT_KERNEL_PROP property and translates it to the node
2985 * image node offset when found (>=0)
2986 * negative number on failure (FDT_ERR_* code)
2988 int fit_conf_get_fdt_node (const void *fit, int noffset)
2990 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2994 * fit_conf_print - prints out the FIT configuration details
2995 * @fit: pointer to the FIT format image header
2996 * @noffset: offset of the configuration node
2997 * @p: pointer to prefix string
2999 * fit_conf_print() lists all mandatory properies for the processed
3000 * configuration node.
3003 * no returned results
3005 void fit_conf_print (const void *fit, int noffset, const char *p)
3011 /* Mandatory properties */
3012 ret = fit_get_desc (fit, noffset, &desc);
3013 printf ("%s Description: ", p);
3015 printf ("unavailable\n");
3017 printf ("%s\n", desc);
3019 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3020 printf ("%s Kernel: ", p);
3022 printf ("unavailable\n");
3024 printf ("%s\n", uname);
3026 /* Optional properties */
3027 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3029 printf ("%s Init Ramdisk: %s\n", p, uname);
3031 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3033 printf ("%s FDT: %s\n", p, uname);
3037 * fit_check_ramdisk - verify FIT format ramdisk subimage
3038 * @fit_hdr: pointer to the FIT ramdisk header
3039 * @rd_noffset: ramdisk subimage node offset within FIT image
3040 * @arch: requested ramdisk image architecture type
3041 * @verify: data CRC verification flag
3043 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3044 * specified FIT image.
3051 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3053 fit_image_print (fit, rd_noffset, " ");
3056 puts (" Verifying Hash Integrity ... ");
3057 if (!fit_image_check_hashes (fit, rd_noffset)) {
3058 puts ("Bad Data Hash\n");
3059 show_boot_progress (-125);
3065 show_boot_progress (126);
3066 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3067 !fit_image_check_arch (fit, rd_noffset, arch) ||
3068 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3069 printf ("No Linux %s Ramdisk Image\n",
3070 genimg_get_arch_name(arch));
3071 show_boot_progress (-126);
3075 show_boot_progress (127);
3078 #endif /* USE_HOSTCC */
3079 #endif /* CONFIG_FIT */