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)
53 #include <fdt_support.h>
58 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
61 DECLARE_GLOBAL_DATA_PTR;
63 static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
64 int argc, char *argv[],
65 ulong rd_addr, uint8_t arch, int verify);
70 #endif /* !USE_HOSTCC*/
72 typedef struct table_entry {
73 int id; /* as defined in image.h */
74 char *sname; /* short (input) name */
75 char *lname; /* long (output) name */
78 static table_entry_t uimage_arch[] = {
79 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
80 { IH_ARCH_ALPHA, "alpha", "Alpha", },
81 { IH_ARCH_ARM, "arm", "ARM", },
82 { IH_ARCH_I386, "x86", "Intel x86", },
83 { IH_ARCH_IA64, "ia64", "IA64", },
84 { IH_ARCH_M68K, "m68k", "M68K", },
85 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
86 { IH_ARCH_MIPS, "mips", "MIPS", },
87 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
88 { IH_ARCH_NIOS, "nios", "NIOS", },
89 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
90 { IH_ARCH_PPC, "ppc", "PowerPC", },
91 { IH_ARCH_S390, "s390", "IBM S390", },
92 { IH_ARCH_SH, "sh", "SuperH", },
93 { IH_ARCH_SPARC, "sparc", "SPARC", },
94 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
95 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
96 { IH_ARCH_AVR32, "avr32", "AVR32", },
100 static table_entry_t uimage_os[] = {
101 { IH_OS_INVALID, NULL, "Invalid OS", },
102 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
103 { IH_OS_ARTOS, "artos", "ARTOS", },
105 { IH_OS_LINUX, "linux", "Linux", },
106 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
107 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
109 { IH_OS_NETBSD, "netbsd", "NetBSD", },
110 { IH_OS_RTEMS, "rtems", "RTEMS", },
111 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
112 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
113 { IH_OS_QNX, "qnx", "QNX", },
114 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
117 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
118 { IH_OS_DELL, "dell", "Dell", },
119 { IH_OS_ESIX, "esix", "Esix", },
120 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
121 { IH_OS_IRIX, "irix", "Irix", },
122 { IH_OS_NCR, "ncr", "NCR", },
123 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
124 { IH_OS_PSOS, "psos", "pSOS", },
125 { IH_OS_SCO, "sco", "SCO", },
126 { IH_OS_SOLARIS, "solaris", "Solaris", },
127 { IH_OS_SVR4, "svr4", "SVR4", },
132 static table_entry_t uimage_type[] = {
133 { IH_TYPE_INVALID, NULL, "Invalid Image", },
134 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
135 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
136 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
137 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
138 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
139 { IH_TYPE_SCRIPT, "script", "Script", },
140 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
141 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
145 static table_entry_t uimage_comp[] = {
146 { IH_COMP_NONE, "none", "uncompressed", },
147 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
148 { IH_COMP_GZIP, "gzip", "gzip compressed", },
152 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
153 static void genimg_print_size (uint32_t size);
154 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
155 static void genimg_print_time (time_t timestamp);
158 /*****************************************************************************/
159 /* Legacy format routines */
160 /*****************************************************************************/
161 int image_check_hcrc (image_header_t *hdr)
164 ulong len = image_get_header_size ();
165 image_header_t header;
167 /* Copy header so we can blank CRC field for re-calculation */
168 memmove (&header, (char *)hdr, image_get_header_size ());
169 image_set_hcrc (&header, 0);
171 hcrc = crc32 (0, (unsigned char *)&header, len);
173 return (hcrc == image_get_hcrc (hdr));
176 int image_check_dcrc (image_header_t *hdr)
178 ulong data = image_get_data (hdr);
179 ulong len = image_get_data_size (hdr);
180 ulong dcrc = crc32 (0, (unsigned char *)data, len);
182 return (dcrc == image_get_dcrc (hdr));
186 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
189 ulong len = image_get_data_size (hdr);
190 ulong data = image_get_data (hdr);
192 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
194 ulong edata = cdata + len;
196 while (cdata < edata) {
197 ulong chunk = edata - cdata;
201 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
207 dcrc = crc32 (0, (unsigned char *)data, len);
210 return (dcrc == image_get_dcrc (hdr));
212 #endif /* !USE_HOSTCC */
215 * image_multi_count - get component (sub-image) count
216 * @hdr: pointer to the header of the multi component image
218 * image_multi_count() returns number of components in a multi
221 * Note: no checking of the image type is done, caller must pass
222 * a valid multi component image.
225 * number of components
227 ulong image_multi_count (image_header_t *hdr)
232 /* get start of the image payload, which in case of multi
233 * component images that points to a table of component sizes */
234 size = (uint32_t *)image_get_data (hdr);
236 /* count non empty slots */
237 for (i = 0; size[i]; ++i)
244 * image_multi_getimg - get component data address and size
245 * @hdr: pointer to the header of the multi component image
246 * @idx: index of the requested component
247 * @data: pointer to a ulong variable, will hold component data address
248 * @len: pointer to a ulong variable, will hold component size
250 * image_multi_getimg() returns size and data address for the requested
251 * component in a multi component image.
253 * Note: no checking of the image type is done, caller must pass
254 * a valid multi component image.
257 * data address and size of the component, if idx is valid
258 * 0 in data and len, if idx is out of range
260 void image_multi_getimg (image_header_t *hdr, ulong idx,
261 ulong *data, ulong *len)
265 ulong offset, tail, count, img_data;
267 /* get number of component */
268 count = image_multi_count (hdr);
270 /* get start of the image payload, which in case of multi
271 * component images that points to a table of component sizes */
272 size = (uint32_t *)image_get_data (hdr);
274 /* get address of the proper component data start, which means
275 * skipping sizes table (add 1 for last, null entry) */
276 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
279 *len = uimage_to_cpu (size[idx]);
283 /* go over all indices preceding requested component idx */
284 for (i = 0; i < idx; i++) {
285 /* add up i-th component size */
286 offset += uimage_to_cpu (size[i]);
288 /* add up alignment for i-th component */
289 tail += (4 - uimage_to_cpu (size[i]) % 4);
292 /* calculate idx-th component data address */
293 *data = img_data + offset + tail;
300 static void image_print_type (image_header_t *hdr)
302 const char *os, *arch, *type, *comp;
304 os = genimg_get_os_name (image_get_os (hdr));
305 arch = genimg_get_arch_name (image_get_arch (hdr));
306 type = genimg_get_type_name (image_get_type (hdr));
307 comp = genimg_get_comp_name (image_get_comp (hdr));
309 printf ("%s %s %s (%s)\n", arch, os, type, comp);
313 * __image_print_contents - prints out the contents of the legacy format image
314 * @hdr: pointer to the legacy format image header
315 * @p: pointer to prefix string
317 * __image_print_contents() formats a multi line legacy image contents description.
318 * The routine prints out all header fields followed by the size/offset data
319 * for MULTI/SCRIPT images.
322 * no returned results
324 static void __image_print_contents (image_header_t *hdr, const char *p)
326 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
327 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
328 printf ("%sCreated: ", p);
329 genimg_print_time ((time_t)image_get_time (hdr));
331 printf ("%sImage Type: ", p);
332 image_print_type (hdr);
333 printf ("%sData Size: ", p);
334 genimg_print_size (image_get_data_size (hdr));
335 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
336 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
338 if (image_check_type (hdr, IH_TYPE_MULTI) ||
339 image_check_type (hdr, IH_TYPE_SCRIPT)) {
342 ulong count = image_multi_count (hdr);
344 printf ("%sContents:\n", p);
345 for (i = 0; i < count; i++) {
346 image_multi_getimg (hdr, i, &data, &len);
348 printf ("%s Image %d: ", p, i);
349 genimg_print_size (len);
351 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
353 * the user may need to know offsets
354 * if planning to do something with
357 printf ("%s Offset = 0x%08lx\n", p, data);
363 inline void image_print_contents (image_header_t *hdr)
365 __image_print_contents (hdr, " ");
368 inline void image_print_contents_noindent (image_header_t *hdr)
370 __image_print_contents (hdr, "");
375 * image_get_ramdisk - get and verify ramdisk image
376 * @cmdtp: command table pointer
377 * @flag: command flag
378 * @argc: command argument count
379 * @argv: command argument list
380 * @rd_addr: ramdisk image start address
381 * @arch: expected ramdisk architecture
382 * @verify: checksum verification flag
384 * image_get_ramdisk() returns a pointer to the verified ramdisk image
385 * header. Routine receives image start address and expected architecture
386 * flag. Verification done covers data and header integrity and os/type/arch
389 * If dataflash support is enabled routine checks for dataflash addresses
390 * and handles required dataflash reads.
393 * pointer to a ramdisk image header, if image was found and valid
394 * otherwise, return NULL
396 static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
397 int argc, char *argv[],
398 ulong rd_addr, uint8_t arch, int verify)
400 image_header_t *rd_hdr;
402 show_boot_progress (9);
403 rd_hdr = (image_header_t *)rd_addr;
405 if (!image_check_magic (rd_hdr)) {
406 puts ("Bad Magic Number\n");
407 show_boot_progress (-10);
411 if (!image_check_hcrc (rd_hdr)) {
412 puts ("Bad Header Checksum\n");
413 show_boot_progress (-11);
417 show_boot_progress (10);
418 image_print_contents (rd_hdr);
421 puts(" Verifying Checksum ... ");
422 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
423 puts ("Bad Data CRC\n");
424 show_boot_progress (-12);
430 show_boot_progress (11);
432 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
433 !image_check_arch (rd_hdr, arch) ||
434 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
435 printf ("No Linux %s Ramdisk Image\n",
436 genimg_get_arch_name(arch));
437 show_boot_progress (-13);
443 #endif /* !USE_HOSTCC */
445 /*****************************************************************************/
446 /* Shared dual-format routines */
447 /*****************************************************************************/
449 int getenv_verify (void)
451 char *s = getenv ("verify");
452 return (s && (*s == 'n')) ? 0 : 1;
455 int getenv_autostart (void)
457 char *s = getenv ("autostart");
458 return (s && (*s == 'n')) ? 0 : 1;
461 ulong getenv_bootm_low(void)
463 char *s = getenv ("bootm_low");
465 ulong tmp = simple_strtoul (s, NULL, 16);
469 #ifdef CFG_SDRAM_BASE
470 return CFG_SDRAM_BASE;
476 ulong getenv_bootm_size(void)
478 char *s = getenv ("bootm_size");
480 ulong tmp = simple_strtoul (s, NULL, 16);
484 return gd->bd->bi_memsize;
487 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
489 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
491 size_t tail = (len > chunksz) ? chunksz : len;
493 memmove (to, from, tail);
498 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
499 memmove (to, from, len);
500 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
502 #endif /* !USE_HOSTCC */
504 static void genimg_print_size (uint32_t size)
507 printf ("%d Bytes = ", size);
508 print_size (size, "\n");
510 printf ("%d Bytes = %.2f kB = %.2f MB\n",
511 size, (double)size / 1.024e3,
512 (double)size / 1.048576e6);
516 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
517 static void genimg_print_time (time_t timestamp)
522 to_tm (timestamp, &tm);
523 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
524 tm.tm_year, tm.tm_mon, tm.tm_mday,
525 tm.tm_hour, tm.tm_min, tm.tm_sec);
527 printf ("%s", ctime(×tamp));
530 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
533 * get_table_entry_name - translate entry id to long name
534 * @table: pointer to a translation table for entries of a specific type
535 * @msg: message to be returned when translation fails
536 * @id: entry id to be translated
538 * get_table_entry_name() will go over translation table trying to find
539 * entry that matches given id. If matching entry is found, its long
540 * name is returned to the caller.
543 * long entry name if translation succeeds
546 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
548 for (; table->id >= 0; ++table) {
550 return (table->lname);
555 const char *genimg_get_os_name (uint8_t os)
557 return (get_table_entry_name (uimage_os, "Unknown OS", os));
560 const char *genimg_get_arch_name (uint8_t arch)
562 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
565 const char *genimg_get_type_name (uint8_t type)
567 return (get_table_entry_name (uimage_type, "Unknown Image", type));
570 const char *genimg_get_comp_name (uint8_t comp)
572 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
576 * get_table_entry_id - translate short entry name to id
577 * @table: pointer to a translation table for entries of a specific type
578 * @table_name: to be used in case of error
579 * @name: entry short name to be translated
581 * get_table_entry_id() will go over translation table trying to find
582 * entry that matches given short name. If matching entry is found,
583 * its id returned to the caller.
586 * entry id if translation succeeds
589 static int get_table_entry_id (table_entry_t *table,
590 const char *table_name, const char *name)
596 for (t = table; t->id >= 0; ++t) {
597 if (t->sname && strcasecmp(t->sname, name) == 0)
601 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
602 for (t = table; t->id >= 0; ++t) {
603 if (t->sname == NULL)
605 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
608 fprintf (stderr, "\n");
610 for (t = table; t->id >= 0; ++t) {
611 if (t->sname && strcmp(t->sname, name) == 0)
614 debug ("Invalid %s Type: %s\n", table_name, name);
615 #endif /* USE_HOSTCC */
619 int genimg_get_os_id (const char *name)
621 return (get_table_entry_id (uimage_os, "OS", name));
624 int genimg_get_arch_id (const char *name)
626 return (get_table_entry_id (uimage_arch, "CPU", name));
629 int genimg_get_type_id (const char *name)
631 return (get_table_entry_id (uimage_type, "Image", name));
634 int genimg_get_comp_id (const char *name)
636 return (get_table_entry_id (uimage_comp, "Compression", name));
641 * genimg_get_format - get image format type
642 * @img_addr: image start address
644 * genimg_get_format() checks whether provided address points to a valid
645 * legacy or FIT image.
647 * New uImage format and FDT blob are based on a libfdt. FDT blob
648 * may be passed directly or embedded in a FIT image. In both situations
649 * genimg_get_format() must be able to dectect libfdt header.
652 * image format type or IMAGE_FORMAT_INVALID if no image is present
654 int genimg_get_format (void *img_addr)
656 ulong format = IMAGE_FORMAT_INVALID;
658 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
662 hdr = (image_header_t *)img_addr;
663 if (image_check_magic(hdr))
664 format = IMAGE_FORMAT_LEGACY;
665 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
667 fit_hdr = (char *)img_addr;
668 if (fdt_check_header (fit_hdr) == 0)
669 format = IMAGE_FORMAT_FIT;
677 * genimg_get_image - get image from special storage (if necessary)
678 * @img_addr: image start address
680 * genimg_get_image() checks if provided image start adddress is located
681 * in a dataflash storage. If so, image is moved to a system RAM memory.
684 * image start address after possible relocation from special storage
686 ulong genimg_get_image (ulong img_addr)
688 ulong ram_addr = img_addr;
690 #ifdef CONFIG_HAS_DATAFLASH
691 ulong h_size, d_size;
693 if (addr_dataflash (img_addr)){
694 /* ger RAM address */
695 ram_addr = CFG_LOAD_ADDR;
697 /* get header size */
698 h_size = image_get_header_size ();
699 #if defined(CONFIG_FIT)
700 if (sizeof(struct fdt_header) > h_size)
701 h_size = sizeof(struct fdt_header);
705 debug (" Reading image header from dataflash address "
706 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
708 read_dataflash (img_addr, h_size, (char *)ram_addr);
711 switch (genimg_get_format ((void *)ram_addr)) {
712 case IMAGE_FORMAT_LEGACY:
713 d_size = image_get_data_size ((image_header_t *)ram_addr);
714 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
717 #if defined(CONFIG_FIT)
718 case IMAGE_FORMAT_FIT:
719 d_size = fit_get_size ((const void *)ram_addr) - h_size;
720 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
725 printf (" No valid image found at 0x%08lx\n", img_addr);
729 /* read in image data */
730 debug (" Reading image remaining data from dataflash address "
731 "%08lx to RAM address %08lx\n", img_addr + h_size,
734 read_dataflash (img_addr + h_size, d_size,
735 (char *)(ram_addr + h_size));
738 #endif /* CONFIG_HAS_DATAFLASH */
744 * boot_get_ramdisk - main ramdisk handling routine
745 * @cmdtp: command table pointer
746 * @flag: command flag
747 * @argc: command argument count
748 * @argv: command argument list
749 * @images: pointer to the bootm images structure
750 * @arch: expected ramdisk architecture
751 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
752 * @rd_end: pointer to a ulong variable, will hold ramdisk end
754 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
755 * Curently supported are the following ramdisk sources:
756 * - multicomponent kernel/ramdisk image,
757 * - commandline provided address of decicated ramdisk image.
760 * rd_start and rd_end are set to ramdisk start/end addresses if
761 * ramdisk image is found and valid
762 * rd_start and rd_end are set to 0 if no ramdisk exists
763 * return 1 if ramdisk image is found but corrupted
765 int boot_get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
766 bootm_headers_t *images, uint8_t arch,
767 ulong *rd_start, ulong *rd_end)
769 ulong rd_addr, rd_load;
770 ulong rd_data, rd_len;
771 image_header_t *rd_hdr;
772 #if defined(CONFIG_FIT)
774 const char *fit_uname_config = NULL;
775 const char *fit_uname_ramdisk = NULL;
780 * Look for a '-' which indicates to ignore the
783 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
784 debug ("## Skipping init Ramdisk\n");
785 rd_len = rd_data = 0;
786 } else if (argc >= 3) {
787 #if defined(CONFIG_FIT)
789 * If the init ramdisk comes from the FIT image and the FIT image
790 * address is omitted in the command line argument, try to use
791 * os FIT image address or default load address.
793 if (images->fit_uname_os)
794 default_addr = (ulong)images->fit_hdr_os;
796 default_addr = load_addr;
798 if (fit_parse_conf (argv[2], default_addr,
799 &rd_addr, &fit_uname_config)) {
800 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
801 fit_uname_config, rd_addr);
802 } else if (fit_parse_subimage (argv[2], default_addr,
803 &rd_addr, &fit_uname_ramdisk)) {
804 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
805 fit_uname_ramdisk, rd_addr);
809 rd_addr = simple_strtoul(argv[2], NULL, 16);
810 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
814 /* copy from dataflash if needed */
815 printf ("## Loading init Ramdisk Image at %08lx ...\n",
817 rd_addr = genimg_get_image (rd_addr);
820 * Check if there is an initrd image at the
821 * address provided in the second bootm argument
822 * check image type, for FIT images get FIT node.
824 switch (genimg_get_format ((void *)rd_addr)) {
825 case IMAGE_FORMAT_LEGACY:
827 debug ("* ramdisk: legacy format image\n");
829 rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv,
830 rd_addr, arch, images->verify);
832 if (rd_hdr == NULL) {
838 rd_data = image_get_data (rd_hdr);
839 rd_len = image_get_data_size (rd_hdr);
840 rd_load = image_get_load (rd_hdr);
842 #if defined(CONFIG_FIT)
843 case IMAGE_FORMAT_FIT:
844 fit_hdr = (void *)rd_addr;
845 debug ("* ramdisk: FIT format image\n");
846 fit_unsupported_reset ("ramdisk");
850 printf ("Wrong Image Format for %s command\n",
852 rd_data = rd_len = 0;
855 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
857 * We need to copy the ramdisk to SRAM to let Linux boot
860 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
863 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
865 } else if (images->legacy_hdr_valid &&
866 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
868 * Now check if we have a legacy mult-component image,
869 * get second entry data start address and len.
871 show_boot_progress (13);
872 printf ("## Loading init Ramdisk from multi component "
873 "Image at %08lx ...\n",
874 (ulong)images->legacy_hdr_os);
876 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
881 show_boot_progress (14);
882 rd_len = rd_data = 0;
886 debug ("## No init Ramdisk\n");
891 *rd_end = rd_data + rd_len;
893 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
899 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
901 * boot_ramdisk_high - relocate init ramdisk
902 * @lmb: pointer to lmb handle, will be used for memory mgmt
903 * @rd_data: ramdisk data start address
904 * @rd_len: ramdisk data length
905 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
906 * start address (after possible relocation)
907 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
908 * end address (after possible relocation)
910 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
911 * variable and if requested ramdisk data is moved to a specified location.
913 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
914 * start/end addresses if ramdisk image start and len were provided,
915 * otherwise set initrd_start and initrd_end set to zeros.
921 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
922 ulong *initrd_start, ulong *initrd_end)
926 int initrd_copy_to_ram = 1;
928 if ((s = getenv ("initrd_high")) != NULL) {
929 /* a value of "no" or a similar string will act like 0,
930 * turning the "load high" feature off. This is intentional.
932 initrd_high = simple_strtoul (s, NULL, 16);
933 if (initrd_high == ~0)
934 initrd_copy_to_ram = 0;
936 /* not set, no restrictions to load high */
940 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
941 initrd_high, initrd_copy_to_ram);
944 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
945 debug (" in-place initrd\n");
946 *initrd_start = rd_data;
947 *initrd_end = rd_data + rd_len;
948 lmb_reserve(lmb, rd_data, rd_len);
951 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
953 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
955 if (*initrd_start == 0) {
956 puts ("ramdisk - allocation error\n");
959 show_boot_progress (12);
961 *initrd_end = *initrd_start + rd_len;
962 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
963 *initrd_start, *initrd_end);
965 memmove_wd ((void *)*initrd_start,
966 (void *)rd_data, rd_len, CHUNKSZ);
974 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
975 *initrd_start, *initrd_end);
984 * boot_get_cmdline - allocate and initialize kernel cmdline
985 * @lmb: pointer to lmb handle, will be used for memory mgmt
986 * @cmd_start: pointer to a ulong variable, will hold cmdline start
987 * @cmd_end: pointer to a ulong variable, will hold cmdline end
988 * @bootmap_base: ulong variable, holds offset in physical memory to
991 * boot_get_cmdline() allocates space for kernel command line below
992 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
993 * variable is present its contents is copied to allocated kernel
1000 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1006 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1007 CFG_BOOTMAPSZ + bootmap_base);
1009 if (cmdline == NULL)
1012 if ((s = getenv("bootargs")) == NULL)
1017 *cmd_start = (ulong) & cmdline[0];
1018 *cmd_end = *cmd_start + strlen(cmdline);
1020 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1026 * boot_get_kbd - allocate and initialize kernel copy of board info
1027 * @lmb: pointer to lmb handle, will be used for memory mgmt
1028 * @kbd: double pointer to board info data
1029 * @bootmap_base: ulong variable, holds offset in physical memory to
1032 * boot_get_kbd() allocates space for kernel copy of board info data below
1033 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1034 * the current u-boot board info data.
1040 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1042 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1043 CFG_BOOTMAPSZ + bootmap_base);
1049 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1051 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1052 do_bdinfo(NULL, 0, 0, NULL);
1057 #endif /* CONFIG_PPC || CONFIG_M68K */
1058 #endif /* !USE_HOSTCC */
1060 #if defined(CONFIG_FIT)
1061 /*****************************************************************************/
1062 /* New uImage format routines */
1063 /*****************************************************************************/
1065 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1066 ulong *addr, const char **name)
1073 sep = strchr (spec, sepc);
1076 *addr = simple_strtoul (spec, NULL, 16);
1086 * fit_parse_conf - parse FIT configuration spec
1087 * @spec: input string, containing configuration spec
1088 * @add_curr: current image address (to be used as a possible default)
1089 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1091 * @conf_name double pointer to a char, will hold pointer to a configuration
1094 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1095 * where <addr> is a FIT image address that contains configuration
1096 * with a <conf> unit name.
1098 * Address part is optional, and if omitted default add_curr will
1102 * 1 if spec is a valid configuration string,
1103 * addr and conf_name are set accordingly
1106 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1107 ulong *addr, const char **conf_name)
1109 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1113 * fit_parse_subimage - parse FIT subimage spec
1114 * @spec: input string, containing subimage spec
1115 * @add_curr: current image address (to be used as a possible default)
1116 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1118 * @image_name: double pointer to a char, will hold pointer to a subimage name
1120 * fit_parse_subimage() expects subimage spec in the for of
1121 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1122 * subimage with a <subimg> unit name.
1124 * Address part is optional, and if omitted default add_curr will
1128 * 1 if spec is a valid subimage string,
1129 * addr and image_name are set accordingly
1132 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1133 ulong *addr, const char **image_name)
1135 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1137 #endif /* !USE_HOSTCC */
1139 static void fit_get_debug (const void *fit, int noffset,
1140 char *prop_name, int err)
1142 debug ("Can't get '%s' property from FIT 0x%08lx, "
1143 "node: offset %d, name %s (%s)\n",
1144 prop_name, (ulong)fit, noffset,
1145 fit_get_name (fit, noffset, NULL),
1146 fdt_strerror (err));
1150 * __fit_print_contents - prints out the contents of the FIT format image
1151 * @fit: pointer to the FIT format image header
1152 * @p: pointer to prefix string
1154 * __fit_print_contents() formats a multi line FIT image contents description.
1155 * The routine prints out FIT image properties (root node level) follwed by
1156 * the details of each component image.
1159 * no returned results
1161 static void __fit_print_contents (const void *fit, const char *p)
1171 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1175 /* Root node properties */
1176 ret = fit_get_desc (fit, 0, &desc);
1177 printf ("%sFIT description: ", p);
1179 printf ("unavailable\n");
1181 printf ("%s\n", desc);
1183 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1184 ret = fit_get_timestamp (fit, 0, ×tamp);
1185 printf ("%sCreated: ", p);
1187 printf ("unavailable\n");
1189 genimg_print_time (timestamp);
1192 /* Find images parent node offset */
1193 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1194 if (images_noffset < 0) {
1195 printf ("Can't find images parent node '%s' (%s)\n",
1196 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1200 /* Process its subnodes, print out component images details */
1201 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1202 (noffset >= 0) && (ndepth > 0);
1203 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1206 * Direct child node of the images parent node,
1207 * i.e. component image node.
1209 printf ("%s Image %u (%s)\n", p, count++,
1210 fit_get_name(fit, noffset, NULL));
1212 fit_image_print (fit, noffset, p);
1216 /* Find configurations parent node offset */
1217 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1218 if (confs_noffset < 0) {
1219 debug ("Can't get configurations parent node '%s' (%s)\n",
1220 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1224 /* get default configuration unit name from default property */
1225 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1227 printf ("%s Default Configuration: '%s'\n", p, uname);
1229 /* Process its subnodes, print out configurations details */
1230 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1231 (noffset >= 0) && (ndepth > 0);
1232 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1235 * Direct child node of the configurations parent node,
1236 * i.e. configuration node.
1238 printf ("%s Configuration %u (%s)\n", p, count++,
1239 fit_get_name(fit, noffset, NULL));
1241 fit_conf_print (fit, noffset, p);
1246 inline void fit_print_contents (const void *fit)
1248 __fit_print_contents (fit, " ");
1251 inline void fit_print_contents_noindent (const void *fit)
1253 __fit_print_contents (fit, "");
1257 * fit_image_print - prints out the FIT component image details
1258 * @fit: pointer to the FIT format image header
1259 * @image_noffset: offset of the component image node
1260 * @p: pointer to prefix string
1262 * fit_image_print() lists all mandatory properies for the processed component
1263 * image. If present, hash nodes are printed out as well.
1266 * no returned results
1268 void fit_image_print (const void *fit, int image_noffset, const char *p)
1271 uint8_t type, arch, os, comp;
1279 /* Mandatory properties */
1280 ret = fit_get_desc (fit, image_noffset, &desc);
1281 printf ("%s Description: ", p);
1283 printf ("unavailable\n");
1285 printf ("%s\n", desc);
1287 fit_image_get_type (fit, image_noffset, &type);
1288 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1290 fit_image_get_comp (fit, image_noffset, &comp);
1291 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1293 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1296 printf ("%s Data Start: ", p);
1298 printf ("unavailable\n");
1300 printf ("0x%08lx\n", (ulong)data);
1303 printf ("%s Data Size: ", p);
1305 printf ("unavailable\n");
1307 genimg_print_size (size);
1309 /* Remaining, type dependent properties */
1310 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1311 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1312 (type == IH_TYPE_FLATDT)) {
1313 fit_image_get_arch (fit, image_noffset, &arch);
1314 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1317 if (type == IH_TYPE_KERNEL) {
1318 fit_image_get_os (fit, image_noffset, &os);
1319 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1322 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1323 ret = fit_image_get_load (fit, image_noffset, &load);
1324 printf ("%s Load Address: ", p);
1326 printf ("unavailable\n");
1328 printf ("0x%08lx\n", load);
1330 fit_image_get_entry (fit, image_noffset, &entry);
1331 printf ("%s Entry Point: ", p);
1333 printf ("unavailable\n");
1335 printf ("0x%08lx\n", entry);
1338 /* Process all hash subnodes of the component image node */
1339 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1340 (noffset >= 0) && (ndepth > 0);
1341 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1343 /* Direct child node of the component image node */
1344 fit_image_print_hash (fit, noffset, p);
1350 * fit_image_print_hash - prints out the hash node details
1351 * @fit: pointer to the FIT format image header
1352 * @noffset: offset of the hash node
1353 * @p: pointer to prefix string
1355 * fit_image_print_hash() lists properies for the processed hash node
1358 * no returned results
1360 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1368 * Check subnode name, must be equal to "hash".
1369 * Multiple hash nodes require unique unit node
1370 * names, e.g. hash@1, hash@2, etc.
1372 if (strncmp (fit_get_name(fit, noffset, NULL),
1374 strlen(FIT_HASH_NODENAME)) != 0)
1377 debug ("%s Hash node: '%s'\n", p,
1378 fit_get_name (fit, noffset, NULL));
1380 printf ("%s Hash algo: ", p);
1381 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1382 printf ("invalid/unsupported\n");
1385 printf ("%s\n", algo);
1387 ret = fit_image_hash_get_value (fit, noffset, &value,
1389 printf ("%s Hash value: ", p);
1391 printf ("unavailable\n");
1393 for (i = 0; i < value_len; i++)
1394 printf ("%02x", value[i]);
1398 debug ("%s Hash len: %d\n", p, value_len);
1402 * fit_get_desc - get node description property
1403 * @fit: pointer to the FIT format image header
1404 * @noffset: node offset
1405 * @desc: double pointer to the char, will hold pointer to the descrption
1407 * fit_get_desc() reads description property from a given node, if
1408 * description is found pointer to it is returened in third call argument.
1414 int fit_get_desc (const void *fit, int noffset, char **desc)
1418 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1419 if (*desc == NULL) {
1420 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1428 * fit_get_timestamp - get node timestamp property
1429 * @fit: pointer to the FIT format image header
1430 * @noffset: node offset
1431 * @timestamp: pointer to the time_t, will hold read timestamp
1433 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1434 * is found and has a correct size its value is retured in third call
1439 * -1, on property read failure
1440 * -2, on wrong timestamp size
1442 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1447 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1449 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1452 if (len != sizeof (uint32_t)) {
1453 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1457 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1462 * fit_image_get_node - get node offset for component image of a given unit name
1463 * @fit: pointer to the FIT format image header
1464 * @image_uname: component image node unit name
1466 * fit_image_get_node() finds a component image (withing the '/images'
1467 * node) of a provided unit name. If image is found its node offset is
1468 * returned to the caller.
1471 * image node offset when found (>=0)
1472 * negative number on failure (FDT_ERR_* code)
1474 int fit_image_get_node (const void *fit, const char *image_uname)
1476 int noffset, images_noffset;
1478 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1479 if (images_noffset < 0) {
1480 debug ("Can't find images parent node '%s' (%s)\n",
1481 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1482 return images_noffset;
1485 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1487 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1488 image_uname, fdt_strerror (noffset));
1495 * fit_image_get_os - get os id for a given component image node
1496 * @fit: pointer to the FIT format image header
1497 * @noffset: component image node offset
1498 * @os: pointer to the uint8_t, will hold os numeric id
1500 * fit_image_get_os() finds os property in a given component image node.
1501 * If the property is found, its (string) value is translated to the numeric
1502 * id which is returned to the caller.
1508 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1513 /* Get OS name from property data */
1514 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1516 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1521 /* Translate OS name to id */
1522 *os = genimg_get_os_id (data);
1527 * fit_image_get_arch - get arch id for a given component image node
1528 * @fit: pointer to the FIT format image header
1529 * @noffset: component image node offset
1530 * @arch: pointer to the uint8_t, will hold arch numeric id
1532 * fit_image_get_arch() finds arch property in a given component image node.
1533 * If the property is found, its (string) value is translated to the numeric
1534 * id which is returned to the caller.
1540 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1545 /* Get architecture name from property data */
1546 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1548 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1553 /* Translate architecture name to id */
1554 *arch = genimg_get_arch_id (data);
1559 * fit_image_get_type - get type id for a given component image node
1560 * @fit: pointer to the FIT format image header
1561 * @noffset: component image node offset
1562 * @type: pointer to the uint8_t, will hold type numeric id
1564 * fit_image_get_type() finds type property in a given component image node.
1565 * If the property is found, its (string) value is translated to the numeric
1566 * id which is returned to the caller.
1572 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1577 /* Get image type name from property data */
1578 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1580 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1585 /* Translate image type name to id */
1586 *type = genimg_get_type_id (data);
1591 * fit_image_get_comp - get comp id for a given component image node
1592 * @fit: pointer to the FIT format image header
1593 * @noffset: component image node offset
1594 * @comp: pointer to the uint8_t, will hold comp numeric id
1596 * fit_image_get_comp() finds comp property in a given component image node.
1597 * If the property is found, its (string) value is translated to the numeric
1598 * id which is returned to the caller.
1604 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1609 /* Get compression name from property data */
1610 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1612 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1617 /* Translate compression name to id */
1618 *comp = genimg_get_comp_id (data);
1623 * fit_image_get_load - get load address property for a given component image node
1624 * @fit: pointer to the FIT format image header
1625 * @noffset: component image node offset
1626 * @load: pointer to the uint32_t, will hold load address
1628 * fit_image_get_load() finds load address property in a given component image node.
1629 * If the property is found, its value is returned to the caller.
1635 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1638 const uint32_t *data;
1640 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1642 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1646 *load = uimage_to_cpu (*data);
1651 * fit_image_get_entry - get entry point address property for a given component image node
1652 * @fit: pointer to the FIT format image header
1653 * @noffset: component image node offset
1654 * @entry: pointer to the uint32_t, will hold entry point address
1656 * fit_image_get_entry() finds entry point address property in a given component image node.
1657 * If the property is found, its value is returned to the caller.
1663 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1666 const uint32_t *data;
1668 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1670 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1674 *entry = uimage_to_cpu (*data);
1679 * fit_image_get_data - get data property and its size for a given component image node
1680 * @fit: pointer to the FIT format image header
1681 * @noffset: component image node offset
1682 * @data: double pointer to void, will hold data property's data address
1683 * @size: pointer to size_t, will hold data property's data size
1685 * fit_image_get_data() finds data property in a given component image node.
1686 * If the property is found its data start address and size are returned to
1693 int fit_image_get_data (const void *fit, int noffset,
1694 const void **data, size_t *size)
1698 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1699 if (*data == NULL) {
1700 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1710 * fit_image_hash_get_algo - get hash algorithm name
1711 * @fit: pointer to the FIT format image header
1712 * @noffset: hash node offset
1713 * @algo: double pointer to char, will hold pointer to the algorithm name
1715 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1716 * If the property is found its data start address is returned to the caller.
1722 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1726 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1727 if (*algo == NULL) {
1728 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1736 * fit_image_hash_get_value - get hash value and length
1737 * @fit: pointer to the FIT format image header
1738 * @noffset: hash node offset
1739 * @value: double pointer to uint8_t, will hold address of a hash value data
1740 * @value_len: pointer to an int, will hold hash data length
1742 * fit_image_hash_get_value() finds hash value property in a given hash node.
1743 * If the property is found its data start address and size are returned to
1750 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1755 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1756 if (*value == NULL) {
1757 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1767 * fit_set_timestamp - set node timestamp property
1768 * @fit: pointer to the FIT format image header
1769 * @noffset: node offset
1770 * @timestamp: timestamp value to be set
1772 * fit_set_timestamp() attempts to set timestamp property in the requested
1773 * node and returns operation status to the caller.
1777 * -1, on property read failure
1779 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1784 t = cpu_to_uimage (timestamp);
1785 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1788 printf ("Can't set '%s' property for '%s' node (%s)\n",
1789 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1790 fdt_strerror (ret));
1798 * calculate_hash - calculate and return hash for provided input data
1799 * @data: pointer to the input data
1800 * @data_len: data length
1801 * @algo: requested hash algorithm
1802 * @value: pointer to the char, will hold hash value data (caller must
1803 * allocate enough free space)
1804 * value_len: length of the calculated hash
1806 * calculate_hash() computes input data hash according to the requested algorithm.
1807 * Resulting hash value is placed in caller provided 'value' buffer, length
1808 * of the calculated hash is returned via value_len pointer argument.
1812 * -1, when algo is unsupported
1814 static int calculate_hash (const void *data, int data_len, const char *algo,
1815 uint8_t *value, int *value_len)
1817 if (strcmp (algo, "crc32") == 0 ) {
1818 *((uint32_t *)value) = crc32 (0, data, data_len);
1819 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1821 } else if (strcmp (algo, "sha1") == 0 ) {
1822 sha1_csum ((unsigned char *) data, data_len,
1823 (unsigned char *) value);
1825 } else if (strcmp (algo, "md5") == 0 ) {
1826 printf ("MD5 not supported\n");
1829 debug ("Unsupported hash alogrithm\n");
1837 * fit_set_hashes - process FIT component image nodes and calculate hashes
1838 * @fit: pointer to the FIT format image header
1840 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1841 * Hashes are calculated for all component images which have hash subnodes
1842 * with algorithm property set to one of the supported hash algorithms.
1846 * libfdt error code, on failure
1848 int fit_set_hashes (void *fit)
1855 /* Find images parent node offset */
1856 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1857 if (images_noffset < 0) {
1858 printf ("Can't find images parent node '%s' (%s)\n",
1859 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1860 return images_noffset;
1863 /* Process its subnodes, print out component images details */
1864 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1865 (noffset >= 0) && (ndepth > 0);
1866 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1869 * Direct child node of the images parent node,
1870 * i.e. component image node.
1872 ret = fit_image_set_hashes (fit, noffset);
1882 * fit_image_set_hashes - calculate/set hashes for given component image node
1883 * @fit: pointer to the FIT format image header
1884 * @image_noffset: requested component image node
1886 * fit_image_set_hashes() adds hash values for an component image node. All
1887 * existing hash subnodes are checked, if algorithm property is set to one of
1888 * the supported hash algorithms, hash value is computed and corresponding
1889 * hash node property is set, for example:
1891 * Input component image node structure:
1893 * o image@1 (at image_noffset)
1894 * | - data = [binary data]
1898 * Output component image node structure:
1900 * o image@1 (at image_noffset)
1901 * | - data = [binary data]
1904 * |- value = sha1(data)
1910 int fit_image_set_hashes (void *fit, int image_noffset)
1915 uint8_t value[FIT_MAX_HASH_LEN];
1920 /* Get image data and data length */
1921 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
1922 printf ("Can't get image data/size\n");
1926 /* Process all hash subnodes of the component image node */
1927 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1928 (noffset >= 0) && (ndepth > 0);
1929 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1931 /* Direct child node of the component image node */
1934 * Check subnode name, must be equal to "hash".
1935 * Multiple hash nodes require unique unit node
1936 * names, e.g. hash@1, hash@2, etc.
1938 if (strncmp (fit_get_name(fit, noffset, NULL),
1940 strlen(FIT_HASH_NODENAME)) != 0) {
1941 /* Not a hash subnode, skip it */
1945 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1946 printf ("Can't get hash algo property for "
1947 "'%s' hash node in '%s' image node\n",
1948 fit_get_name (fit, noffset, NULL),
1949 fit_get_name (fit, image_noffset, NULL));
1953 if (calculate_hash (data, size, algo, value, &value_len)) {
1954 printf ("Unsupported hash algorithm (%s) for "
1955 "'%s' hash node in '%s' image node\n",
1956 algo, fit_get_name (fit, noffset, NULL),
1957 fit_get_name (fit, image_noffset, NULL));
1961 if (fit_image_hash_set_value (fit, noffset, value,
1963 printf ("Can't set hash value for "
1964 "'%s' hash node in '%s' image node\n",
1965 fit_get_name (fit, noffset, NULL),
1966 fit_get_name (fit, image_noffset, NULL));
1976 * fit_image_hash_set_value - set hash value in requested has node
1977 * @fit: pointer to the FIT format image header
1978 * @noffset: hash node offset
1979 * @value: hash value to be set
1980 * @value_len: hash value length
1982 * fit_image_hash_set_value() attempts to set hash value in a node at offset
1983 * given and returns operation status to the caller.
1989 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
1994 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
1996 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
1997 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
1998 fdt_strerror (ret));
2004 #endif /* USE_HOSTCC */
2007 * fit_image_check_hashes - verify data intergity
2008 * @fit: pointer to the FIT format image header
2009 * @image_noffset: component image node offset
2011 * fit_image_check_hashes() goes over component image hash nodes,
2012 * re-calculates each data hash and compares with the value stored in hash
2016 * 1, if all hashes are valid
2017 * 0, otherwise (or on error)
2019 int fit_image_check_hashes (const void *fit, int image_noffset)
2026 uint8_t value[FIT_MAX_HASH_LEN];
2032 /* Get image data and data length */
2033 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2034 printf ("Can't get image data/size\n");
2038 /* Process all hash subnodes of the component image node */
2039 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2040 (noffset >= 0) && (ndepth > 0);
2041 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2043 /* Direct child node of the component image node */
2046 * Check subnode name, must be equal to "hash".
2047 * Multiple hash nodes require unique unit node
2048 * names, e.g. hash@1, hash@2, etc.
2050 if (strncmp (fit_get_name(fit, noffset, NULL),
2052 strlen(FIT_HASH_NODENAME)) != 0)
2055 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2056 err_msg = "Can't get hash algo property";
2059 printf ("%s", algo);
2061 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2063 err_msg = "Can't get hash value property";
2067 if (calculate_hash (data, size, algo, value, &value_len)) {
2068 err_msg = "Unsupported hash algorithm";
2072 if (value_len != fit_value_len) {
2073 err_msg = "Bad hash value len";
2075 } else if (memcmp (value, fit_value, value_len) != 0) {
2076 err_msg = "Bad hash value";
2086 printf ("%s for '%s' hash node in '%s' image node\n",
2087 err_msg, fit_get_name (fit, noffset, NULL),
2088 fit_get_name (fit, image_noffset, NULL));
2093 * fit_image_check_os - check whether image node is of a given os type
2094 * @fit: pointer to the FIT format image header
2095 * @noffset: component image node offset
2096 * @os: requested image os
2098 * fit_image_check_os() reads image os property and compares its numeric
2099 * id with the requested os. Comparison result is returned to the caller.
2102 * 1 if image is of given os type
2103 * 0 otherwise (or on error)
2105 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2109 if (fit_image_get_os (fit, noffset, &image_os))
2111 return (os == image_os);
2115 * fit_image_check_arch - check whether image node is of a given arch
2116 * @fit: pointer to the FIT format image header
2117 * @noffset: component image node offset
2118 * @arch: requested imagearch
2120 * fit_image_check_arch() reads image arch property and compares its numeric
2121 * id with the requested arch. Comparison result is returned to the caller.
2124 * 1 if image is of given arch
2125 * 0 otherwise (or on error)
2127 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2131 if (fit_image_get_arch (fit, noffset, &image_arch))
2133 return (arch == image_arch);
2137 * fit_image_check_type - check whether image node is of a given type
2138 * @fit: pointer to the FIT format image header
2139 * @noffset: component image node offset
2140 * @type: requested image type
2142 * fit_image_check_type() reads image type property and compares its numeric
2143 * id with the requested type. Comparison result is returned to the caller.
2146 * 1 if image is of given type
2147 * 0 otherwise (or on error)
2149 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2153 if (fit_image_get_type (fit, noffset, &image_type))
2155 return (type == image_type);
2159 * fit_image_check_comp - check whether image node uses given compression
2160 * @fit: pointer to the FIT format image header
2161 * @noffset: component image node offset
2162 * @comp: requested image compression type
2164 * fit_image_check_comp() reads image compression property and compares its
2165 * numeric id with the requested compression type. Comparison result is
2166 * returned to the caller.
2169 * 1 if image uses requested compression
2170 * 0 otherwise (or on error)
2172 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2176 if (fit_image_get_comp (fit, noffset, &image_comp))
2178 return (comp == image_comp);
2182 * fit_check_format - sanity check FIT image format
2183 * @fit: pointer to the FIT format image header
2185 * fit_check_format() runs a basic sanity FIT image verification.
2186 * Routine checks for mandatory properties, nodes, etc.
2192 int fit_check_format (const void *fit)
2194 /* mandatory / node 'description' property */
2195 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2196 debug ("Wrong FIT format: no description\n");
2200 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2201 /* mandatory / node 'timestamp' property */
2202 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2203 debug ("Wrong FIT format: no description\n");
2208 /* mandatory subimages parent '/images' node */
2209 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2210 debug ("Wrong FIT format: no images parent node\n");
2218 * fit_conf_get_node - get node offset for configuration of a given unit name
2219 * @fit: pointer to the FIT format image header
2220 * @conf_uname: configuration node unit name
2222 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2223 * parant node) of a provided unit name. If configuration is found its node offset
2224 * is returned to the caller.
2226 * When NULL is provided in second argument fit_conf_get_node() will search
2227 * for a default configuration node instead. Default configuration node unit name
2228 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2231 * configuration node offset when found (>=0)
2232 * negative number on failure (FDT_ERR_* code)
2234 int fit_conf_get_node (const void *fit, const char *conf_uname)
2236 int noffset, confs_noffset;
2239 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2240 if (confs_noffset < 0) {
2241 debug ("Can't find configurations parent node '%s' (%s)\n",
2242 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2243 return confs_noffset;
2246 if (conf_uname == NULL) {
2247 /* get configuration unit name from the default property */
2248 debug ("No configuration specified, trying default...\n");
2249 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2250 if (conf_uname == NULL) {
2251 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2254 debug ("Found default configuration: '%s'\n", conf_uname);
2257 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2259 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2260 conf_uname, fdt_strerror (noffset));
2266 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2267 const char *prop_name)
2272 /* get kernel image unit name from configuration kernel property */
2273 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2277 return fit_image_get_node (fit, uname);
2281 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2282 * a given configuration
2283 * @fit: pointer to the FIT format image header
2284 * @noffset: configuration node offset
2286 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2287 * configuration FIT_KERNEL_PROP property and translates it to the node
2291 * image node offset when found (>=0)
2292 * negative number on failure (FDT_ERR_* code)
2294 int fit_conf_get_kernel_node (const void *fit, int noffset)
2296 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2300 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2301 * a given configuration
2302 * @fit: pointer to the FIT format image header
2303 * @noffset: configuration node offset
2305 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2306 * configuration FIT_KERNEL_PROP property and translates it to the node
2310 * image node offset when found (>=0)
2311 * negative number on failure (FDT_ERR_* code)
2313 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2315 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2319 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2320 * a given configuration
2321 * @fit: pointer to the FIT format image header
2322 * @noffset: configuration node offset
2324 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2325 * configuration FIT_KERNEL_PROP property and translates it to the node
2329 * image node offset when found (>=0)
2330 * negative number on failure (FDT_ERR_* code)
2332 int fit_conf_get_fdt_node (const void *fit, int noffset)
2334 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2338 * fit_conf_print - prints out the FIT configuration details
2339 * @fit: pointer to the FIT format image header
2340 * @conf_noffset: offset of the configuration node
2341 * @p: pointer to prefix string
2343 * fit_conf_print() lists all mandatory properies for the processed
2344 * configuration node.
2347 * no returned results
2349 void fit_conf_print (const void *fit, int noffset, const char *p)
2355 /* Mandatory properties */
2356 ret = fit_get_desc (fit, noffset, &desc);
2357 printf ("%s Description: ", p);
2359 printf ("unavailable\n");
2361 printf ("%s\n", desc);
2363 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2364 printf ("%s Kernel: ", p);
2366 printf ("unavailable\n");
2368 printf ("%s\n", uname);
2370 /* Optional properties */
2371 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2373 printf ("%s Init Ramdisk: %s\n", p, uname);
2375 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2377 printf ("%s FDT: %s\n", p, uname);
2379 #endif /* CONFIG_FIT */