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 #if defined(CFG_SDRAM_BASE)
467 return CFG_SDRAM_BASE;
468 #elif defined(CONFIG_ARM)
469 return gd->bd->bi_dram[0].start;
475 ulong getenv_bootm_size(void)
477 char *s = getenv ("bootm_size");
479 ulong tmp = simple_strtoul (s, NULL, 16);
483 #if defined(CONFIG_ARM)
484 return gd->bd->bi_dram[0].size;
486 return gd->bd->bi_memsize;
490 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
492 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
494 size_t tail = (len > chunksz) ? chunksz : len;
496 memmove (to, from, tail);
501 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
502 memmove (to, from, len);
503 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
505 #endif /* !USE_HOSTCC */
507 static void genimg_print_size (uint32_t size)
510 printf ("%d Bytes = ", size);
511 print_size (size, "\n");
513 printf ("%d Bytes = %.2f kB = %.2f MB\n",
514 size, (double)size / 1.024e3,
515 (double)size / 1.048576e6);
519 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
520 static void genimg_print_time (time_t timestamp)
525 to_tm (timestamp, &tm);
526 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
527 tm.tm_year, tm.tm_mon, tm.tm_mday,
528 tm.tm_hour, tm.tm_min, tm.tm_sec);
530 printf ("%s", ctime(×tamp));
533 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
536 * get_table_entry_name - translate entry id to long name
537 * @table: pointer to a translation table for entries of a specific type
538 * @msg: message to be returned when translation fails
539 * @id: entry id to be translated
541 * get_table_entry_name() will go over translation table trying to find
542 * entry that matches given id. If matching entry is found, its long
543 * name is returned to the caller.
546 * long entry name if translation succeeds
549 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
551 for (; table->id >= 0; ++table) {
553 return (table->lname);
558 const char *genimg_get_os_name (uint8_t os)
560 return (get_table_entry_name (uimage_os, "Unknown OS", os));
563 const char *genimg_get_arch_name (uint8_t arch)
565 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
568 const char *genimg_get_type_name (uint8_t type)
570 return (get_table_entry_name (uimage_type, "Unknown Image", type));
573 const char *genimg_get_comp_name (uint8_t comp)
575 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
579 * get_table_entry_id - translate short entry name to id
580 * @table: pointer to a translation table for entries of a specific type
581 * @table_name: to be used in case of error
582 * @name: entry short name to be translated
584 * get_table_entry_id() will go over translation table trying to find
585 * entry that matches given short name. If matching entry is found,
586 * its id returned to the caller.
589 * entry id if translation succeeds
592 static int get_table_entry_id (table_entry_t *table,
593 const char *table_name, const char *name)
599 for (t = table; t->id >= 0; ++t) {
600 if (t->sname && strcasecmp(t->sname, name) == 0)
604 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
605 for (t = table; t->id >= 0; ++t) {
606 if (t->sname == NULL)
608 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
611 fprintf (stderr, "\n");
613 for (t = table; t->id >= 0; ++t) {
614 if (t->sname && strcmp(t->sname, name) == 0)
617 debug ("Invalid %s Type: %s\n", table_name, name);
618 #endif /* USE_HOSTCC */
622 int genimg_get_os_id (const char *name)
624 return (get_table_entry_id (uimage_os, "OS", name));
627 int genimg_get_arch_id (const char *name)
629 return (get_table_entry_id (uimage_arch, "CPU", name));
632 int genimg_get_type_id (const char *name)
634 return (get_table_entry_id (uimage_type, "Image", name));
637 int genimg_get_comp_id (const char *name)
639 return (get_table_entry_id (uimage_comp, "Compression", name));
644 * genimg_get_format - get image format type
645 * @img_addr: image start address
647 * genimg_get_format() checks whether provided address points to a valid
648 * legacy or FIT image.
650 * New uImage format and FDT blob are based on a libfdt. FDT blob
651 * may be passed directly or embedded in a FIT image. In both situations
652 * genimg_get_format() must be able to dectect libfdt header.
655 * image format type or IMAGE_FORMAT_INVALID if no image is present
657 int genimg_get_format (void *img_addr)
659 ulong format = IMAGE_FORMAT_INVALID;
661 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
665 hdr = (image_header_t *)img_addr;
666 if (image_check_magic(hdr))
667 format = IMAGE_FORMAT_LEGACY;
668 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
670 fit_hdr = (char *)img_addr;
671 if (fdt_check_header (fit_hdr) == 0)
672 format = IMAGE_FORMAT_FIT;
680 * genimg_get_image - get image from special storage (if necessary)
681 * @img_addr: image start address
683 * genimg_get_image() checks if provided image start adddress is located
684 * in a dataflash storage. If so, image is moved to a system RAM memory.
687 * image start address after possible relocation from special storage
689 ulong genimg_get_image (ulong img_addr)
691 ulong ram_addr = img_addr;
693 #ifdef CONFIG_HAS_DATAFLASH
694 ulong h_size, d_size;
696 if (addr_dataflash (img_addr)){
697 /* ger RAM address */
698 ram_addr = CFG_LOAD_ADDR;
700 /* get header size */
701 h_size = image_get_header_size ();
702 #if defined(CONFIG_FIT)
703 if (sizeof(struct fdt_header) > h_size)
704 h_size = sizeof(struct fdt_header);
708 debug (" Reading image header from dataflash address "
709 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
711 read_dataflash (img_addr, h_size, (char *)ram_addr);
714 switch (genimg_get_format ((void *)ram_addr)) {
715 case IMAGE_FORMAT_LEGACY:
716 d_size = image_get_data_size ((image_header_t *)ram_addr);
717 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
720 #if defined(CONFIG_FIT)
721 case IMAGE_FORMAT_FIT:
722 d_size = fit_get_size ((const void *)ram_addr) - h_size;
723 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
728 printf (" No valid image found at 0x%08lx\n", img_addr);
732 /* read in image data */
733 debug (" Reading image remaining data from dataflash address "
734 "%08lx to RAM address %08lx\n", img_addr + h_size,
737 read_dataflash (img_addr + h_size, d_size,
738 (char *)(ram_addr + h_size));
741 #endif /* CONFIG_HAS_DATAFLASH */
747 * fit_has_config - check if there is a valid FIT configuration
748 * @images: pointer to the bootm command headers structure
750 * fit_has_config() checks if there is a FIT configuration in use
751 * (if FTI support is present).
754 * 0, no FIT support or no configuration found
755 * 1, configuration found
757 int genimg_has_config (bootm_headers_t *images)
759 #if defined(CONFIG_FIT)
760 if (images->fit_uname_cfg)
767 * boot_get_ramdisk - main ramdisk handling routine
768 * @argc: command argument count
769 * @argv: command argument list
770 * @images: pointer to the bootm images structure
771 * @arch: expected ramdisk architecture
772 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
773 * @rd_end: pointer to a ulong variable, will hold ramdisk end
775 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
776 * Curently supported are the following ramdisk sources:
777 * - multicomponent kernel/ramdisk image,
778 * - commandline provided address of decicated ramdisk image.
781 * 0, if ramdisk image was found and valid, or skiped
782 * rd_start and rd_end are set to ramdisk start/end addresses if
783 * ramdisk image is found and valid
785 * 1, if ramdisk image is found but corrupted
786 * rd_start and rd_end are set to 0 if no ramdisk exists
788 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
789 uint8_t arch, ulong *rd_start, ulong *rd_end)
791 ulong rd_addr, rd_load;
792 ulong rd_data, rd_len;
793 image_header_t *rd_hdr;
794 #if defined(CONFIG_FIT)
796 const char *fit_uname_config = NULL;
797 const char *fit_uname_ramdisk = NULL;
809 * Look for a '-' which indicates to ignore the
812 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
813 debug ("## Skipping init Ramdisk\n");
814 rd_len = rd_data = 0;
815 } else if (argc >= 3 || genimg_has_config (images)) {
816 #if defined(CONFIG_FIT)
819 * If the init ramdisk comes from the FIT image and
820 * the FIT image address is omitted in the command
821 * line argument, try to use os FIT image address or
822 * default load address.
824 if (images->fit_uname_os)
825 default_addr = (ulong)images->fit_hdr_os;
827 default_addr = load_addr;
829 if (fit_parse_conf (argv[2], default_addr,
830 &rd_addr, &fit_uname_config)) {
831 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
832 fit_uname_config, rd_addr);
833 } else if (fit_parse_subimage (argv[2], default_addr,
834 &rd_addr, &fit_uname_ramdisk)) {
835 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
836 fit_uname_ramdisk, rd_addr);
840 rd_addr = simple_strtoul(argv[2], NULL, 16);
841 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
844 #if defined(CONFIG_FIT)
846 /* use FIT configuration provided in first bootm
849 rd_addr = (ulong)images->fit_hdr_os;
850 fit_uname_config = images->fit_uname_cfg;
851 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
852 fit_uname_config, rd_addr);
855 * Check whether configuration has ramdisk defined,
856 * if not, don't try to use it, quit silently.
858 fit_hdr = (void *)rd_addr;
859 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
860 if (cfg_noffset < 0) {
861 debug ("* ramdisk: no such config\n");
865 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
866 if (rd_noffset < 0) {
867 debug ("* ramdisk: no ramdisk in config\n");
873 /* copy from dataflash if needed */
874 rd_addr = genimg_get_image (rd_addr);
877 * Check if there is an initrd image at the
878 * address provided in the second bootm argument
879 * check image type, for FIT images get FIT node.
881 switch (genimg_get_format ((void *)rd_addr)) {
882 case IMAGE_FORMAT_LEGACY:
883 printf ("## Loading init Ramdisk from Legacy "
884 "Image at %08lx ...\n", rd_addr);
886 show_boot_progress (9);
887 rd_hdr = image_get_ramdisk (rd_addr, arch,
893 rd_data = image_get_data (rd_hdr);
894 rd_len = image_get_data_size (rd_hdr);
895 rd_load = image_get_load (rd_hdr);
897 #if defined(CONFIG_FIT)
898 case IMAGE_FORMAT_FIT:
899 fit_hdr = (void *)rd_addr;
900 printf ("## Loading init Ramdisk from FIT "
901 "Image at %08lx ...\n", rd_addr);
903 show_boot_progress (120);
904 if (!fit_check_format (fit_hdr)) {
905 puts ("Bad FIT ramdisk image format!\n");
906 show_boot_progress (-120);
909 show_boot_progress (121);
911 if (!fit_uname_ramdisk) {
913 * no ramdisk image node unit name, try to get config
914 * node first. If config unit node name is NULL
915 * fit_conf_get_node() will try to find default config node
917 show_boot_progress (122);
918 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
919 if (cfg_noffset < 0) {
920 puts ("Could not find configuration node\n");
921 show_boot_progress (-122);
924 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
925 printf (" Using '%s' configuration\n", fit_uname_config);
927 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
928 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
930 /* get ramdisk component image node offset */
931 show_boot_progress (123);
932 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
934 if (rd_noffset < 0) {
935 puts ("Could not find subimage node\n");
936 show_boot_progress (-124);
940 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
942 show_boot_progress (125);
943 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
946 /* get ramdisk image data address and length */
947 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
948 puts ("Could not find ramdisk subimage data!\n");
949 show_boot_progress (-127);
952 show_boot_progress (128);
954 rd_data = (ulong)data;
957 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
958 puts ("Can't get ramdisk subimage load address!\n");
959 show_boot_progress (-129);
962 show_boot_progress (129);
964 images->fit_hdr_rd = fit_hdr;
965 images->fit_uname_rd = fit_uname_ramdisk;
966 images->fit_noffset_rd = rd_noffset;
970 puts ("Wrong Ramdisk Image Format\n");
971 rd_data = rd_len = rd_load = 0;
974 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
976 * We need to copy the ramdisk to SRAM to let Linux boot
979 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
982 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
984 } else if (images->legacy_hdr_valid &&
985 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
987 * Now check if we have a legacy mult-component image,
988 * get second entry data start address and len.
990 show_boot_progress (13);
991 printf ("## Loading init Ramdisk from multi component "
992 "Legacy Image at %08lx ...\n",
993 (ulong)images->legacy_hdr_os);
995 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
1000 show_boot_progress (14);
1001 rd_len = rd_data = 0;
1005 debug ("## No init Ramdisk\n");
1007 *rd_start = rd_data;
1008 *rd_end = rd_data + rd_len;
1010 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1011 *rd_start, *rd_end);
1016 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
1018 * boot_ramdisk_high - relocate init ramdisk
1019 * @lmb: pointer to lmb handle, will be used for memory mgmt
1020 * @rd_data: ramdisk data start address
1021 * @rd_len: ramdisk data length
1022 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1023 * start address (after possible relocation)
1024 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1025 * end address (after possible relocation)
1027 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1028 * variable and if requested ramdisk data is moved to a specified location.
1030 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1031 * start/end addresses if ramdisk image start and len were provided,
1032 * otherwise set initrd_start and initrd_end set to zeros.
1038 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1039 ulong *initrd_start, ulong *initrd_end)
1043 int initrd_copy_to_ram = 1;
1045 if ((s = getenv ("initrd_high")) != NULL) {
1046 /* a value of "no" or a similar string will act like 0,
1047 * turning the "load high" feature off. This is intentional.
1049 initrd_high = simple_strtoul (s, NULL, 16);
1050 if (initrd_high == ~0)
1051 initrd_copy_to_ram = 0;
1053 /* not set, no restrictions to load high */
1057 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1058 initrd_high, initrd_copy_to_ram);
1061 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1062 debug (" in-place initrd\n");
1063 *initrd_start = rd_data;
1064 *initrd_end = rd_data + rd_len;
1065 lmb_reserve(lmb, rd_data, rd_len);
1068 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1070 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
1072 if (*initrd_start == 0) {
1073 puts ("ramdisk - allocation error\n");
1076 show_boot_progress (12);
1078 *initrd_end = *initrd_start + rd_len;
1079 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1080 *initrd_start, *initrd_end);
1082 memmove_wd ((void *)*initrd_start,
1083 (void *)rd_data, rd_len, CHUNKSZ);
1091 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1092 *initrd_start, *initrd_end);
1101 * boot_get_cmdline - allocate and initialize kernel cmdline
1102 * @lmb: pointer to lmb handle, will be used for memory mgmt
1103 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1104 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1105 * @bootmap_base: ulong variable, holds offset in physical memory to
1108 * boot_get_cmdline() allocates space for kernel command line below
1109 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1110 * variable is present its contents is copied to allocated kernel
1117 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1123 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1124 CFG_BOOTMAPSZ + bootmap_base);
1126 if (cmdline == NULL)
1129 if ((s = getenv("bootargs")) == NULL)
1134 *cmd_start = (ulong) & cmdline[0];
1135 *cmd_end = *cmd_start + strlen(cmdline);
1137 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1143 * boot_get_kbd - allocate and initialize kernel copy of board info
1144 * @lmb: pointer to lmb handle, will be used for memory mgmt
1145 * @kbd: double pointer to board info data
1146 * @bootmap_base: ulong variable, holds offset in physical memory to
1149 * boot_get_kbd() allocates space for kernel copy of board info data below
1150 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1151 * the current u-boot board info data.
1157 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1159 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1160 CFG_BOOTMAPSZ + bootmap_base);
1166 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1168 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1169 do_bdinfo(NULL, 0, 0, NULL);
1174 #endif /* CONFIG_PPC || CONFIG_M68K */
1175 #endif /* !USE_HOSTCC */
1177 #if defined(CONFIG_FIT)
1178 /*****************************************************************************/
1179 /* New uImage format routines */
1180 /*****************************************************************************/
1182 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1183 ulong *addr, const char **name)
1190 sep = strchr (spec, sepc);
1193 *addr = simple_strtoul (spec, NULL, 16);
1203 * fit_parse_conf - parse FIT configuration spec
1204 * @spec: input string, containing configuration spec
1205 * @add_curr: current image address (to be used as a possible default)
1206 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1208 * @conf_name double pointer to a char, will hold pointer to a configuration
1211 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1212 * where <addr> is a FIT image address that contains configuration
1213 * with a <conf> unit name.
1215 * Address part is optional, and if omitted default add_curr will
1219 * 1 if spec is a valid configuration string,
1220 * addr and conf_name are set accordingly
1223 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1224 ulong *addr, const char **conf_name)
1226 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1230 * fit_parse_subimage - parse FIT subimage spec
1231 * @spec: input string, containing subimage spec
1232 * @add_curr: current image address (to be used as a possible default)
1233 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1235 * @image_name: double pointer to a char, will hold pointer to a subimage name
1237 * fit_parse_subimage() expects subimage spec in the for of
1238 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1239 * subimage with a <subimg> unit name.
1241 * Address part is optional, and if omitted default add_curr will
1245 * 1 if spec is a valid subimage string,
1246 * addr and image_name are set accordingly
1249 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1250 ulong *addr, const char **image_name)
1252 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1254 #endif /* !USE_HOSTCC */
1256 static void fit_get_debug (const void *fit, int noffset,
1257 char *prop_name, int err)
1259 debug ("Can't get '%s' property from FIT 0x%08lx, "
1260 "node: offset %d, name %s (%s)\n",
1261 prop_name, (ulong)fit, noffset,
1262 fit_get_name (fit, noffset, NULL),
1263 fdt_strerror (err));
1267 * __fit_print_contents - prints out the contents of the FIT format image
1268 * @fit: pointer to the FIT format image header
1269 * @p: pointer to prefix string
1271 * __fit_print_contents() formats a multi line FIT image contents description.
1272 * The routine prints out FIT image properties (root node level) follwed by
1273 * the details of each component image.
1276 * no returned results
1278 static void __fit_print_contents (const void *fit, const char *p)
1288 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1292 /* Root node properties */
1293 ret = fit_get_desc (fit, 0, &desc);
1294 printf ("%sFIT description: ", p);
1296 printf ("unavailable\n");
1298 printf ("%s\n", desc);
1300 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1301 ret = fit_get_timestamp (fit, 0, ×tamp);
1302 printf ("%sCreated: ", p);
1304 printf ("unavailable\n");
1306 genimg_print_time (timestamp);
1309 /* Find images parent node offset */
1310 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1311 if (images_noffset < 0) {
1312 printf ("Can't find images parent node '%s' (%s)\n",
1313 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1317 /* Process its subnodes, print out component images details */
1318 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1319 (noffset >= 0) && (ndepth > 0);
1320 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1323 * Direct child node of the images parent node,
1324 * i.e. component image node.
1326 printf ("%s Image %u (%s)\n", p, count++,
1327 fit_get_name(fit, noffset, NULL));
1329 fit_image_print (fit, noffset, p);
1333 /* Find configurations parent node offset */
1334 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1335 if (confs_noffset < 0) {
1336 debug ("Can't get configurations parent node '%s' (%s)\n",
1337 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1341 /* get default configuration unit name from default property */
1342 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1344 printf ("%s Default Configuration: '%s'\n", p, uname);
1346 /* Process its subnodes, print out configurations details */
1347 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1348 (noffset >= 0) && (ndepth > 0);
1349 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1352 * Direct child node of the configurations parent node,
1353 * i.e. configuration node.
1355 printf ("%s Configuration %u (%s)\n", p, count++,
1356 fit_get_name(fit, noffset, NULL));
1358 fit_conf_print (fit, noffset, p);
1363 inline void fit_print_contents (const void *fit)
1365 __fit_print_contents (fit, " ");
1368 inline void fit_print_contents_noindent (const void *fit)
1370 __fit_print_contents (fit, "");
1374 * fit_image_print - prints out the FIT component image details
1375 * @fit: pointer to the FIT format image header
1376 * @image_noffset: offset of the component image node
1377 * @p: pointer to prefix string
1379 * fit_image_print() lists all mandatory properies for the processed component
1380 * image. If present, hash nodes are printed out as well.
1383 * no returned results
1385 void fit_image_print (const void *fit, int image_noffset, const char *p)
1388 uint8_t type, arch, os, comp;
1396 /* Mandatory properties */
1397 ret = fit_get_desc (fit, image_noffset, &desc);
1398 printf ("%s Description: ", p);
1400 printf ("unavailable\n");
1402 printf ("%s\n", desc);
1404 fit_image_get_type (fit, image_noffset, &type);
1405 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1407 fit_image_get_comp (fit, image_noffset, &comp);
1408 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1410 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1413 printf ("%s Data Start: ", p);
1415 printf ("unavailable\n");
1417 printf ("0x%08lx\n", (ulong)data);
1420 printf ("%s Data Size: ", p);
1422 printf ("unavailable\n");
1424 genimg_print_size (size);
1426 /* Remaining, type dependent properties */
1427 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1428 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1429 (type == IH_TYPE_FLATDT)) {
1430 fit_image_get_arch (fit, image_noffset, &arch);
1431 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1434 if (type == IH_TYPE_KERNEL) {
1435 fit_image_get_os (fit, image_noffset, &os);
1436 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1439 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1440 ret = fit_image_get_load (fit, image_noffset, &load);
1441 printf ("%s Load Address: ", p);
1443 printf ("unavailable\n");
1445 printf ("0x%08lx\n", load);
1447 fit_image_get_entry (fit, image_noffset, &entry);
1448 printf ("%s Entry Point: ", p);
1450 printf ("unavailable\n");
1452 printf ("0x%08lx\n", entry);
1455 /* Process all hash subnodes of the component image node */
1456 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1457 (noffset >= 0) && (ndepth > 0);
1458 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1460 /* Direct child node of the component image node */
1461 fit_image_print_hash (fit, noffset, p);
1467 * fit_image_print_hash - prints out the hash node details
1468 * @fit: pointer to the FIT format image header
1469 * @noffset: offset of the hash node
1470 * @p: pointer to prefix string
1472 * fit_image_print_hash() lists properies for the processed hash node
1475 * no returned results
1477 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1485 * Check subnode name, must be equal to "hash".
1486 * Multiple hash nodes require unique unit node
1487 * names, e.g. hash@1, hash@2, etc.
1489 if (strncmp (fit_get_name(fit, noffset, NULL),
1491 strlen(FIT_HASH_NODENAME)) != 0)
1494 debug ("%s Hash node: '%s'\n", p,
1495 fit_get_name (fit, noffset, NULL));
1497 printf ("%s Hash algo: ", p);
1498 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1499 printf ("invalid/unsupported\n");
1502 printf ("%s\n", algo);
1504 ret = fit_image_hash_get_value (fit, noffset, &value,
1506 printf ("%s Hash value: ", p);
1508 printf ("unavailable\n");
1510 for (i = 0; i < value_len; i++)
1511 printf ("%02x", value[i]);
1515 debug ("%s Hash len: %d\n", p, value_len);
1519 * fit_get_desc - get node description property
1520 * @fit: pointer to the FIT format image header
1521 * @noffset: node offset
1522 * @desc: double pointer to the char, will hold pointer to the descrption
1524 * fit_get_desc() reads description property from a given node, if
1525 * description is found pointer to it is returened in third call argument.
1531 int fit_get_desc (const void *fit, int noffset, char **desc)
1535 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1536 if (*desc == NULL) {
1537 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1545 * fit_get_timestamp - get node timestamp property
1546 * @fit: pointer to the FIT format image header
1547 * @noffset: node offset
1548 * @timestamp: pointer to the time_t, will hold read timestamp
1550 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1551 * is found and has a correct size its value is retured in third call
1556 * -1, on property read failure
1557 * -2, on wrong timestamp size
1559 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1564 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1566 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1569 if (len != sizeof (uint32_t)) {
1570 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1574 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1579 * fit_image_get_node - get node offset for component image of a given unit name
1580 * @fit: pointer to the FIT format image header
1581 * @image_uname: component image node unit name
1583 * fit_image_get_node() finds a component image (withing the '/images'
1584 * node) of a provided unit name. If image is found its node offset is
1585 * returned to the caller.
1588 * image node offset when found (>=0)
1589 * negative number on failure (FDT_ERR_* code)
1591 int fit_image_get_node (const void *fit, const char *image_uname)
1593 int noffset, images_noffset;
1595 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1596 if (images_noffset < 0) {
1597 debug ("Can't find images parent node '%s' (%s)\n",
1598 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1599 return images_noffset;
1602 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1604 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1605 image_uname, fdt_strerror (noffset));
1612 * fit_image_get_os - get os id for a given component image node
1613 * @fit: pointer to the FIT format image header
1614 * @noffset: component image node offset
1615 * @os: pointer to the uint8_t, will hold os numeric id
1617 * fit_image_get_os() finds os property in a given component image node.
1618 * If the property is found, its (string) value is translated to the numeric
1619 * id which is returned to the caller.
1625 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1630 /* Get OS name from property data */
1631 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1633 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1638 /* Translate OS name to id */
1639 *os = genimg_get_os_id (data);
1644 * fit_image_get_arch - get arch id for a given component image node
1645 * @fit: pointer to the FIT format image header
1646 * @noffset: component image node offset
1647 * @arch: pointer to the uint8_t, will hold arch numeric id
1649 * fit_image_get_arch() finds arch property in a given component image node.
1650 * If the property is found, its (string) value is translated to the numeric
1651 * id which is returned to the caller.
1657 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1662 /* Get architecture name from property data */
1663 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1665 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1670 /* Translate architecture name to id */
1671 *arch = genimg_get_arch_id (data);
1676 * fit_image_get_type - get type id for a given component image node
1677 * @fit: pointer to the FIT format image header
1678 * @noffset: component image node offset
1679 * @type: pointer to the uint8_t, will hold type numeric id
1681 * fit_image_get_type() finds type property in a given component image node.
1682 * If the property is found, its (string) value is translated to the numeric
1683 * id which is returned to the caller.
1689 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1694 /* Get image type name from property data */
1695 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1697 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1702 /* Translate image type name to id */
1703 *type = genimg_get_type_id (data);
1708 * fit_image_get_comp - get comp id for a given component image node
1709 * @fit: pointer to the FIT format image header
1710 * @noffset: component image node offset
1711 * @comp: pointer to the uint8_t, will hold comp numeric id
1713 * fit_image_get_comp() finds comp property in a given component image node.
1714 * If the property is found, its (string) value is translated to the numeric
1715 * id which is returned to the caller.
1721 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1726 /* Get compression name from property data */
1727 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1729 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1734 /* Translate compression name to id */
1735 *comp = genimg_get_comp_id (data);
1740 * fit_image_get_load - get load address property for a given component image node
1741 * @fit: pointer to the FIT format image header
1742 * @noffset: component image node offset
1743 * @load: pointer to the uint32_t, will hold load address
1745 * fit_image_get_load() finds load address property in a given component image node.
1746 * If the property is found, its value is returned to the caller.
1752 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1755 const uint32_t *data;
1757 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1759 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1763 *load = uimage_to_cpu (*data);
1768 * fit_image_get_entry - get entry point address property for a given component image node
1769 * @fit: pointer to the FIT format image header
1770 * @noffset: component image node offset
1771 * @entry: pointer to the uint32_t, will hold entry point address
1773 * fit_image_get_entry() finds entry point address property in a given component image node.
1774 * If the property is found, its value is returned to the caller.
1780 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1783 const uint32_t *data;
1785 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1787 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1791 *entry = uimage_to_cpu (*data);
1796 * fit_image_get_data - get data property and its size for a given component image node
1797 * @fit: pointer to the FIT format image header
1798 * @noffset: component image node offset
1799 * @data: double pointer to void, will hold data property's data address
1800 * @size: pointer to size_t, will hold data property's data size
1802 * fit_image_get_data() finds data property in a given component image node.
1803 * If the property is found its data start address and size are returned to
1810 int fit_image_get_data (const void *fit, int noffset,
1811 const void **data, size_t *size)
1815 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1816 if (*data == NULL) {
1817 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1827 * fit_image_hash_get_algo - get hash algorithm name
1828 * @fit: pointer to the FIT format image header
1829 * @noffset: hash node offset
1830 * @algo: double pointer to char, will hold pointer to the algorithm name
1832 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1833 * If the property is found its data start address is returned to the caller.
1839 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1843 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1844 if (*algo == NULL) {
1845 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1853 * fit_image_hash_get_value - get hash value and length
1854 * @fit: pointer to the FIT format image header
1855 * @noffset: hash node offset
1856 * @value: double pointer to uint8_t, will hold address of a hash value data
1857 * @value_len: pointer to an int, will hold hash data length
1859 * fit_image_hash_get_value() finds hash value property in a given hash node.
1860 * If the property is found its data start address and size are returned to
1867 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1872 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1873 if (*value == NULL) {
1874 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1884 * fit_set_timestamp - set node timestamp property
1885 * @fit: pointer to the FIT format image header
1886 * @noffset: node offset
1887 * @timestamp: timestamp value to be set
1889 * fit_set_timestamp() attempts to set timestamp property in the requested
1890 * node and returns operation status to the caller.
1894 * -1, on property read failure
1896 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1901 t = cpu_to_uimage (timestamp);
1902 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1905 printf ("Can't set '%s' property for '%s' node (%s)\n",
1906 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1907 fdt_strerror (ret));
1915 * calculate_hash - calculate and return hash for provided input data
1916 * @data: pointer to the input data
1917 * @data_len: data length
1918 * @algo: requested hash algorithm
1919 * @value: pointer to the char, will hold hash value data (caller must
1920 * allocate enough free space)
1921 * value_len: length of the calculated hash
1923 * calculate_hash() computes input data hash according to the requested algorithm.
1924 * Resulting hash value is placed in caller provided 'value' buffer, length
1925 * of the calculated hash is returned via value_len pointer argument.
1929 * -1, when algo is unsupported
1931 static int calculate_hash (const void *data, int data_len, const char *algo,
1932 uint8_t *value, int *value_len)
1934 if (strcmp (algo, "crc32") == 0 ) {
1935 *((uint32_t *)value) = crc32 (0, data, data_len);
1936 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1938 } else if (strcmp (algo, "sha1") == 0 ) {
1939 sha1_csum ((unsigned char *) data, data_len,
1940 (unsigned char *) value);
1942 } else if (strcmp (algo, "md5") == 0 ) {
1943 printf ("MD5 not supported\n");
1946 debug ("Unsupported hash alogrithm\n");
1954 * fit_set_hashes - process FIT component image nodes and calculate hashes
1955 * @fit: pointer to the FIT format image header
1957 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1958 * Hashes are calculated for all component images which have hash subnodes
1959 * with algorithm property set to one of the supported hash algorithms.
1963 * libfdt error code, on failure
1965 int fit_set_hashes (void *fit)
1972 /* Find images parent node offset */
1973 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1974 if (images_noffset < 0) {
1975 printf ("Can't find images parent node '%s' (%s)\n",
1976 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1977 return images_noffset;
1980 /* Process its subnodes, print out component images details */
1981 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1982 (noffset >= 0) && (ndepth > 0);
1983 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1986 * Direct child node of the images parent node,
1987 * i.e. component image node.
1989 ret = fit_image_set_hashes (fit, noffset);
1999 * fit_image_set_hashes - calculate/set hashes for given component image node
2000 * @fit: pointer to the FIT format image header
2001 * @image_noffset: requested component image node
2003 * fit_image_set_hashes() adds hash values for an component image node. All
2004 * existing hash subnodes are checked, if algorithm property is set to one of
2005 * the supported hash algorithms, hash value is computed and corresponding
2006 * hash node property is set, for example:
2008 * Input component image node structure:
2010 * o image@1 (at image_noffset)
2011 * | - data = [binary data]
2015 * Output component image node structure:
2017 * o image@1 (at image_noffset)
2018 * | - data = [binary data]
2021 * |- value = sha1(data)
2027 int fit_image_set_hashes (void *fit, int image_noffset)
2032 uint8_t value[FIT_MAX_HASH_LEN];
2037 /* Get image data and data length */
2038 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2039 printf ("Can't get image data/size\n");
2043 /* Process all hash subnodes of the component image node */
2044 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2045 (noffset >= 0) && (ndepth > 0);
2046 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2048 /* Direct child node of the component image node */
2051 * Check subnode name, must be equal to "hash".
2052 * Multiple hash nodes require unique unit node
2053 * names, e.g. hash@1, hash@2, etc.
2055 if (strncmp (fit_get_name(fit, noffset, NULL),
2057 strlen(FIT_HASH_NODENAME)) != 0) {
2058 /* Not a hash subnode, skip it */
2062 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2063 printf ("Can't get hash algo property for "
2064 "'%s' hash node in '%s' image node\n",
2065 fit_get_name (fit, noffset, NULL),
2066 fit_get_name (fit, image_noffset, NULL));
2070 if (calculate_hash (data, size, algo, value, &value_len)) {
2071 printf ("Unsupported hash algorithm (%s) for "
2072 "'%s' hash node in '%s' image node\n",
2073 algo, fit_get_name (fit, noffset, NULL),
2074 fit_get_name (fit, image_noffset, NULL));
2078 if (fit_image_hash_set_value (fit, noffset, value,
2080 printf ("Can't set hash value for "
2081 "'%s' hash node in '%s' image node\n",
2082 fit_get_name (fit, noffset, NULL),
2083 fit_get_name (fit, image_noffset, NULL));
2093 * fit_image_hash_set_value - set hash value in requested has node
2094 * @fit: pointer to the FIT format image header
2095 * @noffset: hash node offset
2096 * @value: hash value to be set
2097 * @value_len: hash value length
2099 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2100 * given and returns operation status to the caller.
2106 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2111 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2113 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2114 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2115 fdt_strerror (ret));
2121 #endif /* USE_HOSTCC */
2124 * fit_image_check_hashes - verify data intergity
2125 * @fit: pointer to the FIT format image header
2126 * @image_noffset: component image node offset
2128 * fit_image_check_hashes() goes over component image hash nodes,
2129 * re-calculates each data hash and compares with the value stored in hash
2133 * 1, if all hashes are valid
2134 * 0, otherwise (or on error)
2136 int fit_image_check_hashes (const void *fit, int image_noffset)
2143 uint8_t value[FIT_MAX_HASH_LEN];
2149 /* Get image data and data length */
2150 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2151 printf ("Can't get image data/size\n");
2155 /* Process all hash subnodes of the component image node */
2156 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2157 (noffset >= 0) && (ndepth > 0);
2158 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2160 /* Direct child node of the component image node */
2163 * Check subnode name, must be equal to "hash".
2164 * Multiple hash nodes require unique unit node
2165 * names, e.g. hash@1, hash@2, etc.
2167 if (strncmp (fit_get_name(fit, noffset, NULL),
2169 strlen(FIT_HASH_NODENAME)) != 0)
2172 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2173 err_msg = "Can't get hash algo property";
2176 printf ("%s", algo);
2178 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2180 err_msg = "Can't get hash value property";
2184 if (calculate_hash (data, size, algo, value, &value_len)) {
2185 err_msg = "Unsupported hash algorithm";
2189 if (value_len != fit_value_len) {
2190 err_msg = "Bad hash value len";
2192 } else if (memcmp (value, fit_value, value_len) != 0) {
2193 err_msg = "Bad hash value";
2203 printf ("%s for '%s' hash node in '%s' image node\n",
2204 err_msg, fit_get_name (fit, noffset, NULL),
2205 fit_get_name (fit, image_noffset, NULL));
2210 * fit_image_check_os - check whether image node is of a given os type
2211 * @fit: pointer to the FIT format image header
2212 * @noffset: component image node offset
2213 * @os: requested image os
2215 * fit_image_check_os() reads image os property and compares its numeric
2216 * id with the requested os. Comparison result is returned to the caller.
2219 * 1 if image is of given os type
2220 * 0 otherwise (or on error)
2222 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2226 if (fit_image_get_os (fit, noffset, &image_os))
2228 return (os == image_os);
2232 * fit_image_check_arch - check whether image node is of a given arch
2233 * @fit: pointer to the FIT format image header
2234 * @noffset: component image node offset
2235 * @arch: requested imagearch
2237 * fit_image_check_arch() reads image arch property and compares its numeric
2238 * id with the requested arch. Comparison result is returned to the caller.
2241 * 1 if image is of given arch
2242 * 0 otherwise (or on error)
2244 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2248 if (fit_image_get_arch (fit, noffset, &image_arch))
2250 return (arch == image_arch);
2254 * fit_image_check_type - check whether image node is of a given type
2255 * @fit: pointer to the FIT format image header
2256 * @noffset: component image node offset
2257 * @type: requested image type
2259 * fit_image_check_type() reads image type property and compares its numeric
2260 * id with the requested type. Comparison result is returned to the caller.
2263 * 1 if image is of given type
2264 * 0 otherwise (or on error)
2266 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2270 if (fit_image_get_type (fit, noffset, &image_type))
2272 return (type == image_type);
2276 * fit_image_check_comp - check whether image node uses given compression
2277 * @fit: pointer to the FIT format image header
2278 * @noffset: component image node offset
2279 * @comp: requested image compression type
2281 * fit_image_check_comp() reads image compression property and compares its
2282 * numeric id with the requested compression type. Comparison result is
2283 * returned to the caller.
2286 * 1 if image uses requested compression
2287 * 0 otherwise (or on error)
2289 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2293 if (fit_image_get_comp (fit, noffset, &image_comp))
2295 return (comp == image_comp);
2299 * fit_check_format - sanity check FIT image format
2300 * @fit: pointer to the FIT format image header
2302 * fit_check_format() runs a basic sanity FIT image verification.
2303 * Routine checks for mandatory properties, nodes, etc.
2309 int fit_check_format (const void *fit)
2311 /* mandatory / node 'description' property */
2312 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2313 debug ("Wrong FIT format: no description\n");
2317 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2318 /* mandatory / node 'timestamp' property */
2319 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2320 debug ("Wrong FIT format: no description\n");
2325 /* mandatory subimages parent '/images' node */
2326 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2327 debug ("Wrong FIT format: no images parent node\n");
2335 * fit_conf_get_node - get node offset for configuration of a given unit name
2336 * @fit: pointer to the FIT format image header
2337 * @conf_uname: configuration node unit name
2339 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2340 * parant node) of a provided unit name. If configuration is found its node offset
2341 * is returned to the caller.
2343 * When NULL is provided in second argument fit_conf_get_node() will search
2344 * for a default configuration node instead. Default configuration node unit name
2345 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2348 * configuration node offset when found (>=0)
2349 * negative number on failure (FDT_ERR_* code)
2351 int fit_conf_get_node (const void *fit, const char *conf_uname)
2353 int noffset, confs_noffset;
2356 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2357 if (confs_noffset < 0) {
2358 debug ("Can't find configurations parent node '%s' (%s)\n",
2359 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2360 return confs_noffset;
2363 if (conf_uname == NULL) {
2364 /* get configuration unit name from the default property */
2365 debug ("No configuration specified, trying default...\n");
2366 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2367 if (conf_uname == NULL) {
2368 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2371 debug ("Found default configuration: '%s'\n", conf_uname);
2374 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2376 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2377 conf_uname, fdt_strerror (noffset));
2383 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2384 const char *prop_name)
2389 /* get kernel image unit name from configuration kernel property */
2390 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2394 return fit_image_get_node (fit, uname);
2398 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2399 * a given configuration
2400 * @fit: pointer to the FIT format image header
2401 * @noffset: configuration node offset
2403 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2404 * configuration FIT_KERNEL_PROP property and translates it to the node
2408 * image node offset when found (>=0)
2409 * negative number on failure (FDT_ERR_* code)
2411 int fit_conf_get_kernel_node (const void *fit, int noffset)
2413 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2417 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2418 * a given configuration
2419 * @fit: pointer to the FIT format image header
2420 * @noffset: configuration node offset
2422 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2423 * configuration FIT_KERNEL_PROP property and translates it to the node
2427 * image node offset when found (>=0)
2428 * negative number on failure (FDT_ERR_* code)
2430 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2432 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2436 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2437 * a given configuration
2438 * @fit: pointer to the FIT format image header
2439 * @noffset: configuration node offset
2441 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2442 * configuration FIT_KERNEL_PROP property and translates it to the node
2446 * image node offset when found (>=0)
2447 * negative number on failure (FDT_ERR_* code)
2449 int fit_conf_get_fdt_node (const void *fit, int noffset)
2451 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2455 * fit_conf_print - prints out the FIT configuration details
2456 * @fit: pointer to the FIT format image header
2457 * @noffset: offset of the configuration node
2458 * @p: pointer to prefix string
2460 * fit_conf_print() lists all mandatory properies for the processed
2461 * configuration node.
2464 * no returned results
2466 void fit_conf_print (const void *fit, int noffset, const char *p)
2472 /* Mandatory properties */
2473 ret = fit_get_desc (fit, noffset, &desc);
2474 printf ("%s Description: ", p);
2476 printf ("unavailable\n");
2478 printf ("%s\n", desc);
2480 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2481 printf ("%s Kernel: ", p);
2483 printf ("unavailable\n");
2485 printf ("%s\n", uname);
2487 /* Optional properties */
2488 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2490 printf ("%s Init Ramdisk: %s\n", p, uname);
2492 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2494 printf ("%s FDT: %s\n", p, uname);
2498 * fit_check_ramdisk - verify FIT format ramdisk subimage
2499 * @fit_hdr: pointer to the FIT ramdisk header
2500 * @rd_noffset: ramdisk subimage node offset within FIT image
2501 * @arch: requested ramdisk image architecture type
2502 * @verify: data CRC verification flag
2504 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2505 * specified FIT image.
2512 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2514 fit_image_print (fit, rd_noffset, " ");
2517 puts (" Verifying Hash Integrity ... ");
2518 if (!fit_image_check_hashes (fit, rd_noffset)) {
2519 puts ("Bad Data Hash\n");
2520 show_boot_progress (-125);
2526 show_boot_progress (126);
2527 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2528 !fit_image_check_arch (fit, rd_noffset, arch) ||
2529 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2530 printf ("No Linux %s Ramdisk Image\n",
2531 genimg_get_arch_name(arch));
2532 show_boot_progress (-126);
2536 show_boot_progress (127);
2539 #endif /* USE_HOSTCC */
2540 #endif /* CONFIG_FIT */