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 const table_entry_t uimage_arch[] = {
78 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
79 { IH_ARCH_ALPHA, "alpha", "Alpha", },
80 { IH_ARCH_ARM, "arm", "ARM", },
81 { IH_ARCH_I386, "x86", "Intel x86", },
82 { IH_ARCH_IA64, "ia64", "IA64", },
83 { IH_ARCH_M68K, "m68k", "M68K", },
84 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
85 { IH_ARCH_MIPS, "mips", "MIPS", },
86 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
87 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
88 { IH_ARCH_PPC, "powerpc", "PowerPC", },
89 { IH_ARCH_PPC, "ppc", "PowerPC", },
90 { IH_ARCH_S390, "s390", "IBM S390", },
91 { IH_ARCH_SH, "sh", "SuperH", },
92 { IH_ARCH_SPARC, "sparc", "SPARC", },
93 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
94 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
95 { IH_ARCH_AVR32, "avr32", "AVR32", },
99 static const 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_OSE, "ose", "Enea OSE", },
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 const 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 const 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)
438 char *s = getenv ("bootm_size");
440 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
443 s = getenv("bootm_low");
445 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
450 #if defined(CONFIG_ARM)
451 return gd->bd->bi_dram[0].size - tmp;
453 return gd->bd->bi_memsize - tmp;
457 phys_size_t getenv_bootm_mapsize(void)
460 char *s = getenv ("bootm_mapsize");
462 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
466 #if defined(CONFIG_SYS_BOOTMAPSZ)
467 return CONFIG_SYS_BOOTMAPSZ;
469 return getenv_bootm_size();
473 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
478 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
480 size_t tail = (len > chunksz) ? chunksz : len;
482 memmove (to, from, tail);
487 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
488 memmove (to, from, len);
489 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
491 #endif /* !USE_HOSTCC */
493 void genimg_print_size (uint32_t size)
496 printf ("%d Bytes = ", size);
497 print_size (size, "\n");
499 printf ("%d Bytes = %.2f kB = %.2f MB\n",
500 size, (double)size / 1.024e3,
501 (double)size / 1.048576e6);
505 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
506 static void genimg_print_time (time_t timestamp)
511 to_tm (timestamp, &tm);
512 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
513 tm.tm_year, tm.tm_mon, tm.tm_mday,
514 tm.tm_hour, tm.tm_min, tm.tm_sec);
516 printf ("%s", ctime(×tamp));
519 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
522 * get_table_entry_name - translate entry id to long name
523 * @table: pointer to a translation table for entries of a specific type
524 * @msg: message to be returned when translation fails
525 * @id: entry id to be translated
527 * get_table_entry_name() will go over translation table trying to find
528 * entry that matches given id. If matching entry is found, its long
529 * name is returned to the caller.
532 * long entry name if translation succeeds
535 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
537 for (; table->id >= 0; ++table) {
539 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
542 return table->lname + gd->reloc_off;
548 const char *genimg_get_os_name (uint8_t os)
550 return (get_table_entry_name (uimage_os, "Unknown OS", os));
553 const char *genimg_get_arch_name (uint8_t arch)
555 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
558 const char *genimg_get_type_name (uint8_t type)
560 return (get_table_entry_name (uimage_type, "Unknown Image", type));
563 const char *genimg_get_comp_name (uint8_t comp)
565 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
569 * get_table_entry_id - translate short entry name to id
570 * @table: pointer to a translation table for entries of a specific type
571 * @table_name: to be used in case of error
572 * @name: entry short name to be translated
574 * get_table_entry_id() will go over translation table trying to find
575 * entry that matches given short name. If matching entry is found,
576 * its id returned to the caller.
579 * entry id if translation succeeds
582 int get_table_entry_id(const table_entry_t *table,
583 const char *table_name, const char *name)
585 const table_entry_t *t;
589 for (t = table; t->id >= 0; ++t) {
590 if (t->sname && strcasecmp(t->sname, name) == 0)
594 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
595 for (t = table; t->id >= 0; ++t) {
596 if (t->sname == NULL)
598 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
601 fprintf (stderr, "\n");
603 for (t = table; t->id >= 0; ++t) {
604 #ifdef CONFIG_NEEDS_MANUAL_RELOC
605 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
607 if (t->sname && strcmp(t->sname, name) == 0)
611 debug ("Invalid %s Type: %s\n", table_name, name);
612 #endif /* USE_HOSTCC */
616 int genimg_get_os_id (const char *name)
618 return (get_table_entry_id (uimage_os, "OS", name));
621 int genimg_get_arch_id (const char *name)
623 return (get_table_entry_id (uimage_arch, "CPU", name));
626 int genimg_get_type_id (const char *name)
628 return (get_table_entry_id (uimage_type, "Image", name));
631 int genimg_get_comp_id (const char *name)
633 return (get_table_entry_id (uimage_comp, "Compression", name));
638 * genimg_get_format - get image format type
639 * @img_addr: image start address
641 * genimg_get_format() checks whether provided address points to a valid
642 * legacy or FIT image.
644 * New uImage format and FDT blob are based on a libfdt. FDT blob
645 * may be passed directly or embedded in a FIT image. In both situations
646 * genimg_get_format() must be able to dectect libfdt header.
649 * image format type or IMAGE_FORMAT_INVALID if no image is present
651 int genimg_get_format (void *img_addr)
653 ulong format = IMAGE_FORMAT_INVALID;
654 const image_header_t *hdr;
655 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
659 hdr = (const image_header_t *)img_addr;
660 if (image_check_magic(hdr))
661 format = IMAGE_FORMAT_LEGACY;
662 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
664 fit_hdr = (char *)img_addr;
665 if (fdt_check_header (fit_hdr) == 0)
666 format = IMAGE_FORMAT_FIT;
674 * genimg_get_image - get image from special storage (if necessary)
675 * @img_addr: image start address
677 * genimg_get_image() checks if provided image start adddress is located
678 * in a dataflash storage. If so, image is moved to a system RAM memory.
681 * image start address after possible relocation from special storage
683 ulong genimg_get_image (ulong img_addr)
685 ulong ram_addr = img_addr;
687 #ifdef CONFIG_HAS_DATAFLASH
688 ulong h_size, d_size;
690 if (addr_dataflash (img_addr)){
691 /* ger RAM address */
692 ram_addr = CONFIG_SYS_LOAD_ADDR;
694 /* get header size */
695 h_size = image_get_header_size ();
696 #if defined(CONFIG_FIT)
697 if (sizeof(struct fdt_header) > h_size)
698 h_size = sizeof(struct fdt_header);
702 debug (" Reading image header from dataflash address "
703 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
705 read_dataflash (img_addr, h_size, (char *)ram_addr);
708 switch (genimg_get_format ((void *)ram_addr)) {
709 case IMAGE_FORMAT_LEGACY:
710 d_size = image_get_data_size ((const image_header_t *)ram_addr);
711 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
714 #if defined(CONFIG_FIT)
715 case IMAGE_FORMAT_FIT:
716 d_size = fit_get_size ((const void *)ram_addr) - h_size;
717 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
722 printf (" No valid image found at 0x%08lx\n", img_addr);
726 /* read in image data */
727 debug (" Reading image remaining data from dataflash address "
728 "%08lx to RAM address %08lx\n", img_addr + h_size,
731 read_dataflash (img_addr + h_size, d_size,
732 (char *)(ram_addr + h_size));
735 #endif /* CONFIG_HAS_DATAFLASH */
741 * fit_has_config - check if there is a valid FIT configuration
742 * @images: pointer to the bootm command headers structure
744 * fit_has_config() checks if there is a FIT configuration in use
745 * (if FTI support is present).
748 * 0, no FIT support or no configuration found
749 * 1, configuration found
751 int genimg_has_config (bootm_headers_t *images)
753 #if defined(CONFIG_FIT)
754 if (images->fit_uname_cfg)
761 * boot_get_ramdisk - main ramdisk handling routine
762 * @argc: command argument count
763 * @argv: command argument list
764 * @images: pointer to the bootm images structure
765 * @arch: expected ramdisk architecture
766 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
767 * @rd_end: pointer to a ulong variable, will hold ramdisk end
769 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
770 * Curently supported are the following ramdisk sources:
771 * - multicomponent kernel/ramdisk image,
772 * - commandline provided address of decicated ramdisk image.
775 * 0, if ramdisk image was found and valid, or skiped
776 * rd_start and rd_end are set to ramdisk start/end addresses if
777 * ramdisk image is found and valid
779 * 1, if ramdisk image is found but corrupted, or invalid
780 * rd_start and rd_end are set to 0 if no ramdisk exists
782 int boot_get_ramdisk (int argc, char * const argv[], bootm_headers_t *images,
783 uint8_t arch, ulong *rd_start, ulong *rd_end)
785 ulong rd_addr, rd_load;
786 ulong rd_data, rd_len;
787 const image_header_t *rd_hdr;
788 #if defined(CONFIG_FIT)
790 const char *fit_uname_config = NULL;
791 const char *fit_uname_ramdisk = NULL;
803 * Look for a '-' which indicates to ignore the
806 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
807 debug ("## Skipping init Ramdisk\n");
808 rd_len = rd_data = 0;
809 } else if (argc >= 3 || genimg_has_config (images)) {
810 #if defined(CONFIG_FIT)
813 * If the init ramdisk comes from the FIT image and
814 * the FIT image address is omitted in the command
815 * line argument, try to use os FIT image address or
816 * default load address.
818 if (images->fit_uname_os)
819 default_addr = (ulong)images->fit_hdr_os;
821 default_addr = load_addr;
823 if (fit_parse_conf (argv[2], default_addr,
824 &rd_addr, &fit_uname_config)) {
825 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
826 fit_uname_config, rd_addr);
827 } else if (fit_parse_subimage (argv[2], default_addr,
828 &rd_addr, &fit_uname_ramdisk)) {
829 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
830 fit_uname_ramdisk, rd_addr);
834 rd_addr = simple_strtoul(argv[2], NULL, 16);
835 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
838 #if defined(CONFIG_FIT)
840 /* use FIT configuration provided in first bootm
843 rd_addr = (ulong)images->fit_hdr_os;
844 fit_uname_config = images->fit_uname_cfg;
845 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
846 fit_uname_config, rd_addr);
849 * Check whether configuration has ramdisk defined,
850 * if not, don't try to use it, quit silently.
852 fit_hdr = (void *)rd_addr;
853 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
854 if (cfg_noffset < 0) {
855 debug ("* ramdisk: no such config\n");
859 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
860 if (rd_noffset < 0) {
861 debug ("* ramdisk: no ramdisk in config\n");
867 /* copy from dataflash if needed */
868 rd_addr = genimg_get_image (rd_addr);
871 * Check if there is an initrd image at the
872 * address provided in the second bootm argument
873 * check image type, for FIT images get FIT node.
875 switch (genimg_get_format ((void *)rd_addr)) {
876 case IMAGE_FORMAT_LEGACY:
877 printf ("## Loading init Ramdisk from Legacy "
878 "Image at %08lx ...\n", rd_addr);
880 show_boot_progress (9);
881 rd_hdr = image_get_ramdisk (rd_addr, arch,
887 rd_data = image_get_data (rd_hdr);
888 rd_len = image_get_data_size (rd_hdr);
889 rd_load = image_get_load (rd_hdr);
891 #if defined(CONFIG_FIT)
892 case IMAGE_FORMAT_FIT:
893 fit_hdr = (void *)rd_addr;
894 printf ("## Loading init Ramdisk from FIT "
895 "Image at %08lx ...\n", rd_addr);
897 show_boot_progress (120);
898 if (!fit_check_format (fit_hdr)) {
899 puts ("Bad FIT ramdisk image format!\n");
900 show_boot_progress (-120);
903 show_boot_progress (121);
905 if (!fit_uname_ramdisk) {
907 * no ramdisk image node unit name, try to get config
908 * node first. If config unit node name is NULL
909 * fit_conf_get_node() will try to find default config node
911 show_boot_progress (122);
912 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
913 if (cfg_noffset < 0) {
914 puts ("Could not find configuration node\n");
915 show_boot_progress (-122);
918 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
919 printf (" Using '%s' configuration\n", fit_uname_config);
921 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
922 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
924 /* get ramdisk component image node offset */
925 show_boot_progress (123);
926 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
928 if (rd_noffset < 0) {
929 puts ("Could not find subimage node\n");
930 show_boot_progress (-124);
934 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
936 show_boot_progress (125);
937 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
940 /* get ramdisk image data address and length */
941 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
942 puts ("Could not find ramdisk subimage data!\n");
943 show_boot_progress (-127);
946 show_boot_progress (128);
948 rd_data = (ulong)data;
951 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
952 puts ("Can't get ramdisk subimage load address!\n");
953 show_boot_progress (-129);
956 show_boot_progress (129);
958 images->fit_hdr_rd = fit_hdr;
959 images->fit_uname_rd = fit_uname_ramdisk;
960 images->fit_noffset_rd = rd_noffset;
964 puts ("Wrong Ramdisk Image Format\n");
965 rd_data = rd_len = rd_load = 0;
969 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
971 * We need to copy the ramdisk to SRAM to let Linux boot
974 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
977 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
979 } else if (images->legacy_hdr_valid &&
980 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
982 * Now check if we have a legacy mult-component image,
983 * get second entry data start address and len.
985 show_boot_progress (13);
986 printf ("## Loading init Ramdisk from multi component "
987 "Legacy Image at %08lx ...\n",
988 (ulong)images->legacy_hdr_os);
990 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
995 show_boot_progress (14);
996 rd_len = rd_data = 0;
1000 debug ("## No init Ramdisk\n");
1002 *rd_start = rd_data;
1003 *rd_end = rd_data + rd_len;
1005 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1006 *rd_start, *rd_end);
1011 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1013 * boot_ramdisk_high - relocate init ramdisk
1014 * @lmb: pointer to lmb handle, will be used for memory mgmt
1015 * @rd_data: ramdisk data start address
1016 * @rd_len: ramdisk data length
1017 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1018 * start address (after possible relocation)
1019 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1020 * end address (after possible relocation)
1022 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1023 * variable and if requested ramdisk data is moved to a specified location.
1025 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1026 * start/end addresses if ramdisk image start and len were provided,
1027 * otherwise set initrd_start and initrd_end set to zeros.
1033 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1034 ulong *initrd_start, ulong *initrd_end)
1038 int initrd_copy_to_ram = 1;
1040 if ((s = getenv ("initrd_high")) != NULL) {
1041 /* a value of "no" or a similar string will act like 0,
1042 * turning the "load high" feature off. This is intentional.
1044 initrd_high = simple_strtoul (s, NULL, 16);
1045 if (initrd_high == ~0)
1046 initrd_copy_to_ram = 0;
1048 /* not set, no restrictions to load high */
1053 #ifdef CONFIG_LOGBUFFER
1054 /* Prevent initrd from overwriting logbuffer */
1055 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1058 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1059 initrd_high, initrd_copy_to_ram);
1062 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1063 debug (" in-place initrd\n");
1064 *initrd_start = rd_data;
1065 *initrd_end = rd_data + rd_len;
1066 lmb_reserve(lmb, rd_data, rd_len);
1069 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1071 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
1073 if (*initrd_start == 0) {
1074 puts ("ramdisk - allocation error\n");
1077 show_boot_progress (12);
1079 *initrd_end = *initrd_start + rd_len;
1080 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1081 *initrd_start, *initrd_end);
1083 memmove_wd ((void *)*initrd_start,
1084 (void *)rd_data, rd_len, CHUNKSZ);
1092 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1093 *initrd_start, *initrd_end);
1100 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1102 #ifdef CONFIG_OF_LIBFDT
1103 static void fdt_error (const char *msg)
1107 puts (" - must RESET the board to recover.\n");
1110 static const image_header_t *image_get_fdt (ulong fdt_addr)
1112 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1114 image_print_contents (fdt_hdr);
1116 puts (" Verifying Checksum ... ");
1117 if (!image_check_hcrc (fdt_hdr)) {
1118 fdt_error ("fdt header checksum invalid");
1122 if (!image_check_dcrc (fdt_hdr)) {
1123 fdt_error ("fdt checksum invalid");
1128 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1129 fdt_error ("uImage is not a fdt");
1132 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1133 fdt_error ("uImage is compressed");
1136 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1137 fdt_error ("uImage data is not a fdt");
1144 * fit_check_fdt - verify FIT format FDT subimage
1145 * @fit_hdr: pointer to the FIT header
1146 * fdt_noffset: FDT subimage node offset within FIT image
1147 * @verify: data CRC verification flag
1149 * fit_check_fdt() verifies integrity of the FDT subimage and from
1150 * specified FIT image.
1156 #if defined(CONFIG_FIT)
1157 static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1159 fit_image_print (fit, fdt_noffset, " ");
1162 puts (" Verifying Hash Integrity ... ");
1163 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1164 fdt_error ("Bad Data Hash");
1170 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1171 fdt_error ("Not a FDT image");
1175 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1176 fdt_error ("FDT image is compressed");
1182 #endif /* CONFIG_FIT */
1184 #ifndef CONFIG_SYS_FDT_PAD
1185 #define CONFIG_SYS_FDT_PAD 0x3000
1188 #if defined(CONFIG_OF_LIBFDT)
1190 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1191 * @lmb: pointer to lmb handle, will be used for memory mgmt
1192 * @fdt_blob: pointer to fdt blob base address
1194 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1195 * memreserve regions prevents u-boot from using them to store the initrd
1198 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1200 uint64_t addr, size;
1203 if (fdt_check_header (fdt_blob) != 0)
1206 total = fdt_num_mem_rsv(fdt_blob);
1207 for (i = 0; i < total; i++) {
1208 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1210 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1211 (unsigned long long)addr, (unsigned long long)size);
1212 lmb_reserve(lmb, addr, size);
1217 * boot_relocate_fdt - relocate flat device tree
1218 * @lmb: pointer to lmb handle, will be used for memory mgmt
1219 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1220 * @of_size: pointer to a ulong variable, will hold fdt length
1222 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1223 * relocates the of_flat_tree into that region, even if the fdt is already in
1224 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1227 * of_flat_tree and of_size are set to final (after relocation) values
1233 int boot_relocate_fdt (struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1235 void *fdt_blob = *of_flat_tree;
1244 if (fdt_check_header (fdt_blob) != 0) {
1245 fdt_error ("image is not a fdt");
1249 /* position on a 4K boundary before the alloc_current */
1250 /* Pad the FDT by a specified amount */
1251 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1252 of_start = (void *)(unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
1253 getenv_bootm_mapsize() + getenv_bootm_low());
1255 if (of_start == 0) {
1256 puts("device tree - allocation error\n");
1260 debug ("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1261 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1263 printf (" Loading Device Tree to %p, end %p ... ",
1264 of_start, of_start + of_len - 1);
1266 err = fdt_open_into (fdt_blob, of_start, of_len);
1268 fdt_error ("fdt move failed");
1273 *of_flat_tree = of_start;
1276 set_working_fdt_addr(*of_flat_tree);
1282 #endif /* CONFIG_OF_LIBFDT */
1285 * boot_get_fdt - main fdt handling routine
1286 * @argc: command argument count
1287 * @argv: command argument list
1288 * @images: pointer to the bootm images structure
1289 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1290 * @of_size: pointer to a ulong variable, will hold fdt length
1292 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1293 * Curently supported are the following ramdisk sources:
1294 * - multicomponent kernel/ramdisk image,
1295 * - commandline provided address of decicated ramdisk image.
1298 * 0, if fdt image was found and valid, or skipped
1299 * of_flat_tree and of_size are set to fdt start address and length if
1300 * fdt image is found and valid
1302 * 1, if fdt image is found but corrupted
1303 * of_flat_tree and of_size are set to 0 if no fdt exists
1305 int boot_get_fdt (int flag, int argc, char * const argv[], bootm_headers_t *images,
1306 char **of_flat_tree, ulong *of_size)
1308 const image_header_t *fdt_hdr;
1310 char *fdt_blob = NULL;
1311 ulong image_start, image_end;
1312 ulong load_start, load_end;
1313 #if defined(CONFIG_FIT)
1315 const char *fit_uname_config = NULL;
1316 const char *fit_uname_fdt = NULL;
1324 *of_flat_tree = NULL;
1327 if (argc > 3 || genimg_has_config (images)) {
1328 #if defined(CONFIG_FIT)
1331 * If the FDT blob comes from the FIT image and the
1332 * FIT image address is omitted in the command line
1333 * argument, try to use ramdisk or os FIT image
1334 * address or default load address.
1336 if (images->fit_uname_rd)
1337 default_addr = (ulong)images->fit_hdr_rd;
1338 else if (images->fit_uname_os)
1339 default_addr = (ulong)images->fit_hdr_os;
1341 default_addr = load_addr;
1343 if (fit_parse_conf (argv[3], default_addr,
1344 &fdt_addr, &fit_uname_config)) {
1345 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1346 fit_uname_config, fdt_addr);
1347 } else if (fit_parse_subimage (argv[3], default_addr,
1348 &fdt_addr, &fit_uname_fdt)) {
1349 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1350 fit_uname_fdt, fdt_addr);
1354 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1355 debug ("* fdt: cmdline image address = 0x%08lx\n",
1358 #if defined(CONFIG_FIT)
1360 /* use FIT configuration provided in first bootm
1363 fdt_addr = (ulong)images->fit_hdr_os;
1364 fit_uname_config = images->fit_uname_cfg;
1365 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1366 fit_uname_config, fdt_addr);
1369 * Check whether configuration has FDT blob defined,
1370 * if not quit silently.
1372 fit_hdr = (void *)fdt_addr;
1373 cfg_noffset = fit_conf_get_node (fit_hdr,
1375 if (cfg_noffset < 0) {
1376 debug ("* fdt: no such config\n");
1380 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1382 if (fdt_noffset < 0) {
1383 debug ("* fdt: no fdt in config\n");
1389 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1392 /* copy from dataflash if needed */
1393 fdt_addr = genimg_get_image (fdt_addr);
1396 * Check if there is an FDT image at the
1397 * address provided in the second bootm argument
1398 * check image type, for FIT images get a FIT node.
1400 switch (genimg_get_format ((void *)fdt_addr)) {
1401 case IMAGE_FORMAT_LEGACY:
1402 /* verify fdt_addr points to a valid image header */
1403 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1405 fdt_hdr = image_get_fdt (fdt_addr);
1410 * move image data to the load address,
1411 * make sure we don't overwrite initial image
1413 image_start = (ulong)fdt_hdr;
1414 image_end = image_get_image_end (fdt_hdr);
1416 load_start = image_get_load (fdt_hdr);
1417 load_end = load_start + image_get_data_size (fdt_hdr);
1419 if ((load_start < image_end) && (load_end > image_start)) {
1420 fdt_error ("fdt overwritten");
1424 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1425 image_get_data (fdt_hdr), load_start);
1427 memmove ((void *)load_start,
1428 (void *)image_get_data (fdt_hdr),
1429 image_get_data_size (fdt_hdr));
1431 fdt_blob = (char *)load_start;
1433 case IMAGE_FORMAT_FIT:
1435 * This case will catch both: new uImage format
1436 * (libfdt based) and raw FDT blob (also libfdt
1439 #if defined(CONFIG_FIT)
1440 /* check FDT blob vs FIT blob */
1441 if (fit_check_format ((const void *)fdt_addr)) {
1445 fit_hdr = (void *)fdt_addr;
1446 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1449 if (!fit_uname_fdt) {
1451 * no FDT blob image node unit name,
1452 * try to get config node first. If
1453 * config unit node name is NULL
1454 * fit_conf_get_node() will try to
1455 * find default config node
1457 cfg_noffset = fit_conf_get_node (fit_hdr,
1460 if (cfg_noffset < 0) {
1461 fdt_error ("Could not find configuration node\n");
1465 fit_uname_config = fdt_get_name (fit_hdr,
1467 printf (" Using '%s' configuration\n",
1470 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1472 fit_uname_fdt = fit_get_name (fit_hdr,
1475 /* get FDT component image node offset */
1476 fdt_noffset = fit_image_get_node (fit_hdr,
1479 if (fdt_noffset < 0) {
1480 fdt_error ("Could not find subimage node\n");
1484 printf (" Trying '%s' FDT blob subimage\n",
1487 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1491 /* get ramdisk image data address and length */
1492 if (fit_image_get_data (fit_hdr, fdt_noffset,
1494 fdt_error ("Could not find FDT subimage data");
1498 /* verift that image data is a proper FDT blob */
1499 if (fdt_check_header ((char *)data) != 0) {
1500 fdt_error ("Subimage data is not a FTD");
1505 * move image data to the load address,
1506 * make sure we don't overwrite initial image
1508 image_start = (ulong)fit_hdr;
1509 image_end = fit_get_end (fit_hdr);
1511 if (fit_image_get_load (fit_hdr, fdt_noffset,
1512 &load_start) == 0) {
1513 load_end = load_start + size;
1515 if ((load_start < image_end) &&
1516 (load_end > image_start)) {
1517 fdt_error ("FDT overwritten");
1521 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1522 (ulong)data, load_start);
1524 memmove ((void *)load_start,
1525 (void *)data, size);
1527 fdt_blob = (char *)load_start;
1529 fdt_blob = (char *)data;
1532 images->fit_hdr_fdt = fit_hdr;
1533 images->fit_uname_fdt = fit_uname_fdt;
1534 images->fit_noffset_fdt = fdt_noffset;
1542 fdt_blob = (char *)fdt_addr;
1543 debug ("* fdt: raw FDT blob\n");
1544 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1548 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
1552 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1554 } else if (images->legacy_hdr_valid &&
1555 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1557 ulong fdt_data, fdt_len;
1560 * Now check if we have a legacy multi-component image,
1561 * get second entry data start address and len.
1563 printf ("## Flattened Device Tree from multi "
1564 "component Image at %08lX\n",
1565 (ulong)images->legacy_hdr_os);
1567 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1570 fdt_blob = (char *)fdt_data;
1571 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1573 if (fdt_check_header (fdt_blob) != 0) {
1574 fdt_error ("image is not a fdt");
1578 if (fdt_totalsize(fdt_blob) != fdt_len) {
1579 fdt_error ("fdt size != image size");
1583 debug ("## No Flattened Device Tree\n");
1587 debug ("## No Flattened Device Tree\n");
1591 *of_flat_tree = fdt_blob;
1592 *of_size = fdt_totalsize(fdt_blob);
1593 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
1594 (ulong)*of_flat_tree, *of_size);
1603 #endif /* CONFIG_OF_LIBFDT */
1605 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1607 * boot_get_cmdline - allocate and initialize kernel cmdline
1608 * @lmb: pointer to lmb handle, will be used for memory mgmt
1609 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1610 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1612 * boot_get_cmdline() allocates space for kernel command line below
1613 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1614 * variable is present its contents is copied to allocated kernel
1621 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1626 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1627 getenv_bootm_mapsize() + getenv_bootm_low());
1629 if (cmdline == NULL)
1632 if ((s = getenv("bootargs")) == NULL)
1637 *cmd_start = (ulong) & cmdline[0];
1638 *cmd_end = *cmd_start + strlen(cmdline);
1640 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1644 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1646 #ifdef CONFIG_SYS_BOOT_GET_KBD
1648 * boot_get_kbd - allocate and initialize kernel copy of board info
1649 * @lmb: pointer to lmb handle, will be used for memory mgmt
1650 * @kbd: double pointer to board info data
1652 * boot_get_kbd() allocates space for kernel copy of board info data below
1653 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1654 * with the current u-boot board info data.
1660 int boot_get_kbd (struct lmb *lmb, bd_t **kbd)
1662 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1663 getenv_bootm_mapsize() + getenv_bootm_low());
1669 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1671 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1672 do_bdinfo(NULL, 0, 0, NULL);
1677 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1678 #endif /* !USE_HOSTCC */
1680 #if defined(CONFIG_FIT)
1681 /*****************************************************************************/
1682 /* New uImage format routines */
1683 /*****************************************************************************/
1685 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1686 ulong *addr, const char **name)
1693 sep = strchr (spec, sepc);
1696 *addr = simple_strtoul (spec, NULL, 16);
1706 * fit_parse_conf - parse FIT configuration spec
1707 * @spec: input string, containing configuration spec
1708 * @add_curr: current image address (to be used as a possible default)
1709 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1711 * @conf_name double pointer to a char, will hold pointer to a configuration
1714 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1715 * where <addr> is a FIT image address that contains configuration
1716 * with a <conf> unit name.
1718 * Address part is optional, and if omitted default add_curr will
1722 * 1 if spec is a valid configuration string,
1723 * addr and conf_name are set accordingly
1726 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1727 ulong *addr, const char **conf_name)
1729 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1733 * fit_parse_subimage - parse FIT subimage spec
1734 * @spec: input string, containing subimage spec
1735 * @add_curr: current image address (to be used as a possible default)
1736 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1738 * @image_name: double pointer to a char, will hold pointer to a subimage name
1740 * fit_parse_subimage() expects subimage spec in the for of
1741 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1742 * subimage with a <subimg> unit name.
1744 * Address part is optional, and if omitted default add_curr will
1748 * 1 if spec is a valid subimage string,
1749 * addr and image_name are set accordingly
1752 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1753 ulong *addr, const char **image_name)
1755 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1757 #endif /* !USE_HOSTCC */
1759 static void fit_get_debug (const void *fit, int noffset,
1760 char *prop_name, int err)
1762 debug ("Can't get '%s' property from FIT 0x%08lx, "
1763 "node: offset %d, name %s (%s)\n",
1764 prop_name, (ulong)fit, noffset,
1765 fit_get_name (fit, noffset, NULL),
1766 fdt_strerror (err));
1770 * fit_print_contents - prints out the contents of the FIT format image
1771 * @fit: pointer to the FIT format image header
1772 * @p: pointer to prefix string
1774 * fit_print_contents() formats a multi line FIT image contents description.
1775 * The routine prints out FIT image properties (root node level) follwed by
1776 * the details of each component image.
1779 * no returned results
1781 void fit_print_contents (const void *fit)
1792 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1802 /* Root node properties */
1803 ret = fit_get_desc (fit, 0, &desc);
1804 printf ("%sFIT description: ", p);
1806 printf ("unavailable\n");
1808 printf ("%s\n", desc);
1810 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1811 ret = fit_get_timestamp (fit, 0, ×tamp);
1812 printf ("%sCreated: ", p);
1814 printf ("unavailable\n");
1816 genimg_print_time (timestamp);
1819 /* Find images parent node offset */
1820 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1821 if (images_noffset < 0) {
1822 printf ("Can't find images parent node '%s' (%s)\n",
1823 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1827 /* Process its subnodes, print out component images details */
1828 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1829 (noffset >= 0) && (ndepth > 0);
1830 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1833 * Direct child node of the images parent node,
1834 * i.e. component image node.
1836 printf ("%s Image %u (%s)\n", p, count++,
1837 fit_get_name(fit, noffset, NULL));
1839 fit_image_print (fit, noffset, p);
1843 /* Find configurations parent node offset */
1844 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1845 if (confs_noffset < 0) {
1846 debug ("Can't get configurations parent node '%s' (%s)\n",
1847 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1851 /* get default configuration unit name from default property */
1852 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1854 printf ("%s Default Configuration: '%s'\n", p, uname);
1856 /* Process its subnodes, print out configurations details */
1857 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1858 (noffset >= 0) && (ndepth > 0);
1859 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1862 * Direct child node of the configurations parent node,
1863 * i.e. configuration node.
1865 printf ("%s Configuration %u (%s)\n", p, count++,
1866 fit_get_name(fit, noffset, NULL));
1868 fit_conf_print (fit, noffset, p);
1874 * fit_image_print - prints out the FIT component image details
1875 * @fit: pointer to the FIT format image header
1876 * @image_noffset: offset of the component image node
1877 * @p: pointer to prefix string
1879 * fit_image_print() lists all mandatory properies for the processed component
1880 * image. If present, hash nodes are printed out as well. Load
1881 * address for images of type firmware is also printed out. Since the load
1882 * address is not mandatory for firmware images, it will be output as
1883 * "unavailable" when not present.
1886 * no returned results
1888 void fit_image_print (const void *fit, int image_noffset, const char *p)
1891 uint8_t type, arch, os, comp;
1899 /* Mandatory properties */
1900 ret = fit_get_desc (fit, image_noffset, &desc);
1901 printf ("%s Description: ", p);
1903 printf ("unavailable\n");
1905 printf ("%s\n", desc);
1907 fit_image_get_type (fit, image_noffset, &type);
1908 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1910 fit_image_get_comp (fit, image_noffset, &comp);
1911 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1913 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1916 printf ("%s Data Start: ", p);
1918 printf ("unavailable\n");
1920 printf ("0x%08lx\n", (ulong)data);
1923 printf ("%s Data Size: ", p);
1925 printf ("unavailable\n");
1927 genimg_print_size (size);
1929 /* Remaining, type dependent properties */
1930 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1931 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1932 (type == IH_TYPE_FLATDT)) {
1933 fit_image_get_arch (fit, image_noffset, &arch);
1934 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1937 if (type == IH_TYPE_KERNEL) {
1938 fit_image_get_os (fit, image_noffset, &os);
1939 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1942 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1943 (type == IH_TYPE_FIRMWARE)) {
1944 ret = fit_image_get_load (fit, image_noffset, &load);
1945 printf ("%s Load Address: ", p);
1947 printf ("unavailable\n");
1949 printf ("0x%08lx\n", load);
1952 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1953 fit_image_get_entry (fit, image_noffset, &entry);
1954 printf ("%s Entry Point: ", p);
1956 printf ("unavailable\n");
1958 printf ("0x%08lx\n", entry);
1961 /* Process all hash subnodes of the component image node */
1962 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1963 (noffset >= 0) && (ndepth > 0);
1964 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1966 /* Direct child node of the component image node */
1967 fit_image_print_hash (fit, noffset, p);
1973 * fit_image_print_hash - prints out the hash node details
1974 * @fit: pointer to the FIT format image header
1975 * @noffset: offset of the hash node
1976 * @p: pointer to prefix string
1978 * fit_image_print_hash() lists properies for the processed hash node
1981 * no returned results
1983 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1991 * Check subnode name, must be equal to "hash".
1992 * Multiple hash nodes require unique unit node
1993 * names, e.g. hash@1, hash@2, etc.
1995 if (strncmp (fit_get_name(fit, noffset, NULL),
1997 strlen(FIT_HASH_NODENAME)) != 0)
2000 debug ("%s Hash node: '%s'\n", p,
2001 fit_get_name (fit, noffset, NULL));
2003 printf ("%s Hash algo: ", p);
2004 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2005 printf ("invalid/unsupported\n");
2008 printf ("%s\n", algo);
2010 ret = fit_image_hash_get_value (fit, noffset, &value,
2012 printf ("%s Hash value: ", p);
2014 printf ("unavailable\n");
2016 for (i = 0; i < value_len; i++)
2017 printf ("%02x", value[i]);
2021 debug ("%s Hash len: %d\n", p, value_len);
2025 * fit_get_desc - get node description property
2026 * @fit: pointer to the FIT format image header
2027 * @noffset: node offset
2028 * @desc: double pointer to the char, will hold pointer to the descrption
2030 * fit_get_desc() reads description property from a given node, if
2031 * description is found pointer to it is returened in third call argument.
2037 int fit_get_desc (const void *fit, int noffset, char **desc)
2041 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2042 if (*desc == NULL) {
2043 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2051 * fit_get_timestamp - get node timestamp property
2052 * @fit: pointer to the FIT format image header
2053 * @noffset: node offset
2054 * @timestamp: pointer to the time_t, will hold read timestamp
2056 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2057 * is found and has a correct size its value is retured in third call
2062 * -1, on property read failure
2063 * -2, on wrong timestamp size
2065 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2070 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2072 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2075 if (len != sizeof (uint32_t)) {
2076 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2080 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2085 * fit_image_get_node - get node offset for component image of a given unit name
2086 * @fit: pointer to the FIT format image header
2087 * @image_uname: component image node unit name
2089 * fit_image_get_node() finds a component image (withing the '/images'
2090 * node) of a provided unit name. If image is found its node offset is
2091 * returned to the caller.
2094 * image node offset when found (>=0)
2095 * negative number on failure (FDT_ERR_* code)
2097 int fit_image_get_node (const void *fit, const char *image_uname)
2099 int noffset, images_noffset;
2101 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2102 if (images_noffset < 0) {
2103 debug ("Can't find images parent node '%s' (%s)\n",
2104 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2105 return images_noffset;
2108 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2110 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2111 image_uname, fdt_strerror (noffset));
2118 * fit_image_get_os - get os id for a given component image node
2119 * @fit: pointer to the FIT format image header
2120 * @noffset: component image node offset
2121 * @os: pointer to the uint8_t, will hold os numeric id
2123 * fit_image_get_os() finds os property in a given component image node.
2124 * If the property is found, its (string) value is translated to the numeric
2125 * id which is returned to the caller.
2131 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2136 /* Get OS name from property data */
2137 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2139 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2144 /* Translate OS name to id */
2145 *os = genimg_get_os_id (data);
2150 * fit_image_get_arch - get arch id for a given component image node
2151 * @fit: pointer to the FIT format image header
2152 * @noffset: component image node offset
2153 * @arch: pointer to the uint8_t, will hold arch numeric id
2155 * fit_image_get_arch() finds arch property in a given component image node.
2156 * If the property is found, its (string) value is translated to the numeric
2157 * id which is returned to the caller.
2163 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2168 /* Get architecture name from property data */
2169 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2171 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2176 /* Translate architecture name to id */
2177 *arch = genimg_get_arch_id (data);
2182 * fit_image_get_type - get type id for a given component image node
2183 * @fit: pointer to the FIT format image header
2184 * @noffset: component image node offset
2185 * @type: pointer to the uint8_t, will hold type numeric id
2187 * fit_image_get_type() finds type property in a given component image node.
2188 * If the property is found, its (string) value is translated to the numeric
2189 * id which is returned to the caller.
2195 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2200 /* Get image type name from property data */
2201 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2203 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2208 /* Translate image type name to id */
2209 *type = genimg_get_type_id (data);
2214 * fit_image_get_comp - get comp id for a given component image node
2215 * @fit: pointer to the FIT format image header
2216 * @noffset: component image node offset
2217 * @comp: pointer to the uint8_t, will hold comp numeric id
2219 * fit_image_get_comp() finds comp property in a given component image node.
2220 * If the property is found, its (string) value is translated to the numeric
2221 * id which is returned to the caller.
2227 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2232 /* Get compression name from property data */
2233 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2235 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2240 /* Translate compression name to id */
2241 *comp = genimg_get_comp_id (data);
2246 * fit_image_get_load - get load address property for a given component image node
2247 * @fit: pointer to the FIT format image header
2248 * @noffset: component image node offset
2249 * @load: pointer to the uint32_t, will hold load address
2251 * fit_image_get_load() finds load address property in a given component image node.
2252 * If the property is found, its value is returned to the caller.
2258 int fit_image_get_load (const void *fit, int noffset, ulong *load)
2261 const uint32_t *data;
2263 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2265 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2269 *load = uimage_to_cpu (*data);
2274 * fit_image_get_entry - get entry point address property for a given component image node
2275 * @fit: pointer to the FIT format image header
2276 * @noffset: component image node offset
2277 * @entry: pointer to the uint32_t, will hold entry point address
2279 * fit_image_get_entry() finds entry point address property in a given component image node.
2280 * If the property is found, its value is returned to the caller.
2286 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2289 const uint32_t *data;
2291 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2293 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2297 *entry = uimage_to_cpu (*data);
2302 * fit_image_get_data - get data property and its size for a given component image node
2303 * @fit: pointer to the FIT format image header
2304 * @noffset: component image node offset
2305 * @data: double pointer to void, will hold data property's data address
2306 * @size: pointer to size_t, will hold data property's data size
2308 * fit_image_get_data() finds data property in a given component image node.
2309 * If the property is found its data start address and size are returned to
2316 int fit_image_get_data (const void *fit, int noffset,
2317 const void **data, size_t *size)
2321 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2322 if (*data == NULL) {
2323 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2333 * fit_image_hash_get_algo - get hash algorithm name
2334 * @fit: pointer to the FIT format image header
2335 * @noffset: hash node offset
2336 * @algo: double pointer to char, will hold pointer to the algorithm name
2338 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2339 * If the property is found its data start address is returned to the caller.
2345 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2349 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2350 if (*algo == NULL) {
2351 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2359 * fit_image_hash_get_value - get hash value and length
2360 * @fit: pointer to the FIT format image header
2361 * @noffset: hash node offset
2362 * @value: double pointer to uint8_t, will hold address of a hash value data
2363 * @value_len: pointer to an int, will hold hash data length
2365 * fit_image_hash_get_value() finds hash value property in a given hash node.
2366 * If the property is found its data start address and size are returned to
2373 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2378 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2379 if (*value == NULL) {
2380 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2390 * fit_set_timestamp - set node timestamp property
2391 * @fit: pointer to the FIT format image header
2392 * @noffset: node offset
2393 * @timestamp: timestamp value to be set
2395 * fit_set_timestamp() attempts to set timestamp property in the requested
2396 * node and returns operation status to the caller.
2400 * -1, on property read failure
2402 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2407 t = cpu_to_uimage (timestamp);
2408 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2411 printf ("Can't set '%s' property for '%s' node (%s)\n",
2412 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2413 fdt_strerror (ret));
2421 * calculate_hash - calculate and return hash for provided input data
2422 * @data: pointer to the input data
2423 * @data_len: data length
2424 * @algo: requested hash algorithm
2425 * @value: pointer to the char, will hold hash value data (caller must
2426 * allocate enough free space)
2427 * value_len: length of the calculated hash
2429 * calculate_hash() computes input data hash according to the requested algorithm.
2430 * Resulting hash value is placed in caller provided 'value' buffer, length
2431 * of the calculated hash is returned via value_len pointer argument.
2435 * -1, when algo is unsupported
2437 static int calculate_hash (const void *data, int data_len, const char *algo,
2438 uint8_t *value, int *value_len)
2440 if (strcmp (algo, "crc32") == 0 ) {
2441 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2443 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2445 } else if (strcmp (algo, "sha1") == 0 ) {
2446 sha1_csum_wd ((unsigned char *) data, data_len,
2447 (unsigned char *) value, CHUNKSZ_SHA1);
2449 } else if (strcmp (algo, "md5") == 0 ) {
2450 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2453 debug ("Unsupported hash alogrithm\n");
2461 * fit_set_hashes - process FIT component image nodes and calculate hashes
2462 * @fit: pointer to the FIT format image header
2464 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2465 * Hashes are calculated for all component images which have hash subnodes
2466 * with algorithm property set to one of the supported hash algorithms.
2470 * libfdt error code, on failure
2472 int fit_set_hashes (void *fit)
2479 /* Find images parent node offset */
2480 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2481 if (images_noffset < 0) {
2482 printf ("Can't find images parent node '%s' (%s)\n",
2483 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2484 return images_noffset;
2487 /* Process its subnodes, print out component images details */
2488 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2489 (noffset >= 0) && (ndepth > 0);
2490 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2493 * Direct child node of the images parent node,
2494 * i.e. component image node.
2496 ret = fit_image_set_hashes (fit, noffset);
2506 * fit_image_set_hashes - calculate/set hashes for given component image node
2507 * @fit: pointer to the FIT format image header
2508 * @image_noffset: requested component image node
2510 * fit_image_set_hashes() adds hash values for an component image node. All
2511 * existing hash subnodes are checked, if algorithm property is set to one of
2512 * the supported hash algorithms, hash value is computed and corresponding
2513 * hash node property is set, for example:
2515 * Input component image node structure:
2517 * o image@1 (at image_noffset)
2518 * | - data = [binary data]
2522 * Output component image node structure:
2524 * o image@1 (at image_noffset)
2525 * | - data = [binary data]
2528 * |- value = sha1(data)
2534 int fit_image_set_hashes (void *fit, int image_noffset)
2539 uint8_t value[FIT_MAX_HASH_LEN];
2544 /* Get image data and data length */
2545 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2546 printf ("Can't get image data/size\n");
2550 /* Process all hash subnodes of the component image node */
2551 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2552 (noffset >= 0) && (ndepth > 0);
2553 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2555 /* Direct child node of the component image node */
2558 * Check subnode name, must be equal to "hash".
2559 * Multiple hash nodes require unique unit node
2560 * names, e.g. hash@1, hash@2, etc.
2562 if (strncmp (fit_get_name(fit, noffset, NULL),
2564 strlen(FIT_HASH_NODENAME)) != 0) {
2565 /* Not a hash subnode, skip it */
2569 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2570 printf ("Can't get hash algo property for "
2571 "'%s' hash node in '%s' image node\n",
2572 fit_get_name (fit, noffset, NULL),
2573 fit_get_name (fit, image_noffset, NULL));
2577 if (calculate_hash (data, size, algo, value, &value_len)) {
2578 printf ("Unsupported hash algorithm (%s) for "
2579 "'%s' hash node in '%s' image node\n",
2580 algo, fit_get_name (fit, noffset, NULL),
2581 fit_get_name (fit, image_noffset, NULL));
2585 if (fit_image_hash_set_value (fit, noffset, value,
2587 printf ("Can't set hash value for "
2588 "'%s' hash node in '%s' image node\n",
2589 fit_get_name (fit, noffset, NULL),
2590 fit_get_name (fit, image_noffset, NULL));
2600 * fit_image_hash_set_value - set hash value in requested has node
2601 * @fit: pointer to the FIT format image header
2602 * @noffset: hash node offset
2603 * @value: hash value to be set
2604 * @value_len: hash value length
2606 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2607 * given and returns operation status to the caller.
2613 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2618 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2620 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2621 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2622 fdt_strerror (ret));
2628 #endif /* USE_HOSTCC */
2631 * fit_image_check_hashes - verify data intergity
2632 * @fit: pointer to the FIT format image header
2633 * @image_noffset: component image node offset
2635 * fit_image_check_hashes() goes over component image hash nodes,
2636 * re-calculates each data hash and compares with the value stored in hash
2640 * 1, if all hashes are valid
2641 * 0, otherwise (or on error)
2643 int fit_image_check_hashes (const void *fit, int image_noffset)
2650 uint8_t value[FIT_MAX_HASH_LEN];
2656 /* Get image data and data length */
2657 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2658 printf ("Can't get image data/size\n");
2662 /* Process all hash subnodes of the component image node */
2663 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2664 (noffset >= 0) && (ndepth > 0);
2665 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2667 /* Direct child node of the component image node */
2670 * Check subnode name, must be equal to "hash".
2671 * Multiple hash nodes require unique unit node
2672 * names, e.g. hash@1, hash@2, etc.
2674 if (strncmp (fit_get_name(fit, noffset, NULL),
2676 strlen(FIT_HASH_NODENAME)) != 0)
2679 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2680 err_msg = " error!\nCan't get hash algo "
2684 printf ("%s", algo);
2686 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2688 err_msg = " error!\nCan't get hash value "
2693 if (calculate_hash (data, size, algo, value, &value_len)) {
2694 err_msg = " error!\nUnsupported hash algorithm";
2698 if (value_len != fit_value_len) {
2699 err_msg = " error !\nBad hash value len";
2701 } else if (memcmp (value, fit_value, value_len) != 0) {
2702 err_msg = " error!\nBad hash value";
2712 printf ("%s for '%s' hash node in '%s' image node\n",
2713 err_msg, fit_get_name (fit, noffset, NULL),
2714 fit_get_name (fit, image_noffset, NULL));
2719 * fit_all_image_check_hashes - verify data intergity for all images
2720 * @fit: pointer to the FIT format image header
2722 * fit_all_image_check_hashes() goes over all images in the FIT and
2723 * for every images checks if all it's hashes are valid.
2726 * 1, if all hashes of all images are valid
2727 * 0, otherwise (or on error)
2729 int fit_all_image_check_hashes (const void *fit)
2736 /* Find images parent node offset */
2737 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2738 if (images_noffset < 0) {
2739 printf ("Can't find images parent node '%s' (%s)\n",
2740 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2744 /* Process all image subnodes, check hashes for each */
2745 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2747 for (ndepth = 0, count = 0,
2748 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2749 (noffset >= 0) && (ndepth > 0);
2750 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2753 * Direct child node of the images parent node,
2754 * i.e. component image node.
2756 printf (" Hash(es) for Image %u (%s): ", count++,
2757 fit_get_name (fit, noffset, NULL));
2759 if (!fit_image_check_hashes (fit, noffset))
2768 * fit_image_check_os - check whether image node is of a given os type
2769 * @fit: pointer to the FIT format image header
2770 * @noffset: component image node offset
2771 * @os: requested image os
2773 * fit_image_check_os() reads image os property and compares its numeric
2774 * id with the requested os. Comparison result is returned to the caller.
2777 * 1 if image is of given os type
2778 * 0 otherwise (or on error)
2780 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2784 if (fit_image_get_os (fit, noffset, &image_os))
2786 return (os == image_os);
2790 * fit_image_check_arch - check whether image node is of a given arch
2791 * @fit: pointer to the FIT format image header
2792 * @noffset: component image node offset
2793 * @arch: requested imagearch
2795 * fit_image_check_arch() reads image arch property and compares its numeric
2796 * id with the requested arch. Comparison result is returned to the caller.
2799 * 1 if image is of given arch
2800 * 0 otherwise (or on error)
2802 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2806 if (fit_image_get_arch (fit, noffset, &image_arch))
2808 return (arch == image_arch);
2812 * fit_image_check_type - check whether image node is of a given type
2813 * @fit: pointer to the FIT format image header
2814 * @noffset: component image node offset
2815 * @type: requested image type
2817 * fit_image_check_type() reads image type property and compares its numeric
2818 * id with the requested type. Comparison result is returned to the caller.
2821 * 1 if image is of given type
2822 * 0 otherwise (or on error)
2824 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2828 if (fit_image_get_type (fit, noffset, &image_type))
2830 return (type == image_type);
2834 * fit_image_check_comp - check whether image node uses given compression
2835 * @fit: pointer to the FIT format image header
2836 * @noffset: component image node offset
2837 * @comp: requested image compression type
2839 * fit_image_check_comp() reads image compression property and compares its
2840 * numeric id with the requested compression type. Comparison result is
2841 * returned to the caller.
2844 * 1 if image uses requested compression
2845 * 0 otherwise (or on error)
2847 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2851 if (fit_image_get_comp (fit, noffset, &image_comp))
2853 return (comp == image_comp);
2857 * fit_check_format - sanity check FIT image format
2858 * @fit: pointer to the FIT format image header
2860 * fit_check_format() runs a basic sanity FIT image verification.
2861 * Routine checks for mandatory properties, nodes, etc.
2867 int fit_check_format (const void *fit)
2869 /* mandatory / node 'description' property */
2870 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2871 debug ("Wrong FIT format: no description\n");
2875 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2876 /* mandatory / node 'timestamp' property */
2877 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2878 debug ("Wrong FIT format: no timestamp\n");
2883 /* mandatory subimages parent '/images' node */
2884 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2885 debug ("Wrong FIT format: no images parent node\n");
2893 * fit_conf_get_node - get node offset for configuration of a given unit name
2894 * @fit: pointer to the FIT format image header
2895 * @conf_uname: configuration node unit name
2897 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2898 * parant node) of a provided unit name. If configuration is found its node offset
2899 * is returned to the caller.
2901 * When NULL is provided in second argument fit_conf_get_node() will search
2902 * for a default configuration node instead. Default configuration node unit name
2903 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2906 * configuration node offset when found (>=0)
2907 * negative number on failure (FDT_ERR_* code)
2909 int fit_conf_get_node (const void *fit, const char *conf_uname)
2911 int noffset, confs_noffset;
2914 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2915 if (confs_noffset < 0) {
2916 debug ("Can't find configurations parent node '%s' (%s)\n",
2917 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2918 return confs_noffset;
2921 if (conf_uname == NULL) {
2922 /* get configuration unit name from the default property */
2923 debug ("No configuration specified, trying default...\n");
2924 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2925 if (conf_uname == NULL) {
2926 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2929 debug ("Found default configuration: '%s'\n", conf_uname);
2932 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2934 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2935 conf_uname, fdt_strerror (noffset));
2941 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2942 const char *prop_name)
2947 /* get kernel image unit name from configuration kernel property */
2948 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2952 return fit_image_get_node (fit, uname);
2956 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2957 * a given configuration
2958 * @fit: pointer to the FIT format image header
2959 * @noffset: configuration node offset
2961 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2962 * configuration FIT_KERNEL_PROP property and translates it to the node
2966 * image node offset when found (>=0)
2967 * negative number on failure (FDT_ERR_* code)
2969 int fit_conf_get_kernel_node (const void *fit, int noffset)
2971 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2975 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2976 * a given configuration
2977 * @fit: pointer to the FIT format image header
2978 * @noffset: configuration node offset
2980 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2981 * configuration FIT_KERNEL_PROP property and translates it to the node
2985 * image node offset when found (>=0)
2986 * negative number on failure (FDT_ERR_* code)
2988 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2990 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2994 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2995 * a given configuration
2996 * @fit: pointer to the FIT format image header
2997 * @noffset: configuration node offset
2999 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3000 * configuration FIT_KERNEL_PROP property and translates it to the node
3004 * image node offset when found (>=0)
3005 * negative number on failure (FDT_ERR_* code)
3007 int fit_conf_get_fdt_node (const void *fit, int noffset)
3009 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
3013 * fit_conf_print - prints out the FIT configuration details
3014 * @fit: pointer to the FIT format image header
3015 * @noffset: offset of the configuration node
3016 * @p: pointer to prefix string
3018 * fit_conf_print() lists all mandatory properies for the processed
3019 * configuration node.
3022 * no returned results
3024 void fit_conf_print (const void *fit, int noffset, const char *p)
3030 /* Mandatory properties */
3031 ret = fit_get_desc (fit, noffset, &desc);
3032 printf ("%s Description: ", p);
3034 printf ("unavailable\n");
3036 printf ("%s\n", desc);
3038 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3039 printf ("%s Kernel: ", p);
3041 printf ("unavailable\n");
3043 printf ("%s\n", uname);
3045 /* Optional properties */
3046 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3048 printf ("%s Init Ramdisk: %s\n", p, uname);
3050 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3052 printf ("%s FDT: %s\n", p, uname);
3056 * fit_check_ramdisk - verify FIT format ramdisk subimage
3057 * @fit_hdr: pointer to the FIT ramdisk header
3058 * @rd_noffset: ramdisk subimage node offset within FIT image
3059 * @arch: requested ramdisk image architecture type
3060 * @verify: data CRC verification flag
3062 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3063 * specified FIT image.
3070 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3072 fit_image_print (fit, rd_noffset, " ");
3075 puts (" Verifying Hash Integrity ... ");
3076 if (!fit_image_check_hashes (fit, rd_noffset)) {
3077 puts ("Bad Data Hash\n");
3078 show_boot_progress (-125);
3084 show_boot_progress (126);
3085 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3086 !fit_image_check_arch (fit, rd_noffset, arch) ||
3087 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3088 printf ("No Linux %s Ramdisk Image\n",
3089 genimg_get_arch_name(arch));
3090 show_boot_progress (-126);
3094 show_boot_progress (127);
3097 #endif /* USE_HOSTCC */
3098 #endif /* CONFIG_FIT */