2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
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 (cmd_tbl_t *cmdtp, int flag,
70 int argc, char *argv[],
71 ulong rd_addr, uint8_t arch, int verify);
76 #endif /* !USE_HOSTCC*/
78 typedef struct table_entry {
79 int id; /* as defined in image.h */
80 char *sname; /* short (input) name */
81 char *lname; /* long (output) name */
84 static table_entry_t uimage_arch[] = {
85 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
86 { IH_ARCH_ALPHA, "alpha", "Alpha", },
87 { IH_ARCH_ARM, "arm", "ARM", },
88 { IH_ARCH_I386, "x86", "Intel x86", },
89 { IH_ARCH_IA64, "ia64", "IA64", },
90 { IH_ARCH_M68K, "m68k", "M68K", },
91 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
92 { IH_ARCH_MIPS, "mips", "MIPS", },
93 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
94 { IH_ARCH_NIOS, "nios", "NIOS", },
95 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
96 { IH_ARCH_PPC, "ppc", "PowerPC", },
97 { IH_ARCH_S390, "s390", "IBM S390", },
98 { IH_ARCH_SH, "sh", "SuperH", },
99 { IH_ARCH_SPARC, "sparc", "SPARC", },
100 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
101 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
102 { IH_ARCH_AVR32, "avr32", "AVR32", },
106 static table_entry_t uimage_os[] = {
107 { IH_OS_INVALID, NULL, "Invalid OS", },
108 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
109 { IH_OS_ARTOS, "artos", "ARTOS", },
111 { IH_OS_LINUX, "linux", "Linux", },
112 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
113 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
115 { IH_OS_NETBSD, "netbsd", "NetBSD", },
116 { IH_OS_RTEMS, "rtems", "RTEMS", },
117 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
118 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
119 { IH_OS_QNX, "qnx", "QNX", },
120 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
123 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
124 { IH_OS_DELL, "dell", "Dell", },
125 { IH_OS_ESIX, "esix", "Esix", },
126 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
127 { IH_OS_IRIX, "irix", "Irix", },
128 { IH_OS_NCR, "ncr", "NCR", },
129 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
130 { IH_OS_PSOS, "psos", "pSOS", },
131 { IH_OS_SCO, "sco", "SCO", },
132 { IH_OS_SOLARIS, "solaris", "Solaris", },
133 { IH_OS_SVR4, "svr4", "SVR4", },
138 static table_entry_t uimage_type[] = {
139 { IH_TYPE_INVALID, NULL, "Invalid Image", },
140 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
141 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
142 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
143 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
144 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
145 { IH_TYPE_SCRIPT, "script", "Script", },
146 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
147 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
151 static table_entry_t uimage_comp[] = {
152 { IH_COMP_NONE, "none", "uncompressed", },
153 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
154 { IH_COMP_GZIP, "gzip", "gzip compressed", },
158 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
159 static void genimg_print_size (uint32_t size);
160 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
161 static void genimg_print_time (time_t timestamp);
164 /*****************************************************************************/
165 /* Legacy format routines */
166 /*****************************************************************************/
167 int image_check_hcrc (image_header_t *hdr)
170 ulong len = image_get_header_size ();
171 image_header_t header;
173 /* Copy header so we can blank CRC field for re-calculation */
174 memmove (&header, (char *)hdr, image_get_header_size ());
175 image_set_hcrc (&header, 0);
177 hcrc = crc32 (0, (unsigned char *)&header, len);
179 return (hcrc == image_get_hcrc (hdr));
182 int image_check_dcrc (image_header_t *hdr)
184 ulong data = image_get_data (hdr);
185 ulong len = image_get_data_size (hdr);
186 ulong dcrc = crc32 (0, (unsigned char *)data, len);
188 return (dcrc == image_get_dcrc (hdr));
192 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
195 ulong len = image_get_data_size (hdr);
196 ulong data = image_get_data (hdr);
198 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
200 ulong edata = cdata + len;
202 while (cdata < edata) {
203 ulong chunk = edata - cdata;
207 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
213 dcrc = crc32 (0, (unsigned char *)data, len);
216 return (dcrc == image_get_dcrc (hdr));
218 #endif /* !USE_HOSTCC */
221 * image_multi_count - get component (sub-image) count
222 * @hdr: pointer to the header of the multi component image
224 * image_multi_count() returns number of components in a multi
227 * Note: no checking of the image type is done, caller must pass
228 * a valid multi component image.
231 * number of components
233 ulong image_multi_count (image_header_t *hdr)
238 /* get start of the image payload, which in case of multi
239 * component images that points to a table of component sizes */
240 size = (uint32_t *)image_get_data (hdr);
242 /* count non empty slots */
243 for (i = 0; size[i]; ++i)
250 * image_multi_getimg - get component data address and size
251 * @hdr: pointer to the header of the multi component image
252 * @idx: index of the requested component
253 * @data: pointer to a ulong variable, will hold component data address
254 * @len: pointer to a ulong variable, will hold component size
256 * image_multi_getimg() returns size and data address for the requested
257 * component in a multi component image.
259 * Note: no checking of the image type is done, caller must pass
260 * a valid multi component image.
263 * data address and size of the component, if idx is valid
264 * 0 in data and len, if idx is out of range
266 void image_multi_getimg (image_header_t *hdr, ulong idx,
267 ulong *data, ulong *len)
271 ulong offset, tail, count, img_data;
273 /* get number of component */
274 count = image_multi_count (hdr);
276 /* get start of the image payload, which in case of multi
277 * component images that points to a table of component sizes */
278 size = (uint32_t *)image_get_data (hdr);
280 /* get address of the proper component data start, which means
281 * skipping sizes table (add 1 for last, null entry) */
282 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
285 *len = uimage_to_cpu (size[idx]);
289 /* go over all indices preceding requested component idx */
290 for (i = 0; i < idx; i++) {
291 /* add up i-th component size */
292 offset += uimage_to_cpu (size[i]);
294 /* add up alignment for i-th component */
295 tail += (4 - uimage_to_cpu (size[i]) % 4);
298 /* calculate idx-th component data address */
299 *data = img_data + offset + tail;
306 static void image_print_type (image_header_t *hdr)
308 const char *os, *arch, *type, *comp;
310 os = genimg_get_os_name (image_get_os (hdr));
311 arch = genimg_get_arch_name (image_get_arch (hdr));
312 type = genimg_get_type_name (image_get_type (hdr));
313 comp = genimg_get_comp_name (image_get_comp (hdr));
315 printf ("%s %s %s (%s)\n", arch, os, type, comp);
319 * __image_print_contents - prints out the contents of the legacy format image
320 * @hdr: pointer to the legacy format image header
321 * @p: pointer to prefix string
323 * __image_print_contents() formats a multi line legacy image contents description.
324 * The routine prints out all header fields followed by the size/offset data
325 * for MULTI/SCRIPT images.
328 * no returned results
330 static void __image_print_contents (image_header_t *hdr, const char *p)
332 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
333 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
334 printf ("%sCreated: ", p);
335 genimg_print_time ((time_t)image_get_time (hdr));
337 printf ("%sImage Type: ", p);
338 image_print_type (hdr);
339 printf ("%sData Size: ", p);
340 genimg_print_size (image_get_data_size (hdr));
341 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
342 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
344 if (image_check_type (hdr, IH_TYPE_MULTI) ||
345 image_check_type (hdr, IH_TYPE_SCRIPT)) {
348 ulong count = image_multi_count (hdr);
350 printf ("%sContents:\n", p);
351 for (i = 0; i < count; i++) {
352 image_multi_getimg (hdr, i, &data, &len);
354 printf ("%s Image %d: ", p, i);
355 genimg_print_size (len);
357 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
359 * the user may need to know offsets
360 * if planning to do something with
363 printf ("%s Offset = 0x%08lx\n", p, data);
369 inline void image_print_contents (image_header_t *hdr)
371 __image_print_contents (hdr, " ");
374 inline void image_print_contents_noindent (image_header_t *hdr)
376 __image_print_contents (hdr, "");
381 * image_get_ramdisk - get and verify ramdisk image
382 * @cmdtp: command table pointer
383 * @flag: command flag
384 * @argc: command argument count
385 * @argv: command argument list
386 * @rd_addr: ramdisk image start address
387 * @arch: expected ramdisk architecture
388 * @verify: checksum verification flag
390 * image_get_ramdisk() returns a pointer to the verified ramdisk image
391 * header. Routine receives image start address and expected architecture
392 * flag. Verification done covers data and header integrity and os/type/arch
395 * If dataflash support is enabled routine checks for dataflash addresses
396 * and handles required dataflash reads.
399 * pointer to a ramdisk image header, if image was found and valid
400 * otherwise, return NULL
402 static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
403 int argc, char *argv[],
404 ulong rd_addr, uint8_t arch, int verify)
406 image_header_t *rd_hdr;
408 show_boot_progress (9);
409 rd_hdr = (image_header_t *)rd_addr;
411 if (!image_check_magic (rd_hdr)) {
412 puts ("Bad Magic Number\n");
413 show_boot_progress (-10);
417 if (!image_check_hcrc (rd_hdr)) {
418 puts ("Bad Header Checksum\n");
419 show_boot_progress (-11);
423 show_boot_progress (10);
424 image_print_contents (rd_hdr);
427 puts(" Verifying Checksum ... ");
428 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
429 puts ("Bad Data CRC\n");
430 show_boot_progress (-12);
436 show_boot_progress (11);
438 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
439 !image_check_arch (rd_hdr, arch) ||
440 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
441 printf ("No Linux %s Ramdisk Image\n",
442 genimg_get_arch_name(arch));
443 show_boot_progress (-13);
449 #endif /* !USE_HOSTCC */
451 /*****************************************************************************/
452 /* Shared dual-format routines */
453 /*****************************************************************************/
455 int getenv_verify (void)
457 char *s = getenv ("verify");
458 return (s && (*s == 'n')) ? 0 : 1;
461 int getenv_autostart (void)
463 char *s = getenv ("autostart");
464 return (s && (*s == 'n')) ? 0 : 1;
467 ulong getenv_bootm_low(void)
469 char *s = getenv ("bootm_low");
471 ulong tmp = simple_strtoul (s, NULL, 16);
475 #ifdef CFG_SDRAM_BASE
476 return CFG_SDRAM_BASE;
482 ulong getenv_bootm_size(void)
484 char *s = getenv ("bootm_size");
486 ulong tmp = simple_strtoul (s, NULL, 16);
490 return gd->bd->bi_memsize;
493 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
495 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
497 size_t tail = (len > chunksz) ? chunksz : len;
499 memmove (to, from, tail);
504 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
505 memmove (to, from, len);
506 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
508 #endif /* !USE_HOSTCC */
510 static void genimg_print_size (uint32_t size)
513 printf ("%d Bytes = ", size);
514 print_size (size, "\n");
516 printf ("%d Bytes = %.2f kB = %.2f MB\n",
517 size, (double)size / 1.024e3,
518 (double)size / 1.048576e6);
522 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
523 static void genimg_print_time (time_t timestamp)
528 to_tm (timestamp, &tm);
529 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
530 tm.tm_year, tm.tm_mon, tm.tm_mday,
531 tm.tm_hour, tm.tm_min, tm.tm_sec);
533 printf ("%s", ctime(×tamp));
536 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
539 * get_table_entry_name - translate entry id to long name
540 * @table: pointer to a translation table for entries of a specific type
541 * @msg: message to be returned when translation fails
542 * @id: entry id to be translated
544 * get_table_entry_name() will go over translation table trying to find
545 * entry that matches given id. If matching entry is found, its long
546 * name is returned to the caller.
549 * long entry name if translation succeeds
552 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
554 for (; table->id >= 0; ++table) {
556 return (table->lname);
561 const char *genimg_get_os_name (uint8_t os)
563 return (get_table_entry_name (uimage_os, "Unknown OS", os));
566 const char *genimg_get_arch_name (uint8_t arch)
568 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
571 const char *genimg_get_type_name (uint8_t type)
573 return (get_table_entry_name (uimage_type, "Unknown Image", type));
576 const char *genimg_get_comp_name (uint8_t comp)
578 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
582 * get_table_entry_id - translate short entry name to id
583 * @table: pointer to a translation table for entries of a specific type
584 * @table_name: to be used in case of error
585 * @name: entry short name to be translated
587 * get_table_entry_id() will go over translation table trying to find
588 * entry that matches given short name. If matching entry is found,
589 * its id returned to the caller.
592 * entry id if translation succeeds
595 static int get_table_entry_id (table_entry_t *table,
596 const char *table_name, const char *name)
602 for (t = table; t->id >= 0; ++t) {
603 if (t->sname && strcasecmp(t->sname, name) == 0)
607 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
608 for (t = table; t->id >= 0; ++t) {
609 if (t->sname == NULL)
611 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
614 fprintf (stderr, "\n");
616 for (t = table; t->id >= 0; ++t) {
617 if (t->sname && strcmp(t->sname, name) == 0)
620 debug ("Invalid %s Type: %s\n", table_name, name);
621 #endif /* USE_HOSTCC */
625 int genimg_get_os_id (const char *name)
627 return (get_table_entry_id (uimage_os, "OS", name));
630 int genimg_get_arch_id (const char *name)
632 return (get_table_entry_id (uimage_arch, "CPU", name));
635 int genimg_get_type_id (const char *name)
637 return (get_table_entry_id (uimage_type, "Image", name));
640 int genimg_get_comp_id (const char *name)
642 return (get_table_entry_id (uimage_comp, "Compression", name));
647 * genimg_get_format - get image format type
648 * @img_addr: image start address
650 * genimg_get_format() checks whether provided address points to a valid
651 * legacy or FIT image.
653 * New uImage format and FDT blob are based on a libfdt. FDT blob
654 * may be passed directly or embedded in a FIT image. In both situations
655 * genimg_get_format() must be able to dectect libfdt header.
658 * image format type or IMAGE_FORMAT_INVALID if no image is present
660 int genimg_get_format (void *img_addr)
662 ulong format = IMAGE_FORMAT_INVALID;
664 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
668 hdr = (image_header_t *)img_addr;
669 if (image_check_magic(hdr))
670 format = IMAGE_FORMAT_LEGACY;
671 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
673 fit_hdr = (char *)img_addr;
674 if (fdt_check_header (fit_hdr) == 0)
675 format = IMAGE_FORMAT_FIT;
683 * genimg_get_image - get image from special storage (if necessary)
684 * @img_addr: image start address
686 * genimg_get_image() checks if provided image start adddress is located
687 * in a dataflash storage. If so, image is moved to a system RAM memory.
690 * image start address after possible relocation from special storage
692 ulong genimg_get_image (ulong img_addr)
694 ulong ram_addr = img_addr;
696 #ifdef CONFIG_HAS_DATAFLASH
697 ulong h_size, d_size;
699 if (addr_dataflash (img_addr)){
700 /* ger RAM address */
701 ram_addr = CFG_LOAD_ADDR;
703 /* get header size */
704 h_size = image_get_header_size ();
705 #if defined(CONFIG_FIT)
706 if (sizeof(struct fdt_header) > h_size)
707 h_size = sizeof(struct fdt_header);
711 debug (" Reading image header from dataflash address "
712 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
714 read_dataflash (img_addr, h_size, (char *)ram_addr);
717 switch (genimg_get_format ((void *)ram_addr)) {
718 case IMAGE_FORMAT_LEGACY:
719 d_size = image_get_data_size ((image_header_t *)ram_addr);
720 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
723 #if defined(CONFIG_FIT)
724 case IMAGE_FORMAT_FIT:
725 d_size = fit_get_size ((const void *)ram_addr) - h_size;
726 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
731 printf (" No valid image found at 0x%08lx\n", img_addr);
735 /* read in image data */
736 debug (" Reading image remaining data from dataflash address "
737 "%08lx to RAM address %08lx\n", img_addr + h_size,
740 read_dataflash (img_addr + h_size, d_size,
741 (char *)(ram_addr + h_size));
744 #endif /* CONFIG_HAS_DATAFLASH */
750 * boot_get_ramdisk - main ramdisk handling routine
751 * @cmdtp: command table pointer
752 * @flag: command flag
753 * @argc: command argument count
754 * @argv: command argument list
755 * @images: pointer to the bootm images structure
756 * @arch: expected ramdisk architecture
757 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
758 * @rd_end: pointer to a ulong variable, will hold ramdisk end
760 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
761 * Curently supported are the following ramdisk sources:
762 * - multicomponent kernel/ramdisk image,
763 * - commandline provided address of decicated ramdisk image.
766 * rd_start and rd_end are set to ramdisk start/end addresses if
767 * ramdisk image is found and valid
768 * rd_start and rd_end are set to 0 if no ramdisk exists
769 * return 1 if ramdisk image is found but corrupted
771 int boot_get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
772 bootm_headers_t *images, uint8_t arch,
773 ulong *rd_start, ulong *rd_end)
775 ulong rd_addr, rd_load;
776 ulong rd_data, rd_len;
777 image_header_t *rd_hdr;
778 #if defined(CONFIG_FIT)
780 const char *fit_uname_config = NULL;
781 const char *fit_uname_ramdisk = NULL;
793 * Look for a '-' which indicates to ignore the
796 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
797 debug ("## Skipping init Ramdisk\n");
798 rd_len = rd_data = 0;
799 } else if (argc >= 3) {
800 #if defined(CONFIG_FIT)
802 * If the init ramdisk comes from the FIT image and the FIT image
803 * address is omitted in the command line argument, try to use
804 * os FIT image address or default load address.
806 if (images->fit_uname_os)
807 default_addr = (ulong)images->fit_hdr_os;
809 default_addr = load_addr;
811 if (fit_parse_conf (argv[2], default_addr,
812 &rd_addr, &fit_uname_config)) {
813 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
814 fit_uname_config, rd_addr);
815 } else if (fit_parse_subimage (argv[2], default_addr,
816 &rd_addr, &fit_uname_ramdisk)) {
817 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
818 fit_uname_ramdisk, rd_addr);
822 rd_addr = simple_strtoul(argv[2], NULL, 16);
823 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
827 /* copy from dataflash if needed */
828 rd_addr = genimg_get_image (rd_addr);
831 * Check if there is an initrd image at the
832 * address provided in the second bootm argument
833 * check image type, for FIT images get FIT node.
835 switch (genimg_get_format ((void *)rd_addr)) {
836 case IMAGE_FORMAT_LEGACY:
837 printf ("## Loading init Ramdisk from Legacy "
838 "Image at %08lx ...\n", rd_addr);
840 rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv,
841 rd_addr, arch, images->verify);
846 rd_data = image_get_data (rd_hdr);
847 rd_len = image_get_data_size (rd_hdr);
848 rd_load = image_get_load (rd_hdr);
850 #if defined(CONFIG_FIT)
851 case IMAGE_FORMAT_FIT:
852 fit_hdr = (void *)rd_addr;
853 printf ("## Loading init Ramdisk from FIT "
854 "Image at %08lx ...\n", rd_addr);
856 if (!fit_check_format (fit_hdr)) {
857 puts ("Bad FIT ramdisk image format!\n");
861 if (!fit_uname_ramdisk) {
863 * no ramdisk image node unit name, try to get config
864 * node first. If config unit node name is NULL
865 * fit_conf_get_node() will try to find default config node
867 conf_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
868 if (conf_noffset < 0)
871 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, conf_noffset);
872 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
874 /* get ramdisk component image node offset */
875 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
880 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
882 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
885 /* get ramdisk image data address and length */
886 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
887 puts ("Could not find ramdisk subimage data!\n");
891 rd_data = (ulong)data;
894 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
895 puts ("Can't get ramdisk subimage load address!\n");
899 images->fit_hdr_rd = fit_hdr;
900 images->fit_uname_rd = fit_uname_ramdisk;
904 printf ("Wrong Image Format for %s command\n",
906 rd_data = rd_len = rd_load = 0;
909 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
911 * We need to copy the ramdisk to SRAM to let Linux boot
914 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
917 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
919 } else if (images->legacy_hdr_valid &&
920 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
922 * Now check if we have a legacy mult-component image,
923 * get second entry data start address and len.
925 show_boot_progress (13);
926 printf ("## Loading init Ramdisk from multi component "
927 "Legacy Image at %08lx ...\n",
928 (ulong)images->legacy_hdr_os);
930 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
935 show_boot_progress (14);
936 rd_len = rd_data = 0;
940 debug ("## No init Ramdisk\n");
943 *rd_end = rd_data + rd_len;
945 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
951 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
953 * boot_ramdisk_high - relocate init ramdisk
954 * @lmb: pointer to lmb handle, will be used for memory mgmt
955 * @rd_data: ramdisk data start address
956 * @rd_len: ramdisk data length
957 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
958 * start address (after possible relocation)
959 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
960 * end address (after possible relocation)
962 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
963 * variable and if requested ramdisk data is moved to a specified location.
965 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
966 * start/end addresses if ramdisk image start and len were provided,
967 * otherwise set initrd_start and initrd_end set to zeros.
973 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
974 ulong *initrd_start, ulong *initrd_end)
978 int initrd_copy_to_ram = 1;
980 if ((s = getenv ("initrd_high")) != NULL) {
981 /* a value of "no" or a similar string will act like 0,
982 * turning the "load high" feature off. This is intentional.
984 initrd_high = simple_strtoul (s, NULL, 16);
985 if (initrd_high == ~0)
986 initrd_copy_to_ram = 0;
988 /* not set, no restrictions to load high */
992 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
993 initrd_high, initrd_copy_to_ram);
996 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
997 debug (" in-place initrd\n");
998 *initrd_start = rd_data;
999 *initrd_end = rd_data + rd_len;
1000 lmb_reserve(lmb, rd_data, rd_len);
1003 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1005 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
1007 if (*initrd_start == 0) {
1008 puts ("ramdisk - allocation error\n");
1011 show_boot_progress (12);
1013 *initrd_end = *initrd_start + rd_len;
1014 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1015 *initrd_start, *initrd_end);
1017 memmove_wd ((void *)*initrd_start,
1018 (void *)rd_data, rd_len, CHUNKSZ);
1026 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1027 *initrd_start, *initrd_end);
1036 * boot_get_cmdline - allocate and initialize kernel cmdline
1037 * @lmb: pointer to lmb handle, will be used for memory mgmt
1038 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1039 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1040 * @bootmap_base: ulong variable, holds offset in physical memory to
1043 * boot_get_cmdline() allocates space for kernel command line below
1044 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1045 * variable is present its contents is copied to allocated kernel
1052 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1058 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1059 CFG_BOOTMAPSZ + bootmap_base);
1061 if (cmdline == NULL)
1064 if ((s = getenv("bootargs")) == NULL)
1069 *cmd_start = (ulong) & cmdline[0];
1070 *cmd_end = *cmd_start + strlen(cmdline);
1072 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1078 * boot_get_kbd - allocate and initialize kernel copy of board info
1079 * @lmb: pointer to lmb handle, will be used for memory mgmt
1080 * @kbd: double pointer to board info data
1081 * @bootmap_base: ulong variable, holds offset in physical memory to
1084 * boot_get_kbd() allocates space for kernel copy of board info data below
1085 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1086 * the current u-boot board info data.
1092 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1094 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1095 CFG_BOOTMAPSZ + bootmap_base);
1101 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1103 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1104 do_bdinfo(NULL, 0, 0, NULL);
1109 #endif /* CONFIG_PPC || CONFIG_M68K */
1110 #endif /* !USE_HOSTCC */
1112 #if defined(CONFIG_FIT)
1113 /*****************************************************************************/
1114 /* New uImage format routines */
1115 /*****************************************************************************/
1117 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1118 ulong *addr, const char **name)
1125 sep = strchr (spec, sepc);
1128 *addr = simple_strtoul (spec, NULL, 16);
1138 * fit_parse_conf - parse FIT configuration spec
1139 * @spec: input string, containing configuration spec
1140 * @add_curr: current image address (to be used as a possible default)
1141 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1143 * @conf_name double pointer to a char, will hold pointer to a configuration
1146 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1147 * where <addr> is a FIT image address that contains configuration
1148 * with a <conf> unit name.
1150 * Address part is optional, and if omitted default add_curr will
1154 * 1 if spec is a valid configuration string,
1155 * addr and conf_name are set accordingly
1158 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1159 ulong *addr, const char **conf_name)
1161 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1165 * fit_parse_subimage - parse FIT subimage spec
1166 * @spec: input string, containing subimage spec
1167 * @add_curr: current image address (to be used as a possible default)
1168 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1170 * @image_name: double pointer to a char, will hold pointer to a subimage name
1172 * fit_parse_subimage() expects subimage spec in the for of
1173 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1174 * subimage with a <subimg> unit name.
1176 * Address part is optional, and if omitted default add_curr will
1180 * 1 if spec is a valid subimage string,
1181 * addr and image_name are set accordingly
1184 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1185 ulong *addr, const char **image_name)
1187 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1189 #endif /* !USE_HOSTCC */
1191 static void fit_get_debug (const void *fit, int noffset,
1192 char *prop_name, int err)
1194 debug ("Can't get '%s' property from FIT 0x%08lx, "
1195 "node: offset %d, name %s (%s)\n",
1196 prop_name, (ulong)fit, noffset,
1197 fit_get_name (fit, noffset, NULL),
1198 fdt_strerror (err));
1202 * __fit_print_contents - prints out the contents of the FIT format image
1203 * @fit: pointer to the FIT format image header
1204 * @p: pointer to prefix string
1206 * __fit_print_contents() formats a multi line FIT image contents description.
1207 * The routine prints out FIT image properties (root node level) follwed by
1208 * the details of each component image.
1211 * no returned results
1213 static void __fit_print_contents (const void *fit, const char *p)
1223 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1227 /* Root node properties */
1228 ret = fit_get_desc (fit, 0, &desc);
1229 printf ("%sFIT description: ", p);
1231 printf ("unavailable\n");
1233 printf ("%s\n", desc);
1235 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1236 ret = fit_get_timestamp (fit, 0, ×tamp);
1237 printf ("%sCreated: ", p);
1239 printf ("unavailable\n");
1241 genimg_print_time (timestamp);
1244 /* Find images parent node offset */
1245 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1246 if (images_noffset < 0) {
1247 printf ("Can't find images parent node '%s' (%s)\n",
1248 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1252 /* Process its subnodes, print out component images details */
1253 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1254 (noffset >= 0) && (ndepth > 0);
1255 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1258 * Direct child node of the images parent node,
1259 * i.e. component image node.
1261 printf ("%s Image %u (%s)\n", p, count++,
1262 fit_get_name(fit, noffset, NULL));
1264 fit_image_print (fit, noffset, p);
1268 /* Find configurations parent node offset */
1269 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1270 if (confs_noffset < 0) {
1271 debug ("Can't get configurations parent node '%s' (%s)\n",
1272 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1276 /* get default configuration unit name from default property */
1277 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1279 printf ("%s Default Configuration: '%s'\n", p, uname);
1281 /* Process its subnodes, print out configurations details */
1282 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1283 (noffset >= 0) && (ndepth > 0);
1284 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1287 * Direct child node of the configurations parent node,
1288 * i.e. configuration node.
1290 printf ("%s Configuration %u (%s)\n", p, count++,
1291 fit_get_name(fit, noffset, NULL));
1293 fit_conf_print (fit, noffset, p);
1298 inline void fit_print_contents (const void *fit)
1300 __fit_print_contents (fit, " ");
1303 inline void fit_print_contents_noindent (const void *fit)
1305 __fit_print_contents (fit, "");
1309 * fit_image_print - prints out the FIT component image details
1310 * @fit: pointer to the FIT format image header
1311 * @image_noffset: offset of the component image node
1312 * @p: pointer to prefix string
1314 * fit_image_print() lists all mandatory properies for the processed component
1315 * image. If present, hash nodes are printed out as well.
1318 * no returned results
1320 void fit_image_print (const void *fit, int image_noffset, const char *p)
1323 uint8_t type, arch, os, comp;
1331 /* Mandatory properties */
1332 ret = fit_get_desc (fit, image_noffset, &desc);
1333 printf ("%s Description: ", p);
1335 printf ("unavailable\n");
1337 printf ("%s\n", desc);
1339 fit_image_get_type (fit, image_noffset, &type);
1340 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1342 fit_image_get_comp (fit, image_noffset, &comp);
1343 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1345 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1348 printf ("%s Data Start: ", p);
1350 printf ("unavailable\n");
1352 printf ("0x%08lx\n", (ulong)data);
1355 printf ("%s Data Size: ", p);
1357 printf ("unavailable\n");
1359 genimg_print_size (size);
1361 /* Remaining, type dependent properties */
1362 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1363 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1364 (type == IH_TYPE_FLATDT)) {
1365 fit_image_get_arch (fit, image_noffset, &arch);
1366 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1369 if (type == IH_TYPE_KERNEL) {
1370 fit_image_get_os (fit, image_noffset, &os);
1371 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1374 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1375 ret = fit_image_get_load (fit, image_noffset, &load);
1376 printf ("%s Load Address: ", p);
1378 printf ("unavailable\n");
1380 printf ("0x%08lx\n", load);
1382 fit_image_get_entry (fit, image_noffset, &entry);
1383 printf ("%s Entry Point: ", p);
1385 printf ("unavailable\n");
1387 printf ("0x%08lx\n", entry);
1390 /* Process all hash subnodes of the component image node */
1391 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1392 (noffset >= 0) && (ndepth > 0);
1393 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1395 /* Direct child node of the component image node */
1396 fit_image_print_hash (fit, noffset, p);
1402 * fit_image_print_hash - prints out the hash node details
1403 * @fit: pointer to the FIT format image header
1404 * @noffset: offset of the hash node
1405 * @p: pointer to prefix string
1407 * fit_image_print_hash() lists properies for the processed hash node
1410 * no returned results
1412 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1420 * Check subnode name, must be equal to "hash".
1421 * Multiple hash nodes require unique unit node
1422 * names, e.g. hash@1, hash@2, etc.
1424 if (strncmp (fit_get_name(fit, noffset, NULL),
1426 strlen(FIT_HASH_NODENAME)) != 0)
1429 debug ("%s Hash node: '%s'\n", p,
1430 fit_get_name (fit, noffset, NULL));
1432 printf ("%s Hash algo: ", p);
1433 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1434 printf ("invalid/unsupported\n");
1437 printf ("%s\n", algo);
1439 ret = fit_image_hash_get_value (fit, noffset, &value,
1441 printf ("%s Hash value: ", p);
1443 printf ("unavailable\n");
1445 for (i = 0; i < value_len; i++)
1446 printf ("%02x", value[i]);
1450 debug ("%s Hash len: %d\n", p, value_len);
1454 * fit_get_desc - get node description property
1455 * @fit: pointer to the FIT format image header
1456 * @noffset: node offset
1457 * @desc: double pointer to the char, will hold pointer to the descrption
1459 * fit_get_desc() reads description property from a given node, if
1460 * description is found pointer to it is returened in third call argument.
1466 int fit_get_desc (const void *fit, int noffset, char **desc)
1470 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1471 if (*desc == NULL) {
1472 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1480 * fit_get_timestamp - get node timestamp property
1481 * @fit: pointer to the FIT format image header
1482 * @noffset: node offset
1483 * @timestamp: pointer to the time_t, will hold read timestamp
1485 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1486 * is found and has a correct size its value is retured in third call
1491 * -1, on property read failure
1492 * -2, on wrong timestamp size
1494 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1499 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1501 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1504 if (len != sizeof (uint32_t)) {
1505 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1509 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1514 * fit_image_get_node - get node offset for component image of a given unit name
1515 * @fit: pointer to the FIT format image header
1516 * @image_uname: component image node unit name
1518 * fit_image_get_node() finds a component image (withing the '/images'
1519 * node) of a provided unit name. If image is found its node offset is
1520 * returned to the caller.
1523 * image node offset when found (>=0)
1524 * negative number on failure (FDT_ERR_* code)
1526 int fit_image_get_node (const void *fit, const char *image_uname)
1528 int noffset, images_noffset;
1530 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1531 if (images_noffset < 0) {
1532 debug ("Can't find images parent node '%s' (%s)\n",
1533 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1534 return images_noffset;
1537 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1539 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1540 image_uname, fdt_strerror (noffset));
1547 * fit_image_get_os - get os id for a given component image node
1548 * @fit: pointer to the FIT format image header
1549 * @noffset: component image node offset
1550 * @os: pointer to the uint8_t, will hold os numeric id
1552 * fit_image_get_os() finds os property in a given component image node.
1553 * If the property is found, its (string) value is translated to the numeric
1554 * id which is returned to the caller.
1560 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1565 /* Get OS name from property data */
1566 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1568 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1573 /* Translate OS name to id */
1574 *os = genimg_get_os_id (data);
1579 * fit_image_get_arch - get arch id for a given component image node
1580 * @fit: pointer to the FIT format image header
1581 * @noffset: component image node offset
1582 * @arch: pointer to the uint8_t, will hold arch numeric id
1584 * fit_image_get_arch() finds arch property in a given component image node.
1585 * If the property is found, its (string) value is translated to the numeric
1586 * id which is returned to the caller.
1592 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1597 /* Get architecture name from property data */
1598 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1600 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1605 /* Translate architecture name to id */
1606 *arch = genimg_get_arch_id (data);
1611 * fit_image_get_type - get type id for a given component image node
1612 * @fit: pointer to the FIT format image header
1613 * @noffset: component image node offset
1614 * @type: pointer to the uint8_t, will hold type numeric id
1616 * fit_image_get_type() finds type property in a given component image node.
1617 * If the property is found, its (string) value is translated to the numeric
1618 * id which is returned to the caller.
1624 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1629 /* Get image type name from property data */
1630 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1632 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1637 /* Translate image type name to id */
1638 *type = genimg_get_type_id (data);
1643 * fit_image_get_comp - get comp id for a given component image node
1644 * @fit: pointer to the FIT format image header
1645 * @noffset: component image node offset
1646 * @comp: pointer to the uint8_t, will hold comp numeric id
1648 * fit_image_get_comp() finds comp property in a given component image node.
1649 * If the property is found, its (string) value is translated to the numeric
1650 * id which is returned to the caller.
1656 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1661 /* Get compression name from property data */
1662 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1664 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1669 /* Translate compression name to id */
1670 *comp = genimg_get_comp_id (data);
1675 * fit_image_get_load - get load address property for a given component image node
1676 * @fit: pointer to the FIT format image header
1677 * @noffset: component image node offset
1678 * @load: pointer to the uint32_t, will hold load address
1680 * fit_image_get_load() finds load address property in a given component image node.
1681 * If the property is found, its value is returned to the caller.
1687 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1690 const uint32_t *data;
1692 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1694 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1698 *load = uimage_to_cpu (*data);
1703 * fit_image_get_entry - get entry point address property for a given component image node
1704 * @fit: pointer to the FIT format image header
1705 * @noffset: component image node offset
1706 * @entry: pointer to the uint32_t, will hold entry point address
1708 * fit_image_get_entry() finds entry point address property in a given component image node.
1709 * If the property is found, its value is returned to the caller.
1715 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1718 const uint32_t *data;
1720 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1722 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1726 *entry = uimage_to_cpu (*data);
1731 * fit_image_get_data - get data property and its size for a given component image node
1732 * @fit: pointer to the FIT format image header
1733 * @noffset: component image node offset
1734 * @data: double pointer to void, will hold data property's data address
1735 * @size: pointer to size_t, will hold data property's data size
1737 * fit_image_get_data() finds data property in a given component image node.
1738 * If the property is found its data start address and size are returned to
1745 int fit_image_get_data (const void *fit, int noffset,
1746 const void **data, size_t *size)
1750 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1751 if (*data == NULL) {
1752 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1762 * fit_image_hash_get_algo - get hash algorithm name
1763 * @fit: pointer to the FIT format image header
1764 * @noffset: hash node offset
1765 * @algo: double pointer to char, will hold pointer to the algorithm name
1767 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1768 * If the property is found its data start address is returned to the caller.
1774 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1778 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1779 if (*algo == NULL) {
1780 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1788 * fit_image_hash_get_value - get hash value and length
1789 * @fit: pointer to the FIT format image header
1790 * @noffset: hash node offset
1791 * @value: double pointer to uint8_t, will hold address of a hash value data
1792 * @value_len: pointer to an int, will hold hash data length
1794 * fit_image_hash_get_value() finds hash value property in a given hash node.
1795 * If the property is found its data start address and size are returned to
1802 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1807 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1808 if (*value == NULL) {
1809 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1819 * fit_set_timestamp - set node timestamp property
1820 * @fit: pointer to the FIT format image header
1821 * @noffset: node offset
1822 * @timestamp: timestamp value to be set
1824 * fit_set_timestamp() attempts to set timestamp property in the requested
1825 * node and returns operation status to the caller.
1829 * -1, on property read failure
1831 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1836 t = cpu_to_uimage (timestamp);
1837 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1840 printf ("Can't set '%s' property for '%s' node (%s)\n",
1841 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1842 fdt_strerror (ret));
1850 * calculate_hash - calculate and return hash for provided input data
1851 * @data: pointer to the input data
1852 * @data_len: data length
1853 * @algo: requested hash algorithm
1854 * @value: pointer to the char, will hold hash value data (caller must
1855 * allocate enough free space)
1856 * value_len: length of the calculated hash
1858 * calculate_hash() computes input data hash according to the requested algorithm.
1859 * Resulting hash value is placed in caller provided 'value' buffer, length
1860 * of the calculated hash is returned via value_len pointer argument.
1864 * -1, when algo is unsupported
1866 static int calculate_hash (const void *data, int data_len, const char *algo,
1867 uint8_t *value, int *value_len)
1869 if (strcmp (algo, "crc32") == 0 ) {
1870 *((uint32_t *)value) = crc32 (0, data, data_len);
1871 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1873 } else if (strcmp (algo, "sha1") == 0 ) {
1874 sha1_csum ((unsigned char *) data, data_len,
1875 (unsigned char *) value);
1877 } else if (strcmp (algo, "md5") == 0 ) {
1878 printf ("MD5 not supported\n");
1881 debug ("Unsupported hash alogrithm\n");
1889 * fit_set_hashes - process FIT component image nodes and calculate hashes
1890 * @fit: pointer to the FIT format image header
1892 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1893 * Hashes are calculated for all component images which have hash subnodes
1894 * with algorithm property set to one of the supported hash algorithms.
1898 * libfdt error code, on failure
1900 int fit_set_hashes (void *fit)
1907 /* Find images parent node offset */
1908 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1909 if (images_noffset < 0) {
1910 printf ("Can't find images parent node '%s' (%s)\n",
1911 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1912 return images_noffset;
1915 /* Process its subnodes, print out component images details */
1916 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1917 (noffset >= 0) && (ndepth > 0);
1918 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1921 * Direct child node of the images parent node,
1922 * i.e. component image node.
1924 ret = fit_image_set_hashes (fit, noffset);
1934 * fit_image_set_hashes - calculate/set hashes for given component image node
1935 * @fit: pointer to the FIT format image header
1936 * @image_noffset: requested component image node
1938 * fit_image_set_hashes() adds hash values for an component image node. All
1939 * existing hash subnodes are checked, if algorithm property is set to one of
1940 * the supported hash algorithms, hash value is computed and corresponding
1941 * hash node property is set, for example:
1943 * Input component image node structure:
1945 * o image@1 (at image_noffset)
1946 * | - data = [binary data]
1950 * Output component image node structure:
1952 * o image@1 (at image_noffset)
1953 * | - data = [binary data]
1956 * |- value = sha1(data)
1962 int fit_image_set_hashes (void *fit, int image_noffset)
1967 uint8_t value[FIT_MAX_HASH_LEN];
1972 /* Get image data and data length */
1973 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
1974 printf ("Can't get image data/size\n");
1978 /* Process all hash subnodes of the component image node */
1979 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1980 (noffset >= 0) && (ndepth > 0);
1981 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1983 /* Direct child node of the component image node */
1986 * Check subnode name, must be equal to "hash".
1987 * Multiple hash nodes require unique unit node
1988 * names, e.g. hash@1, hash@2, etc.
1990 if (strncmp (fit_get_name(fit, noffset, NULL),
1992 strlen(FIT_HASH_NODENAME)) != 0) {
1993 /* Not a hash subnode, skip it */
1997 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1998 printf ("Can't get hash algo property for "
1999 "'%s' hash node in '%s' image node\n",
2000 fit_get_name (fit, noffset, NULL),
2001 fit_get_name (fit, image_noffset, NULL));
2005 if (calculate_hash (data, size, algo, value, &value_len)) {
2006 printf ("Unsupported hash algorithm (%s) for "
2007 "'%s' hash node in '%s' image node\n",
2008 algo, fit_get_name (fit, noffset, NULL),
2009 fit_get_name (fit, image_noffset, NULL));
2013 if (fit_image_hash_set_value (fit, noffset, value,
2015 printf ("Can't set hash value for "
2016 "'%s' hash node in '%s' image node\n",
2017 fit_get_name (fit, noffset, NULL),
2018 fit_get_name (fit, image_noffset, NULL));
2028 * fit_image_hash_set_value - set hash value in requested has node
2029 * @fit: pointer to the FIT format image header
2030 * @noffset: hash node offset
2031 * @value: hash value to be set
2032 * @value_len: hash value length
2034 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2035 * given and returns operation status to the caller.
2041 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2046 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2048 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2049 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2050 fdt_strerror (ret));
2056 #endif /* USE_HOSTCC */
2059 * fit_image_check_hashes - verify data intergity
2060 * @fit: pointer to the FIT format image header
2061 * @image_noffset: component image node offset
2063 * fit_image_check_hashes() goes over component image hash nodes,
2064 * re-calculates each data hash and compares with the value stored in hash
2068 * 1, if all hashes are valid
2069 * 0, otherwise (or on error)
2071 int fit_image_check_hashes (const void *fit, int image_noffset)
2078 uint8_t value[FIT_MAX_HASH_LEN];
2084 /* Get image data and data length */
2085 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2086 printf ("Can't get image data/size\n");
2090 /* Process all hash subnodes of the component image node */
2091 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2092 (noffset >= 0) && (ndepth > 0);
2093 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2095 /* Direct child node of the component image node */
2098 * Check subnode name, must be equal to "hash".
2099 * Multiple hash nodes require unique unit node
2100 * names, e.g. hash@1, hash@2, etc.
2102 if (strncmp (fit_get_name(fit, noffset, NULL),
2104 strlen(FIT_HASH_NODENAME)) != 0)
2107 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2108 err_msg = "Can't get hash algo property";
2111 printf ("%s", algo);
2113 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2115 err_msg = "Can't get hash value property";
2119 if (calculate_hash (data, size, algo, value, &value_len)) {
2120 err_msg = "Unsupported hash algorithm";
2124 if (value_len != fit_value_len) {
2125 err_msg = "Bad hash value len";
2127 } else if (memcmp (value, fit_value, value_len) != 0) {
2128 err_msg = "Bad hash value";
2138 printf ("%s for '%s' hash node in '%s' image node\n",
2139 err_msg, fit_get_name (fit, noffset, NULL),
2140 fit_get_name (fit, image_noffset, NULL));
2145 * fit_image_check_os - check whether image node is of a given os type
2146 * @fit: pointer to the FIT format image header
2147 * @noffset: component image node offset
2148 * @os: requested image os
2150 * fit_image_check_os() reads image os property and compares its numeric
2151 * id with the requested os. Comparison result is returned to the caller.
2154 * 1 if image is of given os type
2155 * 0 otherwise (or on error)
2157 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2161 if (fit_image_get_os (fit, noffset, &image_os))
2163 return (os == image_os);
2167 * fit_image_check_arch - check whether image node is of a given arch
2168 * @fit: pointer to the FIT format image header
2169 * @noffset: component image node offset
2170 * @arch: requested imagearch
2172 * fit_image_check_arch() reads image arch property and compares its numeric
2173 * id with the requested arch. Comparison result is returned to the caller.
2176 * 1 if image is of given arch
2177 * 0 otherwise (or on error)
2179 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2183 if (fit_image_get_arch (fit, noffset, &image_arch))
2185 return (arch == image_arch);
2189 * fit_image_check_type - check whether image node is of a given type
2190 * @fit: pointer to the FIT format image header
2191 * @noffset: component image node offset
2192 * @type: requested image type
2194 * fit_image_check_type() reads image type property and compares its numeric
2195 * id with the requested type. Comparison result is returned to the caller.
2198 * 1 if image is of given type
2199 * 0 otherwise (or on error)
2201 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2205 if (fit_image_get_type (fit, noffset, &image_type))
2207 return (type == image_type);
2211 * fit_image_check_comp - check whether image node uses given compression
2212 * @fit: pointer to the FIT format image header
2213 * @noffset: component image node offset
2214 * @comp: requested image compression type
2216 * fit_image_check_comp() reads image compression property and compares its
2217 * numeric id with the requested compression type. Comparison result is
2218 * returned to the caller.
2221 * 1 if image uses requested compression
2222 * 0 otherwise (or on error)
2224 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2228 if (fit_image_get_comp (fit, noffset, &image_comp))
2230 return (comp == image_comp);
2234 * fit_check_format - sanity check FIT image format
2235 * @fit: pointer to the FIT format image header
2237 * fit_check_format() runs a basic sanity FIT image verification.
2238 * Routine checks for mandatory properties, nodes, etc.
2244 int fit_check_format (const void *fit)
2246 /* mandatory / node 'description' property */
2247 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2248 debug ("Wrong FIT format: no description\n");
2252 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2253 /* mandatory / node 'timestamp' property */
2254 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2255 debug ("Wrong FIT format: no description\n");
2260 /* mandatory subimages parent '/images' node */
2261 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2262 debug ("Wrong FIT format: no images parent node\n");
2270 * fit_conf_get_node - get node offset for configuration of a given unit name
2271 * @fit: pointer to the FIT format image header
2272 * @conf_uname: configuration node unit name
2274 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2275 * parant node) of a provided unit name. If configuration is found its node offset
2276 * is returned to the caller.
2278 * When NULL is provided in second argument fit_conf_get_node() will search
2279 * for a default configuration node instead. Default configuration node unit name
2280 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2283 * configuration node offset when found (>=0)
2284 * negative number on failure (FDT_ERR_* code)
2286 int fit_conf_get_node (const void *fit, const char *conf_uname)
2288 int noffset, confs_noffset;
2291 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2292 if (confs_noffset < 0) {
2293 debug ("Can't find configurations parent node '%s' (%s)\n",
2294 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2295 return confs_noffset;
2298 if (conf_uname == NULL) {
2299 /* get configuration unit name from the default property */
2300 debug ("No configuration specified, trying default...\n");
2301 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2302 if (conf_uname == NULL) {
2303 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2306 debug ("Found default configuration: '%s'\n", conf_uname);
2309 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2311 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2312 conf_uname, fdt_strerror (noffset));
2318 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2319 const char *prop_name)
2324 /* get kernel image unit name from configuration kernel property */
2325 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2329 return fit_image_get_node (fit, uname);
2333 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2334 * a given configuration
2335 * @fit: pointer to the FIT format image header
2336 * @noffset: configuration node offset
2338 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2339 * configuration FIT_KERNEL_PROP property and translates it to the node
2343 * image node offset when found (>=0)
2344 * negative number on failure (FDT_ERR_* code)
2346 int fit_conf_get_kernel_node (const void *fit, int noffset)
2348 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2352 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2353 * a given configuration
2354 * @fit: pointer to the FIT format image header
2355 * @noffset: configuration node offset
2357 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2358 * configuration FIT_KERNEL_PROP property and translates it to the node
2362 * image node offset when found (>=0)
2363 * negative number on failure (FDT_ERR_* code)
2365 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2367 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2371 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2372 * a given configuration
2373 * @fit: pointer to the FIT format image header
2374 * @noffset: configuration node offset
2376 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2377 * configuration FIT_KERNEL_PROP property and translates it to the node
2381 * image node offset when found (>=0)
2382 * negative number on failure (FDT_ERR_* code)
2384 int fit_conf_get_fdt_node (const void *fit, int noffset)
2386 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2390 * fit_conf_print - prints out the FIT configuration details
2391 * @fit: pointer to the FIT format image header
2392 * @conf_noffset: offset of the configuration node
2393 * @p: pointer to prefix string
2395 * fit_conf_print() lists all mandatory properies for the processed
2396 * configuration node.
2399 * no returned results
2401 void fit_conf_print (const void *fit, int noffset, const char *p)
2407 /* Mandatory properties */
2408 ret = fit_get_desc (fit, noffset, &desc);
2409 printf ("%s Description: ", p);
2411 printf ("unavailable\n");
2413 printf ("%s\n", desc);
2415 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2416 printf ("%s Kernel: ", p);
2418 printf ("unavailable\n");
2420 printf ("%s\n", uname);
2422 /* Optional properties */
2423 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2425 printf ("%s Init Ramdisk: %s\n", p, uname);
2427 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2429 printf ("%s FDT: %s\n", p, uname);
2433 * fit_check_ramdisk - verify FIT format ramdisk subimage
2434 * @fit_hdr: pointer to the FIT ramdisk header
2435 * @rd_noffset: ramdisk subimage node offset within FIT image
2436 * @arch: requested ramdisk image architecture type
2437 * @verify: data CRC verification flag
2439 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2440 * specified FIT image.
2447 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2449 fit_image_print (fit, rd_noffset, " ");
2452 puts (" Verifying Hash Integrity ... ");
2453 if (!fit_image_check_hashes (fit, rd_noffset)) {
2454 puts ("Bad Data Hash\n");
2460 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2461 !fit_image_check_arch (fit, rd_noffset, arch) ||
2462 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2463 printf ("No Linux %s Ramdisk Image\n",
2464 genimg_get_arch_name(arch));
2470 #endif /* USE_HOSTCC */
2471 #endif /* CONFIG_FIT */