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)
453 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
455 size_t tail = (len > chunksz) ? chunksz : len;
457 memmove (to, from, tail);
462 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
463 memmove (to, from, len);
464 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
466 #endif /* !USE_HOSTCC */
468 void genimg_print_size (uint32_t size)
471 printf ("%d Bytes = ", size);
472 print_size (size, "\n");
474 printf ("%d Bytes = %.2f kB = %.2f MB\n",
475 size, (double)size / 1.024e3,
476 (double)size / 1.048576e6);
480 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
481 static void genimg_print_time (time_t timestamp)
486 to_tm (timestamp, &tm);
487 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
488 tm.tm_year, tm.tm_mon, tm.tm_mday,
489 tm.tm_hour, tm.tm_min, tm.tm_sec);
491 printf ("%s", ctime(×tamp));
494 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
497 * get_table_entry_name - translate entry id to long name
498 * @table: pointer to a translation table for entries of a specific type
499 * @msg: message to be returned when translation fails
500 * @id: entry id to be translated
502 * get_table_entry_name() will go over translation table trying to find
503 * entry that matches given id. If matching entry is found, its long
504 * name is returned to the caller.
507 * long entry name if translation succeeds
510 char *get_table_entry_name (table_entry_t *table, char *msg, int id)
512 for (; table->id >= 0; ++table) {
514 #if defined(USE_HOSTCC) || defined(CONFIG_RELOC_FIXUP_WORKS)
517 return table->lname + gd->reloc_off;
523 const char *genimg_get_os_name (uint8_t os)
525 return (get_table_entry_name (uimage_os, "Unknown OS", os));
528 const char *genimg_get_arch_name (uint8_t arch)
530 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
533 const char *genimg_get_type_name (uint8_t type)
535 return (get_table_entry_name (uimage_type, "Unknown Image", type));
538 const char *genimg_get_comp_name (uint8_t comp)
540 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
544 * get_table_entry_id - translate short entry name to id
545 * @table: pointer to a translation table for entries of a specific type
546 * @table_name: to be used in case of error
547 * @name: entry short name to be translated
549 * get_table_entry_id() will go over translation table trying to find
550 * entry that matches given short name. If matching entry is found,
551 * its id returned to the caller.
554 * entry id if translation succeeds
557 int get_table_entry_id (table_entry_t *table,
558 const char *table_name, const char *name)
564 for (t = table; t->id >= 0; ++t) {
565 if (t->sname && strcasecmp(t->sname, name) == 0)
569 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
570 for (t = table; t->id >= 0; ++t) {
571 if (t->sname == NULL)
573 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
576 fprintf (stderr, "\n");
578 for (t = table; t->id >= 0; ++t) {
579 #ifdef CONFIG_RELOC_FIXUP_WORKS
580 if (t->sname && strcmp(t->sname, name) == 0)
582 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
586 debug ("Invalid %s Type: %s\n", table_name, name);
587 #endif /* USE_HOSTCC */
591 int genimg_get_os_id (const char *name)
593 return (get_table_entry_id (uimage_os, "OS", name));
596 int genimg_get_arch_id (const char *name)
598 return (get_table_entry_id (uimage_arch, "CPU", name));
601 int genimg_get_type_id (const char *name)
603 return (get_table_entry_id (uimage_type, "Image", name));
606 int genimg_get_comp_id (const char *name)
608 return (get_table_entry_id (uimage_comp, "Compression", name));
613 * genimg_get_format - get image format type
614 * @img_addr: image start address
616 * genimg_get_format() checks whether provided address points to a valid
617 * legacy or FIT image.
619 * New uImage format and FDT blob are based on a libfdt. FDT blob
620 * may be passed directly or embedded in a FIT image. In both situations
621 * genimg_get_format() must be able to dectect libfdt header.
624 * image format type or IMAGE_FORMAT_INVALID if no image is present
626 int genimg_get_format (void *img_addr)
628 ulong format = IMAGE_FORMAT_INVALID;
629 const image_header_t *hdr;
630 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
634 hdr = (const image_header_t *)img_addr;
635 if (image_check_magic(hdr))
636 format = IMAGE_FORMAT_LEGACY;
637 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
639 fit_hdr = (char *)img_addr;
640 if (fdt_check_header (fit_hdr) == 0)
641 format = IMAGE_FORMAT_FIT;
649 * genimg_get_image - get image from special storage (if necessary)
650 * @img_addr: image start address
652 * genimg_get_image() checks if provided image start adddress is located
653 * in a dataflash storage. If so, image is moved to a system RAM memory.
656 * image start address after possible relocation from special storage
658 ulong genimg_get_image (ulong img_addr)
660 ulong ram_addr = img_addr;
662 #ifdef CONFIG_HAS_DATAFLASH
663 ulong h_size, d_size;
665 if (addr_dataflash (img_addr)){
666 /* ger RAM address */
667 ram_addr = CONFIG_SYS_LOAD_ADDR;
669 /* get header size */
670 h_size = image_get_header_size ();
671 #if defined(CONFIG_FIT)
672 if (sizeof(struct fdt_header) > h_size)
673 h_size = sizeof(struct fdt_header);
677 debug (" Reading image header from dataflash address "
678 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
680 read_dataflash (img_addr, h_size, (char *)ram_addr);
683 switch (genimg_get_format ((void *)ram_addr)) {
684 case IMAGE_FORMAT_LEGACY:
685 d_size = image_get_data_size ((const image_header_t *)ram_addr);
686 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
689 #if defined(CONFIG_FIT)
690 case IMAGE_FORMAT_FIT:
691 d_size = fit_get_size ((const void *)ram_addr) - h_size;
692 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
697 printf (" No valid image found at 0x%08lx\n", img_addr);
701 /* read in image data */
702 debug (" Reading image remaining data from dataflash address "
703 "%08lx to RAM address %08lx\n", img_addr + h_size,
706 read_dataflash (img_addr + h_size, d_size,
707 (char *)(ram_addr + h_size));
710 #endif /* CONFIG_HAS_DATAFLASH */
716 * fit_has_config - check if there is a valid FIT configuration
717 * @images: pointer to the bootm command headers structure
719 * fit_has_config() checks if there is a FIT configuration in use
720 * (if FTI support is present).
723 * 0, no FIT support or no configuration found
724 * 1, configuration found
726 int genimg_has_config (bootm_headers_t *images)
728 #if defined(CONFIG_FIT)
729 if (images->fit_uname_cfg)
736 * boot_get_ramdisk - main ramdisk handling routine
737 * @argc: command argument count
738 * @argv: command argument list
739 * @images: pointer to the bootm images structure
740 * @arch: expected ramdisk architecture
741 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
742 * @rd_end: pointer to a ulong variable, will hold ramdisk end
744 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
745 * Curently supported are the following ramdisk sources:
746 * - multicomponent kernel/ramdisk image,
747 * - commandline provided address of decicated ramdisk image.
750 * 0, if ramdisk image was found and valid, or skiped
751 * rd_start and rd_end are set to ramdisk start/end addresses if
752 * ramdisk image is found and valid
754 * 1, if ramdisk image is found but corrupted, or invalid
755 * rd_start and rd_end are set to 0 if no ramdisk exists
757 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
758 uint8_t arch, ulong *rd_start, ulong *rd_end)
760 ulong rd_addr, rd_load;
761 ulong rd_data, rd_len;
762 const image_header_t *rd_hdr;
763 #if defined(CONFIG_FIT)
765 const char *fit_uname_config = NULL;
766 const char *fit_uname_ramdisk = NULL;
778 * Look for a '-' which indicates to ignore the
781 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
782 debug ("## Skipping init Ramdisk\n");
783 rd_len = rd_data = 0;
784 } else if (argc >= 3 || genimg_has_config (images)) {
785 #if defined(CONFIG_FIT)
788 * If the init ramdisk comes from the FIT image and
789 * the FIT image address is omitted in the command
790 * line argument, try to use os FIT image address or
791 * default load address.
793 if (images->fit_uname_os)
794 default_addr = (ulong)images->fit_hdr_os;
796 default_addr = load_addr;
798 if (fit_parse_conf (argv[2], default_addr,
799 &rd_addr, &fit_uname_config)) {
800 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
801 fit_uname_config, rd_addr);
802 } else if (fit_parse_subimage (argv[2], default_addr,
803 &rd_addr, &fit_uname_ramdisk)) {
804 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
805 fit_uname_ramdisk, rd_addr);
809 rd_addr = simple_strtoul(argv[2], NULL, 16);
810 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
813 #if defined(CONFIG_FIT)
815 /* use FIT configuration provided in first bootm
818 rd_addr = (ulong)images->fit_hdr_os;
819 fit_uname_config = images->fit_uname_cfg;
820 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
821 fit_uname_config, rd_addr);
824 * Check whether configuration has ramdisk defined,
825 * if not, don't try to use it, quit silently.
827 fit_hdr = (void *)rd_addr;
828 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
829 if (cfg_noffset < 0) {
830 debug ("* ramdisk: no such config\n");
834 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
835 if (rd_noffset < 0) {
836 debug ("* ramdisk: no ramdisk in config\n");
842 /* copy from dataflash if needed */
843 rd_addr = genimg_get_image (rd_addr);
846 * Check if there is an initrd image at the
847 * address provided in the second bootm argument
848 * check image type, for FIT images get FIT node.
850 switch (genimg_get_format ((void *)rd_addr)) {
851 case IMAGE_FORMAT_LEGACY:
852 printf ("## Loading init Ramdisk from Legacy "
853 "Image at %08lx ...\n", rd_addr);
855 show_boot_progress (9);
856 rd_hdr = image_get_ramdisk (rd_addr, arch,
862 rd_data = image_get_data (rd_hdr);
863 rd_len = image_get_data_size (rd_hdr);
864 rd_load = image_get_load (rd_hdr);
866 #if defined(CONFIG_FIT)
867 case IMAGE_FORMAT_FIT:
868 fit_hdr = (void *)rd_addr;
869 printf ("## Loading init Ramdisk from FIT "
870 "Image at %08lx ...\n", rd_addr);
872 show_boot_progress (120);
873 if (!fit_check_format (fit_hdr)) {
874 puts ("Bad FIT ramdisk image format!\n");
875 show_boot_progress (-120);
878 show_boot_progress (121);
880 if (!fit_uname_ramdisk) {
882 * no ramdisk image node unit name, try to get config
883 * node first. If config unit node name is NULL
884 * fit_conf_get_node() will try to find default config node
886 show_boot_progress (122);
887 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
888 if (cfg_noffset < 0) {
889 puts ("Could not find configuration node\n");
890 show_boot_progress (-122);
893 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
894 printf (" Using '%s' configuration\n", fit_uname_config);
896 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
897 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
899 /* get ramdisk component image node offset */
900 show_boot_progress (123);
901 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
903 if (rd_noffset < 0) {
904 puts ("Could not find subimage node\n");
905 show_boot_progress (-124);
909 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
911 show_boot_progress (125);
912 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
915 /* get ramdisk image data address and length */
916 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
917 puts ("Could not find ramdisk subimage data!\n");
918 show_boot_progress (-127);
921 show_boot_progress (128);
923 rd_data = (ulong)data;
926 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
927 puts ("Can't get ramdisk subimage load address!\n");
928 show_boot_progress (-129);
931 show_boot_progress (129);
933 images->fit_hdr_rd = fit_hdr;
934 images->fit_uname_rd = fit_uname_ramdisk;
935 images->fit_noffset_rd = rd_noffset;
939 puts ("Wrong Ramdisk Image Format\n");
940 rd_data = rd_len = rd_load = 0;
944 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
946 * We need to copy the ramdisk to SRAM to let Linux boot
949 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
952 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
954 } else if (images->legacy_hdr_valid &&
955 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
957 * Now check if we have a legacy mult-component image,
958 * get second entry data start address and len.
960 show_boot_progress (13);
961 printf ("## Loading init Ramdisk from multi component "
962 "Legacy Image at %08lx ...\n",
963 (ulong)images->legacy_hdr_os);
965 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
970 show_boot_progress (14);
971 rd_len = rd_data = 0;
975 debug ("## No init Ramdisk\n");
978 *rd_end = rd_data + rd_len;
980 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
986 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
988 * boot_ramdisk_high - relocate init ramdisk
989 * @lmb: pointer to lmb handle, will be used for memory mgmt
990 * @rd_data: ramdisk data start address
991 * @rd_len: ramdisk data length
992 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
993 * start address (after possible relocation)
994 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
995 * end address (after possible relocation)
997 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
998 * variable and if requested ramdisk data is moved to a specified location.
1000 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1001 * start/end addresses if ramdisk image start and len were provided,
1002 * otherwise set initrd_start and initrd_end set to zeros.
1008 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1009 ulong *initrd_start, ulong *initrd_end)
1013 int initrd_copy_to_ram = 1;
1015 if ((s = getenv ("initrd_high")) != NULL) {
1016 /* a value of "no" or a similar string will act like 0,
1017 * turning the "load high" feature off. This is intentional.
1019 initrd_high = simple_strtoul (s, NULL, 16);
1020 if (initrd_high == ~0)
1021 initrd_copy_to_ram = 0;
1023 /* not set, no restrictions to load high */
1028 #ifdef CONFIG_LOGBUFFER
1029 /* Prevent initrd from overwriting logbuffer */
1030 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1033 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1034 initrd_high, initrd_copy_to_ram);
1037 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1038 debug (" in-place initrd\n");
1039 *initrd_start = rd_data;
1040 *initrd_end = rd_data + rd_len;
1041 lmb_reserve(lmb, rd_data, rd_len);
1044 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1046 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
1048 if (*initrd_start == 0) {
1049 puts ("ramdisk - allocation error\n");
1052 show_boot_progress (12);
1054 *initrd_end = *initrd_start + rd_len;
1055 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1056 *initrd_start, *initrd_end);
1058 memmove_wd ((void *)*initrd_start,
1059 (void *)rd_data, rd_len, CHUNKSZ);
1067 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1068 *initrd_start, *initrd_end);
1075 #endif /* defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC) */
1077 #ifdef CONFIG_OF_LIBFDT
1078 static void fdt_error (const char *msg)
1082 puts (" - must RESET the board to recover.\n");
1085 static const image_header_t *image_get_fdt (ulong fdt_addr)
1087 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1089 image_print_contents (fdt_hdr);
1091 puts (" Verifying Checksum ... ");
1092 if (!image_check_hcrc (fdt_hdr)) {
1093 fdt_error ("fdt header checksum invalid");
1097 if (!image_check_dcrc (fdt_hdr)) {
1098 fdt_error ("fdt checksum invalid");
1103 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1104 fdt_error ("uImage is not a fdt");
1107 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1108 fdt_error ("uImage is compressed");
1111 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1112 fdt_error ("uImage data is not a fdt");
1119 * fit_check_fdt - verify FIT format FDT subimage
1120 * @fit_hdr: pointer to the FIT header
1121 * fdt_noffset: FDT subimage node offset within FIT image
1122 * @verify: data CRC verification flag
1124 * fit_check_fdt() verifies integrity of the FDT subimage and from
1125 * specified FIT image.
1131 #if defined(CONFIG_FIT)
1132 static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1134 fit_image_print (fit, fdt_noffset, " ");
1137 puts (" Verifying Hash Integrity ... ");
1138 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1139 fdt_error ("Bad Data Hash");
1145 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1146 fdt_error ("Not a FDT image");
1150 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1151 fdt_error ("FDT image is compressed");
1157 #endif /* CONFIG_FIT */
1159 #ifndef CONFIG_SYS_FDT_PAD
1160 #define CONFIG_SYS_FDT_PAD 0x3000
1164 * boot_relocate_fdt - relocate flat device tree
1165 * @lmb: pointer to lmb handle, will be used for memory mgmt
1166 * @bootmap_base: base address of the bootmap region
1167 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1168 * @of_size: pointer to a ulong variable, will hold fdt length
1170 * boot_relocate_fdt() determines if the of_flat_tree address is within
1171 * the bootmap and if not relocates it into that region
1173 * of_flat_tree and of_size are set to final (after relocation) values
1179 int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
1180 char **of_flat_tree, ulong *of_size)
1182 char *fdt_blob = *of_flat_tree;
1190 if (fdt_check_header (fdt_blob) != 0) {
1191 fdt_error ("image is not a fdt");
1195 #ifndef CONFIG_SYS_NO_FLASH
1196 /* move the blob if it is in flash (set relocate) */
1197 if (addr2info ((ulong)fdt_blob) != NULL)
1202 * The blob needs to be inside the boot mapping.
1204 if (fdt_blob < (char *)bootmap_base)
1207 if ((fdt_blob + *of_size + CONFIG_SYS_FDT_PAD) >=
1208 ((char *)CONFIG_SYS_BOOTMAPSZ + bootmap_base))
1211 /* move flattend device tree if needed */
1216 /* position on a 4K boundary before the alloc_current */
1217 /* Pad the FDT by a specified amount */
1218 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1219 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
1220 (CONFIG_SYS_BOOTMAPSZ + bootmap_base));
1222 if (of_start == 0) {
1223 puts("device tree - allocation error\n");
1227 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
1228 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
1231 printf (" Loading Device Tree to %08lx, end %08lx ... ",
1232 of_start, of_start + of_len - 1);
1234 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
1236 fdt_error ("fdt move failed");
1241 *of_flat_tree = (char *)of_start;
1244 *of_flat_tree = fdt_blob;
1245 of_len = (CONFIG_SYS_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
1246 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
1247 fdt_set_totalsize(*of_flat_tree, of_len);
1252 set_working_fdt_addr(*of_flat_tree);
1260 * boot_get_fdt - main fdt handling routine
1261 * @argc: command argument count
1262 * @argv: command argument list
1263 * @images: pointer to the bootm images structure
1264 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1265 * @of_size: pointer to a ulong variable, will hold fdt length
1267 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1268 * Curently supported are the following ramdisk sources:
1269 * - multicomponent kernel/ramdisk image,
1270 * - commandline provided address of decicated ramdisk image.
1273 * 0, if fdt image was found and valid, or skipped
1274 * of_flat_tree and of_size are set to fdt start address and length if
1275 * fdt image is found and valid
1277 * 1, if fdt image is found but corrupted
1278 * of_flat_tree and of_size are set to 0 if no fdt exists
1280 int boot_get_fdt (int flag, int argc, char *argv[], bootm_headers_t *images,
1281 char **of_flat_tree, ulong *of_size)
1283 const image_header_t *fdt_hdr;
1285 char *fdt_blob = NULL;
1286 ulong image_start, image_end;
1287 ulong load_start, load_end;
1288 #if defined(CONFIG_FIT)
1290 const char *fit_uname_config = NULL;
1291 const char *fit_uname_fdt = NULL;
1299 *of_flat_tree = NULL;
1302 if (argc > 3 || genimg_has_config (images)) {
1303 #if defined(CONFIG_FIT)
1306 * If the FDT blob comes from the FIT image and the
1307 * FIT image address is omitted in the command line
1308 * argument, try to use ramdisk or os FIT image
1309 * address or default load address.
1311 if (images->fit_uname_rd)
1312 default_addr = (ulong)images->fit_hdr_rd;
1313 else if (images->fit_uname_os)
1314 default_addr = (ulong)images->fit_hdr_os;
1316 default_addr = load_addr;
1318 if (fit_parse_conf (argv[3], default_addr,
1319 &fdt_addr, &fit_uname_config)) {
1320 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1321 fit_uname_config, fdt_addr);
1322 } else if (fit_parse_subimage (argv[3], default_addr,
1323 &fdt_addr, &fit_uname_fdt)) {
1324 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1325 fit_uname_fdt, fdt_addr);
1329 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1330 debug ("* fdt: cmdline image address = 0x%08lx\n",
1333 #if defined(CONFIG_FIT)
1335 /* use FIT configuration provided in first bootm
1338 fdt_addr = (ulong)images->fit_hdr_os;
1339 fit_uname_config = images->fit_uname_cfg;
1340 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1341 fit_uname_config, fdt_addr);
1344 * Check whether configuration has FDT blob defined,
1345 * if not quit silently.
1347 fit_hdr = (void *)fdt_addr;
1348 cfg_noffset = fit_conf_get_node (fit_hdr,
1350 if (cfg_noffset < 0) {
1351 debug ("* fdt: no such config\n");
1355 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1357 if (fdt_noffset < 0) {
1358 debug ("* fdt: no fdt in config\n");
1364 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1367 /* copy from dataflash if needed */
1368 fdt_addr = genimg_get_image (fdt_addr);
1371 * Check if there is an FDT image at the
1372 * address provided in the second bootm argument
1373 * check image type, for FIT images get a FIT node.
1375 switch (genimg_get_format ((void *)fdt_addr)) {
1376 case IMAGE_FORMAT_LEGACY:
1377 /* verify fdt_addr points to a valid image header */
1378 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1380 fdt_hdr = image_get_fdt (fdt_addr);
1385 * move image data to the load address,
1386 * make sure we don't overwrite initial image
1388 image_start = (ulong)fdt_hdr;
1389 image_end = image_get_image_end (fdt_hdr);
1391 load_start = image_get_load (fdt_hdr);
1392 load_end = load_start + image_get_data_size (fdt_hdr);
1394 if ((load_start < image_end) && (load_end > image_start)) {
1395 fdt_error ("fdt overwritten");
1399 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1400 image_get_data (fdt_hdr), load_start);
1402 memmove ((void *)load_start,
1403 (void *)image_get_data (fdt_hdr),
1404 image_get_data_size (fdt_hdr));
1406 fdt_blob = (char *)load_start;
1408 case IMAGE_FORMAT_FIT:
1410 * This case will catch both: new uImage format
1411 * (libfdt based) and raw FDT blob (also libfdt
1414 #if defined(CONFIG_FIT)
1415 /* check FDT blob vs FIT blob */
1416 if (fit_check_format ((const void *)fdt_addr)) {
1420 fit_hdr = (void *)fdt_addr;
1421 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1424 if (!fit_uname_fdt) {
1426 * no FDT blob image node unit name,
1427 * try to get config node first. If
1428 * config unit node name is NULL
1429 * fit_conf_get_node() will try to
1430 * find default config node
1432 cfg_noffset = fit_conf_get_node (fit_hdr,
1435 if (cfg_noffset < 0) {
1436 fdt_error ("Could not find configuration node\n");
1440 fit_uname_config = fdt_get_name (fit_hdr,
1442 printf (" Using '%s' configuration\n",
1445 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1447 fit_uname_fdt = fit_get_name (fit_hdr,
1450 /* get FDT component image node offset */
1451 fdt_noffset = fit_image_get_node (fit_hdr,
1454 if (fdt_noffset < 0) {
1455 fdt_error ("Could not find subimage node\n");
1459 printf (" Trying '%s' FDT blob subimage\n",
1462 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1466 /* get ramdisk image data address and length */
1467 if (fit_image_get_data (fit_hdr, fdt_noffset,
1469 fdt_error ("Could not find FDT subimage data");
1473 /* verift that image data is a proper FDT blob */
1474 if (fdt_check_header ((char *)data) != 0) {
1475 fdt_error ("Subimage data is not a FTD");
1480 * move image data to the load address,
1481 * make sure we don't overwrite initial image
1483 image_start = (ulong)fit_hdr;
1484 image_end = fit_get_end (fit_hdr);
1486 if (fit_image_get_load (fit_hdr, fdt_noffset,
1487 &load_start) == 0) {
1488 load_end = load_start + size;
1490 if ((load_start < image_end) &&
1491 (load_end > image_start)) {
1492 fdt_error ("FDT overwritten");
1496 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1497 (ulong)data, load_start);
1499 memmove ((void *)load_start,
1500 (void *)data, size);
1502 fdt_blob = (char *)load_start;
1504 fdt_blob = (char *)data;
1507 images->fit_hdr_fdt = fit_hdr;
1508 images->fit_uname_fdt = fit_uname_fdt;
1509 images->fit_noffset_fdt = fdt_noffset;
1517 fdt_blob = (char *)fdt_addr;
1518 debug ("* fdt: raw FDT blob\n");
1519 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1523 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
1527 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1529 } else if (images->legacy_hdr_valid &&
1530 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1532 ulong fdt_data, fdt_len;
1535 * Now check if we have a legacy multi-component image,
1536 * get second entry data start address and len.
1538 printf ("## Flattened Device Tree from multi "
1539 "component Image at %08lX\n",
1540 (ulong)images->legacy_hdr_os);
1542 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1545 fdt_blob = (char *)fdt_data;
1546 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1548 if (fdt_check_header (fdt_blob) != 0) {
1549 fdt_error ("image is not a fdt");
1553 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
1554 fdt_error ("fdt size != image size");
1558 debug ("## No Flattened Device Tree\n");
1562 debug ("## No Flattened Device Tree\n");
1566 *of_flat_tree = fdt_blob;
1567 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
1568 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
1569 (ulong)*of_flat_tree, *of_size);
1578 #endif /* CONFIG_OF_LIBFDT */
1580 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
1582 * boot_get_cmdline - allocate and initialize kernel cmdline
1583 * @lmb: pointer to lmb handle, will be used for memory mgmt
1584 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1585 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1586 * @bootmap_base: ulong variable, holds offset in physical memory to
1589 * boot_get_cmdline() allocates space for kernel command line below
1590 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1591 * variable is present its contents is copied to allocated kernel
1598 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1604 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1605 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
1607 if (cmdline == NULL)
1610 if ((s = getenv("bootargs")) == NULL)
1615 *cmd_start = (ulong) & cmdline[0];
1616 *cmd_end = *cmd_start + strlen(cmdline);
1618 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1624 * boot_get_kbd - allocate and initialize kernel copy of board info
1625 * @lmb: pointer to lmb handle, will be used for memory mgmt
1626 * @kbd: double pointer to board info data
1627 * @bootmap_base: ulong variable, holds offset in physical memory to
1630 * boot_get_kbd() allocates space for kernel copy of board info data below
1631 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1632 * the current u-boot board info data.
1638 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1640 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1641 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
1647 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1649 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1650 do_bdinfo(NULL, 0, 0, NULL);
1655 #endif /* CONFIG_PPC || CONFIG_M68K */
1656 #endif /* !USE_HOSTCC */
1658 #if defined(CONFIG_FIT)
1659 /*****************************************************************************/
1660 /* New uImage format routines */
1661 /*****************************************************************************/
1663 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1664 ulong *addr, const char **name)
1671 sep = strchr (spec, sepc);
1674 *addr = simple_strtoul (spec, NULL, 16);
1684 * fit_parse_conf - parse FIT configuration spec
1685 * @spec: input string, containing configuration spec
1686 * @add_curr: current image address (to be used as a possible default)
1687 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1689 * @conf_name double pointer to a char, will hold pointer to a configuration
1692 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1693 * where <addr> is a FIT image address that contains configuration
1694 * with a <conf> unit name.
1696 * Address part is optional, and if omitted default add_curr will
1700 * 1 if spec is a valid configuration string,
1701 * addr and conf_name are set accordingly
1704 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1705 ulong *addr, const char **conf_name)
1707 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1711 * fit_parse_subimage - parse FIT subimage spec
1712 * @spec: input string, containing subimage spec
1713 * @add_curr: current image address (to be used as a possible default)
1714 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1716 * @image_name: double pointer to a char, will hold pointer to a subimage name
1718 * fit_parse_subimage() expects subimage spec in the for of
1719 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1720 * subimage with a <subimg> unit name.
1722 * Address part is optional, and if omitted default add_curr will
1726 * 1 if spec is a valid subimage string,
1727 * addr and image_name are set accordingly
1730 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1731 ulong *addr, const char **image_name)
1733 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1735 #endif /* !USE_HOSTCC */
1737 static void fit_get_debug (const void *fit, int noffset,
1738 char *prop_name, int err)
1740 debug ("Can't get '%s' property from FIT 0x%08lx, "
1741 "node: offset %d, name %s (%s)\n",
1742 prop_name, (ulong)fit, noffset,
1743 fit_get_name (fit, noffset, NULL),
1744 fdt_strerror (err));
1748 * fit_print_contents - prints out the contents of the FIT format image
1749 * @fit: pointer to the FIT format image header
1750 * @p: pointer to prefix string
1752 * fit_print_contents() formats a multi line FIT image contents description.
1753 * The routine prints out FIT image properties (root node level) follwed by
1754 * the details of each component image.
1757 * no returned results
1759 void fit_print_contents (const void *fit)
1770 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1780 /* Root node properties */
1781 ret = fit_get_desc (fit, 0, &desc);
1782 printf ("%sFIT description: ", p);
1784 printf ("unavailable\n");
1786 printf ("%s\n", desc);
1788 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1789 ret = fit_get_timestamp (fit, 0, ×tamp);
1790 printf ("%sCreated: ", p);
1792 printf ("unavailable\n");
1794 genimg_print_time (timestamp);
1797 /* Find images parent node offset */
1798 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1799 if (images_noffset < 0) {
1800 printf ("Can't find images parent node '%s' (%s)\n",
1801 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1805 /* Process its subnodes, print out component images details */
1806 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1807 (noffset >= 0) && (ndepth > 0);
1808 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1811 * Direct child node of the images parent node,
1812 * i.e. component image node.
1814 printf ("%s Image %u (%s)\n", p, count++,
1815 fit_get_name(fit, noffset, NULL));
1817 fit_image_print (fit, noffset, p);
1821 /* Find configurations parent node offset */
1822 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1823 if (confs_noffset < 0) {
1824 debug ("Can't get configurations parent node '%s' (%s)\n",
1825 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1829 /* get default configuration unit name from default property */
1830 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1832 printf ("%s Default Configuration: '%s'\n", p, uname);
1834 /* Process its subnodes, print out configurations details */
1835 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1836 (noffset >= 0) && (ndepth > 0);
1837 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1840 * Direct child node of the configurations parent node,
1841 * i.e. configuration node.
1843 printf ("%s Configuration %u (%s)\n", p, count++,
1844 fit_get_name(fit, noffset, NULL));
1846 fit_conf_print (fit, noffset, p);
1852 * fit_image_print - prints out the FIT component image details
1853 * @fit: pointer to the FIT format image header
1854 * @image_noffset: offset of the component image node
1855 * @p: pointer to prefix string
1857 * fit_image_print() lists all mandatory properies for the processed component
1858 * image. If present, hash nodes are printed out as well. Load
1859 * address for images of type firmware is also printed out. Since the load
1860 * address is not mandatory for firmware images, it will be output as
1861 * "unavailable" when not present.
1864 * no returned results
1866 void fit_image_print (const void *fit, int image_noffset, const char *p)
1869 uint8_t type, arch, os, comp;
1877 /* Mandatory properties */
1878 ret = fit_get_desc (fit, image_noffset, &desc);
1879 printf ("%s Description: ", p);
1881 printf ("unavailable\n");
1883 printf ("%s\n", desc);
1885 fit_image_get_type (fit, image_noffset, &type);
1886 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1888 fit_image_get_comp (fit, image_noffset, &comp);
1889 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1891 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1894 printf ("%s Data Start: ", p);
1896 printf ("unavailable\n");
1898 printf ("0x%08lx\n", (ulong)data);
1901 printf ("%s Data Size: ", p);
1903 printf ("unavailable\n");
1905 genimg_print_size (size);
1907 /* Remaining, type dependent properties */
1908 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1909 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1910 (type == IH_TYPE_FLATDT)) {
1911 fit_image_get_arch (fit, image_noffset, &arch);
1912 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1915 if (type == IH_TYPE_KERNEL) {
1916 fit_image_get_os (fit, image_noffset, &os);
1917 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1920 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1921 (type == IH_TYPE_FIRMWARE)) {
1922 ret = fit_image_get_load (fit, image_noffset, &load);
1923 printf ("%s Load Address: ", p);
1925 printf ("unavailable\n");
1927 printf ("0x%08lx\n", load);
1930 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1931 fit_image_get_entry (fit, image_noffset, &entry);
1932 printf ("%s Entry Point: ", p);
1934 printf ("unavailable\n");
1936 printf ("0x%08lx\n", entry);
1939 /* Process all hash subnodes of the component image node */
1940 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1941 (noffset >= 0) && (ndepth > 0);
1942 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1944 /* Direct child node of the component image node */
1945 fit_image_print_hash (fit, noffset, p);
1951 * fit_image_print_hash - prints out the hash node details
1952 * @fit: pointer to the FIT format image header
1953 * @noffset: offset of the hash node
1954 * @p: pointer to prefix string
1956 * fit_image_print_hash() lists properies for the processed hash node
1959 * no returned results
1961 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1969 * Check subnode name, must be equal to "hash".
1970 * Multiple hash nodes require unique unit node
1971 * names, e.g. hash@1, hash@2, etc.
1973 if (strncmp (fit_get_name(fit, noffset, NULL),
1975 strlen(FIT_HASH_NODENAME)) != 0)
1978 debug ("%s Hash node: '%s'\n", p,
1979 fit_get_name (fit, noffset, NULL));
1981 printf ("%s Hash algo: ", p);
1982 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1983 printf ("invalid/unsupported\n");
1986 printf ("%s\n", algo);
1988 ret = fit_image_hash_get_value (fit, noffset, &value,
1990 printf ("%s Hash value: ", p);
1992 printf ("unavailable\n");
1994 for (i = 0; i < value_len; i++)
1995 printf ("%02x", value[i]);
1999 debug ("%s Hash len: %d\n", p, value_len);
2003 * fit_get_desc - get node description property
2004 * @fit: pointer to the FIT format image header
2005 * @noffset: node offset
2006 * @desc: double pointer to the char, will hold pointer to the descrption
2008 * fit_get_desc() reads description property from a given node, if
2009 * description is found pointer to it is returened in third call argument.
2015 int fit_get_desc (const void *fit, int noffset, char **desc)
2019 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2020 if (*desc == NULL) {
2021 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2029 * fit_get_timestamp - get node timestamp property
2030 * @fit: pointer to the FIT format image header
2031 * @noffset: node offset
2032 * @timestamp: pointer to the time_t, will hold read timestamp
2034 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2035 * is found and has a correct size its value is retured in third call
2040 * -1, on property read failure
2041 * -2, on wrong timestamp size
2043 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2048 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2050 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2053 if (len != sizeof (uint32_t)) {
2054 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2058 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2063 * fit_image_get_node - get node offset for component image of a given unit name
2064 * @fit: pointer to the FIT format image header
2065 * @image_uname: component image node unit name
2067 * fit_image_get_node() finds a component image (withing the '/images'
2068 * node) of a provided unit name. If image is found its node offset is
2069 * returned to the caller.
2072 * image node offset when found (>=0)
2073 * negative number on failure (FDT_ERR_* code)
2075 int fit_image_get_node (const void *fit, const char *image_uname)
2077 int noffset, images_noffset;
2079 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2080 if (images_noffset < 0) {
2081 debug ("Can't find images parent node '%s' (%s)\n",
2082 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2083 return images_noffset;
2086 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2088 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2089 image_uname, fdt_strerror (noffset));
2096 * fit_image_get_os - get os id for a given component image node
2097 * @fit: pointer to the FIT format image header
2098 * @noffset: component image node offset
2099 * @os: pointer to the uint8_t, will hold os numeric id
2101 * fit_image_get_os() finds os property in a given component image node.
2102 * If the property is found, its (string) value is translated to the numeric
2103 * id which is returned to the caller.
2109 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2114 /* Get OS name from property data */
2115 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2117 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2122 /* Translate OS name to id */
2123 *os = genimg_get_os_id (data);
2128 * fit_image_get_arch - get arch id for a given component image node
2129 * @fit: pointer to the FIT format image header
2130 * @noffset: component image node offset
2131 * @arch: pointer to the uint8_t, will hold arch numeric id
2133 * fit_image_get_arch() finds arch property in a given component image node.
2134 * If the property is found, its (string) value is translated to the numeric
2135 * id which is returned to the caller.
2141 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2146 /* Get architecture name from property data */
2147 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2149 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2154 /* Translate architecture name to id */
2155 *arch = genimg_get_arch_id (data);
2160 * fit_image_get_type - get type id for a given component image node
2161 * @fit: pointer to the FIT format image header
2162 * @noffset: component image node offset
2163 * @type: pointer to the uint8_t, will hold type numeric id
2165 * fit_image_get_type() finds type property in a given component image node.
2166 * If the property is found, its (string) value is translated to the numeric
2167 * id which is returned to the caller.
2173 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2178 /* Get image type name from property data */
2179 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2181 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2186 /* Translate image type name to id */
2187 *type = genimg_get_type_id (data);
2192 * fit_image_get_comp - get comp id for a given component image node
2193 * @fit: pointer to the FIT format image header
2194 * @noffset: component image node offset
2195 * @comp: pointer to the uint8_t, will hold comp numeric id
2197 * fit_image_get_comp() finds comp property in a given component image node.
2198 * If the property is found, its (string) value is translated to the numeric
2199 * id which is returned to the caller.
2205 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2210 /* Get compression name from property data */
2211 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2213 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2218 /* Translate compression name to id */
2219 *comp = genimg_get_comp_id (data);
2224 * fit_image_get_load - get load address property for a given component image node
2225 * @fit: pointer to the FIT format image header
2226 * @noffset: component image node offset
2227 * @load: pointer to the uint32_t, will hold load address
2229 * fit_image_get_load() finds load address property in a given component image node.
2230 * If the property is found, its value is returned to the caller.
2236 int fit_image_get_load (const void *fit, int noffset, ulong *load)
2239 const uint32_t *data;
2241 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2243 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2247 *load = uimage_to_cpu (*data);
2252 * fit_image_get_entry - get entry point address property for a given component image node
2253 * @fit: pointer to the FIT format image header
2254 * @noffset: component image node offset
2255 * @entry: pointer to the uint32_t, will hold entry point address
2257 * fit_image_get_entry() finds entry point address property in a given component image node.
2258 * If the property is found, its value is returned to the caller.
2264 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2267 const uint32_t *data;
2269 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2271 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2275 *entry = uimage_to_cpu (*data);
2280 * fit_image_get_data - get data property and its size for a given component image node
2281 * @fit: pointer to the FIT format image header
2282 * @noffset: component image node offset
2283 * @data: double pointer to void, will hold data property's data address
2284 * @size: pointer to size_t, will hold data property's data size
2286 * fit_image_get_data() finds data property in a given component image node.
2287 * If the property is found its data start address and size are returned to
2294 int fit_image_get_data (const void *fit, int noffset,
2295 const void **data, size_t *size)
2299 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2300 if (*data == NULL) {
2301 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2311 * fit_image_hash_get_algo - get hash algorithm name
2312 * @fit: pointer to the FIT format image header
2313 * @noffset: hash node offset
2314 * @algo: double pointer to char, will hold pointer to the algorithm name
2316 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2317 * If the property is found its data start address is returned to the caller.
2323 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2327 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2328 if (*algo == NULL) {
2329 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2337 * fit_image_hash_get_value - get hash value and length
2338 * @fit: pointer to the FIT format image header
2339 * @noffset: hash node offset
2340 * @value: double pointer to uint8_t, will hold address of a hash value data
2341 * @value_len: pointer to an int, will hold hash data length
2343 * fit_image_hash_get_value() finds hash value property in a given hash node.
2344 * If the property is found its data start address and size are returned to
2351 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2356 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2357 if (*value == NULL) {
2358 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2368 * fit_set_timestamp - set node timestamp property
2369 * @fit: pointer to the FIT format image header
2370 * @noffset: node offset
2371 * @timestamp: timestamp value to be set
2373 * fit_set_timestamp() attempts to set timestamp property in the requested
2374 * node and returns operation status to the caller.
2378 * -1, on property read failure
2380 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2385 t = cpu_to_uimage (timestamp);
2386 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2389 printf ("Can't set '%s' property for '%s' node (%s)\n",
2390 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2391 fdt_strerror (ret));
2399 * calculate_hash - calculate and return hash for provided input data
2400 * @data: pointer to the input data
2401 * @data_len: data length
2402 * @algo: requested hash algorithm
2403 * @value: pointer to the char, will hold hash value data (caller must
2404 * allocate enough free space)
2405 * value_len: length of the calculated hash
2407 * calculate_hash() computes input data hash according to the requested algorithm.
2408 * Resulting hash value is placed in caller provided 'value' buffer, length
2409 * of the calculated hash is returned via value_len pointer argument.
2413 * -1, when algo is unsupported
2415 static int calculate_hash (const void *data, int data_len, const char *algo,
2416 uint8_t *value, int *value_len)
2418 if (strcmp (algo, "crc32") == 0 ) {
2419 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2421 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2423 } else if (strcmp (algo, "sha1") == 0 ) {
2424 sha1_csum_wd ((unsigned char *) data, data_len,
2425 (unsigned char *) value, CHUNKSZ_SHA1);
2427 } else if (strcmp (algo, "md5") == 0 ) {
2428 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2431 debug ("Unsupported hash alogrithm\n");
2439 * fit_set_hashes - process FIT component image nodes and calculate hashes
2440 * @fit: pointer to the FIT format image header
2442 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2443 * Hashes are calculated for all component images which have hash subnodes
2444 * with algorithm property set to one of the supported hash algorithms.
2448 * libfdt error code, on failure
2450 int fit_set_hashes (void *fit)
2457 /* Find images parent node offset */
2458 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2459 if (images_noffset < 0) {
2460 printf ("Can't find images parent node '%s' (%s)\n",
2461 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2462 return images_noffset;
2465 /* Process its subnodes, print out component images details */
2466 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2467 (noffset >= 0) && (ndepth > 0);
2468 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2471 * Direct child node of the images parent node,
2472 * i.e. component image node.
2474 ret = fit_image_set_hashes (fit, noffset);
2484 * fit_image_set_hashes - calculate/set hashes for given component image node
2485 * @fit: pointer to the FIT format image header
2486 * @image_noffset: requested component image node
2488 * fit_image_set_hashes() adds hash values for an component image node. All
2489 * existing hash subnodes are checked, if algorithm property is set to one of
2490 * the supported hash algorithms, hash value is computed and corresponding
2491 * hash node property is set, for example:
2493 * Input component image node structure:
2495 * o image@1 (at image_noffset)
2496 * | - data = [binary data]
2500 * Output component image node structure:
2502 * o image@1 (at image_noffset)
2503 * | - data = [binary data]
2506 * |- value = sha1(data)
2512 int fit_image_set_hashes (void *fit, int image_noffset)
2517 uint8_t value[FIT_MAX_HASH_LEN];
2522 /* Get image data and data length */
2523 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2524 printf ("Can't get image data/size\n");
2528 /* Process all hash subnodes of the component image node */
2529 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2530 (noffset >= 0) && (ndepth > 0);
2531 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2533 /* Direct child node of the component image node */
2536 * Check subnode name, must be equal to "hash".
2537 * Multiple hash nodes require unique unit node
2538 * names, e.g. hash@1, hash@2, etc.
2540 if (strncmp (fit_get_name(fit, noffset, NULL),
2542 strlen(FIT_HASH_NODENAME)) != 0) {
2543 /* Not a hash subnode, skip it */
2547 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2548 printf ("Can't get hash algo property for "
2549 "'%s' hash node in '%s' image node\n",
2550 fit_get_name (fit, noffset, NULL),
2551 fit_get_name (fit, image_noffset, NULL));
2555 if (calculate_hash (data, size, algo, value, &value_len)) {
2556 printf ("Unsupported hash algorithm (%s) for "
2557 "'%s' hash node in '%s' image node\n",
2558 algo, fit_get_name (fit, noffset, NULL),
2559 fit_get_name (fit, image_noffset, NULL));
2563 if (fit_image_hash_set_value (fit, noffset, value,
2565 printf ("Can't set hash value for "
2566 "'%s' hash node in '%s' image node\n",
2567 fit_get_name (fit, noffset, NULL),
2568 fit_get_name (fit, image_noffset, NULL));
2578 * fit_image_hash_set_value - set hash value in requested has node
2579 * @fit: pointer to the FIT format image header
2580 * @noffset: hash node offset
2581 * @value: hash value to be set
2582 * @value_len: hash value length
2584 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2585 * given and returns operation status to the caller.
2591 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2596 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2598 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2599 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2600 fdt_strerror (ret));
2606 #endif /* USE_HOSTCC */
2609 * fit_image_check_hashes - verify data intergity
2610 * @fit: pointer to the FIT format image header
2611 * @image_noffset: component image node offset
2613 * fit_image_check_hashes() goes over component image hash nodes,
2614 * re-calculates each data hash and compares with the value stored in hash
2618 * 1, if all hashes are valid
2619 * 0, otherwise (or on error)
2621 int fit_image_check_hashes (const void *fit, int image_noffset)
2628 uint8_t value[FIT_MAX_HASH_LEN];
2634 /* Get image data and data length */
2635 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2636 printf ("Can't get image data/size\n");
2640 /* Process all hash subnodes of the component image node */
2641 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2642 (noffset >= 0) && (ndepth > 0);
2643 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2645 /* Direct child node of the component image node */
2648 * Check subnode name, must be equal to "hash".
2649 * Multiple hash nodes require unique unit node
2650 * names, e.g. hash@1, hash@2, etc.
2652 if (strncmp (fit_get_name(fit, noffset, NULL),
2654 strlen(FIT_HASH_NODENAME)) != 0)
2657 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2658 err_msg = " error!\nCan't get hash algo "
2662 printf ("%s", algo);
2664 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2666 err_msg = " error!\nCan't get hash value "
2671 if (calculate_hash (data, size, algo, value, &value_len)) {
2672 err_msg = " error!\nUnsupported hash algorithm";
2676 if (value_len != fit_value_len) {
2677 err_msg = " error !\nBad hash value len";
2679 } else if (memcmp (value, fit_value, value_len) != 0) {
2680 err_msg = " error!\nBad hash value";
2690 printf ("%s for '%s' hash node in '%s' image node\n",
2691 err_msg, fit_get_name (fit, noffset, NULL),
2692 fit_get_name (fit, image_noffset, NULL));
2697 * fit_all_image_check_hashes - verify data intergity for all images
2698 * @fit: pointer to the FIT format image header
2700 * fit_all_image_check_hashes() goes over all images in the FIT and
2701 * for every images checks if all it's hashes are valid.
2704 * 1, if all hashes of all images are valid
2705 * 0, otherwise (or on error)
2707 int fit_all_image_check_hashes (const void *fit)
2714 /* Find images parent node offset */
2715 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2716 if (images_noffset < 0) {
2717 printf ("Can't find images parent node '%s' (%s)\n",
2718 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2722 /* Process all image subnodes, check hashes for each */
2723 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2725 for (ndepth = 0, count = 0,
2726 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2727 (noffset >= 0) && (ndepth > 0);
2728 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2731 * Direct child node of the images parent node,
2732 * i.e. component image node.
2734 printf (" Hash(es) for Image %u (%s): ", count++,
2735 fit_get_name (fit, noffset, NULL));
2737 if (!fit_image_check_hashes (fit, noffset))
2746 * fit_image_check_os - check whether image node is of a given os type
2747 * @fit: pointer to the FIT format image header
2748 * @noffset: component image node offset
2749 * @os: requested image os
2751 * fit_image_check_os() reads image os property and compares its numeric
2752 * id with the requested os. Comparison result is returned to the caller.
2755 * 1 if image is of given os type
2756 * 0 otherwise (or on error)
2758 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2762 if (fit_image_get_os (fit, noffset, &image_os))
2764 return (os == image_os);
2768 * fit_image_check_arch - check whether image node is of a given arch
2769 * @fit: pointer to the FIT format image header
2770 * @noffset: component image node offset
2771 * @arch: requested imagearch
2773 * fit_image_check_arch() reads image arch property and compares its numeric
2774 * id with the requested arch. Comparison result is returned to the caller.
2777 * 1 if image is of given arch
2778 * 0 otherwise (or on error)
2780 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2784 if (fit_image_get_arch (fit, noffset, &image_arch))
2786 return (arch == image_arch);
2790 * fit_image_check_type - check whether image node is of a given type
2791 * @fit: pointer to the FIT format image header
2792 * @noffset: component image node offset
2793 * @type: requested image type
2795 * fit_image_check_type() reads image type property and compares its numeric
2796 * id with the requested type. Comparison result is returned to the caller.
2799 * 1 if image is of given type
2800 * 0 otherwise (or on error)
2802 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2806 if (fit_image_get_type (fit, noffset, &image_type))
2808 return (type == image_type);
2812 * fit_image_check_comp - check whether image node uses given compression
2813 * @fit: pointer to the FIT format image header
2814 * @noffset: component image node offset
2815 * @comp: requested image compression type
2817 * fit_image_check_comp() reads image compression property and compares its
2818 * numeric id with the requested compression type. Comparison result is
2819 * returned to the caller.
2822 * 1 if image uses requested compression
2823 * 0 otherwise (or on error)
2825 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2829 if (fit_image_get_comp (fit, noffset, &image_comp))
2831 return (comp == image_comp);
2835 * fit_check_format - sanity check FIT image format
2836 * @fit: pointer to the FIT format image header
2838 * fit_check_format() runs a basic sanity FIT image verification.
2839 * Routine checks for mandatory properties, nodes, etc.
2845 int fit_check_format (const void *fit)
2847 /* mandatory / node 'description' property */
2848 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2849 debug ("Wrong FIT format: no description\n");
2853 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2854 /* mandatory / node 'timestamp' property */
2855 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2856 debug ("Wrong FIT format: no timestamp\n");
2861 /* mandatory subimages parent '/images' node */
2862 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2863 debug ("Wrong FIT format: no images parent node\n");
2871 * fit_conf_get_node - get node offset for configuration of a given unit name
2872 * @fit: pointer to the FIT format image header
2873 * @conf_uname: configuration node unit name
2875 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2876 * parant node) of a provided unit name. If configuration is found its node offset
2877 * is returned to the caller.
2879 * When NULL is provided in second argument fit_conf_get_node() will search
2880 * for a default configuration node instead. Default configuration node unit name
2881 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2884 * configuration node offset when found (>=0)
2885 * negative number on failure (FDT_ERR_* code)
2887 int fit_conf_get_node (const void *fit, const char *conf_uname)
2889 int noffset, confs_noffset;
2892 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2893 if (confs_noffset < 0) {
2894 debug ("Can't find configurations parent node '%s' (%s)\n",
2895 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2896 return confs_noffset;
2899 if (conf_uname == NULL) {
2900 /* get configuration unit name from the default property */
2901 debug ("No configuration specified, trying default...\n");
2902 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2903 if (conf_uname == NULL) {
2904 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2907 debug ("Found default configuration: '%s'\n", conf_uname);
2910 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2912 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2913 conf_uname, fdt_strerror (noffset));
2919 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2920 const char *prop_name)
2925 /* get kernel image unit name from configuration kernel property */
2926 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2930 return fit_image_get_node (fit, uname);
2934 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2935 * a given configuration
2936 * @fit: pointer to the FIT format image header
2937 * @noffset: configuration node offset
2939 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2940 * configuration FIT_KERNEL_PROP property and translates it to the node
2944 * image node offset when found (>=0)
2945 * negative number on failure (FDT_ERR_* code)
2947 int fit_conf_get_kernel_node (const void *fit, int noffset)
2949 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2953 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2954 * a given configuration
2955 * @fit: pointer to the FIT format image header
2956 * @noffset: configuration node offset
2958 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2959 * configuration FIT_KERNEL_PROP property and translates it to the node
2963 * image node offset when found (>=0)
2964 * negative number on failure (FDT_ERR_* code)
2966 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2968 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2972 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2973 * a given configuration
2974 * @fit: pointer to the FIT format image header
2975 * @noffset: configuration node offset
2977 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2978 * configuration FIT_KERNEL_PROP property and translates it to the node
2982 * image node offset when found (>=0)
2983 * negative number on failure (FDT_ERR_* code)
2985 int fit_conf_get_fdt_node (const void *fit, int noffset)
2987 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2991 * fit_conf_print - prints out the FIT configuration details
2992 * @fit: pointer to the FIT format image header
2993 * @noffset: offset of the configuration node
2994 * @p: pointer to prefix string
2996 * fit_conf_print() lists all mandatory properies for the processed
2997 * configuration node.
3000 * no returned results
3002 void fit_conf_print (const void *fit, int noffset, const char *p)
3008 /* Mandatory properties */
3009 ret = fit_get_desc (fit, noffset, &desc);
3010 printf ("%s Description: ", p);
3012 printf ("unavailable\n");
3014 printf ("%s\n", desc);
3016 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3017 printf ("%s Kernel: ", p);
3019 printf ("unavailable\n");
3021 printf ("%s\n", uname);
3023 /* Optional properties */
3024 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3026 printf ("%s Init Ramdisk: %s\n", p, uname);
3028 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3030 printf ("%s FDT: %s\n", p, uname);
3034 * fit_check_ramdisk - verify FIT format ramdisk subimage
3035 * @fit_hdr: pointer to the FIT ramdisk header
3036 * @rd_noffset: ramdisk subimage node offset within FIT image
3037 * @arch: requested ramdisk image architecture type
3038 * @verify: data CRC verification flag
3040 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3041 * specified FIT image.
3048 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3050 fit_image_print (fit, rd_noffset, " ");
3053 puts (" Verifying Hash Integrity ... ");
3054 if (!fit_image_check_hashes (fit, rd_noffset)) {
3055 puts ("Bad Data Hash\n");
3056 show_boot_progress (-125);
3062 show_boot_progress (126);
3063 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3064 !fit_image_check_arch (fit, rd_noffset, arch) ||
3065 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3066 printf ("No Linux %s Ramdisk Image\n",
3067 genimg_get_arch_name(arch));
3068 show_boot_progress (-126);
3072 show_boot_progress (127);
3075 #endif /* USE_HOSTCC */
3076 #endif /* CONFIG_FIT */