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,
31 #ifdef CONFIG_SHOW_BOOT_PROGRESS
32 #include <status_led.h>
35 #ifdef CONFIG_HAS_DATAFLASH
36 #include <dataflash.h>
39 #ifdef CONFIG_LOGBUFFER
43 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
49 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
52 #include <fdt_support.h>
55 #if defined(CONFIG_FIT)
56 #include <u-boot/md5.h>
59 static int fit_check_ramdisk (const void *fit, int os_noffset,
60 uint8_t arch, int verify);
64 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
67 DECLARE_GLOBAL_DATA_PTR;
69 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
73 #include <u-boot/md5.h>
76 #endif /* !USE_HOSTCC*/
78 typedef struct table_entry {
79 int id; /* as defined in image.h */
80 char *sname; /* short (input) name */
81 char *lname; /* long (output) name */
84 static table_entry_t uimage_arch[] = {
85 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
86 { IH_ARCH_ALPHA, "alpha", "Alpha", },
87 { IH_ARCH_ARM, "arm", "ARM", },
88 { IH_ARCH_I386, "x86", "Intel x86", },
89 { IH_ARCH_IA64, "ia64", "IA64", },
90 { IH_ARCH_M68K, "m68k", "M68K", },
91 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
92 { IH_ARCH_MIPS, "mips", "MIPS", },
93 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
94 { IH_ARCH_NIOS, "nios", "NIOS", },
95 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
96 { IH_ARCH_PPC, "powerpc", "PowerPC", },
97 { IH_ARCH_PPC, "ppc", "PowerPC", },
98 { IH_ARCH_S390, "s390", "IBM S390", },
99 { IH_ARCH_SH, "sh", "SuperH", },
100 { IH_ARCH_SPARC, "sparc", "SPARC", },
101 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
102 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
103 { IH_ARCH_AVR32, "avr32", "AVR32", },
107 static table_entry_t uimage_os[] = {
108 { IH_OS_INVALID, NULL, "Invalid OS", },
109 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
110 { IH_OS_ARTOS, "artos", "ARTOS", },
112 { IH_OS_LINUX, "linux", "Linux", },
113 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
114 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
116 { IH_OS_NETBSD, "netbsd", "NetBSD", },
117 { IH_OS_RTEMS, "rtems", "RTEMS", },
118 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
119 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
120 { IH_OS_QNX, "qnx", "QNX", },
121 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
124 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
125 { IH_OS_DELL, "dell", "Dell", },
126 { IH_OS_ESIX, "esix", "Esix", },
127 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
128 { IH_OS_IRIX, "irix", "Irix", },
129 { IH_OS_NCR, "ncr", "NCR", },
130 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
131 { IH_OS_PSOS, "psos", "pSOS", },
132 { IH_OS_SCO, "sco", "SCO", },
133 { IH_OS_SOLARIS, "solaris", "Solaris", },
134 { IH_OS_SVR4, "svr4", "SVR4", },
139 static table_entry_t uimage_type[] = {
140 { IH_TYPE_INVALID, NULL, "Invalid Image", },
141 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
142 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
143 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
144 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
145 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
146 { IH_TYPE_SCRIPT, "script", "Script", },
147 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
148 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
152 static table_entry_t uimage_comp[] = {
153 { IH_COMP_NONE, "none", "uncompressed", },
154 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
155 { IH_COMP_GZIP, "gzip", "gzip compressed", },
159 uint32_t crc32 (uint32_t, const unsigned char *, uint);
160 uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
161 static void genimg_print_size (uint32_t size);
162 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
163 static void genimg_print_time (time_t timestamp);
166 /*****************************************************************************/
167 /* Legacy format routines */
168 /*****************************************************************************/
169 int image_check_hcrc (image_header_t *hdr)
172 ulong len = image_get_header_size ();
173 image_header_t header;
175 /* Copy header so we can blank CRC field for re-calculation */
176 memmove (&header, (char *)hdr, image_get_header_size ());
177 image_set_hcrc (&header, 0);
179 hcrc = crc32 (0, (unsigned char *)&header, len);
181 return (hcrc == image_get_hcrc (hdr));
184 int image_check_dcrc (image_header_t *hdr)
186 ulong data = image_get_data (hdr);
187 ulong len = image_get_data_size (hdr);
188 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
190 return (dcrc == image_get_dcrc (hdr));
195 * image_multi_count - get component (sub-image) count
196 * @hdr: pointer to the header of the multi component image
198 * image_multi_count() returns number of components in a multi
201 * Note: no checking of the image type is done, caller must pass
202 * a valid multi component image.
205 * number of components
207 ulong image_multi_count (image_header_t *hdr)
212 /* get start of the image payload, which in case of multi
213 * component images that points to a table of component sizes */
214 size = (uint32_t *)image_get_data (hdr);
216 /* count non empty slots */
217 for (i = 0; size[i]; ++i)
224 * image_multi_getimg - get component data address and size
225 * @hdr: pointer to the header of the multi component image
226 * @idx: index of the requested component
227 * @data: pointer to a ulong variable, will hold component data address
228 * @len: pointer to a ulong variable, will hold component size
230 * image_multi_getimg() returns size and data address for the requested
231 * component in a multi component image.
233 * Note: no checking of the image type is done, caller must pass
234 * a valid multi component image.
237 * data address and size of the component, if idx is valid
238 * 0 in data and len, if idx is out of range
240 void image_multi_getimg (image_header_t *hdr, ulong idx,
241 ulong *data, ulong *len)
245 ulong offset, tail, count, img_data;
247 /* get number of component */
248 count = image_multi_count (hdr);
250 /* get start of the image payload, which in case of multi
251 * component images that points to a table of component sizes */
252 size = (uint32_t *)image_get_data (hdr);
254 /* get address of the proper component data start, which means
255 * skipping sizes table (add 1 for last, null entry) */
256 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
259 *len = uimage_to_cpu (size[idx]);
263 /* go over all indices preceding requested component idx */
264 for (i = 0; i < idx; i++) {
265 /* add up i-th component size */
266 offset += uimage_to_cpu (size[i]);
268 /* add up alignment for i-th component */
269 tail += (4 - uimage_to_cpu (size[i]) % 4);
272 /* calculate idx-th component data address */
273 *data = img_data + offset + tail;
280 static void image_print_type (image_header_t *hdr)
282 const char *os, *arch, *type, *comp;
284 os = genimg_get_os_name (image_get_os (hdr));
285 arch = genimg_get_arch_name (image_get_arch (hdr));
286 type = genimg_get_type_name (image_get_type (hdr));
287 comp = genimg_get_comp_name (image_get_comp (hdr));
289 printf ("%s %s %s (%s)\n", arch, os, type, comp);
293 * image_print_contents - prints out the contents of the legacy format image
294 * @hdr: pointer to the legacy format image header
295 * @p: pointer to prefix string
297 * image_print_contents() formats a multi line legacy image contents description.
298 * The routine prints out all header fields followed by the size/offset data
299 * for MULTI/SCRIPT images.
302 * no returned results
304 void image_print_contents (image_header_t *hdr)
314 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
315 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
316 printf ("%sCreated: ", p);
317 genimg_print_time ((time_t)image_get_time (hdr));
319 printf ("%sImage Type: ", p);
320 image_print_type (hdr);
321 printf ("%sData Size: ", p);
322 genimg_print_size (image_get_data_size (hdr));
323 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
324 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
326 if (image_check_type (hdr, IH_TYPE_MULTI) ||
327 image_check_type (hdr, IH_TYPE_SCRIPT)) {
330 ulong count = image_multi_count (hdr);
332 printf ("%sContents:\n", p);
333 for (i = 0; i < count; i++) {
334 image_multi_getimg (hdr, i, &data, &len);
336 printf ("%s Image %d: ", p, i);
337 genimg_print_size (len);
339 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
341 * the user may need to know offsets
342 * if planning to do something with
345 printf ("%s Offset = 0x%08lx\n", p, data);
354 * image_get_ramdisk - get and verify ramdisk image
355 * @rd_addr: ramdisk image start address
356 * @arch: expected ramdisk architecture
357 * @verify: checksum verification flag
359 * image_get_ramdisk() returns a pointer to the verified ramdisk image
360 * header. Routine receives image start address and expected architecture
361 * flag. Verification done covers data and header integrity and os/type/arch
364 * If dataflash support is enabled routine checks for dataflash addresses
365 * and handles required dataflash reads.
368 * pointer to a ramdisk image header, if image was found and valid
369 * otherwise, return NULL
371 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
374 image_header_t *rd_hdr = (image_header_t *)rd_addr;
376 if (!image_check_magic (rd_hdr)) {
377 puts ("Bad Magic Number\n");
378 show_boot_progress (-10);
382 if (!image_check_hcrc (rd_hdr)) {
383 puts ("Bad Header Checksum\n");
384 show_boot_progress (-11);
388 show_boot_progress (10);
389 image_print_contents (rd_hdr);
392 puts(" Verifying Checksum ... ");
393 if (!image_check_dcrc (rd_hdr)) {
394 puts ("Bad Data CRC\n");
395 show_boot_progress (-12);
401 show_boot_progress (11);
403 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
404 !image_check_arch (rd_hdr, arch) ||
405 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
406 printf ("No Linux %s Ramdisk Image\n",
407 genimg_get_arch_name(arch));
408 show_boot_progress (-13);
414 #endif /* !USE_HOSTCC */
416 /*****************************************************************************/
417 /* Shared dual-format routines */
418 /*****************************************************************************/
420 int getenv_yesno (char *var)
422 char *s = getenv (var);
423 return (s && (*s == 'n')) ? 0 : 1;
426 ulong getenv_bootm_low(void)
428 char *s = getenv ("bootm_low");
430 ulong tmp = simple_strtoul (s, NULL, 16);
434 #if defined(CFG_SDRAM_BASE)
435 return CFG_SDRAM_BASE;
436 #elif defined(CONFIG_ARM)
437 return gd->bd->bi_dram[0].start;
443 ulong getenv_bootm_size(void)
445 char *s = getenv ("bootm_size");
447 ulong tmp = simple_strtoul (s, NULL, 16);
451 #if defined(CONFIG_ARM)
452 return gd->bd->bi_dram[0].size;
454 return gd->bd->bi_memsize;
458 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
460 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
462 size_t tail = (len > chunksz) ? chunksz : len;
464 memmove (to, from, tail);
469 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
470 memmove (to, from, len);
471 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
473 #endif /* !USE_HOSTCC */
475 static void genimg_print_size (uint32_t size)
478 printf ("%d Bytes = ", size);
479 print_size (size, "\n");
481 printf ("%d Bytes = %.2f kB = %.2f MB\n",
482 size, (double)size / 1.024e3,
483 (double)size / 1.048576e6);
487 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
488 static void genimg_print_time (time_t timestamp)
493 to_tm (timestamp, &tm);
494 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
495 tm.tm_year, tm.tm_mon, tm.tm_mday,
496 tm.tm_hour, tm.tm_min, tm.tm_sec);
498 printf ("%s", ctime(×tamp));
501 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
504 * get_table_entry_name - translate entry id to long name
505 * @table: pointer to a translation table for entries of a specific type
506 * @msg: message to be returned when translation fails
507 * @id: entry id to be translated
509 * get_table_entry_name() will go over translation table trying to find
510 * entry that matches given id. If matching entry is found, its long
511 * name is returned to the caller.
514 * long entry name if translation succeeds
517 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
519 for (; table->id >= 0; ++table) {
521 return (table->lname);
526 const char *genimg_get_os_name (uint8_t os)
528 return (get_table_entry_name (uimage_os, "Unknown OS", os));
531 const char *genimg_get_arch_name (uint8_t arch)
533 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
536 const char *genimg_get_type_name (uint8_t type)
538 return (get_table_entry_name (uimage_type, "Unknown Image", type));
541 const char *genimg_get_comp_name (uint8_t comp)
543 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
547 * get_table_entry_id - translate short entry name to id
548 * @table: pointer to a translation table for entries of a specific type
549 * @table_name: to be used in case of error
550 * @name: entry short name to be translated
552 * get_table_entry_id() will go over translation table trying to find
553 * entry that matches given short name. If matching entry is found,
554 * its id returned to the caller.
557 * entry id if translation succeeds
560 static int get_table_entry_id (table_entry_t *table,
561 const char *table_name, const char *name)
567 for (t = table; t->id >= 0; ++t) {
568 if (t->sname && strcasecmp(t->sname, name) == 0)
572 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
573 for (t = table; t->id >= 0; ++t) {
574 if (t->sname == NULL)
576 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
579 fprintf (stderr, "\n");
581 for (t = table; t->id >= 0; ++t) {
582 if (t->sname && strcmp(t->sname, name) == 0)
585 debug ("Invalid %s Type: %s\n", table_name, name);
586 #endif /* USE_HOSTCC */
590 int genimg_get_os_id (const char *name)
592 return (get_table_entry_id (uimage_os, "OS", name));
595 int genimg_get_arch_id (const char *name)
597 return (get_table_entry_id (uimage_arch, "CPU", name));
600 int genimg_get_type_id (const char *name)
602 return (get_table_entry_id (uimage_type, "Image", name));
605 int genimg_get_comp_id (const char *name)
607 return (get_table_entry_id (uimage_comp, "Compression", name));
612 * genimg_get_format - get image format type
613 * @img_addr: image start address
615 * genimg_get_format() checks whether provided address points to a valid
616 * legacy or FIT image.
618 * New uImage format and FDT blob are based on a libfdt. FDT blob
619 * may be passed directly or embedded in a FIT image. In both situations
620 * genimg_get_format() must be able to dectect libfdt header.
623 * image format type or IMAGE_FORMAT_INVALID if no image is present
625 int genimg_get_format (void *img_addr)
627 ulong format = IMAGE_FORMAT_INVALID;
629 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
633 hdr = (image_header_t *)img_addr;
634 if (image_check_magic(hdr))
635 format = IMAGE_FORMAT_LEGACY;
636 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
638 fit_hdr = (char *)img_addr;
639 if (fdt_check_header (fit_hdr) == 0)
640 format = IMAGE_FORMAT_FIT;
648 * genimg_get_image - get image from special storage (if necessary)
649 * @img_addr: image start address
651 * genimg_get_image() checks if provided image start adddress is located
652 * in a dataflash storage. If so, image is moved to a system RAM memory.
655 * image start address after possible relocation from special storage
657 ulong genimg_get_image (ulong img_addr)
659 ulong ram_addr = img_addr;
661 #ifdef CONFIG_HAS_DATAFLASH
662 ulong h_size, d_size;
664 if (addr_dataflash (img_addr)){
665 /* ger RAM address */
666 ram_addr = CFG_LOAD_ADDR;
668 /* get header size */
669 h_size = image_get_header_size ();
670 #if defined(CONFIG_FIT)
671 if (sizeof(struct fdt_header) > h_size)
672 h_size = sizeof(struct fdt_header);
676 debug (" Reading image header from dataflash address "
677 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
679 read_dataflash (img_addr, h_size, (char *)ram_addr);
682 switch (genimg_get_format ((void *)ram_addr)) {
683 case IMAGE_FORMAT_LEGACY:
684 d_size = image_get_data_size ((image_header_t *)ram_addr);
685 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
688 #if defined(CONFIG_FIT)
689 case IMAGE_FORMAT_FIT:
690 d_size = fit_get_size ((const void *)ram_addr) - h_size;
691 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
696 printf (" No valid image found at 0x%08lx\n", img_addr);
700 /* read in image data */
701 debug (" Reading image remaining data from dataflash address "
702 "%08lx to RAM address %08lx\n", img_addr + h_size,
705 read_dataflash (img_addr + h_size, d_size,
706 (char *)(ram_addr + h_size));
709 #endif /* CONFIG_HAS_DATAFLASH */
715 * fit_has_config - check if there is a valid FIT configuration
716 * @images: pointer to the bootm command headers structure
718 * fit_has_config() checks if there is a FIT configuration in use
719 * (if FTI support is present).
722 * 0, no FIT support or no configuration found
723 * 1, configuration found
725 int genimg_has_config (bootm_headers_t *images)
727 #if defined(CONFIG_FIT)
728 if (images->fit_uname_cfg)
735 * boot_get_ramdisk - main ramdisk handling routine
736 * @argc: command argument count
737 * @argv: command argument list
738 * @images: pointer to the bootm images structure
739 * @arch: expected ramdisk architecture
740 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
741 * @rd_end: pointer to a ulong variable, will hold ramdisk end
743 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
744 * Curently supported are the following ramdisk sources:
745 * - multicomponent kernel/ramdisk image,
746 * - commandline provided address of decicated ramdisk image.
749 * 0, if ramdisk image was found and valid, or skiped
750 * rd_start and rd_end are set to ramdisk start/end addresses if
751 * ramdisk image is found and valid
753 * 1, if ramdisk image is found but corrupted
754 * rd_start and rd_end are set to 0 if no ramdisk exists
756 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
757 uint8_t arch, ulong *rd_start, ulong *rd_end)
759 ulong rd_addr, rd_load;
760 ulong rd_data, rd_len;
761 image_header_t *rd_hdr;
762 #if defined(CONFIG_FIT)
764 const char *fit_uname_config = NULL;
765 const char *fit_uname_ramdisk = NULL;
777 * Look for a '-' which indicates to ignore the
780 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
781 debug ("## Skipping init Ramdisk\n");
782 rd_len = rd_data = 0;
783 } else if (argc >= 3 || genimg_has_config (images)) {
784 #if defined(CONFIG_FIT)
787 * If the init ramdisk comes from the FIT image and
788 * the FIT image address is omitted in the command
789 * line argument, try to use os FIT image address or
790 * default load address.
792 if (images->fit_uname_os)
793 default_addr = (ulong)images->fit_hdr_os;
795 default_addr = load_addr;
797 if (fit_parse_conf (argv[2], default_addr,
798 &rd_addr, &fit_uname_config)) {
799 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
800 fit_uname_config, rd_addr);
801 } else if (fit_parse_subimage (argv[2], default_addr,
802 &rd_addr, &fit_uname_ramdisk)) {
803 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
804 fit_uname_ramdisk, rd_addr);
808 rd_addr = simple_strtoul(argv[2], NULL, 16);
809 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
812 #if defined(CONFIG_FIT)
814 /* use FIT configuration provided in first bootm
817 rd_addr = (ulong)images->fit_hdr_os;
818 fit_uname_config = images->fit_uname_cfg;
819 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
820 fit_uname_config, rd_addr);
823 * Check whether configuration has ramdisk defined,
824 * if not, don't try to use it, quit silently.
826 fit_hdr = (void *)rd_addr;
827 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
828 if (cfg_noffset < 0) {
829 debug ("* ramdisk: no such config\n");
833 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
834 if (rd_noffset < 0) {
835 debug ("* ramdisk: no ramdisk in config\n");
841 /* copy from dataflash if needed */
842 rd_addr = genimg_get_image (rd_addr);
845 * Check if there is an initrd image at the
846 * address provided in the second bootm argument
847 * check image type, for FIT images get FIT node.
849 switch (genimg_get_format ((void *)rd_addr)) {
850 case IMAGE_FORMAT_LEGACY:
851 printf ("## Loading init Ramdisk from Legacy "
852 "Image at %08lx ...\n", rd_addr);
854 show_boot_progress (9);
855 rd_hdr = image_get_ramdisk (rd_addr, arch,
861 rd_data = image_get_data (rd_hdr);
862 rd_len = image_get_data_size (rd_hdr);
863 rd_load = image_get_load (rd_hdr);
865 #if defined(CONFIG_FIT)
866 case IMAGE_FORMAT_FIT:
867 fit_hdr = (void *)rd_addr;
868 printf ("## Loading init Ramdisk from FIT "
869 "Image at %08lx ...\n", rd_addr);
871 show_boot_progress (120);
872 if (!fit_check_format (fit_hdr)) {
873 puts ("Bad FIT ramdisk image format!\n");
874 show_boot_progress (-120);
877 show_boot_progress (121);
879 if (!fit_uname_ramdisk) {
881 * no ramdisk image node unit name, try to get config
882 * node first. If config unit node name is NULL
883 * fit_conf_get_node() will try to find default config node
885 show_boot_progress (122);
886 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
887 if (cfg_noffset < 0) {
888 puts ("Could not find configuration node\n");
889 show_boot_progress (-122);
892 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
893 printf (" Using '%s' configuration\n", fit_uname_config);
895 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
896 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
898 /* get ramdisk component image node offset */
899 show_boot_progress (123);
900 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
902 if (rd_noffset < 0) {
903 puts ("Could not find subimage node\n");
904 show_boot_progress (-124);
908 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
910 show_boot_progress (125);
911 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
914 /* get ramdisk image data address and length */
915 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
916 puts ("Could not find ramdisk subimage data!\n");
917 show_boot_progress (-127);
920 show_boot_progress (128);
922 rd_data = (ulong)data;
925 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
926 puts ("Can't get ramdisk subimage load address!\n");
927 show_boot_progress (-129);
930 show_boot_progress (129);
932 images->fit_hdr_rd = fit_hdr;
933 images->fit_uname_rd = fit_uname_ramdisk;
934 images->fit_noffset_rd = rd_noffset;
938 puts ("Wrong Ramdisk Image Format\n");
939 rd_data = rd_len = rd_load = 0;
942 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
944 * We need to copy the ramdisk to SRAM to let Linux boot
947 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
950 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
952 } else if (images->legacy_hdr_valid &&
953 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
955 * Now check if we have a legacy mult-component image,
956 * get second entry data start address and len.
958 show_boot_progress (13);
959 printf ("## Loading init Ramdisk from multi component "
960 "Legacy Image at %08lx ...\n",
961 (ulong)images->legacy_hdr_os);
963 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
968 show_boot_progress (14);
969 rd_len = rd_data = 0;
973 debug ("## No init Ramdisk\n");
976 *rd_end = rd_data + rd_len;
978 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
984 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
986 * boot_ramdisk_high - relocate init ramdisk
987 * @lmb: pointer to lmb handle, will be used for memory mgmt
988 * @rd_data: ramdisk data start address
989 * @rd_len: ramdisk data length
990 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
991 * start address (after possible relocation)
992 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
993 * end address (after possible relocation)
995 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
996 * variable and if requested ramdisk data is moved to a specified location.
998 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
999 * start/end addresses if ramdisk image start and len were provided,
1000 * otherwise set initrd_start and initrd_end set to zeros.
1006 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1007 ulong *initrd_start, ulong *initrd_end)
1011 int initrd_copy_to_ram = 1;
1013 if ((s = getenv ("initrd_high")) != NULL) {
1014 /* a value of "no" or a similar string will act like 0,
1015 * turning the "load high" feature off. This is intentional.
1017 initrd_high = simple_strtoul (s, NULL, 16);
1018 if (initrd_high == ~0)
1019 initrd_copy_to_ram = 0;
1021 /* not set, no restrictions to load high */
1025 /* Prevent initrd from overwriting logbuffer */
1026 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1028 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1029 initrd_high, initrd_copy_to_ram);
1032 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1033 debug (" in-place initrd\n");
1034 *initrd_start = rd_data;
1035 *initrd_end = rd_data + rd_len;
1036 lmb_reserve(lmb, rd_data, rd_len);
1039 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1041 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
1043 if (*initrd_start == 0) {
1044 puts ("ramdisk - allocation error\n");
1047 show_boot_progress (12);
1049 *initrd_end = *initrd_start + rd_len;
1050 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1051 *initrd_start, *initrd_end);
1053 memmove_wd ((void *)*initrd_start,
1054 (void *)rd_data, rd_len, CHUNKSZ);
1062 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1063 *initrd_start, *initrd_end);
1072 * boot_get_cmdline - allocate and initialize kernel cmdline
1073 * @lmb: pointer to lmb handle, will be used for memory mgmt
1074 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1075 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1076 * @bootmap_base: ulong variable, holds offset in physical memory to
1079 * boot_get_cmdline() allocates space for kernel command line below
1080 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1081 * variable is present its contents is copied to allocated kernel
1088 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1094 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1095 CFG_BOOTMAPSZ + bootmap_base);
1097 if (cmdline == NULL)
1100 if ((s = getenv("bootargs")) == NULL)
1105 *cmd_start = (ulong) & cmdline[0];
1106 *cmd_end = *cmd_start + strlen(cmdline);
1108 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1114 * boot_get_kbd - allocate and initialize kernel copy of board info
1115 * @lmb: pointer to lmb handle, will be used for memory mgmt
1116 * @kbd: double pointer to board info data
1117 * @bootmap_base: ulong variable, holds offset in physical memory to
1120 * boot_get_kbd() allocates space for kernel copy of board info data below
1121 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1122 * the current u-boot board info data.
1128 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1130 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1131 CFG_BOOTMAPSZ + bootmap_base);
1137 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1139 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1140 do_bdinfo(NULL, 0, 0, NULL);
1145 #endif /* CONFIG_PPC || CONFIG_M68K */
1146 #endif /* !USE_HOSTCC */
1148 #if defined(CONFIG_FIT)
1149 /*****************************************************************************/
1150 /* New uImage format routines */
1151 /*****************************************************************************/
1153 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1154 ulong *addr, const char **name)
1161 sep = strchr (spec, sepc);
1164 *addr = simple_strtoul (spec, NULL, 16);
1174 * fit_parse_conf - parse FIT configuration spec
1175 * @spec: input string, containing configuration spec
1176 * @add_curr: current image address (to be used as a possible default)
1177 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1179 * @conf_name double pointer to a char, will hold pointer to a configuration
1182 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1183 * where <addr> is a FIT image address that contains configuration
1184 * with a <conf> unit name.
1186 * Address part is optional, and if omitted default add_curr will
1190 * 1 if spec is a valid configuration string,
1191 * addr and conf_name are set accordingly
1194 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1195 ulong *addr, const char **conf_name)
1197 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1201 * fit_parse_subimage - parse FIT subimage spec
1202 * @spec: input string, containing subimage spec
1203 * @add_curr: current image address (to be used as a possible default)
1204 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1206 * @image_name: double pointer to a char, will hold pointer to a subimage name
1208 * fit_parse_subimage() expects subimage spec in the for of
1209 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1210 * subimage with a <subimg> unit name.
1212 * Address part is optional, and if omitted default add_curr will
1216 * 1 if spec is a valid subimage string,
1217 * addr and image_name are set accordingly
1220 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1221 ulong *addr, const char **image_name)
1223 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1225 #endif /* !USE_HOSTCC */
1227 static void fit_get_debug (const void *fit, int noffset,
1228 char *prop_name, int err)
1230 debug ("Can't get '%s' property from FIT 0x%08lx, "
1231 "node: offset %d, name %s (%s)\n",
1232 prop_name, (ulong)fit, noffset,
1233 fit_get_name (fit, noffset, NULL),
1234 fdt_strerror (err));
1238 * fit_print_contents - prints out the contents of the FIT format image
1239 * @fit: pointer to the FIT format image header
1240 * @p: pointer to prefix string
1242 * fit_print_contents() formats a multi line FIT image contents description.
1243 * The routine prints out FIT image properties (root node level) follwed by
1244 * the details of each component image.
1247 * no returned results
1249 void fit_print_contents (const void *fit)
1260 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1270 /* Root node properties */
1271 ret = fit_get_desc (fit, 0, &desc);
1272 printf ("%sFIT description: ", p);
1274 printf ("unavailable\n");
1276 printf ("%s\n", desc);
1278 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1279 ret = fit_get_timestamp (fit, 0, ×tamp);
1280 printf ("%sCreated: ", p);
1282 printf ("unavailable\n");
1284 genimg_print_time (timestamp);
1287 /* Find images parent node offset */
1288 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1289 if (images_noffset < 0) {
1290 printf ("Can't find images parent node '%s' (%s)\n",
1291 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1295 /* Process its subnodes, print out component images details */
1296 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1297 (noffset >= 0) && (ndepth > 0);
1298 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1301 * Direct child node of the images parent node,
1302 * i.e. component image node.
1304 printf ("%s Image %u (%s)\n", p, count++,
1305 fit_get_name(fit, noffset, NULL));
1307 fit_image_print (fit, noffset, p);
1311 /* Find configurations parent node offset */
1312 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1313 if (confs_noffset < 0) {
1314 debug ("Can't get configurations parent node '%s' (%s)\n",
1315 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1319 /* get default configuration unit name from default property */
1320 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1322 printf ("%s Default Configuration: '%s'\n", p, uname);
1324 /* Process its subnodes, print out configurations details */
1325 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1326 (noffset >= 0) && (ndepth > 0);
1327 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1330 * Direct child node of the configurations parent node,
1331 * i.e. configuration node.
1333 printf ("%s Configuration %u (%s)\n", p, count++,
1334 fit_get_name(fit, noffset, NULL));
1336 fit_conf_print (fit, noffset, p);
1342 * fit_image_print - prints out the FIT component image details
1343 * @fit: pointer to the FIT format image header
1344 * @image_noffset: offset of the component image node
1345 * @p: pointer to prefix string
1347 * fit_image_print() lists all mandatory properies for the processed component
1348 * image. If present, hash nodes are printed out as well.
1351 * no returned results
1353 void fit_image_print (const void *fit, int image_noffset, const char *p)
1356 uint8_t type, arch, os, comp;
1364 /* Mandatory properties */
1365 ret = fit_get_desc (fit, image_noffset, &desc);
1366 printf ("%s Description: ", p);
1368 printf ("unavailable\n");
1370 printf ("%s\n", desc);
1372 fit_image_get_type (fit, image_noffset, &type);
1373 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1375 fit_image_get_comp (fit, image_noffset, &comp);
1376 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1378 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1381 printf ("%s Data Start: ", p);
1383 printf ("unavailable\n");
1385 printf ("0x%08lx\n", (ulong)data);
1388 printf ("%s Data Size: ", p);
1390 printf ("unavailable\n");
1392 genimg_print_size (size);
1394 /* Remaining, type dependent properties */
1395 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1396 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1397 (type == IH_TYPE_FLATDT)) {
1398 fit_image_get_arch (fit, image_noffset, &arch);
1399 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1402 if (type == IH_TYPE_KERNEL) {
1403 fit_image_get_os (fit, image_noffset, &os);
1404 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1407 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1408 ret = fit_image_get_load (fit, image_noffset, &load);
1409 printf ("%s Load Address: ", p);
1411 printf ("unavailable\n");
1413 printf ("0x%08lx\n", load);
1415 fit_image_get_entry (fit, image_noffset, &entry);
1416 printf ("%s Entry Point: ", p);
1418 printf ("unavailable\n");
1420 printf ("0x%08lx\n", entry);
1423 /* Process all hash subnodes of the component image node */
1424 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1425 (noffset >= 0) && (ndepth > 0);
1426 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1428 /* Direct child node of the component image node */
1429 fit_image_print_hash (fit, noffset, p);
1435 * fit_image_print_hash - prints out the hash node details
1436 * @fit: pointer to the FIT format image header
1437 * @noffset: offset of the hash node
1438 * @p: pointer to prefix string
1440 * fit_image_print_hash() lists properies for the processed hash node
1443 * no returned results
1445 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1453 * Check subnode name, must be equal to "hash".
1454 * Multiple hash nodes require unique unit node
1455 * names, e.g. hash@1, hash@2, etc.
1457 if (strncmp (fit_get_name(fit, noffset, NULL),
1459 strlen(FIT_HASH_NODENAME)) != 0)
1462 debug ("%s Hash node: '%s'\n", p,
1463 fit_get_name (fit, noffset, NULL));
1465 printf ("%s Hash algo: ", p);
1466 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1467 printf ("invalid/unsupported\n");
1470 printf ("%s\n", algo);
1472 ret = fit_image_hash_get_value (fit, noffset, &value,
1474 printf ("%s Hash value: ", p);
1476 printf ("unavailable\n");
1478 for (i = 0; i < value_len; i++)
1479 printf ("%02x", value[i]);
1483 debug ("%s Hash len: %d\n", p, value_len);
1487 * fit_get_desc - get node description property
1488 * @fit: pointer to the FIT format image header
1489 * @noffset: node offset
1490 * @desc: double pointer to the char, will hold pointer to the descrption
1492 * fit_get_desc() reads description property from a given node, if
1493 * description is found pointer to it is returened in third call argument.
1499 int fit_get_desc (const void *fit, int noffset, char **desc)
1503 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1504 if (*desc == NULL) {
1505 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1513 * fit_get_timestamp - get node timestamp property
1514 * @fit: pointer to the FIT format image header
1515 * @noffset: node offset
1516 * @timestamp: pointer to the time_t, will hold read timestamp
1518 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1519 * is found and has a correct size its value is retured in third call
1524 * -1, on property read failure
1525 * -2, on wrong timestamp size
1527 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1532 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1534 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1537 if (len != sizeof (uint32_t)) {
1538 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1542 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1547 * fit_image_get_node - get node offset for component image of a given unit name
1548 * @fit: pointer to the FIT format image header
1549 * @image_uname: component image node unit name
1551 * fit_image_get_node() finds a component image (withing the '/images'
1552 * node) of a provided unit name. If image is found its node offset is
1553 * returned to the caller.
1556 * image node offset when found (>=0)
1557 * negative number on failure (FDT_ERR_* code)
1559 int fit_image_get_node (const void *fit, const char *image_uname)
1561 int noffset, images_noffset;
1563 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1564 if (images_noffset < 0) {
1565 debug ("Can't find images parent node '%s' (%s)\n",
1566 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1567 return images_noffset;
1570 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1572 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1573 image_uname, fdt_strerror (noffset));
1580 * fit_image_get_os - get os id for a given component image node
1581 * @fit: pointer to the FIT format image header
1582 * @noffset: component image node offset
1583 * @os: pointer to the uint8_t, will hold os numeric id
1585 * fit_image_get_os() finds os property in a given component image node.
1586 * If the property is found, its (string) value is translated to the numeric
1587 * id which is returned to the caller.
1593 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1598 /* Get OS name from property data */
1599 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1601 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1606 /* Translate OS name to id */
1607 *os = genimg_get_os_id (data);
1612 * fit_image_get_arch - get arch id for a given component image node
1613 * @fit: pointer to the FIT format image header
1614 * @noffset: component image node offset
1615 * @arch: pointer to the uint8_t, will hold arch numeric id
1617 * fit_image_get_arch() finds arch property in a given component image node.
1618 * If the property is found, its (string) value is translated to the numeric
1619 * id which is returned to the caller.
1625 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1630 /* Get architecture name from property data */
1631 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1633 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1638 /* Translate architecture name to id */
1639 *arch = genimg_get_arch_id (data);
1644 * fit_image_get_type - get type id for a given component image node
1645 * @fit: pointer to the FIT format image header
1646 * @noffset: component image node offset
1647 * @type: pointer to the uint8_t, will hold type numeric id
1649 * fit_image_get_type() finds type property in a given component image node.
1650 * If the property is found, its (string) value is translated to the numeric
1651 * id which is returned to the caller.
1657 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1662 /* Get image type name from property data */
1663 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1665 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1670 /* Translate image type name to id */
1671 *type = genimg_get_type_id (data);
1676 * fit_image_get_comp - get comp id for a given component image node
1677 * @fit: pointer to the FIT format image header
1678 * @noffset: component image node offset
1679 * @comp: pointer to the uint8_t, will hold comp numeric id
1681 * fit_image_get_comp() finds comp property in a given component image node.
1682 * If the property is found, its (string) value is translated to the numeric
1683 * id which is returned to the caller.
1689 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1694 /* Get compression name from property data */
1695 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1697 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1702 /* Translate compression name to id */
1703 *comp = genimg_get_comp_id (data);
1708 * fit_image_get_load - get load address property for a given component image node
1709 * @fit: pointer to the FIT format image header
1710 * @noffset: component image node offset
1711 * @load: pointer to the uint32_t, will hold load address
1713 * fit_image_get_load() finds load address property in a given component image node.
1714 * If the property is found, its value is returned to the caller.
1720 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1723 const uint32_t *data;
1725 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1727 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1731 *load = uimage_to_cpu (*data);
1736 * fit_image_get_entry - get entry point address property for a given component image node
1737 * @fit: pointer to the FIT format image header
1738 * @noffset: component image node offset
1739 * @entry: pointer to the uint32_t, will hold entry point address
1741 * fit_image_get_entry() finds entry point address property in a given component image node.
1742 * If the property is found, its value is returned to the caller.
1748 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1751 const uint32_t *data;
1753 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1755 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1759 *entry = uimage_to_cpu (*data);
1764 * fit_image_get_data - get data property and its size for a given component image node
1765 * @fit: pointer to the FIT format image header
1766 * @noffset: component image node offset
1767 * @data: double pointer to void, will hold data property's data address
1768 * @size: pointer to size_t, will hold data property's data size
1770 * fit_image_get_data() finds data property in a given component image node.
1771 * If the property is found its data start address and size are returned to
1778 int fit_image_get_data (const void *fit, int noffset,
1779 const void **data, size_t *size)
1783 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1784 if (*data == NULL) {
1785 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1795 * fit_image_hash_get_algo - get hash algorithm name
1796 * @fit: pointer to the FIT format image header
1797 * @noffset: hash node offset
1798 * @algo: double pointer to char, will hold pointer to the algorithm name
1800 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1801 * If the property is found its data start address is returned to the caller.
1807 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1811 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1812 if (*algo == NULL) {
1813 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1821 * fit_image_hash_get_value - get hash value and length
1822 * @fit: pointer to the FIT format image header
1823 * @noffset: hash node offset
1824 * @value: double pointer to uint8_t, will hold address of a hash value data
1825 * @value_len: pointer to an int, will hold hash data length
1827 * fit_image_hash_get_value() finds hash value property in a given hash node.
1828 * If the property is found its data start address and size are returned to
1835 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1840 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1841 if (*value == NULL) {
1842 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1852 * fit_set_timestamp - set node timestamp property
1853 * @fit: pointer to the FIT format image header
1854 * @noffset: node offset
1855 * @timestamp: timestamp value to be set
1857 * fit_set_timestamp() attempts to set timestamp property in the requested
1858 * node and returns operation status to the caller.
1862 * -1, on property read failure
1864 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1869 t = cpu_to_uimage (timestamp);
1870 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1873 printf ("Can't set '%s' property for '%s' node (%s)\n",
1874 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1875 fdt_strerror (ret));
1883 * calculate_hash - calculate and return hash for provided input data
1884 * @data: pointer to the input data
1885 * @data_len: data length
1886 * @algo: requested hash algorithm
1887 * @value: pointer to the char, will hold hash value data (caller must
1888 * allocate enough free space)
1889 * value_len: length of the calculated hash
1891 * calculate_hash() computes input data hash according to the requested algorithm.
1892 * Resulting hash value is placed in caller provided 'value' buffer, length
1893 * of the calculated hash is returned via value_len pointer argument.
1897 * -1, when algo is unsupported
1899 static int calculate_hash (const void *data, int data_len, const char *algo,
1900 uint8_t *value, int *value_len)
1902 if (strcmp (algo, "crc32") == 0 ) {
1903 *((uint32_t *)value) = crc32_wd (0, data, data_len,
1905 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1907 } else if (strcmp (algo, "sha1") == 0 ) {
1908 sha1_csum_wd ((unsigned char *) data, data_len,
1909 (unsigned char *) value, CHUNKSZ_SHA1);
1911 } else if (strcmp (algo, "md5") == 0 ) {
1912 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1915 debug ("Unsupported hash alogrithm\n");
1923 * fit_set_hashes - process FIT component image nodes and calculate hashes
1924 * @fit: pointer to the FIT format image header
1926 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1927 * Hashes are calculated for all component images which have hash subnodes
1928 * with algorithm property set to one of the supported hash algorithms.
1932 * libfdt error code, on failure
1934 int fit_set_hashes (void *fit)
1941 /* Find images parent node offset */
1942 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1943 if (images_noffset < 0) {
1944 printf ("Can't find images parent node '%s' (%s)\n",
1945 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1946 return images_noffset;
1949 /* Process its subnodes, print out component images details */
1950 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1951 (noffset >= 0) && (ndepth > 0);
1952 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1955 * Direct child node of the images parent node,
1956 * i.e. component image node.
1958 ret = fit_image_set_hashes (fit, noffset);
1968 * fit_image_set_hashes - calculate/set hashes for given component image node
1969 * @fit: pointer to the FIT format image header
1970 * @image_noffset: requested component image node
1972 * fit_image_set_hashes() adds hash values for an component image node. All
1973 * existing hash subnodes are checked, if algorithm property is set to one of
1974 * the supported hash algorithms, hash value is computed and corresponding
1975 * hash node property is set, for example:
1977 * Input component image node structure:
1979 * o image@1 (at image_noffset)
1980 * | - data = [binary data]
1984 * Output component image node structure:
1986 * o image@1 (at image_noffset)
1987 * | - data = [binary data]
1990 * |- value = sha1(data)
1996 int fit_image_set_hashes (void *fit, int image_noffset)
2001 uint8_t value[FIT_MAX_HASH_LEN];
2006 /* Get image data and data length */
2007 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2008 printf ("Can't get image data/size\n");
2012 /* Process all hash subnodes of the component image node */
2013 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2014 (noffset >= 0) && (ndepth > 0);
2015 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2017 /* Direct child node of the component image node */
2020 * Check subnode name, must be equal to "hash".
2021 * Multiple hash nodes require unique unit node
2022 * names, e.g. hash@1, hash@2, etc.
2024 if (strncmp (fit_get_name(fit, noffset, NULL),
2026 strlen(FIT_HASH_NODENAME)) != 0) {
2027 /* Not a hash subnode, skip it */
2031 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2032 printf ("Can't get hash algo property for "
2033 "'%s' hash node in '%s' image node\n",
2034 fit_get_name (fit, noffset, NULL),
2035 fit_get_name (fit, image_noffset, NULL));
2039 if (calculate_hash (data, size, algo, value, &value_len)) {
2040 printf ("Unsupported hash algorithm (%s) for "
2041 "'%s' hash node in '%s' image node\n",
2042 algo, fit_get_name (fit, noffset, NULL),
2043 fit_get_name (fit, image_noffset, NULL));
2047 if (fit_image_hash_set_value (fit, noffset, value,
2049 printf ("Can't set hash value for "
2050 "'%s' hash node in '%s' image node\n",
2051 fit_get_name (fit, noffset, NULL),
2052 fit_get_name (fit, image_noffset, NULL));
2062 * fit_image_hash_set_value - set hash value in requested has node
2063 * @fit: pointer to the FIT format image header
2064 * @noffset: hash node offset
2065 * @value: hash value to be set
2066 * @value_len: hash value length
2068 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2069 * given and returns operation status to the caller.
2075 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2080 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2082 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2083 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2084 fdt_strerror (ret));
2090 #endif /* USE_HOSTCC */
2093 * fit_image_check_hashes - verify data intergity
2094 * @fit: pointer to the FIT format image header
2095 * @image_noffset: component image node offset
2097 * fit_image_check_hashes() goes over component image hash nodes,
2098 * re-calculates each data hash and compares with the value stored in hash
2102 * 1, if all hashes are valid
2103 * 0, otherwise (or on error)
2105 int fit_image_check_hashes (const void *fit, int image_noffset)
2112 uint8_t value[FIT_MAX_HASH_LEN];
2118 /* Get image data and data length */
2119 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2120 printf ("Can't get image data/size\n");
2124 /* Process all hash subnodes of the component image node */
2125 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2126 (noffset >= 0) && (ndepth > 0);
2127 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2129 /* Direct child node of the component image node */
2132 * Check subnode name, must be equal to "hash".
2133 * Multiple hash nodes require unique unit node
2134 * names, e.g. hash@1, hash@2, etc.
2136 if (strncmp (fit_get_name(fit, noffset, NULL),
2138 strlen(FIT_HASH_NODENAME)) != 0)
2141 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2142 err_msg = "Can't get hash algo property";
2145 printf ("%s", algo);
2147 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2149 err_msg = "Can't get hash value property";
2153 if (calculate_hash (data, size, algo, value, &value_len)) {
2154 err_msg = "Unsupported hash algorithm";
2158 if (value_len != fit_value_len) {
2159 err_msg = "Bad hash value len";
2161 } else if (memcmp (value, fit_value, value_len) != 0) {
2162 err_msg = "Bad hash value";
2172 printf ("%s for '%s' hash node in '%s' image node\n",
2173 err_msg, fit_get_name (fit, noffset, NULL),
2174 fit_get_name (fit, image_noffset, NULL));
2179 * fit_image_check_os - check whether image node is of a given os type
2180 * @fit: pointer to the FIT format image header
2181 * @noffset: component image node offset
2182 * @os: requested image os
2184 * fit_image_check_os() reads image os property and compares its numeric
2185 * id with the requested os. Comparison result is returned to the caller.
2188 * 1 if image is of given os type
2189 * 0 otherwise (or on error)
2191 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2195 if (fit_image_get_os (fit, noffset, &image_os))
2197 return (os == image_os);
2201 * fit_image_check_arch - check whether image node is of a given arch
2202 * @fit: pointer to the FIT format image header
2203 * @noffset: component image node offset
2204 * @arch: requested imagearch
2206 * fit_image_check_arch() reads image arch property and compares its numeric
2207 * id with the requested arch. Comparison result is returned to the caller.
2210 * 1 if image is of given arch
2211 * 0 otherwise (or on error)
2213 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2217 if (fit_image_get_arch (fit, noffset, &image_arch))
2219 return (arch == image_arch);
2223 * fit_image_check_type - check whether image node is of a given type
2224 * @fit: pointer to the FIT format image header
2225 * @noffset: component image node offset
2226 * @type: requested image type
2228 * fit_image_check_type() reads image type property and compares its numeric
2229 * id with the requested type. Comparison result is returned to the caller.
2232 * 1 if image is of given type
2233 * 0 otherwise (or on error)
2235 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2239 if (fit_image_get_type (fit, noffset, &image_type))
2241 return (type == image_type);
2245 * fit_image_check_comp - check whether image node uses given compression
2246 * @fit: pointer to the FIT format image header
2247 * @noffset: component image node offset
2248 * @comp: requested image compression type
2250 * fit_image_check_comp() reads image compression property and compares its
2251 * numeric id with the requested compression type. Comparison result is
2252 * returned to the caller.
2255 * 1 if image uses requested compression
2256 * 0 otherwise (or on error)
2258 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2262 if (fit_image_get_comp (fit, noffset, &image_comp))
2264 return (comp == image_comp);
2268 * fit_check_format - sanity check FIT image format
2269 * @fit: pointer to the FIT format image header
2271 * fit_check_format() runs a basic sanity FIT image verification.
2272 * Routine checks for mandatory properties, nodes, etc.
2278 int fit_check_format (const void *fit)
2280 /* mandatory / node 'description' property */
2281 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2282 debug ("Wrong FIT format: no description\n");
2286 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2287 /* mandatory / node 'timestamp' property */
2288 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2289 debug ("Wrong FIT format: no description\n");
2294 /* mandatory subimages parent '/images' node */
2295 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2296 debug ("Wrong FIT format: no images parent node\n");
2304 * fit_conf_get_node - get node offset for configuration of a given unit name
2305 * @fit: pointer to the FIT format image header
2306 * @conf_uname: configuration node unit name
2308 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2309 * parant node) of a provided unit name. If configuration is found its node offset
2310 * is returned to the caller.
2312 * When NULL is provided in second argument fit_conf_get_node() will search
2313 * for a default configuration node instead. Default configuration node unit name
2314 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2317 * configuration node offset when found (>=0)
2318 * negative number on failure (FDT_ERR_* code)
2320 int fit_conf_get_node (const void *fit, const char *conf_uname)
2322 int noffset, confs_noffset;
2325 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2326 if (confs_noffset < 0) {
2327 debug ("Can't find configurations parent node '%s' (%s)\n",
2328 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2329 return confs_noffset;
2332 if (conf_uname == NULL) {
2333 /* get configuration unit name from the default property */
2334 debug ("No configuration specified, trying default...\n");
2335 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2336 if (conf_uname == NULL) {
2337 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2340 debug ("Found default configuration: '%s'\n", conf_uname);
2343 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2345 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2346 conf_uname, fdt_strerror (noffset));
2352 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2353 const char *prop_name)
2358 /* get kernel image unit name from configuration kernel property */
2359 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2363 return fit_image_get_node (fit, uname);
2367 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2368 * a given configuration
2369 * @fit: pointer to the FIT format image header
2370 * @noffset: configuration node offset
2372 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2373 * configuration FIT_KERNEL_PROP property and translates it to the node
2377 * image node offset when found (>=0)
2378 * negative number on failure (FDT_ERR_* code)
2380 int fit_conf_get_kernel_node (const void *fit, int noffset)
2382 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2386 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2387 * a given configuration
2388 * @fit: pointer to the FIT format image header
2389 * @noffset: configuration node offset
2391 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2392 * configuration FIT_KERNEL_PROP property and translates it to the node
2396 * image node offset when found (>=0)
2397 * negative number on failure (FDT_ERR_* code)
2399 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2401 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2405 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2406 * a given configuration
2407 * @fit: pointer to the FIT format image header
2408 * @noffset: configuration node offset
2410 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2411 * configuration FIT_KERNEL_PROP property and translates it to the node
2415 * image node offset when found (>=0)
2416 * negative number on failure (FDT_ERR_* code)
2418 int fit_conf_get_fdt_node (const void *fit, int noffset)
2420 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2424 * fit_conf_print - prints out the FIT configuration details
2425 * @fit: pointer to the FIT format image header
2426 * @noffset: offset of the configuration node
2427 * @p: pointer to prefix string
2429 * fit_conf_print() lists all mandatory properies for the processed
2430 * configuration node.
2433 * no returned results
2435 void fit_conf_print (const void *fit, int noffset, const char *p)
2441 /* Mandatory properties */
2442 ret = fit_get_desc (fit, noffset, &desc);
2443 printf ("%s Description: ", p);
2445 printf ("unavailable\n");
2447 printf ("%s\n", desc);
2449 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2450 printf ("%s Kernel: ", p);
2452 printf ("unavailable\n");
2454 printf ("%s\n", uname);
2456 /* Optional properties */
2457 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2459 printf ("%s Init Ramdisk: %s\n", p, uname);
2461 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2463 printf ("%s FDT: %s\n", p, uname);
2467 * fit_check_ramdisk - verify FIT format ramdisk subimage
2468 * @fit_hdr: pointer to the FIT ramdisk header
2469 * @rd_noffset: ramdisk subimage node offset within FIT image
2470 * @arch: requested ramdisk image architecture type
2471 * @verify: data CRC verification flag
2473 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2474 * specified FIT image.
2481 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2483 fit_image_print (fit, rd_noffset, " ");
2486 puts (" Verifying Hash Integrity ... ");
2487 if (!fit_image_check_hashes (fit, rd_noffset)) {
2488 puts ("Bad Data Hash\n");
2489 show_boot_progress (-125);
2495 show_boot_progress (126);
2496 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2497 !fit_image_check_arch (fit, rd_noffset, arch) ||
2498 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2499 printf ("No Linux %s Ramdisk Image\n",
2500 genimg_get_arch_name(arch));
2501 show_boot_progress (-126);
2505 show_boot_progress (127);
2508 #endif /* USE_HOSTCC */
2509 #endif /* CONFIG_FIT */