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,
30 #ifdef CONFIG_SHOW_BOOT_PROGRESS
31 #include <status_led.h>
34 #ifdef CONFIG_HAS_DATAFLASH
35 #include <dataflash.h>
38 #ifdef CONFIG_LOGBUFFER
42 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
48 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
51 #include <fdt_support.h>
54 #if defined(CONFIG_FIT)
55 #include <u-boot/md5.h>
58 static int fit_check_ramdisk (const void *fit, int os_noffset,
59 uint8_t arch, int verify);
63 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
66 DECLARE_GLOBAL_DATA_PTR;
68 static const image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
72 #include <u-boot/md5.h>
75 #endif /* !USE_HOSTCC*/
77 static const table_entry_t uimage_arch[] = {
78 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
79 { IH_ARCH_ALPHA, "alpha", "Alpha", },
80 { IH_ARCH_ARM, "arm", "ARM", },
81 { IH_ARCH_I386, "x86", "Intel x86", },
82 { IH_ARCH_IA64, "ia64", "IA64", },
83 { IH_ARCH_M68K, "m68k", "M68K", },
84 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
85 { IH_ARCH_MIPS, "mips", "MIPS", },
86 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
87 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
88 { IH_ARCH_PPC, "powerpc", "PowerPC", },
89 { IH_ARCH_PPC, "ppc", "PowerPC", },
90 { IH_ARCH_S390, "s390", "IBM S390", },
91 { IH_ARCH_SH, "sh", "SuperH", },
92 { IH_ARCH_SPARC, "sparc", "SPARC", },
93 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
94 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
95 { IH_ARCH_AVR32, "avr32", "AVR32", },
99 static const table_entry_t uimage_os[] = {
100 { IH_OS_INVALID, NULL, "Invalid OS", },
101 { IH_OS_LINUX, "linux", "Linux", },
102 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
103 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
105 { IH_OS_NETBSD, "netbsd", "NetBSD", },
106 { IH_OS_OSE, "ose", "Enea OSE", },
107 { IH_OS_RTEMS, "rtems", "RTEMS", },
108 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
109 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
110 { IH_OS_QNX, "qnx", "QNX", },
111 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
113 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
114 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
117 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
118 { IH_OS_DELL, "dell", "Dell", },
119 { IH_OS_ESIX, "esix", "Esix", },
120 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
121 { IH_OS_IRIX, "irix", "Irix", },
122 { IH_OS_NCR, "ncr", "NCR", },
123 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
124 { IH_OS_PSOS, "psos", "pSOS", },
125 { IH_OS_SCO, "sco", "SCO", },
126 { IH_OS_SOLARIS, "solaris", "Solaris", },
127 { IH_OS_SVR4, "svr4", "SVR4", },
132 static const table_entry_t uimage_type[] = {
133 { IH_TYPE_INVALID, NULL, "Invalid Image", },
134 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
135 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
136 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
137 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
138 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
139 { IH_TYPE_SCRIPT, "script", "Script", },
140 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
141 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
142 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
143 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
144 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
148 static const table_entry_t uimage_comp[] = {
149 { IH_COMP_NONE, "none", "uncompressed", },
150 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
151 { IH_COMP_GZIP, "gzip", "gzip compressed", },
152 { IH_COMP_LZMA, "lzma", "lzma compressed", },
153 { IH_COMP_LZO, "lzo", "lzo compressed", },
157 uint32_t crc32 (uint32_t, const unsigned char *, uint);
158 uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
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 (const 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 (const image_header_t *hdr)
183 ulong data = image_get_data (hdr);
184 ulong len = image_get_data_size (hdr);
185 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
187 return (dcrc == image_get_dcrc (hdr));
191 * image_multi_count - get component (sub-image) count
192 * @hdr: pointer to the header of the multi component image
194 * image_multi_count() returns number of components in a multi
197 * Note: no checking of the image type is done, caller must pass
198 * a valid multi component image.
201 * number of components
203 ulong image_multi_count (const image_header_t *hdr)
208 /* get start of the image payload, which in case of multi
209 * component images that points to a table of component sizes */
210 size = (uint32_t *)image_get_data (hdr);
212 /* count non empty slots */
213 for (i = 0; size[i]; ++i)
220 * image_multi_getimg - get component data address and size
221 * @hdr: pointer to the header of the multi component image
222 * @idx: index of the requested component
223 * @data: pointer to a ulong variable, will hold component data address
224 * @len: pointer to a ulong variable, will hold component size
226 * image_multi_getimg() returns size and data address for the requested
227 * component in a multi component image.
229 * Note: no checking of the image type is done, caller must pass
230 * a valid multi component image.
233 * data address and size of the component, if idx is valid
234 * 0 in data and len, if idx is out of range
236 void image_multi_getimg (const image_header_t *hdr, ulong idx,
237 ulong *data, ulong *len)
241 ulong offset, count, img_data;
243 /* get number of component */
244 count = image_multi_count (hdr);
246 /* get start of the image payload, which in case of multi
247 * component images that points to a table of component sizes */
248 size = (uint32_t *)image_get_data (hdr);
250 /* get address of the proper component data start, which means
251 * skipping sizes table (add 1 for last, null entry) */
252 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
255 *len = uimage_to_cpu (size[idx]);
258 /* go over all indices preceding requested component idx */
259 for (i = 0; i < idx; i++) {
260 /* add up i-th component size, rounding up to 4 bytes */
261 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
264 /* calculate idx-th component data address */
265 *data = img_data + offset;
272 static void image_print_type (const image_header_t *hdr)
274 const char *os, *arch, *type, *comp;
276 os = genimg_get_os_name (image_get_os (hdr));
277 arch = genimg_get_arch_name (image_get_arch (hdr));
278 type = genimg_get_type_name (image_get_type (hdr));
279 comp = genimg_get_comp_name (image_get_comp (hdr));
281 printf ("%s %s %s (%s)\n", arch, os, type, comp);
285 * image_print_contents - prints out the contents of the legacy format image
286 * @ptr: pointer to the legacy format image header
287 * @p: pointer to prefix string
289 * image_print_contents() formats a multi line legacy image contents description.
290 * The routine prints out all header fields followed by the size/offset data
291 * for MULTI/SCRIPT images.
294 * no returned results
296 void image_print_contents (const void *ptr)
298 const image_header_t *hdr = (const image_header_t *)ptr;
307 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
308 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
309 printf ("%sCreated: ", p);
310 genimg_print_time ((time_t)image_get_time (hdr));
312 printf ("%sImage Type: ", p);
313 image_print_type (hdr);
314 printf ("%sData Size: ", p);
315 genimg_print_size (image_get_data_size (hdr));
316 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
317 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
319 if (image_check_type (hdr, IH_TYPE_MULTI) ||
320 image_check_type (hdr, IH_TYPE_SCRIPT)) {
323 ulong count = image_multi_count (hdr);
325 printf ("%sContents:\n", p);
326 for (i = 0; i < count; i++) {
327 image_multi_getimg (hdr, i, &data, &len);
329 printf ("%s Image %d: ", p, i);
330 genimg_print_size (len);
332 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
334 * the user may need to know offsets
335 * if planning to do something with
338 printf ("%s Offset = 0x%08lx\n", p, data);
347 * image_get_ramdisk - get and verify ramdisk image
348 * @rd_addr: ramdisk image start address
349 * @arch: expected ramdisk architecture
350 * @verify: checksum verification flag
352 * image_get_ramdisk() returns a pointer to the verified ramdisk image
353 * header. Routine receives image start address and expected architecture
354 * flag. Verification done covers data and header integrity and os/type/arch
357 * If dataflash support is enabled routine checks for dataflash addresses
358 * and handles required dataflash reads.
361 * pointer to a ramdisk image header, if image was found and valid
362 * otherwise, return NULL
364 static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
367 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
369 if (!image_check_magic (rd_hdr)) {
370 puts ("Bad Magic Number\n");
371 show_boot_progress (-10);
375 if (!image_check_hcrc (rd_hdr)) {
376 puts ("Bad Header Checksum\n");
377 show_boot_progress (-11);
381 show_boot_progress (10);
382 image_print_contents (rd_hdr);
385 puts(" Verifying Checksum ... ");
386 if (!image_check_dcrc (rd_hdr)) {
387 puts ("Bad Data CRC\n");
388 show_boot_progress (-12);
394 show_boot_progress (11);
396 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
397 !image_check_arch (rd_hdr, arch) ||
398 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
399 printf ("No Linux %s Ramdisk Image\n",
400 genimg_get_arch_name(arch));
401 show_boot_progress (-13);
407 #endif /* !USE_HOSTCC */
409 /*****************************************************************************/
410 /* Shared dual-format routines */
411 /*****************************************************************************/
413 int getenv_yesno (char *var)
415 char *s = getenv (var);
416 return (s && (*s == 'n')) ? 0 : 1;
419 ulong getenv_bootm_low(void)
421 char *s = getenv ("bootm_low");
423 ulong tmp = simple_strtoul (s, NULL, 16);
427 #if defined(CONFIG_SYS_SDRAM_BASE)
428 return CONFIG_SYS_SDRAM_BASE;
429 #elif defined(CONFIG_ARM)
430 return gd->bd->bi_dram[0].start;
436 phys_size_t getenv_bootm_size(void)
439 char *s = getenv ("bootm_size");
441 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
444 s = getenv("bootm_low");
446 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
451 #if defined(CONFIG_ARM)
452 return gd->bd->bi_dram[0].size - tmp;
454 return gd->bd->bi_memsize - tmp;
458 phys_size_t getenv_bootm_mapsize(void)
461 char *s = getenv ("bootm_mapsize");
463 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
467 #if defined(CONFIG_SYS_BOOTMAPSZ)
468 return CONFIG_SYS_BOOTMAPSZ;
470 return getenv_bootm_size();
474 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
479 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
481 size_t tail = (len > chunksz) ? chunksz : len;
483 memmove (to, from, tail);
488 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
489 memmove (to, from, len);
490 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
492 #endif /* !USE_HOSTCC */
494 void genimg_print_size (uint32_t size)
497 printf ("%d Bytes = ", size);
498 print_size (size, "\n");
500 printf ("%d Bytes = %.2f kB = %.2f MB\n",
501 size, (double)size / 1.024e3,
502 (double)size / 1.048576e6);
506 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
507 static void genimg_print_time (time_t timestamp)
512 to_tm (timestamp, &tm);
513 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
514 tm.tm_year, tm.tm_mon, tm.tm_mday,
515 tm.tm_hour, tm.tm_min, tm.tm_sec);
517 printf ("%s", ctime(×tamp));
520 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
523 * get_table_entry_name - translate entry id to long name
524 * @table: pointer to a translation table for entries of a specific type
525 * @msg: message to be returned when translation fails
526 * @id: entry id to be translated
528 * get_table_entry_name() will go over translation table trying to find
529 * entry that matches given id. If matching entry is found, its long
530 * name is returned to the caller.
533 * long entry name if translation succeeds
536 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
538 for (; table->id >= 0; ++table) {
540 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
543 return table->lname + gd->reloc_off;
549 const char *genimg_get_os_name (uint8_t os)
551 return (get_table_entry_name (uimage_os, "Unknown OS", os));
554 const char *genimg_get_arch_name (uint8_t arch)
556 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
559 const char *genimg_get_type_name (uint8_t type)
561 return (get_table_entry_name (uimage_type, "Unknown Image", type));
564 const char *genimg_get_comp_name (uint8_t comp)
566 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
570 * get_table_entry_id - translate short entry name to id
571 * @table: pointer to a translation table for entries of a specific type
572 * @table_name: to be used in case of error
573 * @name: entry short name to be translated
575 * get_table_entry_id() will go over translation table trying to find
576 * entry that matches given short name. If matching entry is found,
577 * its id returned to the caller.
580 * entry id if translation succeeds
583 int get_table_entry_id(const table_entry_t *table,
584 const char *table_name, const char *name)
586 const table_entry_t *t;
590 for (t = table; t->id >= 0; ++t) {
591 if (t->sname && strcasecmp(t->sname, name) == 0)
595 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
596 for (t = table; t->id >= 0; ++t) {
597 if (t->sname == NULL)
599 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
602 fprintf (stderr, "\n");
604 for (t = table; t->id >= 0; ++t) {
605 #ifdef CONFIG_NEEDS_MANUAL_RELOC
606 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
608 if (t->sname && strcmp(t->sname, name) == 0)
612 debug ("Invalid %s Type: %s\n", table_name, name);
613 #endif /* USE_HOSTCC */
617 int genimg_get_os_id (const char *name)
619 return (get_table_entry_id (uimage_os, "OS", name));
622 int genimg_get_arch_id (const char *name)
624 return (get_table_entry_id (uimage_arch, "CPU", name));
627 int genimg_get_type_id (const char *name)
629 return (get_table_entry_id (uimage_type, "Image", name));
632 int genimg_get_comp_id (const char *name)
634 return (get_table_entry_id (uimage_comp, "Compression", name));
639 * genimg_get_format - get image format type
640 * @img_addr: image start address
642 * genimg_get_format() checks whether provided address points to a valid
643 * legacy or FIT image.
645 * New uImage format and FDT blob are based on a libfdt. FDT blob
646 * may be passed directly or embedded in a FIT image. In both situations
647 * genimg_get_format() must be able to dectect libfdt header.
650 * image format type or IMAGE_FORMAT_INVALID if no image is present
652 int genimg_get_format (void *img_addr)
654 ulong format = IMAGE_FORMAT_INVALID;
655 const image_header_t *hdr;
656 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
660 hdr = (const image_header_t *)img_addr;
661 if (image_check_magic(hdr))
662 format = IMAGE_FORMAT_LEGACY;
663 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
665 fit_hdr = (char *)img_addr;
666 if (fdt_check_header (fit_hdr) == 0)
667 format = IMAGE_FORMAT_FIT;
675 * genimg_get_image - get image from special storage (if necessary)
676 * @img_addr: image start address
678 * genimg_get_image() checks if provided image start adddress is located
679 * in a dataflash storage. If so, image is moved to a system RAM memory.
682 * image start address after possible relocation from special storage
684 ulong genimg_get_image (ulong img_addr)
686 ulong ram_addr = img_addr;
688 #ifdef CONFIG_HAS_DATAFLASH
689 ulong h_size, d_size;
691 if (addr_dataflash (img_addr)){
692 /* ger RAM address */
693 ram_addr = CONFIG_SYS_LOAD_ADDR;
695 /* get header size */
696 h_size = image_get_header_size ();
697 #if defined(CONFIG_FIT)
698 if (sizeof(struct fdt_header) > h_size)
699 h_size = sizeof(struct fdt_header);
703 debug (" Reading image header from dataflash address "
704 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
706 read_dataflash (img_addr, h_size, (char *)ram_addr);
709 switch (genimg_get_format ((void *)ram_addr)) {
710 case IMAGE_FORMAT_LEGACY:
711 d_size = image_get_data_size ((const image_header_t *)ram_addr);
712 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
715 #if defined(CONFIG_FIT)
716 case IMAGE_FORMAT_FIT:
717 d_size = fit_get_size ((const void *)ram_addr) - h_size;
718 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
723 printf (" No valid image found at 0x%08lx\n", img_addr);
727 /* read in image data */
728 debug (" Reading image remaining data from dataflash address "
729 "%08lx to RAM address %08lx\n", img_addr + h_size,
732 read_dataflash (img_addr + h_size, d_size,
733 (char *)(ram_addr + h_size));
736 #endif /* CONFIG_HAS_DATAFLASH */
742 * fit_has_config - check if there is a valid FIT configuration
743 * @images: pointer to the bootm command headers structure
745 * fit_has_config() checks if there is a FIT configuration in use
746 * (if FTI support is present).
749 * 0, no FIT support or no configuration found
750 * 1, configuration found
752 int genimg_has_config (bootm_headers_t *images)
754 #if defined(CONFIG_FIT)
755 if (images->fit_uname_cfg)
762 * boot_get_ramdisk - main ramdisk handling routine
763 * @argc: command argument count
764 * @argv: command argument list
765 * @images: pointer to the bootm images structure
766 * @arch: expected ramdisk architecture
767 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
768 * @rd_end: pointer to a ulong variable, will hold ramdisk end
770 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
771 * Curently supported are the following ramdisk sources:
772 * - multicomponent kernel/ramdisk image,
773 * - commandline provided address of decicated ramdisk image.
776 * 0, if ramdisk image was found and valid, or skiped
777 * rd_start and rd_end are set to ramdisk start/end addresses if
778 * ramdisk image is found and valid
780 * 1, if ramdisk image is found but corrupted, or invalid
781 * rd_start and rd_end are set to 0 if no ramdisk exists
783 int boot_get_ramdisk (int argc, char * const argv[], bootm_headers_t *images,
784 uint8_t arch, ulong *rd_start, ulong *rd_end)
786 ulong rd_addr, rd_load;
787 ulong rd_data, rd_len;
788 const image_header_t *rd_hdr;
789 #if defined(CONFIG_FIT)
791 const char *fit_uname_config = NULL;
792 const char *fit_uname_ramdisk = NULL;
804 * Look for a '-' which indicates to ignore the
807 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
808 debug ("## Skipping init Ramdisk\n");
809 rd_len = rd_data = 0;
810 } else if (argc >= 3 || genimg_has_config (images)) {
811 #if defined(CONFIG_FIT)
814 * If the init ramdisk comes from the FIT image and
815 * the FIT image address is omitted in the command
816 * line argument, try to use os FIT image address or
817 * default load address.
819 if (images->fit_uname_os)
820 default_addr = (ulong)images->fit_hdr_os;
822 default_addr = load_addr;
824 if (fit_parse_conf (argv[2], default_addr,
825 &rd_addr, &fit_uname_config)) {
826 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
827 fit_uname_config, rd_addr);
828 } else if (fit_parse_subimage (argv[2], default_addr,
829 &rd_addr, &fit_uname_ramdisk)) {
830 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
831 fit_uname_ramdisk, rd_addr);
835 rd_addr = simple_strtoul(argv[2], NULL, 16);
836 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
839 #if defined(CONFIG_FIT)
841 /* use FIT configuration provided in first bootm
844 rd_addr = (ulong)images->fit_hdr_os;
845 fit_uname_config = images->fit_uname_cfg;
846 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
847 fit_uname_config, rd_addr);
850 * Check whether configuration has ramdisk defined,
851 * if not, don't try to use it, quit silently.
853 fit_hdr = (void *)rd_addr;
854 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
855 if (cfg_noffset < 0) {
856 debug ("* ramdisk: no such config\n");
860 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
861 if (rd_noffset < 0) {
862 debug ("* ramdisk: no ramdisk in config\n");
868 /* copy from dataflash if needed */
869 rd_addr = genimg_get_image (rd_addr);
872 * Check if there is an initrd image at the
873 * address provided in the second bootm argument
874 * check image type, for FIT images get FIT node.
876 switch (genimg_get_format ((void *)rd_addr)) {
877 case IMAGE_FORMAT_LEGACY:
878 printf ("## Loading init Ramdisk from Legacy "
879 "Image at %08lx ...\n", rd_addr);
881 show_boot_progress (9);
882 rd_hdr = image_get_ramdisk (rd_addr, arch,
888 rd_data = image_get_data (rd_hdr);
889 rd_len = image_get_data_size (rd_hdr);
890 rd_load = image_get_load (rd_hdr);
892 #if defined(CONFIG_FIT)
893 case IMAGE_FORMAT_FIT:
894 fit_hdr = (void *)rd_addr;
895 printf ("## Loading init Ramdisk from FIT "
896 "Image at %08lx ...\n", rd_addr);
898 show_boot_progress (120);
899 if (!fit_check_format (fit_hdr)) {
900 puts ("Bad FIT ramdisk image format!\n");
901 show_boot_progress (-120);
904 show_boot_progress (121);
906 if (!fit_uname_ramdisk) {
908 * no ramdisk image node unit name, try to get config
909 * node first. If config unit node name is NULL
910 * fit_conf_get_node() will try to find default config node
912 show_boot_progress (122);
913 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
914 if (cfg_noffset < 0) {
915 puts ("Could not find configuration node\n");
916 show_boot_progress (-122);
919 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
920 printf (" Using '%s' configuration\n", fit_uname_config);
922 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
923 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
925 /* get ramdisk component image node offset */
926 show_boot_progress (123);
927 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
929 if (rd_noffset < 0) {
930 puts ("Could not find subimage node\n");
931 show_boot_progress (-124);
935 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
937 show_boot_progress (125);
938 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
941 /* get ramdisk image data address and length */
942 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
943 puts ("Could not find ramdisk subimage data!\n");
944 show_boot_progress (-127);
947 show_boot_progress (128);
949 rd_data = (ulong)data;
952 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
953 puts ("Can't get ramdisk subimage load address!\n");
954 show_boot_progress (-129);
957 show_boot_progress (129);
959 images->fit_hdr_rd = fit_hdr;
960 images->fit_uname_rd = fit_uname_ramdisk;
961 images->fit_noffset_rd = rd_noffset;
965 puts ("Wrong Ramdisk Image Format\n");
966 rd_data = rd_len = rd_load = 0;
970 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
972 * We need to copy the ramdisk to SRAM to let Linux boot
975 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
978 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
980 } else if (images->legacy_hdr_valid &&
981 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
983 * Now check if we have a legacy mult-component image,
984 * get second entry data start address and len.
986 show_boot_progress (13);
987 printf ("## Loading init Ramdisk from multi component "
988 "Legacy Image at %08lx ...\n",
989 (ulong)images->legacy_hdr_os);
991 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
996 show_boot_progress (14);
997 rd_len = rd_data = 0;
1001 debug ("## No init Ramdisk\n");
1003 *rd_start = rd_data;
1004 *rd_end = rd_data + rd_len;
1006 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1007 *rd_start, *rd_end);
1012 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1014 * boot_ramdisk_high - relocate init ramdisk
1015 * @lmb: pointer to lmb handle, will be used for memory mgmt
1016 * @rd_data: ramdisk data start address
1017 * @rd_len: ramdisk data length
1018 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1019 * start address (after possible relocation)
1020 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1021 * end address (after possible relocation)
1023 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1024 * variable and if requested ramdisk data is moved to a specified location.
1026 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1027 * start/end addresses if ramdisk image start and len were provided,
1028 * otherwise set initrd_start and initrd_end set to zeros.
1034 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
1035 ulong *initrd_start, ulong *initrd_end)
1039 int initrd_copy_to_ram = 1;
1041 if ((s = getenv ("initrd_high")) != NULL) {
1042 /* a value of "no" or a similar string will act like 0,
1043 * turning the "load high" feature off. This is intentional.
1045 initrd_high = simple_strtoul (s, NULL, 16);
1046 if (initrd_high == ~0)
1047 initrd_copy_to_ram = 0;
1049 /* not set, no restrictions to load high */
1054 #ifdef CONFIG_LOGBUFFER
1055 /* Prevent initrd from overwriting logbuffer */
1056 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1059 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1060 initrd_high, initrd_copy_to_ram);
1063 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1064 debug (" in-place initrd\n");
1065 *initrd_start = rd_data;
1066 *initrd_end = rd_data + rd_len;
1067 lmb_reserve(lmb, rd_data, rd_len);
1070 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
1072 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
1074 if (*initrd_start == 0) {
1075 puts ("ramdisk - allocation error\n");
1078 show_boot_progress (12);
1080 *initrd_end = *initrd_start + rd_len;
1081 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1082 *initrd_start, *initrd_end);
1084 memmove_wd ((void *)*initrd_start,
1085 (void *)rd_data, rd_len, CHUNKSZ);
1093 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1094 *initrd_start, *initrd_end);
1101 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1103 #ifdef CONFIG_OF_LIBFDT
1104 static void fdt_error (const char *msg)
1108 puts (" - must RESET the board to recover.\n");
1111 static const image_header_t *image_get_fdt (ulong fdt_addr)
1113 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1115 image_print_contents (fdt_hdr);
1117 puts (" Verifying Checksum ... ");
1118 if (!image_check_hcrc (fdt_hdr)) {
1119 fdt_error ("fdt header checksum invalid");
1123 if (!image_check_dcrc (fdt_hdr)) {
1124 fdt_error ("fdt checksum invalid");
1129 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1130 fdt_error ("uImage is not a fdt");
1133 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1134 fdt_error ("uImage is compressed");
1137 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1138 fdt_error ("uImage data is not a fdt");
1145 * fit_check_fdt - verify FIT format FDT subimage
1146 * @fit_hdr: pointer to the FIT header
1147 * fdt_noffset: FDT subimage node offset within FIT image
1148 * @verify: data CRC verification flag
1150 * fit_check_fdt() verifies integrity of the FDT subimage and from
1151 * specified FIT image.
1157 #if defined(CONFIG_FIT)
1158 static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1160 fit_image_print (fit, fdt_noffset, " ");
1163 puts (" Verifying Hash Integrity ... ");
1164 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1165 fdt_error ("Bad Data Hash");
1171 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1172 fdt_error ("Not a FDT image");
1176 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1177 fdt_error ("FDT image is compressed");
1183 #endif /* CONFIG_FIT */
1185 #ifndef CONFIG_SYS_FDT_PAD
1186 #define CONFIG_SYS_FDT_PAD 0x3000
1189 #if defined(CONFIG_OF_LIBFDT)
1191 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1192 * @lmb: pointer to lmb handle, will be used for memory mgmt
1193 * @fdt_blob: pointer to fdt blob base address
1195 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1196 * memreserve regions prevents u-boot from using them to store the initrd
1199 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1201 uint64_t addr, size;
1204 if (fdt_check_header (fdt_blob) != 0)
1207 total = fdt_num_mem_rsv(fdt_blob);
1208 for (i = 0; i < total; i++) {
1209 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1211 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1212 (unsigned long long)addr, (unsigned long long)size);
1213 lmb_reserve(lmb, addr, size);
1218 * boot_relocate_fdt - relocate flat device tree
1219 * @lmb: pointer to lmb handle, will be used for memory mgmt
1220 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1221 * @of_size: pointer to a ulong variable, will hold fdt length
1223 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1224 * relocates the of_flat_tree into that region, even if the fdt is already in
1225 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1228 * of_flat_tree and of_size are set to final (after relocation) values
1234 int boot_relocate_fdt (struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1236 void *fdt_blob = *of_flat_tree;
1241 int disable_relocation = 0;
1247 if (fdt_check_header (fdt_blob) != 0) {
1248 fdt_error ("image is not a fdt");
1252 /* position on a 4K boundary before the alloc_current */
1253 /* Pad the FDT by a specified amount */
1254 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1256 /* If fdt_high is set use it to select the relocation address */
1257 fdt_high = getenv("fdt_high");
1259 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1261 if (((ulong) desired_addr) == ~0UL) {
1262 /* All ones means use fdt in place */
1263 desired_addr = fdt_blob;
1264 disable_relocation = 1;
1268 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1272 if (desired_addr && of_start != desired_addr) {
1273 puts("Failed using fdt_high value for Device Tree");
1278 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
1282 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1283 getenv_bootm_mapsize()
1284 + getenv_bootm_low());
1287 if (of_start == 0) {
1288 puts("device tree - allocation error\n");
1292 if (disable_relocation) {
1293 /* We assume there is space after the existing fdt to use for padding */
1294 fdt_set_totalsize(of_start, of_len);
1295 printf(" Using Device Tree in place at %p, end %p\n",
1296 of_start, of_start + of_len - 1);
1298 debug ("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1299 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1301 printf (" Loading Device Tree to %p, end %p ... ",
1302 of_start, of_start + of_len - 1);
1304 err = fdt_open_into (fdt_blob, of_start, of_len);
1306 fdt_error ("fdt move failed");
1312 *of_flat_tree = of_start;
1315 set_working_fdt_addr(*of_flat_tree);
1321 #endif /* CONFIG_OF_LIBFDT */
1324 * boot_get_fdt - main fdt handling routine
1325 * @argc: command argument count
1326 * @argv: command argument list
1327 * @images: pointer to the bootm images structure
1328 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1329 * @of_size: pointer to a ulong variable, will hold fdt length
1331 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1332 * Curently supported are the following ramdisk sources:
1333 * - multicomponent kernel/ramdisk image,
1334 * - commandline provided address of decicated ramdisk image.
1337 * 0, if fdt image was found and valid, or skipped
1338 * of_flat_tree and of_size are set to fdt start address and length if
1339 * fdt image is found and valid
1341 * 1, if fdt image is found but corrupted
1342 * of_flat_tree and of_size are set to 0 if no fdt exists
1344 int boot_get_fdt (int flag, int argc, char * const argv[], bootm_headers_t *images,
1345 char **of_flat_tree, ulong *of_size)
1347 const image_header_t *fdt_hdr;
1349 char *fdt_blob = NULL;
1350 ulong image_start, image_end;
1351 ulong load_start, load_end;
1352 #if defined(CONFIG_FIT)
1354 const char *fit_uname_config = NULL;
1355 const char *fit_uname_fdt = NULL;
1363 *of_flat_tree = NULL;
1366 if (argc > 3 || genimg_has_config (images)) {
1367 #if defined(CONFIG_FIT)
1370 * If the FDT blob comes from the FIT image and the
1371 * FIT image address is omitted in the command line
1372 * argument, try to use ramdisk or os FIT image
1373 * address or default load address.
1375 if (images->fit_uname_rd)
1376 default_addr = (ulong)images->fit_hdr_rd;
1377 else if (images->fit_uname_os)
1378 default_addr = (ulong)images->fit_hdr_os;
1380 default_addr = load_addr;
1382 if (fit_parse_conf (argv[3], default_addr,
1383 &fdt_addr, &fit_uname_config)) {
1384 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1385 fit_uname_config, fdt_addr);
1386 } else if (fit_parse_subimage (argv[3], default_addr,
1387 &fdt_addr, &fit_uname_fdt)) {
1388 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1389 fit_uname_fdt, fdt_addr);
1393 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1394 debug ("* fdt: cmdline image address = 0x%08lx\n",
1397 #if defined(CONFIG_FIT)
1399 /* use FIT configuration provided in first bootm
1402 fdt_addr = (ulong)images->fit_hdr_os;
1403 fit_uname_config = images->fit_uname_cfg;
1404 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1405 fit_uname_config, fdt_addr);
1408 * Check whether configuration has FDT blob defined,
1409 * if not quit silently.
1411 fit_hdr = (void *)fdt_addr;
1412 cfg_noffset = fit_conf_get_node (fit_hdr,
1414 if (cfg_noffset < 0) {
1415 debug ("* fdt: no such config\n");
1419 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1421 if (fdt_noffset < 0) {
1422 debug ("* fdt: no fdt in config\n");
1428 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1431 /* copy from dataflash if needed */
1432 fdt_addr = genimg_get_image (fdt_addr);
1435 * Check if there is an FDT image at the
1436 * address provided in the second bootm argument
1437 * check image type, for FIT images get a FIT node.
1439 switch (genimg_get_format ((void *)fdt_addr)) {
1440 case IMAGE_FORMAT_LEGACY:
1441 /* verify fdt_addr points to a valid image header */
1442 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1444 fdt_hdr = image_get_fdt (fdt_addr);
1449 * move image data to the load address,
1450 * make sure we don't overwrite initial image
1452 image_start = (ulong)fdt_hdr;
1453 image_end = image_get_image_end (fdt_hdr);
1455 load_start = image_get_load (fdt_hdr);
1456 load_end = load_start + image_get_data_size (fdt_hdr);
1458 if ((load_start < image_end) && (load_end > image_start)) {
1459 fdt_error ("fdt overwritten");
1463 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1464 image_get_data (fdt_hdr), load_start);
1466 memmove ((void *)load_start,
1467 (void *)image_get_data (fdt_hdr),
1468 image_get_data_size (fdt_hdr));
1470 fdt_blob = (char *)load_start;
1472 case IMAGE_FORMAT_FIT:
1474 * This case will catch both: new uImage format
1475 * (libfdt based) and raw FDT blob (also libfdt
1478 #if defined(CONFIG_FIT)
1479 /* check FDT blob vs FIT blob */
1480 if (fit_check_format ((const void *)fdt_addr)) {
1484 fit_hdr = (void *)fdt_addr;
1485 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1488 if (!fit_uname_fdt) {
1490 * no FDT blob image node unit name,
1491 * try to get config node first. If
1492 * config unit node name is NULL
1493 * fit_conf_get_node() will try to
1494 * find default config node
1496 cfg_noffset = fit_conf_get_node (fit_hdr,
1499 if (cfg_noffset < 0) {
1500 fdt_error ("Could not find configuration node\n");
1504 fit_uname_config = fdt_get_name (fit_hdr,
1506 printf (" Using '%s' configuration\n",
1509 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1511 fit_uname_fdt = fit_get_name (fit_hdr,
1514 /* get FDT component image node offset */
1515 fdt_noffset = fit_image_get_node (fit_hdr,
1518 if (fdt_noffset < 0) {
1519 fdt_error ("Could not find subimage node\n");
1523 printf (" Trying '%s' FDT blob subimage\n",
1526 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1530 /* get ramdisk image data address and length */
1531 if (fit_image_get_data (fit_hdr, fdt_noffset,
1533 fdt_error ("Could not find FDT subimage data");
1537 /* verift that image data is a proper FDT blob */
1538 if (fdt_check_header ((char *)data) != 0) {
1539 fdt_error ("Subimage data is not a FTD");
1544 * move image data to the load address,
1545 * make sure we don't overwrite initial image
1547 image_start = (ulong)fit_hdr;
1548 image_end = fit_get_end (fit_hdr);
1550 if (fit_image_get_load (fit_hdr, fdt_noffset,
1551 &load_start) == 0) {
1552 load_end = load_start + size;
1554 if ((load_start < image_end) &&
1555 (load_end > image_start)) {
1556 fdt_error ("FDT overwritten");
1560 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1561 (ulong)data, load_start);
1563 memmove ((void *)load_start,
1564 (void *)data, size);
1566 fdt_blob = (char *)load_start;
1568 fdt_blob = (char *)data;
1571 images->fit_hdr_fdt = fit_hdr;
1572 images->fit_uname_fdt = fit_uname_fdt;
1573 images->fit_noffset_fdt = fdt_noffset;
1581 fdt_blob = (char *)fdt_addr;
1582 debug ("* fdt: raw FDT blob\n");
1583 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1587 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
1591 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1593 } else if (images->legacy_hdr_valid &&
1594 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1596 ulong fdt_data, fdt_len;
1599 * Now check if we have a legacy multi-component image,
1600 * get second entry data start address and len.
1602 printf ("## Flattened Device Tree from multi "
1603 "component Image at %08lX\n",
1604 (ulong)images->legacy_hdr_os);
1606 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1609 fdt_blob = (char *)fdt_data;
1610 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1612 if (fdt_check_header (fdt_blob) != 0) {
1613 fdt_error ("image is not a fdt");
1617 if (fdt_totalsize(fdt_blob) != fdt_len) {
1618 fdt_error ("fdt size != image size");
1622 debug ("## No Flattened Device Tree\n");
1626 debug ("## No Flattened Device Tree\n");
1630 *of_flat_tree = fdt_blob;
1631 *of_size = fdt_totalsize(fdt_blob);
1632 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
1633 (ulong)*of_flat_tree, *of_size);
1642 #endif /* CONFIG_OF_LIBFDT */
1644 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1646 * boot_get_cmdline - allocate and initialize kernel cmdline
1647 * @lmb: pointer to lmb handle, will be used for memory mgmt
1648 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1649 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1651 * boot_get_cmdline() allocates space for kernel command line below
1652 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1653 * variable is present its contents is copied to allocated kernel
1660 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1665 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1666 getenv_bootm_mapsize() + getenv_bootm_low());
1668 if (cmdline == NULL)
1671 if ((s = getenv("bootargs")) == NULL)
1676 *cmd_start = (ulong) & cmdline[0];
1677 *cmd_end = *cmd_start + strlen(cmdline);
1679 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1683 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1685 #ifdef CONFIG_SYS_BOOT_GET_KBD
1687 * boot_get_kbd - allocate and initialize kernel copy of board info
1688 * @lmb: pointer to lmb handle, will be used for memory mgmt
1689 * @kbd: double pointer to board info data
1691 * boot_get_kbd() allocates space for kernel copy of board info data below
1692 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1693 * with the current u-boot board info data.
1699 int boot_get_kbd (struct lmb *lmb, bd_t **kbd)
1701 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1702 getenv_bootm_mapsize() + getenv_bootm_low());
1708 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1710 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1711 do_bdinfo(NULL, 0, 0, NULL);
1716 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1717 #endif /* !USE_HOSTCC */
1719 #if defined(CONFIG_FIT)
1720 /*****************************************************************************/
1721 /* New uImage format routines */
1722 /*****************************************************************************/
1724 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1725 ulong *addr, const char **name)
1732 sep = strchr (spec, sepc);
1735 *addr = simple_strtoul (spec, NULL, 16);
1745 * fit_parse_conf - parse FIT configuration spec
1746 * @spec: input string, containing configuration spec
1747 * @add_curr: current image address (to be used as a possible default)
1748 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1750 * @conf_name double pointer to a char, will hold pointer to a configuration
1753 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1754 * where <addr> is a FIT image address that contains configuration
1755 * with a <conf> unit name.
1757 * Address part is optional, and if omitted default add_curr will
1761 * 1 if spec is a valid configuration string,
1762 * addr and conf_name are set accordingly
1765 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1766 ulong *addr, const char **conf_name)
1768 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1772 * fit_parse_subimage - parse FIT subimage spec
1773 * @spec: input string, containing subimage spec
1774 * @add_curr: current image address (to be used as a possible default)
1775 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1777 * @image_name: double pointer to a char, will hold pointer to a subimage name
1779 * fit_parse_subimage() expects subimage spec in the for of
1780 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1781 * subimage with a <subimg> unit name.
1783 * Address part is optional, and if omitted default add_curr will
1787 * 1 if spec is a valid subimage string,
1788 * addr and image_name are set accordingly
1791 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1792 ulong *addr, const char **image_name)
1794 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1796 #endif /* !USE_HOSTCC */
1798 static void fit_get_debug (const void *fit, int noffset,
1799 char *prop_name, int err)
1801 debug ("Can't get '%s' property from FIT 0x%08lx, "
1802 "node: offset %d, name %s (%s)\n",
1803 prop_name, (ulong)fit, noffset,
1804 fit_get_name (fit, noffset, NULL),
1805 fdt_strerror (err));
1809 * fit_print_contents - prints out the contents of the FIT format image
1810 * @fit: pointer to the FIT format image header
1811 * @p: pointer to prefix string
1813 * fit_print_contents() formats a multi line FIT image contents description.
1814 * The routine prints out FIT image properties (root node level) follwed by
1815 * the details of each component image.
1818 * no returned results
1820 void fit_print_contents (const void *fit)
1831 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1841 /* Root node properties */
1842 ret = fit_get_desc (fit, 0, &desc);
1843 printf ("%sFIT description: ", p);
1845 printf ("unavailable\n");
1847 printf ("%s\n", desc);
1849 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1850 ret = fit_get_timestamp (fit, 0, ×tamp);
1851 printf ("%sCreated: ", p);
1853 printf ("unavailable\n");
1855 genimg_print_time (timestamp);
1858 /* Find images parent node offset */
1859 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1860 if (images_noffset < 0) {
1861 printf ("Can't find images parent node '%s' (%s)\n",
1862 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1866 /* Process its subnodes, print out component images details */
1867 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1868 (noffset >= 0) && (ndepth > 0);
1869 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1872 * Direct child node of the images parent node,
1873 * i.e. component image node.
1875 printf ("%s Image %u (%s)\n", p, count++,
1876 fit_get_name(fit, noffset, NULL));
1878 fit_image_print (fit, noffset, p);
1882 /* Find configurations parent node offset */
1883 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1884 if (confs_noffset < 0) {
1885 debug ("Can't get configurations parent node '%s' (%s)\n",
1886 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1890 /* get default configuration unit name from default property */
1891 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1893 printf ("%s Default Configuration: '%s'\n", p, uname);
1895 /* Process its subnodes, print out configurations details */
1896 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1897 (noffset >= 0) && (ndepth > 0);
1898 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1901 * Direct child node of the configurations parent node,
1902 * i.e. configuration node.
1904 printf ("%s Configuration %u (%s)\n", p, count++,
1905 fit_get_name(fit, noffset, NULL));
1907 fit_conf_print (fit, noffset, p);
1913 * fit_image_print - prints out the FIT component image details
1914 * @fit: pointer to the FIT format image header
1915 * @image_noffset: offset of the component image node
1916 * @p: pointer to prefix string
1918 * fit_image_print() lists all mandatory properies for the processed component
1919 * image. If present, hash nodes are printed out as well. Load
1920 * address for images of type firmware is also printed out. Since the load
1921 * address is not mandatory for firmware images, it will be output as
1922 * "unavailable" when not present.
1925 * no returned results
1927 void fit_image_print (const void *fit, int image_noffset, const char *p)
1930 uint8_t type, arch, os, comp;
1938 /* Mandatory properties */
1939 ret = fit_get_desc (fit, image_noffset, &desc);
1940 printf ("%s Description: ", p);
1942 printf ("unavailable\n");
1944 printf ("%s\n", desc);
1946 fit_image_get_type (fit, image_noffset, &type);
1947 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1949 fit_image_get_comp (fit, image_noffset, &comp);
1950 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1952 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1955 printf ("%s Data Start: ", p);
1957 printf ("unavailable\n");
1959 printf ("0x%08lx\n", (ulong)data);
1962 printf ("%s Data Size: ", p);
1964 printf ("unavailable\n");
1966 genimg_print_size (size);
1968 /* Remaining, type dependent properties */
1969 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1970 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1971 (type == IH_TYPE_FLATDT)) {
1972 fit_image_get_arch (fit, image_noffset, &arch);
1973 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1976 if (type == IH_TYPE_KERNEL) {
1977 fit_image_get_os (fit, image_noffset, &os);
1978 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1981 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1982 (type == IH_TYPE_FIRMWARE)) {
1983 ret = fit_image_get_load (fit, image_noffset, &load);
1984 printf ("%s Load Address: ", p);
1986 printf ("unavailable\n");
1988 printf ("0x%08lx\n", load);
1991 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1992 fit_image_get_entry (fit, image_noffset, &entry);
1993 printf ("%s Entry Point: ", p);
1995 printf ("unavailable\n");
1997 printf ("0x%08lx\n", entry);
2000 /* Process all hash subnodes of the component image node */
2001 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2002 (noffset >= 0) && (ndepth > 0);
2003 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2005 /* Direct child node of the component image node */
2006 fit_image_print_hash (fit, noffset, p);
2012 * fit_image_print_hash - prints out the hash node details
2013 * @fit: pointer to the FIT format image header
2014 * @noffset: offset of the hash node
2015 * @p: pointer to prefix string
2017 * fit_image_print_hash() lists properies for the processed hash node
2020 * no returned results
2022 void fit_image_print_hash (const void *fit, int noffset, const char *p)
2030 * Check subnode name, must be equal to "hash".
2031 * Multiple hash nodes require unique unit node
2032 * names, e.g. hash@1, hash@2, etc.
2034 if (strncmp (fit_get_name(fit, noffset, NULL),
2036 strlen(FIT_HASH_NODENAME)) != 0)
2039 debug ("%s Hash node: '%s'\n", p,
2040 fit_get_name (fit, noffset, NULL));
2042 printf ("%s Hash algo: ", p);
2043 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2044 printf ("invalid/unsupported\n");
2047 printf ("%s\n", algo);
2049 ret = fit_image_hash_get_value (fit, noffset, &value,
2051 printf ("%s Hash value: ", p);
2053 printf ("unavailable\n");
2055 for (i = 0; i < value_len; i++)
2056 printf ("%02x", value[i]);
2060 debug ("%s Hash len: %d\n", p, value_len);
2064 * fit_get_desc - get node description property
2065 * @fit: pointer to the FIT format image header
2066 * @noffset: node offset
2067 * @desc: double pointer to the char, will hold pointer to the descrption
2069 * fit_get_desc() reads description property from a given node, if
2070 * description is found pointer to it is returened in third call argument.
2076 int fit_get_desc (const void *fit, int noffset, char **desc)
2080 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2081 if (*desc == NULL) {
2082 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2090 * fit_get_timestamp - get node timestamp property
2091 * @fit: pointer to the FIT format image header
2092 * @noffset: node offset
2093 * @timestamp: pointer to the time_t, will hold read timestamp
2095 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2096 * is found and has a correct size its value is retured in third call
2101 * -1, on property read failure
2102 * -2, on wrong timestamp size
2104 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2109 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2111 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2114 if (len != sizeof (uint32_t)) {
2115 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2119 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2124 * fit_image_get_node - get node offset for component image of a given unit name
2125 * @fit: pointer to the FIT format image header
2126 * @image_uname: component image node unit name
2128 * fit_image_get_node() finds a component image (withing the '/images'
2129 * node) of a provided unit name. If image is found its node offset is
2130 * returned to the caller.
2133 * image node offset when found (>=0)
2134 * negative number on failure (FDT_ERR_* code)
2136 int fit_image_get_node (const void *fit, const char *image_uname)
2138 int noffset, images_noffset;
2140 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2141 if (images_noffset < 0) {
2142 debug ("Can't find images parent node '%s' (%s)\n",
2143 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2144 return images_noffset;
2147 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2149 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2150 image_uname, fdt_strerror (noffset));
2157 * fit_image_get_os - get os id for a given component image node
2158 * @fit: pointer to the FIT format image header
2159 * @noffset: component image node offset
2160 * @os: pointer to the uint8_t, will hold os numeric id
2162 * fit_image_get_os() finds os property in a given component image node.
2163 * If the property is found, its (string) value is translated to the numeric
2164 * id which is returned to the caller.
2170 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2175 /* Get OS name from property data */
2176 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2178 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2183 /* Translate OS name to id */
2184 *os = genimg_get_os_id (data);
2189 * fit_image_get_arch - get arch id for a given component image node
2190 * @fit: pointer to the FIT format image header
2191 * @noffset: component image node offset
2192 * @arch: pointer to the uint8_t, will hold arch numeric id
2194 * fit_image_get_arch() finds arch property in a given component image node.
2195 * If the property is found, its (string) value is translated to the numeric
2196 * id which is returned to the caller.
2202 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2207 /* Get architecture name from property data */
2208 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2210 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2215 /* Translate architecture name to id */
2216 *arch = genimg_get_arch_id (data);
2221 * fit_image_get_type - get type id for a given component image node
2222 * @fit: pointer to the FIT format image header
2223 * @noffset: component image node offset
2224 * @type: pointer to the uint8_t, will hold type numeric id
2226 * fit_image_get_type() finds type property in a given component image node.
2227 * If the property is found, its (string) value is translated to the numeric
2228 * id which is returned to the caller.
2234 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2239 /* Get image type name from property data */
2240 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2242 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2247 /* Translate image type name to id */
2248 *type = genimg_get_type_id (data);
2253 * fit_image_get_comp - get comp id for a given component image node
2254 * @fit: pointer to the FIT format image header
2255 * @noffset: component image node offset
2256 * @comp: pointer to the uint8_t, will hold comp numeric id
2258 * fit_image_get_comp() finds comp property in a given component image node.
2259 * If the property is found, its (string) value is translated to the numeric
2260 * id which is returned to the caller.
2266 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2271 /* Get compression name from property data */
2272 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2274 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2279 /* Translate compression name to id */
2280 *comp = genimg_get_comp_id (data);
2285 * fit_image_get_load - get load address property for a given component image node
2286 * @fit: pointer to the FIT format image header
2287 * @noffset: component image node offset
2288 * @load: pointer to the uint32_t, will hold load address
2290 * fit_image_get_load() finds load address property in a given component image node.
2291 * If the property is found, its value is returned to the caller.
2297 int fit_image_get_load (const void *fit, int noffset, ulong *load)
2300 const uint32_t *data;
2302 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2304 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2308 *load = uimage_to_cpu (*data);
2313 * fit_image_get_entry - get entry point address property for a given component image node
2314 * @fit: pointer to the FIT format image header
2315 * @noffset: component image node offset
2316 * @entry: pointer to the uint32_t, will hold entry point address
2318 * fit_image_get_entry() finds entry point address property in a given component image node.
2319 * If the property is found, its value is returned to the caller.
2325 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2328 const uint32_t *data;
2330 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2332 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2336 *entry = uimage_to_cpu (*data);
2341 * fit_image_get_data - get data property and its size for a given component image node
2342 * @fit: pointer to the FIT format image header
2343 * @noffset: component image node offset
2344 * @data: double pointer to void, will hold data property's data address
2345 * @size: pointer to size_t, will hold data property's data size
2347 * fit_image_get_data() finds data property in a given component image node.
2348 * If the property is found its data start address and size are returned to
2355 int fit_image_get_data (const void *fit, int noffset,
2356 const void **data, size_t *size)
2360 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2361 if (*data == NULL) {
2362 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2372 * fit_image_hash_get_algo - get hash algorithm name
2373 * @fit: pointer to the FIT format image header
2374 * @noffset: hash node offset
2375 * @algo: double pointer to char, will hold pointer to the algorithm name
2377 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2378 * If the property is found its data start address is returned to the caller.
2384 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2388 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2389 if (*algo == NULL) {
2390 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2398 * fit_image_hash_get_value - get hash value and length
2399 * @fit: pointer to the FIT format image header
2400 * @noffset: hash node offset
2401 * @value: double pointer to uint8_t, will hold address of a hash value data
2402 * @value_len: pointer to an int, will hold hash data length
2404 * fit_image_hash_get_value() finds hash value property in a given hash node.
2405 * If the property is found its data start address and size are returned to
2412 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2417 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2418 if (*value == NULL) {
2419 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2429 * fit_set_timestamp - set node timestamp property
2430 * @fit: pointer to the FIT format image header
2431 * @noffset: node offset
2432 * @timestamp: timestamp value to be set
2434 * fit_set_timestamp() attempts to set timestamp property in the requested
2435 * node and returns operation status to the caller.
2439 * -1, on property read failure
2441 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2446 t = cpu_to_uimage (timestamp);
2447 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2450 printf ("Can't set '%s' property for '%s' node (%s)\n",
2451 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2452 fdt_strerror (ret));
2460 * calculate_hash - calculate and return hash for provided input data
2461 * @data: pointer to the input data
2462 * @data_len: data length
2463 * @algo: requested hash algorithm
2464 * @value: pointer to the char, will hold hash value data (caller must
2465 * allocate enough free space)
2466 * value_len: length of the calculated hash
2468 * calculate_hash() computes input data hash according to the requested algorithm.
2469 * Resulting hash value is placed in caller provided 'value' buffer, length
2470 * of the calculated hash is returned via value_len pointer argument.
2474 * -1, when algo is unsupported
2476 static int calculate_hash (const void *data, int data_len, const char *algo,
2477 uint8_t *value, int *value_len)
2479 if (strcmp (algo, "crc32") == 0 ) {
2480 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2482 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2484 } else if (strcmp (algo, "sha1") == 0 ) {
2485 sha1_csum_wd ((unsigned char *) data, data_len,
2486 (unsigned char *) value, CHUNKSZ_SHA1);
2488 } else if (strcmp (algo, "md5") == 0 ) {
2489 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2492 debug ("Unsupported hash alogrithm\n");
2500 * fit_set_hashes - process FIT component image nodes and calculate hashes
2501 * @fit: pointer to the FIT format image header
2503 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2504 * Hashes are calculated for all component images which have hash subnodes
2505 * with algorithm property set to one of the supported hash algorithms.
2509 * libfdt error code, on failure
2511 int fit_set_hashes (void *fit)
2518 /* Find images parent node offset */
2519 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2520 if (images_noffset < 0) {
2521 printf ("Can't find images parent node '%s' (%s)\n",
2522 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2523 return images_noffset;
2526 /* Process its subnodes, print out component images details */
2527 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2528 (noffset >= 0) && (ndepth > 0);
2529 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2532 * Direct child node of the images parent node,
2533 * i.e. component image node.
2535 ret = fit_image_set_hashes (fit, noffset);
2545 * fit_image_set_hashes - calculate/set hashes for given component image node
2546 * @fit: pointer to the FIT format image header
2547 * @image_noffset: requested component image node
2549 * fit_image_set_hashes() adds hash values for an component image node. All
2550 * existing hash subnodes are checked, if algorithm property is set to one of
2551 * the supported hash algorithms, hash value is computed and corresponding
2552 * hash node property is set, for example:
2554 * Input component image node structure:
2556 * o image@1 (at image_noffset)
2557 * | - data = [binary data]
2561 * Output component image node structure:
2563 * o image@1 (at image_noffset)
2564 * | - data = [binary data]
2567 * |- value = sha1(data)
2573 int fit_image_set_hashes (void *fit, int image_noffset)
2578 uint8_t value[FIT_MAX_HASH_LEN];
2583 /* Get image data and data length */
2584 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2585 printf ("Can't get image data/size\n");
2589 /* Process all hash subnodes of the component image node */
2590 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2591 (noffset >= 0) && (ndepth > 0);
2592 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2594 /* Direct child node of the component image node */
2597 * Check subnode name, must be equal to "hash".
2598 * Multiple hash nodes require unique unit node
2599 * names, e.g. hash@1, hash@2, etc.
2601 if (strncmp (fit_get_name(fit, noffset, NULL),
2603 strlen(FIT_HASH_NODENAME)) != 0) {
2604 /* Not a hash subnode, skip it */
2608 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2609 printf ("Can't get hash algo property for "
2610 "'%s' hash node in '%s' image node\n",
2611 fit_get_name (fit, noffset, NULL),
2612 fit_get_name (fit, image_noffset, NULL));
2616 if (calculate_hash (data, size, algo, value, &value_len)) {
2617 printf ("Unsupported hash algorithm (%s) for "
2618 "'%s' hash node in '%s' image node\n",
2619 algo, fit_get_name (fit, noffset, NULL),
2620 fit_get_name (fit, image_noffset, NULL));
2624 if (fit_image_hash_set_value (fit, noffset, value,
2626 printf ("Can't set hash value for "
2627 "'%s' hash node in '%s' image node\n",
2628 fit_get_name (fit, noffset, NULL),
2629 fit_get_name (fit, image_noffset, NULL));
2639 * fit_image_hash_set_value - set hash value in requested has node
2640 * @fit: pointer to the FIT format image header
2641 * @noffset: hash node offset
2642 * @value: hash value to be set
2643 * @value_len: hash value length
2645 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2646 * given and returns operation status to the caller.
2652 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2657 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2659 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2660 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2661 fdt_strerror (ret));
2667 #endif /* USE_HOSTCC */
2670 * fit_image_check_hashes - verify data intergity
2671 * @fit: pointer to the FIT format image header
2672 * @image_noffset: component image node offset
2674 * fit_image_check_hashes() goes over component image hash nodes,
2675 * re-calculates each data hash and compares with the value stored in hash
2679 * 1, if all hashes are valid
2680 * 0, otherwise (or on error)
2682 int fit_image_check_hashes (const void *fit, int image_noffset)
2689 uint8_t value[FIT_MAX_HASH_LEN];
2695 /* Get image data and data length */
2696 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2697 printf ("Can't get image data/size\n");
2701 /* Process all hash subnodes of the component image node */
2702 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2703 (noffset >= 0) && (ndepth > 0);
2704 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2706 /* Direct child node of the component image node */
2709 * Check subnode name, must be equal to "hash".
2710 * Multiple hash nodes require unique unit node
2711 * names, e.g. hash@1, hash@2, etc.
2713 if (strncmp (fit_get_name(fit, noffset, NULL),
2715 strlen(FIT_HASH_NODENAME)) != 0)
2718 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2719 err_msg = " error!\nCan't get hash algo "
2723 printf ("%s", algo);
2725 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2727 err_msg = " error!\nCan't get hash value "
2732 if (calculate_hash (data, size, algo, value, &value_len)) {
2733 err_msg = " error!\nUnsupported hash algorithm";
2737 if (value_len != fit_value_len) {
2738 err_msg = " error !\nBad hash value len";
2740 } else if (memcmp (value, fit_value, value_len) != 0) {
2741 err_msg = " error!\nBad hash value";
2751 printf ("%s for '%s' hash node in '%s' image node\n",
2752 err_msg, fit_get_name (fit, noffset, NULL),
2753 fit_get_name (fit, image_noffset, NULL));
2758 * fit_all_image_check_hashes - verify data intergity for all images
2759 * @fit: pointer to the FIT format image header
2761 * fit_all_image_check_hashes() goes over all images in the FIT and
2762 * for every images checks if all it's hashes are valid.
2765 * 1, if all hashes of all images are valid
2766 * 0, otherwise (or on error)
2768 int fit_all_image_check_hashes (const void *fit)
2775 /* Find images parent node offset */
2776 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2777 if (images_noffset < 0) {
2778 printf ("Can't find images parent node '%s' (%s)\n",
2779 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2783 /* Process all image subnodes, check hashes for each */
2784 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2786 for (ndepth = 0, count = 0,
2787 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2788 (noffset >= 0) && (ndepth > 0);
2789 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2792 * Direct child node of the images parent node,
2793 * i.e. component image node.
2795 printf (" Hash(es) for Image %u (%s): ", count++,
2796 fit_get_name (fit, noffset, NULL));
2798 if (!fit_image_check_hashes (fit, noffset))
2807 * fit_image_check_os - check whether image node is of a given os type
2808 * @fit: pointer to the FIT format image header
2809 * @noffset: component image node offset
2810 * @os: requested image os
2812 * fit_image_check_os() reads image os property and compares its numeric
2813 * id with the requested os. Comparison result is returned to the caller.
2816 * 1 if image is of given os type
2817 * 0 otherwise (or on error)
2819 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2823 if (fit_image_get_os (fit, noffset, &image_os))
2825 return (os == image_os);
2829 * fit_image_check_arch - check whether image node is of a given arch
2830 * @fit: pointer to the FIT format image header
2831 * @noffset: component image node offset
2832 * @arch: requested imagearch
2834 * fit_image_check_arch() reads image arch property and compares its numeric
2835 * id with the requested arch. Comparison result is returned to the caller.
2838 * 1 if image is of given arch
2839 * 0 otherwise (or on error)
2841 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2845 if (fit_image_get_arch (fit, noffset, &image_arch))
2847 return (arch == image_arch);
2851 * fit_image_check_type - check whether image node is of a given type
2852 * @fit: pointer to the FIT format image header
2853 * @noffset: component image node offset
2854 * @type: requested image type
2856 * fit_image_check_type() reads image type property and compares its numeric
2857 * id with the requested type. Comparison result is returned to the caller.
2860 * 1 if image is of given type
2861 * 0 otherwise (or on error)
2863 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2867 if (fit_image_get_type (fit, noffset, &image_type))
2869 return (type == image_type);
2873 * fit_image_check_comp - check whether image node uses given compression
2874 * @fit: pointer to the FIT format image header
2875 * @noffset: component image node offset
2876 * @comp: requested image compression type
2878 * fit_image_check_comp() reads image compression property and compares its
2879 * numeric id with the requested compression type. Comparison result is
2880 * returned to the caller.
2883 * 1 if image uses requested compression
2884 * 0 otherwise (or on error)
2886 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2890 if (fit_image_get_comp (fit, noffset, &image_comp))
2892 return (comp == image_comp);
2896 * fit_check_format - sanity check FIT image format
2897 * @fit: pointer to the FIT format image header
2899 * fit_check_format() runs a basic sanity FIT image verification.
2900 * Routine checks for mandatory properties, nodes, etc.
2906 int fit_check_format (const void *fit)
2908 /* mandatory / node 'description' property */
2909 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2910 debug ("Wrong FIT format: no description\n");
2914 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2915 /* mandatory / node 'timestamp' property */
2916 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2917 debug ("Wrong FIT format: no timestamp\n");
2922 /* mandatory subimages parent '/images' node */
2923 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2924 debug ("Wrong FIT format: no images parent node\n");
2932 * fit_conf_get_node - get node offset for configuration of a given unit name
2933 * @fit: pointer to the FIT format image header
2934 * @conf_uname: configuration node unit name
2936 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2937 * parant node) of a provided unit name. If configuration is found its node offset
2938 * is returned to the caller.
2940 * When NULL is provided in second argument fit_conf_get_node() will search
2941 * for a default configuration node instead. Default configuration node unit name
2942 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2945 * configuration node offset when found (>=0)
2946 * negative number on failure (FDT_ERR_* code)
2948 int fit_conf_get_node (const void *fit, const char *conf_uname)
2950 int noffset, confs_noffset;
2953 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2954 if (confs_noffset < 0) {
2955 debug ("Can't find configurations parent node '%s' (%s)\n",
2956 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2957 return confs_noffset;
2960 if (conf_uname == NULL) {
2961 /* get configuration unit name from the default property */
2962 debug ("No configuration specified, trying default...\n");
2963 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2964 if (conf_uname == NULL) {
2965 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2968 debug ("Found default configuration: '%s'\n", conf_uname);
2971 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2973 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2974 conf_uname, fdt_strerror (noffset));
2980 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2981 const char *prop_name)
2986 /* get kernel image unit name from configuration kernel property */
2987 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2991 return fit_image_get_node (fit, uname);
2995 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2996 * a given configuration
2997 * @fit: pointer to the FIT format image header
2998 * @noffset: configuration node offset
3000 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3001 * configuration FIT_KERNEL_PROP property and translates it to the node
3005 * image node offset when found (>=0)
3006 * negative number on failure (FDT_ERR_* code)
3008 int fit_conf_get_kernel_node (const void *fit, int noffset)
3010 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
3014 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3015 * a given configuration
3016 * @fit: pointer to the FIT format image header
3017 * @noffset: configuration node offset
3019 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3020 * configuration FIT_KERNEL_PROP property and translates it to the node
3024 * image node offset when found (>=0)
3025 * negative number on failure (FDT_ERR_* code)
3027 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
3029 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
3033 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3034 * a given configuration
3035 * @fit: pointer to the FIT format image header
3036 * @noffset: configuration node offset
3038 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3039 * configuration FIT_KERNEL_PROP property and translates it to the node
3043 * image node offset when found (>=0)
3044 * negative number on failure (FDT_ERR_* code)
3046 int fit_conf_get_fdt_node (const void *fit, int noffset)
3048 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
3052 * fit_conf_print - prints out the FIT configuration details
3053 * @fit: pointer to the FIT format image header
3054 * @noffset: offset of the configuration node
3055 * @p: pointer to prefix string
3057 * fit_conf_print() lists all mandatory properies for the processed
3058 * configuration node.
3061 * no returned results
3063 void fit_conf_print (const void *fit, int noffset, const char *p)
3069 /* Mandatory properties */
3070 ret = fit_get_desc (fit, noffset, &desc);
3071 printf ("%s Description: ", p);
3073 printf ("unavailable\n");
3075 printf ("%s\n", desc);
3077 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3078 printf ("%s Kernel: ", p);
3080 printf ("unavailable\n");
3082 printf ("%s\n", uname);
3084 /* Optional properties */
3085 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3087 printf ("%s Init Ramdisk: %s\n", p, uname);
3089 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3091 printf ("%s FDT: %s\n", p, uname);
3095 * fit_check_ramdisk - verify FIT format ramdisk subimage
3096 * @fit_hdr: pointer to the FIT ramdisk header
3097 * @rd_noffset: ramdisk subimage node offset within FIT image
3098 * @arch: requested ramdisk image architecture type
3099 * @verify: data CRC verification flag
3101 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3102 * specified FIT image.
3109 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3111 fit_image_print (fit, rd_noffset, " ");
3114 puts (" Verifying Hash Integrity ... ");
3115 if (!fit_image_check_hashes (fit, rd_noffset)) {
3116 puts ("Bad Data Hash\n");
3117 show_boot_progress (-125);
3123 show_boot_progress (126);
3124 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3125 !fit_image_check_arch (fit, rd_noffset, arch) ||
3126 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3127 printf ("No Linux %s Ramdisk Image\n",
3128 genimg_get_arch_name(arch));
3129 show_boot_progress (-126);
3133 show_boot_progress (127);
3136 #endif /* USE_HOSTCC */
3137 #endif /* CONFIG_FIT */