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,
32 #ifdef CONFIG_SHOW_BOOT_PROGRESS
33 #include <status_led.h>
36 #ifdef CONFIG_HAS_DATAFLASH
37 #include <dataflash.h>
40 #ifdef CONFIG_LOGBUFFER
44 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
50 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
53 #include <fdt_support.h>
56 #if defined(CONFIG_FIT)
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,
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, "ppc", "PowerPC", },
96 { IH_ARCH_S390, "s390", "IBM S390", },
97 { IH_ARCH_SH, "sh", "SuperH", },
98 { IH_ARCH_SPARC, "sparc", "SPARC", },
99 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
100 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
101 { IH_ARCH_AVR32, "avr32", "AVR32", },
105 static table_entry_t uimage_os[] = {
106 { IH_OS_INVALID, NULL, "Invalid OS", },
107 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
108 { IH_OS_ARTOS, "artos", "ARTOS", },
110 { IH_OS_LINUX, "linux", "Linux", },
111 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
112 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
114 { IH_OS_NETBSD, "netbsd", "NetBSD", },
115 { IH_OS_RTEMS, "rtems", "RTEMS", },
116 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
117 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
118 { IH_OS_QNX, "qnx", "QNX", },
119 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
122 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
123 { IH_OS_DELL, "dell", "Dell", },
124 { IH_OS_ESIX, "esix", "Esix", },
125 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
126 { IH_OS_IRIX, "irix", "Irix", },
127 { IH_OS_NCR, "ncr", "NCR", },
128 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
129 { IH_OS_PSOS, "psos", "pSOS", },
130 { IH_OS_SCO, "sco", "SCO", },
131 { IH_OS_SOLARIS, "solaris", "Solaris", },
132 { IH_OS_SVR4, "svr4", "SVR4", },
137 static table_entry_t uimage_type[] = {
138 { IH_TYPE_INVALID, NULL, "Invalid Image", },
139 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
140 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
141 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
142 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
143 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
144 { IH_TYPE_SCRIPT, "script", "Script", },
145 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
146 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
150 static table_entry_t uimage_comp[] = {
151 { IH_COMP_NONE, "none", "uncompressed", },
152 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
153 { IH_COMP_GZIP, "gzip", "gzip compressed", },
157 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
158 static void genimg_print_size (uint32_t size);
159 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
160 static void genimg_print_time (time_t timestamp);
163 /*****************************************************************************/
164 /* Legacy format routines */
165 /*****************************************************************************/
166 int image_check_hcrc (image_header_t *hdr)
169 ulong len = image_get_header_size ();
170 image_header_t header;
172 /* Copy header so we can blank CRC field for re-calculation */
173 memmove (&header, (char *)hdr, image_get_header_size ());
174 image_set_hcrc (&header, 0);
176 hcrc = crc32 (0, (unsigned char *)&header, len);
178 return (hcrc == image_get_hcrc (hdr));
181 int image_check_dcrc (image_header_t *hdr)
183 ulong data = image_get_data (hdr);
184 ulong len = image_get_data_size (hdr);
185 ulong dcrc = crc32 (0, (unsigned char *)data, len);
187 return (dcrc == image_get_dcrc (hdr));
191 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
194 ulong len = image_get_data_size (hdr);
195 ulong data = image_get_data (hdr);
197 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
199 ulong edata = cdata + len;
201 while (cdata < edata) {
202 ulong chunk = edata - cdata;
206 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
212 dcrc = crc32 (0, (unsigned char *)data, len);
215 return (dcrc == image_get_dcrc (hdr));
217 #endif /* !USE_HOSTCC */
220 * image_multi_count - get component (sub-image) count
221 * @hdr: pointer to the header of the multi component image
223 * image_multi_count() returns number of components in a multi
226 * Note: no checking of the image type is done, caller must pass
227 * a valid multi component image.
230 * number of components
232 ulong image_multi_count (image_header_t *hdr)
237 /* get start of the image payload, which in case of multi
238 * component images that points to a table of component sizes */
239 size = (uint32_t *)image_get_data (hdr);
241 /* count non empty slots */
242 for (i = 0; size[i]; ++i)
249 * image_multi_getimg - get component data address and size
250 * @hdr: pointer to the header of the multi component image
251 * @idx: index of the requested component
252 * @data: pointer to a ulong variable, will hold component data address
253 * @len: pointer to a ulong variable, will hold component size
255 * image_multi_getimg() returns size and data address for the requested
256 * component in a multi component image.
258 * Note: no checking of the image type is done, caller must pass
259 * a valid multi component image.
262 * data address and size of the component, if idx is valid
263 * 0 in data and len, if idx is out of range
265 void image_multi_getimg (image_header_t *hdr, ulong idx,
266 ulong *data, ulong *len)
270 ulong offset, tail, count, img_data;
272 /* get number of component */
273 count = image_multi_count (hdr);
275 /* get start of the image payload, which in case of multi
276 * component images that points to a table of component sizes */
277 size = (uint32_t *)image_get_data (hdr);
279 /* get address of the proper component data start, which means
280 * skipping sizes table (add 1 for last, null entry) */
281 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
284 *len = uimage_to_cpu (size[idx]);
288 /* go over all indices preceding requested component idx */
289 for (i = 0; i < idx; i++) {
290 /* add up i-th component size */
291 offset += uimage_to_cpu (size[i]);
293 /* add up alignment for i-th component */
294 tail += (4 - uimage_to_cpu (size[i]) % 4);
297 /* calculate idx-th component data address */
298 *data = img_data + offset + tail;
305 static void image_print_type (image_header_t *hdr)
307 const char *os, *arch, *type, *comp;
309 os = genimg_get_os_name (image_get_os (hdr));
310 arch = genimg_get_arch_name (image_get_arch (hdr));
311 type = genimg_get_type_name (image_get_type (hdr));
312 comp = genimg_get_comp_name (image_get_comp (hdr));
314 printf ("%s %s %s (%s)\n", arch, os, type, comp);
318 * __image_print_contents - prints out the contents of the legacy format image
319 * @hdr: pointer to the legacy format image header
320 * @p: pointer to prefix string
322 * __image_print_contents() formats a multi line legacy image contents description.
323 * The routine prints out all header fields followed by the size/offset data
324 * for MULTI/SCRIPT images.
327 * no returned results
329 static void __image_print_contents (image_header_t *hdr, const char *p)
331 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
332 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
333 printf ("%sCreated: ", p);
334 genimg_print_time ((time_t)image_get_time (hdr));
336 printf ("%sImage Type: ", p);
337 image_print_type (hdr);
338 printf ("%sData Size: ", p);
339 genimg_print_size (image_get_data_size (hdr));
340 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
341 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
343 if (image_check_type (hdr, IH_TYPE_MULTI) ||
344 image_check_type (hdr, IH_TYPE_SCRIPT)) {
347 ulong count = image_multi_count (hdr);
349 printf ("%sContents:\n", p);
350 for (i = 0; i < count; i++) {
351 image_multi_getimg (hdr, i, &data, &len);
353 printf ("%s Image %d: ", p, i);
354 genimg_print_size (len);
356 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
358 * the user may need to know offsets
359 * if planning to do something with
362 printf ("%s Offset = 0x%08lx\n", p, data);
368 inline void image_print_contents (image_header_t *hdr)
370 __image_print_contents (hdr, " ");
373 inline void image_print_contents_noindent (image_header_t *hdr)
375 __image_print_contents (hdr, "");
380 * image_get_ramdisk - get and verify ramdisk image
381 * @rd_addr: ramdisk image start address
382 * @arch: expected ramdisk architecture
383 * @verify: checksum verification flag
385 * image_get_ramdisk() returns a pointer to the verified ramdisk image
386 * header. Routine receives image start address and expected architecture
387 * flag. Verification done covers data and header integrity and os/type/arch
390 * If dataflash support is enabled routine checks for dataflash addresses
391 * and handles required dataflash reads.
394 * pointer to a ramdisk image header, if image was found and valid
395 * otherwise, return NULL
397 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
400 image_header_t *rd_hdr = (image_header_t *)rd_addr;
402 if (!image_check_magic (rd_hdr)) {
403 puts ("Bad Magic Number\n");
404 show_boot_progress (-10);
408 if (!image_check_hcrc (rd_hdr)) {
409 puts ("Bad Header Checksum\n");
410 show_boot_progress (-11);
414 show_boot_progress (10);
415 image_print_contents (rd_hdr);
418 puts(" Verifying Checksum ... ");
419 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
420 puts ("Bad Data CRC\n");
421 show_boot_progress (-12);
427 show_boot_progress (11);
429 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
430 !image_check_arch (rd_hdr, arch) ||
431 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
432 printf ("No Linux %s Ramdisk Image\n",
433 genimg_get_arch_name(arch));
434 show_boot_progress (-13);
440 #endif /* !USE_HOSTCC */
442 /*****************************************************************************/
443 /* Shared dual-format routines */
444 /*****************************************************************************/
446 int getenv_verify (void)
448 char *s = getenv ("verify");
449 return (s && (*s == 'n')) ? 0 : 1;
452 int getenv_autostart (void)
454 char *s = getenv ("autostart");
455 return (s && (*s == 'n')) ? 0 : 1;
458 ulong getenv_bootm_low(void)
460 char *s = getenv ("bootm_low");
462 ulong tmp = simple_strtoul (s, NULL, 16);
466 #ifdef CFG_SDRAM_BASE
467 return CFG_SDRAM_BASE;
473 ulong getenv_bootm_size(void)
475 char *s = getenv ("bootm_size");
477 ulong tmp = simple_strtoul (s, NULL, 16);
481 return gd->bd->bi_memsize;
484 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
486 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
488 size_t tail = (len > chunksz) ? chunksz : len;
490 memmove (to, from, tail);
495 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
496 memmove (to, from, len);
497 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
499 #endif /* !USE_HOSTCC */
501 static void genimg_print_size (uint32_t size)
504 printf ("%d Bytes = ", size);
505 print_size (size, "\n");
507 printf ("%d Bytes = %.2f kB = %.2f MB\n",
508 size, (double)size / 1.024e3,
509 (double)size / 1.048576e6);
513 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
514 static void genimg_print_time (time_t timestamp)
519 to_tm (timestamp, &tm);
520 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
521 tm.tm_year, tm.tm_mon, tm.tm_mday,
522 tm.tm_hour, tm.tm_min, tm.tm_sec);
524 printf ("%s", ctime(×tamp));
527 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
530 * get_table_entry_name - translate entry id to long name
531 * @table: pointer to a translation table for entries of a specific type
532 * @msg: message to be returned when translation fails
533 * @id: entry id to be translated
535 * get_table_entry_name() will go over translation table trying to find
536 * entry that matches given id. If matching entry is found, its long
537 * name is returned to the caller.
540 * long entry name if translation succeeds
543 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
545 for (; table->id >= 0; ++table) {
547 return (table->lname);
552 const char *genimg_get_os_name (uint8_t os)
554 return (get_table_entry_name (uimage_os, "Unknown OS", os));
557 const char *genimg_get_arch_name (uint8_t arch)
559 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
562 const char *genimg_get_type_name (uint8_t type)
564 return (get_table_entry_name (uimage_type, "Unknown Image", type));
567 const char *genimg_get_comp_name (uint8_t comp)
569 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
573 * get_table_entry_id - translate short entry name to id
574 * @table: pointer to a translation table for entries of a specific type
575 * @table_name: to be used in case of error
576 * @name: entry short name to be translated
578 * get_table_entry_id() will go over translation table trying to find
579 * entry that matches given short name. If matching entry is found,
580 * its id returned to the caller.
583 * entry id if translation succeeds
586 static int get_table_entry_id (table_entry_t *table,
587 const char *table_name, const char *name)
593 for (t = table; t->id >= 0; ++t) {
594 if (t->sname && strcasecmp(t->sname, name) == 0)
598 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
599 for (t = table; t->id >= 0; ++t) {
600 if (t->sname == NULL)
602 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
605 fprintf (stderr, "\n");
607 for (t = table; t->id >= 0; ++t) {
608 if (t->sname && strcmp(t->sname, name) == 0)
611 debug ("Invalid %s Type: %s\n", table_name, name);
612 #endif /* USE_HOSTCC */
616 int genimg_get_os_id (const char *name)
618 return (get_table_entry_id (uimage_os, "OS", name));
621 int genimg_get_arch_id (const char *name)
623 return (get_table_entry_id (uimage_arch, "CPU", name));
626 int genimg_get_type_id (const char *name)
628 return (get_table_entry_id (uimage_type, "Image", name));
631 int genimg_get_comp_id (const char *name)
633 return (get_table_entry_id (uimage_comp, "Compression", name));
638 * genimg_get_format - get image format type
639 * @img_addr: image start address
641 * genimg_get_format() checks whether provided address points to a valid
642 * legacy or FIT image.
644 * New uImage format and FDT blob are based on a libfdt. FDT blob
645 * may be passed directly or embedded in a FIT image. In both situations
646 * genimg_get_format() must be able to dectect libfdt header.
649 * image format type or IMAGE_FORMAT_INVALID if no image is present
651 int genimg_get_format (void *img_addr)
653 ulong format = IMAGE_FORMAT_INVALID;
655 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
659 hdr = (image_header_t *)img_addr;
660 if (image_check_magic(hdr))
661 format = IMAGE_FORMAT_LEGACY;
662 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
664 fit_hdr = (char *)img_addr;
665 if (fdt_check_header (fit_hdr) == 0)
666 format = IMAGE_FORMAT_FIT;
674 * genimg_get_image - get image from special storage (if necessary)
675 * @img_addr: image start address
677 * genimg_get_image() checks if provided image start adddress is located
678 * in a dataflash storage. If so, image is moved to a system RAM memory.
681 * image start address after possible relocation from special storage
683 ulong genimg_get_image (ulong img_addr)
685 ulong ram_addr = img_addr;
687 #ifdef CONFIG_HAS_DATAFLASH
688 ulong h_size, d_size;
690 if (addr_dataflash (img_addr)){
691 /* ger RAM address */
692 ram_addr = CFG_LOAD_ADDR;
694 /* get header size */
695 h_size = image_get_header_size ();
696 #if defined(CONFIG_FIT)
697 if (sizeof(struct fdt_header) > h_size)
698 h_size = sizeof(struct fdt_header);
702 debug (" Reading image header from dataflash address "
703 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
705 read_dataflash (img_addr, h_size, (char *)ram_addr);
708 switch (genimg_get_format ((void *)ram_addr)) {
709 case IMAGE_FORMAT_LEGACY:
710 d_size = image_get_data_size ((image_header_t *)ram_addr);
711 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
714 #if defined(CONFIG_FIT)
715 case IMAGE_FORMAT_FIT:
716 d_size = fit_get_size ((const void *)ram_addr) - h_size;
717 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
722 printf (" No valid image found at 0x%08lx\n", img_addr);
726 /* read in image data */
727 debug (" Reading image remaining data from dataflash address "
728 "%08lx to RAM address %08lx\n", img_addr + h_size,
731 read_dataflash (img_addr + h_size, d_size,
732 (char *)(ram_addr + h_size));
735 #endif /* CONFIG_HAS_DATAFLASH */
741 * boot_get_ramdisk - main ramdisk handling routine
742 * @argc: command argument count
743 * @argv: command argument list
744 * @images: pointer to the bootm images structure
745 * @arch: expected ramdisk architecture
746 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
747 * @rd_end: pointer to a ulong variable, will hold ramdisk end
749 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
750 * Curently supported are the following ramdisk sources:
751 * - multicomponent kernel/ramdisk image,
752 * - commandline provided address of decicated ramdisk image.
755 * 0, if ramdisk image was found and valid, or skiped
756 * rd_start and rd_end are set to ramdisk start/end addresses if
757 * ramdisk image is found and valid
759 * 1, if ramdisk image is found but corrupted
760 * rd_start and rd_end are set to 0 if no ramdisk exists
762 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
763 uint8_t arch, ulong *rd_start, ulong *rd_end)
765 ulong rd_addr, rd_load;
766 ulong rd_data, rd_len;
767 image_header_t *rd_hdr;
768 #if defined(CONFIG_FIT)
770 const char *fit_uname_config = NULL;
771 const char *fit_uname_ramdisk = NULL;
783 * Look for a '-' which indicates to ignore the
786 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
787 debug ("## Skipping init Ramdisk\n");
788 rd_len = rd_data = 0;
789 } else if (argc >= 3) {
790 #if defined(CONFIG_FIT)
792 * If the init ramdisk comes from the FIT image and the FIT image
793 * address is omitted in the command line argument, try to use
794 * os FIT image address or default load address.
796 if (images->fit_uname_os)
797 default_addr = (ulong)images->fit_hdr_os;
799 default_addr = load_addr;
801 if (fit_parse_conf (argv[2], default_addr,
802 &rd_addr, &fit_uname_config)) {
803 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
804 fit_uname_config, rd_addr);
805 } else if (fit_parse_subimage (argv[2], default_addr,
806 &rd_addr, &fit_uname_ramdisk)) {
807 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
808 fit_uname_ramdisk, rd_addr);
812 rd_addr = simple_strtoul(argv[2], NULL, 16);
813 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
817 /* copy from dataflash if needed */
818 rd_addr = genimg_get_image (rd_addr);
821 * Check if there is an initrd image at the
822 * address provided in the second bootm argument
823 * check image type, for FIT images get FIT node.
825 switch (genimg_get_format ((void *)rd_addr)) {
826 case IMAGE_FORMAT_LEGACY:
827 printf ("## Loading init Ramdisk from Legacy "
828 "Image at %08lx ...\n", rd_addr);
830 show_boot_progress (9);
831 rd_hdr = image_get_ramdisk (rd_addr, arch,
837 rd_data = image_get_data (rd_hdr);
838 rd_len = image_get_data_size (rd_hdr);
839 rd_load = image_get_load (rd_hdr);
841 #if defined(CONFIG_FIT)
842 case IMAGE_FORMAT_FIT:
843 fit_hdr = (void *)rd_addr;
844 printf ("## Loading init Ramdisk from FIT "
845 "Image at %08lx ...\n", rd_addr);
847 show_boot_progress (120);
848 if (!fit_check_format (fit_hdr)) {
849 puts ("Bad FIT ramdisk image format!\n");
850 show_boot_progress (-120);
853 show_boot_progress (121);
855 if (!fit_uname_ramdisk) {
857 * no ramdisk image node unit name, try to get config
858 * node first. If config unit node name is NULL
859 * fit_conf_get_node() will try to find default config node
861 show_boot_progress (122);
862 conf_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
863 if (conf_noffset < 0) {
864 show_boot_progress (-122);
868 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, conf_noffset);
869 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
871 /* get ramdisk component image node offset */
872 show_boot_progress (123);
873 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
875 if (rd_noffset < 0) {
876 show_boot_progress (-124);
880 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
882 show_boot_progress (125);
883 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
886 /* get ramdisk image data address and length */
887 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
888 puts ("Could not find ramdisk subimage data!\n");
889 show_boot_progress (-127);
892 show_boot_progress (128);
894 rd_data = (ulong)data;
897 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
898 puts ("Can't get ramdisk subimage load address!\n");
899 show_boot_progress (-129);
902 show_boot_progress (129);
904 images->fit_hdr_rd = fit_hdr;
905 images->fit_uname_rd = fit_uname_ramdisk;
906 images->fit_noffset_rd = rd_noffset;
910 puts ("Wrong Ramdisk Image Format\n");
911 rd_data = rd_len = rd_load = 0;
914 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
916 * We need to copy the ramdisk to SRAM to let Linux boot
919 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
922 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
924 } else if (images->legacy_hdr_valid &&
925 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
927 * Now check if we have a legacy mult-component image,
928 * get second entry data start address and len.
930 show_boot_progress (13);
931 printf ("## Loading init Ramdisk from multi component "
932 "Legacy Image at %08lx ...\n",
933 (ulong)images->legacy_hdr_os);
935 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
940 show_boot_progress (14);
941 rd_len = rd_data = 0;
945 debug ("## No init Ramdisk\n");
948 *rd_end = rd_data + rd_len;
950 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
956 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
958 * boot_ramdisk_high - relocate init ramdisk
959 * @lmb: pointer to lmb handle, will be used for memory mgmt
960 * @rd_data: ramdisk data start address
961 * @rd_len: ramdisk data length
962 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
963 * start address (after possible relocation)
964 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
965 * end address (after possible relocation)
967 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
968 * variable and if requested ramdisk data is moved to a specified location.
970 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
971 * start/end addresses if ramdisk image start and len were provided,
972 * otherwise set initrd_start and initrd_end set to zeros.
978 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
979 ulong *initrd_start, ulong *initrd_end)
983 int initrd_copy_to_ram = 1;
985 if ((s = getenv ("initrd_high")) != NULL) {
986 /* a value of "no" or a similar string will act like 0,
987 * turning the "load high" feature off. This is intentional.
989 initrd_high = simple_strtoul (s, NULL, 16);
990 if (initrd_high == ~0)
991 initrd_copy_to_ram = 0;
993 /* not set, no restrictions to load high */
997 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
998 initrd_high, initrd_copy_to_ram);
1001 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1002 debug (" in-place initrd\n");
1003 *initrd_start = rd_data;
1004 *initrd_end = rd_data + rd_len;
1005 lmb_reserve(lmb, rd_data, rd_len);
1008 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1010 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
1012 if (*initrd_start == 0) {
1013 puts ("ramdisk - allocation error\n");
1016 show_boot_progress (12);
1018 *initrd_end = *initrd_start + rd_len;
1019 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1020 *initrd_start, *initrd_end);
1022 memmove_wd ((void *)*initrd_start,
1023 (void *)rd_data, rd_len, CHUNKSZ);
1031 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1032 *initrd_start, *initrd_end);
1041 * boot_get_cmdline - allocate and initialize kernel cmdline
1042 * @lmb: pointer to lmb handle, will be used for memory mgmt
1043 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1044 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1045 * @bootmap_base: ulong variable, holds offset in physical memory to
1048 * boot_get_cmdline() allocates space for kernel command line below
1049 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1050 * variable is present its contents is copied to allocated kernel
1057 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1063 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1064 CFG_BOOTMAPSZ + bootmap_base);
1066 if (cmdline == NULL)
1069 if ((s = getenv("bootargs")) == NULL)
1074 *cmd_start = (ulong) & cmdline[0];
1075 *cmd_end = *cmd_start + strlen(cmdline);
1077 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1083 * boot_get_kbd - allocate and initialize kernel copy of board info
1084 * @lmb: pointer to lmb handle, will be used for memory mgmt
1085 * @kbd: double pointer to board info data
1086 * @bootmap_base: ulong variable, holds offset in physical memory to
1089 * boot_get_kbd() allocates space for kernel copy of board info data below
1090 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1091 * the current u-boot board info data.
1097 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1099 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1100 CFG_BOOTMAPSZ + bootmap_base);
1106 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1108 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1109 do_bdinfo(NULL, 0, 0, NULL);
1114 #endif /* CONFIG_PPC || CONFIG_M68K */
1115 #endif /* !USE_HOSTCC */
1117 #if defined(CONFIG_FIT)
1118 /*****************************************************************************/
1119 /* New uImage format routines */
1120 /*****************************************************************************/
1122 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1123 ulong *addr, const char **name)
1130 sep = strchr (spec, sepc);
1133 *addr = simple_strtoul (spec, NULL, 16);
1143 * fit_parse_conf - parse FIT configuration spec
1144 * @spec: input string, containing configuration spec
1145 * @add_curr: current image address (to be used as a possible default)
1146 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1148 * @conf_name double pointer to a char, will hold pointer to a configuration
1151 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1152 * where <addr> is a FIT image address that contains configuration
1153 * with a <conf> unit name.
1155 * Address part is optional, and if omitted default add_curr will
1159 * 1 if spec is a valid configuration string,
1160 * addr and conf_name are set accordingly
1163 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1164 ulong *addr, const char **conf_name)
1166 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1170 * fit_parse_subimage - parse FIT subimage spec
1171 * @spec: input string, containing subimage spec
1172 * @add_curr: current image address (to be used as a possible default)
1173 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1175 * @image_name: double pointer to a char, will hold pointer to a subimage name
1177 * fit_parse_subimage() expects subimage spec in the for of
1178 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1179 * subimage with a <subimg> unit name.
1181 * Address part is optional, and if omitted default add_curr will
1185 * 1 if spec is a valid subimage string,
1186 * addr and image_name are set accordingly
1189 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1190 ulong *addr, const char **image_name)
1192 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1194 #endif /* !USE_HOSTCC */
1196 static void fit_get_debug (const void *fit, int noffset,
1197 char *prop_name, int err)
1199 debug ("Can't get '%s' property from FIT 0x%08lx, "
1200 "node: offset %d, name %s (%s)\n",
1201 prop_name, (ulong)fit, noffset,
1202 fit_get_name (fit, noffset, NULL),
1203 fdt_strerror (err));
1207 * __fit_print_contents - prints out the contents of the FIT format image
1208 * @fit: pointer to the FIT format image header
1209 * @p: pointer to prefix string
1211 * __fit_print_contents() formats a multi line FIT image contents description.
1212 * The routine prints out FIT image properties (root node level) follwed by
1213 * the details of each component image.
1216 * no returned results
1218 static void __fit_print_contents (const void *fit, const char *p)
1228 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1232 /* Root node properties */
1233 ret = fit_get_desc (fit, 0, &desc);
1234 printf ("%sFIT description: ", p);
1236 printf ("unavailable\n");
1238 printf ("%s\n", desc);
1240 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1241 ret = fit_get_timestamp (fit, 0, ×tamp);
1242 printf ("%sCreated: ", p);
1244 printf ("unavailable\n");
1246 genimg_print_time (timestamp);
1249 /* Find images parent node offset */
1250 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1251 if (images_noffset < 0) {
1252 printf ("Can't find images parent node '%s' (%s)\n",
1253 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1257 /* Process its subnodes, print out component images details */
1258 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1259 (noffset >= 0) && (ndepth > 0);
1260 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1263 * Direct child node of the images parent node,
1264 * i.e. component image node.
1266 printf ("%s Image %u (%s)\n", p, count++,
1267 fit_get_name(fit, noffset, NULL));
1269 fit_image_print (fit, noffset, p);
1273 /* Find configurations parent node offset */
1274 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1275 if (confs_noffset < 0) {
1276 debug ("Can't get configurations parent node '%s' (%s)\n",
1277 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1281 /* get default configuration unit name from default property */
1282 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1284 printf ("%s Default Configuration: '%s'\n", p, uname);
1286 /* Process its subnodes, print out configurations details */
1287 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1288 (noffset >= 0) && (ndepth > 0);
1289 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1292 * Direct child node of the configurations parent node,
1293 * i.e. configuration node.
1295 printf ("%s Configuration %u (%s)\n", p, count++,
1296 fit_get_name(fit, noffset, NULL));
1298 fit_conf_print (fit, noffset, p);
1303 inline void fit_print_contents (const void *fit)
1305 __fit_print_contents (fit, " ");
1308 inline void fit_print_contents_noindent (const void *fit)
1310 __fit_print_contents (fit, "");
1314 * fit_image_print - prints out the FIT component image details
1315 * @fit: pointer to the FIT format image header
1316 * @image_noffset: offset of the component image node
1317 * @p: pointer to prefix string
1319 * fit_image_print() lists all mandatory properies for the processed component
1320 * image. If present, hash nodes are printed out as well.
1323 * no returned results
1325 void fit_image_print (const void *fit, int image_noffset, const char *p)
1328 uint8_t type, arch, os, comp;
1336 /* Mandatory properties */
1337 ret = fit_get_desc (fit, image_noffset, &desc);
1338 printf ("%s Description: ", p);
1340 printf ("unavailable\n");
1342 printf ("%s\n", desc);
1344 fit_image_get_type (fit, image_noffset, &type);
1345 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1347 fit_image_get_comp (fit, image_noffset, &comp);
1348 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1350 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1353 printf ("%s Data Start: ", p);
1355 printf ("unavailable\n");
1357 printf ("0x%08lx\n", (ulong)data);
1360 printf ("%s Data Size: ", p);
1362 printf ("unavailable\n");
1364 genimg_print_size (size);
1366 /* Remaining, type dependent properties */
1367 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1368 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1369 (type == IH_TYPE_FLATDT)) {
1370 fit_image_get_arch (fit, image_noffset, &arch);
1371 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1374 if (type == IH_TYPE_KERNEL) {
1375 fit_image_get_os (fit, image_noffset, &os);
1376 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1379 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1380 ret = fit_image_get_load (fit, image_noffset, &load);
1381 printf ("%s Load Address: ", p);
1383 printf ("unavailable\n");
1385 printf ("0x%08lx\n", load);
1387 fit_image_get_entry (fit, image_noffset, &entry);
1388 printf ("%s Entry Point: ", p);
1390 printf ("unavailable\n");
1392 printf ("0x%08lx\n", entry);
1395 /* Process all hash subnodes of the component image node */
1396 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1397 (noffset >= 0) && (ndepth > 0);
1398 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1400 /* Direct child node of the component image node */
1401 fit_image_print_hash (fit, noffset, p);
1407 * fit_image_print_hash - prints out the hash node details
1408 * @fit: pointer to the FIT format image header
1409 * @noffset: offset of the hash node
1410 * @p: pointer to prefix string
1412 * fit_image_print_hash() lists properies for the processed hash node
1415 * no returned results
1417 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1425 * Check subnode name, must be equal to "hash".
1426 * Multiple hash nodes require unique unit node
1427 * names, e.g. hash@1, hash@2, etc.
1429 if (strncmp (fit_get_name(fit, noffset, NULL),
1431 strlen(FIT_HASH_NODENAME)) != 0)
1434 debug ("%s Hash node: '%s'\n", p,
1435 fit_get_name (fit, noffset, NULL));
1437 printf ("%s Hash algo: ", p);
1438 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1439 printf ("invalid/unsupported\n");
1442 printf ("%s\n", algo);
1444 ret = fit_image_hash_get_value (fit, noffset, &value,
1446 printf ("%s Hash value: ", p);
1448 printf ("unavailable\n");
1450 for (i = 0; i < value_len; i++)
1451 printf ("%02x", value[i]);
1455 debug ("%s Hash len: %d\n", p, value_len);
1459 * fit_get_desc - get node description property
1460 * @fit: pointer to the FIT format image header
1461 * @noffset: node offset
1462 * @desc: double pointer to the char, will hold pointer to the descrption
1464 * fit_get_desc() reads description property from a given node, if
1465 * description is found pointer to it is returened in third call argument.
1471 int fit_get_desc (const void *fit, int noffset, char **desc)
1475 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1476 if (*desc == NULL) {
1477 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1485 * fit_get_timestamp - get node timestamp property
1486 * @fit: pointer to the FIT format image header
1487 * @noffset: node offset
1488 * @timestamp: pointer to the time_t, will hold read timestamp
1490 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1491 * is found and has a correct size its value is retured in third call
1496 * -1, on property read failure
1497 * -2, on wrong timestamp size
1499 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1504 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1506 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1509 if (len != sizeof (uint32_t)) {
1510 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1514 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1519 * fit_image_get_node - get node offset for component image of a given unit name
1520 * @fit: pointer to the FIT format image header
1521 * @image_uname: component image node unit name
1523 * fit_image_get_node() finds a component image (withing the '/images'
1524 * node) of a provided unit name. If image is found its node offset is
1525 * returned to the caller.
1528 * image node offset when found (>=0)
1529 * negative number on failure (FDT_ERR_* code)
1531 int fit_image_get_node (const void *fit, const char *image_uname)
1533 int noffset, images_noffset;
1535 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1536 if (images_noffset < 0) {
1537 debug ("Can't find images parent node '%s' (%s)\n",
1538 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1539 return images_noffset;
1542 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1544 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1545 image_uname, fdt_strerror (noffset));
1552 * fit_image_get_os - get os id for a given component image node
1553 * @fit: pointer to the FIT format image header
1554 * @noffset: component image node offset
1555 * @os: pointer to the uint8_t, will hold os numeric id
1557 * fit_image_get_os() finds os property in a given component image node.
1558 * If the property is found, its (string) value is translated to the numeric
1559 * id which is returned to the caller.
1565 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1570 /* Get OS name from property data */
1571 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1573 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1578 /* Translate OS name to id */
1579 *os = genimg_get_os_id (data);
1584 * fit_image_get_arch - get arch id for a given component image node
1585 * @fit: pointer to the FIT format image header
1586 * @noffset: component image node offset
1587 * @arch: pointer to the uint8_t, will hold arch numeric id
1589 * fit_image_get_arch() finds arch property in a given component image node.
1590 * If the property is found, its (string) value is translated to the numeric
1591 * id which is returned to the caller.
1597 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1602 /* Get architecture name from property data */
1603 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1605 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1610 /* Translate architecture name to id */
1611 *arch = genimg_get_arch_id (data);
1616 * fit_image_get_type - get type id for a given component image node
1617 * @fit: pointer to the FIT format image header
1618 * @noffset: component image node offset
1619 * @type: pointer to the uint8_t, will hold type numeric id
1621 * fit_image_get_type() finds type property in a given component image node.
1622 * If the property is found, its (string) value is translated to the numeric
1623 * id which is returned to the caller.
1629 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1634 /* Get image type name from property data */
1635 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1637 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1642 /* Translate image type name to id */
1643 *type = genimg_get_type_id (data);
1648 * fit_image_get_comp - get comp id for a given component image node
1649 * @fit: pointer to the FIT format image header
1650 * @noffset: component image node offset
1651 * @comp: pointer to the uint8_t, will hold comp numeric id
1653 * fit_image_get_comp() finds comp property in a given component image node.
1654 * If the property is found, its (string) value is translated to the numeric
1655 * id which is returned to the caller.
1661 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1666 /* Get compression name from property data */
1667 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1669 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1674 /* Translate compression name to id */
1675 *comp = genimg_get_comp_id (data);
1680 * fit_image_get_load - get load address property for a given component image node
1681 * @fit: pointer to the FIT format image header
1682 * @noffset: component image node offset
1683 * @load: pointer to the uint32_t, will hold load address
1685 * fit_image_get_load() finds load address property in a given component image node.
1686 * If the property is found, its value is returned to the caller.
1692 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1695 const uint32_t *data;
1697 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1699 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1703 *load = uimage_to_cpu (*data);
1708 * fit_image_get_entry - get entry point address property for a given component image node
1709 * @fit: pointer to the FIT format image header
1710 * @noffset: component image node offset
1711 * @entry: pointer to the uint32_t, will hold entry point address
1713 * fit_image_get_entry() finds entry point 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_entry (const void *fit, int noffset, ulong *entry)
1723 const uint32_t *data;
1725 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1727 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1731 *entry = uimage_to_cpu (*data);
1736 * fit_image_get_data - get data property and its size for a given component image node
1737 * @fit: pointer to the FIT format image header
1738 * @noffset: component image node offset
1739 * @data: double pointer to void, will hold data property's data address
1740 * @size: pointer to size_t, will hold data property's data size
1742 * fit_image_get_data() finds data property in a given component image node.
1743 * If the property is found its data start address and size are returned to
1750 int fit_image_get_data (const void *fit, int noffset,
1751 const void **data, size_t *size)
1755 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1756 if (*data == NULL) {
1757 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1767 * fit_image_hash_get_algo - get hash algorithm name
1768 * @fit: pointer to the FIT format image header
1769 * @noffset: hash node offset
1770 * @algo: double pointer to char, will hold pointer to the algorithm name
1772 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1773 * If the property is found its data start address is returned to the caller.
1779 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1783 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1784 if (*algo == NULL) {
1785 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1793 * fit_image_hash_get_value - get hash value and length
1794 * @fit: pointer to the FIT format image header
1795 * @noffset: hash node offset
1796 * @value: double pointer to uint8_t, will hold address of a hash value data
1797 * @value_len: pointer to an int, will hold hash data length
1799 * fit_image_hash_get_value() finds hash value property in a given hash node.
1800 * If the property is found its data start address and size are returned to
1807 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1812 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1813 if (*value == NULL) {
1814 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1824 * fit_set_timestamp - set node timestamp property
1825 * @fit: pointer to the FIT format image header
1826 * @noffset: node offset
1827 * @timestamp: timestamp value to be set
1829 * fit_set_timestamp() attempts to set timestamp property in the requested
1830 * node and returns operation status to the caller.
1834 * -1, on property read failure
1836 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1841 t = cpu_to_uimage (timestamp);
1842 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1845 printf ("Can't set '%s' property for '%s' node (%s)\n",
1846 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1847 fdt_strerror (ret));
1855 * calculate_hash - calculate and return hash for provided input data
1856 * @data: pointer to the input data
1857 * @data_len: data length
1858 * @algo: requested hash algorithm
1859 * @value: pointer to the char, will hold hash value data (caller must
1860 * allocate enough free space)
1861 * value_len: length of the calculated hash
1863 * calculate_hash() computes input data hash according to the requested algorithm.
1864 * Resulting hash value is placed in caller provided 'value' buffer, length
1865 * of the calculated hash is returned via value_len pointer argument.
1869 * -1, when algo is unsupported
1871 static int calculate_hash (const void *data, int data_len, const char *algo,
1872 uint8_t *value, int *value_len)
1874 if (strcmp (algo, "crc32") == 0 ) {
1875 *((uint32_t *)value) = crc32 (0, data, data_len);
1876 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1878 } else if (strcmp (algo, "sha1") == 0 ) {
1879 sha1_csum ((unsigned char *) data, data_len,
1880 (unsigned char *) value);
1882 } else if (strcmp (algo, "md5") == 0 ) {
1883 printf ("MD5 not supported\n");
1886 debug ("Unsupported hash alogrithm\n");
1894 * fit_set_hashes - process FIT component image nodes and calculate hashes
1895 * @fit: pointer to the FIT format image header
1897 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1898 * Hashes are calculated for all component images which have hash subnodes
1899 * with algorithm property set to one of the supported hash algorithms.
1903 * libfdt error code, on failure
1905 int fit_set_hashes (void *fit)
1912 /* Find images parent node offset */
1913 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1914 if (images_noffset < 0) {
1915 printf ("Can't find images parent node '%s' (%s)\n",
1916 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1917 return images_noffset;
1920 /* Process its subnodes, print out component images details */
1921 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1922 (noffset >= 0) && (ndepth > 0);
1923 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1926 * Direct child node of the images parent node,
1927 * i.e. component image node.
1929 ret = fit_image_set_hashes (fit, noffset);
1939 * fit_image_set_hashes - calculate/set hashes for given component image node
1940 * @fit: pointer to the FIT format image header
1941 * @image_noffset: requested component image node
1943 * fit_image_set_hashes() adds hash values for an component image node. All
1944 * existing hash subnodes are checked, if algorithm property is set to one of
1945 * the supported hash algorithms, hash value is computed and corresponding
1946 * hash node property is set, for example:
1948 * Input component image node structure:
1950 * o image@1 (at image_noffset)
1951 * | - data = [binary data]
1955 * Output component image node structure:
1957 * o image@1 (at image_noffset)
1958 * | - data = [binary data]
1961 * |- value = sha1(data)
1967 int fit_image_set_hashes (void *fit, int image_noffset)
1972 uint8_t value[FIT_MAX_HASH_LEN];
1977 /* Get image data and data length */
1978 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
1979 printf ("Can't get image data/size\n");
1983 /* Process all hash subnodes of the component image node */
1984 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1985 (noffset >= 0) && (ndepth > 0);
1986 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1988 /* Direct child node of the component image node */
1991 * Check subnode name, must be equal to "hash".
1992 * Multiple hash nodes require unique unit node
1993 * names, e.g. hash@1, hash@2, etc.
1995 if (strncmp (fit_get_name(fit, noffset, NULL),
1997 strlen(FIT_HASH_NODENAME)) != 0) {
1998 /* Not a hash subnode, skip it */
2002 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2003 printf ("Can't get hash algo property for "
2004 "'%s' hash node in '%s' image node\n",
2005 fit_get_name (fit, noffset, NULL),
2006 fit_get_name (fit, image_noffset, NULL));
2010 if (calculate_hash (data, size, algo, value, &value_len)) {
2011 printf ("Unsupported hash algorithm (%s) for "
2012 "'%s' hash node in '%s' image node\n",
2013 algo, fit_get_name (fit, noffset, NULL),
2014 fit_get_name (fit, image_noffset, NULL));
2018 if (fit_image_hash_set_value (fit, noffset, value,
2020 printf ("Can't set hash value for "
2021 "'%s' hash node in '%s' image node\n",
2022 fit_get_name (fit, noffset, NULL),
2023 fit_get_name (fit, image_noffset, NULL));
2033 * fit_image_hash_set_value - set hash value in requested has node
2034 * @fit: pointer to the FIT format image header
2035 * @noffset: hash node offset
2036 * @value: hash value to be set
2037 * @value_len: hash value length
2039 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2040 * given and returns operation status to the caller.
2046 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2051 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2053 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2054 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2055 fdt_strerror (ret));
2061 #endif /* USE_HOSTCC */
2064 * fit_image_check_hashes - verify data intergity
2065 * @fit: pointer to the FIT format image header
2066 * @image_noffset: component image node offset
2068 * fit_image_check_hashes() goes over component image hash nodes,
2069 * re-calculates each data hash and compares with the value stored in hash
2073 * 1, if all hashes are valid
2074 * 0, otherwise (or on error)
2076 int fit_image_check_hashes (const void *fit, int image_noffset)
2083 uint8_t value[FIT_MAX_HASH_LEN];
2089 /* Get image data and data length */
2090 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2091 printf ("Can't get image data/size\n");
2095 /* Process all hash subnodes of the component image node */
2096 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2097 (noffset >= 0) && (ndepth > 0);
2098 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2100 /* Direct child node of the component image node */
2103 * Check subnode name, must be equal to "hash".
2104 * Multiple hash nodes require unique unit node
2105 * names, e.g. hash@1, hash@2, etc.
2107 if (strncmp (fit_get_name(fit, noffset, NULL),
2109 strlen(FIT_HASH_NODENAME)) != 0)
2112 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2113 err_msg = "Can't get hash algo property";
2116 printf ("%s", algo);
2118 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2120 err_msg = "Can't get hash value property";
2124 if (calculate_hash (data, size, algo, value, &value_len)) {
2125 err_msg = "Unsupported hash algorithm";
2129 if (value_len != fit_value_len) {
2130 err_msg = "Bad hash value len";
2132 } else if (memcmp (value, fit_value, value_len) != 0) {
2133 err_msg = "Bad hash value";
2143 printf ("%s for '%s' hash node in '%s' image node\n",
2144 err_msg, fit_get_name (fit, noffset, NULL),
2145 fit_get_name (fit, image_noffset, NULL));
2150 * fit_image_check_os - check whether image node is of a given os type
2151 * @fit: pointer to the FIT format image header
2152 * @noffset: component image node offset
2153 * @os: requested image os
2155 * fit_image_check_os() reads image os property and compares its numeric
2156 * id with the requested os. Comparison result is returned to the caller.
2159 * 1 if image is of given os type
2160 * 0 otherwise (or on error)
2162 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2166 if (fit_image_get_os (fit, noffset, &image_os))
2168 return (os == image_os);
2172 * fit_image_check_arch - check whether image node is of a given arch
2173 * @fit: pointer to the FIT format image header
2174 * @noffset: component image node offset
2175 * @arch: requested imagearch
2177 * fit_image_check_arch() reads image arch property and compares its numeric
2178 * id with the requested arch. Comparison result is returned to the caller.
2181 * 1 if image is of given arch
2182 * 0 otherwise (or on error)
2184 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2188 if (fit_image_get_arch (fit, noffset, &image_arch))
2190 return (arch == image_arch);
2194 * fit_image_check_type - check whether image node is of a given type
2195 * @fit: pointer to the FIT format image header
2196 * @noffset: component image node offset
2197 * @type: requested image type
2199 * fit_image_check_type() reads image type property and compares its numeric
2200 * id with the requested type. Comparison result is returned to the caller.
2203 * 1 if image is of given type
2204 * 0 otherwise (or on error)
2206 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2210 if (fit_image_get_type (fit, noffset, &image_type))
2212 return (type == image_type);
2216 * fit_image_check_comp - check whether image node uses given compression
2217 * @fit: pointer to the FIT format image header
2218 * @noffset: component image node offset
2219 * @comp: requested image compression type
2221 * fit_image_check_comp() reads image compression property and compares its
2222 * numeric id with the requested compression type. Comparison result is
2223 * returned to the caller.
2226 * 1 if image uses requested compression
2227 * 0 otherwise (or on error)
2229 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2233 if (fit_image_get_comp (fit, noffset, &image_comp))
2235 return (comp == image_comp);
2239 * fit_check_format - sanity check FIT image format
2240 * @fit: pointer to the FIT format image header
2242 * fit_check_format() runs a basic sanity FIT image verification.
2243 * Routine checks for mandatory properties, nodes, etc.
2249 int fit_check_format (const void *fit)
2251 /* mandatory / node 'description' property */
2252 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2253 debug ("Wrong FIT format: no description\n");
2257 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2258 /* mandatory / node 'timestamp' property */
2259 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2260 debug ("Wrong FIT format: no description\n");
2265 /* mandatory subimages parent '/images' node */
2266 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2267 debug ("Wrong FIT format: no images parent node\n");
2275 * fit_conf_get_node - get node offset for configuration of a given unit name
2276 * @fit: pointer to the FIT format image header
2277 * @conf_uname: configuration node unit name
2279 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2280 * parant node) of a provided unit name. If configuration is found its node offset
2281 * is returned to the caller.
2283 * When NULL is provided in second argument fit_conf_get_node() will search
2284 * for a default configuration node instead. Default configuration node unit name
2285 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2288 * configuration node offset when found (>=0)
2289 * negative number on failure (FDT_ERR_* code)
2291 int fit_conf_get_node (const void *fit, const char *conf_uname)
2293 int noffset, confs_noffset;
2296 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2297 if (confs_noffset < 0) {
2298 debug ("Can't find configurations parent node '%s' (%s)\n",
2299 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2300 return confs_noffset;
2303 if (conf_uname == NULL) {
2304 /* get configuration unit name from the default property */
2305 debug ("No configuration specified, trying default...\n");
2306 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2307 if (conf_uname == NULL) {
2308 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2311 debug ("Found default configuration: '%s'\n", conf_uname);
2314 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2316 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2317 conf_uname, fdt_strerror (noffset));
2323 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2324 const char *prop_name)
2329 /* get kernel image unit name from configuration kernel property */
2330 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2334 return fit_image_get_node (fit, uname);
2338 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2339 * a given configuration
2340 * @fit: pointer to the FIT format image header
2341 * @noffset: configuration node offset
2343 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2344 * configuration FIT_KERNEL_PROP property and translates it to the node
2348 * image node offset when found (>=0)
2349 * negative number on failure (FDT_ERR_* code)
2351 int fit_conf_get_kernel_node (const void *fit, int noffset)
2353 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2357 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2358 * a given configuration
2359 * @fit: pointer to the FIT format image header
2360 * @noffset: configuration node offset
2362 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2363 * configuration FIT_KERNEL_PROP property and translates it to the node
2367 * image node offset when found (>=0)
2368 * negative number on failure (FDT_ERR_* code)
2370 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2372 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2376 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2377 * a given configuration
2378 * @fit: pointer to the FIT format image header
2379 * @noffset: configuration node offset
2381 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2382 * configuration FIT_KERNEL_PROP property and translates it to the node
2386 * image node offset when found (>=0)
2387 * negative number on failure (FDT_ERR_* code)
2389 int fit_conf_get_fdt_node (const void *fit, int noffset)
2391 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2395 * fit_conf_print - prints out the FIT configuration details
2396 * @fit: pointer to the FIT format image header
2397 * @conf_noffset: offset of the configuration node
2398 * @p: pointer to prefix string
2400 * fit_conf_print() lists all mandatory properies for the processed
2401 * configuration node.
2404 * no returned results
2406 void fit_conf_print (const void *fit, int noffset, const char *p)
2412 /* Mandatory properties */
2413 ret = fit_get_desc (fit, noffset, &desc);
2414 printf ("%s Description: ", p);
2416 printf ("unavailable\n");
2418 printf ("%s\n", desc);
2420 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2421 printf ("%s Kernel: ", p);
2423 printf ("unavailable\n");
2425 printf ("%s\n", uname);
2427 /* Optional properties */
2428 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2430 printf ("%s Init Ramdisk: %s\n", p, uname);
2432 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2434 printf ("%s FDT: %s\n", p, uname);
2438 * fit_check_ramdisk - verify FIT format ramdisk subimage
2439 * @fit_hdr: pointer to the FIT ramdisk header
2440 * @rd_noffset: ramdisk subimage node offset within FIT image
2441 * @arch: requested ramdisk image architecture type
2442 * @verify: data CRC verification flag
2444 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2445 * specified FIT image.
2452 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2454 fit_image_print (fit, rd_noffset, " ");
2457 puts (" Verifying Hash Integrity ... ");
2458 if (!fit_image_check_hashes (fit, rd_noffset)) {
2459 puts ("Bad Data Hash\n");
2460 show_boot_progress (-125);
2466 show_boot_progress (126);
2467 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2468 !fit_image_check_arch (fit, rd_noffset, arch) ||
2469 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2470 printf ("No Linux %s Ramdisk Image\n",
2471 genimg_get_arch_name(arch));
2472 show_boot_progress (-126);
2476 show_boot_progress (127);
2479 #endif /* USE_HOSTCC */
2480 #endif /* CONFIG_FIT */