2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #ifdef CONFIG_SHOW_BOOT_PROGRESS
31 #include <status_led.h>
34 #ifdef CONFIG_HAS_DATAFLASH
35 #include <dataflash.h>
38 #ifdef CONFIG_LOGBUFFER
42 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
48 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
51 #include <fdt_support.h>
54 #if defined(CONFIG_FIT)
55 #include <u-boot/md5.h>
58 static int fit_check_ramdisk (const void *fit, int os_noffset,
59 uint8_t arch, int verify);
63 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
66 DECLARE_GLOBAL_DATA_PTR;
68 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
72 #include <u-boot/md5.h>
75 #endif /* !USE_HOSTCC*/
77 typedef struct table_entry {
78 int id; /* as defined in image.h */
79 char *sname; /* short (input) name */
80 char *lname; /* long (output) name */
83 static table_entry_t uimage_arch[] = {
84 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
85 { IH_ARCH_ALPHA, "alpha", "Alpha", },
86 { IH_ARCH_ARM, "arm", "ARM", },
87 { IH_ARCH_I386, "x86", "Intel x86", },
88 { IH_ARCH_IA64, "ia64", "IA64", },
89 { IH_ARCH_M68K, "m68k", "M68K", },
90 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
91 { IH_ARCH_MIPS, "mips", "MIPS", },
92 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
93 { IH_ARCH_NIOS, "nios", "NIOS", },
94 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
95 { IH_ARCH_PPC, "powerpc", "PowerPC", },
96 { IH_ARCH_PPC, "ppc", "PowerPC", },
97 { IH_ARCH_S390, "s390", "IBM S390", },
98 { IH_ARCH_SH, "sh", "SuperH", },
99 { IH_ARCH_SPARC, "sparc", "SPARC", },
100 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
101 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
102 { IH_ARCH_AVR32, "avr32", "AVR32", },
106 static table_entry_t uimage_os[] = {
107 { IH_OS_INVALID, NULL, "Invalid OS", },
108 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
109 { IH_OS_ARTOS, "artos", "ARTOS", },
111 { IH_OS_LINUX, "linux", "Linux", },
112 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
113 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
115 { IH_OS_NETBSD, "netbsd", "NetBSD", },
116 { IH_OS_RTEMS, "rtems", "RTEMS", },
117 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
118 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
119 { IH_OS_QNX, "qnx", "QNX", },
120 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
123 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
124 { IH_OS_DELL, "dell", "Dell", },
125 { IH_OS_ESIX, "esix", "Esix", },
126 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
127 { IH_OS_IRIX, "irix", "Irix", },
128 { IH_OS_NCR, "ncr", "NCR", },
129 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
130 { IH_OS_PSOS, "psos", "pSOS", },
131 { IH_OS_SCO, "sco", "SCO", },
132 { IH_OS_SOLARIS, "solaris", "Solaris", },
133 { IH_OS_SVR4, "svr4", "SVR4", },
138 static table_entry_t uimage_type[] = {
139 { IH_TYPE_INVALID, NULL, "Invalid Image", },
140 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
141 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
142 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
143 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
144 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
145 { IH_TYPE_SCRIPT, "script", "Script", },
146 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
147 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
151 static table_entry_t uimage_comp[] = {
152 { IH_COMP_NONE, "none", "uncompressed", },
153 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
154 { IH_COMP_GZIP, "gzip", "gzip compressed", },
158 uint32_t crc32 (uint32_t, const unsigned char *, uint);
159 uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
160 static void genimg_print_size (uint32_t size);
161 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
162 static void genimg_print_time (time_t timestamp);
165 /*****************************************************************************/
166 /* Legacy format routines */
167 /*****************************************************************************/
168 int image_check_hcrc (image_header_t *hdr)
171 ulong len = image_get_header_size ();
172 image_header_t header;
174 /* Copy header so we can blank CRC field for re-calculation */
175 memmove (&header, (char *)hdr, image_get_header_size ());
176 image_set_hcrc (&header, 0);
178 hcrc = crc32 (0, (unsigned char *)&header, len);
180 return (hcrc == image_get_hcrc (hdr));
183 int image_check_dcrc (image_header_t *hdr)
185 ulong data = image_get_data (hdr);
186 ulong len = image_get_data_size (hdr);
187 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
189 return (dcrc == image_get_dcrc (hdr));
194 * image_multi_count - get component (sub-image) count
195 * @hdr: pointer to the header of the multi component image
197 * image_multi_count() returns number of components in a multi
200 * Note: no checking of the image type is done, caller must pass
201 * a valid multi component image.
204 * number of components
206 ulong image_multi_count (image_header_t *hdr)
211 /* get start of the image payload, which in case of multi
212 * component images that points to a table of component sizes */
213 size = (uint32_t *)image_get_data (hdr);
215 /* count non empty slots */
216 for (i = 0; size[i]; ++i)
223 * image_multi_getimg - get component data address and size
224 * @hdr: pointer to the header of the multi component image
225 * @idx: index of the requested component
226 * @data: pointer to a ulong variable, will hold component data address
227 * @len: pointer to a ulong variable, will hold component size
229 * image_multi_getimg() returns size and data address for the requested
230 * component in a multi component image.
232 * Note: no checking of the image type is done, caller must pass
233 * a valid multi component image.
236 * data address and size of the component, if idx is valid
237 * 0 in data and len, if idx is out of range
239 void image_multi_getimg (image_header_t *hdr, ulong idx,
240 ulong *data, ulong *len)
244 ulong offset, count, img_data;
246 /* get number of component */
247 count = image_multi_count (hdr);
249 /* get start of the image payload, which in case of multi
250 * component images that points to a table of component sizes */
251 size = (uint32_t *)image_get_data (hdr);
253 /* get address of the proper component data start, which means
254 * skipping sizes table (add 1 for last, null entry) */
255 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
258 *len = uimage_to_cpu (size[idx]);
261 /* go over all indices preceding requested component idx */
262 for (i = 0; i < idx; i++) {
263 /* add up i-th component size, rounding up to 4 bytes */
264 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
267 /* calculate idx-th component data address */
268 *data = img_data + offset;
275 static void image_print_type (image_header_t *hdr)
277 const char *os, *arch, *type, *comp;
279 os = genimg_get_os_name (image_get_os (hdr));
280 arch = genimg_get_arch_name (image_get_arch (hdr));
281 type = genimg_get_type_name (image_get_type (hdr));
282 comp = genimg_get_comp_name (image_get_comp (hdr));
284 printf ("%s %s %s (%s)\n", arch, os, type, comp);
288 * image_print_contents - prints out the contents of the legacy format image
289 * @hdr: pointer to the legacy format image header
290 * @p: pointer to prefix string
292 * image_print_contents() formats a multi line legacy image contents description.
293 * The routine prints out all header fields followed by the size/offset data
294 * for MULTI/SCRIPT images.
297 * no returned results
299 void image_print_contents (image_header_t *hdr)
309 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
310 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
311 printf ("%sCreated: ", p);
312 genimg_print_time ((time_t)image_get_time (hdr));
314 printf ("%sImage Type: ", p);
315 image_print_type (hdr);
316 printf ("%sData Size: ", p);
317 genimg_print_size (image_get_data_size (hdr));
318 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
319 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
321 if (image_check_type (hdr, IH_TYPE_MULTI) ||
322 image_check_type (hdr, IH_TYPE_SCRIPT)) {
325 ulong count = image_multi_count (hdr);
327 printf ("%sContents:\n", p);
328 for (i = 0; i < count; i++) {
329 image_multi_getimg (hdr, i, &data, &len);
331 printf ("%s Image %d: ", p, i);
332 genimg_print_size (len);
334 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
336 * the user may need to know offsets
337 * if planning to do something with
340 printf ("%s Offset = 0x%08lx\n", p, data);
349 * image_get_ramdisk - get and verify ramdisk image
350 * @rd_addr: ramdisk image start address
351 * @arch: expected ramdisk architecture
352 * @verify: checksum verification flag
354 * image_get_ramdisk() returns a pointer to the verified ramdisk image
355 * header. Routine receives image start address and expected architecture
356 * flag. Verification done covers data and header integrity and os/type/arch
359 * If dataflash support is enabled routine checks for dataflash addresses
360 * and handles required dataflash reads.
363 * pointer to a ramdisk image header, if image was found and valid
364 * otherwise, return NULL
366 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
369 image_header_t *rd_hdr = (image_header_t *)rd_addr;
371 if (!image_check_magic (rd_hdr)) {
372 puts ("Bad Magic Number\n");
373 show_boot_progress (-10);
377 if (!image_check_hcrc (rd_hdr)) {
378 puts ("Bad Header Checksum\n");
379 show_boot_progress (-11);
383 show_boot_progress (10);
384 image_print_contents (rd_hdr);
387 puts(" Verifying Checksum ... ");
388 if (!image_check_dcrc (rd_hdr)) {
389 puts ("Bad Data CRC\n");
390 show_boot_progress (-12);
396 show_boot_progress (11);
398 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
399 !image_check_arch (rd_hdr, arch) ||
400 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
401 printf ("No Linux %s Ramdisk Image\n",
402 genimg_get_arch_name(arch));
403 show_boot_progress (-13);
409 #endif /* !USE_HOSTCC */
411 /*****************************************************************************/
412 /* Shared dual-format routines */
413 /*****************************************************************************/
415 int getenv_yesno (char *var)
417 char *s = getenv (var);
418 return (s && (*s == 'n')) ? 0 : 1;
421 ulong getenv_bootm_low(void)
423 char *s = getenv ("bootm_low");
425 ulong tmp = simple_strtoul (s, NULL, 16);
429 #if defined(CFG_SDRAM_BASE)
430 return CFG_SDRAM_BASE;
431 #elif defined(CONFIG_ARM)
432 return gd->bd->bi_dram[0].start;
438 ulong getenv_bootm_size(void)
440 char *s = getenv ("bootm_size");
442 ulong tmp = simple_strtoul (s, NULL, 16);
446 #if defined(CONFIG_ARM)
447 return gd->bd->bi_dram[0].size;
449 return gd->bd->bi_memsize;
453 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
455 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
457 size_t tail = (len > chunksz) ? chunksz : len;
459 memmove (to, from, tail);
464 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
465 memmove (to, from, len);
466 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
468 #endif /* !USE_HOSTCC */
470 static void genimg_print_size (uint32_t size)
473 printf ("%d Bytes = ", size);
474 print_size (size, "\n");
476 printf ("%d Bytes = %.2f kB = %.2f MB\n",
477 size, (double)size / 1.024e3,
478 (double)size / 1.048576e6);
482 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
483 static void genimg_print_time (time_t timestamp)
488 to_tm (timestamp, &tm);
489 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
490 tm.tm_year, tm.tm_mon, tm.tm_mday,
491 tm.tm_hour, tm.tm_min, tm.tm_sec);
493 printf ("%s", ctime(×tamp));
496 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
499 * get_table_entry_name - translate entry id to long name
500 * @table: pointer to a translation table for entries of a specific type
501 * @msg: message to be returned when translation fails
502 * @id: entry id to be translated
504 * get_table_entry_name() will go over translation table trying to find
505 * entry that matches given id. If matching entry is found, its long
506 * name is returned to the caller.
509 * long entry name if translation succeeds
512 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
514 for (; table->id >= 0; ++table) {
516 return (table->lname);
521 const char *genimg_get_os_name (uint8_t os)
523 return (get_table_entry_name (uimage_os, "Unknown OS", os));
526 const char *genimg_get_arch_name (uint8_t arch)
528 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
531 const char *genimg_get_type_name (uint8_t type)
533 return (get_table_entry_name (uimage_type, "Unknown Image", type));
536 const char *genimg_get_comp_name (uint8_t comp)
538 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
542 * get_table_entry_id - translate short entry name to id
543 * @table: pointer to a translation table for entries of a specific type
544 * @table_name: to be used in case of error
545 * @name: entry short name to be translated
547 * get_table_entry_id() will go over translation table trying to find
548 * entry that matches given short name. If matching entry is found,
549 * its id returned to the caller.
552 * entry id if translation succeeds
555 static int get_table_entry_id (table_entry_t *table,
556 const char *table_name, const char *name)
562 for (t = table; t->id >= 0; ++t) {
563 if (t->sname && strcasecmp(t->sname, name) == 0)
567 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
568 for (t = table; t->id >= 0; ++t) {
569 if (t->sname == NULL)
571 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
574 fprintf (stderr, "\n");
576 for (t = table; t->id >= 0; ++t) {
577 if (t->sname && strcmp(t->sname, name) == 0)
580 debug ("Invalid %s Type: %s\n", table_name, name);
581 #endif /* USE_HOSTCC */
585 int genimg_get_os_id (const char *name)
587 return (get_table_entry_id (uimage_os, "OS", name));
590 int genimg_get_arch_id (const char *name)
592 return (get_table_entry_id (uimage_arch, "CPU", name));
595 int genimg_get_type_id (const char *name)
597 return (get_table_entry_id (uimage_type, "Image", name));
600 int genimg_get_comp_id (const char *name)
602 return (get_table_entry_id (uimage_comp, "Compression", name));
607 * genimg_get_format - get image format type
608 * @img_addr: image start address
610 * genimg_get_format() checks whether provided address points to a valid
611 * legacy or FIT image.
613 * New uImage format and FDT blob are based on a libfdt. FDT blob
614 * may be passed directly or embedded in a FIT image. In both situations
615 * genimg_get_format() must be able to dectect libfdt header.
618 * image format type or IMAGE_FORMAT_INVALID if no image is present
620 int genimg_get_format (void *img_addr)
622 ulong format = IMAGE_FORMAT_INVALID;
624 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
628 hdr = (image_header_t *)img_addr;
629 if (image_check_magic(hdr))
630 format = IMAGE_FORMAT_LEGACY;
631 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
633 fit_hdr = (char *)img_addr;
634 if (fdt_check_header (fit_hdr) == 0)
635 format = IMAGE_FORMAT_FIT;
643 * genimg_get_image - get image from special storage (if necessary)
644 * @img_addr: image start address
646 * genimg_get_image() checks if provided image start adddress is located
647 * in a dataflash storage. If so, image is moved to a system RAM memory.
650 * image start address after possible relocation from special storage
652 ulong genimg_get_image (ulong img_addr)
654 ulong ram_addr = img_addr;
656 #ifdef CONFIG_HAS_DATAFLASH
657 ulong h_size, d_size;
659 if (addr_dataflash (img_addr)){
660 /* ger RAM address */
661 ram_addr = CFG_LOAD_ADDR;
663 /* get header size */
664 h_size = image_get_header_size ();
665 #if defined(CONFIG_FIT)
666 if (sizeof(struct fdt_header) > h_size)
667 h_size = sizeof(struct fdt_header);
671 debug (" Reading image header from dataflash address "
672 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
674 read_dataflash (img_addr, h_size, (char *)ram_addr);
677 switch (genimg_get_format ((void *)ram_addr)) {
678 case IMAGE_FORMAT_LEGACY:
679 d_size = image_get_data_size ((image_header_t *)ram_addr);
680 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
683 #if defined(CONFIG_FIT)
684 case IMAGE_FORMAT_FIT:
685 d_size = fit_get_size ((const void *)ram_addr) - h_size;
686 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
691 printf (" No valid image found at 0x%08lx\n", img_addr);
695 /* read in image data */
696 debug (" Reading image remaining data from dataflash address "
697 "%08lx to RAM address %08lx\n", img_addr + h_size,
700 read_dataflash (img_addr + h_size, d_size,
701 (char *)(ram_addr + h_size));
704 #endif /* CONFIG_HAS_DATAFLASH */
710 * fit_has_config - check if there is a valid FIT configuration
711 * @images: pointer to the bootm command headers structure
713 * fit_has_config() checks if there is a FIT configuration in use
714 * (if FTI support is present).
717 * 0, no FIT support or no configuration found
718 * 1, configuration found
720 int genimg_has_config (bootm_headers_t *images)
722 #if defined(CONFIG_FIT)
723 if (images->fit_uname_cfg)
730 * boot_get_ramdisk - main ramdisk handling routine
731 * @argc: command argument count
732 * @argv: command argument list
733 * @images: pointer to the bootm images structure
734 * @arch: expected ramdisk architecture
735 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
736 * @rd_end: pointer to a ulong variable, will hold ramdisk end
738 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
739 * Curently supported are the following ramdisk sources:
740 * - multicomponent kernel/ramdisk image,
741 * - commandline provided address of decicated ramdisk image.
744 * 0, if ramdisk image was found and valid, or skiped
745 * rd_start and rd_end are set to ramdisk start/end addresses if
746 * ramdisk image is found and valid
748 * 1, if ramdisk image is found but corrupted
749 * rd_start and rd_end are set to 0 if no ramdisk exists
751 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
752 uint8_t arch, ulong *rd_start, ulong *rd_end)
754 ulong rd_addr, rd_load;
755 ulong rd_data, rd_len;
756 image_header_t *rd_hdr;
757 #if defined(CONFIG_FIT)
759 const char *fit_uname_config = NULL;
760 const char *fit_uname_ramdisk = NULL;
772 * Look for a '-' which indicates to ignore the
775 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
776 debug ("## Skipping init Ramdisk\n");
777 rd_len = rd_data = 0;
778 } else if (argc >= 3 || genimg_has_config (images)) {
779 #if defined(CONFIG_FIT)
782 * If the init ramdisk comes from the FIT image and
783 * the FIT image address is omitted in the command
784 * line argument, try to use os FIT image address or
785 * default load address.
787 if (images->fit_uname_os)
788 default_addr = (ulong)images->fit_hdr_os;
790 default_addr = load_addr;
792 if (fit_parse_conf (argv[2], default_addr,
793 &rd_addr, &fit_uname_config)) {
794 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
795 fit_uname_config, rd_addr);
796 } else if (fit_parse_subimage (argv[2], default_addr,
797 &rd_addr, &fit_uname_ramdisk)) {
798 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
799 fit_uname_ramdisk, rd_addr);
803 rd_addr = simple_strtoul(argv[2], NULL, 16);
804 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
807 #if defined(CONFIG_FIT)
809 /* use FIT configuration provided in first bootm
812 rd_addr = (ulong)images->fit_hdr_os;
813 fit_uname_config = images->fit_uname_cfg;
814 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
815 fit_uname_config, rd_addr);
818 * Check whether configuration has ramdisk defined,
819 * if not, don't try to use it, quit silently.
821 fit_hdr = (void *)rd_addr;
822 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
823 if (cfg_noffset < 0) {
824 debug ("* ramdisk: no such config\n");
828 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
829 if (rd_noffset < 0) {
830 debug ("* ramdisk: no ramdisk in config\n");
836 /* copy from dataflash if needed */
837 rd_addr = genimg_get_image (rd_addr);
840 * Check if there is an initrd image at the
841 * address provided in the second bootm argument
842 * check image type, for FIT images get FIT node.
844 switch (genimg_get_format ((void *)rd_addr)) {
845 case IMAGE_FORMAT_LEGACY:
846 printf ("## Loading init Ramdisk from Legacy "
847 "Image at %08lx ...\n", rd_addr);
849 show_boot_progress (9);
850 rd_hdr = image_get_ramdisk (rd_addr, arch,
856 rd_data = image_get_data (rd_hdr);
857 rd_len = image_get_data_size (rd_hdr);
858 rd_load = image_get_load (rd_hdr);
860 #if defined(CONFIG_FIT)
861 case IMAGE_FORMAT_FIT:
862 fit_hdr = (void *)rd_addr;
863 printf ("## Loading init Ramdisk from FIT "
864 "Image at %08lx ...\n", rd_addr);
866 show_boot_progress (120);
867 if (!fit_check_format (fit_hdr)) {
868 puts ("Bad FIT ramdisk image format!\n");
869 show_boot_progress (-120);
872 show_boot_progress (121);
874 if (!fit_uname_ramdisk) {
876 * no ramdisk image node unit name, try to get config
877 * node first. If config unit node name is NULL
878 * fit_conf_get_node() will try to find default config node
880 show_boot_progress (122);
881 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
882 if (cfg_noffset < 0) {
883 puts ("Could not find configuration node\n");
884 show_boot_progress (-122);
887 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
888 printf (" Using '%s' configuration\n", fit_uname_config);
890 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
891 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
893 /* get ramdisk component image node offset */
894 show_boot_progress (123);
895 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
897 if (rd_noffset < 0) {
898 puts ("Could not find subimage node\n");
899 show_boot_progress (-124);
903 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
905 show_boot_progress (125);
906 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
909 /* get ramdisk image data address and length */
910 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
911 puts ("Could not find ramdisk subimage data!\n");
912 show_boot_progress (-127);
915 show_boot_progress (128);
917 rd_data = (ulong)data;
920 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
921 puts ("Can't get ramdisk subimage load address!\n");
922 show_boot_progress (-129);
925 show_boot_progress (129);
927 images->fit_hdr_rd = fit_hdr;
928 images->fit_uname_rd = fit_uname_ramdisk;
929 images->fit_noffset_rd = rd_noffset;
933 puts ("Wrong Ramdisk Image Format\n");
934 rd_data = rd_len = rd_load = 0;
937 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
939 * We need to copy the ramdisk to SRAM to let Linux boot
942 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
945 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
947 } else if (images->legacy_hdr_valid &&
948 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
950 * Now check if we have a legacy mult-component image,
951 * get second entry data start address and len.
953 show_boot_progress (13);
954 printf ("## Loading init Ramdisk from multi component "
955 "Legacy Image at %08lx ...\n",
956 (ulong)images->legacy_hdr_os);
958 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
963 show_boot_progress (14);
964 rd_len = rd_data = 0;
968 debug ("## No init Ramdisk\n");
971 *rd_end = rd_data + rd_len;
973 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
979 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
981 * boot_ramdisk_high - relocate init ramdisk
982 * @lmb: pointer to lmb handle, will be used for memory mgmt
983 * @rd_data: ramdisk data start address
984 * @rd_len: ramdisk data length
985 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
986 * start address (after possible relocation)
987 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
988 * end address (after possible relocation)
990 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
991 * variable and if requested ramdisk data is moved to a specified location.
993 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
994 * start/end addresses if ramdisk image start and len were provided,
995 * otherwise set initrd_start and initrd_end set to zeros.
1001 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1002 ulong *initrd_start, ulong *initrd_end)
1006 int initrd_copy_to_ram = 1;
1008 if ((s = getenv ("initrd_high")) != NULL) {
1009 /* a value of "no" or a similar string will act like 0,
1010 * turning the "load high" feature off. This is intentional.
1012 initrd_high = simple_strtoul (s, NULL, 16);
1013 if (initrd_high == ~0)
1014 initrd_copy_to_ram = 0;
1016 /* not set, no restrictions to load high */
1021 #ifdef CONFIG_LOGBUFFER
1022 /* Prevent initrd from overwriting logbuffer */
1023 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1026 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1027 initrd_high, initrd_copy_to_ram);
1030 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1031 debug (" in-place initrd\n");
1032 *initrd_start = rd_data;
1033 *initrd_end = rd_data + rd_len;
1034 lmb_reserve(lmb, rd_data, rd_len);
1037 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1039 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
1041 if (*initrd_start == 0) {
1042 puts ("ramdisk - allocation error\n");
1045 show_boot_progress (12);
1047 *initrd_end = *initrd_start + rd_len;
1048 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1049 *initrd_start, *initrd_end);
1051 memmove_wd ((void *)*initrd_start,
1052 (void *)rd_data, rd_len, CHUNKSZ);
1060 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1061 *initrd_start, *initrd_end);
1070 * boot_get_cmdline - allocate and initialize kernel cmdline
1071 * @lmb: pointer to lmb handle, will be used for memory mgmt
1072 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1073 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1074 * @bootmap_base: ulong variable, holds offset in physical memory to
1077 * boot_get_cmdline() allocates space for kernel command line below
1078 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1079 * variable is present its contents is copied to allocated kernel
1086 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1092 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1093 CFG_BOOTMAPSZ + bootmap_base);
1095 if (cmdline == NULL)
1098 if ((s = getenv("bootargs")) == NULL)
1103 *cmd_start = (ulong) & cmdline[0];
1104 *cmd_end = *cmd_start + strlen(cmdline);
1106 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1112 * boot_get_kbd - allocate and initialize kernel copy of board info
1113 * @lmb: pointer to lmb handle, will be used for memory mgmt
1114 * @kbd: double pointer to board info data
1115 * @bootmap_base: ulong variable, holds offset in physical memory to
1118 * boot_get_kbd() allocates space for kernel copy of board info data below
1119 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1120 * the current u-boot board info data.
1126 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1128 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1129 CFG_BOOTMAPSZ + bootmap_base);
1135 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1137 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1138 do_bdinfo(NULL, 0, 0, NULL);
1143 #endif /* CONFIG_PPC || CONFIG_M68K */
1144 #endif /* !USE_HOSTCC */
1146 #if defined(CONFIG_FIT)
1147 /*****************************************************************************/
1148 /* New uImage format routines */
1149 /*****************************************************************************/
1151 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1152 ulong *addr, const char **name)
1159 sep = strchr (spec, sepc);
1162 *addr = simple_strtoul (spec, NULL, 16);
1172 * fit_parse_conf - parse FIT configuration spec
1173 * @spec: input string, containing configuration spec
1174 * @add_curr: current image address (to be used as a possible default)
1175 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1177 * @conf_name double pointer to a char, will hold pointer to a configuration
1180 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1181 * where <addr> is a FIT image address that contains configuration
1182 * with a <conf> unit name.
1184 * Address part is optional, and if omitted default add_curr will
1188 * 1 if spec is a valid configuration string,
1189 * addr and conf_name are set accordingly
1192 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1193 ulong *addr, const char **conf_name)
1195 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1199 * fit_parse_subimage - parse FIT subimage spec
1200 * @spec: input string, containing subimage spec
1201 * @add_curr: current image address (to be used as a possible default)
1202 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1204 * @image_name: double pointer to a char, will hold pointer to a subimage name
1206 * fit_parse_subimage() expects subimage spec in the for of
1207 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1208 * subimage with a <subimg> unit name.
1210 * Address part is optional, and if omitted default add_curr will
1214 * 1 if spec is a valid subimage string,
1215 * addr and image_name are set accordingly
1218 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1219 ulong *addr, const char **image_name)
1221 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1223 #endif /* !USE_HOSTCC */
1225 static void fit_get_debug (const void *fit, int noffset,
1226 char *prop_name, int err)
1228 debug ("Can't get '%s' property from FIT 0x%08lx, "
1229 "node: offset %d, name %s (%s)\n",
1230 prop_name, (ulong)fit, noffset,
1231 fit_get_name (fit, noffset, NULL),
1232 fdt_strerror (err));
1236 * fit_print_contents - prints out the contents of the FIT format image
1237 * @fit: pointer to the FIT format image header
1238 * @p: pointer to prefix string
1240 * fit_print_contents() formats a multi line FIT image contents description.
1241 * The routine prints out FIT image properties (root node level) follwed by
1242 * the details of each component image.
1245 * no returned results
1247 void fit_print_contents (const void *fit)
1258 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1268 /* Root node properties */
1269 ret = fit_get_desc (fit, 0, &desc);
1270 printf ("%sFIT description: ", p);
1272 printf ("unavailable\n");
1274 printf ("%s\n", desc);
1276 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1277 ret = fit_get_timestamp (fit, 0, ×tamp);
1278 printf ("%sCreated: ", p);
1280 printf ("unavailable\n");
1282 genimg_print_time (timestamp);
1285 /* Find images parent node offset */
1286 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1287 if (images_noffset < 0) {
1288 printf ("Can't find images parent node '%s' (%s)\n",
1289 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1293 /* Process its subnodes, print out component images details */
1294 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1295 (noffset >= 0) && (ndepth > 0);
1296 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1299 * Direct child node of the images parent node,
1300 * i.e. component image node.
1302 printf ("%s Image %u (%s)\n", p, count++,
1303 fit_get_name(fit, noffset, NULL));
1305 fit_image_print (fit, noffset, p);
1309 /* Find configurations parent node offset */
1310 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1311 if (confs_noffset < 0) {
1312 debug ("Can't get configurations parent node '%s' (%s)\n",
1313 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1317 /* get default configuration unit name from default property */
1318 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1320 printf ("%s Default Configuration: '%s'\n", p, uname);
1322 /* Process its subnodes, print out configurations details */
1323 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1324 (noffset >= 0) && (ndepth > 0);
1325 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1328 * Direct child node of the configurations parent node,
1329 * i.e. configuration node.
1331 printf ("%s Configuration %u (%s)\n", p, count++,
1332 fit_get_name(fit, noffset, NULL));
1334 fit_conf_print (fit, noffset, p);
1340 * fit_image_print - prints out the FIT component image details
1341 * @fit: pointer to the FIT format image header
1342 * @image_noffset: offset of the component image node
1343 * @p: pointer to prefix string
1345 * fit_image_print() lists all mandatory properies for the processed component
1346 * image. If present, hash nodes are printed out as well.
1349 * no returned results
1351 void fit_image_print (const void *fit, int image_noffset, const char *p)
1354 uint8_t type, arch, os, comp;
1362 /* Mandatory properties */
1363 ret = fit_get_desc (fit, image_noffset, &desc);
1364 printf ("%s Description: ", p);
1366 printf ("unavailable\n");
1368 printf ("%s\n", desc);
1370 fit_image_get_type (fit, image_noffset, &type);
1371 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1373 fit_image_get_comp (fit, image_noffset, &comp);
1374 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1376 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1379 printf ("%s Data Start: ", p);
1381 printf ("unavailable\n");
1383 printf ("0x%08lx\n", (ulong)data);
1386 printf ("%s Data Size: ", p);
1388 printf ("unavailable\n");
1390 genimg_print_size (size);
1392 /* Remaining, type dependent properties */
1393 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1394 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1395 (type == IH_TYPE_FLATDT)) {
1396 fit_image_get_arch (fit, image_noffset, &arch);
1397 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1400 if (type == IH_TYPE_KERNEL) {
1401 fit_image_get_os (fit, image_noffset, &os);
1402 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1405 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1406 ret = fit_image_get_load (fit, image_noffset, &load);
1407 printf ("%s Load Address: ", p);
1409 printf ("unavailable\n");
1411 printf ("0x%08lx\n", load);
1413 fit_image_get_entry (fit, image_noffset, &entry);
1414 printf ("%s Entry Point: ", p);
1416 printf ("unavailable\n");
1418 printf ("0x%08lx\n", entry);
1421 /* Process all hash subnodes of the component image node */
1422 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1423 (noffset >= 0) && (ndepth > 0);
1424 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1426 /* Direct child node of the component image node */
1427 fit_image_print_hash (fit, noffset, p);
1433 * fit_image_print_hash - prints out the hash node details
1434 * @fit: pointer to the FIT format image header
1435 * @noffset: offset of the hash node
1436 * @p: pointer to prefix string
1438 * fit_image_print_hash() lists properies for the processed hash node
1441 * no returned results
1443 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1451 * Check subnode name, must be equal to "hash".
1452 * Multiple hash nodes require unique unit node
1453 * names, e.g. hash@1, hash@2, etc.
1455 if (strncmp (fit_get_name(fit, noffset, NULL),
1457 strlen(FIT_HASH_NODENAME)) != 0)
1460 debug ("%s Hash node: '%s'\n", p,
1461 fit_get_name (fit, noffset, NULL));
1463 printf ("%s Hash algo: ", p);
1464 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1465 printf ("invalid/unsupported\n");
1468 printf ("%s\n", algo);
1470 ret = fit_image_hash_get_value (fit, noffset, &value,
1472 printf ("%s Hash value: ", p);
1474 printf ("unavailable\n");
1476 for (i = 0; i < value_len; i++)
1477 printf ("%02x", value[i]);
1481 debug ("%s Hash len: %d\n", p, value_len);
1485 * fit_get_desc - get node description property
1486 * @fit: pointer to the FIT format image header
1487 * @noffset: node offset
1488 * @desc: double pointer to the char, will hold pointer to the descrption
1490 * fit_get_desc() reads description property from a given node, if
1491 * description is found pointer to it is returened in third call argument.
1497 int fit_get_desc (const void *fit, int noffset, char **desc)
1501 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1502 if (*desc == NULL) {
1503 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1511 * fit_get_timestamp - get node timestamp property
1512 * @fit: pointer to the FIT format image header
1513 * @noffset: node offset
1514 * @timestamp: pointer to the time_t, will hold read timestamp
1516 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1517 * is found and has a correct size its value is retured in third call
1522 * -1, on property read failure
1523 * -2, on wrong timestamp size
1525 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1530 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1532 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1535 if (len != sizeof (uint32_t)) {
1536 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1540 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1545 * fit_image_get_node - get node offset for component image of a given unit name
1546 * @fit: pointer to the FIT format image header
1547 * @image_uname: component image node unit name
1549 * fit_image_get_node() finds a component image (withing the '/images'
1550 * node) of a provided unit name. If image is found its node offset is
1551 * returned to the caller.
1554 * image node offset when found (>=0)
1555 * negative number on failure (FDT_ERR_* code)
1557 int fit_image_get_node (const void *fit, const char *image_uname)
1559 int noffset, images_noffset;
1561 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1562 if (images_noffset < 0) {
1563 debug ("Can't find images parent node '%s' (%s)\n",
1564 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1565 return images_noffset;
1568 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1570 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1571 image_uname, fdt_strerror (noffset));
1578 * fit_image_get_os - get os id for a given component image node
1579 * @fit: pointer to the FIT format image header
1580 * @noffset: component image node offset
1581 * @os: pointer to the uint8_t, will hold os numeric id
1583 * fit_image_get_os() finds os property in a given component image node.
1584 * If the property is found, its (string) value is translated to the numeric
1585 * id which is returned to the caller.
1591 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1596 /* Get OS name from property data */
1597 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1599 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1604 /* Translate OS name to id */
1605 *os = genimg_get_os_id (data);
1610 * fit_image_get_arch - get arch id for a given component image node
1611 * @fit: pointer to the FIT format image header
1612 * @noffset: component image node offset
1613 * @arch: pointer to the uint8_t, will hold arch numeric id
1615 * fit_image_get_arch() finds arch property in a given component image node.
1616 * If the property is found, its (string) value is translated to the numeric
1617 * id which is returned to the caller.
1623 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1628 /* Get architecture name from property data */
1629 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1631 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1636 /* Translate architecture name to id */
1637 *arch = genimg_get_arch_id (data);
1642 * fit_image_get_type - get type id for a given component image node
1643 * @fit: pointer to the FIT format image header
1644 * @noffset: component image node offset
1645 * @type: pointer to the uint8_t, will hold type numeric id
1647 * fit_image_get_type() finds type property in a given component image node.
1648 * If the property is found, its (string) value is translated to the numeric
1649 * id which is returned to the caller.
1655 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1660 /* Get image type name from property data */
1661 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1663 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1668 /* Translate image type name to id */
1669 *type = genimg_get_type_id (data);
1674 * fit_image_get_comp - get comp id for a given component image node
1675 * @fit: pointer to the FIT format image header
1676 * @noffset: component image node offset
1677 * @comp: pointer to the uint8_t, will hold comp numeric id
1679 * fit_image_get_comp() finds comp property in a given component image node.
1680 * If the property is found, its (string) value is translated to the numeric
1681 * id which is returned to the caller.
1687 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1692 /* Get compression name from property data */
1693 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1695 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1700 /* Translate compression name to id */
1701 *comp = genimg_get_comp_id (data);
1706 * fit_image_get_load - get load address property for a given component image node
1707 * @fit: pointer to the FIT format image header
1708 * @noffset: component image node offset
1709 * @load: pointer to the uint32_t, will hold load address
1711 * fit_image_get_load() finds load address property in a given component image node.
1712 * If the property is found, its value is returned to the caller.
1718 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1721 const uint32_t *data;
1723 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1725 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1729 *load = uimage_to_cpu (*data);
1734 * fit_image_get_entry - get entry point address property for a given component image node
1735 * @fit: pointer to the FIT format image header
1736 * @noffset: component image node offset
1737 * @entry: pointer to the uint32_t, will hold entry point address
1739 * fit_image_get_entry() finds entry point address property in a given component image node.
1740 * If the property is found, its value is returned to the caller.
1746 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1749 const uint32_t *data;
1751 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1753 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1757 *entry = uimage_to_cpu (*data);
1762 * fit_image_get_data - get data property and its size for a given component image node
1763 * @fit: pointer to the FIT format image header
1764 * @noffset: component image node offset
1765 * @data: double pointer to void, will hold data property's data address
1766 * @size: pointer to size_t, will hold data property's data size
1768 * fit_image_get_data() finds data property in a given component image node.
1769 * If the property is found its data start address and size are returned to
1776 int fit_image_get_data (const void *fit, int noffset,
1777 const void **data, size_t *size)
1781 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1782 if (*data == NULL) {
1783 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1793 * fit_image_hash_get_algo - get hash algorithm name
1794 * @fit: pointer to the FIT format image header
1795 * @noffset: hash node offset
1796 * @algo: double pointer to char, will hold pointer to the algorithm name
1798 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1799 * If the property is found its data start address is returned to the caller.
1805 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1809 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1810 if (*algo == NULL) {
1811 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1819 * fit_image_hash_get_value - get hash value and length
1820 * @fit: pointer to the FIT format image header
1821 * @noffset: hash node offset
1822 * @value: double pointer to uint8_t, will hold address of a hash value data
1823 * @value_len: pointer to an int, will hold hash data length
1825 * fit_image_hash_get_value() finds hash value property in a given hash node.
1826 * If the property is found its data start address and size are returned to
1833 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1838 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1839 if (*value == NULL) {
1840 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1850 * fit_set_timestamp - set node timestamp property
1851 * @fit: pointer to the FIT format image header
1852 * @noffset: node offset
1853 * @timestamp: timestamp value to be set
1855 * fit_set_timestamp() attempts to set timestamp property in the requested
1856 * node and returns operation status to the caller.
1860 * -1, on property read failure
1862 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1867 t = cpu_to_uimage (timestamp);
1868 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1871 printf ("Can't set '%s' property for '%s' node (%s)\n",
1872 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1873 fdt_strerror (ret));
1881 * calculate_hash - calculate and return hash for provided input data
1882 * @data: pointer to the input data
1883 * @data_len: data length
1884 * @algo: requested hash algorithm
1885 * @value: pointer to the char, will hold hash value data (caller must
1886 * allocate enough free space)
1887 * value_len: length of the calculated hash
1889 * calculate_hash() computes input data hash according to the requested algorithm.
1890 * Resulting hash value is placed in caller provided 'value' buffer, length
1891 * of the calculated hash is returned via value_len pointer argument.
1895 * -1, when algo is unsupported
1897 static int calculate_hash (const void *data, int data_len, const char *algo,
1898 uint8_t *value, int *value_len)
1900 if (strcmp (algo, "crc32") == 0 ) {
1901 *((uint32_t *)value) = crc32_wd (0, data, data_len,
1903 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1905 } else if (strcmp (algo, "sha1") == 0 ) {
1906 sha1_csum_wd ((unsigned char *) data, data_len,
1907 (unsigned char *) value, CHUNKSZ_SHA1);
1909 } else if (strcmp (algo, "md5") == 0 ) {
1910 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1913 debug ("Unsupported hash alogrithm\n");
1921 * fit_set_hashes - process FIT component image nodes and calculate hashes
1922 * @fit: pointer to the FIT format image header
1924 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1925 * Hashes are calculated for all component images which have hash subnodes
1926 * with algorithm property set to one of the supported hash algorithms.
1930 * libfdt error code, on failure
1932 int fit_set_hashes (void *fit)
1939 /* Find images parent node offset */
1940 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1941 if (images_noffset < 0) {
1942 printf ("Can't find images parent node '%s' (%s)\n",
1943 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1944 return images_noffset;
1947 /* Process its subnodes, print out component images details */
1948 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1949 (noffset >= 0) && (ndepth > 0);
1950 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1953 * Direct child node of the images parent node,
1954 * i.e. component image node.
1956 ret = fit_image_set_hashes (fit, noffset);
1966 * fit_image_set_hashes - calculate/set hashes for given component image node
1967 * @fit: pointer to the FIT format image header
1968 * @image_noffset: requested component image node
1970 * fit_image_set_hashes() adds hash values for an component image node. All
1971 * existing hash subnodes are checked, if algorithm property is set to one of
1972 * the supported hash algorithms, hash value is computed and corresponding
1973 * hash node property is set, for example:
1975 * Input component image node structure:
1977 * o image@1 (at image_noffset)
1978 * | - data = [binary data]
1982 * Output component image node structure:
1984 * o image@1 (at image_noffset)
1985 * | - data = [binary data]
1988 * |- value = sha1(data)
1994 int fit_image_set_hashes (void *fit, int image_noffset)
1999 uint8_t value[FIT_MAX_HASH_LEN];
2004 /* Get image data and data length */
2005 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2006 printf ("Can't get image data/size\n");
2010 /* Process all hash subnodes of the component image node */
2011 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2012 (noffset >= 0) && (ndepth > 0);
2013 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2015 /* Direct child node of the component image node */
2018 * Check subnode name, must be equal to "hash".
2019 * Multiple hash nodes require unique unit node
2020 * names, e.g. hash@1, hash@2, etc.
2022 if (strncmp (fit_get_name(fit, noffset, NULL),
2024 strlen(FIT_HASH_NODENAME)) != 0) {
2025 /* Not a hash subnode, skip it */
2029 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2030 printf ("Can't get hash algo property for "
2031 "'%s' hash node in '%s' image node\n",
2032 fit_get_name (fit, noffset, NULL),
2033 fit_get_name (fit, image_noffset, NULL));
2037 if (calculate_hash (data, size, algo, value, &value_len)) {
2038 printf ("Unsupported hash algorithm (%s) for "
2039 "'%s' hash node in '%s' image node\n",
2040 algo, fit_get_name (fit, noffset, NULL),
2041 fit_get_name (fit, image_noffset, NULL));
2045 if (fit_image_hash_set_value (fit, noffset, value,
2047 printf ("Can't set hash value for "
2048 "'%s' hash node in '%s' image node\n",
2049 fit_get_name (fit, noffset, NULL),
2050 fit_get_name (fit, image_noffset, NULL));
2060 * fit_image_hash_set_value - set hash value in requested has node
2061 * @fit: pointer to the FIT format image header
2062 * @noffset: hash node offset
2063 * @value: hash value to be set
2064 * @value_len: hash value length
2066 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2067 * given and returns operation status to the caller.
2073 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2078 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2080 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2081 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2082 fdt_strerror (ret));
2088 #endif /* USE_HOSTCC */
2091 * fit_image_check_hashes - verify data intergity
2092 * @fit: pointer to the FIT format image header
2093 * @image_noffset: component image node offset
2095 * fit_image_check_hashes() goes over component image hash nodes,
2096 * re-calculates each data hash and compares with the value stored in hash
2100 * 1, if all hashes are valid
2101 * 0, otherwise (or on error)
2103 int fit_image_check_hashes (const void *fit, int image_noffset)
2110 uint8_t value[FIT_MAX_HASH_LEN];
2116 /* Get image data and data length */
2117 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2118 printf ("Can't get image data/size\n");
2122 /* Process all hash subnodes of the component image node */
2123 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2124 (noffset >= 0) && (ndepth > 0);
2125 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2127 /* Direct child node of the component image node */
2130 * Check subnode name, must be equal to "hash".
2131 * Multiple hash nodes require unique unit node
2132 * names, e.g. hash@1, hash@2, etc.
2134 if (strncmp (fit_get_name(fit, noffset, NULL),
2136 strlen(FIT_HASH_NODENAME)) != 0)
2139 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2140 err_msg = "Can't get hash algo property";
2143 printf ("%s", algo);
2145 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2147 err_msg = "Can't get hash value property";
2151 if (calculate_hash (data, size, algo, value, &value_len)) {
2152 err_msg = "Unsupported hash algorithm";
2156 if (value_len != fit_value_len) {
2157 err_msg = "Bad hash value len";
2159 } else if (memcmp (value, fit_value, value_len) != 0) {
2160 err_msg = "Bad hash value";
2170 printf ("%s for '%s' hash node in '%s' image node\n",
2171 err_msg, fit_get_name (fit, noffset, NULL),
2172 fit_get_name (fit, image_noffset, NULL));
2177 * fit_image_check_os - check whether image node is of a given os type
2178 * @fit: pointer to the FIT format image header
2179 * @noffset: component image node offset
2180 * @os: requested image os
2182 * fit_image_check_os() reads image os property and compares its numeric
2183 * id with the requested os. Comparison result is returned to the caller.
2186 * 1 if image is of given os type
2187 * 0 otherwise (or on error)
2189 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2193 if (fit_image_get_os (fit, noffset, &image_os))
2195 return (os == image_os);
2199 * fit_image_check_arch - check whether image node is of a given arch
2200 * @fit: pointer to the FIT format image header
2201 * @noffset: component image node offset
2202 * @arch: requested imagearch
2204 * fit_image_check_arch() reads image arch property and compares its numeric
2205 * id with the requested arch. Comparison result is returned to the caller.
2208 * 1 if image is of given arch
2209 * 0 otherwise (or on error)
2211 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2215 if (fit_image_get_arch (fit, noffset, &image_arch))
2217 return (arch == image_arch);
2221 * fit_image_check_type - check whether image node is of a given type
2222 * @fit: pointer to the FIT format image header
2223 * @noffset: component image node offset
2224 * @type: requested image type
2226 * fit_image_check_type() reads image type property and compares its numeric
2227 * id with the requested type. Comparison result is returned to the caller.
2230 * 1 if image is of given type
2231 * 0 otherwise (or on error)
2233 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2237 if (fit_image_get_type (fit, noffset, &image_type))
2239 return (type == image_type);
2243 * fit_image_check_comp - check whether image node uses given compression
2244 * @fit: pointer to the FIT format image header
2245 * @noffset: component image node offset
2246 * @comp: requested image compression type
2248 * fit_image_check_comp() reads image compression property and compares its
2249 * numeric id with the requested compression type. Comparison result is
2250 * returned to the caller.
2253 * 1 if image uses requested compression
2254 * 0 otherwise (or on error)
2256 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2260 if (fit_image_get_comp (fit, noffset, &image_comp))
2262 return (comp == image_comp);
2266 * fit_check_format - sanity check FIT image format
2267 * @fit: pointer to the FIT format image header
2269 * fit_check_format() runs a basic sanity FIT image verification.
2270 * Routine checks for mandatory properties, nodes, etc.
2276 int fit_check_format (const void *fit)
2278 /* mandatory / node 'description' property */
2279 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2280 debug ("Wrong FIT format: no description\n");
2284 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2285 /* mandatory / node 'timestamp' property */
2286 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2287 debug ("Wrong FIT format: no description\n");
2292 /* mandatory subimages parent '/images' node */
2293 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2294 debug ("Wrong FIT format: no images parent node\n");
2302 * fit_conf_get_node - get node offset for configuration of a given unit name
2303 * @fit: pointer to the FIT format image header
2304 * @conf_uname: configuration node unit name
2306 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2307 * parant node) of a provided unit name. If configuration is found its node offset
2308 * is returned to the caller.
2310 * When NULL is provided in second argument fit_conf_get_node() will search
2311 * for a default configuration node instead. Default configuration node unit name
2312 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2315 * configuration node offset when found (>=0)
2316 * negative number on failure (FDT_ERR_* code)
2318 int fit_conf_get_node (const void *fit, const char *conf_uname)
2320 int noffset, confs_noffset;
2323 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2324 if (confs_noffset < 0) {
2325 debug ("Can't find configurations parent node '%s' (%s)\n",
2326 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2327 return confs_noffset;
2330 if (conf_uname == NULL) {
2331 /* get configuration unit name from the default property */
2332 debug ("No configuration specified, trying default...\n");
2333 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2334 if (conf_uname == NULL) {
2335 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2338 debug ("Found default configuration: '%s'\n", conf_uname);
2341 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2343 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2344 conf_uname, fdt_strerror (noffset));
2350 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2351 const char *prop_name)
2356 /* get kernel image unit name from configuration kernel property */
2357 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2361 return fit_image_get_node (fit, uname);
2365 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2366 * a given configuration
2367 * @fit: pointer to the FIT format image header
2368 * @noffset: configuration node offset
2370 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2371 * configuration FIT_KERNEL_PROP property and translates it to the node
2375 * image node offset when found (>=0)
2376 * negative number on failure (FDT_ERR_* code)
2378 int fit_conf_get_kernel_node (const void *fit, int noffset)
2380 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2384 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2385 * a given configuration
2386 * @fit: pointer to the FIT format image header
2387 * @noffset: configuration node offset
2389 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2390 * configuration FIT_KERNEL_PROP property and translates it to the node
2394 * image node offset when found (>=0)
2395 * negative number on failure (FDT_ERR_* code)
2397 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2399 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2403 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2404 * a given configuration
2405 * @fit: pointer to the FIT format image header
2406 * @noffset: configuration node offset
2408 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2409 * configuration FIT_KERNEL_PROP property and translates it to the node
2413 * image node offset when found (>=0)
2414 * negative number on failure (FDT_ERR_* code)
2416 int fit_conf_get_fdt_node (const void *fit, int noffset)
2418 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2422 * fit_conf_print - prints out the FIT configuration details
2423 * @fit: pointer to the FIT format image header
2424 * @noffset: offset of the configuration node
2425 * @p: pointer to prefix string
2427 * fit_conf_print() lists all mandatory properies for the processed
2428 * configuration node.
2431 * no returned results
2433 void fit_conf_print (const void *fit, int noffset, const char *p)
2439 /* Mandatory properties */
2440 ret = fit_get_desc (fit, noffset, &desc);
2441 printf ("%s Description: ", p);
2443 printf ("unavailable\n");
2445 printf ("%s\n", desc);
2447 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2448 printf ("%s Kernel: ", p);
2450 printf ("unavailable\n");
2452 printf ("%s\n", uname);
2454 /* Optional properties */
2455 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2457 printf ("%s Init Ramdisk: %s\n", p, uname);
2459 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2461 printf ("%s FDT: %s\n", p, uname);
2465 * fit_check_ramdisk - verify FIT format ramdisk subimage
2466 * @fit_hdr: pointer to the FIT ramdisk header
2467 * @rd_noffset: ramdisk subimage node offset within FIT image
2468 * @arch: requested ramdisk image architecture type
2469 * @verify: data CRC verification flag
2471 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2472 * specified FIT image.
2479 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2481 fit_image_print (fit, rd_noffset, " ");
2484 puts (" Verifying Hash Integrity ... ");
2485 if (!fit_image_check_hashes (fit, rd_noffset)) {
2486 puts ("Bad Data Hash\n");
2487 show_boot_progress (-125);
2493 show_boot_progress (126);
2494 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2495 !fit_image_check_arch (fit, rd_noffset, arch) ||
2496 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2497 printf ("No Linux %s Ramdisk Image\n",
2498 genimg_get_arch_name(arch));
2499 show_boot_progress (-126);
2503 show_boot_progress (127);
2506 #endif /* USE_HOSTCC */
2507 #endif /* CONFIG_FIT */