2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #ifdef CONFIG_SHOW_BOOT_PROGRESS
33 #include <status_led.h>
36 #ifdef CONFIG_HAS_DATAFLASH
37 #include <dataflash.h>
40 #ifdef CONFIG_LOGBUFFER
44 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
50 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
53 #include <fdt_support.h>
56 #if defined(CONFIG_FIT)
59 static int fit_check_ramdisk (const void *fit, int os_noffset,
60 uint8_t arch, int verify);
64 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
67 DECLARE_GLOBAL_DATA_PTR;
69 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
75 #endif /* !USE_HOSTCC*/
77 typedef struct table_entry {
78 int id; /* as defined in image.h */
79 char *sname; /* short (input) name */
80 char *lname; /* long (output) name */
83 static table_entry_t uimage_arch[] = {
84 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
85 { IH_ARCH_ALPHA, "alpha", "Alpha", },
86 { IH_ARCH_ARM, "arm", "ARM", },
87 { IH_ARCH_I386, "x86", "Intel x86", },
88 { IH_ARCH_IA64, "ia64", "IA64", },
89 { IH_ARCH_M68K, "m68k", "M68K", },
90 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
91 { IH_ARCH_MIPS, "mips", "MIPS", },
92 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
93 { IH_ARCH_NIOS, "nios", "NIOS", },
94 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
95 { IH_ARCH_PPC, "ppc", "PowerPC", },
96 { IH_ARCH_S390, "s390", "IBM S390", },
97 { IH_ARCH_SH, "sh", "SuperH", },
98 { IH_ARCH_SPARC, "sparc", "SPARC", },
99 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
100 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
101 { IH_ARCH_AVR32, "avr32", "AVR32", },
105 static table_entry_t uimage_os[] = {
106 { IH_OS_INVALID, NULL, "Invalid OS", },
107 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
108 { IH_OS_ARTOS, "artos", "ARTOS", },
110 { IH_OS_LINUX, "linux", "Linux", },
111 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
112 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
114 { IH_OS_NETBSD, "netbsd", "NetBSD", },
115 { IH_OS_RTEMS, "rtems", "RTEMS", },
116 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
117 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
118 { IH_OS_QNX, "qnx", "QNX", },
119 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
122 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
123 { IH_OS_DELL, "dell", "Dell", },
124 { IH_OS_ESIX, "esix", "Esix", },
125 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
126 { IH_OS_IRIX, "irix", "Irix", },
127 { IH_OS_NCR, "ncr", "NCR", },
128 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
129 { IH_OS_PSOS, "psos", "pSOS", },
130 { IH_OS_SCO, "sco", "SCO", },
131 { IH_OS_SOLARIS, "solaris", "Solaris", },
132 { IH_OS_SVR4, "svr4", "SVR4", },
137 static table_entry_t uimage_type[] = {
138 { IH_TYPE_INVALID, NULL, "Invalid Image", },
139 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
140 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
141 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
142 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
143 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
144 { IH_TYPE_SCRIPT, "script", "Script", },
145 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
146 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
150 static table_entry_t uimage_comp[] = {
151 { IH_COMP_NONE, "none", "uncompressed", },
152 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
153 { IH_COMP_GZIP, "gzip", "gzip compressed", },
157 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
158 static void genimg_print_size (uint32_t size);
159 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
160 static void genimg_print_time (time_t timestamp);
163 /*****************************************************************************/
164 /* Legacy format routines */
165 /*****************************************************************************/
166 int image_check_hcrc (image_header_t *hdr)
169 ulong len = image_get_header_size ();
170 image_header_t header;
172 /* Copy header so we can blank CRC field for re-calculation */
173 memmove (&header, (char *)hdr, image_get_header_size ());
174 image_set_hcrc (&header, 0);
176 hcrc = crc32 (0, (unsigned char *)&header, len);
178 return (hcrc == image_get_hcrc (hdr));
181 int image_check_dcrc (image_header_t *hdr)
183 ulong data = image_get_data (hdr);
184 ulong len = image_get_data_size (hdr);
185 ulong dcrc = crc32 (0, (unsigned char *)data, len);
187 return (dcrc == image_get_dcrc (hdr));
191 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
194 ulong len = image_get_data_size (hdr);
195 ulong data = image_get_data (hdr);
197 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
199 ulong edata = cdata + len;
201 while (cdata < edata) {
202 ulong chunk = edata - cdata;
206 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
212 dcrc = crc32 (0, (unsigned char *)data, len);
215 return (dcrc == image_get_dcrc (hdr));
217 #endif /* !USE_HOSTCC */
220 * image_multi_count - get component (sub-image) count
221 * @hdr: pointer to the header of the multi component image
223 * image_multi_count() returns number of components in a multi
226 * Note: no checking of the image type is done, caller must pass
227 * a valid multi component image.
230 * number of components
232 ulong image_multi_count (image_header_t *hdr)
237 /* get start of the image payload, which in case of multi
238 * component images that points to a table of component sizes */
239 size = (uint32_t *)image_get_data (hdr);
241 /* count non empty slots */
242 for (i = 0; size[i]; ++i)
249 * image_multi_getimg - get component data address and size
250 * @hdr: pointer to the header of the multi component image
251 * @idx: index of the requested component
252 * @data: pointer to a ulong variable, will hold component data address
253 * @len: pointer to a ulong variable, will hold component size
255 * image_multi_getimg() returns size and data address for the requested
256 * component in a multi component image.
258 * Note: no checking of the image type is done, caller must pass
259 * a valid multi component image.
262 * data address and size of the component, if idx is valid
263 * 0 in data and len, if idx is out of range
265 void image_multi_getimg (image_header_t *hdr, ulong idx,
266 ulong *data, ulong *len)
270 ulong offset, tail, count, img_data;
272 /* get number of component */
273 count = image_multi_count (hdr);
275 /* get start of the image payload, which in case of multi
276 * component images that points to a table of component sizes */
277 size = (uint32_t *)image_get_data (hdr);
279 /* get address of the proper component data start, which means
280 * skipping sizes table (add 1 for last, null entry) */
281 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
284 *len = uimage_to_cpu (size[idx]);
288 /* go over all indices preceding requested component idx */
289 for (i = 0; i < idx; i++) {
290 /* add up i-th component size */
291 offset += uimage_to_cpu (size[i]);
293 /* add up alignment for i-th component */
294 tail += (4 - uimage_to_cpu (size[i]) % 4);
297 /* calculate idx-th component data address */
298 *data = img_data + offset + tail;
305 static void image_print_type (image_header_t *hdr)
307 const char *os, *arch, *type, *comp;
309 os = genimg_get_os_name (image_get_os (hdr));
310 arch = genimg_get_arch_name (image_get_arch (hdr));
311 type = genimg_get_type_name (image_get_type (hdr));
312 comp = genimg_get_comp_name (image_get_comp (hdr));
314 printf ("%s %s %s (%s)\n", arch, os, type, comp);
318 * __image_print_contents - prints out the contents of the legacy format image
319 * @hdr: pointer to the legacy format image header
320 * @p: pointer to prefix string
322 * __image_print_contents() formats a multi line legacy image contents description.
323 * The routine prints out all header fields followed by the size/offset data
324 * for MULTI/SCRIPT images.
327 * no returned results
329 static void __image_print_contents (image_header_t *hdr, const char *p)
331 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
332 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
333 printf ("%sCreated: ", p);
334 genimg_print_time ((time_t)image_get_time (hdr));
336 printf ("%sImage Type: ", p);
337 image_print_type (hdr);
338 printf ("%sData Size: ", p);
339 genimg_print_size (image_get_data_size (hdr));
340 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
341 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
343 if (image_check_type (hdr, IH_TYPE_MULTI) ||
344 image_check_type (hdr, IH_TYPE_SCRIPT)) {
347 ulong count = image_multi_count (hdr);
349 printf ("%sContents:\n", p);
350 for (i = 0; i < count; i++) {
351 image_multi_getimg (hdr, i, &data, &len);
353 printf ("%s Image %d: ", p, i);
354 genimg_print_size (len);
356 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
358 * the user may need to know offsets
359 * if planning to do something with
362 printf ("%s Offset = 0x%08lx\n", p, data);
368 inline void image_print_contents (image_header_t *hdr)
370 __image_print_contents (hdr, " ");
373 inline void image_print_contents_noindent (image_header_t *hdr)
375 __image_print_contents (hdr, "");
380 * image_get_ramdisk - get and verify ramdisk image
381 * @rd_addr: ramdisk image start address
382 * @arch: expected ramdisk architecture
383 * @verify: checksum verification flag
385 * image_get_ramdisk() returns a pointer to the verified ramdisk image
386 * header. Routine receives image start address and expected architecture
387 * flag. Verification done covers data and header integrity and os/type/arch
390 * If dataflash support is enabled routine checks for dataflash addresses
391 * and handles required dataflash reads.
394 * pointer to a ramdisk image header, if image was found and valid
395 * otherwise, return NULL
397 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
400 image_header_t *rd_hdr;
402 show_boot_progress (9);
403 rd_hdr = (image_header_t *)rd_addr;
405 if (!image_check_magic (rd_hdr)) {
406 puts ("Bad Magic Number\n");
407 show_boot_progress (-10);
411 if (!image_check_hcrc (rd_hdr)) {
412 puts ("Bad Header Checksum\n");
413 show_boot_progress (-11);
417 show_boot_progress (10);
418 image_print_contents (rd_hdr);
421 puts(" Verifying Checksum ... ");
422 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
423 puts ("Bad Data CRC\n");
424 show_boot_progress (-12);
430 show_boot_progress (11);
432 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
433 !image_check_arch (rd_hdr, arch) ||
434 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
435 printf ("No Linux %s Ramdisk Image\n",
436 genimg_get_arch_name(arch));
437 show_boot_progress (-13);
443 #endif /* !USE_HOSTCC */
445 /*****************************************************************************/
446 /* Shared dual-format routines */
447 /*****************************************************************************/
449 int getenv_verify (void)
451 char *s = getenv ("verify");
452 return (s && (*s == 'n')) ? 0 : 1;
455 int getenv_autostart (void)
457 char *s = getenv ("autostart");
458 return (s && (*s == 'n')) ? 0 : 1;
461 ulong getenv_bootm_low(void)
463 char *s = getenv ("bootm_low");
465 ulong tmp = simple_strtoul (s, NULL, 16);
469 #ifdef CFG_SDRAM_BASE
470 return CFG_SDRAM_BASE;
476 ulong getenv_bootm_size(void)
478 char *s = getenv ("bootm_size");
480 ulong tmp = simple_strtoul (s, NULL, 16);
484 return gd->bd->bi_memsize;
487 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
489 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
491 size_t tail = (len > chunksz) ? chunksz : len;
493 memmove (to, from, tail);
498 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
499 memmove (to, from, len);
500 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
502 #endif /* !USE_HOSTCC */
504 static void genimg_print_size (uint32_t size)
507 printf ("%d Bytes = ", size);
508 print_size (size, "\n");
510 printf ("%d Bytes = %.2f kB = %.2f MB\n",
511 size, (double)size / 1.024e3,
512 (double)size / 1.048576e6);
516 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
517 static void genimg_print_time (time_t timestamp)
522 to_tm (timestamp, &tm);
523 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
524 tm.tm_year, tm.tm_mon, tm.tm_mday,
525 tm.tm_hour, tm.tm_min, tm.tm_sec);
527 printf ("%s", ctime(×tamp));
530 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
533 * get_table_entry_name - translate entry id to long name
534 * @table: pointer to a translation table for entries of a specific type
535 * @msg: message to be returned when translation fails
536 * @id: entry id to be translated
538 * get_table_entry_name() will go over translation table trying to find
539 * entry that matches given id. If matching entry is found, its long
540 * name is returned to the caller.
543 * long entry name if translation succeeds
546 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
548 for (; table->id >= 0; ++table) {
550 return (table->lname);
555 const char *genimg_get_os_name (uint8_t os)
557 return (get_table_entry_name (uimage_os, "Unknown OS", os));
560 const char *genimg_get_arch_name (uint8_t arch)
562 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
565 const char *genimg_get_type_name (uint8_t type)
567 return (get_table_entry_name (uimage_type, "Unknown Image", type));
570 const char *genimg_get_comp_name (uint8_t comp)
572 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
576 * get_table_entry_id - translate short entry name to id
577 * @table: pointer to a translation table for entries of a specific type
578 * @table_name: to be used in case of error
579 * @name: entry short name to be translated
581 * get_table_entry_id() will go over translation table trying to find
582 * entry that matches given short name. If matching entry is found,
583 * its id returned to the caller.
586 * entry id if translation succeeds
589 static int get_table_entry_id (table_entry_t *table,
590 const char *table_name, const char *name)
596 for (t = table; t->id >= 0; ++t) {
597 if (t->sname && strcasecmp(t->sname, name) == 0)
601 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
602 for (t = table; t->id >= 0; ++t) {
603 if (t->sname == NULL)
605 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
608 fprintf (stderr, "\n");
610 for (t = table; t->id >= 0; ++t) {
611 if (t->sname && strcmp(t->sname, name) == 0)
614 debug ("Invalid %s Type: %s\n", table_name, name);
615 #endif /* USE_HOSTCC */
619 int genimg_get_os_id (const char *name)
621 return (get_table_entry_id (uimage_os, "OS", name));
624 int genimg_get_arch_id (const char *name)
626 return (get_table_entry_id (uimage_arch, "CPU", name));
629 int genimg_get_type_id (const char *name)
631 return (get_table_entry_id (uimage_type, "Image", name));
634 int genimg_get_comp_id (const char *name)
636 return (get_table_entry_id (uimage_comp, "Compression", name));
641 * genimg_get_format - get image format type
642 * @img_addr: image start address
644 * genimg_get_format() checks whether provided address points to a valid
645 * legacy or FIT image.
647 * New uImage format and FDT blob are based on a libfdt. FDT blob
648 * may be passed directly or embedded in a FIT image. In both situations
649 * genimg_get_format() must be able to dectect libfdt header.
652 * image format type or IMAGE_FORMAT_INVALID if no image is present
654 int genimg_get_format (void *img_addr)
656 ulong format = IMAGE_FORMAT_INVALID;
658 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
662 hdr = (image_header_t *)img_addr;
663 if (image_check_magic(hdr))
664 format = IMAGE_FORMAT_LEGACY;
665 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
667 fit_hdr = (char *)img_addr;
668 if (fdt_check_header (fit_hdr) == 0)
669 format = IMAGE_FORMAT_FIT;
677 * genimg_get_image - get image from special storage (if necessary)
678 * @img_addr: image start address
680 * genimg_get_image() checks if provided image start adddress is located
681 * in a dataflash storage. If so, image is moved to a system RAM memory.
684 * image start address after possible relocation from special storage
686 ulong genimg_get_image (ulong img_addr)
688 ulong ram_addr = img_addr;
690 #ifdef CONFIG_HAS_DATAFLASH
691 ulong h_size, d_size;
693 if (addr_dataflash (img_addr)){
694 /* ger RAM address */
695 ram_addr = CFG_LOAD_ADDR;
697 /* get header size */
698 h_size = image_get_header_size ();
699 #if defined(CONFIG_FIT)
700 if (sizeof(struct fdt_header) > h_size)
701 h_size = sizeof(struct fdt_header);
705 debug (" Reading image header from dataflash address "
706 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
708 read_dataflash (img_addr, h_size, (char *)ram_addr);
711 switch (genimg_get_format ((void *)ram_addr)) {
712 case IMAGE_FORMAT_LEGACY:
713 d_size = image_get_data_size ((image_header_t *)ram_addr);
714 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
717 #if defined(CONFIG_FIT)
718 case IMAGE_FORMAT_FIT:
719 d_size = fit_get_size ((const void *)ram_addr) - h_size;
720 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
725 printf (" No valid image found at 0x%08lx\n", img_addr);
729 /* read in image data */
730 debug (" Reading image remaining data from dataflash address "
731 "%08lx to RAM address %08lx\n", img_addr + h_size,
734 read_dataflash (img_addr + h_size, d_size,
735 (char *)(ram_addr + h_size));
738 #endif /* CONFIG_HAS_DATAFLASH */
744 * boot_get_ramdisk - main ramdisk handling routine
745 * @argc: command argument count
746 * @argv: command argument list
747 * @images: pointer to the bootm images structure
748 * @arch: expected ramdisk architecture
749 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
750 * @rd_end: pointer to a ulong variable, will hold ramdisk end
752 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
753 * Curently supported are the following ramdisk sources:
754 * - multicomponent kernel/ramdisk image,
755 * - commandline provided address of decicated ramdisk image.
758 * 0, if ramdisk image was found and valid, or skiped
759 * rd_start and rd_end are set to ramdisk start/end addresses if
760 * ramdisk image is found and valid
762 * 1, if ramdisk image is found but corrupted
763 * rd_start and rd_end are set to 0 if no ramdisk exists
765 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
766 uint8_t arch, ulong *rd_start, ulong *rd_end)
768 ulong rd_addr, rd_load;
769 ulong rd_data, rd_len;
770 image_header_t *rd_hdr;
771 #if defined(CONFIG_FIT)
773 const char *fit_uname_config = NULL;
774 const char *fit_uname_ramdisk = NULL;
786 * Look for a '-' which indicates to ignore the
789 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
790 debug ("## Skipping init Ramdisk\n");
791 rd_len = rd_data = 0;
792 } else if (argc >= 3) {
793 #if defined(CONFIG_FIT)
795 * If the init ramdisk comes from the FIT image and the FIT image
796 * address is omitted in the command line argument, try to use
797 * os FIT image address or default load address.
799 if (images->fit_uname_os)
800 default_addr = (ulong)images->fit_hdr_os;
802 default_addr = load_addr;
804 if (fit_parse_conf (argv[2], default_addr,
805 &rd_addr, &fit_uname_config)) {
806 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
807 fit_uname_config, rd_addr);
808 } else if (fit_parse_subimage (argv[2], default_addr,
809 &rd_addr, &fit_uname_ramdisk)) {
810 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
811 fit_uname_ramdisk, rd_addr);
815 rd_addr = simple_strtoul(argv[2], NULL, 16);
816 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
820 /* copy from dataflash if needed */
821 rd_addr = genimg_get_image (rd_addr);
824 * Check if there is an initrd image at the
825 * address provided in the second bootm argument
826 * check image type, for FIT images get FIT node.
828 switch (genimg_get_format ((void *)rd_addr)) {
829 case IMAGE_FORMAT_LEGACY:
830 printf ("## Loading init Ramdisk from Legacy "
831 "Image at %08lx ...\n", rd_addr);
833 rd_hdr = image_get_ramdisk (rd_addr, arch,
839 rd_data = image_get_data (rd_hdr);
840 rd_len = image_get_data_size (rd_hdr);
841 rd_load = image_get_load (rd_hdr);
843 #if defined(CONFIG_FIT)
844 case IMAGE_FORMAT_FIT:
845 fit_hdr = (void *)rd_addr;
846 printf ("## Loading init Ramdisk from FIT "
847 "Image at %08lx ...\n", rd_addr);
849 if (!fit_check_format (fit_hdr)) {
850 puts ("Bad FIT ramdisk image format!\n");
854 if (!fit_uname_ramdisk) {
856 * no ramdisk image node unit name, try to get config
857 * node first. If config unit node name is NULL
858 * fit_conf_get_node() will try to find default config node
860 conf_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
861 if (conf_noffset < 0)
864 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, conf_noffset);
865 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
867 /* get ramdisk component image node offset */
868 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
873 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
875 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
878 /* get ramdisk image data address and length */
879 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
880 puts ("Could not find ramdisk subimage data!\n");
884 rd_data = (ulong)data;
887 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
888 puts ("Can't get ramdisk subimage load address!\n");
892 images->fit_hdr_rd = fit_hdr;
893 images->fit_uname_rd = fit_uname_ramdisk;
894 images->fit_noffset_rd = rd_noffset;
898 puts ("Wrong Ramdisk Image Format\n");
899 rd_data = rd_len = rd_load = 0;
902 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
904 * We need to copy the ramdisk to SRAM to let Linux boot
907 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
910 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
912 } else if (images->legacy_hdr_valid &&
913 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
915 * Now check if we have a legacy mult-component image,
916 * get second entry data start address and len.
918 show_boot_progress (13);
919 printf ("## Loading init Ramdisk from multi component "
920 "Legacy Image at %08lx ...\n",
921 (ulong)images->legacy_hdr_os);
923 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
928 show_boot_progress (14);
929 rd_len = rd_data = 0;
933 debug ("## No init Ramdisk\n");
936 *rd_end = rd_data + rd_len;
938 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
944 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
946 * boot_ramdisk_high - relocate init ramdisk
947 * @lmb: pointer to lmb handle, will be used for memory mgmt
948 * @rd_data: ramdisk data start address
949 * @rd_len: ramdisk data length
950 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
951 * start address (after possible relocation)
952 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
953 * end address (after possible relocation)
955 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
956 * variable and if requested ramdisk data is moved to a specified location.
958 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
959 * start/end addresses if ramdisk image start and len were provided,
960 * otherwise set initrd_start and initrd_end set to zeros.
966 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
967 ulong *initrd_start, ulong *initrd_end)
971 int initrd_copy_to_ram = 1;
973 if ((s = getenv ("initrd_high")) != NULL) {
974 /* a value of "no" or a similar string will act like 0,
975 * turning the "load high" feature off. This is intentional.
977 initrd_high = simple_strtoul (s, NULL, 16);
978 if (initrd_high == ~0)
979 initrd_copy_to_ram = 0;
981 /* not set, no restrictions to load high */
985 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
986 initrd_high, initrd_copy_to_ram);
989 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
990 debug (" in-place initrd\n");
991 *initrd_start = rd_data;
992 *initrd_end = rd_data + rd_len;
993 lmb_reserve(lmb, rd_data, rd_len);
996 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
998 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
1000 if (*initrd_start == 0) {
1001 puts ("ramdisk - allocation error\n");
1004 show_boot_progress (12);
1006 *initrd_end = *initrd_start + rd_len;
1007 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1008 *initrd_start, *initrd_end);
1010 memmove_wd ((void *)*initrd_start,
1011 (void *)rd_data, rd_len, CHUNKSZ);
1019 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1020 *initrd_start, *initrd_end);
1029 * boot_get_cmdline - allocate and initialize kernel cmdline
1030 * @lmb: pointer to lmb handle, will be used for memory mgmt
1031 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1032 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1033 * @bootmap_base: ulong variable, holds offset in physical memory to
1036 * boot_get_cmdline() allocates space for kernel command line below
1037 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1038 * variable is present its contents is copied to allocated kernel
1045 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1051 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1052 CFG_BOOTMAPSZ + bootmap_base);
1054 if (cmdline == NULL)
1057 if ((s = getenv("bootargs")) == NULL)
1062 *cmd_start = (ulong) & cmdline[0];
1063 *cmd_end = *cmd_start + strlen(cmdline);
1065 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1071 * boot_get_kbd - allocate and initialize kernel copy of board info
1072 * @lmb: pointer to lmb handle, will be used for memory mgmt
1073 * @kbd: double pointer to board info data
1074 * @bootmap_base: ulong variable, holds offset in physical memory to
1077 * boot_get_kbd() allocates space for kernel copy of board info data below
1078 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1079 * the current u-boot board info data.
1085 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1087 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1088 CFG_BOOTMAPSZ + bootmap_base);
1094 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1096 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1097 do_bdinfo(NULL, 0, 0, NULL);
1102 #endif /* CONFIG_PPC || CONFIG_M68K */
1103 #endif /* !USE_HOSTCC */
1105 #if defined(CONFIG_FIT)
1106 /*****************************************************************************/
1107 /* New uImage format routines */
1108 /*****************************************************************************/
1110 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1111 ulong *addr, const char **name)
1118 sep = strchr (spec, sepc);
1121 *addr = simple_strtoul (spec, NULL, 16);
1131 * fit_parse_conf - parse FIT configuration spec
1132 * @spec: input string, containing configuration spec
1133 * @add_curr: current image address (to be used as a possible default)
1134 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1136 * @conf_name double pointer to a char, will hold pointer to a configuration
1139 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1140 * where <addr> is a FIT image address that contains configuration
1141 * with a <conf> unit name.
1143 * Address part is optional, and if omitted default add_curr will
1147 * 1 if spec is a valid configuration string,
1148 * addr and conf_name are set accordingly
1151 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1152 ulong *addr, const char **conf_name)
1154 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1158 * fit_parse_subimage - parse FIT subimage spec
1159 * @spec: input string, containing subimage spec
1160 * @add_curr: current image address (to be used as a possible default)
1161 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1163 * @image_name: double pointer to a char, will hold pointer to a subimage name
1165 * fit_parse_subimage() expects subimage spec in the for of
1166 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1167 * subimage with a <subimg> unit name.
1169 * Address part is optional, and if omitted default add_curr will
1173 * 1 if spec is a valid subimage string,
1174 * addr and image_name are set accordingly
1177 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1178 ulong *addr, const char **image_name)
1180 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1182 #endif /* !USE_HOSTCC */
1184 static void fit_get_debug (const void *fit, int noffset,
1185 char *prop_name, int err)
1187 debug ("Can't get '%s' property from FIT 0x%08lx, "
1188 "node: offset %d, name %s (%s)\n",
1189 prop_name, (ulong)fit, noffset,
1190 fit_get_name (fit, noffset, NULL),
1191 fdt_strerror (err));
1195 * __fit_print_contents - prints out the contents of the FIT format image
1196 * @fit: pointer to the FIT format image header
1197 * @p: pointer to prefix string
1199 * __fit_print_contents() formats a multi line FIT image contents description.
1200 * The routine prints out FIT image properties (root node level) follwed by
1201 * the details of each component image.
1204 * no returned results
1206 static void __fit_print_contents (const void *fit, const char *p)
1216 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1220 /* Root node properties */
1221 ret = fit_get_desc (fit, 0, &desc);
1222 printf ("%sFIT description: ", p);
1224 printf ("unavailable\n");
1226 printf ("%s\n", desc);
1228 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1229 ret = fit_get_timestamp (fit, 0, ×tamp);
1230 printf ("%sCreated: ", p);
1232 printf ("unavailable\n");
1234 genimg_print_time (timestamp);
1237 /* Find images parent node offset */
1238 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1239 if (images_noffset < 0) {
1240 printf ("Can't find images parent node '%s' (%s)\n",
1241 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1245 /* Process its subnodes, print out component images details */
1246 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1247 (noffset >= 0) && (ndepth > 0);
1248 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1251 * Direct child node of the images parent node,
1252 * i.e. component image node.
1254 printf ("%s Image %u (%s)\n", p, count++,
1255 fit_get_name(fit, noffset, NULL));
1257 fit_image_print (fit, noffset, p);
1261 /* Find configurations parent node offset */
1262 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1263 if (confs_noffset < 0) {
1264 debug ("Can't get configurations parent node '%s' (%s)\n",
1265 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1269 /* get default configuration unit name from default property */
1270 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1272 printf ("%s Default Configuration: '%s'\n", p, uname);
1274 /* Process its subnodes, print out configurations details */
1275 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1276 (noffset >= 0) && (ndepth > 0);
1277 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1280 * Direct child node of the configurations parent node,
1281 * i.e. configuration node.
1283 printf ("%s Configuration %u (%s)\n", p, count++,
1284 fit_get_name(fit, noffset, NULL));
1286 fit_conf_print (fit, noffset, p);
1291 inline void fit_print_contents (const void *fit)
1293 __fit_print_contents (fit, " ");
1296 inline void fit_print_contents_noindent (const void *fit)
1298 __fit_print_contents (fit, "");
1302 * fit_image_print - prints out the FIT component image details
1303 * @fit: pointer to the FIT format image header
1304 * @image_noffset: offset of the component image node
1305 * @p: pointer to prefix string
1307 * fit_image_print() lists all mandatory properies for the processed component
1308 * image. If present, hash nodes are printed out as well.
1311 * no returned results
1313 void fit_image_print (const void *fit, int image_noffset, const char *p)
1316 uint8_t type, arch, os, comp;
1324 /* Mandatory properties */
1325 ret = fit_get_desc (fit, image_noffset, &desc);
1326 printf ("%s Description: ", p);
1328 printf ("unavailable\n");
1330 printf ("%s\n", desc);
1332 fit_image_get_type (fit, image_noffset, &type);
1333 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1335 fit_image_get_comp (fit, image_noffset, &comp);
1336 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1338 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1341 printf ("%s Data Start: ", p);
1343 printf ("unavailable\n");
1345 printf ("0x%08lx\n", (ulong)data);
1348 printf ("%s Data Size: ", p);
1350 printf ("unavailable\n");
1352 genimg_print_size (size);
1354 /* Remaining, type dependent properties */
1355 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1356 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1357 (type == IH_TYPE_FLATDT)) {
1358 fit_image_get_arch (fit, image_noffset, &arch);
1359 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1362 if (type == IH_TYPE_KERNEL) {
1363 fit_image_get_os (fit, image_noffset, &os);
1364 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1367 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1368 ret = fit_image_get_load (fit, image_noffset, &load);
1369 printf ("%s Load Address: ", p);
1371 printf ("unavailable\n");
1373 printf ("0x%08lx\n", load);
1375 fit_image_get_entry (fit, image_noffset, &entry);
1376 printf ("%s Entry Point: ", p);
1378 printf ("unavailable\n");
1380 printf ("0x%08lx\n", entry);
1383 /* Process all hash subnodes of the component image node */
1384 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1385 (noffset >= 0) && (ndepth > 0);
1386 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1388 /* Direct child node of the component image node */
1389 fit_image_print_hash (fit, noffset, p);
1395 * fit_image_print_hash - prints out the hash node details
1396 * @fit: pointer to the FIT format image header
1397 * @noffset: offset of the hash node
1398 * @p: pointer to prefix string
1400 * fit_image_print_hash() lists properies for the processed hash node
1403 * no returned results
1405 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1413 * Check subnode name, must be equal to "hash".
1414 * Multiple hash nodes require unique unit node
1415 * names, e.g. hash@1, hash@2, etc.
1417 if (strncmp (fit_get_name(fit, noffset, NULL),
1419 strlen(FIT_HASH_NODENAME)) != 0)
1422 debug ("%s Hash node: '%s'\n", p,
1423 fit_get_name (fit, noffset, NULL));
1425 printf ("%s Hash algo: ", p);
1426 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1427 printf ("invalid/unsupported\n");
1430 printf ("%s\n", algo);
1432 ret = fit_image_hash_get_value (fit, noffset, &value,
1434 printf ("%s Hash value: ", p);
1436 printf ("unavailable\n");
1438 for (i = 0; i < value_len; i++)
1439 printf ("%02x", value[i]);
1443 debug ("%s Hash len: %d\n", p, value_len);
1447 * fit_get_desc - get node description property
1448 * @fit: pointer to the FIT format image header
1449 * @noffset: node offset
1450 * @desc: double pointer to the char, will hold pointer to the descrption
1452 * fit_get_desc() reads description property from a given node, if
1453 * description is found pointer to it is returened in third call argument.
1459 int fit_get_desc (const void *fit, int noffset, char **desc)
1463 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1464 if (*desc == NULL) {
1465 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1473 * fit_get_timestamp - get node timestamp property
1474 * @fit: pointer to the FIT format image header
1475 * @noffset: node offset
1476 * @timestamp: pointer to the time_t, will hold read timestamp
1478 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1479 * is found and has a correct size its value is retured in third call
1484 * -1, on property read failure
1485 * -2, on wrong timestamp size
1487 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1492 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1494 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1497 if (len != sizeof (uint32_t)) {
1498 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1502 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1507 * fit_image_get_node - get node offset for component image of a given unit name
1508 * @fit: pointer to the FIT format image header
1509 * @image_uname: component image node unit name
1511 * fit_image_get_node() finds a component image (withing the '/images'
1512 * node) of a provided unit name. If image is found its node offset is
1513 * returned to the caller.
1516 * image node offset when found (>=0)
1517 * negative number on failure (FDT_ERR_* code)
1519 int fit_image_get_node (const void *fit, const char *image_uname)
1521 int noffset, images_noffset;
1523 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1524 if (images_noffset < 0) {
1525 debug ("Can't find images parent node '%s' (%s)\n",
1526 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1527 return images_noffset;
1530 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1532 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1533 image_uname, fdt_strerror (noffset));
1540 * fit_image_get_os - get os id for a given component image node
1541 * @fit: pointer to the FIT format image header
1542 * @noffset: component image node offset
1543 * @os: pointer to the uint8_t, will hold os numeric id
1545 * fit_image_get_os() finds os property in a given component image node.
1546 * If the property is found, its (string) value is translated to the numeric
1547 * id which is returned to the caller.
1553 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1558 /* Get OS name from property data */
1559 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1561 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1566 /* Translate OS name to id */
1567 *os = genimg_get_os_id (data);
1572 * fit_image_get_arch - get arch id for a given component image node
1573 * @fit: pointer to the FIT format image header
1574 * @noffset: component image node offset
1575 * @arch: pointer to the uint8_t, will hold arch numeric id
1577 * fit_image_get_arch() finds arch property in a given component image node.
1578 * If the property is found, its (string) value is translated to the numeric
1579 * id which is returned to the caller.
1585 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1590 /* Get architecture name from property data */
1591 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1593 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1598 /* Translate architecture name to id */
1599 *arch = genimg_get_arch_id (data);
1604 * fit_image_get_type - get type id for a given component image node
1605 * @fit: pointer to the FIT format image header
1606 * @noffset: component image node offset
1607 * @type: pointer to the uint8_t, will hold type numeric id
1609 * fit_image_get_type() finds type property in a given component image node.
1610 * If the property is found, its (string) value is translated to the numeric
1611 * id which is returned to the caller.
1617 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1622 /* Get image type name from property data */
1623 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1625 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1630 /* Translate image type name to id */
1631 *type = genimg_get_type_id (data);
1636 * fit_image_get_comp - get comp id for a given component image node
1637 * @fit: pointer to the FIT format image header
1638 * @noffset: component image node offset
1639 * @comp: pointer to the uint8_t, will hold comp numeric id
1641 * fit_image_get_comp() finds comp property in a given component image node.
1642 * If the property is found, its (string) value is translated to the numeric
1643 * id which is returned to the caller.
1649 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1654 /* Get compression name from property data */
1655 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1657 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1662 /* Translate compression name to id */
1663 *comp = genimg_get_comp_id (data);
1668 * fit_image_get_load - get load address property for a given component image node
1669 * @fit: pointer to the FIT format image header
1670 * @noffset: component image node offset
1671 * @load: pointer to the uint32_t, will hold load address
1673 * fit_image_get_load() finds load address property in a given component image node.
1674 * If the property is found, its value is returned to the caller.
1680 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1683 const uint32_t *data;
1685 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1687 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1691 *load = uimage_to_cpu (*data);
1696 * fit_image_get_entry - get entry point address property for a given component image node
1697 * @fit: pointer to the FIT format image header
1698 * @noffset: component image node offset
1699 * @entry: pointer to the uint32_t, will hold entry point address
1701 * fit_image_get_entry() finds entry point address property in a given component image node.
1702 * If the property is found, its value is returned to the caller.
1708 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1711 const uint32_t *data;
1713 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1715 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1719 *entry = uimage_to_cpu (*data);
1724 * fit_image_get_data - get data property and its size for a given component image node
1725 * @fit: pointer to the FIT format image header
1726 * @noffset: component image node offset
1727 * @data: double pointer to void, will hold data property's data address
1728 * @size: pointer to size_t, will hold data property's data size
1730 * fit_image_get_data() finds data property in a given component image node.
1731 * If the property is found its data start address and size are returned to
1738 int fit_image_get_data (const void *fit, int noffset,
1739 const void **data, size_t *size)
1743 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1744 if (*data == NULL) {
1745 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1755 * fit_image_hash_get_algo - get hash algorithm name
1756 * @fit: pointer to the FIT format image header
1757 * @noffset: hash node offset
1758 * @algo: double pointer to char, will hold pointer to the algorithm name
1760 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1761 * If the property is found its data start address is returned to the caller.
1767 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1771 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1772 if (*algo == NULL) {
1773 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1781 * fit_image_hash_get_value - get hash value and length
1782 * @fit: pointer to the FIT format image header
1783 * @noffset: hash node offset
1784 * @value: double pointer to uint8_t, will hold address of a hash value data
1785 * @value_len: pointer to an int, will hold hash data length
1787 * fit_image_hash_get_value() finds hash value property in a given hash node.
1788 * If the property is found its data start address and size are returned to
1795 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1800 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1801 if (*value == NULL) {
1802 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1812 * fit_set_timestamp - set node timestamp property
1813 * @fit: pointer to the FIT format image header
1814 * @noffset: node offset
1815 * @timestamp: timestamp value to be set
1817 * fit_set_timestamp() attempts to set timestamp property in the requested
1818 * node and returns operation status to the caller.
1822 * -1, on property read failure
1824 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1829 t = cpu_to_uimage (timestamp);
1830 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1833 printf ("Can't set '%s' property for '%s' node (%s)\n",
1834 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1835 fdt_strerror (ret));
1843 * calculate_hash - calculate and return hash for provided input data
1844 * @data: pointer to the input data
1845 * @data_len: data length
1846 * @algo: requested hash algorithm
1847 * @value: pointer to the char, will hold hash value data (caller must
1848 * allocate enough free space)
1849 * value_len: length of the calculated hash
1851 * calculate_hash() computes input data hash according to the requested algorithm.
1852 * Resulting hash value is placed in caller provided 'value' buffer, length
1853 * of the calculated hash is returned via value_len pointer argument.
1857 * -1, when algo is unsupported
1859 static int calculate_hash (const void *data, int data_len, const char *algo,
1860 uint8_t *value, int *value_len)
1862 if (strcmp (algo, "crc32") == 0 ) {
1863 *((uint32_t *)value) = crc32 (0, data, data_len);
1864 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1866 } else if (strcmp (algo, "sha1") == 0 ) {
1867 sha1_csum ((unsigned char *) data, data_len,
1868 (unsigned char *) value);
1870 } else if (strcmp (algo, "md5") == 0 ) {
1871 printf ("MD5 not supported\n");
1874 debug ("Unsupported hash alogrithm\n");
1882 * fit_set_hashes - process FIT component image nodes and calculate hashes
1883 * @fit: pointer to the FIT format image header
1885 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1886 * Hashes are calculated for all component images which have hash subnodes
1887 * with algorithm property set to one of the supported hash algorithms.
1891 * libfdt error code, on failure
1893 int fit_set_hashes (void *fit)
1900 /* Find images parent node offset */
1901 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1902 if (images_noffset < 0) {
1903 printf ("Can't find images parent node '%s' (%s)\n",
1904 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1905 return images_noffset;
1908 /* Process its subnodes, print out component images details */
1909 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1910 (noffset >= 0) && (ndepth > 0);
1911 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1914 * Direct child node of the images parent node,
1915 * i.e. component image node.
1917 ret = fit_image_set_hashes (fit, noffset);
1927 * fit_image_set_hashes - calculate/set hashes for given component image node
1928 * @fit: pointer to the FIT format image header
1929 * @image_noffset: requested component image node
1931 * fit_image_set_hashes() adds hash values for an component image node. All
1932 * existing hash subnodes are checked, if algorithm property is set to one of
1933 * the supported hash algorithms, hash value is computed and corresponding
1934 * hash node property is set, for example:
1936 * Input component image node structure:
1938 * o image@1 (at image_noffset)
1939 * | - data = [binary data]
1943 * Output component image node structure:
1945 * o image@1 (at image_noffset)
1946 * | - data = [binary data]
1949 * |- value = sha1(data)
1955 int fit_image_set_hashes (void *fit, int image_noffset)
1960 uint8_t value[FIT_MAX_HASH_LEN];
1965 /* Get image data and data length */
1966 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
1967 printf ("Can't get image data/size\n");
1971 /* Process all hash subnodes of the component image node */
1972 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1973 (noffset >= 0) && (ndepth > 0);
1974 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1976 /* Direct child node of the component image node */
1979 * Check subnode name, must be equal to "hash".
1980 * Multiple hash nodes require unique unit node
1981 * names, e.g. hash@1, hash@2, etc.
1983 if (strncmp (fit_get_name(fit, noffset, NULL),
1985 strlen(FIT_HASH_NODENAME)) != 0) {
1986 /* Not a hash subnode, skip it */
1990 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1991 printf ("Can't get hash algo property for "
1992 "'%s' hash node in '%s' image node\n",
1993 fit_get_name (fit, noffset, NULL),
1994 fit_get_name (fit, image_noffset, NULL));
1998 if (calculate_hash (data, size, algo, value, &value_len)) {
1999 printf ("Unsupported hash algorithm (%s) for "
2000 "'%s' hash node in '%s' image node\n",
2001 algo, fit_get_name (fit, noffset, NULL),
2002 fit_get_name (fit, image_noffset, NULL));
2006 if (fit_image_hash_set_value (fit, noffset, value,
2008 printf ("Can't set hash value for "
2009 "'%s' hash node in '%s' image node\n",
2010 fit_get_name (fit, noffset, NULL),
2011 fit_get_name (fit, image_noffset, NULL));
2021 * fit_image_hash_set_value - set hash value in requested has node
2022 * @fit: pointer to the FIT format image header
2023 * @noffset: hash node offset
2024 * @value: hash value to be set
2025 * @value_len: hash value length
2027 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2028 * given and returns operation status to the caller.
2034 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2039 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2041 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2042 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2043 fdt_strerror (ret));
2049 #endif /* USE_HOSTCC */
2052 * fit_image_check_hashes - verify data intergity
2053 * @fit: pointer to the FIT format image header
2054 * @image_noffset: component image node offset
2056 * fit_image_check_hashes() goes over component image hash nodes,
2057 * re-calculates each data hash and compares with the value stored in hash
2061 * 1, if all hashes are valid
2062 * 0, otherwise (or on error)
2064 int fit_image_check_hashes (const void *fit, int image_noffset)
2071 uint8_t value[FIT_MAX_HASH_LEN];
2077 /* Get image data and data length */
2078 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2079 printf ("Can't get image data/size\n");
2083 /* Process all hash subnodes of the component image node */
2084 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2085 (noffset >= 0) && (ndepth > 0);
2086 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2088 /* Direct child node of the component image node */
2091 * Check subnode name, must be equal to "hash".
2092 * Multiple hash nodes require unique unit node
2093 * names, e.g. hash@1, hash@2, etc.
2095 if (strncmp (fit_get_name(fit, noffset, NULL),
2097 strlen(FIT_HASH_NODENAME)) != 0)
2100 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2101 err_msg = "Can't get hash algo property";
2104 printf ("%s", algo);
2106 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2108 err_msg = "Can't get hash value property";
2112 if (calculate_hash (data, size, algo, value, &value_len)) {
2113 err_msg = "Unsupported hash algorithm";
2117 if (value_len != fit_value_len) {
2118 err_msg = "Bad hash value len";
2120 } else if (memcmp (value, fit_value, value_len) != 0) {
2121 err_msg = "Bad hash value";
2131 printf ("%s for '%s' hash node in '%s' image node\n",
2132 err_msg, fit_get_name (fit, noffset, NULL),
2133 fit_get_name (fit, image_noffset, NULL));
2138 * fit_image_check_os - check whether image node is of a given os type
2139 * @fit: pointer to the FIT format image header
2140 * @noffset: component image node offset
2141 * @os: requested image os
2143 * fit_image_check_os() reads image os property and compares its numeric
2144 * id with the requested os. Comparison result is returned to the caller.
2147 * 1 if image is of given os type
2148 * 0 otherwise (or on error)
2150 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2154 if (fit_image_get_os (fit, noffset, &image_os))
2156 return (os == image_os);
2160 * fit_image_check_arch - check whether image node is of a given arch
2161 * @fit: pointer to the FIT format image header
2162 * @noffset: component image node offset
2163 * @arch: requested imagearch
2165 * fit_image_check_arch() reads image arch property and compares its numeric
2166 * id with the requested arch. Comparison result is returned to the caller.
2169 * 1 if image is of given arch
2170 * 0 otherwise (or on error)
2172 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2176 if (fit_image_get_arch (fit, noffset, &image_arch))
2178 return (arch == image_arch);
2182 * fit_image_check_type - check whether image node is of a given type
2183 * @fit: pointer to the FIT format image header
2184 * @noffset: component image node offset
2185 * @type: requested image type
2187 * fit_image_check_type() reads image type property and compares its numeric
2188 * id with the requested type. Comparison result is returned to the caller.
2191 * 1 if image is of given type
2192 * 0 otherwise (or on error)
2194 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2198 if (fit_image_get_type (fit, noffset, &image_type))
2200 return (type == image_type);
2204 * fit_image_check_comp - check whether image node uses given compression
2205 * @fit: pointer to the FIT format image header
2206 * @noffset: component image node offset
2207 * @comp: requested image compression type
2209 * fit_image_check_comp() reads image compression property and compares its
2210 * numeric id with the requested compression type. Comparison result is
2211 * returned to the caller.
2214 * 1 if image uses requested compression
2215 * 0 otherwise (or on error)
2217 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2221 if (fit_image_get_comp (fit, noffset, &image_comp))
2223 return (comp == image_comp);
2227 * fit_check_format - sanity check FIT image format
2228 * @fit: pointer to the FIT format image header
2230 * fit_check_format() runs a basic sanity FIT image verification.
2231 * Routine checks for mandatory properties, nodes, etc.
2237 int fit_check_format (const void *fit)
2239 /* mandatory / node 'description' property */
2240 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2241 debug ("Wrong FIT format: no description\n");
2245 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2246 /* mandatory / node 'timestamp' property */
2247 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2248 debug ("Wrong FIT format: no description\n");
2253 /* mandatory subimages parent '/images' node */
2254 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2255 debug ("Wrong FIT format: no images parent node\n");
2263 * fit_conf_get_node - get node offset for configuration of a given unit name
2264 * @fit: pointer to the FIT format image header
2265 * @conf_uname: configuration node unit name
2267 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2268 * parant node) of a provided unit name. If configuration is found its node offset
2269 * is returned to the caller.
2271 * When NULL is provided in second argument fit_conf_get_node() will search
2272 * for a default configuration node instead. Default configuration node unit name
2273 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2276 * configuration node offset when found (>=0)
2277 * negative number on failure (FDT_ERR_* code)
2279 int fit_conf_get_node (const void *fit, const char *conf_uname)
2281 int noffset, confs_noffset;
2284 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2285 if (confs_noffset < 0) {
2286 debug ("Can't find configurations parent node '%s' (%s)\n",
2287 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2288 return confs_noffset;
2291 if (conf_uname == NULL) {
2292 /* get configuration unit name from the default property */
2293 debug ("No configuration specified, trying default...\n");
2294 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2295 if (conf_uname == NULL) {
2296 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2299 debug ("Found default configuration: '%s'\n", conf_uname);
2302 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2304 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2305 conf_uname, fdt_strerror (noffset));
2311 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2312 const char *prop_name)
2317 /* get kernel image unit name from configuration kernel property */
2318 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2322 return fit_image_get_node (fit, uname);
2326 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2327 * a given configuration
2328 * @fit: pointer to the FIT format image header
2329 * @noffset: configuration node offset
2331 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2332 * configuration FIT_KERNEL_PROP property and translates it to the node
2336 * image node offset when found (>=0)
2337 * negative number on failure (FDT_ERR_* code)
2339 int fit_conf_get_kernel_node (const void *fit, int noffset)
2341 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2345 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2346 * a given configuration
2347 * @fit: pointer to the FIT format image header
2348 * @noffset: configuration node offset
2350 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2351 * configuration FIT_KERNEL_PROP property and translates it to the node
2355 * image node offset when found (>=0)
2356 * negative number on failure (FDT_ERR_* code)
2358 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2360 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2364 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2365 * a given configuration
2366 * @fit: pointer to the FIT format image header
2367 * @noffset: configuration node offset
2369 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2370 * configuration FIT_KERNEL_PROP property and translates it to the node
2374 * image node offset when found (>=0)
2375 * negative number on failure (FDT_ERR_* code)
2377 int fit_conf_get_fdt_node (const void *fit, int noffset)
2379 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2383 * fit_conf_print - prints out the FIT configuration details
2384 * @fit: pointer to the FIT format image header
2385 * @conf_noffset: offset of the configuration node
2386 * @p: pointer to prefix string
2388 * fit_conf_print() lists all mandatory properies for the processed
2389 * configuration node.
2392 * no returned results
2394 void fit_conf_print (const void *fit, int noffset, const char *p)
2400 /* Mandatory properties */
2401 ret = fit_get_desc (fit, noffset, &desc);
2402 printf ("%s Description: ", p);
2404 printf ("unavailable\n");
2406 printf ("%s\n", desc);
2408 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2409 printf ("%s Kernel: ", p);
2411 printf ("unavailable\n");
2413 printf ("%s\n", uname);
2415 /* Optional properties */
2416 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2418 printf ("%s Init Ramdisk: %s\n", p, uname);
2420 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2422 printf ("%s FDT: %s\n", p, uname);
2426 * fit_check_ramdisk - verify FIT format ramdisk subimage
2427 * @fit_hdr: pointer to the FIT ramdisk header
2428 * @rd_noffset: ramdisk subimage node offset within FIT image
2429 * @arch: requested ramdisk image architecture type
2430 * @verify: data CRC verification flag
2432 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2433 * specified FIT image.
2440 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2442 fit_image_print (fit, rd_noffset, " ");
2445 puts (" Verifying Hash Integrity ... ");
2446 if (!fit_image_check_hashes (fit, rd_noffset)) {
2447 puts ("Bad Data Hash\n");
2453 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2454 !fit_image_check_arch (fit, rd_noffset, arch) ||
2455 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2456 printf ("No Linux %s Ramdisk Image\n",
2457 genimg_get_arch_name(arch));
2463 #endif /* USE_HOSTCC */
2464 #endif /* CONFIG_FIT */