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 #ifdef CONFIG_LOGBUFFER
43 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
49 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
52 #include <fdt_support.h>
55 #if defined(CONFIG_FIT)
56 #include <u-boot/md5.h>
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,
73 #include <u-boot/md5.h>
76 #endif /* !USE_HOSTCC*/
78 typedef struct table_entry {
79 int id; /* as defined in image.h */
80 char *sname; /* short (input) name */
81 char *lname; /* long (output) name */
84 static table_entry_t uimage_arch[] = {
85 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
86 { IH_ARCH_ALPHA, "alpha", "Alpha", },
87 { IH_ARCH_ARM, "arm", "ARM", },
88 { IH_ARCH_I386, "x86", "Intel x86", },
89 { IH_ARCH_IA64, "ia64", "IA64", },
90 { IH_ARCH_M68K, "m68k", "M68K", },
91 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
92 { IH_ARCH_MIPS, "mips", "MIPS", },
93 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
94 { IH_ARCH_NIOS, "nios", "NIOS", },
95 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
96 { IH_ARCH_PPC, "ppc", "PowerPC", },
97 { IH_ARCH_S390, "s390", "IBM S390", },
98 { IH_ARCH_SH, "sh", "SuperH", },
99 { IH_ARCH_SPARC, "sparc", "SPARC", },
100 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
101 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
102 { IH_ARCH_AVR32, "avr32", "AVR32", },
106 static table_entry_t uimage_os[] = {
107 { IH_OS_INVALID, NULL, "Invalid OS", },
108 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
109 { IH_OS_ARTOS, "artos", "ARTOS", },
111 { IH_OS_LINUX, "linux", "Linux", },
112 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
113 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
115 { IH_OS_NETBSD, "netbsd", "NetBSD", },
116 { IH_OS_RTEMS, "rtems", "RTEMS", },
117 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
118 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
119 { IH_OS_QNX, "qnx", "QNX", },
120 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
123 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
124 { IH_OS_DELL, "dell", "Dell", },
125 { IH_OS_ESIX, "esix", "Esix", },
126 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
127 { IH_OS_IRIX, "irix", "Irix", },
128 { IH_OS_NCR, "ncr", "NCR", },
129 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
130 { IH_OS_PSOS, "psos", "pSOS", },
131 { IH_OS_SCO, "sco", "SCO", },
132 { IH_OS_SOLARIS, "solaris", "Solaris", },
133 { IH_OS_SVR4, "svr4", "SVR4", },
138 static table_entry_t uimage_type[] = {
139 { IH_TYPE_INVALID, NULL, "Invalid Image", },
140 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
141 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
142 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
143 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
144 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
145 { IH_TYPE_SCRIPT, "script", "Script", },
146 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
147 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
151 static table_entry_t uimage_comp[] = {
152 { IH_COMP_NONE, "none", "uncompressed", },
153 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
154 { IH_COMP_GZIP, "gzip", "gzip compressed", },
158 uint32_t crc32 (uint32_t, const unsigned char *, uint);
159 static void genimg_print_size (uint32_t size);
160 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
161 static void genimg_print_time (time_t timestamp);
164 /*****************************************************************************/
165 /* Legacy format routines */
166 /*****************************************************************************/
167 int image_check_hcrc (image_header_t *hdr)
170 ulong len = image_get_header_size ();
171 image_header_t header;
173 /* Copy header so we can blank CRC field for re-calculation */
174 memmove (&header, (char *)hdr, image_get_header_size ());
175 image_set_hcrc (&header, 0);
177 hcrc = crc32 (0, (unsigned char *)&header, len);
179 return (hcrc == image_get_hcrc (hdr));
182 int image_check_dcrc (image_header_t *hdr)
184 ulong data = image_get_data (hdr);
185 ulong len = image_get_data_size (hdr);
186 ulong dcrc = crc32 (0, (unsigned char *)data, len);
188 return (dcrc == image_get_dcrc (hdr));
192 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
195 ulong len = image_get_data_size (hdr);
196 ulong data = image_get_data (hdr);
198 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
200 ulong edata = cdata + len;
202 while (cdata < edata) {
203 ulong chunk = edata - cdata;
207 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
213 dcrc = crc32 (0, (unsigned char *)data, len);
216 return (dcrc == image_get_dcrc (hdr));
218 #endif /* !USE_HOSTCC */
221 * image_multi_count - get component (sub-image) count
222 * @hdr: pointer to the header of the multi component image
224 * image_multi_count() returns number of components in a multi
227 * Note: no checking of the image type is done, caller must pass
228 * a valid multi component image.
231 * number of components
233 ulong image_multi_count (image_header_t *hdr)
238 /* get start of the image payload, which in case of multi
239 * component images that points to a table of component sizes */
240 size = (uint32_t *)image_get_data (hdr);
242 /* count non empty slots */
243 for (i = 0; size[i]; ++i)
250 * image_multi_getimg - get component data address and size
251 * @hdr: pointer to the header of the multi component image
252 * @idx: index of the requested component
253 * @data: pointer to a ulong variable, will hold component data address
254 * @len: pointer to a ulong variable, will hold component size
256 * image_multi_getimg() returns size and data address for the requested
257 * component in a multi component image.
259 * Note: no checking of the image type is done, caller must pass
260 * a valid multi component image.
263 * data address and size of the component, if idx is valid
264 * 0 in data and len, if idx is out of range
266 void image_multi_getimg (image_header_t *hdr, ulong idx,
267 ulong *data, ulong *len)
271 ulong offset, tail, count, img_data;
273 /* get number of component */
274 count = image_multi_count (hdr);
276 /* get start of the image payload, which in case of multi
277 * component images that points to a table of component sizes */
278 size = (uint32_t *)image_get_data (hdr);
280 /* get address of the proper component data start, which means
281 * skipping sizes table (add 1 for last, null entry) */
282 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
285 *len = uimage_to_cpu (size[idx]);
289 /* go over all indices preceding requested component idx */
290 for (i = 0; i < idx; i++) {
291 /* add up i-th component size */
292 offset += uimage_to_cpu (size[i]);
294 /* add up alignment for i-th component */
295 tail += (4 - uimage_to_cpu (size[i]) % 4);
298 /* calculate idx-th component data address */
299 *data = img_data + offset + tail;
306 static void image_print_type (image_header_t *hdr)
308 const char *os, *arch, *type, *comp;
310 os = genimg_get_os_name (image_get_os (hdr));
311 arch = genimg_get_arch_name (image_get_arch (hdr));
312 type = genimg_get_type_name (image_get_type (hdr));
313 comp = genimg_get_comp_name (image_get_comp (hdr));
315 printf ("%s %s %s (%s)\n", arch, os, type, comp);
319 * image_print_contents - prints out the contents of the legacy format image
320 * @hdr: pointer to the legacy format image header
321 * @p: pointer to prefix string
323 * image_print_contents() formats a multi line legacy image contents description.
324 * The routine prints out all header fields followed by the size/offset data
325 * for MULTI/SCRIPT images.
328 * no returned results
330 void image_print_contents (image_header_t *hdr)
340 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
341 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
342 printf ("%sCreated: ", p);
343 genimg_print_time ((time_t)image_get_time (hdr));
345 printf ("%sImage Type: ", p);
346 image_print_type (hdr);
347 printf ("%sData Size: ", p);
348 genimg_print_size (image_get_data_size (hdr));
349 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
350 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
352 if (image_check_type (hdr, IH_TYPE_MULTI) ||
353 image_check_type (hdr, IH_TYPE_SCRIPT)) {
356 ulong count = image_multi_count (hdr);
358 printf ("%sContents:\n", p);
359 for (i = 0; i < count; i++) {
360 image_multi_getimg (hdr, i, &data, &len);
362 printf ("%s Image %d: ", p, i);
363 genimg_print_size (len);
365 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
367 * the user may need to know offsets
368 * if planning to do something with
371 printf ("%s Offset = 0x%08lx\n", p, data);
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 = (image_header_t *)rd_addr;
402 if (!image_check_magic (rd_hdr)) {
403 puts ("Bad Magic Number\n");
404 show_boot_progress (-10);
408 if (!image_check_hcrc (rd_hdr)) {
409 puts ("Bad Header Checksum\n");
410 show_boot_progress (-11);
414 show_boot_progress (10);
415 image_print_contents (rd_hdr);
418 puts(" Verifying Checksum ... ");
419 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
420 puts ("Bad Data CRC\n");
421 show_boot_progress (-12);
427 show_boot_progress (11);
429 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
430 !image_check_arch (rd_hdr, arch) ||
431 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
432 printf ("No Linux %s Ramdisk Image\n",
433 genimg_get_arch_name(arch));
434 show_boot_progress (-13);
440 #endif /* !USE_HOSTCC */
442 /*****************************************************************************/
443 /* Shared dual-format routines */
444 /*****************************************************************************/
446 int getenv_yesno (char *var)
448 char *s = getenv (var);
449 return (s && (*s == 'n')) ? 0 : 1;
452 ulong getenv_bootm_low(void)
454 char *s = getenv ("bootm_low");
456 ulong tmp = simple_strtoul (s, NULL, 16);
460 #if defined(CFG_SDRAM_BASE)
461 return CFG_SDRAM_BASE;
462 #elif defined(CONFIG_ARM)
463 return gd->bd->bi_dram[0].start;
469 ulong getenv_bootm_size(void)
471 char *s = getenv ("bootm_size");
473 ulong tmp = simple_strtoul (s, NULL, 16);
477 #if defined(CONFIG_ARM)
478 return gd->bd->bi_dram[0].size;
480 return gd->bd->bi_memsize;
484 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
486 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
488 size_t tail = (len > chunksz) ? chunksz : len;
490 memmove (to, from, tail);
495 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
496 memmove (to, from, len);
497 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
499 #endif /* !USE_HOSTCC */
501 static void genimg_print_size (uint32_t size)
504 printf ("%d Bytes = ", size);
505 print_size (size, "\n");
507 printf ("%d Bytes = %.2f kB = %.2f MB\n",
508 size, (double)size / 1.024e3,
509 (double)size / 1.048576e6);
513 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
514 static void genimg_print_time (time_t timestamp)
519 to_tm (timestamp, &tm);
520 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
521 tm.tm_year, tm.tm_mon, tm.tm_mday,
522 tm.tm_hour, tm.tm_min, tm.tm_sec);
524 printf ("%s", ctime(×tamp));
527 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
530 * get_table_entry_name - translate entry id to long name
531 * @table: pointer to a translation table for entries of a specific type
532 * @msg: message to be returned when translation fails
533 * @id: entry id to be translated
535 * get_table_entry_name() will go over translation table trying to find
536 * entry that matches given id. If matching entry is found, its long
537 * name is returned to the caller.
540 * long entry name if translation succeeds
543 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
545 for (; table->id >= 0; ++table) {
547 return (table->lname);
552 const char *genimg_get_os_name (uint8_t os)
554 return (get_table_entry_name (uimage_os, "Unknown OS", os));
557 const char *genimg_get_arch_name (uint8_t arch)
559 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
562 const char *genimg_get_type_name (uint8_t type)
564 return (get_table_entry_name (uimage_type, "Unknown Image", type));
567 const char *genimg_get_comp_name (uint8_t comp)
569 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
573 * get_table_entry_id - translate short entry name to id
574 * @table: pointer to a translation table for entries of a specific type
575 * @table_name: to be used in case of error
576 * @name: entry short name to be translated
578 * get_table_entry_id() will go over translation table trying to find
579 * entry that matches given short name. If matching entry is found,
580 * its id returned to the caller.
583 * entry id if translation succeeds
586 static int get_table_entry_id (table_entry_t *table,
587 const char *table_name, const char *name)
593 for (t = table; t->id >= 0; ++t) {
594 if (t->sname && strcasecmp(t->sname, name) == 0)
598 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
599 for (t = table; t->id >= 0; ++t) {
600 if (t->sname == NULL)
602 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
605 fprintf (stderr, "\n");
607 for (t = table; t->id >= 0; ++t) {
608 if (t->sname && strcmp(t->sname, name) == 0)
611 debug ("Invalid %s Type: %s\n", table_name, name);
612 #endif /* USE_HOSTCC */
616 int genimg_get_os_id (const char *name)
618 return (get_table_entry_id (uimage_os, "OS", name));
621 int genimg_get_arch_id (const char *name)
623 return (get_table_entry_id (uimage_arch, "CPU", name));
626 int genimg_get_type_id (const char *name)
628 return (get_table_entry_id (uimage_type, "Image", name));
631 int genimg_get_comp_id (const char *name)
633 return (get_table_entry_id (uimage_comp, "Compression", name));
638 * genimg_get_format - get image format type
639 * @img_addr: image start address
641 * genimg_get_format() checks whether provided address points to a valid
642 * legacy or FIT image.
644 * New uImage format and FDT blob are based on a libfdt. FDT blob
645 * may be passed directly or embedded in a FIT image. In both situations
646 * genimg_get_format() must be able to dectect libfdt header.
649 * image format type or IMAGE_FORMAT_INVALID if no image is present
651 int genimg_get_format (void *img_addr)
653 ulong format = IMAGE_FORMAT_INVALID;
655 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
659 hdr = (image_header_t *)img_addr;
660 if (image_check_magic(hdr))
661 format = IMAGE_FORMAT_LEGACY;
662 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
664 fit_hdr = (char *)img_addr;
665 if (fdt_check_header (fit_hdr) == 0)
666 format = IMAGE_FORMAT_FIT;
674 * genimg_get_image - get image from special storage (if necessary)
675 * @img_addr: image start address
677 * genimg_get_image() checks if provided image start adddress is located
678 * in a dataflash storage. If so, image is moved to a system RAM memory.
681 * image start address after possible relocation from special storage
683 ulong genimg_get_image (ulong img_addr)
685 ulong ram_addr = img_addr;
687 #ifdef CONFIG_HAS_DATAFLASH
688 ulong h_size, d_size;
690 if (addr_dataflash (img_addr)){
691 /* ger RAM address */
692 ram_addr = CFG_LOAD_ADDR;
694 /* get header size */
695 h_size = image_get_header_size ();
696 #if defined(CONFIG_FIT)
697 if (sizeof(struct fdt_header) > h_size)
698 h_size = sizeof(struct fdt_header);
702 debug (" Reading image header from dataflash address "
703 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
705 read_dataflash (img_addr, h_size, (char *)ram_addr);
708 switch (genimg_get_format ((void *)ram_addr)) {
709 case IMAGE_FORMAT_LEGACY:
710 d_size = image_get_data_size ((image_header_t *)ram_addr);
711 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
714 #if defined(CONFIG_FIT)
715 case IMAGE_FORMAT_FIT:
716 d_size = fit_get_size ((const void *)ram_addr) - h_size;
717 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
722 printf (" No valid image found at 0x%08lx\n", img_addr);
726 /* read in image data */
727 debug (" Reading image remaining data from dataflash address "
728 "%08lx to RAM address %08lx\n", img_addr + h_size,
731 read_dataflash (img_addr + h_size, d_size,
732 (char *)(ram_addr + h_size));
735 #endif /* CONFIG_HAS_DATAFLASH */
741 * fit_has_config - check if there is a valid FIT configuration
742 * @images: pointer to the bootm command headers structure
744 * fit_has_config() checks if there is a FIT configuration in use
745 * (if FTI support is present).
748 * 0, no FIT support or no configuration found
749 * 1, configuration found
751 int genimg_has_config (bootm_headers_t *images)
753 #if defined(CONFIG_FIT)
754 if (images->fit_uname_cfg)
761 * boot_get_ramdisk - main ramdisk handling routine
762 * @argc: command argument count
763 * @argv: command argument list
764 * @images: pointer to the bootm images structure
765 * @arch: expected ramdisk architecture
766 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
767 * @rd_end: pointer to a ulong variable, will hold ramdisk end
769 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
770 * Curently supported are the following ramdisk sources:
771 * - multicomponent kernel/ramdisk image,
772 * - commandline provided address of decicated ramdisk image.
775 * 0, if ramdisk image was found and valid, or skiped
776 * rd_start and rd_end are set to ramdisk start/end addresses if
777 * ramdisk image is found and valid
779 * 1, if ramdisk image is found but corrupted
780 * rd_start and rd_end are set to 0 if no ramdisk exists
782 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
783 uint8_t arch, ulong *rd_start, ulong *rd_end)
785 ulong rd_addr, rd_load;
786 ulong rd_data, rd_len;
787 image_header_t *rd_hdr;
788 #if defined(CONFIG_FIT)
790 const char *fit_uname_config = NULL;
791 const char *fit_uname_ramdisk = NULL;
803 * Look for a '-' which indicates to ignore the
806 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
807 debug ("## Skipping init Ramdisk\n");
808 rd_len = rd_data = 0;
809 } else if (argc >= 3 || genimg_has_config (images)) {
810 #if defined(CONFIG_FIT)
813 * If the init ramdisk comes from the FIT image and
814 * the FIT image address is omitted in the command
815 * line argument, try to use os FIT image address or
816 * default load address.
818 if (images->fit_uname_os)
819 default_addr = (ulong)images->fit_hdr_os;
821 default_addr = load_addr;
823 if (fit_parse_conf (argv[2], default_addr,
824 &rd_addr, &fit_uname_config)) {
825 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
826 fit_uname_config, rd_addr);
827 } else if (fit_parse_subimage (argv[2], default_addr,
828 &rd_addr, &fit_uname_ramdisk)) {
829 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
830 fit_uname_ramdisk, rd_addr);
834 rd_addr = simple_strtoul(argv[2], NULL, 16);
835 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
838 #if defined(CONFIG_FIT)
840 /* use FIT configuration provided in first bootm
843 rd_addr = (ulong)images->fit_hdr_os;
844 fit_uname_config = images->fit_uname_cfg;
845 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
846 fit_uname_config, rd_addr);
849 * Check whether configuration has ramdisk defined,
850 * if not, don't try to use it, quit silently.
852 fit_hdr = (void *)rd_addr;
853 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
854 if (cfg_noffset < 0) {
855 debug ("* ramdisk: no such config\n");
859 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
860 if (rd_noffset < 0) {
861 debug ("* ramdisk: no ramdisk in config\n");
867 /* copy from dataflash if needed */
868 rd_addr = genimg_get_image (rd_addr);
871 * Check if there is an initrd image at the
872 * address provided in the second bootm argument
873 * check image type, for FIT images get FIT node.
875 switch (genimg_get_format ((void *)rd_addr)) {
876 case IMAGE_FORMAT_LEGACY:
877 printf ("## Loading init Ramdisk from Legacy "
878 "Image at %08lx ...\n", rd_addr);
880 show_boot_progress (9);
881 rd_hdr = image_get_ramdisk (rd_addr, arch,
887 rd_data = image_get_data (rd_hdr);
888 rd_len = image_get_data_size (rd_hdr);
889 rd_load = image_get_load (rd_hdr);
891 #if defined(CONFIG_FIT)
892 case IMAGE_FORMAT_FIT:
893 fit_hdr = (void *)rd_addr;
894 printf ("## Loading init Ramdisk from FIT "
895 "Image at %08lx ...\n", rd_addr);
897 show_boot_progress (120);
898 if (!fit_check_format (fit_hdr)) {
899 puts ("Bad FIT ramdisk image format!\n");
900 show_boot_progress (-120);
903 show_boot_progress (121);
905 if (!fit_uname_ramdisk) {
907 * no ramdisk image node unit name, try to get config
908 * node first. If config unit node name is NULL
909 * fit_conf_get_node() will try to find default config node
911 show_boot_progress (122);
912 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
913 if (cfg_noffset < 0) {
914 puts ("Could not find configuration node\n");
915 show_boot_progress (-122);
918 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
919 printf (" Using '%s' configuration\n", fit_uname_config);
921 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
922 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
924 /* get ramdisk component image node offset */
925 show_boot_progress (123);
926 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
928 if (rd_noffset < 0) {
929 puts ("Could not find subimage node\n");
930 show_boot_progress (-124);
934 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
936 show_boot_progress (125);
937 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
940 /* get ramdisk image data address and length */
941 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
942 puts ("Could not find ramdisk subimage data!\n");
943 show_boot_progress (-127);
946 show_boot_progress (128);
948 rd_data = (ulong)data;
951 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
952 puts ("Can't get ramdisk subimage load address!\n");
953 show_boot_progress (-129);
956 show_boot_progress (129);
958 images->fit_hdr_rd = fit_hdr;
959 images->fit_uname_rd = fit_uname_ramdisk;
960 images->fit_noffset_rd = rd_noffset;
964 puts ("Wrong Ramdisk Image Format\n");
965 rd_data = rd_len = rd_load = 0;
968 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
970 * We need to copy the ramdisk to SRAM to let Linux boot
973 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
976 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
978 } else if (images->legacy_hdr_valid &&
979 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
981 * Now check if we have a legacy mult-component image,
982 * get second entry data start address and len.
984 show_boot_progress (13);
985 printf ("## Loading init Ramdisk from multi component "
986 "Legacy Image at %08lx ...\n",
987 (ulong)images->legacy_hdr_os);
989 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
994 show_boot_progress (14);
995 rd_len = rd_data = 0;
999 debug ("## No init Ramdisk\n");
1001 *rd_start = rd_data;
1002 *rd_end = rd_data + rd_len;
1004 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1005 *rd_start, *rd_end);
1010 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
1012 * boot_ramdisk_high - relocate init ramdisk
1013 * @lmb: pointer to lmb handle, will be used for memory mgmt
1014 * @rd_data: ramdisk data start address
1015 * @rd_len: ramdisk data length
1016 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1017 * start address (after possible relocation)
1018 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1019 * end address (after possible relocation)
1021 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1022 * variable and if requested ramdisk data is moved to a specified location.
1024 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1025 * start/end addresses if ramdisk image start and len were provided,
1026 * otherwise set initrd_start and initrd_end set to zeros.
1032 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1033 ulong *initrd_start, ulong *initrd_end)
1037 int initrd_copy_to_ram = 1;
1039 if ((s = getenv ("initrd_high")) != NULL) {
1040 /* a value of "no" or a similar string will act like 0,
1041 * turning the "load high" feature off. This is intentional.
1043 initrd_high = simple_strtoul (s, NULL, 16);
1044 if (initrd_high == ~0)
1045 initrd_copy_to_ram = 0;
1047 /* not set, no restrictions to load high */
1051 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1052 initrd_high, initrd_copy_to_ram);
1055 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1056 debug (" in-place initrd\n");
1057 *initrd_start = rd_data;
1058 *initrd_end = rd_data + rd_len;
1059 lmb_reserve(lmb, rd_data, rd_len);
1062 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1064 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
1066 if (*initrd_start == 0) {
1067 puts ("ramdisk - allocation error\n");
1070 show_boot_progress (12);
1072 *initrd_end = *initrd_start + rd_len;
1073 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1074 *initrd_start, *initrd_end);
1076 memmove_wd ((void *)*initrd_start,
1077 (void *)rd_data, rd_len, CHUNKSZ);
1085 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1086 *initrd_start, *initrd_end);
1095 * boot_get_cmdline - allocate and initialize kernel cmdline
1096 * @lmb: pointer to lmb handle, will be used for memory mgmt
1097 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1098 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1099 * @bootmap_base: ulong variable, holds offset in physical memory to
1102 * boot_get_cmdline() allocates space for kernel command line below
1103 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1104 * variable is present its contents is copied to allocated kernel
1111 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1117 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1118 CFG_BOOTMAPSZ + bootmap_base);
1120 if (cmdline == NULL)
1123 if ((s = getenv("bootargs")) == NULL)
1128 *cmd_start = (ulong) & cmdline[0];
1129 *cmd_end = *cmd_start + strlen(cmdline);
1131 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1137 * boot_get_kbd - allocate and initialize kernel copy of board info
1138 * @lmb: pointer to lmb handle, will be used for memory mgmt
1139 * @kbd: double pointer to board info data
1140 * @bootmap_base: ulong variable, holds offset in physical memory to
1143 * boot_get_kbd() allocates space for kernel copy of board info data below
1144 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1145 * the current u-boot board info data.
1151 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1153 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1154 CFG_BOOTMAPSZ + bootmap_base);
1160 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1162 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1163 do_bdinfo(NULL, 0, 0, NULL);
1168 #endif /* CONFIG_PPC || CONFIG_M68K */
1169 #endif /* !USE_HOSTCC */
1171 #if defined(CONFIG_FIT)
1172 /*****************************************************************************/
1173 /* New uImage format routines */
1174 /*****************************************************************************/
1176 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1177 ulong *addr, const char **name)
1184 sep = strchr (spec, sepc);
1187 *addr = simple_strtoul (spec, NULL, 16);
1197 * fit_parse_conf - parse FIT configuration spec
1198 * @spec: input string, containing configuration spec
1199 * @add_curr: current image address (to be used as a possible default)
1200 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1202 * @conf_name double pointer to a char, will hold pointer to a configuration
1205 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1206 * where <addr> is a FIT image address that contains configuration
1207 * with a <conf> unit name.
1209 * Address part is optional, and if omitted default add_curr will
1213 * 1 if spec is a valid configuration string,
1214 * addr and conf_name are set accordingly
1217 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1218 ulong *addr, const char **conf_name)
1220 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1224 * fit_parse_subimage - parse FIT subimage spec
1225 * @spec: input string, containing subimage spec
1226 * @add_curr: current image address (to be used as a possible default)
1227 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1229 * @image_name: double pointer to a char, will hold pointer to a subimage name
1231 * fit_parse_subimage() expects subimage spec in the for of
1232 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1233 * subimage with a <subimg> unit name.
1235 * Address part is optional, and if omitted default add_curr will
1239 * 1 if spec is a valid subimage string,
1240 * addr and image_name are set accordingly
1243 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1244 ulong *addr, const char **image_name)
1246 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1248 #endif /* !USE_HOSTCC */
1250 static void fit_get_debug (const void *fit, int noffset,
1251 char *prop_name, int err)
1253 debug ("Can't get '%s' property from FIT 0x%08lx, "
1254 "node: offset %d, name %s (%s)\n",
1255 prop_name, (ulong)fit, noffset,
1256 fit_get_name (fit, noffset, NULL),
1257 fdt_strerror (err));
1261 * fit_print_contents - prints out the contents of the FIT format image
1262 * @fit: pointer to the FIT format image header
1263 * @p: pointer to prefix string
1265 * fit_print_contents() formats a multi line FIT image contents description.
1266 * The routine prints out FIT image properties (root node level) follwed by
1267 * the details of each component image.
1270 * no returned results
1272 void fit_print_contents (const void *fit)
1283 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1293 /* Root node properties */
1294 ret = fit_get_desc (fit, 0, &desc);
1295 printf ("%sFIT description: ", p);
1297 printf ("unavailable\n");
1299 printf ("%s\n", desc);
1301 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1302 ret = fit_get_timestamp (fit, 0, ×tamp);
1303 printf ("%sCreated: ", p);
1305 printf ("unavailable\n");
1307 genimg_print_time (timestamp);
1310 /* Find images parent node offset */
1311 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1312 if (images_noffset < 0) {
1313 printf ("Can't find images parent node '%s' (%s)\n",
1314 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1318 /* Process its subnodes, print out component images details */
1319 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1320 (noffset >= 0) && (ndepth > 0);
1321 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1324 * Direct child node of the images parent node,
1325 * i.e. component image node.
1327 printf ("%s Image %u (%s)\n", p, count++,
1328 fit_get_name(fit, noffset, NULL));
1330 fit_image_print (fit, noffset, p);
1334 /* Find configurations parent node offset */
1335 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1336 if (confs_noffset < 0) {
1337 debug ("Can't get configurations parent node '%s' (%s)\n",
1338 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1342 /* get default configuration unit name from default property */
1343 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1345 printf ("%s Default Configuration: '%s'\n", p, uname);
1347 /* Process its subnodes, print out configurations details */
1348 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1349 (noffset >= 0) && (ndepth > 0);
1350 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1353 * Direct child node of the configurations parent node,
1354 * i.e. configuration node.
1356 printf ("%s Configuration %u (%s)\n", p, count++,
1357 fit_get_name(fit, noffset, NULL));
1359 fit_conf_print (fit, noffset, p);
1365 * fit_image_print - prints out the FIT component image details
1366 * @fit: pointer to the FIT format image header
1367 * @image_noffset: offset of the component image node
1368 * @p: pointer to prefix string
1370 * fit_image_print() lists all mandatory properies for the processed component
1371 * image. If present, hash nodes are printed out as well.
1374 * no returned results
1376 void fit_image_print (const void *fit, int image_noffset, const char *p)
1379 uint8_t type, arch, os, comp;
1387 /* Mandatory properties */
1388 ret = fit_get_desc (fit, image_noffset, &desc);
1389 printf ("%s Description: ", p);
1391 printf ("unavailable\n");
1393 printf ("%s\n", desc);
1395 fit_image_get_type (fit, image_noffset, &type);
1396 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1398 fit_image_get_comp (fit, image_noffset, &comp);
1399 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1401 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1404 printf ("%s Data Start: ", p);
1406 printf ("unavailable\n");
1408 printf ("0x%08lx\n", (ulong)data);
1411 printf ("%s Data Size: ", p);
1413 printf ("unavailable\n");
1415 genimg_print_size (size);
1417 /* Remaining, type dependent properties */
1418 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1419 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1420 (type == IH_TYPE_FLATDT)) {
1421 fit_image_get_arch (fit, image_noffset, &arch);
1422 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1425 if (type == IH_TYPE_KERNEL) {
1426 fit_image_get_os (fit, image_noffset, &os);
1427 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1430 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1431 ret = fit_image_get_load (fit, image_noffset, &load);
1432 printf ("%s Load Address: ", p);
1434 printf ("unavailable\n");
1436 printf ("0x%08lx\n", load);
1438 fit_image_get_entry (fit, image_noffset, &entry);
1439 printf ("%s Entry Point: ", p);
1441 printf ("unavailable\n");
1443 printf ("0x%08lx\n", entry);
1446 /* Process all hash subnodes of the component image node */
1447 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1448 (noffset >= 0) && (ndepth > 0);
1449 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1451 /* Direct child node of the component image node */
1452 fit_image_print_hash (fit, noffset, p);
1458 * fit_image_print_hash - prints out the hash node details
1459 * @fit: pointer to the FIT format image header
1460 * @noffset: offset of the hash node
1461 * @p: pointer to prefix string
1463 * fit_image_print_hash() lists properies for the processed hash node
1466 * no returned results
1468 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1476 * Check subnode name, must be equal to "hash".
1477 * Multiple hash nodes require unique unit node
1478 * names, e.g. hash@1, hash@2, etc.
1480 if (strncmp (fit_get_name(fit, noffset, NULL),
1482 strlen(FIT_HASH_NODENAME)) != 0)
1485 debug ("%s Hash node: '%s'\n", p,
1486 fit_get_name (fit, noffset, NULL));
1488 printf ("%s Hash algo: ", p);
1489 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1490 printf ("invalid/unsupported\n");
1493 printf ("%s\n", algo);
1495 ret = fit_image_hash_get_value (fit, noffset, &value,
1497 printf ("%s Hash value: ", p);
1499 printf ("unavailable\n");
1501 for (i = 0; i < value_len; i++)
1502 printf ("%02x", value[i]);
1506 debug ("%s Hash len: %d\n", p, value_len);
1510 * fit_get_desc - get node description property
1511 * @fit: pointer to the FIT format image header
1512 * @noffset: node offset
1513 * @desc: double pointer to the char, will hold pointer to the descrption
1515 * fit_get_desc() reads description property from a given node, if
1516 * description is found pointer to it is returened in third call argument.
1522 int fit_get_desc (const void *fit, int noffset, char **desc)
1526 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1527 if (*desc == NULL) {
1528 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1536 * fit_get_timestamp - get node timestamp property
1537 * @fit: pointer to the FIT format image header
1538 * @noffset: node offset
1539 * @timestamp: pointer to the time_t, will hold read timestamp
1541 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1542 * is found and has a correct size its value is retured in third call
1547 * -1, on property read failure
1548 * -2, on wrong timestamp size
1550 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1555 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1557 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1560 if (len != sizeof (uint32_t)) {
1561 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1565 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1570 * fit_image_get_node - get node offset for component image of a given unit name
1571 * @fit: pointer to the FIT format image header
1572 * @image_uname: component image node unit name
1574 * fit_image_get_node() finds a component image (withing the '/images'
1575 * node) of a provided unit name. If image is found its node offset is
1576 * returned to the caller.
1579 * image node offset when found (>=0)
1580 * negative number on failure (FDT_ERR_* code)
1582 int fit_image_get_node (const void *fit, const char *image_uname)
1584 int noffset, images_noffset;
1586 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1587 if (images_noffset < 0) {
1588 debug ("Can't find images parent node '%s' (%s)\n",
1589 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1590 return images_noffset;
1593 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1595 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1596 image_uname, fdt_strerror (noffset));
1603 * fit_image_get_os - get os id for a given component image node
1604 * @fit: pointer to the FIT format image header
1605 * @noffset: component image node offset
1606 * @os: pointer to the uint8_t, will hold os numeric id
1608 * fit_image_get_os() finds os property in a given component image node.
1609 * If the property is found, its (string) value is translated to the numeric
1610 * id which is returned to the caller.
1616 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1621 /* Get OS name from property data */
1622 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1624 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1629 /* Translate OS name to id */
1630 *os = genimg_get_os_id (data);
1635 * fit_image_get_arch - get arch id for a given component image node
1636 * @fit: pointer to the FIT format image header
1637 * @noffset: component image node offset
1638 * @arch: pointer to the uint8_t, will hold arch numeric id
1640 * fit_image_get_arch() finds arch property in a given component image node.
1641 * If the property is found, its (string) value is translated to the numeric
1642 * id which is returned to the caller.
1648 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1653 /* Get architecture name from property data */
1654 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1656 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1661 /* Translate architecture name to id */
1662 *arch = genimg_get_arch_id (data);
1667 * fit_image_get_type - get type id for a given component image node
1668 * @fit: pointer to the FIT format image header
1669 * @noffset: component image node offset
1670 * @type: pointer to the uint8_t, will hold type numeric id
1672 * fit_image_get_type() finds type property in a given component image node.
1673 * If the property is found, its (string) value is translated to the numeric
1674 * id which is returned to the caller.
1680 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1685 /* Get image type name from property data */
1686 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1688 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1693 /* Translate image type name to id */
1694 *type = genimg_get_type_id (data);
1699 * fit_image_get_comp - get comp id for a given component image node
1700 * @fit: pointer to the FIT format image header
1701 * @noffset: component image node offset
1702 * @comp: pointer to the uint8_t, will hold comp numeric id
1704 * fit_image_get_comp() finds comp property in a given component image node.
1705 * If the property is found, its (string) value is translated to the numeric
1706 * id which is returned to the caller.
1712 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1717 /* Get compression name from property data */
1718 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1720 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1725 /* Translate compression name to id */
1726 *comp = genimg_get_comp_id (data);
1731 * fit_image_get_load - get load address property for a given component image node
1732 * @fit: pointer to the FIT format image header
1733 * @noffset: component image node offset
1734 * @load: pointer to the uint32_t, will hold load address
1736 * fit_image_get_load() finds load address property in a given component image node.
1737 * If the property is found, its value is returned to the caller.
1743 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1746 const uint32_t *data;
1748 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1750 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1754 *load = uimage_to_cpu (*data);
1759 * fit_image_get_entry - get entry point address property for a given component image node
1760 * @fit: pointer to the FIT format image header
1761 * @noffset: component image node offset
1762 * @entry: pointer to the uint32_t, will hold entry point address
1764 * fit_image_get_entry() finds entry point address property in a given component image node.
1765 * If the property is found, its value is returned to the caller.
1771 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1774 const uint32_t *data;
1776 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1778 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1782 *entry = uimage_to_cpu (*data);
1787 * fit_image_get_data - get data property and its size for a given component image node
1788 * @fit: pointer to the FIT format image header
1789 * @noffset: component image node offset
1790 * @data: double pointer to void, will hold data property's data address
1791 * @size: pointer to size_t, will hold data property's data size
1793 * fit_image_get_data() finds data property in a given component image node.
1794 * If the property is found its data start address and size are returned to
1801 int fit_image_get_data (const void *fit, int noffset,
1802 const void **data, size_t *size)
1806 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1807 if (*data == NULL) {
1808 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1818 * fit_image_hash_get_algo - get hash algorithm name
1819 * @fit: pointer to the FIT format image header
1820 * @noffset: hash node offset
1821 * @algo: double pointer to char, will hold pointer to the algorithm name
1823 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1824 * If the property is found its data start address is returned to the caller.
1830 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1834 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1835 if (*algo == NULL) {
1836 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1844 * fit_image_hash_get_value - get hash value and length
1845 * @fit: pointer to the FIT format image header
1846 * @noffset: hash node offset
1847 * @value: double pointer to uint8_t, will hold address of a hash value data
1848 * @value_len: pointer to an int, will hold hash data length
1850 * fit_image_hash_get_value() finds hash value property in a given hash node.
1851 * If the property is found its data start address and size are returned to
1858 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1863 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1864 if (*value == NULL) {
1865 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1875 * fit_set_timestamp - set node timestamp property
1876 * @fit: pointer to the FIT format image header
1877 * @noffset: node offset
1878 * @timestamp: timestamp value to be set
1880 * fit_set_timestamp() attempts to set timestamp property in the requested
1881 * node and returns operation status to the caller.
1885 * -1, on property read failure
1887 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1892 t = cpu_to_uimage (timestamp);
1893 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1896 printf ("Can't set '%s' property for '%s' node (%s)\n",
1897 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1898 fdt_strerror (ret));
1906 * calculate_hash - calculate and return hash for provided input data
1907 * @data: pointer to the input data
1908 * @data_len: data length
1909 * @algo: requested hash algorithm
1910 * @value: pointer to the char, will hold hash value data (caller must
1911 * allocate enough free space)
1912 * value_len: length of the calculated hash
1914 * calculate_hash() computes input data hash according to the requested algorithm.
1915 * Resulting hash value is placed in caller provided 'value' buffer, length
1916 * of the calculated hash is returned via value_len pointer argument.
1920 * -1, when algo is unsupported
1922 static int calculate_hash (const void *data, int data_len, const char *algo,
1923 uint8_t *value, int *value_len)
1925 if (strcmp (algo, "crc32") == 0 ) {
1926 *((uint32_t *)value) = crc32 (0, data, data_len);
1927 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1929 } else if (strcmp (algo, "sha1") == 0 ) {
1930 sha1_csum ((unsigned char *) data, data_len,
1931 (unsigned char *) value);
1933 } else if (strcmp (algo, "md5") == 0 ) {
1934 md5 ((unsigned char *)data, data_len, value);
1937 debug ("Unsupported hash alogrithm\n");
1945 * fit_set_hashes - process FIT component image nodes and calculate hashes
1946 * @fit: pointer to the FIT format image header
1948 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1949 * Hashes are calculated for all component images which have hash subnodes
1950 * with algorithm property set to one of the supported hash algorithms.
1954 * libfdt error code, on failure
1956 int fit_set_hashes (void *fit)
1963 /* Find images parent node offset */
1964 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1965 if (images_noffset < 0) {
1966 printf ("Can't find images parent node '%s' (%s)\n",
1967 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1968 return images_noffset;
1971 /* Process its subnodes, print out component images details */
1972 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1973 (noffset >= 0) && (ndepth > 0);
1974 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1977 * Direct child node of the images parent node,
1978 * i.e. component image node.
1980 ret = fit_image_set_hashes (fit, noffset);
1990 * fit_image_set_hashes - calculate/set hashes for given component image node
1991 * @fit: pointer to the FIT format image header
1992 * @image_noffset: requested component image node
1994 * fit_image_set_hashes() adds hash values for an component image node. All
1995 * existing hash subnodes are checked, if algorithm property is set to one of
1996 * the supported hash algorithms, hash value is computed and corresponding
1997 * hash node property is set, for example:
1999 * Input component image node structure:
2001 * o image@1 (at image_noffset)
2002 * | - data = [binary data]
2006 * Output component image node structure:
2008 * o image@1 (at image_noffset)
2009 * | - data = [binary data]
2012 * |- value = sha1(data)
2018 int fit_image_set_hashes (void *fit, int image_noffset)
2023 uint8_t value[FIT_MAX_HASH_LEN];
2028 /* Get image data and data length */
2029 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2030 printf ("Can't get image data/size\n");
2034 /* Process all hash subnodes of the component image node */
2035 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2036 (noffset >= 0) && (ndepth > 0);
2037 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2039 /* Direct child node of the component image node */
2042 * Check subnode name, must be equal to "hash".
2043 * Multiple hash nodes require unique unit node
2044 * names, e.g. hash@1, hash@2, etc.
2046 if (strncmp (fit_get_name(fit, noffset, NULL),
2048 strlen(FIT_HASH_NODENAME)) != 0) {
2049 /* Not a hash subnode, skip it */
2053 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2054 printf ("Can't get hash algo property for "
2055 "'%s' hash node in '%s' image node\n",
2056 fit_get_name (fit, noffset, NULL),
2057 fit_get_name (fit, image_noffset, NULL));
2061 if (calculate_hash (data, size, algo, value, &value_len)) {
2062 printf ("Unsupported hash algorithm (%s) for "
2063 "'%s' hash node in '%s' image node\n",
2064 algo, fit_get_name (fit, noffset, NULL),
2065 fit_get_name (fit, image_noffset, NULL));
2069 if (fit_image_hash_set_value (fit, noffset, value,
2071 printf ("Can't set hash value for "
2072 "'%s' hash node in '%s' image node\n",
2073 fit_get_name (fit, noffset, NULL),
2074 fit_get_name (fit, image_noffset, NULL));
2084 * fit_image_hash_set_value - set hash value in requested has node
2085 * @fit: pointer to the FIT format image header
2086 * @noffset: hash node offset
2087 * @value: hash value to be set
2088 * @value_len: hash value length
2090 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2091 * given and returns operation status to the caller.
2097 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2102 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2104 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2105 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2106 fdt_strerror (ret));
2112 #endif /* USE_HOSTCC */
2115 * fit_image_check_hashes - verify data intergity
2116 * @fit: pointer to the FIT format image header
2117 * @image_noffset: component image node offset
2119 * fit_image_check_hashes() goes over component image hash nodes,
2120 * re-calculates each data hash and compares with the value stored in hash
2124 * 1, if all hashes are valid
2125 * 0, otherwise (or on error)
2127 int fit_image_check_hashes (const void *fit, int image_noffset)
2134 uint8_t value[FIT_MAX_HASH_LEN];
2140 /* Get image data and data length */
2141 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2142 printf ("Can't get image data/size\n");
2146 /* Process all hash subnodes of the component image node */
2147 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2148 (noffset >= 0) && (ndepth > 0);
2149 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2151 /* Direct child node of the component image node */
2154 * Check subnode name, must be equal to "hash".
2155 * Multiple hash nodes require unique unit node
2156 * names, e.g. hash@1, hash@2, etc.
2158 if (strncmp (fit_get_name(fit, noffset, NULL),
2160 strlen(FIT_HASH_NODENAME)) != 0)
2163 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2164 err_msg = "Can't get hash algo property";
2167 printf ("%s", algo);
2169 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2171 err_msg = "Can't get hash value property";
2175 if (calculate_hash (data, size, algo, value, &value_len)) {
2176 err_msg = "Unsupported hash algorithm";
2180 if (value_len != fit_value_len) {
2181 err_msg = "Bad hash value len";
2183 } else if (memcmp (value, fit_value, value_len) != 0) {
2184 err_msg = "Bad hash value";
2194 printf ("%s for '%s' hash node in '%s' image node\n",
2195 err_msg, fit_get_name (fit, noffset, NULL),
2196 fit_get_name (fit, image_noffset, NULL));
2201 * fit_image_check_os - check whether image node is of a given os type
2202 * @fit: pointer to the FIT format image header
2203 * @noffset: component image node offset
2204 * @os: requested image os
2206 * fit_image_check_os() reads image os property and compares its numeric
2207 * id with the requested os. Comparison result is returned to the caller.
2210 * 1 if image is of given os type
2211 * 0 otherwise (or on error)
2213 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2217 if (fit_image_get_os (fit, noffset, &image_os))
2219 return (os == image_os);
2223 * fit_image_check_arch - check whether image node is of a given arch
2224 * @fit: pointer to the FIT format image header
2225 * @noffset: component image node offset
2226 * @arch: requested imagearch
2228 * fit_image_check_arch() reads image arch property and compares its numeric
2229 * id with the requested arch. Comparison result is returned to the caller.
2232 * 1 if image is of given arch
2233 * 0 otherwise (or on error)
2235 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2239 if (fit_image_get_arch (fit, noffset, &image_arch))
2241 return (arch == image_arch);
2245 * fit_image_check_type - check whether image node is of a given type
2246 * @fit: pointer to the FIT format image header
2247 * @noffset: component image node offset
2248 * @type: requested image type
2250 * fit_image_check_type() reads image type property and compares its numeric
2251 * id with the requested type. Comparison result is returned to the caller.
2254 * 1 if image is of given type
2255 * 0 otherwise (or on error)
2257 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2261 if (fit_image_get_type (fit, noffset, &image_type))
2263 return (type == image_type);
2267 * fit_image_check_comp - check whether image node uses given compression
2268 * @fit: pointer to the FIT format image header
2269 * @noffset: component image node offset
2270 * @comp: requested image compression type
2272 * fit_image_check_comp() reads image compression property and compares its
2273 * numeric id with the requested compression type. Comparison result is
2274 * returned to the caller.
2277 * 1 if image uses requested compression
2278 * 0 otherwise (or on error)
2280 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2284 if (fit_image_get_comp (fit, noffset, &image_comp))
2286 return (comp == image_comp);
2290 * fit_check_format - sanity check FIT image format
2291 * @fit: pointer to the FIT format image header
2293 * fit_check_format() runs a basic sanity FIT image verification.
2294 * Routine checks for mandatory properties, nodes, etc.
2300 int fit_check_format (const void *fit)
2302 /* mandatory / node 'description' property */
2303 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2304 debug ("Wrong FIT format: no description\n");
2308 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2309 /* mandatory / node 'timestamp' property */
2310 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2311 debug ("Wrong FIT format: no description\n");
2316 /* mandatory subimages parent '/images' node */
2317 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2318 debug ("Wrong FIT format: no images parent node\n");
2326 * fit_conf_get_node - get node offset for configuration of a given unit name
2327 * @fit: pointer to the FIT format image header
2328 * @conf_uname: configuration node unit name
2330 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2331 * parant node) of a provided unit name. If configuration is found its node offset
2332 * is returned to the caller.
2334 * When NULL is provided in second argument fit_conf_get_node() will search
2335 * for a default configuration node instead. Default configuration node unit name
2336 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2339 * configuration node offset when found (>=0)
2340 * negative number on failure (FDT_ERR_* code)
2342 int fit_conf_get_node (const void *fit, const char *conf_uname)
2344 int noffset, confs_noffset;
2347 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2348 if (confs_noffset < 0) {
2349 debug ("Can't find configurations parent node '%s' (%s)\n",
2350 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2351 return confs_noffset;
2354 if (conf_uname == NULL) {
2355 /* get configuration unit name from the default property */
2356 debug ("No configuration specified, trying default...\n");
2357 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2358 if (conf_uname == NULL) {
2359 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2362 debug ("Found default configuration: '%s'\n", conf_uname);
2365 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2367 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2368 conf_uname, fdt_strerror (noffset));
2374 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2375 const char *prop_name)
2380 /* get kernel image unit name from configuration kernel property */
2381 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2385 return fit_image_get_node (fit, uname);
2389 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2390 * a given configuration
2391 * @fit: pointer to the FIT format image header
2392 * @noffset: configuration node offset
2394 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2395 * configuration FIT_KERNEL_PROP property and translates it to the node
2399 * image node offset when found (>=0)
2400 * negative number on failure (FDT_ERR_* code)
2402 int fit_conf_get_kernel_node (const void *fit, int noffset)
2404 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2408 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2409 * a given configuration
2410 * @fit: pointer to the FIT format image header
2411 * @noffset: configuration node offset
2413 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2414 * configuration FIT_KERNEL_PROP property and translates it to the node
2418 * image node offset when found (>=0)
2419 * negative number on failure (FDT_ERR_* code)
2421 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2423 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2427 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2428 * a given configuration
2429 * @fit: pointer to the FIT format image header
2430 * @noffset: configuration node offset
2432 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2433 * configuration FIT_KERNEL_PROP property and translates it to the node
2437 * image node offset when found (>=0)
2438 * negative number on failure (FDT_ERR_* code)
2440 int fit_conf_get_fdt_node (const void *fit, int noffset)
2442 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2446 * fit_conf_print - prints out the FIT configuration details
2447 * @fit: pointer to the FIT format image header
2448 * @noffset: offset of the configuration node
2449 * @p: pointer to prefix string
2451 * fit_conf_print() lists all mandatory properies for the processed
2452 * configuration node.
2455 * no returned results
2457 void fit_conf_print (const void *fit, int noffset, const char *p)
2463 /* Mandatory properties */
2464 ret = fit_get_desc (fit, noffset, &desc);
2465 printf ("%s Description: ", p);
2467 printf ("unavailable\n");
2469 printf ("%s\n", desc);
2471 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2472 printf ("%s Kernel: ", p);
2474 printf ("unavailable\n");
2476 printf ("%s\n", uname);
2478 /* Optional properties */
2479 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2481 printf ("%s Init Ramdisk: %s\n", p, uname);
2483 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2485 printf ("%s FDT: %s\n", p, uname);
2489 * fit_check_ramdisk - verify FIT format ramdisk subimage
2490 * @fit_hdr: pointer to the FIT ramdisk header
2491 * @rd_noffset: ramdisk subimage node offset within FIT image
2492 * @arch: requested ramdisk image architecture type
2493 * @verify: data CRC verification flag
2495 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2496 * specified FIT image.
2503 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2505 fit_image_print (fit, rd_noffset, " ");
2508 puts (" Verifying Hash Integrity ... ");
2509 if (!fit_image_check_hashes (fit, rd_noffset)) {
2510 puts ("Bad Data Hash\n");
2511 show_boot_progress (-125);
2517 show_boot_progress (126);
2518 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2519 !fit_image_check_arch (fit, rd_noffset, arch) ||
2520 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2521 printf ("No Linux %s Ramdisk Image\n",
2522 genimg_get_arch_name(arch));
2523 show_boot_progress (-126);
2527 show_boot_progress (127);
2530 #endif /* USE_HOSTCC */
2531 #endif /* CONFIG_FIT */