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_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 table_entry_t uimage_type[] = {
133 { IH_TYPE_INVALID, NULL, "Invalid Image", },
134 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
135 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
136 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
137 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
138 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
139 { IH_TYPE_SCRIPT, "script", "Script", },
140 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
141 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
142 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
143 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
147 static table_entry_t uimage_comp[] = {
148 { IH_COMP_NONE, "none", "uncompressed", },
149 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
150 { IH_COMP_GZIP, "gzip", "gzip compressed", },
151 { IH_COMP_LZMA, "lzma", "lzma compressed", },
152 { IH_COMP_LZO, "lzo", "lzo compressed", },
156 uint32_t crc32 (uint32_t, const unsigned char *, uint);
157 uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
158 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
159 static void genimg_print_time (time_t timestamp);
162 /*****************************************************************************/
163 /* Legacy format routines */
164 /*****************************************************************************/
165 int image_check_hcrc (const image_header_t *hdr)
168 ulong len = image_get_header_size ();
169 image_header_t header;
171 /* Copy header so we can blank CRC field for re-calculation */
172 memmove (&header, (char *)hdr, image_get_header_size ());
173 image_set_hcrc (&header, 0);
175 hcrc = crc32 (0, (unsigned char *)&header, len);
177 return (hcrc == image_get_hcrc (hdr));
180 int image_check_dcrc (const image_header_t *hdr)
182 ulong data = image_get_data (hdr);
183 ulong len = image_get_data_size (hdr);
184 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
186 return (dcrc == image_get_dcrc (hdr));
190 * image_multi_count - get component (sub-image) count
191 * @hdr: pointer to the header of the multi component image
193 * image_multi_count() returns number of components in a multi
196 * Note: no checking of the image type is done, caller must pass
197 * a valid multi component image.
200 * number of components
202 ulong image_multi_count (const image_header_t *hdr)
207 /* get start of the image payload, which in case of multi
208 * component images that points to a table of component sizes */
209 size = (uint32_t *)image_get_data (hdr);
211 /* count non empty slots */
212 for (i = 0; size[i]; ++i)
219 * image_multi_getimg - get component data address and size
220 * @hdr: pointer to the header of the multi component image
221 * @idx: index of the requested component
222 * @data: pointer to a ulong variable, will hold component data address
223 * @len: pointer to a ulong variable, will hold component size
225 * image_multi_getimg() returns size and data address for the requested
226 * component in a multi component image.
228 * Note: no checking of the image type is done, caller must pass
229 * a valid multi component image.
232 * data address and size of the component, if idx is valid
233 * 0 in data and len, if idx is out of range
235 void image_multi_getimg (const image_header_t *hdr, ulong idx,
236 ulong *data, ulong *len)
240 ulong offset, count, img_data;
242 /* get number of component */
243 count = image_multi_count (hdr);
245 /* get start of the image payload, which in case of multi
246 * component images that points to a table of component sizes */
247 size = (uint32_t *)image_get_data (hdr);
249 /* get address of the proper component data start, which means
250 * skipping sizes table (add 1 for last, null entry) */
251 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
254 *len = uimage_to_cpu (size[idx]);
257 /* go over all indices preceding requested component idx */
258 for (i = 0; i < idx; i++) {
259 /* add up i-th component size, rounding up to 4 bytes */
260 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
263 /* calculate idx-th component data address */
264 *data = img_data + offset;
271 static void image_print_type (const image_header_t *hdr)
273 const char *os, *arch, *type, *comp;
275 os = genimg_get_os_name (image_get_os (hdr));
276 arch = genimg_get_arch_name (image_get_arch (hdr));
277 type = genimg_get_type_name (image_get_type (hdr));
278 comp = genimg_get_comp_name (image_get_comp (hdr));
280 printf ("%s %s %s (%s)\n", arch, os, type, comp);
284 * image_print_contents - prints out the contents of the legacy format image
285 * @ptr: pointer to the legacy format image header
286 * @p: pointer to prefix string
288 * image_print_contents() formats a multi line legacy image contents description.
289 * The routine prints out all header fields followed by the size/offset data
290 * for MULTI/SCRIPT images.
293 * no returned results
295 void image_print_contents (const void *ptr)
297 const image_header_t *hdr = (const image_header_t *)ptr;
306 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
307 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
308 printf ("%sCreated: ", p);
309 genimg_print_time ((time_t)image_get_time (hdr));
311 printf ("%sImage Type: ", p);
312 image_print_type (hdr);
313 printf ("%sData Size: ", p);
314 genimg_print_size (image_get_data_size (hdr));
315 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
316 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
318 if (image_check_type (hdr, IH_TYPE_MULTI) ||
319 image_check_type (hdr, IH_TYPE_SCRIPT)) {
322 ulong count = image_multi_count (hdr);
324 printf ("%sContents:\n", p);
325 for (i = 0; i < count; i++) {
326 image_multi_getimg (hdr, i, &data, &len);
328 printf ("%s Image %d: ", p, i);
329 genimg_print_size (len);
331 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
333 * the user may need to know offsets
334 * if planning to do something with
337 printf ("%s Offset = 0x%08lx\n", p, data);
346 * image_get_ramdisk - get and verify ramdisk image
347 * @rd_addr: ramdisk image start address
348 * @arch: expected ramdisk architecture
349 * @verify: checksum verification flag
351 * image_get_ramdisk() returns a pointer to the verified ramdisk image
352 * header. Routine receives image start address and expected architecture
353 * flag. Verification done covers data and header integrity and os/type/arch
356 * If dataflash support is enabled routine checks for dataflash addresses
357 * and handles required dataflash reads.
360 * pointer to a ramdisk image header, if image was found and valid
361 * otherwise, return NULL
363 static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
366 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
368 if (!image_check_magic (rd_hdr)) {
369 puts ("Bad Magic Number\n");
370 show_boot_progress (-10);
374 if (!image_check_hcrc (rd_hdr)) {
375 puts ("Bad Header Checksum\n");
376 show_boot_progress (-11);
380 show_boot_progress (10);
381 image_print_contents (rd_hdr);
384 puts(" Verifying Checksum ... ");
385 if (!image_check_dcrc (rd_hdr)) {
386 puts ("Bad Data CRC\n");
387 show_boot_progress (-12);
393 show_boot_progress (11);
395 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
396 !image_check_arch (rd_hdr, arch) ||
397 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
398 printf ("No Linux %s Ramdisk Image\n",
399 genimg_get_arch_name(arch));
400 show_boot_progress (-13);
406 #endif /* !USE_HOSTCC */
408 /*****************************************************************************/
409 /* Shared dual-format routines */
410 /*****************************************************************************/
412 int getenv_yesno (char *var)
414 char *s = getenv (var);
415 return (s && (*s == 'n')) ? 0 : 1;
418 ulong getenv_bootm_low(void)
420 char *s = getenv ("bootm_low");
422 ulong tmp = simple_strtoul (s, NULL, 16);
426 #if defined(CONFIG_SYS_SDRAM_BASE)
427 return CONFIG_SYS_SDRAM_BASE;
428 #elif defined(CONFIG_ARM)
429 return gd->bd->bi_dram[0].start;
435 phys_size_t getenv_bootm_size(void)
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 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
462 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
464 size_t tail = (len > chunksz) ? chunksz : len;
466 memmove (to, from, tail);
471 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
472 memmove (to, from, len);
473 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
475 #endif /* !USE_HOSTCC */
477 void genimg_print_size (uint32_t size)
480 printf ("%d Bytes = ", size);
481 print_size (size, "\n");
483 printf ("%d Bytes = %.2f kB = %.2f MB\n",
484 size, (double)size / 1.024e3,
485 (double)size / 1.048576e6);
489 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
490 static void genimg_print_time (time_t timestamp)
495 to_tm (timestamp, &tm);
496 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
497 tm.tm_year, tm.tm_mon, tm.tm_mday,
498 tm.tm_hour, tm.tm_min, tm.tm_sec);
500 printf ("%s", ctime(×tamp));
503 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
506 * get_table_entry_name - translate entry id to long name
507 * @table: pointer to a translation table for entries of a specific type
508 * @msg: message to be returned when translation fails
509 * @id: entry id to be translated
511 * get_table_entry_name() will go over translation table trying to find
512 * entry that matches given id. If matching entry is found, its long
513 * name is returned to the caller.
516 * long entry name if translation succeeds
519 char *get_table_entry_name (table_entry_t *table, char *msg, int id)
521 for (; table->id >= 0; ++table) {
523 #if defined(USE_HOSTCC) || defined(CONFIG_RELOC_FIXUP_WORKS)
526 return table->lname + gd->reloc_off;
532 const char *genimg_get_os_name (uint8_t os)
534 return (get_table_entry_name (uimage_os, "Unknown OS", os));
537 const char *genimg_get_arch_name (uint8_t arch)
539 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
542 const char *genimg_get_type_name (uint8_t type)
544 return (get_table_entry_name (uimage_type, "Unknown Image", type));
547 const char *genimg_get_comp_name (uint8_t comp)
549 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
553 * get_table_entry_id - translate short entry name to id
554 * @table: pointer to a translation table for entries of a specific type
555 * @table_name: to be used in case of error
556 * @name: entry short name to be translated
558 * get_table_entry_id() will go over translation table trying to find
559 * entry that matches given short name. If matching entry is found,
560 * its id returned to the caller.
563 * entry id if translation succeeds
566 int get_table_entry_id (table_entry_t *table,
567 const char *table_name, const char *name)
573 for (t = table; t->id >= 0; ++t) {
574 if (t->sname && strcasecmp(t->sname, name) == 0)
578 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
579 for (t = table; t->id >= 0; ++t) {
580 if (t->sname == NULL)
582 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
585 fprintf (stderr, "\n");
587 for (t = table; t->id >= 0; ++t) {
588 #ifdef CONFIG_RELOC_FIXUP_WORKS
589 if (t->sname && strcmp(t->sname, name) == 0)
591 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
595 debug ("Invalid %s Type: %s\n", table_name, name);
596 #endif /* USE_HOSTCC */
600 int genimg_get_os_id (const char *name)
602 return (get_table_entry_id (uimage_os, "OS", name));
605 int genimg_get_arch_id (const char *name)
607 return (get_table_entry_id (uimage_arch, "CPU", name));
610 int genimg_get_type_id (const char *name)
612 return (get_table_entry_id (uimage_type, "Image", name));
615 int genimg_get_comp_id (const char *name)
617 return (get_table_entry_id (uimage_comp, "Compression", name));
622 * genimg_get_format - get image format type
623 * @img_addr: image start address
625 * genimg_get_format() checks whether provided address points to a valid
626 * legacy or FIT image.
628 * New uImage format and FDT blob are based on a libfdt. FDT blob
629 * may be passed directly or embedded in a FIT image. In both situations
630 * genimg_get_format() must be able to dectect libfdt header.
633 * image format type or IMAGE_FORMAT_INVALID if no image is present
635 int genimg_get_format (void *img_addr)
637 ulong format = IMAGE_FORMAT_INVALID;
638 const image_header_t *hdr;
639 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
643 hdr = (const image_header_t *)img_addr;
644 if (image_check_magic(hdr))
645 format = IMAGE_FORMAT_LEGACY;
646 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
648 fit_hdr = (char *)img_addr;
649 if (fdt_check_header (fit_hdr) == 0)
650 format = IMAGE_FORMAT_FIT;
658 * genimg_get_image - get image from special storage (if necessary)
659 * @img_addr: image start address
661 * genimg_get_image() checks if provided image start adddress is located
662 * in a dataflash storage. If so, image is moved to a system RAM memory.
665 * image start address after possible relocation from special storage
667 ulong genimg_get_image (ulong img_addr)
669 ulong ram_addr = img_addr;
671 #ifdef CONFIG_HAS_DATAFLASH
672 ulong h_size, d_size;
674 if (addr_dataflash (img_addr)){
675 /* ger RAM address */
676 ram_addr = CONFIG_SYS_LOAD_ADDR;
678 /* get header size */
679 h_size = image_get_header_size ();
680 #if defined(CONFIG_FIT)
681 if (sizeof(struct fdt_header) > h_size)
682 h_size = sizeof(struct fdt_header);
686 debug (" Reading image header from dataflash address "
687 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
689 read_dataflash (img_addr, h_size, (char *)ram_addr);
692 switch (genimg_get_format ((void *)ram_addr)) {
693 case IMAGE_FORMAT_LEGACY:
694 d_size = image_get_data_size ((const image_header_t *)ram_addr);
695 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
698 #if defined(CONFIG_FIT)
699 case IMAGE_FORMAT_FIT:
700 d_size = fit_get_size ((const void *)ram_addr) - h_size;
701 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
706 printf (" No valid image found at 0x%08lx\n", img_addr);
710 /* read in image data */
711 debug (" Reading image remaining data from dataflash address "
712 "%08lx to RAM address %08lx\n", img_addr + h_size,
715 read_dataflash (img_addr + h_size, d_size,
716 (char *)(ram_addr + h_size));
719 #endif /* CONFIG_HAS_DATAFLASH */
725 * fit_has_config - check if there is a valid FIT configuration
726 * @images: pointer to the bootm command headers structure
728 * fit_has_config() checks if there is a FIT configuration in use
729 * (if FTI support is present).
732 * 0, no FIT support or no configuration found
733 * 1, configuration found
735 int genimg_has_config (bootm_headers_t *images)
737 #if defined(CONFIG_FIT)
738 if (images->fit_uname_cfg)
745 * boot_get_ramdisk - main ramdisk handling routine
746 * @argc: command argument count
747 * @argv: command argument list
748 * @images: pointer to the bootm images structure
749 * @arch: expected ramdisk architecture
750 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
751 * @rd_end: pointer to a ulong variable, will hold ramdisk end
753 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
754 * Curently supported are the following ramdisk sources:
755 * - multicomponent kernel/ramdisk image,
756 * - commandline provided address of decicated ramdisk image.
759 * 0, if ramdisk image was found and valid, or skiped
760 * rd_start and rd_end are set to ramdisk start/end addresses if
761 * ramdisk image is found and valid
763 * 1, if ramdisk image is found but corrupted, or invalid
764 * rd_start and rd_end are set to 0 if no ramdisk exists
766 int boot_get_ramdisk (int argc, char * const argv[], bootm_headers_t *images,
767 uint8_t arch, ulong *rd_start, ulong *rd_end)
769 ulong rd_addr, rd_load;
770 ulong rd_data, rd_len;
771 const image_header_t *rd_hdr;
772 #if defined(CONFIG_FIT)
774 const char *fit_uname_config = NULL;
775 const char *fit_uname_ramdisk = NULL;
787 * Look for a '-' which indicates to ignore the
790 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
791 debug ("## Skipping init Ramdisk\n");
792 rd_len = rd_data = 0;
793 } else if (argc >= 3 || genimg_has_config (images)) {
794 #if defined(CONFIG_FIT)
797 * If the init ramdisk comes from the FIT image and
798 * the FIT image address is omitted in the command
799 * line argument, try to use os FIT image address or
800 * default load address.
802 if (images->fit_uname_os)
803 default_addr = (ulong)images->fit_hdr_os;
805 default_addr = load_addr;
807 if (fit_parse_conf (argv[2], default_addr,
808 &rd_addr, &fit_uname_config)) {
809 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
810 fit_uname_config, rd_addr);
811 } else if (fit_parse_subimage (argv[2], default_addr,
812 &rd_addr, &fit_uname_ramdisk)) {
813 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
814 fit_uname_ramdisk, rd_addr);
818 rd_addr = simple_strtoul(argv[2], NULL, 16);
819 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
822 #if defined(CONFIG_FIT)
824 /* use FIT configuration provided in first bootm
827 rd_addr = (ulong)images->fit_hdr_os;
828 fit_uname_config = images->fit_uname_cfg;
829 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
830 fit_uname_config, rd_addr);
833 * Check whether configuration has ramdisk defined,
834 * if not, don't try to use it, quit silently.
836 fit_hdr = (void *)rd_addr;
837 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
838 if (cfg_noffset < 0) {
839 debug ("* ramdisk: no such config\n");
843 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
844 if (rd_noffset < 0) {
845 debug ("* ramdisk: no ramdisk in config\n");
851 /* copy from dataflash if needed */
852 rd_addr = genimg_get_image (rd_addr);
855 * Check if there is an initrd image at the
856 * address provided in the second bootm argument
857 * check image type, for FIT images get FIT node.
859 switch (genimg_get_format ((void *)rd_addr)) {
860 case IMAGE_FORMAT_LEGACY:
861 printf ("## Loading init Ramdisk from Legacy "
862 "Image at %08lx ...\n", rd_addr);
864 show_boot_progress (9);
865 rd_hdr = image_get_ramdisk (rd_addr, arch,
871 rd_data = image_get_data (rd_hdr);
872 rd_len = image_get_data_size (rd_hdr);
873 rd_load = image_get_load (rd_hdr);
875 #if defined(CONFIG_FIT)
876 case IMAGE_FORMAT_FIT:
877 fit_hdr = (void *)rd_addr;
878 printf ("## Loading init Ramdisk from FIT "
879 "Image at %08lx ...\n", rd_addr);
881 show_boot_progress (120);
882 if (!fit_check_format (fit_hdr)) {
883 puts ("Bad FIT ramdisk image format!\n");
884 show_boot_progress (-120);
887 show_boot_progress (121);
889 if (!fit_uname_ramdisk) {
891 * no ramdisk image node unit name, try to get config
892 * node first. If config unit node name is NULL
893 * fit_conf_get_node() will try to find default config node
895 show_boot_progress (122);
896 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
897 if (cfg_noffset < 0) {
898 puts ("Could not find configuration node\n");
899 show_boot_progress (-122);
902 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
903 printf (" Using '%s' configuration\n", fit_uname_config);
905 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
906 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
908 /* get ramdisk component image node offset */
909 show_boot_progress (123);
910 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
912 if (rd_noffset < 0) {
913 puts ("Could not find subimage node\n");
914 show_boot_progress (-124);
918 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
920 show_boot_progress (125);
921 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
924 /* get ramdisk image data address and length */
925 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
926 puts ("Could not find ramdisk subimage data!\n");
927 show_boot_progress (-127);
930 show_boot_progress (128);
932 rd_data = (ulong)data;
935 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
936 puts ("Can't get ramdisk subimage load address!\n");
937 show_boot_progress (-129);
940 show_boot_progress (129);
942 images->fit_hdr_rd = fit_hdr;
943 images->fit_uname_rd = fit_uname_ramdisk;
944 images->fit_noffset_rd = rd_noffset;
948 puts ("Wrong Ramdisk Image Format\n");
949 rd_data = rd_len = rd_load = 0;
953 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
955 * We need to copy the ramdisk to SRAM to let Linux boot
958 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
961 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
963 } else if (images->legacy_hdr_valid &&
964 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
966 * Now check if we have a legacy mult-component image,
967 * get second entry data start address and len.
969 show_boot_progress (13);
970 printf ("## Loading init Ramdisk from multi component "
971 "Legacy Image at %08lx ...\n",
972 (ulong)images->legacy_hdr_os);
974 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
979 show_boot_progress (14);
980 rd_len = rd_data = 0;
984 debug ("## No init Ramdisk\n");
987 *rd_end = rd_data + rd_len;
989 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
995 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
997 * boot_ramdisk_high - relocate init ramdisk
998 * @lmb: pointer to lmb handle, will be used for memory mgmt
999 * @rd_data: ramdisk data start address
1000 * @rd_len: ramdisk data length
1001 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1002 * start address (after possible relocation)
1003 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1004 * end address (after possible relocation)
1006 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1007 * variable and if requested ramdisk data is moved to a specified location.
1009 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1010 * start/end addresses if ramdisk image start and len were provided,
1011 * otherwise set initrd_start and initrd_end set to zeros.
1017 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1018 ulong *initrd_start, ulong *initrd_end)
1022 int initrd_copy_to_ram = 1;
1024 if ((s = getenv ("initrd_high")) != NULL) {
1025 /* a value of "no" or a similar string will act like 0,
1026 * turning the "load high" feature off. This is intentional.
1028 initrd_high = simple_strtoul (s, NULL, 16);
1029 if (initrd_high == ~0)
1030 initrd_copy_to_ram = 0;
1032 /* not set, no restrictions to load high */
1037 #ifdef CONFIG_LOGBUFFER
1038 /* Prevent initrd from overwriting logbuffer */
1039 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1042 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1043 initrd_high, initrd_copy_to_ram);
1046 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1047 debug (" in-place initrd\n");
1048 *initrd_start = rd_data;
1049 *initrd_end = rd_data + rd_len;
1050 lmb_reserve(lmb, rd_data, rd_len);
1053 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1055 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
1057 if (*initrd_start == 0) {
1058 puts ("ramdisk - allocation error\n");
1061 show_boot_progress (12);
1063 *initrd_end = *initrd_start + rd_len;
1064 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1065 *initrd_start, *initrd_end);
1067 memmove_wd ((void *)*initrd_start,
1068 (void *)rd_data, rd_len, CHUNKSZ);
1076 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1077 *initrd_start, *initrd_end);
1084 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1086 #ifdef CONFIG_OF_LIBFDT
1087 static void fdt_error (const char *msg)
1091 puts (" - must RESET the board to recover.\n");
1094 static const image_header_t *image_get_fdt (ulong fdt_addr)
1096 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1098 image_print_contents (fdt_hdr);
1100 puts (" Verifying Checksum ... ");
1101 if (!image_check_hcrc (fdt_hdr)) {
1102 fdt_error ("fdt header checksum invalid");
1106 if (!image_check_dcrc (fdt_hdr)) {
1107 fdt_error ("fdt checksum invalid");
1112 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1113 fdt_error ("uImage is not a fdt");
1116 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1117 fdt_error ("uImage is compressed");
1120 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1121 fdt_error ("uImage data is not a fdt");
1128 * fit_check_fdt - verify FIT format FDT subimage
1129 * @fit_hdr: pointer to the FIT header
1130 * fdt_noffset: FDT subimage node offset within FIT image
1131 * @verify: data CRC verification flag
1133 * fit_check_fdt() verifies integrity of the FDT subimage and from
1134 * specified FIT image.
1140 #if defined(CONFIG_FIT)
1141 static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1143 fit_image_print (fit, fdt_noffset, " ");
1146 puts (" Verifying Hash Integrity ... ");
1147 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1148 fdt_error ("Bad Data Hash");
1154 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1155 fdt_error ("Not a FDT image");
1159 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1160 fdt_error ("FDT image is compressed");
1166 #endif /* CONFIG_FIT */
1168 #ifndef CONFIG_SYS_FDT_PAD
1169 #define CONFIG_SYS_FDT_PAD 0x3000
1173 * boot_relocate_fdt - relocate flat device tree
1174 * @lmb: pointer to lmb handle, will be used for memory mgmt
1175 * @bootmap_base: base address of the bootmap region
1176 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1177 * @of_size: pointer to a ulong variable, will hold fdt length
1179 * boot_relocate_fdt() determines if the of_flat_tree address is within
1180 * the bootmap and if not relocates it into that region
1182 * of_flat_tree and of_size are set to final (after relocation) values
1188 #if defined(CONFIG_SYS_BOOTMAPSZ)
1189 int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
1190 char **of_flat_tree, ulong *of_size)
1192 char *fdt_blob = *of_flat_tree;
1200 if (fdt_check_header (fdt_blob) != 0) {
1201 fdt_error ("image is not a fdt");
1205 #ifndef CONFIG_SYS_NO_FLASH
1206 /* move the blob if it is in flash (set relocate) */
1207 if (addr2info ((ulong)fdt_blob) != NULL)
1212 * The blob needs to be inside the boot mapping.
1214 if (fdt_blob < (char *)bootmap_base)
1217 if ((fdt_blob + *of_size + CONFIG_SYS_FDT_PAD) >=
1218 ((char *)CONFIG_SYS_BOOTMAPSZ + bootmap_base))
1221 /* move flattend device tree if needed */
1226 /* position on a 4K boundary before the alloc_current */
1227 /* Pad the FDT by a specified amount */
1228 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1229 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
1230 (CONFIG_SYS_BOOTMAPSZ + bootmap_base));
1232 if (of_start == 0) {
1233 puts("device tree - allocation error\n");
1237 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
1238 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
1241 printf (" Loading Device Tree to %08lx, end %08lx ... ",
1242 of_start, of_start + of_len - 1);
1244 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
1246 fdt_error ("fdt move failed");
1251 *of_flat_tree = (char *)of_start;
1254 *of_flat_tree = fdt_blob;
1255 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1256 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
1257 fdt_set_totalsize(*of_flat_tree, of_len);
1262 set_working_fdt_addr(*of_flat_tree);
1268 #endif /* CONFIG_SYS_BOOTMAPSZ */
1271 * boot_get_fdt - main fdt handling routine
1272 * @argc: command argument count
1273 * @argv: command argument list
1274 * @images: pointer to the bootm images structure
1275 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1276 * @of_size: pointer to a ulong variable, will hold fdt length
1278 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1279 * Curently supported are the following ramdisk sources:
1280 * - multicomponent kernel/ramdisk image,
1281 * - commandline provided address of decicated ramdisk image.
1284 * 0, if fdt image was found and valid, or skipped
1285 * of_flat_tree and of_size are set to fdt start address and length if
1286 * fdt image is found and valid
1288 * 1, if fdt image is found but corrupted
1289 * of_flat_tree and of_size are set to 0 if no fdt exists
1291 int boot_get_fdt (int flag, int argc, char * const argv[], bootm_headers_t *images,
1292 char **of_flat_tree, ulong *of_size)
1294 const image_header_t *fdt_hdr;
1296 char *fdt_blob = NULL;
1297 ulong image_start, image_end;
1298 ulong load_start, load_end;
1299 #if defined(CONFIG_FIT)
1301 const char *fit_uname_config = NULL;
1302 const char *fit_uname_fdt = NULL;
1310 *of_flat_tree = NULL;
1313 if (argc > 3 || genimg_has_config (images)) {
1314 #if defined(CONFIG_FIT)
1317 * If the FDT blob comes from the FIT image and the
1318 * FIT image address is omitted in the command line
1319 * argument, try to use ramdisk or os FIT image
1320 * address or default load address.
1322 if (images->fit_uname_rd)
1323 default_addr = (ulong)images->fit_hdr_rd;
1324 else if (images->fit_uname_os)
1325 default_addr = (ulong)images->fit_hdr_os;
1327 default_addr = load_addr;
1329 if (fit_parse_conf (argv[3], default_addr,
1330 &fdt_addr, &fit_uname_config)) {
1331 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1332 fit_uname_config, fdt_addr);
1333 } else if (fit_parse_subimage (argv[3], default_addr,
1334 &fdt_addr, &fit_uname_fdt)) {
1335 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1336 fit_uname_fdt, fdt_addr);
1340 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1341 debug ("* fdt: cmdline image address = 0x%08lx\n",
1344 #if defined(CONFIG_FIT)
1346 /* use FIT configuration provided in first bootm
1349 fdt_addr = (ulong)images->fit_hdr_os;
1350 fit_uname_config = images->fit_uname_cfg;
1351 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1352 fit_uname_config, fdt_addr);
1355 * Check whether configuration has FDT blob defined,
1356 * if not quit silently.
1358 fit_hdr = (void *)fdt_addr;
1359 cfg_noffset = fit_conf_get_node (fit_hdr,
1361 if (cfg_noffset < 0) {
1362 debug ("* fdt: no such config\n");
1366 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1368 if (fdt_noffset < 0) {
1369 debug ("* fdt: no fdt in config\n");
1375 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1378 /* copy from dataflash if needed */
1379 fdt_addr = genimg_get_image (fdt_addr);
1382 * Check if there is an FDT image at the
1383 * address provided in the second bootm argument
1384 * check image type, for FIT images get a FIT node.
1386 switch (genimg_get_format ((void *)fdt_addr)) {
1387 case IMAGE_FORMAT_LEGACY:
1388 /* verify fdt_addr points to a valid image header */
1389 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1391 fdt_hdr = image_get_fdt (fdt_addr);
1396 * move image data to the load address,
1397 * make sure we don't overwrite initial image
1399 image_start = (ulong)fdt_hdr;
1400 image_end = image_get_image_end (fdt_hdr);
1402 load_start = image_get_load (fdt_hdr);
1403 load_end = load_start + image_get_data_size (fdt_hdr);
1405 if ((load_start < image_end) && (load_end > image_start)) {
1406 fdt_error ("fdt overwritten");
1410 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1411 image_get_data (fdt_hdr), load_start);
1413 memmove ((void *)load_start,
1414 (void *)image_get_data (fdt_hdr),
1415 image_get_data_size (fdt_hdr));
1417 fdt_blob = (char *)load_start;
1419 case IMAGE_FORMAT_FIT:
1421 * This case will catch both: new uImage format
1422 * (libfdt based) and raw FDT blob (also libfdt
1425 #if defined(CONFIG_FIT)
1426 /* check FDT blob vs FIT blob */
1427 if (fit_check_format ((const void *)fdt_addr)) {
1431 fit_hdr = (void *)fdt_addr;
1432 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1435 if (!fit_uname_fdt) {
1437 * no FDT blob image node unit name,
1438 * try to get config node first. If
1439 * config unit node name is NULL
1440 * fit_conf_get_node() will try to
1441 * find default config node
1443 cfg_noffset = fit_conf_get_node (fit_hdr,
1446 if (cfg_noffset < 0) {
1447 fdt_error ("Could not find configuration node\n");
1451 fit_uname_config = fdt_get_name (fit_hdr,
1453 printf (" Using '%s' configuration\n",
1456 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1458 fit_uname_fdt = fit_get_name (fit_hdr,
1461 /* get FDT component image node offset */
1462 fdt_noffset = fit_image_get_node (fit_hdr,
1465 if (fdt_noffset < 0) {
1466 fdt_error ("Could not find subimage node\n");
1470 printf (" Trying '%s' FDT blob subimage\n",
1473 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1477 /* get ramdisk image data address and length */
1478 if (fit_image_get_data (fit_hdr, fdt_noffset,
1480 fdt_error ("Could not find FDT subimage data");
1484 /* verift that image data is a proper FDT blob */
1485 if (fdt_check_header ((char *)data) != 0) {
1486 fdt_error ("Subimage data is not a FTD");
1491 * move image data to the load address,
1492 * make sure we don't overwrite initial image
1494 image_start = (ulong)fit_hdr;
1495 image_end = fit_get_end (fit_hdr);
1497 if (fit_image_get_load (fit_hdr, fdt_noffset,
1498 &load_start) == 0) {
1499 load_end = load_start + size;
1501 if ((load_start < image_end) &&
1502 (load_end > image_start)) {
1503 fdt_error ("FDT overwritten");
1507 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1508 (ulong)data, load_start);
1510 memmove ((void *)load_start,
1511 (void *)data, size);
1513 fdt_blob = (char *)load_start;
1515 fdt_blob = (char *)data;
1518 images->fit_hdr_fdt = fit_hdr;
1519 images->fit_uname_fdt = fit_uname_fdt;
1520 images->fit_noffset_fdt = fdt_noffset;
1528 fdt_blob = (char *)fdt_addr;
1529 debug ("* fdt: raw FDT blob\n");
1530 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1534 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
1538 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1540 } else if (images->legacy_hdr_valid &&
1541 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1543 ulong fdt_data, fdt_len;
1546 * Now check if we have a legacy multi-component image,
1547 * get second entry data start address and len.
1549 printf ("## Flattened Device Tree from multi "
1550 "component Image at %08lX\n",
1551 (ulong)images->legacy_hdr_os);
1553 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1556 fdt_blob = (char *)fdt_data;
1557 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1559 if (fdt_check_header (fdt_blob) != 0) {
1560 fdt_error ("image is not a fdt");
1564 if (fdt_totalsize(fdt_blob) != fdt_len) {
1565 fdt_error ("fdt size != image size");
1569 debug ("## No Flattened Device Tree\n");
1573 debug ("## No Flattened Device Tree\n");
1577 *of_flat_tree = fdt_blob;
1578 *of_size = fdt_totalsize(fdt_blob);
1579 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
1580 (ulong)*of_flat_tree, *of_size);
1589 #endif /* CONFIG_OF_LIBFDT */
1591 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1593 * boot_get_cmdline - allocate and initialize kernel cmdline
1594 * @lmb: pointer to lmb handle, will be used for memory mgmt
1595 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1596 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1597 * @bootmap_base: ulong variable, holds offset in physical memory to
1600 * boot_get_cmdline() allocates space for kernel command line below
1601 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1602 * variable is present its contents is copied to allocated kernel
1609 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1615 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1616 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
1618 if (cmdline == NULL)
1621 if ((s = getenv("bootargs")) == NULL)
1626 *cmd_start = (ulong) & cmdline[0];
1627 *cmd_end = *cmd_start + strlen(cmdline);
1629 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1633 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1635 #ifdef CONFIG_SYS_BOOT_GET_KBD
1637 * boot_get_kbd - allocate and initialize kernel copy of board info
1638 * @lmb: pointer to lmb handle, will be used for memory mgmt
1639 * @kbd: double pointer to board info data
1640 * @bootmap_base: ulong variable, holds offset in physical memory to
1643 * boot_get_kbd() allocates space for kernel copy of board info data below
1644 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1645 * the current u-boot board info data.
1651 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1653 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1654 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
1660 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1662 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1663 do_bdinfo(NULL, 0, 0, NULL);
1668 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1669 #endif /* !USE_HOSTCC */
1671 #if defined(CONFIG_FIT)
1672 /*****************************************************************************/
1673 /* New uImage format routines */
1674 /*****************************************************************************/
1676 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1677 ulong *addr, const char **name)
1684 sep = strchr (spec, sepc);
1687 *addr = simple_strtoul (spec, NULL, 16);
1697 * fit_parse_conf - parse FIT configuration spec
1698 * @spec: input string, containing configuration spec
1699 * @add_curr: current image address (to be used as a possible default)
1700 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1702 * @conf_name double pointer to a char, will hold pointer to a configuration
1705 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1706 * where <addr> is a FIT image address that contains configuration
1707 * with a <conf> unit name.
1709 * Address part is optional, and if omitted default add_curr will
1713 * 1 if spec is a valid configuration string,
1714 * addr and conf_name are set accordingly
1717 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1718 ulong *addr, const char **conf_name)
1720 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1724 * fit_parse_subimage - parse FIT subimage spec
1725 * @spec: input string, containing subimage spec
1726 * @add_curr: current image address (to be used as a possible default)
1727 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1729 * @image_name: double pointer to a char, will hold pointer to a subimage name
1731 * fit_parse_subimage() expects subimage spec in the for of
1732 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1733 * subimage with a <subimg> unit name.
1735 * Address part is optional, and if omitted default add_curr will
1739 * 1 if spec is a valid subimage string,
1740 * addr and image_name are set accordingly
1743 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1744 ulong *addr, const char **image_name)
1746 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1748 #endif /* !USE_HOSTCC */
1750 static void fit_get_debug (const void *fit, int noffset,
1751 char *prop_name, int err)
1753 debug ("Can't get '%s' property from FIT 0x%08lx, "
1754 "node: offset %d, name %s (%s)\n",
1755 prop_name, (ulong)fit, noffset,
1756 fit_get_name (fit, noffset, NULL),
1757 fdt_strerror (err));
1761 * fit_print_contents - prints out the contents of the FIT format image
1762 * @fit: pointer to the FIT format image header
1763 * @p: pointer to prefix string
1765 * fit_print_contents() formats a multi line FIT image contents description.
1766 * The routine prints out FIT image properties (root node level) follwed by
1767 * the details of each component image.
1770 * no returned results
1772 void fit_print_contents (const void *fit)
1783 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1793 /* Root node properties */
1794 ret = fit_get_desc (fit, 0, &desc);
1795 printf ("%sFIT description: ", p);
1797 printf ("unavailable\n");
1799 printf ("%s\n", desc);
1801 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1802 ret = fit_get_timestamp (fit, 0, ×tamp);
1803 printf ("%sCreated: ", p);
1805 printf ("unavailable\n");
1807 genimg_print_time (timestamp);
1810 /* Find images parent node offset */
1811 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1812 if (images_noffset < 0) {
1813 printf ("Can't find images parent node '%s' (%s)\n",
1814 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1818 /* Process its subnodes, print out component images details */
1819 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1820 (noffset >= 0) && (ndepth > 0);
1821 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1824 * Direct child node of the images parent node,
1825 * i.e. component image node.
1827 printf ("%s Image %u (%s)\n", p, count++,
1828 fit_get_name(fit, noffset, NULL));
1830 fit_image_print (fit, noffset, p);
1834 /* Find configurations parent node offset */
1835 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1836 if (confs_noffset < 0) {
1837 debug ("Can't get configurations parent node '%s' (%s)\n",
1838 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1842 /* get default configuration unit name from default property */
1843 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1845 printf ("%s Default Configuration: '%s'\n", p, uname);
1847 /* Process its subnodes, print out configurations details */
1848 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1849 (noffset >= 0) && (ndepth > 0);
1850 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1853 * Direct child node of the configurations parent node,
1854 * i.e. configuration node.
1856 printf ("%s Configuration %u (%s)\n", p, count++,
1857 fit_get_name(fit, noffset, NULL));
1859 fit_conf_print (fit, noffset, p);
1865 * fit_image_print - prints out the FIT component image details
1866 * @fit: pointer to the FIT format image header
1867 * @image_noffset: offset of the component image node
1868 * @p: pointer to prefix string
1870 * fit_image_print() lists all mandatory properies for the processed component
1871 * image. If present, hash nodes are printed out as well. Load
1872 * address for images of type firmware is also printed out. Since the load
1873 * address is not mandatory for firmware images, it will be output as
1874 * "unavailable" when not present.
1877 * no returned results
1879 void fit_image_print (const void *fit, int image_noffset, const char *p)
1882 uint8_t type, arch, os, comp;
1890 /* Mandatory properties */
1891 ret = fit_get_desc (fit, image_noffset, &desc);
1892 printf ("%s Description: ", p);
1894 printf ("unavailable\n");
1896 printf ("%s\n", desc);
1898 fit_image_get_type (fit, image_noffset, &type);
1899 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1901 fit_image_get_comp (fit, image_noffset, &comp);
1902 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1904 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1907 printf ("%s Data Start: ", p);
1909 printf ("unavailable\n");
1911 printf ("0x%08lx\n", (ulong)data);
1914 printf ("%s Data Size: ", p);
1916 printf ("unavailable\n");
1918 genimg_print_size (size);
1920 /* Remaining, type dependent properties */
1921 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1922 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1923 (type == IH_TYPE_FLATDT)) {
1924 fit_image_get_arch (fit, image_noffset, &arch);
1925 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1928 if (type == IH_TYPE_KERNEL) {
1929 fit_image_get_os (fit, image_noffset, &os);
1930 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1933 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1934 (type == IH_TYPE_FIRMWARE)) {
1935 ret = fit_image_get_load (fit, image_noffset, &load);
1936 printf ("%s Load Address: ", p);
1938 printf ("unavailable\n");
1940 printf ("0x%08lx\n", load);
1943 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1944 fit_image_get_entry (fit, image_noffset, &entry);
1945 printf ("%s Entry Point: ", p);
1947 printf ("unavailable\n");
1949 printf ("0x%08lx\n", entry);
1952 /* Process all hash subnodes of the component image node */
1953 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1954 (noffset >= 0) && (ndepth > 0);
1955 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1957 /* Direct child node of the component image node */
1958 fit_image_print_hash (fit, noffset, p);
1964 * fit_image_print_hash - prints out the hash node details
1965 * @fit: pointer to the FIT format image header
1966 * @noffset: offset of the hash node
1967 * @p: pointer to prefix string
1969 * fit_image_print_hash() lists properies for the processed hash node
1972 * no returned results
1974 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1982 * Check subnode name, must be equal to "hash".
1983 * Multiple hash nodes require unique unit node
1984 * names, e.g. hash@1, hash@2, etc.
1986 if (strncmp (fit_get_name(fit, noffset, NULL),
1988 strlen(FIT_HASH_NODENAME)) != 0)
1991 debug ("%s Hash node: '%s'\n", p,
1992 fit_get_name (fit, noffset, NULL));
1994 printf ("%s Hash algo: ", p);
1995 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1996 printf ("invalid/unsupported\n");
1999 printf ("%s\n", algo);
2001 ret = fit_image_hash_get_value (fit, noffset, &value,
2003 printf ("%s Hash value: ", p);
2005 printf ("unavailable\n");
2007 for (i = 0; i < value_len; i++)
2008 printf ("%02x", value[i]);
2012 debug ("%s Hash len: %d\n", p, value_len);
2016 * fit_get_desc - get node description property
2017 * @fit: pointer to the FIT format image header
2018 * @noffset: node offset
2019 * @desc: double pointer to the char, will hold pointer to the descrption
2021 * fit_get_desc() reads description property from a given node, if
2022 * description is found pointer to it is returened in third call argument.
2028 int fit_get_desc (const void *fit, int noffset, char **desc)
2032 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2033 if (*desc == NULL) {
2034 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2042 * fit_get_timestamp - get node timestamp property
2043 * @fit: pointer to the FIT format image header
2044 * @noffset: node offset
2045 * @timestamp: pointer to the time_t, will hold read timestamp
2047 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2048 * is found and has a correct size its value is retured in third call
2053 * -1, on property read failure
2054 * -2, on wrong timestamp size
2056 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2061 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2063 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2066 if (len != sizeof (uint32_t)) {
2067 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2071 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2076 * fit_image_get_node - get node offset for component image of a given unit name
2077 * @fit: pointer to the FIT format image header
2078 * @image_uname: component image node unit name
2080 * fit_image_get_node() finds a component image (withing the '/images'
2081 * node) of a provided unit name. If image is found its node offset is
2082 * returned to the caller.
2085 * image node offset when found (>=0)
2086 * negative number on failure (FDT_ERR_* code)
2088 int fit_image_get_node (const void *fit, const char *image_uname)
2090 int noffset, images_noffset;
2092 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2093 if (images_noffset < 0) {
2094 debug ("Can't find images parent node '%s' (%s)\n",
2095 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2096 return images_noffset;
2099 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2101 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2102 image_uname, fdt_strerror (noffset));
2109 * fit_image_get_os - get os id for a given component image node
2110 * @fit: pointer to the FIT format image header
2111 * @noffset: component image node offset
2112 * @os: pointer to the uint8_t, will hold os numeric id
2114 * fit_image_get_os() finds os property in a given component image node.
2115 * If the property is found, its (string) value is translated to the numeric
2116 * id which is returned to the caller.
2122 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2127 /* Get OS name from property data */
2128 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2130 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2135 /* Translate OS name to id */
2136 *os = genimg_get_os_id (data);
2141 * fit_image_get_arch - get arch id for a given component image node
2142 * @fit: pointer to the FIT format image header
2143 * @noffset: component image node offset
2144 * @arch: pointer to the uint8_t, will hold arch numeric id
2146 * fit_image_get_arch() finds arch property in a given component image node.
2147 * If the property is found, its (string) value is translated to the numeric
2148 * id which is returned to the caller.
2154 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2159 /* Get architecture name from property data */
2160 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2162 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2167 /* Translate architecture name to id */
2168 *arch = genimg_get_arch_id (data);
2173 * fit_image_get_type - get type id for a given component image node
2174 * @fit: pointer to the FIT format image header
2175 * @noffset: component image node offset
2176 * @type: pointer to the uint8_t, will hold type numeric id
2178 * fit_image_get_type() finds type property in a given component image node.
2179 * If the property is found, its (string) value is translated to the numeric
2180 * id which is returned to the caller.
2186 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2191 /* Get image type name from property data */
2192 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2194 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2199 /* Translate image type name to id */
2200 *type = genimg_get_type_id (data);
2205 * fit_image_get_comp - get comp id for a given component image node
2206 * @fit: pointer to the FIT format image header
2207 * @noffset: component image node offset
2208 * @comp: pointer to the uint8_t, will hold comp numeric id
2210 * fit_image_get_comp() finds comp property in a given component image node.
2211 * If the property is found, its (string) value is translated to the numeric
2212 * id which is returned to the caller.
2218 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2223 /* Get compression name from property data */
2224 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2226 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2231 /* Translate compression name to id */
2232 *comp = genimg_get_comp_id (data);
2237 * fit_image_get_load - get load address property for a given component image node
2238 * @fit: pointer to the FIT format image header
2239 * @noffset: component image node offset
2240 * @load: pointer to the uint32_t, will hold load address
2242 * fit_image_get_load() finds load address property in a given component image node.
2243 * If the property is found, its value is returned to the caller.
2249 int fit_image_get_load (const void *fit, int noffset, ulong *load)
2252 const uint32_t *data;
2254 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2256 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2260 *load = uimage_to_cpu (*data);
2265 * fit_image_get_entry - get entry point address property for a given component image node
2266 * @fit: pointer to the FIT format image header
2267 * @noffset: component image node offset
2268 * @entry: pointer to the uint32_t, will hold entry point address
2270 * fit_image_get_entry() finds entry point address property in a given component image node.
2271 * If the property is found, its value is returned to the caller.
2277 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2280 const uint32_t *data;
2282 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2284 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2288 *entry = uimage_to_cpu (*data);
2293 * fit_image_get_data - get data property and its size for a given component image node
2294 * @fit: pointer to the FIT format image header
2295 * @noffset: component image node offset
2296 * @data: double pointer to void, will hold data property's data address
2297 * @size: pointer to size_t, will hold data property's data size
2299 * fit_image_get_data() finds data property in a given component image node.
2300 * If the property is found its data start address and size are returned to
2307 int fit_image_get_data (const void *fit, int noffset,
2308 const void **data, size_t *size)
2312 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2313 if (*data == NULL) {
2314 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2324 * fit_image_hash_get_algo - get hash algorithm name
2325 * @fit: pointer to the FIT format image header
2326 * @noffset: hash node offset
2327 * @algo: double pointer to char, will hold pointer to the algorithm name
2329 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2330 * If the property is found its data start address is returned to the caller.
2336 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2340 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2341 if (*algo == NULL) {
2342 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2350 * fit_image_hash_get_value - get hash value and length
2351 * @fit: pointer to the FIT format image header
2352 * @noffset: hash node offset
2353 * @value: double pointer to uint8_t, will hold address of a hash value data
2354 * @value_len: pointer to an int, will hold hash data length
2356 * fit_image_hash_get_value() finds hash value property in a given hash node.
2357 * If the property is found its data start address and size are returned to
2364 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2369 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2370 if (*value == NULL) {
2371 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2381 * fit_set_timestamp - set node timestamp property
2382 * @fit: pointer to the FIT format image header
2383 * @noffset: node offset
2384 * @timestamp: timestamp value to be set
2386 * fit_set_timestamp() attempts to set timestamp property in the requested
2387 * node and returns operation status to the caller.
2391 * -1, on property read failure
2393 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2398 t = cpu_to_uimage (timestamp);
2399 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2402 printf ("Can't set '%s' property for '%s' node (%s)\n",
2403 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2404 fdt_strerror (ret));
2412 * calculate_hash - calculate and return hash for provided input data
2413 * @data: pointer to the input data
2414 * @data_len: data length
2415 * @algo: requested hash algorithm
2416 * @value: pointer to the char, will hold hash value data (caller must
2417 * allocate enough free space)
2418 * value_len: length of the calculated hash
2420 * calculate_hash() computes input data hash according to the requested algorithm.
2421 * Resulting hash value is placed in caller provided 'value' buffer, length
2422 * of the calculated hash is returned via value_len pointer argument.
2426 * -1, when algo is unsupported
2428 static int calculate_hash (const void *data, int data_len, const char *algo,
2429 uint8_t *value, int *value_len)
2431 if (strcmp (algo, "crc32") == 0 ) {
2432 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2434 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2436 } else if (strcmp (algo, "sha1") == 0 ) {
2437 sha1_csum_wd ((unsigned char *) data, data_len,
2438 (unsigned char *) value, CHUNKSZ_SHA1);
2440 } else if (strcmp (algo, "md5") == 0 ) {
2441 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2444 debug ("Unsupported hash alogrithm\n");
2452 * fit_set_hashes - process FIT component image nodes and calculate hashes
2453 * @fit: pointer to the FIT format image header
2455 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2456 * Hashes are calculated for all component images which have hash subnodes
2457 * with algorithm property set to one of the supported hash algorithms.
2461 * libfdt error code, on failure
2463 int fit_set_hashes (void *fit)
2470 /* Find images parent node offset */
2471 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2472 if (images_noffset < 0) {
2473 printf ("Can't find images parent node '%s' (%s)\n",
2474 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2475 return images_noffset;
2478 /* Process its subnodes, print out component images details */
2479 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2480 (noffset >= 0) && (ndepth > 0);
2481 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2484 * Direct child node of the images parent node,
2485 * i.e. component image node.
2487 ret = fit_image_set_hashes (fit, noffset);
2497 * fit_image_set_hashes - calculate/set hashes for given component image node
2498 * @fit: pointer to the FIT format image header
2499 * @image_noffset: requested component image node
2501 * fit_image_set_hashes() adds hash values for an component image node. All
2502 * existing hash subnodes are checked, if algorithm property is set to one of
2503 * the supported hash algorithms, hash value is computed and corresponding
2504 * hash node property is set, for example:
2506 * Input component image node structure:
2508 * o image@1 (at image_noffset)
2509 * | - data = [binary data]
2513 * Output component image node structure:
2515 * o image@1 (at image_noffset)
2516 * | - data = [binary data]
2519 * |- value = sha1(data)
2525 int fit_image_set_hashes (void *fit, int image_noffset)
2530 uint8_t value[FIT_MAX_HASH_LEN];
2535 /* Get image data and data length */
2536 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2537 printf ("Can't get image data/size\n");
2541 /* Process all hash subnodes of the component image node */
2542 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2543 (noffset >= 0) && (ndepth > 0);
2544 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2546 /* Direct child node of the component image node */
2549 * Check subnode name, must be equal to "hash".
2550 * Multiple hash nodes require unique unit node
2551 * names, e.g. hash@1, hash@2, etc.
2553 if (strncmp (fit_get_name(fit, noffset, NULL),
2555 strlen(FIT_HASH_NODENAME)) != 0) {
2556 /* Not a hash subnode, skip it */
2560 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2561 printf ("Can't get hash algo property for "
2562 "'%s' hash node in '%s' image node\n",
2563 fit_get_name (fit, noffset, NULL),
2564 fit_get_name (fit, image_noffset, NULL));
2568 if (calculate_hash (data, size, algo, value, &value_len)) {
2569 printf ("Unsupported hash algorithm (%s) for "
2570 "'%s' hash node in '%s' image node\n",
2571 algo, fit_get_name (fit, noffset, NULL),
2572 fit_get_name (fit, image_noffset, NULL));
2576 if (fit_image_hash_set_value (fit, noffset, value,
2578 printf ("Can't set hash value for "
2579 "'%s' hash node in '%s' image node\n",
2580 fit_get_name (fit, noffset, NULL),
2581 fit_get_name (fit, image_noffset, NULL));
2591 * fit_image_hash_set_value - set hash value in requested has node
2592 * @fit: pointer to the FIT format image header
2593 * @noffset: hash node offset
2594 * @value: hash value to be set
2595 * @value_len: hash value length
2597 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2598 * given and returns operation status to the caller.
2604 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2609 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2611 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2612 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2613 fdt_strerror (ret));
2619 #endif /* USE_HOSTCC */
2622 * fit_image_check_hashes - verify data intergity
2623 * @fit: pointer to the FIT format image header
2624 * @image_noffset: component image node offset
2626 * fit_image_check_hashes() goes over component image hash nodes,
2627 * re-calculates each data hash and compares with the value stored in hash
2631 * 1, if all hashes are valid
2632 * 0, otherwise (or on error)
2634 int fit_image_check_hashes (const void *fit, int image_noffset)
2641 uint8_t value[FIT_MAX_HASH_LEN];
2647 /* Get image data and data length */
2648 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2649 printf ("Can't get image data/size\n");
2653 /* Process all hash subnodes of the component image node */
2654 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2655 (noffset >= 0) && (ndepth > 0);
2656 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2658 /* Direct child node of the component image node */
2661 * Check subnode name, must be equal to "hash".
2662 * Multiple hash nodes require unique unit node
2663 * names, e.g. hash@1, hash@2, etc.
2665 if (strncmp (fit_get_name(fit, noffset, NULL),
2667 strlen(FIT_HASH_NODENAME)) != 0)
2670 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2671 err_msg = " error!\nCan't get hash algo "
2675 printf ("%s", algo);
2677 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2679 err_msg = " error!\nCan't get hash value "
2684 if (calculate_hash (data, size, algo, value, &value_len)) {
2685 err_msg = " error!\nUnsupported hash algorithm";
2689 if (value_len != fit_value_len) {
2690 err_msg = " error !\nBad hash value len";
2692 } else if (memcmp (value, fit_value, value_len) != 0) {
2693 err_msg = " error!\nBad hash value";
2703 printf ("%s for '%s' hash node in '%s' image node\n",
2704 err_msg, fit_get_name (fit, noffset, NULL),
2705 fit_get_name (fit, image_noffset, NULL));
2710 * fit_all_image_check_hashes - verify data intergity for all images
2711 * @fit: pointer to the FIT format image header
2713 * fit_all_image_check_hashes() goes over all images in the FIT and
2714 * for every images checks if all it's hashes are valid.
2717 * 1, if all hashes of all images are valid
2718 * 0, otherwise (or on error)
2720 int fit_all_image_check_hashes (const void *fit)
2727 /* Find images parent node offset */
2728 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2729 if (images_noffset < 0) {
2730 printf ("Can't find images parent node '%s' (%s)\n",
2731 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2735 /* Process all image subnodes, check hashes for each */
2736 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2738 for (ndepth = 0, count = 0,
2739 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2740 (noffset >= 0) && (ndepth > 0);
2741 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2744 * Direct child node of the images parent node,
2745 * i.e. component image node.
2747 printf (" Hash(es) for Image %u (%s): ", count++,
2748 fit_get_name (fit, noffset, NULL));
2750 if (!fit_image_check_hashes (fit, noffset))
2759 * fit_image_check_os - check whether image node is of a given os type
2760 * @fit: pointer to the FIT format image header
2761 * @noffset: component image node offset
2762 * @os: requested image os
2764 * fit_image_check_os() reads image os property and compares its numeric
2765 * id with the requested os. Comparison result is returned to the caller.
2768 * 1 if image is of given os type
2769 * 0 otherwise (or on error)
2771 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2775 if (fit_image_get_os (fit, noffset, &image_os))
2777 return (os == image_os);
2781 * fit_image_check_arch - check whether image node is of a given arch
2782 * @fit: pointer to the FIT format image header
2783 * @noffset: component image node offset
2784 * @arch: requested imagearch
2786 * fit_image_check_arch() reads image arch property and compares its numeric
2787 * id with the requested arch. Comparison result is returned to the caller.
2790 * 1 if image is of given arch
2791 * 0 otherwise (or on error)
2793 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2797 if (fit_image_get_arch (fit, noffset, &image_arch))
2799 return (arch == image_arch);
2803 * fit_image_check_type - check whether image node is of a given type
2804 * @fit: pointer to the FIT format image header
2805 * @noffset: component image node offset
2806 * @type: requested image type
2808 * fit_image_check_type() reads image type property and compares its numeric
2809 * id with the requested type. Comparison result is returned to the caller.
2812 * 1 if image is of given type
2813 * 0 otherwise (or on error)
2815 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2819 if (fit_image_get_type (fit, noffset, &image_type))
2821 return (type == image_type);
2825 * fit_image_check_comp - check whether image node uses given compression
2826 * @fit: pointer to the FIT format image header
2827 * @noffset: component image node offset
2828 * @comp: requested image compression type
2830 * fit_image_check_comp() reads image compression property and compares its
2831 * numeric id with the requested compression type. Comparison result is
2832 * returned to the caller.
2835 * 1 if image uses requested compression
2836 * 0 otherwise (or on error)
2838 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2842 if (fit_image_get_comp (fit, noffset, &image_comp))
2844 return (comp == image_comp);
2848 * fit_check_format - sanity check FIT image format
2849 * @fit: pointer to the FIT format image header
2851 * fit_check_format() runs a basic sanity FIT image verification.
2852 * Routine checks for mandatory properties, nodes, etc.
2858 int fit_check_format (const void *fit)
2860 /* mandatory / node 'description' property */
2861 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2862 debug ("Wrong FIT format: no description\n");
2866 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2867 /* mandatory / node 'timestamp' property */
2868 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2869 debug ("Wrong FIT format: no timestamp\n");
2874 /* mandatory subimages parent '/images' node */
2875 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2876 debug ("Wrong FIT format: no images parent node\n");
2884 * fit_conf_get_node - get node offset for configuration of a given unit name
2885 * @fit: pointer to the FIT format image header
2886 * @conf_uname: configuration node unit name
2888 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2889 * parant node) of a provided unit name. If configuration is found its node offset
2890 * is returned to the caller.
2892 * When NULL is provided in second argument fit_conf_get_node() will search
2893 * for a default configuration node instead. Default configuration node unit name
2894 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2897 * configuration node offset when found (>=0)
2898 * negative number on failure (FDT_ERR_* code)
2900 int fit_conf_get_node (const void *fit, const char *conf_uname)
2902 int noffset, confs_noffset;
2905 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2906 if (confs_noffset < 0) {
2907 debug ("Can't find configurations parent node '%s' (%s)\n",
2908 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2909 return confs_noffset;
2912 if (conf_uname == NULL) {
2913 /* get configuration unit name from the default property */
2914 debug ("No configuration specified, trying default...\n");
2915 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2916 if (conf_uname == NULL) {
2917 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2920 debug ("Found default configuration: '%s'\n", conf_uname);
2923 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2925 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2926 conf_uname, fdt_strerror (noffset));
2932 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2933 const char *prop_name)
2938 /* get kernel image unit name from configuration kernel property */
2939 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2943 return fit_image_get_node (fit, uname);
2947 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2948 * a given configuration
2949 * @fit: pointer to the FIT format image header
2950 * @noffset: configuration node offset
2952 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2953 * configuration FIT_KERNEL_PROP property and translates it to the node
2957 * image node offset when found (>=0)
2958 * negative number on failure (FDT_ERR_* code)
2960 int fit_conf_get_kernel_node (const void *fit, int noffset)
2962 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2966 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2967 * a given configuration
2968 * @fit: pointer to the FIT format image header
2969 * @noffset: configuration node offset
2971 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2972 * configuration FIT_KERNEL_PROP property and translates it to the node
2976 * image node offset when found (>=0)
2977 * negative number on failure (FDT_ERR_* code)
2979 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2981 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2985 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2986 * a given configuration
2987 * @fit: pointer to the FIT format image header
2988 * @noffset: configuration node offset
2990 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2991 * configuration FIT_KERNEL_PROP property and translates it to the node
2995 * image node offset when found (>=0)
2996 * negative number on failure (FDT_ERR_* code)
2998 int fit_conf_get_fdt_node (const void *fit, int noffset)
3000 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
3004 * fit_conf_print - prints out the FIT configuration details
3005 * @fit: pointer to the FIT format image header
3006 * @noffset: offset of the configuration node
3007 * @p: pointer to prefix string
3009 * fit_conf_print() lists all mandatory properies for the processed
3010 * configuration node.
3013 * no returned results
3015 void fit_conf_print (const void *fit, int noffset, const char *p)
3021 /* Mandatory properties */
3022 ret = fit_get_desc (fit, noffset, &desc);
3023 printf ("%s Description: ", p);
3025 printf ("unavailable\n");
3027 printf ("%s\n", desc);
3029 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3030 printf ("%s Kernel: ", p);
3032 printf ("unavailable\n");
3034 printf ("%s\n", uname);
3036 /* Optional properties */
3037 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3039 printf ("%s Init Ramdisk: %s\n", p, uname);
3041 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3043 printf ("%s FDT: %s\n", p, uname);
3047 * fit_check_ramdisk - verify FIT format ramdisk subimage
3048 * @fit_hdr: pointer to the FIT ramdisk header
3049 * @rd_noffset: ramdisk subimage node offset within FIT image
3050 * @arch: requested ramdisk image architecture type
3051 * @verify: data CRC verification flag
3053 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3054 * specified FIT image.
3061 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3063 fit_image_print (fit, rd_noffset, " ");
3066 puts (" Verifying Hash Integrity ... ");
3067 if (!fit_image_check_hashes (fit, rd_noffset)) {
3068 puts ("Bad Data Hash\n");
3069 show_boot_progress (-125);
3075 show_boot_progress (126);
3076 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3077 !fit_image_check_arch (fit, rd_noffset, arch) ||
3078 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3079 printf ("No Linux %s Ramdisk Image\n",
3080 genimg_get_arch_name(arch));
3081 show_boot_progress (-126);
3085 show_boot_progress (127);
3088 #endif /* USE_HOSTCC */
3089 #endif /* CONFIG_FIT */