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,
31 #ifdef CONFIG_SHOW_BOOT_PROGRESS
32 #include <status_led.h>
35 #ifdef CONFIG_HAS_DATAFLASH
36 #include <dataflash.h>
39 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
45 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
48 #include <fdt_support.h>
51 #if defined(CONFIG_FIT)
52 #include <u-boot/md5.h>
55 static int fit_check_ramdisk (const void *fit, int os_noffset,
56 uint8_t arch, int verify);
60 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
63 DECLARE_GLOBAL_DATA_PTR;
65 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
69 #include <u-boot/md5.h>
72 #endif /* !USE_HOSTCC*/
74 typedef struct table_entry {
75 int id; /* as defined in image.h */
76 char *sname; /* short (input) name */
77 char *lname; /* long (output) name */
80 static table_entry_t uimage_arch[] = {
81 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
82 { IH_ARCH_ALPHA, "alpha", "Alpha", },
83 { IH_ARCH_ARM, "arm", "ARM", },
84 { IH_ARCH_I386, "x86", "Intel x86", },
85 { IH_ARCH_IA64, "ia64", "IA64", },
86 { IH_ARCH_M68K, "m68k", "M68K", },
87 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
88 { IH_ARCH_MIPS, "mips", "MIPS", },
89 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
90 { IH_ARCH_NIOS, "nios", "NIOS", },
91 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
92 { IH_ARCH_PPC, "powerpc", "PowerPC", },
93 { IH_ARCH_PPC, "ppc", "PowerPC", },
94 { IH_ARCH_S390, "s390", "IBM S390", },
95 { IH_ARCH_SH, "sh", "SuperH", },
96 { IH_ARCH_SPARC, "sparc", "SPARC", },
97 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
98 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
99 { IH_ARCH_AVR32, "avr32", "AVR32", },
103 static table_entry_t uimage_os[] = {
104 { IH_OS_INVALID, NULL, "Invalid OS", },
105 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
106 { IH_OS_ARTOS, "artos", "ARTOS", },
108 { IH_OS_LINUX, "linux", "Linux", },
109 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
110 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
112 { IH_OS_NETBSD, "netbsd", "NetBSD", },
113 { IH_OS_RTEMS, "rtems", "RTEMS", },
114 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
115 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
116 { IH_OS_QNX, "qnx", "QNX", },
117 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
120 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
121 { IH_OS_DELL, "dell", "Dell", },
122 { IH_OS_ESIX, "esix", "Esix", },
123 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
124 { IH_OS_IRIX, "irix", "Irix", },
125 { IH_OS_NCR, "ncr", "NCR", },
126 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
127 { IH_OS_PSOS, "psos", "pSOS", },
128 { IH_OS_SCO, "sco", "SCO", },
129 { IH_OS_SOLARIS, "solaris", "Solaris", },
130 { IH_OS_SVR4, "svr4", "SVR4", },
135 static table_entry_t uimage_type[] = {
136 { IH_TYPE_INVALID, NULL, "Invalid Image", },
137 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
138 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
139 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
140 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
141 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
142 { IH_TYPE_SCRIPT, "script", "Script", },
143 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
144 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
148 static table_entry_t uimage_comp[] = {
149 { IH_COMP_NONE, "none", "uncompressed", },
150 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
151 { IH_COMP_GZIP, "gzip", "gzip compressed", },
155 uint32_t crc32 (uint32_t, const unsigned char *, uint);
156 uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
157 static void genimg_print_size (uint32_t size);
158 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
159 static void genimg_print_time (time_t timestamp);
162 /*****************************************************************************/
163 /* Legacy format routines */
164 /*****************************************************************************/
165 int image_check_hcrc (image_header_t *hdr)
168 ulong len = image_get_header_size ();
169 image_header_t header;
171 /* Copy header so we can blank CRC field for re-calculation */
172 memmove (&header, (char *)hdr, image_get_header_size ());
173 image_set_hcrc (&header, 0);
175 hcrc = crc32 (0, (unsigned char *)&header, len);
177 return (hcrc == image_get_hcrc (hdr));
180 int image_check_dcrc (image_header_t *hdr)
182 ulong data = image_get_data (hdr);
183 ulong len = image_get_data_size (hdr);
184 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
186 return (dcrc == image_get_dcrc (hdr));
191 * image_multi_count - get component (sub-image) count
192 * @hdr: pointer to the header of the multi component image
194 * image_multi_count() returns number of components in a multi
197 * Note: no checking of the image type is done, caller must pass
198 * a valid multi component image.
201 * number of components
203 ulong image_multi_count (image_header_t *hdr)
208 /* get start of the image payload, which in case of multi
209 * component images that points to a table of component sizes */
210 size = (uint32_t *)image_get_data (hdr);
212 /* count non empty slots */
213 for (i = 0; size[i]; ++i)
220 * image_multi_getimg - get component data address and size
221 * @hdr: pointer to the header of the multi component image
222 * @idx: index of the requested component
223 * @data: pointer to a ulong variable, will hold component data address
224 * @len: pointer to a ulong variable, will hold component size
226 * image_multi_getimg() returns size and data address for the requested
227 * component in a multi component image.
229 * Note: no checking of the image type is done, caller must pass
230 * a valid multi component image.
233 * data address and size of the component, if idx is valid
234 * 0 in data and len, if idx is out of range
236 void image_multi_getimg (image_header_t *hdr, ulong idx,
237 ulong *data, ulong *len)
241 ulong offset, tail, count, img_data;
243 /* get number of component */
244 count = image_multi_count (hdr);
246 /* get start of the image payload, which in case of multi
247 * component images that points to a table of component sizes */
248 size = (uint32_t *)image_get_data (hdr);
250 /* get address of the proper component data start, which means
251 * skipping sizes table (add 1 for last, null entry) */
252 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
255 *len = uimage_to_cpu (size[idx]);
259 /* go over all indices preceding requested component idx */
260 for (i = 0; i < idx; i++) {
261 /* add up i-th component size */
262 offset += uimage_to_cpu (size[i]);
264 /* add up alignment for i-th component */
265 tail += (4 - uimage_to_cpu (size[i]) % 4);
268 /* calculate idx-th component data address */
269 *data = img_data + offset + tail;
276 static void image_print_type (image_header_t *hdr)
278 const char *os, *arch, *type, *comp;
280 os = genimg_get_os_name (image_get_os (hdr));
281 arch = genimg_get_arch_name (image_get_arch (hdr));
282 type = genimg_get_type_name (image_get_type (hdr));
283 comp = genimg_get_comp_name (image_get_comp (hdr));
285 printf ("%s %s %s (%s)\n", arch, os, type, comp);
289 * image_print_contents - prints out the contents of the legacy format image
290 * @hdr: pointer to the legacy format image header
291 * @p: pointer to prefix string
293 * image_print_contents() formats a multi line legacy image contents description.
294 * The routine prints out all header fields followed by the size/offset data
295 * for MULTI/SCRIPT images.
298 * no returned results
300 void image_print_contents (image_header_t *hdr)
310 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
311 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
312 printf ("%sCreated: ", p);
313 genimg_print_time ((time_t)image_get_time (hdr));
315 printf ("%sImage Type: ", p);
316 image_print_type (hdr);
317 printf ("%sData Size: ", p);
318 genimg_print_size (image_get_data_size (hdr));
319 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
320 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
322 if (image_check_type (hdr, IH_TYPE_MULTI) ||
323 image_check_type (hdr, IH_TYPE_SCRIPT)) {
326 ulong count = image_multi_count (hdr);
328 printf ("%sContents:\n", p);
329 for (i = 0; i < count; i++) {
330 image_multi_getimg (hdr, i, &data, &len);
332 printf ("%s Image %d: ", p, i);
333 genimg_print_size (len);
335 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
337 * the user may need to know offsets
338 * if planning to do something with
341 printf ("%s Offset = 0x%08lx\n", p, data);
350 * image_get_ramdisk - get and verify ramdisk image
351 * @rd_addr: ramdisk image start address
352 * @arch: expected ramdisk architecture
353 * @verify: checksum verification flag
355 * image_get_ramdisk() returns a pointer to the verified ramdisk image
356 * header. Routine receives image start address and expected architecture
357 * flag. Verification done covers data and header integrity and os/type/arch
360 * If dataflash support is enabled routine checks for dataflash addresses
361 * and handles required dataflash reads.
364 * pointer to a ramdisk image header, if image was found and valid
365 * otherwise, return NULL
367 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
370 image_header_t *rd_hdr = (image_header_t *)rd_addr;
372 if (!image_check_magic (rd_hdr)) {
373 puts ("Bad Magic Number\n");
374 show_boot_progress (-10);
378 if (!image_check_hcrc (rd_hdr)) {
379 puts ("Bad Header Checksum\n");
380 show_boot_progress (-11);
384 show_boot_progress (10);
385 image_print_contents (rd_hdr);
388 puts(" Verifying Checksum ... ");
389 if (!image_check_dcrc (rd_hdr)) {
390 puts ("Bad Data CRC\n");
391 show_boot_progress (-12);
397 show_boot_progress (11);
399 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
400 !image_check_arch (rd_hdr, arch) ||
401 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
402 printf ("No Linux %s Ramdisk Image\n",
403 genimg_get_arch_name(arch));
404 show_boot_progress (-13);
410 #endif /* !USE_HOSTCC */
412 /*****************************************************************************/
413 /* Shared dual-format routines */
414 /*****************************************************************************/
416 int getenv_yesno (char *var)
418 char *s = getenv (var);
419 return (s && (*s == 'n')) ? 0 : 1;
422 ulong getenv_bootm_low(void)
424 char *s = getenv ("bootm_low");
426 ulong tmp = simple_strtoul (s, NULL, 16);
430 #if defined(CFG_SDRAM_BASE)
431 return CFG_SDRAM_BASE;
432 #elif defined(CONFIG_ARM)
433 return gd->bd->bi_dram[0].start;
439 ulong getenv_bootm_size(void)
441 char *s = getenv ("bootm_size");
443 ulong tmp = simple_strtoul (s, NULL, 16);
447 #if defined(CONFIG_ARM)
448 return gd->bd->bi_dram[0].size;
450 return gd->bd->bi_memsize;
454 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
456 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
458 size_t tail = (len > chunksz) ? chunksz : len;
460 memmove (to, from, tail);
465 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
466 memmove (to, from, len);
467 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
469 #endif /* !USE_HOSTCC */
471 static void genimg_print_size (uint32_t size)
474 printf ("%d Bytes = ", size);
475 print_size (size, "\n");
477 printf ("%d Bytes = %.2f kB = %.2f MB\n",
478 size, (double)size / 1.024e3,
479 (double)size / 1.048576e6);
483 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
484 static void genimg_print_time (time_t timestamp)
489 to_tm (timestamp, &tm);
490 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
491 tm.tm_year, tm.tm_mon, tm.tm_mday,
492 tm.tm_hour, tm.tm_min, tm.tm_sec);
494 printf ("%s", ctime(×tamp));
497 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
500 * get_table_entry_name - translate entry id to long name
501 * @table: pointer to a translation table for entries of a specific type
502 * @msg: message to be returned when translation fails
503 * @id: entry id to be translated
505 * get_table_entry_name() will go over translation table trying to find
506 * entry that matches given id. If matching entry is found, its long
507 * name is returned to the caller.
510 * long entry name if translation succeeds
513 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
515 for (; table->id >= 0; ++table) {
517 return (table->lname);
522 const char *genimg_get_os_name (uint8_t os)
524 return (get_table_entry_name (uimage_os, "Unknown OS", os));
527 const char *genimg_get_arch_name (uint8_t arch)
529 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
532 const char *genimg_get_type_name (uint8_t type)
534 return (get_table_entry_name (uimage_type, "Unknown Image", type));
537 const char *genimg_get_comp_name (uint8_t comp)
539 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
543 * get_table_entry_id - translate short entry name to id
544 * @table: pointer to a translation table for entries of a specific type
545 * @table_name: to be used in case of error
546 * @name: entry short name to be translated
548 * get_table_entry_id() will go over translation table trying to find
549 * entry that matches given short name. If matching entry is found,
550 * its id returned to the caller.
553 * entry id if translation succeeds
556 static int get_table_entry_id (table_entry_t *table,
557 const char *table_name, const char *name)
563 for (t = table; t->id >= 0; ++t) {
564 if (t->sname && strcasecmp(t->sname, name) == 0)
568 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
569 for (t = table; t->id >= 0; ++t) {
570 if (t->sname == NULL)
572 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
575 fprintf (stderr, "\n");
577 for (t = table; t->id >= 0; ++t) {
578 if (t->sname && strcmp(t->sname, name) == 0)
581 debug ("Invalid %s Type: %s\n", table_name, name);
582 #endif /* USE_HOSTCC */
586 int genimg_get_os_id (const char *name)
588 return (get_table_entry_id (uimage_os, "OS", name));
591 int genimg_get_arch_id (const char *name)
593 return (get_table_entry_id (uimage_arch, "CPU", name));
596 int genimg_get_type_id (const char *name)
598 return (get_table_entry_id (uimage_type, "Image", name));
601 int genimg_get_comp_id (const char *name)
603 return (get_table_entry_id (uimage_comp, "Compression", name));
608 * genimg_get_format - get image format type
609 * @img_addr: image start address
611 * genimg_get_format() checks whether provided address points to a valid
612 * legacy or FIT image.
614 * New uImage format and FDT blob are based on a libfdt. FDT blob
615 * may be passed directly or embedded in a FIT image. In both situations
616 * genimg_get_format() must be able to dectect libfdt header.
619 * image format type or IMAGE_FORMAT_INVALID if no image is present
621 int genimg_get_format (void *img_addr)
623 ulong format = IMAGE_FORMAT_INVALID;
625 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
629 hdr = (image_header_t *)img_addr;
630 if (image_check_magic(hdr))
631 format = IMAGE_FORMAT_LEGACY;
632 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
634 fit_hdr = (char *)img_addr;
635 if (fdt_check_header (fit_hdr) == 0)
636 format = IMAGE_FORMAT_FIT;
644 * genimg_get_image - get image from special storage (if necessary)
645 * @img_addr: image start address
647 * genimg_get_image() checks if provided image start adddress is located
648 * in a dataflash storage. If so, image is moved to a system RAM memory.
651 * image start address after possible relocation from special storage
653 ulong genimg_get_image (ulong img_addr)
655 ulong ram_addr = img_addr;
657 #ifdef CONFIG_HAS_DATAFLASH
658 ulong h_size, d_size;
660 if (addr_dataflash (img_addr)){
661 /* ger RAM address */
662 ram_addr = CFG_LOAD_ADDR;
664 /* get header size */
665 h_size = image_get_header_size ();
666 #if defined(CONFIG_FIT)
667 if (sizeof(struct fdt_header) > h_size)
668 h_size = sizeof(struct fdt_header);
672 debug (" Reading image header from dataflash address "
673 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
675 read_dataflash (img_addr, h_size, (char *)ram_addr);
678 switch (genimg_get_format ((void *)ram_addr)) {
679 case IMAGE_FORMAT_LEGACY:
680 d_size = image_get_data_size ((image_header_t *)ram_addr);
681 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
684 #if defined(CONFIG_FIT)
685 case IMAGE_FORMAT_FIT:
686 d_size = fit_get_size ((const void *)ram_addr) - h_size;
687 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
692 printf (" No valid image found at 0x%08lx\n", img_addr);
696 /* read in image data */
697 debug (" Reading image remaining data from dataflash address "
698 "%08lx to RAM address %08lx\n", img_addr + h_size,
701 read_dataflash (img_addr + h_size, d_size,
702 (char *)(ram_addr + h_size));
705 #endif /* CONFIG_HAS_DATAFLASH */
711 * fit_has_config - check if there is a valid FIT configuration
712 * @images: pointer to the bootm command headers structure
714 * fit_has_config() checks if there is a FIT configuration in use
715 * (if FTI support is present).
718 * 0, no FIT support or no configuration found
719 * 1, configuration found
721 int genimg_has_config (bootm_headers_t *images)
723 #if defined(CONFIG_FIT)
724 if (images->fit_uname_cfg)
731 * boot_get_ramdisk - main ramdisk handling routine
732 * @argc: command argument count
733 * @argv: command argument list
734 * @images: pointer to the bootm images structure
735 * @arch: expected ramdisk architecture
736 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
737 * @rd_end: pointer to a ulong variable, will hold ramdisk end
739 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
740 * Curently supported are the following ramdisk sources:
741 * - multicomponent kernel/ramdisk image,
742 * - commandline provided address of decicated ramdisk image.
745 * 0, if ramdisk image was found and valid, or skiped
746 * rd_start and rd_end are set to ramdisk start/end addresses if
747 * ramdisk image is found and valid
749 * 1, if ramdisk image is found but corrupted
750 * rd_start and rd_end are set to 0 if no ramdisk exists
752 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
753 uint8_t arch, ulong *rd_start, ulong *rd_end)
755 ulong rd_addr, rd_load;
756 ulong rd_data, rd_len;
757 image_header_t *rd_hdr;
758 #if defined(CONFIG_FIT)
760 const char *fit_uname_config = NULL;
761 const char *fit_uname_ramdisk = NULL;
773 * Look for a '-' which indicates to ignore the
776 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
777 debug ("## Skipping init Ramdisk\n");
778 rd_len = rd_data = 0;
779 } else if (argc >= 3 || genimg_has_config (images)) {
780 #if defined(CONFIG_FIT)
783 * If the init ramdisk comes from the FIT image and
784 * the FIT image address is omitted in the command
785 * line argument, try to use os FIT image address or
786 * default load address.
788 if (images->fit_uname_os)
789 default_addr = (ulong)images->fit_hdr_os;
791 default_addr = load_addr;
793 if (fit_parse_conf (argv[2], default_addr,
794 &rd_addr, &fit_uname_config)) {
795 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
796 fit_uname_config, rd_addr);
797 } else if (fit_parse_subimage (argv[2], default_addr,
798 &rd_addr, &fit_uname_ramdisk)) {
799 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
800 fit_uname_ramdisk, rd_addr);
804 rd_addr = simple_strtoul(argv[2], NULL, 16);
805 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
808 #if defined(CONFIG_FIT)
810 /* use FIT configuration provided in first bootm
813 rd_addr = (ulong)images->fit_hdr_os;
814 fit_uname_config = images->fit_uname_cfg;
815 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
816 fit_uname_config, rd_addr);
819 * Check whether configuration has ramdisk defined,
820 * if not, don't try to use it, quit silently.
822 fit_hdr = (void *)rd_addr;
823 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
824 if (cfg_noffset < 0) {
825 debug ("* ramdisk: no such config\n");
829 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
830 if (rd_noffset < 0) {
831 debug ("* ramdisk: no ramdisk in config\n");
837 /* copy from dataflash if needed */
838 rd_addr = genimg_get_image (rd_addr);
841 * Check if there is an initrd image at the
842 * address provided in the second bootm argument
843 * check image type, for FIT images get FIT node.
845 switch (genimg_get_format ((void *)rd_addr)) {
846 case IMAGE_FORMAT_LEGACY:
847 printf ("## Loading init Ramdisk from Legacy "
848 "Image at %08lx ...\n", rd_addr);
850 show_boot_progress (9);
851 rd_hdr = image_get_ramdisk (rd_addr, arch,
857 rd_data = image_get_data (rd_hdr);
858 rd_len = image_get_data_size (rd_hdr);
859 rd_load = image_get_load (rd_hdr);
861 #if defined(CONFIG_FIT)
862 case IMAGE_FORMAT_FIT:
863 fit_hdr = (void *)rd_addr;
864 printf ("## Loading init Ramdisk from FIT "
865 "Image at %08lx ...\n", rd_addr);
867 show_boot_progress (120);
868 if (!fit_check_format (fit_hdr)) {
869 puts ("Bad FIT ramdisk image format!\n");
870 show_boot_progress (-120);
873 show_boot_progress (121);
875 if (!fit_uname_ramdisk) {
877 * no ramdisk image node unit name, try to get config
878 * node first. If config unit node name is NULL
879 * fit_conf_get_node() will try to find default config node
881 show_boot_progress (122);
882 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
883 if (cfg_noffset < 0) {
884 puts ("Could not find configuration node\n");
885 show_boot_progress (-122);
888 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
889 printf (" Using '%s' configuration\n", fit_uname_config);
891 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
892 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
894 /* get ramdisk component image node offset */
895 show_boot_progress (123);
896 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
898 if (rd_noffset < 0) {
899 puts ("Could not find subimage node\n");
900 show_boot_progress (-124);
904 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
906 show_boot_progress (125);
907 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
910 /* get ramdisk image data address and length */
911 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
912 puts ("Could not find ramdisk subimage data!\n");
913 show_boot_progress (-127);
916 show_boot_progress (128);
918 rd_data = (ulong)data;
921 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
922 puts ("Can't get ramdisk subimage load address!\n");
923 show_boot_progress (-129);
926 show_boot_progress (129);
928 images->fit_hdr_rd = fit_hdr;
929 images->fit_uname_rd = fit_uname_ramdisk;
930 images->fit_noffset_rd = rd_noffset;
934 puts ("Wrong Ramdisk Image Format\n");
935 rd_data = rd_len = rd_load = 0;
938 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
940 * We need to copy the ramdisk to SRAM to let Linux boot
943 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
946 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
948 } else if (images->legacy_hdr_valid &&
949 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
951 * Now check if we have a legacy mult-component image,
952 * get second entry data start address and len.
954 show_boot_progress (13);
955 printf ("## Loading init Ramdisk from multi component "
956 "Legacy Image at %08lx ...\n",
957 (ulong)images->legacy_hdr_os);
959 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
964 show_boot_progress (14);
965 rd_len = rd_data = 0;
969 debug ("## No init Ramdisk\n");
972 *rd_end = rd_data + rd_len;
974 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
980 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
982 * boot_ramdisk_high - relocate init ramdisk
983 * @lmb: pointer to lmb handle, will be used for memory mgmt
984 * @rd_data: ramdisk data start address
985 * @rd_len: ramdisk data length
986 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
987 * start address (after possible relocation)
988 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
989 * end address (after possible relocation)
991 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
992 * variable and if requested ramdisk data is moved to a specified location.
994 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
995 * start/end addresses if ramdisk image start and len were provided,
996 * otherwise set initrd_start and initrd_end set to zeros.
1002 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1003 ulong *initrd_start, ulong *initrd_end)
1007 int initrd_copy_to_ram = 1;
1009 if ((s = getenv ("initrd_high")) != NULL) {
1010 /* a value of "no" or a similar string will act like 0,
1011 * turning the "load high" feature off. This is intentional.
1013 initrd_high = simple_strtoul (s, NULL, 16);
1014 if (initrd_high == ~0)
1015 initrd_copy_to_ram = 0;
1017 /* not set, no restrictions to load high */
1021 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1022 initrd_high, initrd_copy_to_ram);
1025 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1026 debug (" in-place initrd\n");
1027 *initrd_start = rd_data;
1028 *initrd_end = rd_data + rd_len;
1029 lmb_reserve(lmb, rd_data, rd_len);
1032 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1034 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
1036 if (*initrd_start == 0) {
1037 puts ("ramdisk - allocation error\n");
1040 show_boot_progress (12);
1042 *initrd_end = *initrd_start + rd_len;
1043 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1044 *initrd_start, *initrd_end);
1046 memmove_wd ((void *)*initrd_start,
1047 (void *)rd_data, rd_len, CHUNKSZ);
1055 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1056 *initrd_start, *initrd_end);
1065 * boot_get_cmdline - allocate and initialize kernel cmdline
1066 * @lmb: pointer to lmb handle, will be used for memory mgmt
1067 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1068 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1069 * @bootmap_base: ulong variable, holds offset in physical memory to
1072 * boot_get_cmdline() allocates space for kernel command line below
1073 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1074 * variable is present its contents is copied to allocated kernel
1081 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1087 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1088 CFG_BOOTMAPSZ + bootmap_base);
1090 if (cmdline == NULL)
1093 if ((s = getenv("bootargs")) == NULL)
1098 *cmd_start = (ulong) & cmdline[0];
1099 *cmd_end = *cmd_start + strlen(cmdline);
1101 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1107 * boot_get_kbd - allocate and initialize kernel copy of board info
1108 * @lmb: pointer to lmb handle, will be used for memory mgmt
1109 * @kbd: double pointer to board info data
1110 * @bootmap_base: ulong variable, holds offset in physical memory to
1113 * boot_get_kbd() allocates space for kernel copy of board info data below
1114 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1115 * the current u-boot board info data.
1121 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1123 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1124 CFG_BOOTMAPSZ + bootmap_base);
1130 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1132 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1133 do_bdinfo(NULL, 0, 0, NULL);
1138 #endif /* CONFIG_PPC || CONFIG_M68K */
1139 #endif /* !USE_HOSTCC */
1141 #if defined(CONFIG_FIT)
1142 /*****************************************************************************/
1143 /* New uImage format routines */
1144 /*****************************************************************************/
1146 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1147 ulong *addr, const char **name)
1154 sep = strchr (spec, sepc);
1157 *addr = simple_strtoul (spec, NULL, 16);
1167 * fit_parse_conf - parse FIT configuration spec
1168 * @spec: input string, containing configuration spec
1169 * @add_curr: current image address (to be used as a possible default)
1170 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1172 * @conf_name double pointer to a char, will hold pointer to a configuration
1175 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1176 * where <addr> is a FIT image address that contains configuration
1177 * with a <conf> unit name.
1179 * Address part is optional, and if omitted default add_curr will
1183 * 1 if spec is a valid configuration string,
1184 * addr and conf_name are set accordingly
1187 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1188 ulong *addr, const char **conf_name)
1190 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1194 * fit_parse_subimage - parse FIT subimage spec
1195 * @spec: input string, containing subimage spec
1196 * @add_curr: current image address (to be used as a possible default)
1197 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1199 * @image_name: double pointer to a char, will hold pointer to a subimage name
1201 * fit_parse_subimage() expects subimage spec in the for of
1202 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1203 * subimage with a <subimg> unit name.
1205 * Address part is optional, and if omitted default add_curr will
1209 * 1 if spec is a valid subimage string,
1210 * addr and image_name are set accordingly
1213 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1214 ulong *addr, const char **image_name)
1216 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1218 #endif /* !USE_HOSTCC */
1220 static void fit_get_debug (const void *fit, int noffset,
1221 char *prop_name, int err)
1223 debug ("Can't get '%s' property from FIT 0x%08lx, "
1224 "node: offset %d, name %s (%s)\n",
1225 prop_name, (ulong)fit, noffset,
1226 fit_get_name (fit, noffset, NULL),
1227 fdt_strerror (err));
1231 * fit_print_contents - prints out the contents of the FIT format image
1232 * @fit: pointer to the FIT format image header
1233 * @p: pointer to prefix string
1235 * fit_print_contents() formats a multi line FIT image contents description.
1236 * The routine prints out FIT image properties (root node level) follwed by
1237 * the details of each component image.
1240 * no returned results
1242 void fit_print_contents (const void *fit)
1253 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1263 /* Root node properties */
1264 ret = fit_get_desc (fit, 0, &desc);
1265 printf ("%sFIT description: ", p);
1267 printf ("unavailable\n");
1269 printf ("%s\n", desc);
1271 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1272 ret = fit_get_timestamp (fit, 0, ×tamp);
1273 printf ("%sCreated: ", p);
1275 printf ("unavailable\n");
1277 genimg_print_time (timestamp);
1280 /* Find images parent node offset */
1281 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1282 if (images_noffset < 0) {
1283 printf ("Can't find images parent node '%s' (%s)\n",
1284 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1288 /* Process its subnodes, print out component images details */
1289 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1290 (noffset >= 0) && (ndepth > 0);
1291 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1294 * Direct child node of the images parent node,
1295 * i.e. component image node.
1297 printf ("%s Image %u (%s)\n", p, count++,
1298 fit_get_name(fit, noffset, NULL));
1300 fit_image_print (fit, noffset, p);
1304 /* Find configurations parent node offset */
1305 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1306 if (confs_noffset < 0) {
1307 debug ("Can't get configurations parent node '%s' (%s)\n",
1308 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1312 /* get default configuration unit name from default property */
1313 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1315 printf ("%s Default Configuration: '%s'\n", p, uname);
1317 /* Process its subnodes, print out configurations details */
1318 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1319 (noffset >= 0) && (ndepth > 0);
1320 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1323 * Direct child node of the configurations parent node,
1324 * i.e. configuration node.
1326 printf ("%s Configuration %u (%s)\n", p, count++,
1327 fit_get_name(fit, noffset, NULL));
1329 fit_conf_print (fit, noffset, p);
1335 * fit_image_print - prints out the FIT component image details
1336 * @fit: pointer to the FIT format image header
1337 * @image_noffset: offset of the component image node
1338 * @p: pointer to prefix string
1340 * fit_image_print() lists all mandatory properies for the processed component
1341 * image. If present, hash nodes are printed out as well.
1344 * no returned results
1346 void fit_image_print (const void *fit, int image_noffset, const char *p)
1349 uint8_t type, arch, os, comp;
1357 /* Mandatory properties */
1358 ret = fit_get_desc (fit, image_noffset, &desc);
1359 printf ("%s Description: ", p);
1361 printf ("unavailable\n");
1363 printf ("%s\n", desc);
1365 fit_image_get_type (fit, image_noffset, &type);
1366 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1368 fit_image_get_comp (fit, image_noffset, &comp);
1369 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1371 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1374 printf ("%s Data Start: ", p);
1376 printf ("unavailable\n");
1378 printf ("0x%08lx\n", (ulong)data);
1381 printf ("%s Data Size: ", p);
1383 printf ("unavailable\n");
1385 genimg_print_size (size);
1387 /* Remaining, type dependent properties */
1388 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1389 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1390 (type == IH_TYPE_FLATDT)) {
1391 fit_image_get_arch (fit, image_noffset, &arch);
1392 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1395 if (type == IH_TYPE_KERNEL) {
1396 fit_image_get_os (fit, image_noffset, &os);
1397 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1400 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1401 ret = fit_image_get_load (fit, image_noffset, &load);
1402 printf ("%s Load Address: ", p);
1404 printf ("unavailable\n");
1406 printf ("0x%08lx\n", load);
1408 fit_image_get_entry (fit, image_noffset, &entry);
1409 printf ("%s Entry Point: ", p);
1411 printf ("unavailable\n");
1413 printf ("0x%08lx\n", entry);
1416 /* Process all hash subnodes of the component image node */
1417 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1418 (noffset >= 0) && (ndepth > 0);
1419 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1421 /* Direct child node of the component image node */
1422 fit_image_print_hash (fit, noffset, p);
1428 * fit_image_print_hash - prints out the hash node details
1429 * @fit: pointer to the FIT format image header
1430 * @noffset: offset of the hash node
1431 * @p: pointer to prefix string
1433 * fit_image_print_hash() lists properies for the processed hash node
1436 * no returned results
1438 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1446 * Check subnode name, must be equal to "hash".
1447 * Multiple hash nodes require unique unit node
1448 * names, e.g. hash@1, hash@2, etc.
1450 if (strncmp (fit_get_name(fit, noffset, NULL),
1452 strlen(FIT_HASH_NODENAME)) != 0)
1455 debug ("%s Hash node: '%s'\n", p,
1456 fit_get_name (fit, noffset, NULL));
1458 printf ("%s Hash algo: ", p);
1459 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1460 printf ("invalid/unsupported\n");
1463 printf ("%s\n", algo);
1465 ret = fit_image_hash_get_value (fit, noffset, &value,
1467 printf ("%s Hash value: ", p);
1469 printf ("unavailable\n");
1471 for (i = 0; i < value_len; i++)
1472 printf ("%02x", value[i]);
1476 debug ("%s Hash len: %d\n", p, value_len);
1480 * fit_get_desc - get node description property
1481 * @fit: pointer to the FIT format image header
1482 * @noffset: node offset
1483 * @desc: double pointer to the char, will hold pointer to the descrption
1485 * fit_get_desc() reads description property from a given node, if
1486 * description is found pointer to it is returened in third call argument.
1492 int fit_get_desc (const void *fit, int noffset, char **desc)
1496 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1497 if (*desc == NULL) {
1498 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1506 * fit_get_timestamp - get node timestamp property
1507 * @fit: pointer to the FIT format image header
1508 * @noffset: node offset
1509 * @timestamp: pointer to the time_t, will hold read timestamp
1511 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1512 * is found and has a correct size its value is retured in third call
1517 * -1, on property read failure
1518 * -2, on wrong timestamp size
1520 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1525 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1527 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1530 if (len != sizeof (uint32_t)) {
1531 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1535 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1540 * fit_image_get_node - get node offset for component image of a given unit name
1541 * @fit: pointer to the FIT format image header
1542 * @image_uname: component image node unit name
1544 * fit_image_get_node() finds a component image (withing the '/images'
1545 * node) of a provided unit name. If image is found its node offset is
1546 * returned to the caller.
1549 * image node offset when found (>=0)
1550 * negative number on failure (FDT_ERR_* code)
1552 int fit_image_get_node (const void *fit, const char *image_uname)
1554 int noffset, images_noffset;
1556 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1557 if (images_noffset < 0) {
1558 debug ("Can't find images parent node '%s' (%s)\n",
1559 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1560 return images_noffset;
1563 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1565 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1566 image_uname, fdt_strerror (noffset));
1573 * fit_image_get_os - get os id for a given component image node
1574 * @fit: pointer to the FIT format image header
1575 * @noffset: component image node offset
1576 * @os: pointer to the uint8_t, will hold os numeric id
1578 * fit_image_get_os() finds os property in a given component image node.
1579 * If the property is found, its (string) value is translated to the numeric
1580 * id which is returned to the caller.
1586 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1591 /* Get OS name from property data */
1592 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1594 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1599 /* Translate OS name to id */
1600 *os = genimg_get_os_id (data);
1605 * fit_image_get_arch - get arch id for a given component image node
1606 * @fit: pointer to the FIT format image header
1607 * @noffset: component image node offset
1608 * @arch: pointer to the uint8_t, will hold arch numeric id
1610 * fit_image_get_arch() finds arch property in a given component image node.
1611 * If the property is found, its (string) value is translated to the numeric
1612 * id which is returned to the caller.
1618 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1623 /* Get architecture name from property data */
1624 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1626 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1631 /* Translate architecture name to id */
1632 *arch = genimg_get_arch_id (data);
1637 * fit_image_get_type - get type id for a given component image node
1638 * @fit: pointer to the FIT format image header
1639 * @noffset: component image node offset
1640 * @type: pointer to the uint8_t, will hold type numeric id
1642 * fit_image_get_type() finds type property in a given component image node.
1643 * If the property is found, its (string) value is translated to the numeric
1644 * id which is returned to the caller.
1650 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1655 /* Get image type name from property data */
1656 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1658 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1663 /* Translate image type name to id */
1664 *type = genimg_get_type_id (data);
1669 * fit_image_get_comp - get comp id for a given component image node
1670 * @fit: pointer to the FIT format image header
1671 * @noffset: component image node offset
1672 * @comp: pointer to the uint8_t, will hold comp numeric id
1674 * fit_image_get_comp() finds comp property in a given component image node.
1675 * If the property is found, its (string) value is translated to the numeric
1676 * id which is returned to the caller.
1682 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1687 /* Get compression name from property data */
1688 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1690 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1695 /* Translate compression name to id */
1696 *comp = genimg_get_comp_id (data);
1701 * fit_image_get_load - get load address property for a given component image node
1702 * @fit: pointer to the FIT format image header
1703 * @noffset: component image node offset
1704 * @load: pointer to the uint32_t, will hold load address
1706 * fit_image_get_load() finds load address property in a given component image node.
1707 * If the property is found, its value is returned to the caller.
1713 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1716 const uint32_t *data;
1718 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1720 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1724 *load = uimage_to_cpu (*data);
1729 * fit_image_get_entry - get entry point address property for a given component image node
1730 * @fit: pointer to the FIT format image header
1731 * @noffset: component image node offset
1732 * @entry: pointer to the uint32_t, will hold entry point address
1734 * fit_image_get_entry() finds entry point address property in a given component image node.
1735 * If the property is found, its value is returned to the caller.
1741 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1744 const uint32_t *data;
1746 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1748 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1752 *entry = uimage_to_cpu (*data);
1757 * fit_image_get_data - get data property and its size for a given component image node
1758 * @fit: pointer to the FIT format image header
1759 * @noffset: component image node offset
1760 * @data: double pointer to void, will hold data property's data address
1761 * @size: pointer to size_t, will hold data property's data size
1763 * fit_image_get_data() finds data property in a given component image node.
1764 * If the property is found its data start address and size are returned to
1771 int fit_image_get_data (const void *fit, int noffset,
1772 const void **data, size_t *size)
1776 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1777 if (*data == NULL) {
1778 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1788 * fit_image_hash_get_algo - get hash algorithm name
1789 * @fit: pointer to the FIT format image header
1790 * @noffset: hash node offset
1791 * @algo: double pointer to char, will hold pointer to the algorithm name
1793 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1794 * If the property is found its data start address is returned to the caller.
1800 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1804 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1805 if (*algo == NULL) {
1806 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1814 * fit_image_hash_get_value - get hash value and length
1815 * @fit: pointer to the FIT format image header
1816 * @noffset: hash node offset
1817 * @value: double pointer to uint8_t, will hold address of a hash value data
1818 * @value_len: pointer to an int, will hold hash data length
1820 * fit_image_hash_get_value() finds hash value property in a given hash node.
1821 * If the property is found its data start address and size are returned to
1828 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1833 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1834 if (*value == NULL) {
1835 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1845 * fit_set_timestamp - set node timestamp property
1846 * @fit: pointer to the FIT format image header
1847 * @noffset: node offset
1848 * @timestamp: timestamp value to be set
1850 * fit_set_timestamp() attempts to set timestamp property in the requested
1851 * node and returns operation status to the caller.
1855 * -1, on property read failure
1857 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1862 t = cpu_to_uimage (timestamp);
1863 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1866 printf ("Can't set '%s' property for '%s' node (%s)\n",
1867 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1868 fdt_strerror (ret));
1876 * calculate_hash - calculate and return hash for provided input data
1877 * @data: pointer to the input data
1878 * @data_len: data length
1879 * @algo: requested hash algorithm
1880 * @value: pointer to the char, will hold hash value data (caller must
1881 * allocate enough free space)
1882 * value_len: length of the calculated hash
1884 * calculate_hash() computes input data hash according to the requested algorithm.
1885 * Resulting hash value is placed in caller provided 'value' buffer, length
1886 * of the calculated hash is returned via value_len pointer argument.
1890 * -1, when algo is unsupported
1892 static int calculate_hash (const void *data, int data_len, const char *algo,
1893 uint8_t *value, int *value_len)
1895 if (strcmp (algo, "crc32") == 0 ) {
1896 *((uint32_t *)value) = crc32_wd (0, data, data_len,
1898 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1900 } else if (strcmp (algo, "sha1") == 0 ) {
1901 sha1_csum_wd ((unsigned char *) data, data_len,
1902 (unsigned char *) value, CHUNKSZ_SHA1);
1904 } else if (strcmp (algo, "md5") == 0 ) {
1905 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1908 debug ("Unsupported hash alogrithm\n");
1916 * fit_set_hashes - process FIT component image nodes and calculate hashes
1917 * @fit: pointer to the FIT format image header
1919 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1920 * Hashes are calculated for all component images which have hash subnodes
1921 * with algorithm property set to one of the supported hash algorithms.
1925 * libfdt error code, on failure
1927 int fit_set_hashes (void *fit)
1934 /* Find images parent node offset */
1935 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1936 if (images_noffset < 0) {
1937 printf ("Can't find images parent node '%s' (%s)\n",
1938 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1939 return images_noffset;
1942 /* Process its subnodes, print out component images details */
1943 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1944 (noffset >= 0) && (ndepth > 0);
1945 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1948 * Direct child node of the images parent node,
1949 * i.e. component image node.
1951 ret = fit_image_set_hashes (fit, noffset);
1961 * fit_image_set_hashes - calculate/set hashes for given component image node
1962 * @fit: pointer to the FIT format image header
1963 * @image_noffset: requested component image node
1965 * fit_image_set_hashes() adds hash values for an component image node. All
1966 * existing hash subnodes are checked, if algorithm property is set to one of
1967 * the supported hash algorithms, hash value is computed and corresponding
1968 * hash node property is set, for example:
1970 * Input component image node structure:
1972 * o image@1 (at image_noffset)
1973 * | - data = [binary data]
1977 * Output component image node structure:
1979 * o image@1 (at image_noffset)
1980 * | - data = [binary data]
1983 * |- value = sha1(data)
1989 int fit_image_set_hashes (void *fit, int image_noffset)
1994 uint8_t value[FIT_MAX_HASH_LEN];
1999 /* Get image data and data length */
2000 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2001 printf ("Can't get image data/size\n");
2005 /* Process all hash subnodes of the component image node */
2006 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2007 (noffset >= 0) && (ndepth > 0);
2008 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2010 /* Direct child node of the component image node */
2013 * Check subnode name, must be equal to "hash".
2014 * Multiple hash nodes require unique unit node
2015 * names, e.g. hash@1, hash@2, etc.
2017 if (strncmp (fit_get_name(fit, noffset, NULL),
2019 strlen(FIT_HASH_NODENAME)) != 0) {
2020 /* Not a hash subnode, skip it */
2024 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2025 printf ("Can't get hash algo property for "
2026 "'%s' hash node in '%s' image node\n",
2027 fit_get_name (fit, noffset, NULL),
2028 fit_get_name (fit, image_noffset, NULL));
2032 if (calculate_hash (data, size, algo, value, &value_len)) {
2033 printf ("Unsupported hash algorithm (%s) for "
2034 "'%s' hash node in '%s' image node\n",
2035 algo, fit_get_name (fit, noffset, NULL),
2036 fit_get_name (fit, image_noffset, NULL));
2040 if (fit_image_hash_set_value (fit, noffset, value,
2042 printf ("Can't set hash value for "
2043 "'%s' hash node in '%s' image node\n",
2044 fit_get_name (fit, noffset, NULL),
2045 fit_get_name (fit, image_noffset, NULL));
2055 * fit_image_hash_set_value - set hash value in requested has node
2056 * @fit: pointer to the FIT format image header
2057 * @noffset: hash node offset
2058 * @value: hash value to be set
2059 * @value_len: hash value length
2061 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2062 * given and returns operation status to the caller.
2068 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2073 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2075 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2076 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2077 fdt_strerror (ret));
2083 #endif /* USE_HOSTCC */
2086 * fit_image_check_hashes - verify data intergity
2087 * @fit: pointer to the FIT format image header
2088 * @image_noffset: component image node offset
2090 * fit_image_check_hashes() goes over component image hash nodes,
2091 * re-calculates each data hash and compares with the value stored in hash
2095 * 1, if all hashes are valid
2096 * 0, otherwise (or on error)
2098 int fit_image_check_hashes (const void *fit, int image_noffset)
2105 uint8_t value[FIT_MAX_HASH_LEN];
2111 /* Get image data and data length */
2112 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2113 printf ("Can't get image data/size\n");
2117 /* Process all hash subnodes of the component image node */
2118 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2119 (noffset >= 0) && (ndepth > 0);
2120 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2122 /* Direct child node of the component image node */
2125 * Check subnode name, must be equal to "hash".
2126 * Multiple hash nodes require unique unit node
2127 * names, e.g. hash@1, hash@2, etc.
2129 if (strncmp (fit_get_name(fit, noffset, NULL),
2131 strlen(FIT_HASH_NODENAME)) != 0)
2134 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2135 err_msg = "Can't get hash algo property";
2138 printf ("%s", algo);
2140 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2142 err_msg = "Can't get hash value property";
2146 if (calculate_hash (data, size, algo, value, &value_len)) {
2147 err_msg = "Unsupported hash algorithm";
2151 if (value_len != fit_value_len) {
2152 err_msg = "Bad hash value len";
2154 } else if (memcmp (value, fit_value, value_len) != 0) {
2155 err_msg = "Bad hash value";
2165 printf ("%s for '%s' hash node in '%s' image node\n",
2166 err_msg, fit_get_name (fit, noffset, NULL),
2167 fit_get_name (fit, image_noffset, NULL));
2172 * fit_image_check_os - check whether image node is of a given os type
2173 * @fit: pointer to the FIT format image header
2174 * @noffset: component image node offset
2175 * @os: requested image os
2177 * fit_image_check_os() reads image os property and compares its numeric
2178 * id with the requested os. Comparison result is returned to the caller.
2181 * 1 if image is of given os type
2182 * 0 otherwise (or on error)
2184 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2188 if (fit_image_get_os (fit, noffset, &image_os))
2190 return (os == image_os);
2194 * fit_image_check_arch - check whether image node is of a given arch
2195 * @fit: pointer to the FIT format image header
2196 * @noffset: component image node offset
2197 * @arch: requested imagearch
2199 * fit_image_check_arch() reads image arch property and compares its numeric
2200 * id with the requested arch. Comparison result is returned to the caller.
2203 * 1 if image is of given arch
2204 * 0 otherwise (or on error)
2206 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2210 if (fit_image_get_arch (fit, noffset, &image_arch))
2212 return (arch == image_arch);
2216 * fit_image_check_type - check whether image node is of a given type
2217 * @fit: pointer to the FIT format image header
2218 * @noffset: component image node offset
2219 * @type: requested image type
2221 * fit_image_check_type() reads image type property and compares its numeric
2222 * id with the requested type. Comparison result is returned to the caller.
2225 * 1 if image is of given type
2226 * 0 otherwise (or on error)
2228 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2232 if (fit_image_get_type (fit, noffset, &image_type))
2234 return (type == image_type);
2238 * fit_image_check_comp - check whether image node uses given compression
2239 * @fit: pointer to the FIT format image header
2240 * @noffset: component image node offset
2241 * @comp: requested image compression type
2243 * fit_image_check_comp() reads image compression property and compares its
2244 * numeric id with the requested compression type. Comparison result is
2245 * returned to the caller.
2248 * 1 if image uses requested compression
2249 * 0 otherwise (or on error)
2251 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2255 if (fit_image_get_comp (fit, noffset, &image_comp))
2257 return (comp == image_comp);
2261 * fit_check_format - sanity check FIT image format
2262 * @fit: pointer to the FIT format image header
2264 * fit_check_format() runs a basic sanity FIT image verification.
2265 * Routine checks for mandatory properties, nodes, etc.
2271 int fit_check_format (const void *fit)
2273 /* mandatory / node 'description' property */
2274 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2275 debug ("Wrong FIT format: no description\n");
2279 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2280 /* mandatory / node 'timestamp' property */
2281 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2282 debug ("Wrong FIT format: no description\n");
2287 /* mandatory subimages parent '/images' node */
2288 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2289 debug ("Wrong FIT format: no images parent node\n");
2297 * fit_conf_get_node - get node offset for configuration of a given unit name
2298 * @fit: pointer to the FIT format image header
2299 * @conf_uname: configuration node unit name
2301 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2302 * parant node) of a provided unit name. If configuration is found its node offset
2303 * is returned to the caller.
2305 * When NULL is provided in second argument fit_conf_get_node() will search
2306 * for a default configuration node instead. Default configuration node unit name
2307 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2310 * configuration node offset when found (>=0)
2311 * negative number on failure (FDT_ERR_* code)
2313 int fit_conf_get_node (const void *fit, const char *conf_uname)
2315 int noffset, confs_noffset;
2318 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2319 if (confs_noffset < 0) {
2320 debug ("Can't find configurations parent node '%s' (%s)\n",
2321 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2322 return confs_noffset;
2325 if (conf_uname == NULL) {
2326 /* get configuration unit name from the default property */
2327 debug ("No configuration specified, trying default...\n");
2328 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2329 if (conf_uname == NULL) {
2330 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2333 debug ("Found default configuration: '%s'\n", conf_uname);
2336 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2338 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2339 conf_uname, fdt_strerror (noffset));
2345 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2346 const char *prop_name)
2351 /* get kernel image unit name from configuration kernel property */
2352 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2356 return fit_image_get_node (fit, uname);
2360 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2361 * a given configuration
2362 * @fit: pointer to the FIT format image header
2363 * @noffset: configuration node offset
2365 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2366 * configuration FIT_KERNEL_PROP property and translates it to the node
2370 * image node offset when found (>=0)
2371 * negative number on failure (FDT_ERR_* code)
2373 int fit_conf_get_kernel_node (const void *fit, int noffset)
2375 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2379 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2380 * a given configuration
2381 * @fit: pointer to the FIT format image header
2382 * @noffset: configuration node offset
2384 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2385 * configuration FIT_KERNEL_PROP property and translates it to the node
2389 * image node offset when found (>=0)
2390 * negative number on failure (FDT_ERR_* code)
2392 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2394 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2398 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2399 * a given configuration
2400 * @fit: pointer to the FIT format image header
2401 * @noffset: configuration node offset
2403 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2404 * configuration FIT_KERNEL_PROP property and translates it to the node
2408 * image node offset when found (>=0)
2409 * negative number on failure (FDT_ERR_* code)
2411 int fit_conf_get_fdt_node (const void *fit, int noffset)
2413 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2417 * fit_conf_print - prints out the FIT configuration details
2418 * @fit: pointer to the FIT format image header
2419 * @noffset: offset of the configuration node
2420 * @p: pointer to prefix string
2422 * fit_conf_print() lists all mandatory properies for the processed
2423 * configuration node.
2426 * no returned results
2428 void fit_conf_print (const void *fit, int noffset, const char *p)
2434 /* Mandatory properties */
2435 ret = fit_get_desc (fit, noffset, &desc);
2436 printf ("%s Description: ", p);
2438 printf ("unavailable\n");
2440 printf ("%s\n", desc);
2442 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2443 printf ("%s Kernel: ", p);
2445 printf ("unavailable\n");
2447 printf ("%s\n", uname);
2449 /* Optional properties */
2450 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2452 printf ("%s Init Ramdisk: %s\n", p, uname);
2454 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2456 printf ("%s FDT: %s\n", p, uname);
2460 * fit_check_ramdisk - verify FIT format ramdisk subimage
2461 * @fit_hdr: pointer to the FIT ramdisk header
2462 * @rd_noffset: ramdisk subimage node offset within FIT image
2463 * @arch: requested ramdisk image architecture type
2464 * @verify: data CRC verification flag
2466 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2467 * specified FIT image.
2474 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2476 fit_image_print (fit, rd_noffset, " ");
2479 puts (" Verifying Hash Integrity ... ");
2480 if (!fit_image_check_hashes (fit, rd_noffset)) {
2481 puts ("Bad Data Hash\n");
2482 show_boot_progress (-125);
2488 show_boot_progress (126);
2489 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2490 !fit_image_check_arch (fit, rd_noffset, arch) ||
2491 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2492 printf ("No Linux %s Ramdisk Image\n",
2493 genimg_get_arch_name(arch));
2494 show_boot_progress (-126);
2498 show_boot_progress (127);
2501 #endif /* USE_HOSTCC */
2502 #endif /* CONFIG_FIT */