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 * const 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_NIOS2, "nios2", "NIOS II", },
88 { IH_ARCH_PPC, "powerpc", "PowerPC", },
89 { IH_ARCH_PPC, "ppc", "PowerPC", },
90 { IH_ARCH_S390, "s390", "IBM S390", },
91 { IH_ARCH_SH, "sh", "SuperH", },
92 { IH_ARCH_SPARC, "sparc", "SPARC", },
93 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
94 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
95 { IH_ARCH_AVR32, "avr32", "AVR32", },
99 static table_entry_t uimage_os[] = {
100 { IH_OS_INVALID, NULL, "Invalid OS", },
101 { IH_OS_LINUX, "linux", "Linux", },
102 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
103 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
105 { IH_OS_NETBSD, "netbsd", "NetBSD", },
106 { IH_OS_RTEMS, "rtems", "RTEMS", },
107 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
108 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
109 { IH_OS_QNX, "qnx", "QNX", },
110 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
112 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
113 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
116 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
117 { IH_OS_DELL, "dell", "Dell", },
118 { IH_OS_ESIX, "esix", "Esix", },
119 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
120 { IH_OS_IRIX, "irix", "Irix", },
121 { IH_OS_NCR, "ncr", "NCR", },
122 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
123 { IH_OS_PSOS, "psos", "pSOS", },
124 { IH_OS_SCO, "sco", "SCO", },
125 { IH_OS_SOLARIS, "solaris", "Solaris", },
126 { IH_OS_SVR4, "svr4", "SVR4", },
131 static table_entry_t uimage_type[] = {
132 { IH_TYPE_INVALID, NULL, "Invalid Image", },
133 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
134 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
135 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
136 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
137 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
138 { IH_TYPE_SCRIPT, "script", "Script", },
139 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
140 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
141 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
142 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
146 static table_entry_t uimage_comp[] = {
147 { IH_COMP_NONE, "none", "uncompressed", },
148 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
149 { IH_COMP_GZIP, "gzip", "gzip compressed", },
150 { IH_COMP_LZMA, "lzma", "lzma compressed", },
151 { IH_COMP_LZO, "lzo", "lzo compressed", },
155 uint32_t crc32 (uint32_t, const unsigned char *, uint);
156 uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
157 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
158 static void genimg_print_time (time_t timestamp);
161 /*****************************************************************************/
162 /* Legacy format routines */
163 /*****************************************************************************/
164 int image_check_hcrc (const image_header_t *hdr)
167 ulong len = image_get_header_size ();
168 image_header_t header;
170 /* Copy header so we can blank CRC field for re-calculation */
171 memmove (&header, (char *)hdr, image_get_header_size ());
172 image_set_hcrc (&header, 0);
174 hcrc = crc32 (0, (unsigned char *)&header, len);
176 return (hcrc == image_get_hcrc (hdr));
179 int image_check_dcrc (const image_header_t *hdr)
181 ulong data = image_get_data (hdr);
182 ulong len = image_get_data_size (hdr);
183 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
185 return (dcrc == image_get_dcrc (hdr));
189 * image_multi_count - get component (sub-image) count
190 * @hdr: pointer to the header of the multi component image
192 * image_multi_count() returns number of components in a multi
195 * Note: no checking of the image type is done, caller must pass
196 * a valid multi component image.
199 * number of components
201 ulong image_multi_count (const image_header_t *hdr)
206 /* get start of the image payload, which in case of multi
207 * component images that points to a table of component sizes */
208 size = (uint32_t *)image_get_data (hdr);
210 /* count non empty slots */
211 for (i = 0; size[i]; ++i)
218 * image_multi_getimg - get component data address and size
219 * @hdr: pointer to the header of the multi component image
220 * @idx: index of the requested component
221 * @data: pointer to a ulong variable, will hold component data address
222 * @len: pointer to a ulong variable, will hold component size
224 * image_multi_getimg() returns size and data address for the requested
225 * component in a multi component image.
227 * Note: no checking of the image type is done, caller must pass
228 * a valid multi component image.
231 * data address and size of the component, if idx is valid
232 * 0 in data and len, if idx is out of range
234 void image_multi_getimg (const image_header_t *hdr, ulong idx,
235 ulong *data, ulong *len)
239 ulong offset, count, img_data;
241 /* get number of component */
242 count = image_multi_count (hdr);
244 /* get start of the image payload, which in case of multi
245 * component images that points to a table of component sizes */
246 size = (uint32_t *)image_get_data (hdr);
248 /* get address of the proper component data start, which means
249 * skipping sizes table (add 1 for last, null entry) */
250 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
253 *len = uimage_to_cpu (size[idx]);
256 /* go over all indices preceding requested component idx */
257 for (i = 0; i < idx; i++) {
258 /* add up i-th component size, rounding up to 4 bytes */
259 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
262 /* calculate idx-th component data address */
263 *data = img_data + offset;
270 static void image_print_type (const image_header_t *hdr)
272 const char *os, *arch, *type, *comp;
274 os = genimg_get_os_name (image_get_os (hdr));
275 arch = genimg_get_arch_name (image_get_arch (hdr));
276 type = genimg_get_type_name (image_get_type (hdr));
277 comp = genimg_get_comp_name (image_get_comp (hdr));
279 printf ("%s %s %s (%s)\n", arch, os, type, comp);
283 * image_print_contents - prints out the contents of the legacy format image
284 * @ptr: pointer to the legacy format image header
285 * @p: pointer to prefix string
287 * image_print_contents() formats a multi line legacy image contents description.
288 * The routine prints out all header fields followed by the size/offset data
289 * for MULTI/SCRIPT images.
292 * no returned results
294 void image_print_contents (const void *ptr)
296 const image_header_t *hdr = (const image_header_t *)ptr;
305 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
306 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
307 printf ("%sCreated: ", p);
308 genimg_print_time ((time_t)image_get_time (hdr));
310 printf ("%sImage Type: ", p);
311 image_print_type (hdr);
312 printf ("%sData Size: ", p);
313 genimg_print_size (image_get_data_size (hdr));
314 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
315 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
317 if (image_check_type (hdr, IH_TYPE_MULTI) ||
318 image_check_type (hdr, IH_TYPE_SCRIPT)) {
321 ulong count = image_multi_count (hdr);
323 printf ("%sContents:\n", p);
324 for (i = 0; i < count; i++) {
325 image_multi_getimg (hdr, i, &data, &len);
327 printf ("%s Image %d: ", p, i);
328 genimg_print_size (len);
330 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
332 * the user may need to know offsets
333 * if planning to do something with
336 printf ("%s Offset = 0x%08lx\n", p, data);
345 * image_get_ramdisk - get and verify ramdisk image
346 * @rd_addr: ramdisk image start address
347 * @arch: expected ramdisk architecture
348 * @verify: checksum verification flag
350 * image_get_ramdisk() returns a pointer to the verified ramdisk image
351 * header. Routine receives image start address and expected architecture
352 * flag. Verification done covers data and header integrity and os/type/arch
355 * If dataflash support is enabled routine checks for dataflash addresses
356 * and handles required dataflash reads.
359 * pointer to a ramdisk image header, if image was found and valid
360 * otherwise, return NULL
362 static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
365 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
367 if (!image_check_magic (rd_hdr)) {
368 puts ("Bad Magic Number\n");
369 show_boot_progress (-10);
373 if (!image_check_hcrc (rd_hdr)) {
374 puts ("Bad Header Checksum\n");
375 show_boot_progress (-11);
379 show_boot_progress (10);
380 image_print_contents (rd_hdr);
383 puts(" Verifying Checksum ... ");
384 if (!image_check_dcrc (rd_hdr)) {
385 puts ("Bad Data CRC\n");
386 show_boot_progress (-12);
392 show_boot_progress (11);
394 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
395 !image_check_arch (rd_hdr, arch) ||
396 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
397 printf ("No Linux %s Ramdisk Image\n",
398 genimg_get_arch_name(arch));
399 show_boot_progress (-13);
405 #endif /* !USE_HOSTCC */
407 /*****************************************************************************/
408 /* Shared dual-format routines */
409 /*****************************************************************************/
411 int getenv_yesno (char *var)
413 char *s = getenv (var);
414 return (s && (*s == 'n')) ? 0 : 1;
417 ulong getenv_bootm_low(void)
419 char *s = getenv ("bootm_low");
421 ulong tmp = simple_strtoul (s, NULL, 16);
425 #if defined(CONFIG_SYS_SDRAM_BASE)
426 return CONFIG_SYS_SDRAM_BASE;
427 #elif defined(CONFIG_ARM)
428 return gd->bd->bi_dram[0].start;
434 phys_size_t getenv_bootm_size(void)
436 char *s = getenv ("bootm_size");
439 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
443 #if defined(CONFIG_ARM)
444 return gd->bd->bi_dram[0].size;
446 return gd->bd->bi_memsize;
450 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
455 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
457 size_t tail = (len > chunksz) ? chunksz : len;
459 memmove (to, from, tail);
464 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
465 memmove (to, from, len);
466 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
468 #endif /* !USE_HOSTCC */
470 void genimg_print_size (uint32_t size)
473 printf ("%d Bytes = ", size);
474 print_size (size, "\n");
476 printf ("%d Bytes = %.2f kB = %.2f MB\n",
477 size, (double)size / 1.024e3,
478 (double)size / 1.048576e6);
482 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
483 static void genimg_print_time (time_t timestamp)
488 to_tm (timestamp, &tm);
489 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
490 tm.tm_year, tm.tm_mon, tm.tm_mday,
491 tm.tm_hour, tm.tm_min, tm.tm_sec);
493 printf ("%s", ctime(×tamp));
496 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
499 * get_table_entry_name - translate entry id to long name
500 * @table: pointer to a translation table for entries of a specific type
501 * @msg: message to be returned when translation fails
502 * @id: entry id to be translated
504 * get_table_entry_name() will go over translation table trying to find
505 * entry that matches given id. If matching entry is found, its long
506 * name is returned to the caller.
509 * long entry name if translation succeeds
512 char *get_table_entry_name (table_entry_t *table, char *msg, int id)
514 for (; table->id >= 0; ++table) {
516 #if defined(USE_HOSTCC) || defined(CONFIG_RELOC_FIXUP_WORKS)
519 return table->lname + gd->reloc_off;
525 const char *genimg_get_os_name (uint8_t os)
527 return (get_table_entry_name (uimage_os, "Unknown OS", os));
530 const char *genimg_get_arch_name (uint8_t arch)
532 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
535 const char *genimg_get_type_name (uint8_t type)
537 return (get_table_entry_name (uimage_type, "Unknown Image", type));
540 const char *genimg_get_comp_name (uint8_t comp)
542 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
546 * get_table_entry_id - translate short entry name to id
547 * @table: pointer to a translation table for entries of a specific type
548 * @table_name: to be used in case of error
549 * @name: entry short name to be translated
551 * get_table_entry_id() will go over translation table trying to find
552 * entry that matches given short name. If matching entry is found,
553 * its id returned to the caller.
556 * entry id if translation succeeds
559 int get_table_entry_id (table_entry_t *table,
560 const char *table_name, const char *name)
566 for (t = table; t->id >= 0; ++t) {
567 if (t->sname && strcasecmp(t->sname, name) == 0)
571 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
572 for (t = table; t->id >= 0; ++t) {
573 if (t->sname == NULL)
575 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
578 fprintf (stderr, "\n");
580 for (t = table; t->id >= 0; ++t) {
581 #ifdef CONFIG_RELOC_FIXUP_WORKS
582 if (t->sname && strcmp(t->sname, name) == 0)
584 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
588 debug ("Invalid %s Type: %s\n", table_name, name);
589 #endif /* USE_HOSTCC */
593 int genimg_get_os_id (const char *name)
595 return (get_table_entry_id (uimage_os, "OS", name));
598 int genimg_get_arch_id (const char *name)
600 return (get_table_entry_id (uimage_arch, "CPU", name));
603 int genimg_get_type_id (const char *name)
605 return (get_table_entry_id (uimage_type, "Image", name));
608 int genimg_get_comp_id (const char *name)
610 return (get_table_entry_id (uimage_comp, "Compression", name));
615 * genimg_get_format - get image format type
616 * @img_addr: image start address
618 * genimg_get_format() checks whether provided address points to a valid
619 * legacy or FIT image.
621 * New uImage format and FDT blob are based on a libfdt. FDT blob
622 * may be passed directly or embedded in a FIT image. In both situations
623 * genimg_get_format() must be able to dectect libfdt header.
626 * image format type or IMAGE_FORMAT_INVALID if no image is present
628 int genimg_get_format (void *img_addr)
630 ulong format = IMAGE_FORMAT_INVALID;
631 const image_header_t *hdr;
632 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
636 hdr = (const image_header_t *)img_addr;
637 if (image_check_magic(hdr))
638 format = IMAGE_FORMAT_LEGACY;
639 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
641 fit_hdr = (char *)img_addr;
642 if (fdt_check_header (fit_hdr) == 0)
643 format = IMAGE_FORMAT_FIT;
651 * genimg_get_image - get image from special storage (if necessary)
652 * @img_addr: image start address
654 * genimg_get_image() checks if provided image start adddress is located
655 * in a dataflash storage. If so, image is moved to a system RAM memory.
658 * image start address after possible relocation from special storage
660 ulong genimg_get_image (ulong img_addr)
662 ulong ram_addr = img_addr;
664 #ifdef CONFIG_HAS_DATAFLASH
665 ulong h_size, d_size;
667 if (addr_dataflash (img_addr)){
668 /* ger RAM address */
669 ram_addr = CONFIG_SYS_LOAD_ADDR;
671 /* get header size */
672 h_size = image_get_header_size ();
673 #if defined(CONFIG_FIT)
674 if (sizeof(struct fdt_header) > h_size)
675 h_size = sizeof(struct fdt_header);
679 debug (" Reading image header from dataflash address "
680 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
682 read_dataflash (img_addr, h_size, (char *)ram_addr);
685 switch (genimg_get_format ((void *)ram_addr)) {
686 case IMAGE_FORMAT_LEGACY:
687 d_size = image_get_data_size ((const image_header_t *)ram_addr);
688 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
691 #if defined(CONFIG_FIT)
692 case IMAGE_FORMAT_FIT:
693 d_size = fit_get_size ((const void *)ram_addr) - h_size;
694 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
699 printf (" No valid image found at 0x%08lx\n", img_addr);
703 /* read in image data */
704 debug (" Reading image remaining data from dataflash address "
705 "%08lx to RAM address %08lx\n", img_addr + h_size,
708 read_dataflash (img_addr + h_size, d_size,
709 (char *)(ram_addr + h_size));
712 #endif /* CONFIG_HAS_DATAFLASH */
718 * fit_has_config - check if there is a valid FIT configuration
719 * @images: pointer to the bootm command headers structure
721 * fit_has_config() checks if there is a FIT configuration in use
722 * (if FTI support is present).
725 * 0, no FIT support or no configuration found
726 * 1, configuration found
728 int genimg_has_config (bootm_headers_t *images)
730 #if defined(CONFIG_FIT)
731 if (images->fit_uname_cfg)
738 * boot_get_ramdisk - main ramdisk handling routine
739 * @argc: command argument count
740 * @argv: command argument list
741 * @images: pointer to the bootm images structure
742 * @arch: expected ramdisk architecture
743 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
744 * @rd_end: pointer to a ulong variable, will hold ramdisk end
746 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
747 * Curently supported are the following ramdisk sources:
748 * - multicomponent kernel/ramdisk image,
749 * - commandline provided address of decicated ramdisk image.
752 * 0, if ramdisk image was found and valid, or skiped
753 * rd_start and rd_end are set to ramdisk start/end addresses if
754 * ramdisk image is found and valid
756 * 1, if ramdisk image is found but corrupted, or invalid
757 * rd_start and rd_end are set to 0 if no ramdisk exists
759 int boot_get_ramdisk (int argc, char * const argv[], bootm_headers_t *images,
760 uint8_t arch, ulong *rd_start, ulong *rd_end)
762 ulong rd_addr, rd_load;
763 ulong rd_data, rd_len;
764 const image_header_t *rd_hdr;
765 #if defined(CONFIG_FIT)
767 const char *fit_uname_config = NULL;
768 const char *fit_uname_ramdisk = NULL;
780 * Look for a '-' which indicates to ignore the
783 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
784 debug ("## Skipping init Ramdisk\n");
785 rd_len = rd_data = 0;
786 } else if (argc >= 3 || genimg_has_config (images)) {
787 #if defined(CONFIG_FIT)
790 * If the init ramdisk comes from the FIT image and
791 * the FIT image address is omitted in the command
792 * line argument, try to use os FIT image address or
793 * default load address.
795 if (images->fit_uname_os)
796 default_addr = (ulong)images->fit_hdr_os;
798 default_addr = load_addr;
800 if (fit_parse_conf (argv[2], default_addr,
801 &rd_addr, &fit_uname_config)) {
802 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
803 fit_uname_config, rd_addr);
804 } else if (fit_parse_subimage (argv[2], default_addr,
805 &rd_addr, &fit_uname_ramdisk)) {
806 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
807 fit_uname_ramdisk, rd_addr);
811 rd_addr = simple_strtoul(argv[2], NULL, 16);
812 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
815 #if defined(CONFIG_FIT)
817 /* use FIT configuration provided in first bootm
820 rd_addr = (ulong)images->fit_hdr_os;
821 fit_uname_config = images->fit_uname_cfg;
822 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
823 fit_uname_config, rd_addr);
826 * Check whether configuration has ramdisk defined,
827 * if not, don't try to use it, quit silently.
829 fit_hdr = (void *)rd_addr;
830 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
831 if (cfg_noffset < 0) {
832 debug ("* ramdisk: no such config\n");
836 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
837 if (rd_noffset < 0) {
838 debug ("* ramdisk: no ramdisk in config\n");
844 /* copy from dataflash if needed */
845 rd_addr = genimg_get_image (rd_addr);
848 * Check if there is an initrd image at the
849 * address provided in the second bootm argument
850 * check image type, for FIT images get FIT node.
852 switch (genimg_get_format ((void *)rd_addr)) {
853 case IMAGE_FORMAT_LEGACY:
854 printf ("## Loading init Ramdisk from Legacy "
855 "Image at %08lx ...\n", rd_addr);
857 show_boot_progress (9);
858 rd_hdr = image_get_ramdisk (rd_addr, arch,
864 rd_data = image_get_data (rd_hdr);
865 rd_len = image_get_data_size (rd_hdr);
866 rd_load = image_get_load (rd_hdr);
868 #if defined(CONFIG_FIT)
869 case IMAGE_FORMAT_FIT:
870 fit_hdr = (void *)rd_addr;
871 printf ("## Loading init Ramdisk from FIT "
872 "Image at %08lx ...\n", rd_addr);
874 show_boot_progress (120);
875 if (!fit_check_format (fit_hdr)) {
876 puts ("Bad FIT ramdisk image format!\n");
877 show_boot_progress (-120);
880 show_boot_progress (121);
882 if (!fit_uname_ramdisk) {
884 * no ramdisk image node unit name, try to get config
885 * node first. If config unit node name is NULL
886 * fit_conf_get_node() will try to find default config node
888 show_boot_progress (122);
889 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
890 if (cfg_noffset < 0) {
891 puts ("Could not find configuration node\n");
892 show_boot_progress (-122);
895 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
896 printf (" Using '%s' configuration\n", fit_uname_config);
898 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
899 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
901 /* get ramdisk component image node offset */
902 show_boot_progress (123);
903 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
905 if (rd_noffset < 0) {
906 puts ("Could not find subimage node\n");
907 show_boot_progress (-124);
911 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
913 show_boot_progress (125);
914 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
917 /* get ramdisk image data address and length */
918 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
919 puts ("Could not find ramdisk subimage data!\n");
920 show_boot_progress (-127);
923 show_boot_progress (128);
925 rd_data = (ulong)data;
928 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
929 puts ("Can't get ramdisk subimage load address!\n");
930 show_boot_progress (-129);
933 show_boot_progress (129);
935 images->fit_hdr_rd = fit_hdr;
936 images->fit_uname_rd = fit_uname_ramdisk;
937 images->fit_noffset_rd = rd_noffset;
941 puts ("Wrong Ramdisk Image Format\n");
942 rd_data = rd_len = rd_load = 0;
946 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
948 * We need to copy the ramdisk to SRAM to let Linux boot
951 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
954 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
956 } else if (images->legacy_hdr_valid &&
957 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
959 * Now check if we have a legacy mult-component image,
960 * get second entry data start address and len.
962 show_boot_progress (13);
963 printf ("## Loading init Ramdisk from multi component "
964 "Legacy Image at %08lx ...\n",
965 (ulong)images->legacy_hdr_os);
967 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
972 show_boot_progress (14);
973 rd_len = rd_data = 0;
977 debug ("## No init Ramdisk\n");
980 *rd_end = rd_data + rd_len;
982 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
988 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
990 * boot_ramdisk_high - relocate init ramdisk
991 * @lmb: pointer to lmb handle, will be used for memory mgmt
992 * @rd_data: ramdisk data start address
993 * @rd_len: ramdisk data length
994 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
995 * start address (after possible relocation)
996 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
997 * end address (after possible relocation)
999 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1000 * variable and if requested ramdisk data is moved to a specified location.
1002 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1003 * start/end addresses if ramdisk image start and len were provided,
1004 * otherwise set initrd_start and initrd_end set to zeros.
1010 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1011 ulong *initrd_start, ulong *initrd_end)
1015 int initrd_copy_to_ram = 1;
1017 if ((s = getenv ("initrd_high")) != NULL) {
1018 /* a value of "no" or a similar string will act like 0,
1019 * turning the "load high" feature off. This is intentional.
1021 initrd_high = simple_strtoul (s, NULL, 16);
1022 if (initrd_high == ~0)
1023 initrd_copy_to_ram = 0;
1025 /* not set, no restrictions to load high */
1030 #ifdef CONFIG_LOGBUFFER
1031 /* Prevent initrd from overwriting logbuffer */
1032 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1035 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1036 initrd_high, initrd_copy_to_ram);
1039 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1040 debug (" in-place initrd\n");
1041 *initrd_start = rd_data;
1042 *initrd_end = rd_data + rd_len;
1043 lmb_reserve(lmb, rd_data, rd_len);
1046 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1048 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
1050 if (*initrd_start == 0) {
1051 puts ("ramdisk - allocation error\n");
1054 show_boot_progress (12);
1056 *initrd_end = *initrd_start + rd_len;
1057 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1058 *initrd_start, *initrd_end);
1060 memmove_wd ((void *)*initrd_start,
1061 (void *)rd_data, rd_len, CHUNKSZ);
1069 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1070 *initrd_start, *initrd_end);
1077 #endif /* defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC) */
1079 #ifdef CONFIG_OF_LIBFDT
1080 static void fdt_error (const char *msg)
1084 puts (" - must RESET the board to recover.\n");
1087 static const image_header_t *image_get_fdt (ulong fdt_addr)
1089 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1091 image_print_contents (fdt_hdr);
1093 puts (" Verifying Checksum ... ");
1094 if (!image_check_hcrc (fdt_hdr)) {
1095 fdt_error ("fdt header checksum invalid");
1099 if (!image_check_dcrc (fdt_hdr)) {
1100 fdt_error ("fdt checksum invalid");
1105 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1106 fdt_error ("uImage is not a fdt");
1109 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1110 fdt_error ("uImage is compressed");
1113 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1114 fdt_error ("uImage data is not a fdt");
1121 * fit_check_fdt - verify FIT format FDT subimage
1122 * @fit_hdr: pointer to the FIT header
1123 * fdt_noffset: FDT subimage node offset within FIT image
1124 * @verify: data CRC verification flag
1126 * fit_check_fdt() verifies integrity of the FDT subimage and from
1127 * specified FIT image.
1133 #if defined(CONFIG_FIT)
1134 static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1136 fit_image_print (fit, fdt_noffset, " ");
1139 puts (" Verifying Hash Integrity ... ");
1140 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1141 fdt_error ("Bad Data Hash");
1147 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1148 fdt_error ("Not a FDT image");
1152 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1153 fdt_error ("FDT image is compressed");
1159 #endif /* CONFIG_FIT */
1161 #ifndef CONFIG_SYS_FDT_PAD
1162 #define CONFIG_SYS_FDT_PAD 0x3000
1166 * boot_relocate_fdt - relocate flat device tree
1167 * @lmb: pointer to lmb handle, will be used for memory mgmt
1168 * @bootmap_base: base address of the bootmap region
1169 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1170 * @of_size: pointer to a ulong variable, will hold fdt length
1172 * boot_relocate_fdt() determines if the of_flat_tree address is within
1173 * the bootmap and if not relocates it into that region
1175 * of_flat_tree and of_size are set to final (after relocation) values
1181 int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
1182 char **of_flat_tree, ulong *of_size)
1184 char *fdt_blob = *of_flat_tree;
1192 if (fdt_check_header (fdt_blob) != 0) {
1193 fdt_error ("image is not a fdt");
1197 #ifndef CONFIG_SYS_NO_FLASH
1198 /* move the blob if it is in flash (set relocate) */
1199 if (addr2info ((ulong)fdt_blob) != NULL)
1204 * The blob needs to be inside the boot mapping.
1206 if (fdt_blob < (char *)bootmap_base)
1209 if ((fdt_blob + *of_size + CONFIG_SYS_FDT_PAD) >=
1210 ((char *)CONFIG_SYS_BOOTMAPSZ + bootmap_base))
1213 /* move flattend device tree if needed */
1218 /* position on a 4K boundary before the alloc_current */
1219 /* Pad the FDT by a specified amount */
1220 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1221 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
1222 (CONFIG_SYS_BOOTMAPSZ + bootmap_base));
1224 if (of_start == 0) {
1225 puts("device tree - allocation error\n");
1229 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
1230 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
1233 printf (" Loading Device Tree to %08lx, end %08lx ... ",
1234 of_start, of_start + of_len - 1);
1236 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
1238 fdt_error ("fdt move failed");
1243 *of_flat_tree = (char *)of_start;
1246 *of_flat_tree = fdt_blob;
1247 of_len = (CONFIG_SYS_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
1248 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
1249 fdt_set_totalsize(*of_flat_tree, of_len);
1254 set_working_fdt_addr(*of_flat_tree);
1262 * boot_get_fdt - main fdt handling routine
1263 * @argc: command argument count
1264 * @argv: command argument list
1265 * @images: pointer to the bootm images structure
1266 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1267 * @of_size: pointer to a ulong variable, will hold fdt length
1269 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1270 * Curently supported are the following ramdisk sources:
1271 * - multicomponent kernel/ramdisk image,
1272 * - commandline provided address of decicated ramdisk image.
1275 * 0, if fdt image was found and valid, or skipped
1276 * of_flat_tree and of_size are set to fdt start address and length if
1277 * fdt image is found and valid
1279 * 1, if fdt image is found but corrupted
1280 * of_flat_tree and of_size are set to 0 if no fdt exists
1282 int boot_get_fdt (int flag, int argc, char * const argv[], bootm_headers_t *images,
1283 char **of_flat_tree, ulong *of_size)
1285 const image_header_t *fdt_hdr;
1287 char *fdt_blob = NULL;
1288 ulong image_start, image_end;
1289 ulong load_start, load_end;
1290 #if defined(CONFIG_FIT)
1292 const char *fit_uname_config = NULL;
1293 const char *fit_uname_fdt = NULL;
1301 *of_flat_tree = NULL;
1304 if (argc > 3 || genimg_has_config (images)) {
1305 #if defined(CONFIG_FIT)
1308 * If the FDT blob comes from the FIT image and the
1309 * FIT image address is omitted in the command line
1310 * argument, try to use ramdisk or os FIT image
1311 * address or default load address.
1313 if (images->fit_uname_rd)
1314 default_addr = (ulong)images->fit_hdr_rd;
1315 else if (images->fit_uname_os)
1316 default_addr = (ulong)images->fit_hdr_os;
1318 default_addr = load_addr;
1320 if (fit_parse_conf (argv[3], default_addr,
1321 &fdt_addr, &fit_uname_config)) {
1322 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1323 fit_uname_config, fdt_addr);
1324 } else if (fit_parse_subimage (argv[3], default_addr,
1325 &fdt_addr, &fit_uname_fdt)) {
1326 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1327 fit_uname_fdt, fdt_addr);
1331 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1332 debug ("* fdt: cmdline image address = 0x%08lx\n",
1335 #if defined(CONFIG_FIT)
1337 /* use FIT configuration provided in first bootm
1340 fdt_addr = (ulong)images->fit_hdr_os;
1341 fit_uname_config = images->fit_uname_cfg;
1342 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1343 fit_uname_config, fdt_addr);
1346 * Check whether configuration has FDT blob defined,
1347 * if not quit silently.
1349 fit_hdr = (void *)fdt_addr;
1350 cfg_noffset = fit_conf_get_node (fit_hdr,
1352 if (cfg_noffset < 0) {
1353 debug ("* fdt: no such config\n");
1357 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1359 if (fdt_noffset < 0) {
1360 debug ("* fdt: no fdt in config\n");
1366 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1369 /* copy from dataflash if needed */
1370 fdt_addr = genimg_get_image (fdt_addr);
1373 * Check if there is an FDT image at the
1374 * address provided in the second bootm argument
1375 * check image type, for FIT images get a FIT node.
1377 switch (genimg_get_format ((void *)fdt_addr)) {
1378 case IMAGE_FORMAT_LEGACY:
1379 /* verify fdt_addr points to a valid image header */
1380 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1382 fdt_hdr = image_get_fdt (fdt_addr);
1387 * move image data to the load address,
1388 * make sure we don't overwrite initial image
1390 image_start = (ulong)fdt_hdr;
1391 image_end = image_get_image_end (fdt_hdr);
1393 load_start = image_get_load (fdt_hdr);
1394 load_end = load_start + image_get_data_size (fdt_hdr);
1396 if ((load_start < image_end) && (load_end > image_start)) {
1397 fdt_error ("fdt overwritten");
1401 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1402 image_get_data (fdt_hdr), load_start);
1404 memmove ((void *)load_start,
1405 (void *)image_get_data (fdt_hdr),
1406 image_get_data_size (fdt_hdr));
1408 fdt_blob = (char *)load_start;
1410 case IMAGE_FORMAT_FIT:
1412 * This case will catch both: new uImage format
1413 * (libfdt based) and raw FDT blob (also libfdt
1416 #if defined(CONFIG_FIT)
1417 /* check FDT blob vs FIT blob */
1418 if (fit_check_format ((const void *)fdt_addr)) {
1422 fit_hdr = (void *)fdt_addr;
1423 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1426 if (!fit_uname_fdt) {
1428 * no FDT blob image node unit name,
1429 * try to get config node first. If
1430 * config unit node name is NULL
1431 * fit_conf_get_node() will try to
1432 * find default config node
1434 cfg_noffset = fit_conf_get_node (fit_hdr,
1437 if (cfg_noffset < 0) {
1438 fdt_error ("Could not find configuration node\n");
1442 fit_uname_config = fdt_get_name (fit_hdr,
1444 printf (" Using '%s' configuration\n",
1447 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1449 fit_uname_fdt = fit_get_name (fit_hdr,
1452 /* get FDT component image node offset */
1453 fdt_noffset = fit_image_get_node (fit_hdr,
1456 if (fdt_noffset < 0) {
1457 fdt_error ("Could not find subimage node\n");
1461 printf (" Trying '%s' FDT blob subimage\n",
1464 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1468 /* get ramdisk image data address and length */
1469 if (fit_image_get_data (fit_hdr, fdt_noffset,
1471 fdt_error ("Could not find FDT subimage data");
1475 /* verift that image data is a proper FDT blob */
1476 if (fdt_check_header ((char *)data) != 0) {
1477 fdt_error ("Subimage data is not a FTD");
1482 * move image data to the load address,
1483 * make sure we don't overwrite initial image
1485 image_start = (ulong)fit_hdr;
1486 image_end = fit_get_end (fit_hdr);
1488 if (fit_image_get_load (fit_hdr, fdt_noffset,
1489 &load_start) == 0) {
1490 load_end = load_start + size;
1492 if ((load_start < image_end) &&
1493 (load_end > image_start)) {
1494 fdt_error ("FDT overwritten");
1498 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1499 (ulong)data, load_start);
1501 memmove ((void *)load_start,
1502 (void *)data, size);
1504 fdt_blob = (char *)load_start;
1506 fdt_blob = (char *)data;
1509 images->fit_hdr_fdt = fit_hdr;
1510 images->fit_uname_fdt = fit_uname_fdt;
1511 images->fit_noffset_fdt = fdt_noffset;
1519 fdt_blob = (char *)fdt_addr;
1520 debug ("* fdt: raw FDT blob\n");
1521 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1525 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
1529 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1531 } else if (images->legacy_hdr_valid &&
1532 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1534 ulong fdt_data, fdt_len;
1537 * Now check if we have a legacy multi-component image,
1538 * get second entry data start address and len.
1540 printf ("## Flattened Device Tree from multi "
1541 "component Image at %08lX\n",
1542 (ulong)images->legacy_hdr_os);
1544 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1547 fdt_blob = (char *)fdt_data;
1548 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1550 if (fdt_check_header (fdt_blob) != 0) {
1551 fdt_error ("image is not a fdt");
1555 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
1556 fdt_error ("fdt size != image size");
1560 debug ("## No Flattened Device Tree\n");
1564 debug ("## No Flattened Device Tree\n");
1568 *of_flat_tree = fdt_blob;
1569 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
1570 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
1571 (ulong)*of_flat_tree, *of_size);
1580 #endif /* CONFIG_OF_LIBFDT */
1582 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
1584 * boot_get_cmdline - allocate and initialize kernel cmdline
1585 * @lmb: pointer to lmb handle, will be used for memory mgmt
1586 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1587 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1588 * @bootmap_base: ulong variable, holds offset in physical memory to
1591 * boot_get_cmdline() allocates space for kernel command line below
1592 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1593 * variable is present its contents is copied to allocated kernel
1600 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1606 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1607 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
1609 if (cmdline == NULL)
1612 if ((s = getenv("bootargs")) == NULL)
1617 *cmd_start = (ulong) & cmdline[0];
1618 *cmd_end = *cmd_start + strlen(cmdline);
1620 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1626 * boot_get_kbd - allocate and initialize kernel copy of board info
1627 * @lmb: pointer to lmb handle, will be used for memory mgmt
1628 * @kbd: double pointer to board info data
1629 * @bootmap_base: ulong variable, holds offset in physical memory to
1632 * boot_get_kbd() allocates space for kernel copy of board info data below
1633 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1634 * the current u-boot board info data.
1640 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1642 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1643 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
1649 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1651 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1652 do_bdinfo(NULL, 0, 0, NULL);
1657 #endif /* CONFIG_PPC || CONFIG_M68K */
1658 #endif /* !USE_HOSTCC */
1660 #if defined(CONFIG_FIT)
1661 /*****************************************************************************/
1662 /* New uImage format routines */
1663 /*****************************************************************************/
1665 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1666 ulong *addr, const char **name)
1673 sep = strchr (spec, sepc);
1676 *addr = simple_strtoul (spec, NULL, 16);
1686 * fit_parse_conf - parse FIT configuration spec
1687 * @spec: input string, containing configuration spec
1688 * @add_curr: current image address (to be used as a possible default)
1689 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1691 * @conf_name double pointer to a char, will hold pointer to a configuration
1694 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1695 * where <addr> is a FIT image address that contains configuration
1696 * with a <conf> unit name.
1698 * Address part is optional, and if omitted default add_curr will
1702 * 1 if spec is a valid configuration string,
1703 * addr and conf_name are set accordingly
1706 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1707 ulong *addr, const char **conf_name)
1709 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1713 * fit_parse_subimage - parse FIT subimage spec
1714 * @spec: input string, containing subimage spec
1715 * @add_curr: current image address (to be used as a possible default)
1716 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1718 * @image_name: double pointer to a char, will hold pointer to a subimage name
1720 * fit_parse_subimage() expects subimage spec in the for of
1721 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1722 * subimage with a <subimg> unit name.
1724 * Address part is optional, and if omitted default add_curr will
1728 * 1 if spec is a valid subimage string,
1729 * addr and image_name are set accordingly
1732 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1733 ulong *addr, const char **image_name)
1735 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1737 #endif /* !USE_HOSTCC */
1739 static void fit_get_debug (const void *fit, int noffset,
1740 char *prop_name, int err)
1742 debug ("Can't get '%s' property from FIT 0x%08lx, "
1743 "node: offset %d, name %s (%s)\n",
1744 prop_name, (ulong)fit, noffset,
1745 fit_get_name (fit, noffset, NULL),
1746 fdt_strerror (err));
1750 * fit_print_contents - prints out the contents of the FIT format image
1751 * @fit: pointer to the FIT format image header
1752 * @p: pointer to prefix string
1754 * fit_print_contents() formats a multi line FIT image contents description.
1755 * The routine prints out FIT image properties (root node level) follwed by
1756 * the details of each component image.
1759 * no returned results
1761 void fit_print_contents (const void *fit)
1772 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1782 /* Root node properties */
1783 ret = fit_get_desc (fit, 0, &desc);
1784 printf ("%sFIT description: ", p);
1786 printf ("unavailable\n");
1788 printf ("%s\n", desc);
1790 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1791 ret = fit_get_timestamp (fit, 0, ×tamp);
1792 printf ("%sCreated: ", p);
1794 printf ("unavailable\n");
1796 genimg_print_time (timestamp);
1799 /* Find images parent node offset */
1800 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1801 if (images_noffset < 0) {
1802 printf ("Can't find images parent node '%s' (%s)\n",
1803 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1807 /* Process its subnodes, print out component images details */
1808 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1809 (noffset >= 0) && (ndepth > 0);
1810 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1813 * Direct child node of the images parent node,
1814 * i.e. component image node.
1816 printf ("%s Image %u (%s)\n", p, count++,
1817 fit_get_name(fit, noffset, NULL));
1819 fit_image_print (fit, noffset, p);
1823 /* Find configurations parent node offset */
1824 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1825 if (confs_noffset < 0) {
1826 debug ("Can't get configurations parent node '%s' (%s)\n",
1827 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1831 /* get default configuration unit name from default property */
1832 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1834 printf ("%s Default Configuration: '%s'\n", p, uname);
1836 /* Process its subnodes, print out configurations details */
1837 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1838 (noffset >= 0) && (ndepth > 0);
1839 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1842 * Direct child node of the configurations parent node,
1843 * i.e. configuration node.
1845 printf ("%s Configuration %u (%s)\n", p, count++,
1846 fit_get_name(fit, noffset, NULL));
1848 fit_conf_print (fit, noffset, p);
1854 * fit_image_print - prints out the FIT component image details
1855 * @fit: pointer to the FIT format image header
1856 * @image_noffset: offset of the component image node
1857 * @p: pointer to prefix string
1859 * fit_image_print() lists all mandatory properies for the processed component
1860 * image. If present, hash nodes are printed out as well. Load
1861 * address for images of type firmware is also printed out. Since the load
1862 * address is not mandatory for firmware images, it will be output as
1863 * "unavailable" when not present.
1866 * no returned results
1868 void fit_image_print (const void *fit, int image_noffset, const char *p)
1871 uint8_t type, arch, os, comp;
1879 /* Mandatory properties */
1880 ret = fit_get_desc (fit, image_noffset, &desc);
1881 printf ("%s Description: ", p);
1883 printf ("unavailable\n");
1885 printf ("%s\n", desc);
1887 fit_image_get_type (fit, image_noffset, &type);
1888 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1890 fit_image_get_comp (fit, image_noffset, &comp);
1891 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1893 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1896 printf ("%s Data Start: ", p);
1898 printf ("unavailable\n");
1900 printf ("0x%08lx\n", (ulong)data);
1903 printf ("%s Data Size: ", p);
1905 printf ("unavailable\n");
1907 genimg_print_size (size);
1909 /* Remaining, type dependent properties */
1910 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1911 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1912 (type == IH_TYPE_FLATDT)) {
1913 fit_image_get_arch (fit, image_noffset, &arch);
1914 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1917 if (type == IH_TYPE_KERNEL) {
1918 fit_image_get_os (fit, image_noffset, &os);
1919 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1922 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1923 (type == IH_TYPE_FIRMWARE)) {
1924 ret = fit_image_get_load (fit, image_noffset, &load);
1925 printf ("%s Load Address: ", p);
1927 printf ("unavailable\n");
1929 printf ("0x%08lx\n", load);
1932 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1933 fit_image_get_entry (fit, image_noffset, &entry);
1934 printf ("%s Entry Point: ", p);
1936 printf ("unavailable\n");
1938 printf ("0x%08lx\n", entry);
1941 /* Process all hash subnodes of the component image node */
1942 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1943 (noffset >= 0) && (ndepth > 0);
1944 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1946 /* Direct child node of the component image node */
1947 fit_image_print_hash (fit, noffset, p);
1953 * fit_image_print_hash - prints out the hash node details
1954 * @fit: pointer to the FIT format image header
1955 * @noffset: offset of the hash node
1956 * @p: pointer to prefix string
1958 * fit_image_print_hash() lists properies for the processed hash node
1961 * no returned results
1963 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1971 * Check subnode name, must be equal to "hash".
1972 * Multiple hash nodes require unique unit node
1973 * names, e.g. hash@1, hash@2, etc.
1975 if (strncmp (fit_get_name(fit, noffset, NULL),
1977 strlen(FIT_HASH_NODENAME)) != 0)
1980 debug ("%s Hash node: '%s'\n", p,
1981 fit_get_name (fit, noffset, NULL));
1983 printf ("%s Hash algo: ", p);
1984 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1985 printf ("invalid/unsupported\n");
1988 printf ("%s\n", algo);
1990 ret = fit_image_hash_get_value (fit, noffset, &value,
1992 printf ("%s Hash value: ", p);
1994 printf ("unavailable\n");
1996 for (i = 0; i < value_len; i++)
1997 printf ("%02x", value[i]);
2001 debug ("%s Hash len: %d\n", p, value_len);
2005 * fit_get_desc - get node description property
2006 * @fit: pointer to the FIT format image header
2007 * @noffset: node offset
2008 * @desc: double pointer to the char, will hold pointer to the descrption
2010 * fit_get_desc() reads description property from a given node, if
2011 * description is found pointer to it is returened in third call argument.
2017 int fit_get_desc (const void *fit, int noffset, char **desc)
2021 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2022 if (*desc == NULL) {
2023 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2031 * fit_get_timestamp - get node timestamp property
2032 * @fit: pointer to the FIT format image header
2033 * @noffset: node offset
2034 * @timestamp: pointer to the time_t, will hold read timestamp
2036 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2037 * is found and has a correct size its value is retured in third call
2042 * -1, on property read failure
2043 * -2, on wrong timestamp size
2045 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2050 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2052 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2055 if (len != sizeof (uint32_t)) {
2056 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2060 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2065 * fit_image_get_node - get node offset for component image of a given unit name
2066 * @fit: pointer to the FIT format image header
2067 * @image_uname: component image node unit name
2069 * fit_image_get_node() finds a component image (withing the '/images'
2070 * node) of a provided unit name. If image is found its node offset is
2071 * returned to the caller.
2074 * image node offset when found (>=0)
2075 * negative number on failure (FDT_ERR_* code)
2077 int fit_image_get_node (const void *fit, const char *image_uname)
2079 int noffset, images_noffset;
2081 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2082 if (images_noffset < 0) {
2083 debug ("Can't find images parent node '%s' (%s)\n",
2084 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2085 return images_noffset;
2088 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2090 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2091 image_uname, fdt_strerror (noffset));
2098 * fit_image_get_os - get os id for a given component image node
2099 * @fit: pointer to the FIT format image header
2100 * @noffset: component image node offset
2101 * @os: pointer to the uint8_t, will hold os numeric id
2103 * fit_image_get_os() finds os property in a given component image node.
2104 * If the property is found, its (string) value is translated to the numeric
2105 * id which is returned to the caller.
2111 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2116 /* Get OS name from property data */
2117 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2119 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2124 /* Translate OS name to id */
2125 *os = genimg_get_os_id (data);
2130 * fit_image_get_arch - get arch id for a given component image node
2131 * @fit: pointer to the FIT format image header
2132 * @noffset: component image node offset
2133 * @arch: pointer to the uint8_t, will hold arch numeric id
2135 * fit_image_get_arch() finds arch property in a given component image node.
2136 * If the property is found, its (string) value is translated to the numeric
2137 * id which is returned to the caller.
2143 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2148 /* Get architecture name from property data */
2149 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2151 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2156 /* Translate architecture name to id */
2157 *arch = genimg_get_arch_id (data);
2162 * fit_image_get_type - get type id for a given component image node
2163 * @fit: pointer to the FIT format image header
2164 * @noffset: component image node offset
2165 * @type: pointer to the uint8_t, will hold type numeric id
2167 * fit_image_get_type() finds type property in a given component image node.
2168 * If the property is found, its (string) value is translated to the numeric
2169 * id which is returned to the caller.
2175 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2180 /* Get image type name from property data */
2181 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2183 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2188 /* Translate image type name to id */
2189 *type = genimg_get_type_id (data);
2194 * fit_image_get_comp - get comp id for a given component image node
2195 * @fit: pointer to the FIT format image header
2196 * @noffset: component image node offset
2197 * @comp: pointer to the uint8_t, will hold comp numeric id
2199 * fit_image_get_comp() finds comp property in a given component image node.
2200 * If the property is found, its (string) value is translated to the numeric
2201 * id which is returned to the caller.
2207 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2212 /* Get compression name from property data */
2213 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2215 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2220 /* Translate compression name to id */
2221 *comp = genimg_get_comp_id (data);
2226 * fit_image_get_load - get load address property for a given component image node
2227 * @fit: pointer to the FIT format image header
2228 * @noffset: component image node offset
2229 * @load: pointer to the uint32_t, will hold load address
2231 * fit_image_get_load() finds load address property in a given component image node.
2232 * If the property is found, its value is returned to the caller.
2238 int fit_image_get_load (const void *fit, int noffset, ulong *load)
2241 const uint32_t *data;
2243 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2245 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2249 *load = uimage_to_cpu (*data);
2254 * fit_image_get_entry - get entry point address property for a given component image node
2255 * @fit: pointer to the FIT format image header
2256 * @noffset: component image node offset
2257 * @entry: pointer to the uint32_t, will hold entry point address
2259 * fit_image_get_entry() finds entry point address property in a given component image node.
2260 * If the property is found, its value is returned to the caller.
2266 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2269 const uint32_t *data;
2271 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2273 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2277 *entry = uimage_to_cpu (*data);
2282 * fit_image_get_data - get data property and its size for a given component image node
2283 * @fit: pointer to the FIT format image header
2284 * @noffset: component image node offset
2285 * @data: double pointer to void, will hold data property's data address
2286 * @size: pointer to size_t, will hold data property's data size
2288 * fit_image_get_data() finds data property in a given component image node.
2289 * If the property is found its data start address and size are returned to
2296 int fit_image_get_data (const void *fit, int noffset,
2297 const void **data, size_t *size)
2301 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2302 if (*data == NULL) {
2303 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2313 * fit_image_hash_get_algo - get hash algorithm name
2314 * @fit: pointer to the FIT format image header
2315 * @noffset: hash node offset
2316 * @algo: double pointer to char, will hold pointer to the algorithm name
2318 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2319 * If the property is found its data start address is returned to the caller.
2325 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2329 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2330 if (*algo == NULL) {
2331 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2339 * fit_image_hash_get_value - get hash value and length
2340 * @fit: pointer to the FIT format image header
2341 * @noffset: hash node offset
2342 * @value: double pointer to uint8_t, will hold address of a hash value data
2343 * @value_len: pointer to an int, will hold hash data length
2345 * fit_image_hash_get_value() finds hash value property in a given hash node.
2346 * If the property is found its data start address and size are returned to
2353 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2358 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2359 if (*value == NULL) {
2360 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2370 * fit_set_timestamp - set node timestamp property
2371 * @fit: pointer to the FIT format image header
2372 * @noffset: node offset
2373 * @timestamp: timestamp value to be set
2375 * fit_set_timestamp() attempts to set timestamp property in the requested
2376 * node and returns operation status to the caller.
2380 * -1, on property read failure
2382 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2387 t = cpu_to_uimage (timestamp);
2388 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2391 printf ("Can't set '%s' property for '%s' node (%s)\n",
2392 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2393 fdt_strerror (ret));
2401 * calculate_hash - calculate and return hash for provided input data
2402 * @data: pointer to the input data
2403 * @data_len: data length
2404 * @algo: requested hash algorithm
2405 * @value: pointer to the char, will hold hash value data (caller must
2406 * allocate enough free space)
2407 * value_len: length of the calculated hash
2409 * calculate_hash() computes input data hash according to the requested algorithm.
2410 * Resulting hash value is placed in caller provided 'value' buffer, length
2411 * of the calculated hash is returned via value_len pointer argument.
2415 * -1, when algo is unsupported
2417 static int calculate_hash (const void *data, int data_len, const char *algo,
2418 uint8_t *value, int *value_len)
2420 if (strcmp (algo, "crc32") == 0 ) {
2421 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2423 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2425 } else if (strcmp (algo, "sha1") == 0 ) {
2426 sha1_csum_wd ((unsigned char *) data, data_len,
2427 (unsigned char *) value, CHUNKSZ_SHA1);
2429 } else if (strcmp (algo, "md5") == 0 ) {
2430 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2433 debug ("Unsupported hash alogrithm\n");
2441 * fit_set_hashes - process FIT component image nodes and calculate hashes
2442 * @fit: pointer to the FIT format image header
2444 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2445 * Hashes are calculated for all component images which have hash subnodes
2446 * with algorithm property set to one of the supported hash algorithms.
2450 * libfdt error code, on failure
2452 int fit_set_hashes (void *fit)
2459 /* Find images parent node offset */
2460 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2461 if (images_noffset < 0) {
2462 printf ("Can't find images parent node '%s' (%s)\n",
2463 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2464 return images_noffset;
2467 /* Process its subnodes, print out component images details */
2468 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2469 (noffset >= 0) && (ndepth > 0);
2470 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2473 * Direct child node of the images parent node,
2474 * i.e. component image node.
2476 ret = fit_image_set_hashes (fit, noffset);
2486 * fit_image_set_hashes - calculate/set hashes for given component image node
2487 * @fit: pointer to the FIT format image header
2488 * @image_noffset: requested component image node
2490 * fit_image_set_hashes() adds hash values for an component image node. All
2491 * existing hash subnodes are checked, if algorithm property is set to one of
2492 * the supported hash algorithms, hash value is computed and corresponding
2493 * hash node property is set, for example:
2495 * Input component image node structure:
2497 * o image@1 (at image_noffset)
2498 * | - data = [binary data]
2502 * Output component image node structure:
2504 * o image@1 (at image_noffset)
2505 * | - data = [binary data]
2508 * |- value = sha1(data)
2514 int fit_image_set_hashes (void *fit, int image_noffset)
2519 uint8_t value[FIT_MAX_HASH_LEN];
2524 /* Get image data and data length */
2525 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2526 printf ("Can't get image data/size\n");
2530 /* Process all hash subnodes of the component image node */
2531 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2532 (noffset >= 0) && (ndepth > 0);
2533 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2535 /* Direct child node of the component image node */
2538 * Check subnode name, must be equal to "hash".
2539 * Multiple hash nodes require unique unit node
2540 * names, e.g. hash@1, hash@2, etc.
2542 if (strncmp (fit_get_name(fit, noffset, NULL),
2544 strlen(FIT_HASH_NODENAME)) != 0) {
2545 /* Not a hash subnode, skip it */
2549 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2550 printf ("Can't get hash algo property for "
2551 "'%s' hash node in '%s' image node\n",
2552 fit_get_name (fit, noffset, NULL),
2553 fit_get_name (fit, image_noffset, NULL));
2557 if (calculate_hash (data, size, algo, value, &value_len)) {
2558 printf ("Unsupported hash algorithm (%s) for "
2559 "'%s' hash node in '%s' image node\n",
2560 algo, fit_get_name (fit, noffset, NULL),
2561 fit_get_name (fit, image_noffset, NULL));
2565 if (fit_image_hash_set_value (fit, noffset, value,
2567 printf ("Can't set hash value for "
2568 "'%s' hash node in '%s' image node\n",
2569 fit_get_name (fit, noffset, NULL),
2570 fit_get_name (fit, image_noffset, NULL));
2580 * fit_image_hash_set_value - set hash value in requested has node
2581 * @fit: pointer to the FIT format image header
2582 * @noffset: hash node offset
2583 * @value: hash value to be set
2584 * @value_len: hash value length
2586 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2587 * given and returns operation status to the caller.
2593 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2598 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2600 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2601 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2602 fdt_strerror (ret));
2608 #endif /* USE_HOSTCC */
2611 * fit_image_check_hashes - verify data intergity
2612 * @fit: pointer to the FIT format image header
2613 * @image_noffset: component image node offset
2615 * fit_image_check_hashes() goes over component image hash nodes,
2616 * re-calculates each data hash and compares with the value stored in hash
2620 * 1, if all hashes are valid
2621 * 0, otherwise (or on error)
2623 int fit_image_check_hashes (const void *fit, int image_noffset)
2630 uint8_t value[FIT_MAX_HASH_LEN];
2636 /* Get image data and data length */
2637 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2638 printf ("Can't get image data/size\n");
2642 /* Process all hash subnodes of the component image node */
2643 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2644 (noffset >= 0) && (ndepth > 0);
2645 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2647 /* Direct child node of the component image node */
2650 * Check subnode name, must be equal to "hash".
2651 * Multiple hash nodes require unique unit node
2652 * names, e.g. hash@1, hash@2, etc.
2654 if (strncmp (fit_get_name(fit, noffset, NULL),
2656 strlen(FIT_HASH_NODENAME)) != 0)
2659 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2660 err_msg = " error!\nCan't get hash algo "
2664 printf ("%s", algo);
2666 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2668 err_msg = " error!\nCan't get hash value "
2673 if (calculate_hash (data, size, algo, value, &value_len)) {
2674 err_msg = " error!\nUnsupported hash algorithm";
2678 if (value_len != fit_value_len) {
2679 err_msg = " error !\nBad hash value len";
2681 } else if (memcmp (value, fit_value, value_len) != 0) {
2682 err_msg = " error!\nBad hash value";
2692 printf ("%s for '%s' hash node in '%s' image node\n",
2693 err_msg, fit_get_name (fit, noffset, NULL),
2694 fit_get_name (fit, image_noffset, NULL));
2699 * fit_all_image_check_hashes - verify data intergity for all images
2700 * @fit: pointer to the FIT format image header
2702 * fit_all_image_check_hashes() goes over all images in the FIT and
2703 * for every images checks if all it's hashes are valid.
2706 * 1, if all hashes of all images are valid
2707 * 0, otherwise (or on error)
2709 int fit_all_image_check_hashes (const void *fit)
2716 /* Find images parent node offset */
2717 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2718 if (images_noffset < 0) {
2719 printf ("Can't find images parent node '%s' (%s)\n",
2720 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2724 /* Process all image subnodes, check hashes for each */
2725 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2727 for (ndepth = 0, count = 0,
2728 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2729 (noffset >= 0) && (ndepth > 0);
2730 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2733 * Direct child node of the images parent node,
2734 * i.e. component image node.
2736 printf (" Hash(es) for Image %u (%s): ", count++,
2737 fit_get_name (fit, noffset, NULL));
2739 if (!fit_image_check_hashes (fit, noffset))
2748 * fit_image_check_os - check whether image node is of a given os type
2749 * @fit: pointer to the FIT format image header
2750 * @noffset: component image node offset
2751 * @os: requested image os
2753 * fit_image_check_os() reads image os property and compares its numeric
2754 * id with the requested os. Comparison result is returned to the caller.
2757 * 1 if image is of given os type
2758 * 0 otherwise (or on error)
2760 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2764 if (fit_image_get_os (fit, noffset, &image_os))
2766 return (os == image_os);
2770 * fit_image_check_arch - check whether image node is of a given arch
2771 * @fit: pointer to the FIT format image header
2772 * @noffset: component image node offset
2773 * @arch: requested imagearch
2775 * fit_image_check_arch() reads image arch property and compares its numeric
2776 * id with the requested arch. Comparison result is returned to the caller.
2779 * 1 if image is of given arch
2780 * 0 otherwise (or on error)
2782 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2786 if (fit_image_get_arch (fit, noffset, &image_arch))
2788 return (arch == image_arch);
2792 * fit_image_check_type - check whether image node is of a given type
2793 * @fit: pointer to the FIT format image header
2794 * @noffset: component image node offset
2795 * @type: requested image type
2797 * fit_image_check_type() reads image type property and compares its numeric
2798 * id with the requested type. Comparison result is returned to the caller.
2801 * 1 if image is of given type
2802 * 0 otherwise (or on error)
2804 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2808 if (fit_image_get_type (fit, noffset, &image_type))
2810 return (type == image_type);
2814 * fit_image_check_comp - check whether image node uses given compression
2815 * @fit: pointer to the FIT format image header
2816 * @noffset: component image node offset
2817 * @comp: requested image compression type
2819 * fit_image_check_comp() reads image compression property and compares its
2820 * numeric id with the requested compression type. Comparison result is
2821 * returned to the caller.
2824 * 1 if image uses requested compression
2825 * 0 otherwise (or on error)
2827 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2831 if (fit_image_get_comp (fit, noffset, &image_comp))
2833 return (comp == image_comp);
2837 * fit_check_format - sanity check FIT image format
2838 * @fit: pointer to the FIT format image header
2840 * fit_check_format() runs a basic sanity FIT image verification.
2841 * Routine checks for mandatory properties, nodes, etc.
2847 int fit_check_format (const void *fit)
2849 /* mandatory / node 'description' property */
2850 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2851 debug ("Wrong FIT format: no description\n");
2855 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2856 /* mandatory / node 'timestamp' property */
2857 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2858 debug ("Wrong FIT format: no timestamp\n");
2863 /* mandatory subimages parent '/images' node */
2864 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2865 debug ("Wrong FIT format: no images parent node\n");
2873 * fit_conf_get_node - get node offset for configuration of a given unit name
2874 * @fit: pointer to the FIT format image header
2875 * @conf_uname: configuration node unit name
2877 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2878 * parant node) of a provided unit name. If configuration is found its node offset
2879 * is returned to the caller.
2881 * When NULL is provided in second argument fit_conf_get_node() will search
2882 * for a default configuration node instead. Default configuration node unit name
2883 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2886 * configuration node offset when found (>=0)
2887 * negative number on failure (FDT_ERR_* code)
2889 int fit_conf_get_node (const void *fit, const char *conf_uname)
2891 int noffset, confs_noffset;
2894 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2895 if (confs_noffset < 0) {
2896 debug ("Can't find configurations parent node '%s' (%s)\n",
2897 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2898 return confs_noffset;
2901 if (conf_uname == NULL) {
2902 /* get configuration unit name from the default property */
2903 debug ("No configuration specified, trying default...\n");
2904 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2905 if (conf_uname == NULL) {
2906 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2909 debug ("Found default configuration: '%s'\n", conf_uname);
2912 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2914 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2915 conf_uname, fdt_strerror (noffset));
2921 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2922 const char *prop_name)
2927 /* get kernel image unit name from configuration kernel property */
2928 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2932 return fit_image_get_node (fit, uname);
2936 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2937 * a given configuration
2938 * @fit: pointer to the FIT format image header
2939 * @noffset: configuration node offset
2941 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2942 * configuration FIT_KERNEL_PROP property and translates it to the node
2946 * image node offset when found (>=0)
2947 * negative number on failure (FDT_ERR_* code)
2949 int fit_conf_get_kernel_node (const void *fit, int noffset)
2951 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2955 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2956 * a given configuration
2957 * @fit: pointer to the FIT format image header
2958 * @noffset: configuration node offset
2960 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2961 * configuration FIT_KERNEL_PROP property and translates it to the node
2965 * image node offset when found (>=0)
2966 * negative number on failure (FDT_ERR_* code)
2968 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2970 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2974 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2975 * a given configuration
2976 * @fit: pointer to the FIT format image header
2977 * @noffset: configuration node offset
2979 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2980 * configuration FIT_KERNEL_PROP property and translates it to the node
2984 * image node offset when found (>=0)
2985 * negative number on failure (FDT_ERR_* code)
2987 int fit_conf_get_fdt_node (const void *fit, int noffset)
2989 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2993 * fit_conf_print - prints out the FIT configuration details
2994 * @fit: pointer to the FIT format image header
2995 * @noffset: offset of the configuration node
2996 * @p: pointer to prefix string
2998 * fit_conf_print() lists all mandatory properies for the processed
2999 * configuration node.
3002 * no returned results
3004 void fit_conf_print (const void *fit, int noffset, const char *p)
3010 /* Mandatory properties */
3011 ret = fit_get_desc (fit, noffset, &desc);
3012 printf ("%s Description: ", p);
3014 printf ("unavailable\n");
3016 printf ("%s\n", desc);
3018 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3019 printf ("%s Kernel: ", p);
3021 printf ("unavailable\n");
3023 printf ("%s\n", uname);
3025 /* Optional properties */
3026 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3028 printf ("%s Init Ramdisk: %s\n", p, uname);
3030 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3032 printf ("%s FDT: %s\n", p, uname);
3036 * fit_check_ramdisk - verify FIT format ramdisk subimage
3037 * @fit_hdr: pointer to the FIT ramdisk header
3038 * @rd_noffset: ramdisk subimage node offset within FIT image
3039 * @arch: requested ramdisk image architecture type
3040 * @verify: data CRC verification flag
3042 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3043 * specified FIT image.
3050 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3052 fit_image_print (fit, rd_noffset, " ");
3055 puts (" Verifying Hash Integrity ... ");
3056 if (!fit_image_check_hashes (fit, rd_noffset)) {
3057 puts ("Bad Data Hash\n");
3058 show_boot_progress (-125);
3064 show_boot_progress (126);
3065 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3066 !fit_image_check_arch (fit, rd_noffset, arch) ||
3067 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3068 printf ("No Linux %s Ramdisk Image\n",
3069 genimg_get_arch_name(arch));
3070 show_boot_progress (-126);
3074 show_boot_progress (127);
3077 #endif /* USE_HOSTCC */
3078 #endif /* CONFIG_FIT */