2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #ifdef CONFIG_SHOW_BOOT_PROGRESS
33 #include <status_led.h>
36 #ifdef CONFIG_HAS_DATAFLASH
37 #include <dataflash.h>
40 #ifdef CONFIG_LOGBUFFER
44 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
50 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
53 #include <fdt_support.h>
56 #if defined(CONFIG_FIT)
60 static int fit_check_ramdisk (const void *fit, int os_noffset,
61 uint8_t arch, int verify);
65 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
68 DECLARE_GLOBAL_DATA_PTR;
70 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
77 #endif /* !USE_HOSTCC*/
79 typedef struct table_entry {
80 int id; /* as defined in image.h */
81 char *sname; /* short (input) name */
82 char *lname; /* long (output) name */
85 static table_entry_t uimage_arch[] = {
86 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
87 { IH_ARCH_ALPHA, "alpha", "Alpha", },
88 { IH_ARCH_ARM, "arm", "ARM", },
89 { IH_ARCH_I386, "x86", "Intel x86", },
90 { IH_ARCH_IA64, "ia64", "IA64", },
91 { IH_ARCH_M68K, "m68k", "M68K", },
92 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
93 { IH_ARCH_MIPS, "mips", "MIPS", },
94 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
95 { IH_ARCH_NIOS, "nios", "NIOS", },
96 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
97 { IH_ARCH_PPC, "ppc", "PowerPC", },
98 { IH_ARCH_S390, "s390", "IBM S390", },
99 { IH_ARCH_SH, "sh", "SuperH", },
100 { IH_ARCH_SPARC, "sparc", "SPARC", },
101 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
102 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
103 { IH_ARCH_AVR32, "avr32", "AVR32", },
107 static table_entry_t uimage_os[] = {
108 { IH_OS_INVALID, NULL, "Invalid OS", },
109 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
110 { IH_OS_ARTOS, "artos", "ARTOS", },
112 { IH_OS_LINUX, "linux", "Linux", },
113 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
114 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
116 { IH_OS_NETBSD, "netbsd", "NetBSD", },
117 { IH_OS_RTEMS, "rtems", "RTEMS", },
118 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
119 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
120 { IH_OS_QNX, "qnx", "QNX", },
121 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
124 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
125 { IH_OS_DELL, "dell", "Dell", },
126 { IH_OS_ESIX, "esix", "Esix", },
127 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
128 { IH_OS_IRIX, "irix", "Irix", },
129 { IH_OS_NCR, "ncr", "NCR", },
130 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
131 { IH_OS_PSOS, "psos", "pSOS", },
132 { IH_OS_SCO, "sco", "SCO", },
133 { IH_OS_SOLARIS, "solaris", "Solaris", },
134 { IH_OS_SVR4, "svr4", "SVR4", },
139 static table_entry_t uimage_type[] = {
140 { IH_TYPE_INVALID, NULL, "Invalid Image", },
141 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
142 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
143 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
144 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
145 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
146 { IH_TYPE_SCRIPT, "script", "Script", },
147 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
148 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
152 static table_entry_t uimage_comp[] = {
153 { IH_COMP_NONE, "none", "uncompressed", },
154 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
155 { IH_COMP_GZIP, "gzip", "gzip compressed", },
159 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
160 static void genimg_print_size (uint32_t size);
161 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
162 static void genimg_print_time (time_t timestamp);
165 /*****************************************************************************/
166 /* Legacy format routines */
167 /*****************************************************************************/
168 int image_check_hcrc (image_header_t *hdr)
171 ulong len = image_get_header_size ();
172 image_header_t header;
174 /* Copy header so we can blank CRC field for re-calculation */
175 memmove (&header, (char *)hdr, image_get_header_size ());
176 image_set_hcrc (&header, 0);
178 hcrc = crc32 (0, (unsigned char *)&header, len);
180 return (hcrc == image_get_hcrc (hdr));
183 int image_check_dcrc (image_header_t *hdr)
185 ulong data = image_get_data (hdr);
186 ulong len = image_get_data_size (hdr);
187 ulong dcrc = crc32 (0, (unsigned char *)data, len);
189 return (dcrc == image_get_dcrc (hdr));
193 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
196 ulong len = image_get_data_size (hdr);
197 ulong data = image_get_data (hdr);
199 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
201 ulong edata = cdata + len;
203 while (cdata < edata) {
204 ulong chunk = edata - cdata;
208 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
214 dcrc = crc32 (0, (unsigned char *)data, len);
217 return (dcrc == image_get_dcrc (hdr));
219 #endif /* !USE_HOSTCC */
222 * image_multi_count - get component (sub-image) count
223 * @hdr: pointer to the header of the multi component image
225 * image_multi_count() returns number of components in a multi
228 * Note: no checking of the image type is done, caller must pass
229 * a valid multi component image.
232 * number of components
234 ulong image_multi_count (image_header_t *hdr)
239 /* get start of the image payload, which in case of multi
240 * component images that points to a table of component sizes */
241 size = (uint32_t *)image_get_data (hdr);
243 /* count non empty slots */
244 for (i = 0; size[i]; ++i)
251 * image_multi_getimg - get component data address and size
252 * @hdr: pointer to the header of the multi component image
253 * @idx: index of the requested component
254 * @data: pointer to a ulong variable, will hold component data address
255 * @len: pointer to a ulong variable, will hold component size
257 * image_multi_getimg() returns size and data address for the requested
258 * component in a multi component image.
260 * Note: no checking of the image type is done, caller must pass
261 * a valid multi component image.
264 * data address and size of the component, if idx is valid
265 * 0 in data and len, if idx is out of range
267 void image_multi_getimg (image_header_t *hdr, ulong idx,
268 ulong *data, ulong *len)
272 ulong offset, tail, count, img_data;
274 /* get number of component */
275 count = image_multi_count (hdr);
277 /* get start of the image payload, which in case of multi
278 * component images that points to a table of component sizes */
279 size = (uint32_t *)image_get_data (hdr);
281 /* get address of the proper component data start, which means
282 * skipping sizes table (add 1 for last, null entry) */
283 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
286 *len = uimage_to_cpu (size[idx]);
290 /* go over all indices preceding requested component idx */
291 for (i = 0; i < idx; i++) {
292 /* add up i-th component size */
293 offset += uimage_to_cpu (size[i]);
295 /* add up alignment for i-th component */
296 tail += (4 - uimage_to_cpu (size[i]) % 4);
299 /* calculate idx-th component data address */
300 *data = img_data + offset + tail;
307 static void image_print_type (image_header_t *hdr)
309 const char *os, *arch, *type, *comp;
311 os = genimg_get_os_name (image_get_os (hdr));
312 arch = genimg_get_arch_name (image_get_arch (hdr));
313 type = genimg_get_type_name (image_get_type (hdr));
314 comp = genimg_get_comp_name (image_get_comp (hdr));
316 printf ("%s %s %s (%s)\n", arch, os, type, comp);
320 * __image_print_contents - prints out the contents of the legacy format image
321 * @hdr: pointer to the legacy format image header
322 * @p: pointer to prefix string
324 * __image_print_contents() formats a multi line legacy image contents description.
325 * The routine prints out all header fields followed by the size/offset data
326 * for MULTI/SCRIPT images.
329 * no returned results
331 static void __image_print_contents (image_header_t *hdr, const char *p)
333 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
334 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
335 printf ("%sCreated: ", p);
336 genimg_print_time ((time_t)image_get_time (hdr));
338 printf ("%sImage Type: ", p);
339 image_print_type (hdr);
340 printf ("%sData Size: ", p);
341 genimg_print_size (image_get_data_size (hdr));
342 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
343 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
345 if (image_check_type (hdr, IH_TYPE_MULTI) ||
346 image_check_type (hdr, IH_TYPE_SCRIPT)) {
349 ulong count = image_multi_count (hdr);
351 printf ("%sContents:\n", p);
352 for (i = 0; i < count; i++) {
353 image_multi_getimg (hdr, i, &data, &len);
355 printf ("%s Image %d: ", p, i);
356 genimg_print_size (len);
358 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
360 * the user may need to know offsets
361 * if planning to do something with
364 printf ("%s Offset = 0x%08lx\n", p, data);
370 inline void image_print_contents (image_header_t *hdr)
372 __image_print_contents (hdr, " ");
375 inline void image_print_contents_noindent (image_header_t *hdr)
377 __image_print_contents (hdr, "");
382 * image_get_ramdisk - get and verify ramdisk image
383 * @rd_addr: ramdisk image start address
384 * @arch: expected ramdisk architecture
385 * @verify: checksum verification flag
387 * image_get_ramdisk() returns a pointer to the verified ramdisk image
388 * header. Routine receives image start address and expected architecture
389 * flag. Verification done covers data and header integrity and os/type/arch
392 * If dataflash support is enabled routine checks for dataflash addresses
393 * and handles required dataflash reads.
396 * pointer to a ramdisk image header, if image was found and valid
397 * otherwise, return NULL
399 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
402 image_header_t *rd_hdr = (image_header_t *)rd_addr;
404 if (!image_check_magic (rd_hdr)) {
405 puts ("Bad Magic Number\n");
406 show_boot_progress (-10);
410 if (!image_check_hcrc (rd_hdr)) {
411 puts ("Bad Header Checksum\n");
412 show_boot_progress (-11);
416 show_boot_progress (10);
417 image_print_contents (rd_hdr);
420 puts(" Verifying Checksum ... ");
421 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
422 puts ("Bad Data CRC\n");
423 show_boot_progress (-12);
429 show_boot_progress (11);
431 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
432 !image_check_arch (rd_hdr, arch) ||
433 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
434 printf ("No Linux %s Ramdisk Image\n",
435 genimg_get_arch_name(arch));
436 show_boot_progress (-13);
442 #endif /* !USE_HOSTCC */
444 /*****************************************************************************/
445 /* Shared dual-format routines */
446 /*****************************************************************************/
448 int getenv_verify (void)
450 char *s = getenv ("verify");
451 return (s && (*s == 'n')) ? 0 : 1;
454 int getenv_autostart (void)
456 char *s = getenv ("autostart");
457 return (s && (*s == 'n')) ? 0 : 1;
460 ulong getenv_bootm_low(void)
462 char *s = getenv ("bootm_low");
464 ulong tmp = simple_strtoul (s, NULL, 16);
468 #if defined(CFG_SDRAM_BASE)
469 return CFG_SDRAM_BASE;
470 #elif defined(CONFIG_ARM)
471 return gd->bd->bi_dram[0].start;
477 ulong getenv_bootm_size(void)
479 char *s = getenv ("bootm_size");
481 ulong tmp = simple_strtoul (s, NULL, 16);
485 #if defined(CONFIG_ARM)
486 return gd->bd->bi_dram[0].size;
488 return gd->bd->bi_memsize;
492 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
494 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
496 size_t tail = (len > chunksz) ? chunksz : len;
498 memmove (to, from, tail);
503 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
504 memmove (to, from, len);
505 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
507 #endif /* !USE_HOSTCC */
509 static void genimg_print_size (uint32_t size)
512 printf ("%d Bytes = ", size);
513 print_size (size, "\n");
515 printf ("%d Bytes = %.2f kB = %.2f MB\n",
516 size, (double)size / 1.024e3,
517 (double)size / 1.048576e6);
521 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
522 static void genimg_print_time (time_t timestamp)
527 to_tm (timestamp, &tm);
528 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
529 tm.tm_year, tm.tm_mon, tm.tm_mday,
530 tm.tm_hour, tm.tm_min, tm.tm_sec);
532 printf ("%s", ctime(×tamp));
535 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
538 * get_table_entry_name - translate entry id to long name
539 * @table: pointer to a translation table for entries of a specific type
540 * @msg: message to be returned when translation fails
541 * @id: entry id to be translated
543 * get_table_entry_name() will go over translation table trying to find
544 * entry that matches given id. If matching entry is found, its long
545 * name is returned to the caller.
548 * long entry name if translation succeeds
551 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
553 for (; table->id >= 0; ++table) {
555 return (table->lname);
560 const char *genimg_get_os_name (uint8_t os)
562 return (get_table_entry_name (uimage_os, "Unknown OS", os));
565 const char *genimg_get_arch_name (uint8_t arch)
567 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
570 const char *genimg_get_type_name (uint8_t type)
572 return (get_table_entry_name (uimage_type, "Unknown Image", type));
575 const char *genimg_get_comp_name (uint8_t comp)
577 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
581 * get_table_entry_id - translate short entry name to id
582 * @table: pointer to a translation table for entries of a specific type
583 * @table_name: to be used in case of error
584 * @name: entry short name to be translated
586 * get_table_entry_id() will go over translation table trying to find
587 * entry that matches given short name. If matching entry is found,
588 * its id returned to the caller.
591 * entry id if translation succeeds
594 static int get_table_entry_id (table_entry_t *table,
595 const char *table_name, const char *name)
601 for (t = table; t->id >= 0; ++t) {
602 if (t->sname && strcasecmp(t->sname, name) == 0)
606 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
607 for (t = table; t->id >= 0; ++t) {
608 if (t->sname == NULL)
610 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
613 fprintf (stderr, "\n");
615 for (t = table; t->id >= 0; ++t) {
616 if (t->sname && strcmp(t->sname, name) == 0)
619 debug ("Invalid %s Type: %s\n", table_name, name);
620 #endif /* USE_HOSTCC */
624 int genimg_get_os_id (const char *name)
626 return (get_table_entry_id (uimage_os, "OS", name));
629 int genimg_get_arch_id (const char *name)
631 return (get_table_entry_id (uimage_arch, "CPU", name));
634 int genimg_get_type_id (const char *name)
636 return (get_table_entry_id (uimage_type, "Image", name));
639 int genimg_get_comp_id (const char *name)
641 return (get_table_entry_id (uimage_comp, "Compression", name));
646 * genimg_get_format - get image format type
647 * @img_addr: image start address
649 * genimg_get_format() checks whether provided address points to a valid
650 * legacy or FIT image.
652 * New uImage format and FDT blob are based on a libfdt. FDT blob
653 * may be passed directly or embedded in a FIT image. In both situations
654 * genimg_get_format() must be able to dectect libfdt header.
657 * image format type or IMAGE_FORMAT_INVALID if no image is present
659 int genimg_get_format (void *img_addr)
661 ulong format = IMAGE_FORMAT_INVALID;
663 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
667 hdr = (image_header_t *)img_addr;
668 if (image_check_magic(hdr))
669 format = IMAGE_FORMAT_LEGACY;
670 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
672 fit_hdr = (char *)img_addr;
673 if (fdt_check_header (fit_hdr) == 0)
674 format = IMAGE_FORMAT_FIT;
682 * genimg_get_image - get image from special storage (if necessary)
683 * @img_addr: image start address
685 * genimg_get_image() checks if provided image start adddress is located
686 * in a dataflash storage. If so, image is moved to a system RAM memory.
689 * image start address after possible relocation from special storage
691 ulong genimg_get_image (ulong img_addr)
693 ulong ram_addr = img_addr;
695 #ifdef CONFIG_HAS_DATAFLASH
696 ulong h_size, d_size;
698 if (addr_dataflash (img_addr)){
699 /* ger RAM address */
700 ram_addr = CFG_LOAD_ADDR;
702 /* get header size */
703 h_size = image_get_header_size ();
704 #if defined(CONFIG_FIT)
705 if (sizeof(struct fdt_header) > h_size)
706 h_size = sizeof(struct fdt_header);
710 debug (" Reading image header from dataflash address "
711 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
713 read_dataflash (img_addr, h_size, (char *)ram_addr);
716 switch (genimg_get_format ((void *)ram_addr)) {
717 case IMAGE_FORMAT_LEGACY:
718 d_size = image_get_data_size ((image_header_t *)ram_addr);
719 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
722 #if defined(CONFIG_FIT)
723 case IMAGE_FORMAT_FIT:
724 d_size = fit_get_size ((const void *)ram_addr) - h_size;
725 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
730 printf (" No valid image found at 0x%08lx\n", img_addr);
734 /* read in image data */
735 debug (" Reading image remaining data from dataflash address "
736 "%08lx to RAM address %08lx\n", img_addr + h_size,
739 read_dataflash (img_addr + h_size, d_size,
740 (char *)(ram_addr + h_size));
743 #endif /* CONFIG_HAS_DATAFLASH */
749 * fit_has_config - check if there is a valid FIT configuration
750 * @images: pointer to the bootm command headers structure
752 * fit_has_config() checks if there is a FIT configuration in use
753 * (if FTI support is present).
756 * 0, no FIT support or no configuration found
757 * 1, configuration found
759 int genimg_has_config (bootm_headers_t *images)
761 #if defined(CONFIG_FIT)
762 if (images->fit_uname_cfg)
769 * boot_get_ramdisk - main ramdisk handling routine
770 * @argc: command argument count
771 * @argv: command argument list
772 * @images: pointer to the bootm images structure
773 * @arch: expected ramdisk architecture
774 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
775 * @rd_end: pointer to a ulong variable, will hold ramdisk end
777 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
778 * Curently supported are the following ramdisk sources:
779 * - multicomponent kernel/ramdisk image,
780 * - commandline provided address of decicated ramdisk image.
783 * 0, if ramdisk image was found and valid, or skiped
784 * rd_start and rd_end are set to ramdisk start/end addresses if
785 * ramdisk image is found and valid
787 * 1, if ramdisk image is found but corrupted
788 * rd_start and rd_end are set to 0 if no ramdisk exists
790 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
791 uint8_t arch, ulong *rd_start, ulong *rd_end)
793 ulong rd_addr, rd_load;
794 ulong rd_data, rd_len;
795 image_header_t *rd_hdr;
796 #if defined(CONFIG_FIT)
798 const char *fit_uname_config = NULL;
799 const char *fit_uname_ramdisk = NULL;
811 * Look for a '-' which indicates to ignore the
814 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
815 debug ("## Skipping init Ramdisk\n");
816 rd_len = rd_data = 0;
817 } else if (argc >= 3 || genimg_has_config (images)) {
818 #if defined(CONFIG_FIT)
821 * If the init ramdisk comes from the FIT image and
822 * the FIT image address is omitted in the command
823 * line argument, try to use os FIT image address or
824 * default load address.
826 if (images->fit_uname_os)
827 default_addr = (ulong)images->fit_hdr_os;
829 default_addr = load_addr;
831 if (fit_parse_conf (argv[2], default_addr,
832 &rd_addr, &fit_uname_config)) {
833 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
834 fit_uname_config, rd_addr);
835 } else if (fit_parse_subimage (argv[2], default_addr,
836 &rd_addr, &fit_uname_ramdisk)) {
837 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
838 fit_uname_ramdisk, rd_addr);
842 rd_addr = simple_strtoul(argv[2], NULL, 16);
843 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
846 #if defined(CONFIG_FIT)
848 /* use FIT configuration provided in first bootm
851 rd_addr = (ulong)images->fit_hdr_os;
852 fit_uname_config = images->fit_uname_cfg;
853 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
854 fit_uname_config, rd_addr);
857 * Check whether configuration has ramdisk defined,
858 * if not, don't try to use it, quit silently.
860 fit_hdr = (void *)rd_addr;
861 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
862 if (cfg_noffset < 0) {
863 debug ("* ramdisk: no such config\n");
867 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
868 if (rd_noffset < 0) {
869 debug ("* ramdisk: no ramdisk in config\n");
875 /* copy from dataflash if needed */
876 rd_addr = genimg_get_image (rd_addr);
879 * Check if there is an initrd image at the
880 * address provided in the second bootm argument
881 * check image type, for FIT images get FIT node.
883 switch (genimg_get_format ((void *)rd_addr)) {
884 case IMAGE_FORMAT_LEGACY:
885 printf ("## Loading init Ramdisk from Legacy "
886 "Image at %08lx ...\n", rd_addr);
888 show_boot_progress (9);
889 rd_hdr = image_get_ramdisk (rd_addr, arch,
895 rd_data = image_get_data (rd_hdr);
896 rd_len = image_get_data_size (rd_hdr);
897 rd_load = image_get_load (rd_hdr);
899 #if defined(CONFIG_FIT)
900 case IMAGE_FORMAT_FIT:
901 fit_hdr = (void *)rd_addr;
902 printf ("## Loading init Ramdisk from FIT "
903 "Image at %08lx ...\n", rd_addr);
905 show_boot_progress (120);
906 if (!fit_check_format (fit_hdr)) {
907 puts ("Bad FIT ramdisk image format!\n");
908 show_boot_progress (-120);
911 show_boot_progress (121);
913 if (!fit_uname_ramdisk) {
915 * no ramdisk image node unit name, try to get config
916 * node first. If config unit node name is NULL
917 * fit_conf_get_node() will try to find default config node
919 show_boot_progress (122);
920 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
921 if (cfg_noffset < 0) {
922 puts ("Could not find configuration node\n");
923 show_boot_progress (-122);
926 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
927 printf (" Using '%s' configuration\n", fit_uname_config);
929 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
930 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
932 /* get ramdisk component image node offset */
933 show_boot_progress (123);
934 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
936 if (rd_noffset < 0) {
937 puts ("Could not find subimage node\n");
938 show_boot_progress (-124);
942 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
944 show_boot_progress (125);
945 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
948 /* get ramdisk image data address and length */
949 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
950 puts ("Could not find ramdisk subimage data!\n");
951 show_boot_progress (-127);
954 show_boot_progress (128);
956 rd_data = (ulong)data;
959 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
960 puts ("Can't get ramdisk subimage load address!\n");
961 show_boot_progress (-129);
964 show_boot_progress (129);
966 images->fit_hdr_rd = fit_hdr;
967 images->fit_uname_rd = fit_uname_ramdisk;
968 images->fit_noffset_rd = rd_noffset;
972 puts ("Wrong Ramdisk Image Format\n");
973 rd_data = rd_len = rd_load = 0;
976 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
978 * We need to copy the ramdisk to SRAM to let Linux boot
981 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
984 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
986 } else if (images->legacy_hdr_valid &&
987 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
989 * Now check if we have a legacy mult-component image,
990 * get second entry data start address and len.
992 show_boot_progress (13);
993 printf ("## Loading init Ramdisk from multi component "
994 "Legacy Image at %08lx ...\n",
995 (ulong)images->legacy_hdr_os);
997 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
1002 show_boot_progress (14);
1003 rd_len = rd_data = 0;
1007 debug ("## No init Ramdisk\n");
1009 *rd_start = rd_data;
1010 *rd_end = rd_data + rd_len;
1012 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1013 *rd_start, *rd_end);
1018 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
1020 * boot_ramdisk_high - relocate init ramdisk
1021 * @lmb: pointer to lmb handle, will be used for memory mgmt
1022 * @rd_data: ramdisk data start address
1023 * @rd_len: ramdisk data length
1024 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1025 * start address (after possible relocation)
1026 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1027 * end address (after possible relocation)
1029 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1030 * variable and if requested ramdisk data is moved to a specified location.
1032 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1033 * start/end addresses if ramdisk image start and len were provided,
1034 * otherwise set initrd_start and initrd_end set to zeros.
1040 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1041 ulong *initrd_start, ulong *initrd_end)
1045 int initrd_copy_to_ram = 1;
1047 if ((s = getenv ("initrd_high")) != NULL) {
1048 /* a value of "no" or a similar string will act like 0,
1049 * turning the "load high" feature off. This is intentional.
1051 initrd_high = simple_strtoul (s, NULL, 16);
1052 if (initrd_high == ~0)
1053 initrd_copy_to_ram = 0;
1055 /* not set, no restrictions to load high */
1059 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1060 initrd_high, initrd_copy_to_ram);
1063 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1064 debug (" in-place initrd\n");
1065 *initrd_start = rd_data;
1066 *initrd_end = rd_data + rd_len;
1067 lmb_reserve(lmb, rd_data, rd_len);
1070 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1072 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
1074 if (*initrd_start == 0) {
1075 puts ("ramdisk - allocation error\n");
1078 show_boot_progress (12);
1080 *initrd_end = *initrd_start + rd_len;
1081 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1082 *initrd_start, *initrd_end);
1084 memmove_wd ((void *)*initrd_start,
1085 (void *)rd_data, rd_len, CHUNKSZ);
1093 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1094 *initrd_start, *initrd_end);
1103 * boot_get_cmdline - allocate and initialize kernel cmdline
1104 * @lmb: pointer to lmb handle, will be used for memory mgmt
1105 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1106 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1107 * @bootmap_base: ulong variable, holds offset in physical memory to
1110 * boot_get_cmdline() allocates space for kernel command line below
1111 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1112 * variable is present its contents is copied to allocated kernel
1119 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1125 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1126 CFG_BOOTMAPSZ + bootmap_base);
1128 if (cmdline == NULL)
1131 if ((s = getenv("bootargs")) == NULL)
1136 *cmd_start = (ulong) & cmdline[0];
1137 *cmd_end = *cmd_start + strlen(cmdline);
1139 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1145 * boot_get_kbd - allocate and initialize kernel copy of board info
1146 * @lmb: pointer to lmb handle, will be used for memory mgmt
1147 * @kbd: double pointer to board info data
1148 * @bootmap_base: ulong variable, holds offset in physical memory to
1151 * boot_get_kbd() allocates space for kernel copy of board info data below
1152 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1153 * the current u-boot board info data.
1159 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1161 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1162 CFG_BOOTMAPSZ + bootmap_base);
1168 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1170 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1171 do_bdinfo(NULL, 0, 0, NULL);
1176 #endif /* CONFIG_PPC || CONFIG_M68K */
1177 #endif /* !USE_HOSTCC */
1179 #if defined(CONFIG_FIT)
1180 /*****************************************************************************/
1181 /* New uImage format routines */
1182 /*****************************************************************************/
1184 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1185 ulong *addr, const char **name)
1192 sep = strchr (spec, sepc);
1195 *addr = simple_strtoul (spec, NULL, 16);
1205 * fit_parse_conf - parse FIT configuration spec
1206 * @spec: input string, containing configuration spec
1207 * @add_curr: current image address (to be used as a possible default)
1208 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1210 * @conf_name double pointer to a char, will hold pointer to a configuration
1213 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1214 * where <addr> is a FIT image address that contains configuration
1215 * with a <conf> unit name.
1217 * Address part is optional, and if omitted default add_curr will
1221 * 1 if spec is a valid configuration string,
1222 * addr and conf_name are set accordingly
1225 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1226 ulong *addr, const char **conf_name)
1228 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1232 * fit_parse_subimage - parse FIT subimage spec
1233 * @spec: input string, containing subimage spec
1234 * @add_curr: current image address (to be used as a possible default)
1235 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1237 * @image_name: double pointer to a char, will hold pointer to a subimage name
1239 * fit_parse_subimage() expects subimage spec in the for of
1240 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1241 * subimage with a <subimg> unit name.
1243 * Address part is optional, and if omitted default add_curr will
1247 * 1 if spec is a valid subimage string,
1248 * addr and image_name are set accordingly
1251 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1252 ulong *addr, const char **image_name)
1254 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1256 #endif /* !USE_HOSTCC */
1258 static void fit_get_debug (const void *fit, int noffset,
1259 char *prop_name, int err)
1261 debug ("Can't get '%s' property from FIT 0x%08lx, "
1262 "node: offset %d, name %s (%s)\n",
1263 prop_name, (ulong)fit, noffset,
1264 fit_get_name (fit, noffset, NULL),
1265 fdt_strerror (err));
1269 * __fit_print_contents - prints out the contents of the FIT format image
1270 * @fit: pointer to the FIT format image header
1271 * @p: pointer to prefix string
1273 * __fit_print_contents() formats a multi line FIT image contents description.
1274 * The routine prints out FIT image properties (root node level) follwed by
1275 * the details of each component image.
1278 * no returned results
1280 static void __fit_print_contents (const void *fit, const char *p)
1290 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1294 /* Root node properties */
1295 ret = fit_get_desc (fit, 0, &desc);
1296 printf ("%sFIT description: ", p);
1298 printf ("unavailable\n");
1300 printf ("%s\n", desc);
1302 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1303 ret = fit_get_timestamp (fit, 0, ×tamp);
1304 printf ("%sCreated: ", p);
1306 printf ("unavailable\n");
1308 genimg_print_time (timestamp);
1311 /* Find images parent node offset */
1312 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1313 if (images_noffset < 0) {
1314 printf ("Can't find images parent node '%s' (%s)\n",
1315 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1319 /* Process its subnodes, print out component images details */
1320 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1321 (noffset >= 0) && (ndepth > 0);
1322 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1325 * Direct child node of the images parent node,
1326 * i.e. component image node.
1328 printf ("%s Image %u (%s)\n", p, count++,
1329 fit_get_name(fit, noffset, NULL));
1331 fit_image_print (fit, noffset, p);
1335 /* Find configurations parent node offset */
1336 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1337 if (confs_noffset < 0) {
1338 debug ("Can't get configurations parent node '%s' (%s)\n",
1339 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1343 /* get default configuration unit name from default property */
1344 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1346 printf ("%s Default Configuration: '%s'\n", p, uname);
1348 /* Process its subnodes, print out configurations details */
1349 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1350 (noffset >= 0) && (ndepth > 0);
1351 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1354 * Direct child node of the configurations parent node,
1355 * i.e. configuration node.
1357 printf ("%s Configuration %u (%s)\n", p, count++,
1358 fit_get_name(fit, noffset, NULL));
1360 fit_conf_print (fit, noffset, p);
1365 inline void fit_print_contents (const void *fit)
1367 __fit_print_contents (fit, " ");
1370 inline void fit_print_contents_noindent (const void *fit)
1372 __fit_print_contents (fit, "");
1376 * fit_image_print - prints out the FIT component image details
1377 * @fit: pointer to the FIT format image header
1378 * @image_noffset: offset of the component image node
1379 * @p: pointer to prefix string
1381 * fit_image_print() lists all mandatory properies for the processed component
1382 * image. If present, hash nodes are printed out as well.
1385 * no returned results
1387 void fit_image_print (const void *fit, int image_noffset, const char *p)
1390 uint8_t type, arch, os, comp;
1398 /* Mandatory properties */
1399 ret = fit_get_desc (fit, image_noffset, &desc);
1400 printf ("%s Description: ", p);
1402 printf ("unavailable\n");
1404 printf ("%s\n", desc);
1406 fit_image_get_type (fit, image_noffset, &type);
1407 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1409 fit_image_get_comp (fit, image_noffset, &comp);
1410 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1412 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1415 printf ("%s Data Start: ", p);
1417 printf ("unavailable\n");
1419 printf ("0x%08lx\n", (ulong)data);
1422 printf ("%s Data Size: ", p);
1424 printf ("unavailable\n");
1426 genimg_print_size (size);
1428 /* Remaining, type dependent properties */
1429 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1430 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1431 (type == IH_TYPE_FLATDT)) {
1432 fit_image_get_arch (fit, image_noffset, &arch);
1433 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1436 if (type == IH_TYPE_KERNEL) {
1437 fit_image_get_os (fit, image_noffset, &os);
1438 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1441 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1442 ret = fit_image_get_load (fit, image_noffset, &load);
1443 printf ("%s Load Address: ", p);
1445 printf ("unavailable\n");
1447 printf ("0x%08lx\n", load);
1449 fit_image_get_entry (fit, image_noffset, &entry);
1450 printf ("%s Entry Point: ", p);
1452 printf ("unavailable\n");
1454 printf ("0x%08lx\n", entry);
1457 /* Process all hash subnodes of the component image node */
1458 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1459 (noffset >= 0) && (ndepth > 0);
1460 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1462 /* Direct child node of the component image node */
1463 fit_image_print_hash (fit, noffset, p);
1469 * fit_image_print_hash - prints out the hash node details
1470 * @fit: pointer to the FIT format image header
1471 * @noffset: offset of the hash node
1472 * @p: pointer to prefix string
1474 * fit_image_print_hash() lists properies for the processed hash node
1477 * no returned results
1479 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1487 * Check subnode name, must be equal to "hash".
1488 * Multiple hash nodes require unique unit node
1489 * names, e.g. hash@1, hash@2, etc.
1491 if (strncmp (fit_get_name(fit, noffset, NULL),
1493 strlen(FIT_HASH_NODENAME)) != 0)
1496 debug ("%s Hash node: '%s'\n", p,
1497 fit_get_name (fit, noffset, NULL));
1499 printf ("%s Hash algo: ", p);
1500 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1501 printf ("invalid/unsupported\n");
1504 printf ("%s\n", algo);
1506 ret = fit_image_hash_get_value (fit, noffset, &value,
1508 printf ("%s Hash value: ", p);
1510 printf ("unavailable\n");
1512 for (i = 0; i < value_len; i++)
1513 printf ("%02x", value[i]);
1517 debug ("%s Hash len: %d\n", p, value_len);
1521 * fit_get_desc - get node description property
1522 * @fit: pointer to the FIT format image header
1523 * @noffset: node offset
1524 * @desc: double pointer to the char, will hold pointer to the descrption
1526 * fit_get_desc() reads description property from a given node, if
1527 * description is found pointer to it is returened in third call argument.
1533 int fit_get_desc (const void *fit, int noffset, char **desc)
1537 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1538 if (*desc == NULL) {
1539 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1547 * fit_get_timestamp - get node timestamp property
1548 * @fit: pointer to the FIT format image header
1549 * @noffset: node offset
1550 * @timestamp: pointer to the time_t, will hold read timestamp
1552 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1553 * is found and has a correct size its value is retured in third call
1558 * -1, on property read failure
1559 * -2, on wrong timestamp size
1561 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1566 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1568 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1571 if (len != sizeof (uint32_t)) {
1572 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1576 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1581 * fit_image_get_node - get node offset for component image of a given unit name
1582 * @fit: pointer to the FIT format image header
1583 * @image_uname: component image node unit name
1585 * fit_image_get_node() finds a component image (withing the '/images'
1586 * node) of a provided unit name. If image is found its node offset is
1587 * returned to the caller.
1590 * image node offset when found (>=0)
1591 * negative number on failure (FDT_ERR_* code)
1593 int fit_image_get_node (const void *fit, const char *image_uname)
1595 int noffset, images_noffset;
1597 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1598 if (images_noffset < 0) {
1599 debug ("Can't find images parent node '%s' (%s)\n",
1600 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1601 return images_noffset;
1604 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1606 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1607 image_uname, fdt_strerror (noffset));
1614 * fit_image_get_os - get os id for a given component image node
1615 * @fit: pointer to the FIT format image header
1616 * @noffset: component image node offset
1617 * @os: pointer to the uint8_t, will hold os numeric id
1619 * fit_image_get_os() finds os property in a given component image node.
1620 * If the property is found, its (string) value is translated to the numeric
1621 * id which is returned to the caller.
1627 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1632 /* Get OS name from property data */
1633 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1635 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1640 /* Translate OS name to id */
1641 *os = genimg_get_os_id (data);
1646 * fit_image_get_arch - get arch id for a given component image node
1647 * @fit: pointer to the FIT format image header
1648 * @noffset: component image node offset
1649 * @arch: pointer to the uint8_t, will hold arch numeric id
1651 * fit_image_get_arch() finds arch property in a given component image node.
1652 * If the property is found, its (string) value is translated to the numeric
1653 * id which is returned to the caller.
1659 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1664 /* Get architecture name from property data */
1665 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1667 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1672 /* Translate architecture name to id */
1673 *arch = genimg_get_arch_id (data);
1678 * fit_image_get_type - get type id for a given component image node
1679 * @fit: pointer to the FIT format image header
1680 * @noffset: component image node offset
1681 * @type: pointer to the uint8_t, will hold type numeric id
1683 * fit_image_get_type() finds type property in a given component image node.
1684 * If the property is found, its (string) value is translated to the numeric
1685 * id which is returned to the caller.
1691 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1696 /* Get image type name from property data */
1697 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1699 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1704 /* Translate image type name to id */
1705 *type = genimg_get_type_id (data);
1710 * fit_image_get_comp - get comp id for a given component image node
1711 * @fit: pointer to the FIT format image header
1712 * @noffset: component image node offset
1713 * @comp: pointer to the uint8_t, will hold comp numeric id
1715 * fit_image_get_comp() finds comp property in a given component image node.
1716 * If the property is found, its (string) value is translated to the numeric
1717 * id which is returned to the caller.
1723 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1728 /* Get compression name from property data */
1729 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1731 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1736 /* Translate compression name to id */
1737 *comp = genimg_get_comp_id (data);
1742 * fit_image_get_load - get load address property for a given component image node
1743 * @fit: pointer to the FIT format image header
1744 * @noffset: component image node offset
1745 * @load: pointer to the uint32_t, will hold load address
1747 * fit_image_get_load() finds load address property in a given component image node.
1748 * If the property is found, its value is returned to the caller.
1754 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1757 const uint32_t *data;
1759 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1761 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1765 *load = uimage_to_cpu (*data);
1770 * fit_image_get_entry - get entry point address property for a given component image node
1771 * @fit: pointer to the FIT format image header
1772 * @noffset: component image node offset
1773 * @entry: pointer to the uint32_t, will hold entry point address
1775 * fit_image_get_entry() finds entry point address property in a given component image node.
1776 * If the property is found, its value is returned to the caller.
1782 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1785 const uint32_t *data;
1787 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1789 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1793 *entry = uimage_to_cpu (*data);
1798 * fit_image_get_data - get data property and its size for a given component image node
1799 * @fit: pointer to the FIT format image header
1800 * @noffset: component image node offset
1801 * @data: double pointer to void, will hold data property's data address
1802 * @size: pointer to size_t, will hold data property's data size
1804 * fit_image_get_data() finds data property in a given component image node.
1805 * If the property is found its data start address and size are returned to
1812 int fit_image_get_data (const void *fit, int noffset,
1813 const void **data, size_t *size)
1817 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1818 if (*data == NULL) {
1819 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1829 * fit_image_hash_get_algo - get hash algorithm name
1830 * @fit: pointer to the FIT format image header
1831 * @noffset: hash node offset
1832 * @algo: double pointer to char, will hold pointer to the algorithm name
1834 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1835 * If the property is found its data start address is returned to the caller.
1841 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1845 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1846 if (*algo == NULL) {
1847 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1855 * fit_image_hash_get_value - get hash value and length
1856 * @fit: pointer to the FIT format image header
1857 * @noffset: hash node offset
1858 * @value: double pointer to uint8_t, will hold address of a hash value data
1859 * @value_len: pointer to an int, will hold hash data length
1861 * fit_image_hash_get_value() finds hash value property in a given hash node.
1862 * If the property is found its data start address and size are returned to
1869 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1874 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1875 if (*value == NULL) {
1876 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1886 * fit_set_timestamp - set node timestamp property
1887 * @fit: pointer to the FIT format image header
1888 * @noffset: node offset
1889 * @timestamp: timestamp value to be set
1891 * fit_set_timestamp() attempts to set timestamp property in the requested
1892 * node and returns operation status to the caller.
1896 * -1, on property read failure
1898 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1903 t = cpu_to_uimage (timestamp);
1904 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1907 printf ("Can't set '%s' property for '%s' node (%s)\n",
1908 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1909 fdt_strerror (ret));
1917 * calculate_hash - calculate and return hash for provided input data
1918 * @data: pointer to the input data
1919 * @data_len: data length
1920 * @algo: requested hash algorithm
1921 * @value: pointer to the char, will hold hash value data (caller must
1922 * allocate enough free space)
1923 * value_len: length of the calculated hash
1925 * calculate_hash() computes input data hash according to the requested algorithm.
1926 * Resulting hash value is placed in caller provided 'value' buffer, length
1927 * of the calculated hash is returned via value_len pointer argument.
1931 * -1, when algo is unsupported
1933 static int calculate_hash (const void *data, int data_len, const char *algo,
1934 uint8_t *value, int *value_len)
1936 if (strcmp (algo, "crc32") == 0 ) {
1937 *((uint32_t *)value) = crc32 (0, data, data_len);
1938 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1940 } else if (strcmp (algo, "sha1") == 0 ) {
1941 sha1_csum ((unsigned char *) data, data_len,
1942 (unsigned char *) value);
1944 } else if (strcmp (algo, "md5") == 0 ) {
1945 md5 ((unsigned char *)data, data_len, value);
1948 debug ("Unsupported hash alogrithm\n");
1956 * fit_set_hashes - process FIT component image nodes and calculate hashes
1957 * @fit: pointer to the FIT format image header
1959 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1960 * Hashes are calculated for all component images which have hash subnodes
1961 * with algorithm property set to one of the supported hash algorithms.
1965 * libfdt error code, on failure
1967 int fit_set_hashes (void *fit)
1974 /* Find images parent node offset */
1975 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1976 if (images_noffset < 0) {
1977 printf ("Can't find images parent node '%s' (%s)\n",
1978 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1979 return images_noffset;
1982 /* Process its subnodes, print out component images details */
1983 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1984 (noffset >= 0) && (ndepth > 0);
1985 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1988 * Direct child node of the images parent node,
1989 * i.e. component image node.
1991 ret = fit_image_set_hashes (fit, noffset);
2001 * fit_image_set_hashes - calculate/set hashes for given component image node
2002 * @fit: pointer to the FIT format image header
2003 * @image_noffset: requested component image node
2005 * fit_image_set_hashes() adds hash values for an component image node. All
2006 * existing hash subnodes are checked, if algorithm property is set to one of
2007 * the supported hash algorithms, hash value is computed and corresponding
2008 * hash node property is set, for example:
2010 * Input component image node structure:
2012 * o image@1 (at image_noffset)
2013 * | - data = [binary data]
2017 * Output component image node structure:
2019 * o image@1 (at image_noffset)
2020 * | - data = [binary data]
2023 * |- value = sha1(data)
2029 int fit_image_set_hashes (void *fit, int image_noffset)
2034 uint8_t value[FIT_MAX_HASH_LEN];
2039 /* Get image data and data length */
2040 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2041 printf ("Can't get image data/size\n");
2045 /* Process all hash subnodes of the component image node */
2046 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2047 (noffset >= 0) && (ndepth > 0);
2048 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2050 /* Direct child node of the component image node */
2053 * Check subnode name, must be equal to "hash".
2054 * Multiple hash nodes require unique unit node
2055 * names, e.g. hash@1, hash@2, etc.
2057 if (strncmp (fit_get_name(fit, noffset, NULL),
2059 strlen(FIT_HASH_NODENAME)) != 0) {
2060 /* Not a hash subnode, skip it */
2064 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2065 printf ("Can't get hash algo property for "
2066 "'%s' hash node in '%s' image node\n",
2067 fit_get_name (fit, noffset, NULL),
2068 fit_get_name (fit, image_noffset, NULL));
2072 if (calculate_hash (data, size, algo, value, &value_len)) {
2073 printf ("Unsupported hash algorithm (%s) for "
2074 "'%s' hash node in '%s' image node\n",
2075 algo, fit_get_name (fit, noffset, NULL),
2076 fit_get_name (fit, image_noffset, NULL));
2080 if (fit_image_hash_set_value (fit, noffset, value,
2082 printf ("Can't set hash value for "
2083 "'%s' hash node in '%s' image node\n",
2084 fit_get_name (fit, noffset, NULL),
2085 fit_get_name (fit, image_noffset, NULL));
2095 * fit_image_hash_set_value - set hash value in requested has node
2096 * @fit: pointer to the FIT format image header
2097 * @noffset: hash node offset
2098 * @value: hash value to be set
2099 * @value_len: hash value length
2101 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2102 * given and returns operation status to the caller.
2108 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2113 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2115 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2116 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2117 fdt_strerror (ret));
2123 #endif /* USE_HOSTCC */
2126 * fit_image_check_hashes - verify data intergity
2127 * @fit: pointer to the FIT format image header
2128 * @image_noffset: component image node offset
2130 * fit_image_check_hashes() goes over component image hash nodes,
2131 * re-calculates each data hash and compares with the value stored in hash
2135 * 1, if all hashes are valid
2136 * 0, otherwise (or on error)
2138 int fit_image_check_hashes (const void *fit, int image_noffset)
2145 uint8_t value[FIT_MAX_HASH_LEN];
2151 /* Get image data and data length */
2152 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2153 printf ("Can't get image data/size\n");
2157 /* Process all hash subnodes of the component image node */
2158 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2159 (noffset >= 0) && (ndepth > 0);
2160 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2162 /* Direct child node of the component image node */
2165 * Check subnode name, must be equal to "hash".
2166 * Multiple hash nodes require unique unit node
2167 * names, e.g. hash@1, hash@2, etc.
2169 if (strncmp (fit_get_name(fit, noffset, NULL),
2171 strlen(FIT_HASH_NODENAME)) != 0)
2174 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2175 err_msg = "Can't get hash algo property";
2178 printf ("%s", algo);
2180 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2182 err_msg = "Can't get hash value property";
2186 if (calculate_hash (data, size, algo, value, &value_len)) {
2187 err_msg = "Unsupported hash algorithm";
2191 if (value_len != fit_value_len) {
2192 err_msg = "Bad hash value len";
2194 } else if (memcmp (value, fit_value, value_len) != 0) {
2195 err_msg = "Bad hash value";
2205 printf ("%s for '%s' hash node in '%s' image node\n",
2206 err_msg, fit_get_name (fit, noffset, NULL),
2207 fit_get_name (fit, image_noffset, NULL));
2212 * fit_image_check_os - check whether image node is of a given os type
2213 * @fit: pointer to the FIT format image header
2214 * @noffset: component image node offset
2215 * @os: requested image os
2217 * fit_image_check_os() reads image os property and compares its numeric
2218 * id with the requested os. Comparison result is returned to the caller.
2221 * 1 if image is of given os type
2222 * 0 otherwise (or on error)
2224 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2228 if (fit_image_get_os (fit, noffset, &image_os))
2230 return (os == image_os);
2234 * fit_image_check_arch - check whether image node is of a given arch
2235 * @fit: pointer to the FIT format image header
2236 * @noffset: component image node offset
2237 * @arch: requested imagearch
2239 * fit_image_check_arch() reads image arch property and compares its numeric
2240 * id with the requested arch. Comparison result is returned to the caller.
2243 * 1 if image is of given arch
2244 * 0 otherwise (or on error)
2246 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2250 if (fit_image_get_arch (fit, noffset, &image_arch))
2252 return (arch == image_arch);
2256 * fit_image_check_type - check whether image node is of a given type
2257 * @fit: pointer to the FIT format image header
2258 * @noffset: component image node offset
2259 * @type: requested image type
2261 * fit_image_check_type() reads image type property and compares its numeric
2262 * id with the requested type. Comparison result is returned to the caller.
2265 * 1 if image is of given type
2266 * 0 otherwise (or on error)
2268 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2272 if (fit_image_get_type (fit, noffset, &image_type))
2274 return (type == image_type);
2278 * fit_image_check_comp - check whether image node uses given compression
2279 * @fit: pointer to the FIT format image header
2280 * @noffset: component image node offset
2281 * @comp: requested image compression type
2283 * fit_image_check_comp() reads image compression property and compares its
2284 * numeric id with the requested compression type. Comparison result is
2285 * returned to the caller.
2288 * 1 if image uses requested compression
2289 * 0 otherwise (or on error)
2291 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2295 if (fit_image_get_comp (fit, noffset, &image_comp))
2297 return (comp == image_comp);
2301 * fit_check_format - sanity check FIT image format
2302 * @fit: pointer to the FIT format image header
2304 * fit_check_format() runs a basic sanity FIT image verification.
2305 * Routine checks for mandatory properties, nodes, etc.
2311 int fit_check_format (const void *fit)
2313 /* mandatory / node 'description' property */
2314 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2315 debug ("Wrong FIT format: no description\n");
2319 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2320 /* mandatory / node 'timestamp' property */
2321 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2322 debug ("Wrong FIT format: no description\n");
2327 /* mandatory subimages parent '/images' node */
2328 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2329 debug ("Wrong FIT format: no images parent node\n");
2337 * fit_conf_get_node - get node offset for configuration of a given unit name
2338 * @fit: pointer to the FIT format image header
2339 * @conf_uname: configuration node unit name
2341 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2342 * parant node) of a provided unit name. If configuration is found its node offset
2343 * is returned to the caller.
2345 * When NULL is provided in second argument fit_conf_get_node() will search
2346 * for a default configuration node instead. Default configuration node unit name
2347 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2350 * configuration node offset when found (>=0)
2351 * negative number on failure (FDT_ERR_* code)
2353 int fit_conf_get_node (const void *fit, const char *conf_uname)
2355 int noffset, confs_noffset;
2358 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2359 if (confs_noffset < 0) {
2360 debug ("Can't find configurations parent node '%s' (%s)\n",
2361 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2362 return confs_noffset;
2365 if (conf_uname == NULL) {
2366 /* get configuration unit name from the default property */
2367 debug ("No configuration specified, trying default...\n");
2368 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2369 if (conf_uname == NULL) {
2370 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2373 debug ("Found default configuration: '%s'\n", conf_uname);
2376 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2378 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2379 conf_uname, fdt_strerror (noffset));
2385 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2386 const char *prop_name)
2391 /* get kernel image unit name from configuration kernel property */
2392 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2396 return fit_image_get_node (fit, uname);
2400 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2401 * a given configuration
2402 * @fit: pointer to the FIT format image header
2403 * @noffset: configuration node offset
2405 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2406 * configuration FIT_KERNEL_PROP property and translates it to the node
2410 * image node offset when found (>=0)
2411 * negative number on failure (FDT_ERR_* code)
2413 int fit_conf_get_kernel_node (const void *fit, int noffset)
2415 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2419 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2420 * a given configuration
2421 * @fit: pointer to the FIT format image header
2422 * @noffset: configuration node offset
2424 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2425 * configuration FIT_KERNEL_PROP property and translates it to the node
2429 * image node offset when found (>=0)
2430 * negative number on failure (FDT_ERR_* code)
2432 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2434 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2438 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2439 * a given configuration
2440 * @fit: pointer to the FIT format image header
2441 * @noffset: configuration node offset
2443 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2444 * configuration FIT_KERNEL_PROP property and translates it to the node
2448 * image node offset when found (>=0)
2449 * negative number on failure (FDT_ERR_* code)
2451 int fit_conf_get_fdt_node (const void *fit, int noffset)
2453 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2457 * fit_conf_print - prints out the FIT configuration details
2458 * @fit: pointer to the FIT format image header
2459 * @noffset: offset of the configuration node
2460 * @p: pointer to prefix string
2462 * fit_conf_print() lists all mandatory properies for the processed
2463 * configuration node.
2466 * no returned results
2468 void fit_conf_print (const void *fit, int noffset, const char *p)
2474 /* Mandatory properties */
2475 ret = fit_get_desc (fit, noffset, &desc);
2476 printf ("%s Description: ", p);
2478 printf ("unavailable\n");
2480 printf ("%s\n", desc);
2482 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2483 printf ("%s Kernel: ", p);
2485 printf ("unavailable\n");
2487 printf ("%s\n", uname);
2489 /* Optional properties */
2490 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2492 printf ("%s Init Ramdisk: %s\n", p, uname);
2494 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2496 printf ("%s FDT: %s\n", p, uname);
2500 * fit_check_ramdisk - verify FIT format ramdisk subimage
2501 * @fit_hdr: pointer to the FIT ramdisk header
2502 * @rd_noffset: ramdisk subimage node offset within FIT image
2503 * @arch: requested ramdisk image architecture type
2504 * @verify: data CRC verification flag
2506 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2507 * specified FIT image.
2514 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2516 fit_image_print (fit, rd_noffset, " ");
2519 puts (" Verifying Hash Integrity ... ");
2520 if (!fit_image_check_hashes (fit, rd_noffset)) {
2521 puts ("Bad Data Hash\n");
2522 show_boot_progress (-125);
2528 show_boot_progress (126);
2529 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2530 !fit_image_check_arch (fit, rd_noffset, arch) ||
2531 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2532 printf ("No Linux %s Ramdisk Image\n",
2533 genimg_get_arch_name(arch));
2534 show_boot_progress (-126);
2538 show_boot_progress (127);
2541 #endif /* USE_HOSTCC */
2542 #endif /* CONFIG_FIT */