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", },
96 { IH_ARCH_NDS32, "nds32", "NDS32", },
100 static const table_entry_t uimage_os[] = {
101 { IH_OS_INVALID, NULL, "Invalid OS", },
102 { IH_OS_LINUX, "linux", "Linux", },
103 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
104 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
106 { IH_OS_NETBSD, "netbsd", "NetBSD", },
107 { IH_OS_OSE, "ose", "Enea OSE", },
108 { IH_OS_RTEMS, "rtems", "RTEMS", },
109 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
110 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
111 { IH_OS_QNX, "qnx", "QNX", },
112 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
114 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
115 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
118 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
119 { IH_OS_DELL, "dell", "Dell", },
120 { IH_OS_ESIX, "esix", "Esix", },
121 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
122 { IH_OS_IRIX, "irix", "Irix", },
123 { IH_OS_NCR, "ncr", "NCR", },
124 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
125 { IH_OS_PSOS, "psos", "pSOS", },
126 { IH_OS_SCO, "sco", "SCO", },
127 { IH_OS_SOLARIS, "solaris", "Solaris", },
128 { IH_OS_SVR4, "svr4", "SVR4", },
133 static const table_entry_t uimage_type[] = {
134 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
135 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
136 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
137 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
138 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
139 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
140 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
141 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
142 { IH_TYPE_INVALID, NULL, "Invalid Image", },
143 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
144 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
145 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
146 { IH_TYPE_SCRIPT, "script", "Script", },
147 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
148 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
152 static const table_entry_t uimage_comp[] = {
153 { IH_COMP_NONE, "none", "uncompressed", },
154 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
155 { IH_COMP_GZIP, "gzip", "gzip compressed", },
156 { IH_COMP_LZMA, "lzma", "lzma compressed", },
157 { IH_COMP_LZO, "lzo", "lzo compressed", },
161 uint32_t crc32(uint32_t, const unsigned char *, uint);
162 uint32_t crc32_wd(uint32_t, const unsigned char *, uint, uint);
163 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
164 static void genimg_print_time(time_t timestamp);
167 /*****************************************************************************/
168 /* Legacy format routines */
169 /*****************************************************************************/
170 int image_check_hcrc(const image_header_t *hdr)
173 ulong len = image_get_header_size();
174 image_header_t header;
176 /* Copy header so we can blank CRC field for re-calculation */
177 memmove(&header, (char *)hdr, image_get_header_size());
178 image_set_hcrc(&header, 0);
180 hcrc = crc32(0, (unsigned char *)&header, len);
182 return (hcrc == image_get_hcrc(hdr));
185 int image_check_dcrc(const image_header_t *hdr)
187 ulong data = image_get_data(hdr);
188 ulong len = image_get_data_size(hdr);
189 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
191 return (dcrc == image_get_dcrc(hdr));
195 * image_multi_count - get component (sub-image) count
196 * @hdr: pointer to the header of the multi component image
198 * image_multi_count() returns number of components in a multi
201 * Note: no checking of the image type is done, caller must pass
202 * a valid multi component image.
205 * number of components
207 ulong image_multi_count(const image_header_t *hdr)
212 /* get start of the image payload, which in case of multi
213 * component images that points to a table of component sizes */
214 size = (uint32_t *)image_get_data(hdr);
216 /* count non empty slots */
217 for (i = 0; size[i]; ++i)
224 * image_multi_getimg - get component data address and size
225 * @hdr: pointer to the header of the multi component image
226 * @idx: index of the requested component
227 * @data: pointer to a ulong variable, will hold component data address
228 * @len: pointer to a ulong variable, will hold component size
230 * image_multi_getimg() returns size and data address for the requested
231 * component in a multi component image.
233 * Note: no checking of the image type is done, caller must pass
234 * a valid multi component image.
237 * data address and size of the component, if idx is valid
238 * 0 in data and len, if idx is out of range
240 void image_multi_getimg(const image_header_t *hdr, ulong idx,
241 ulong *data, ulong *len)
245 ulong offset, count, img_data;
247 /* get number of component */
248 count = image_multi_count(hdr);
250 /* get start of the image payload, which in case of multi
251 * component images that points to a table of component sizes */
252 size = (uint32_t *)image_get_data(hdr);
254 /* get address of the proper component data start, which means
255 * skipping sizes table (add 1 for last, null entry) */
256 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
259 *len = uimage_to_cpu(size[idx]);
262 /* go over all indices preceding requested component idx */
263 for (i = 0; i < idx; i++) {
264 /* add up i-th component size, rounding up to 4 bytes */
265 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
268 /* calculate idx-th component data address */
269 *data = img_data + offset;
276 static void image_print_type(const image_header_t *hdr)
278 const char *os, *arch, *type, *comp;
280 os = genimg_get_os_name(image_get_os(hdr));
281 arch = genimg_get_arch_name(image_get_arch(hdr));
282 type = genimg_get_type_name(image_get_type(hdr));
283 comp = genimg_get_comp_name(image_get_comp(hdr));
285 printf("%s %s %s (%s)\n", arch, os, type, comp);
289 * image_print_contents - prints out the contents of the legacy format image
290 * @ptr: pointer to the legacy format image header
291 * @p: pointer to prefix string
293 * image_print_contents() formats a multi line legacy image contents description.
294 * The routine prints out all header fields followed by the size/offset data
295 * for MULTI/SCRIPT images.
298 * no returned results
300 void image_print_contents(const void *ptr)
302 const image_header_t *hdr = (const image_header_t *)ptr;
311 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
312 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
313 printf("%sCreated: ", p);
314 genimg_print_time((time_t)image_get_time(hdr));
316 printf("%sImage Type: ", p);
317 image_print_type(hdr);
318 printf("%sData Size: ", p);
319 genimg_print_size(image_get_data_size(hdr));
320 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
321 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
323 if (image_check_type(hdr, IH_TYPE_MULTI) ||
324 image_check_type(hdr, IH_TYPE_SCRIPT)) {
327 ulong count = image_multi_count(hdr);
329 printf("%sContents:\n", p);
330 for (i = 0; i < count; i++) {
331 image_multi_getimg(hdr, i, &data, &len);
333 printf("%s Image %d: ", p, i);
334 genimg_print_size(len);
336 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
338 * the user may need to know offsets
339 * if planning to do something with
342 printf("%s Offset = 0x%08lx\n", p, data);
351 * image_get_ramdisk - get and verify ramdisk image
352 * @rd_addr: ramdisk image start address
353 * @arch: expected ramdisk architecture
354 * @verify: checksum verification flag
356 * image_get_ramdisk() returns a pointer to the verified ramdisk image
357 * header. Routine receives image start address and expected architecture
358 * flag. Verification done covers data and header integrity and os/type/arch
361 * If dataflash support is enabled routine checks for dataflash addresses
362 * and handles required dataflash reads.
365 * pointer to a ramdisk image header, if image was found and valid
366 * otherwise, return NULL
368 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
371 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
373 if (!image_check_magic(rd_hdr)) {
374 puts("Bad Magic Number\n");
375 show_boot_progress(-10);
379 if (!image_check_hcrc(rd_hdr)) {
380 puts("Bad Header Checksum\n");
381 show_boot_progress(-11);
385 show_boot_progress(10);
386 image_print_contents(rd_hdr);
389 puts(" Verifying Checksum ... ");
390 if (!image_check_dcrc(rd_hdr)) {
391 puts("Bad Data CRC\n");
392 show_boot_progress(-12);
398 show_boot_progress(11);
400 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
401 !image_check_arch(rd_hdr, arch) ||
402 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
403 printf("No Linux %s Ramdisk Image\n",
404 genimg_get_arch_name(arch));
405 show_boot_progress(-13);
411 #endif /* !USE_HOSTCC */
413 /*****************************************************************************/
414 /* Shared dual-format routines */
415 /*****************************************************************************/
417 int getenv_yesno(char *var)
419 char *s = getenv(var);
420 return (s && (*s == 'n')) ? 0 : 1;
423 ulong getenv_bootm_low(void)
425 char *s = getenv("bootm_low");
427 ulong tmp = simple_strtoul(s, NULL, 16);
431 #if defined(CONFIG_SYS_SDRAM_BASE)
432 return CONFIG_SYS_SDRAM_BASE;
433 #elif defined(CONFIG_ARM)
434 return gd->bd->bi_dram[0].start;
440 phys_size_t getenv_bootm_size(void)
443 char *s = getenv("bootm_size");
445 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
448 s = getenv("bootm_low");
450 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
455 #if defined(CONFIG_ARM)
456 return gd->bd->bi_dram[0].size - tmp;
458 return gd->bd->bi_memsize - tmp;
462 phys_size_t getenv_bootm_mapsize(void)
465 char *s = getenv("bootm_mapsize");
467 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
471 #if defined(CONFIG_SYS_BOOTMAPSZ)
472 return CONFIG_SYS_BOOTMAPSZ;
474 return getenv_bootm_size();
478 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
483 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
485 size_t tail = (len > chunksz) ? chunksz : len;
487 memmove(to, from, tail);
492 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
493 memmove(to, from, len);
494 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
496 #endif /* !USE_HOSTCC */
498 void genimg_print_size(uint32_t size)
501 printf("%d Bytes = ", size);
502 print_size(size, "\n");
504 printf("%d Bytes = %.2f kB = %.2f MB\n",
505 size, (double)size / 1.024e3,
506 (double)size / 1.048576e6);
510 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
511 static void genimg_print_time(time_t timestamp)
516 to_tm(timestamp, &tm);
517 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
518 tm.tm_year, tm.tm_mon, tm.tm_mday,
519 tm.tm_hour, tm.tm_min, tm.tm_sec);
521 printf("%s", ctime(×tamp));
524 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
527 * get_table_entry_name - translate entry id to long name
528 * @table: pointer to a translation table for entries of a specific type
529 * @msg: message to be returned when translation fails
530 * @id: entry id to be translated
532 * get_table_entry_name() will go over translation table trying to find
533 * entry that matches given id. If matching entry is found, its long
534 * name is returned to the caller.
537 * long entry name if translation succeeds
540 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
542 for (; table->id >= 0; ++table) {
544 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
547 return table->lname + gd->reloc_off;
553 const char *genimg_get_os_name(uint8_t os)
555 return (get_table_entry_name(uimage_os, "Unknown OS", os));
558 const char *genimg_get_arch_name(uint8_t arch)
560 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
564 const char *genimg_get_type_name(uint8_t type)
566 return (get_table_entry_name(uimage_type, "Unknown Image", type));
569 const char *genimg_get_comp_name(uint8_t comp)
571 return (get_table_entry_name(uimage_comp, "Unknown Compression",
576 * get_table_entry_id - translate short entry name to id
577 * @table: pointer to a translation table for entries of a specific type
578 * @table_name: to be used in case of error
579 * @name: entry short name to be translated
581 * get_table_entry_id() will go over translation table trying to find
582 * entry that matches given short name. If matching entry is found,
583 * its id returned to the caller.
586 * entry id if translation succeeds
589 int get_table_entry_id(const table_entry_t *table,
590 const char *table_name, const char *name)
592 const table_entry_t *t;
596 for (t = table; t->id >= 0; ++t) {
597 if (t->sname && strcasecmp(t->sname, name) == 0)
601 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
602 for (t = table; t->id >= 0; ++t) {
603 if (t->sname == NULL)
605 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
608 fprintf(stderr, "\n");
610 for (t = table; t->id >= 0; ++t) {
611 #ifdef CONFIG_NEEDS_MANUAL_RELOC
612 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
614 if (t->sname && strcmp(t->sname, name) == 0)
618 debug("Invalid %s Type: %s\n", table_name, name);
619 #endif /* USE_HOSTCC */
623 int genimg_get_os_id(const char *name)
625 return (get_table_entry_id(uimage_os, "OS", name));
628 int genimg_get_arch_id(const char *name)
630 return (get_table_entry_id(uimage_arch, "CPU", name));
633 int genimg_get_type_id(const char *name)
635 return (get_table_entry_id(uimage_type, "Image", name));
638 int genimg_get_comp_id(const char *name)
640 return (get_table_entry_id(uimage_comp, "Compression", name));
645 * genimg_get_format - get image format type
646 * @img_addr: image start address
648 * genimg_get_format() checks whether provided address points to a valid
649 * legacy or FIT image.
651 * New uImage format and FDT blob are based on a libfdt. FDT blob
652 * may be passed directly or embedded in a FIT image. In both situations
653 * genimg_get_format() must be able to dectect libfdt header.
656 * image format type or IMAGE_FORMAT_INVALID if no image is present
658 int genimg_get_format(void *img_addr)
660 ulong format = IMAGE_FORMAT_INVALID;
661 const image_header_t *hdr;
662 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
666 hdr = (const image_header_t *)img_addr;
667 if (image_check_magic(hdr))
668 format = IMAGE_FORMAT_LEGACY;
669 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
671 fit_hdr = (char *)img_addr;
672 if (fdt_check_header(fit_hdr) == 0)
673 format = IMAGE_FORMAT_FIT;
681 * genimg_get_image - get image from special storage (if necessary)
682 * @img_addr: image start address
684 * genimg_get_image() checks if provided image start adddress is located
685 * in a dataflash storage. If so, image is moved to a system RAM memory.
688 * image start address after possible relocation from special storage
690 ulong genimg_get_image(ulong img_addr)
692 ulong ram_addr = img_addr;
694 #ifdef CONFIG_HAS_DATAFLASH
695 ulong h_size, d_size;
697 if (addr_dataflash(img_addr)) {
698 /* ger RAM address */
699 ram_addr = CONFIG_SYS_LOAD_ADDR;
701 /* get header size */
702 h_size = image_get_header_size();
703 #if defined(CONFIG_FIT)
704 if (sizeof(struct fdt_header) > h_size)
705 h_size = sizeof(struct fdt_header);
709 debug(" Reading image header from dataflash address "
710 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
712 read_dataflash(img_addr, h_size, (char *)ram_addr);
715 switch (genimg_get_format((void *)ram_addr)) {
716 case IMAGE_FORMAT_LEGACY:
717 d_size = image_get_data_size(
718 (const image_header_t *)ram_addr);
719 debug(" Legacy format image found at 0x%08lx, "
723 #if defined(CONFIG_FIT)
724 case IMAGE_FORMAT_FIT:
725 d_size = fit_get_size((const void *)ram_addr) - h_size;
726 debug(" FIT/FDT format image found at 0x%08lx, "
732 printf(" No valid image found at 0x%08lx\n",
737 /* read in image data */
738 debug(" Reading image remaining data from dataflash address "
739 "%08lx to RAM address %08lx\n", img_addr + h_size,
742 read_dataflash(img_addr + h_size, d_size,
743 (char *)(ram_addr + h_size));
746 #endif /* CONFIG_HAS_DATAFLASH */
752 * fit_has_config - check if there is a valid FIT configuration
753 * @images: pointer to the bootm command headers structure
755 * fit_has_config() checks if there is a FIT configuration in use
756 * (if FTI support is present).
759 * 0, no FIT support or no configuration found
760 * 1, configuration found
762 int genimg_has_config(bootm_headers_t *images)
764 #if defined(CONFIG_FIT)
765 if (images->fit_uname_cfg)
772 * boot_get_ramdisk - main ramdisk handling routine
773 * @argc: command argument count
774 * @argv: command argument list
775 * @images: pointer to the bootm images structure
776 * @arch: expected ramdisk architecture
777 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
778 * @rd_end: pointer to a ulong variable, will hold ramdisk end
780 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
781 * Curently supported are the following ramdisk sources:
782 * - multicomponent kernel/ramdisk image,
783 * - commandline provided address of decicated ramdisk image.
786 * 0, if ramdisk image was found and valid, or skiped
787 * rd_start and rd_end are set to ramdisk start/end addresses if
788 * ramdisk image is found and valid
790 * 1, if ramdisk image is found but corrupted, or invalid
791 * rd_start and rd_end are set to 0 if no ramdisk exists
793 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
794 uint8_t arch, ulong *rd_start, ulong *rd_end)
796 ulong rd_addr, rd_load;
797 ulong rd_data, rd_len;
798 const image_header_t *rd_hdr;
799 #if defined(CONFIG_FIT)
801 const char *fit_uname_config = NULL;
802 const char *fit_uname_ramdisk = NULL;
814 * Look for a '-' which indicates to ignore the
817 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
818 debug("## Skipping init Ramdisk\n");
819 rd_len = rd_data = 0;
820 } else if (argc >= 3 || genimg_has_config(images)) {
821 #if defined(CONFIG_FIT)
824 * If the init ramdisk comes from the FIT image and
825 * the FIT image address is omitted in the command
826 * line argument, try to use os FIT image address or
827 * default load address.
829 if (images->fit_uname_os)
830 default_addr = (ulong)images->fit_hdr_os;
832 default_addr = load_addr;
834 if (fit_parse_conf(argv[2], default_addr,
835 &rd_addr, &fit_uname_config)) {
836 debug("* ramdisk: config '%s' from image at "
838 fit_uname_config, rd_addr);
839 } else if (fit_parse_subimage(argv[2], default_addr,
840 &rd_addr, &fit_uname_ramdisk)) {
841 debug("* ramdisk: subimage '%s' from image at "
843 fit_uname_ramdisk, rd_addr);
847 rd_addr = simple_strtoul(argv[2], NULL, 16);
848 debug("* ramdisk: cmdline image address = "
852 #if defined(CONFIG_FIT)
854 /* use FIT configuration provided in first bootm
857 rd_addr = (ulong)images->fit_hdr_os;
858 fit_uname_config = images->fit_uname_cfg;
859 debug("* ramdisk: using config '%s' from image "
861 fit_uname_config, rd_addr);
864 * Check whether configuration has ramdisk defined,
865 * if not, don't try to use it, quit silently.
867 fit_hdr = (void *)rd_addr;
868 cfg_noffset = fit_conf_get_node(fit_hdr,
870 if (cfg_noffset < 0) {
871 debug("* ramdisk: no such config\n");
875 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
877 if (rd_noffset < 0) {
878 debug("* ramdisk: no ramdisk in config\n");
884 /* copy from dataflash if needed */
885 rd_addr = genimg_get_image(rd_addr);
888 * Check if there is an initrd image at the
889 * address provided in the second bootm argument
890 * check image type, for FIT images get FIT node.
892 switch (genimg_get_format((void *)rd_addr)) {
893 case IMAGE_FORMAT_LEGACY:
894 printf("## Loading init Ramdisk from Legacy "
895 "Image at %08lx ...\n", rd_addr);
897 show_boot_progress(9);
898 rd_hdr = image_get_ramdisk(rd_addr, arch,
904 rd_data = image_get_data(rd_hdr);
905 rd_len = image_get_data_size(rd_hdr);
906 rd_load = image_get_load(rd_hdr);
908 #if defined(CONFIG_FIT)
909 case IMAGE_FORMAT_FIT:
910 fit_hdr = (void *)rd_addr;
911 printf("## Loading init Ramdisk from FIT "
912 "Image at %08lx ...\n", rd_addr);
914 show_boot_progress(120);
915 if (!fit_check_format(fit_hdr)) {
916 puts("Bad FIT ramdisk image format!\n");
917 show_boot_progress(-120);
920 show_boot_progress(121);
922 if (!fit_uname_ramdisk) {
924 * no ramdisk image node unit name, try to get config
925 * node first. If config unit node name is NULL
926 * fit_conf_get_node() will try to find default config node
928 show_boot_progress(122);
929 cfg_noffset = fit_conf_get_node(fit_hdr,
931 if (cfg_noffset < 0) {
932 puts("Could not find configuration "
934 show_boot_progress(-122);
937 fit_uname_config = fdt_get_name(fit_hdr,
939 printf(" Using '%s' configuration\n",
942 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
944 fit_uname_ramdisk = fit_get_name(fit_hdr,
947 /* get ramdisk component image node offset */
948 show_boot_progress(123);
949 rd_noffset = fit_image_get_node(fit_hdr,
952 if (rd_noffset < 0) {
953 puts("Could not find subimage node\n");
954 show_boot_progress(-124);
958 printf(" Trying '%s' ramdisk subimage\n",
961 show_boot_progress(125);
962 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
966 /* get ramdisk image data address and length */
967 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
969 puts("Could not find ramdisk subimage data!\n");
970 show_boot_progress(-127);
973 show_boot_progress(128);
975 rd_data = (ulong)data;
978 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
979 puts("Can't get ramdisk subimage load "
981 show_boot_progress(-129);
984 show_boot_progress(129);
986 images->fit_hdr_rd = fit_hdr;
987 images->fit_uname_rd = fit_uname_ramdisk;
988 images->fit_noffset_rd = rd_noffset;
992 puts("Wrong Ramdisk Image Format\n");
993 rd_data = rd_len = rd_load = 0;
996 } else if (images->legacy_hdr_valid &&
997 image_check_type(&images->legacy_hdr_os_copy,
1001 * Now check if we have a legacy mult-component image,
1002 * get second entry data start address and len.
1004 show_boot_progress(13);
1005 printf("## Loading init Ramdisk from multi component "
1006 "Legacy Image at %08lx ...\n",
1007 (ulong)images->legacy_hdr_os);
1009 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1014 show_boot_progress(14);
1015 rd_len = rd_data = 0;
1019 debug("## No init Ramdisk\n");
1021 *rd_start = rd_data;
1022 *rd_end = rd_data + rd_len;
1024 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1025 *rd_start, *rd_end);
1030 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1032 * boot_ramdisk_high - relocate init ramdisk
1033 * @lmb: pointer to lmb handle, will be used for memory mgmt
1034 * @rd_data: ramdisk data start address
1035 * @rd_len: ramdisk data length
1036 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1037 * start address (after possible relocation)
1038 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1039 * end address (after possible relocation)
1041 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1042 * variable and if requested ramdisk data is moved to a specified location.
1044 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1045 * start/end addresses if ramdisk image start and len were provided,
1046 * otherwise set initrd_start and initrd_end set to zeros.
1052 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1053 ulong *initrd_start, ulong *initrd_end)
1057 int initrd_copy_to_ram = 1;
1059 if ((s = getenv("initrd_high")) != NULL) {
1060 /* a value of "no" or a similar string will act like 0,
1061 * turning the "load high" feature off. This is intentional.
1063 initrd_high = simple_strtoul(s, NULL, 16);
1064 if (initrd_high == ~0)
1065 initrd_copy_to_ram = 0;
1067 /* not set, no restrictions to load high */
1072 #ifdef CONFIG_LOGBUFFER
1073 /* Prevent initrd from overwriting logbuffer */
1074 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1077 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1078 initrd_high, initrd_copy_to_ram);
1081 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1082 debug(" in-place initrd\n");
1083 *initrd_start = rd_data;
1084 *initrd_end = rd_data + rd_len;
1085 lmb_reserve(lmb, rd_data, rd_len);
1088 *initrd_start = (ulong)lmb_alloc_base(lmb,
1089 rd_len, 0x1000, initrd_high);
1091 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1094 if (*initrd_start == 0) {
1095 puts("ramdisk - allocation error\n");
1098 show_boot_progress(12);
1100 *initrd_end = *initrd_start + rd_len;
1101 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
1102 *initrd_start, *initrd_end);
1104 memmove_wd((void *)*initrd_start,
1105 (void *)rd_data, rd_len, CHUNKSZ);
1109 * Ensure the image is flushed to memory to handle
1110 * AMP boot scenarios in which we might not be
1113 flush_cache((unsigned long)*initrd_start, rd_len);
1121 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1122 *initrd_start, *initrd_end);
1129 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1131 #ifdef CONFIG_OF_LIBFDT
1132 static void fdt_error(const char *msg)
1136 puts(" - must RESET the board to recover.\n");
1139 static const image_header_t *image_get_fdt(ulong fdt_addr)
1141 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1143 image_print_contents(fdt_hdr);
1145 puts(" Verifying Checksum ... ");
1146 if (!image_check_hcrc(fdt_hdr)) {
1147 fdt_error("fdt header checksum invalid");
1151 if (!image_check_dcrc(fdt_hdr)) {
1152 fdt_error("fdt checksum invalid");
1157 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1158 fdt_error("uImage is not a fdt");
1161 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1162 fdt_error("uImage is compressed");
1165 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1166 fdt_error("uImage data is not a fdt");
1173 * fit_check_fdt - verify FIT format FDT subimage
1174 * @fit_hdr: pointer to the FIT header
1175 * fdt_noffset: FDT subimage node offset within FIT image
1176 * @verify: data CRC verification flag
1178 * fit_check_fdt() verifies integrity of the FDT subimage and from
1179 * specified FIT image.
1185 #if defined(CONFIG_FIT)
1186 static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
1188 fit_image_print(fit, fdt_noffset, " ");
1191 puts(" Verifying Hash Integrity ... ");
1192 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1193 fdt_error("Bad Data Hash");
1199 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1200 fdt_error("Not a FDT image");
1204 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1205 fdt_error("FDT image is compressed");
1211 #endif /* CONFIG_FIT */
1213 #ifndef CONFIG_SYS_FDT_PAD
1214 #define CONFIG_SYS_FDT_PAD 0x3000
1217 #if defined(CONFIG_OF_LIBFDT)
1219 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1220 * @lmb: pointer to lmb handle, will be used for memory mgmt
1221 * @fdt_blob: pointer to fdt blob base address
1223 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1224 * memreserve regions prevents u-boot from using them to store the initrd
1227 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1229 uint64_t addr, size;
1232 if (fdt_check_header(fdt_blob) != 0)
1235 total = fdt_num_mem_rsv(fdt_blob);
1236 for (i = 0; i < total; i++) {
1237 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1239 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1240 (unsigned long long)addr, (unsigned long long)size);
1241 lmb_reserve(lmb, addr, size);
1246 * boot_relocate_fdt - relocate flat device tree
1247 * @lmb: pointer to lmb handle, will be used for memory mgmt
1248 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1249 * @of_size: pointer to a ulong variable, will hold fdt length
1251 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1252 * relocates the of_flat_tree into that region, even if the fdt is already in
1253 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1256 * of_flat_tree and of_size are set to final (after relocation) values
1262 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1264 void *fdt_blob = *of_flat_tree;
1269 int disable_relocation = 0;
1275 if (fdt_check_header(fdt_blob) != 0) {
1276 fdt_error("image is not a fdt");
1280 /* position on a 4K boundary before the alloc_current */
1281 /* Pad the FDT by a specified amount */
1282 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1284 /* If fdt_high is set use it to select the relocation address */
1285 fdt_high = getenv("fdt_high");
1287 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1289 if (((ulong) desired_addr) == ~0UL) {
1290 /* All ones means use fdt in place */
1291 desired_addr = fdt_blob;
1292 disable_relocation = 1;
1296 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1300 if (desired_addr && of_start != desired_addr) {
1301 puts("Failed using fdt_high value for Device Tree");
1306 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
1310 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1311 getenv_bootm_mapsize()
1312 + getenv_bootm_low());
1315 if (of_start == 0) {
1316 puts("device tree - allocation error\n");
1320 if (disable_relocation) {
1321 /* We assume there is space after the existing fdt to use for padding */
1322 fdt_set_totalsize(of_start, of_len);
1323 printf(" Using Device Tree in place at %p, end %p\n",
1324 of_start, of_start + of_len - 1);
1326 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1327 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1329 printf(" Loading Device Tree to %p, end %p ... ",
1330 of_start, of_start + of_len - 1);
1332 err = fdt_open_into(fdt_blob, of_start, of_len);
1334 fdt_error("fdt move failed");
1340 *of_flat_tree = of_start;
1343 set_working_fdt_addr(*of_flat_tree);
1349 #endif /* CONFIG_OF_LIBFDT */
1352 * boot_get_fdt - main fdt handling routine
1353 * @argc: command argument count
1354 * @argv: command argument list
1355 * @images: pointer to the bootm images structure
1356 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1357 * @of_size: pointer to a ulong variable, will hold fdt length
1359 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1360 * Curently supported are the following ramdisk sources:
1361 * - multicomponent kernel/ramdisk image,
1362 * - commandline provided address of decicated ramdisk image.
1365 * 0, if fdt image was found and valid, or skipped
1366 * of_flat_tree and of_size are set to fdt start address and length if
1367 * fdt image is found and valid
1369 * 1, if fdt image is found but corrupted
1370 * of_flat_tree and of_size are set to 0 if no fdt exists
1372 int boot_get_fdt(int flag, int argc, char * const argv[],
1373 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
1375 const image_header_t *fdt_hdr;
1377 char *fdt_blob = NULL;
1378 ulong image_start, image_end;
1379 ulong load_start, load_end;
1380 #if defined(CONFIG_FIT)
1382 const char *fit_uname_config = NULL;
1383 const char *fit_uname_fdt = NULL;
1391 *of_flat_tree = NULL;
1394 if (argc > 3 || genimg_has_config(images)) {
1395 #if defined(CONFIG_FIT)
1398 * If the FDT blob comes from the FIT image and the
1399 * FIT image address is omitted in the command line
1400 * argument, try to use ramdisk or os FIT image
1401 * address or default load address.
1403 if (images->fit_uname_rd)
1404 default_addr = (ulong)images->fit_hdr_rd;
1405 else if (images->fit_uname_os)
1406 default_addr = (ulong)images->fit_hdr_os;
1408 default_addr = load_addr;
1410 if (fit_parse_conf(argv[3], default_addr,
1411 &fdt_addr, &fit_uname_config)) {
1412 debug("* fdt: config '%s' from image at "
1414 fit_uname_config, fdt_addr);
1415 } else if (fit_parse_subimage(argv[3], default_addr,
1416 &fdt_addr, &fit_uname_fdt)) {
1417 debug("* fdt: subimage '%s' from image at "
1419 fit_uname_fdt, fdt_addr);
1423 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1424 debug("* fdt: cmdline image address = "
1428 #if defined(CONFIG_FIT)
1430 /* use FIT configuration provided in first bootm
1433 fdt_addr = (ulong)images->fit_hdr_os;
1434 fit_uname_config = images->fit_uname_cfg;
1435 debug("* fdt: using config '%s' from image "
1437 fit_uname_config, fdt_addr);
1440 * Check whether configuration has FDT blob defined,
1441 * if not quit silently.
1443 fit_hdr = (void *)fdt_addr;
1444 cfg_noffset = fit_conf_get_node(fit_hdr,
1446 if (cfg_noffset < 0) {
1447 debug("* fdt: no such config\n");
1451 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
1453 if (fdt_noffset < 0) {
1454 debug("* fdt: no fdt in config\n");
1460 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1463 /* copy from dataflash if needed */
1464 fdt_addr = genimg_get_image(fdt_addr);
1467 * Check if there is an FDT image at the
1468 * address provided in the second bootm argument
1469 * check image type, for FIT images get a FIT node.
1471 switch (genimg_get_format((void *)fdt_addr)) {
1472 case IMAGE_FORMAT_LEGACY:
1473 /* verify fdt_addr points to a valid image header */
1474 printf("## Flattened Device Tree from Legacy Image "
1477 fdt_hdr = image_get_fdt(fdt_addr);
1482 * move image data to the load address,
1483 * make sure we don't overwrite initial image
1485 image_start = (ulong)fdt_hdr;
1486 image_end = image_get_image_end(fdt_hdr);
1488 load_start = image_get_load(fdt_hdr);
1489 load_end = load_start + image_get_data_size(fdt_hdr);
1491 if ((load_start < image_end) && (load_end > image_start)) {
1492 fdt_error("fdt overwritten");
1496 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
1497 image_get_data(fdt_hdr), load_start);
1499 memmove((void *)load_start,
1500 (void *)image_get_data(fdt_hdr),
1501 image_get_data_size(fdt_hdr));
1503 fdt_blob = (char *)load_start;
1505 case IMAGE_FORMAT_FIT:
1507 * This case will catch both: new uImage format
1508 * (libfdt based) and raw FDT blob (also libfdt
1511 #if defined(CONFIG_FIT)
1512 /* check FDT blob vs FIT blob */
1513 if (fit_check_format((const void *)fdt_addr)) {
1517 fit_hdr = (void *)fdt_addr;
1518 printf("## Flattened Device Tree from FIT "
1522 if (!fit_uname_fdt) {
1524 * no FDT blob image node unit name,
1525 * try to get config node first. If
1526 * config unit node name is NULL
1527 * fit_conf_get_node() will try to
1528 * find default config node
1530 cfg_noffset = fit_conf_get_node(fit_hdr,
1533 if (cfg_noffset < 0) {
1534 fdt_error("Could not find "
1540 fit_uname_config = fdt_get_name(fit_hdr,
1542 printf(" Using '%s' configuration\n",
1545 fdt_noffset = fit_conf_get_fdt_node(
1548 fit_uname_fdt = fit_get_name(fit_hdr,
1551 /* get FDT component image node offset */
1552 fdt_noffset = fit_image_get_node(
1556 if (fdt_noffset < 0) {
1557 fdt_error("Could not find subimage "
1562 printf(" Trying '%s' FDT blob subimage\n",
1565 if (!fit_check_fdt(fit_hdr, fdt_noffset,
1569 /* get ramdisk image data address and length */
1570 if (fit_image_get_data(fit_hdr, fdt_noffset,
1572 fdt_error("Could not find FDT "
1577 /* verift that image data is a proper FDT blob */
1578 if (fdt_check_header((char *)data) != 0) {
1579 fdt_error("Subimage data is not a FTD");
1584 * move image data to the load address,
1585 * make sure we don't overwrite initial image
1587 image_start = (ulong)fit_hdr;
1588 image_end = fit_get_end(fit_hdr);
1590 if (fit_image_get_load(fit_hdr, fdt_noffset,
1591 &load_start) == 0) {
1592 load_end = load_start + size;
1594 if ((load_start < image_end) &&
1595 (load_end > image_start)) {
1596 fdt_error("FDT overwritten");
1600 printf(" Loading FDT from 0x%08lx "
1605 memmove((void *)load_start,
1606 (void *)data, size);
1608 fdt_blob = (char *)load_start;
1610 fdt_blob = (char *)data;
1613 images->fit_hdr_fdt = fit_hdr;
1614 images->fit_uname_fdt = fit_uname_fdt;
1615 images->fit_noffset_fdt = fdt_noffset;
1623 fdt_blob = (char *)fdt_addr;
1624 debug("* fdt: raw FDT blob\n");
1625 printf("## Flattened Device Tree blob at "
1626 "%08lx\n", (long)fdt_blob);
1630 puts("ERROR: Did not find a cmdline Flattened Device "
1635 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
1637 } else if (images->legacy_hdr_valid &&
1638 image_check_type(&images->legacy_hdr_os_copy,
1641 ulong fdt_data, fdt_len;
1644 * Now check if we have a legacy multi-component image,
1645 * get second entry data start address and len.
1647 printf("## Flattened Device Tree from multi "
1648 "component Image at %08lX\n",
1649 (ulong)images->legacy_hdr_os);
1651 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1655 fdt_blob = (char *)fdt_data;
1656 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
1658 if (fdt_check_header(fdt_blob) != 0) {
1659 fdt_error("image is not a fdt");
1663 if (fdt_totalsize(fdt_blob) != fdt_len) {
1664 fdt_error("fdt size != image size");
1668 debug("## No Flattened Device Tree\n");
1672 debug("## No Flattened Device Tree\n");
1676 *of_flat_tree = fdt_blob;
1677 *of_size = fdt_totalsize(fdt_blob);
1678 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
1679 (ulong)*of_flat_tree, *of_size);
1688 #endif /* CONFIG_OF_LIBFDT */
1690 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1692 * boot_get_cmdline - allocate and initialize kernel cmdline
1693 * @lmb: pointer to lmb handle, will be used for memory mgmt
1694 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1695 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1697 * boot_get_cmdline() allocates space for kernel command line below
1698 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1699 * variable is present its contents is copied to allocated kernel
1706 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1711 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1712 getenv_bootm_mapsize() + getenv_bootm_low());
1714 if (cmdline == NULL)
1717 if ((s = getenv("bootargs")) == NULL)
1722 *cmd_start = (ulong) & cmdline[0];
1723 *cmd_end = *cmd_start + strlen(cmdline);
1725 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1729 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1731 #ifdef CONFIG_SYS_BOOT_GET_KBD
1733 * boot_get_kbd - allocate and initialize kernel copy of board info
1734 * @lmb: pointer to lmb handle, will be used for memory mgmt
1735 * @kbd: double pointer to board info data
1737 * boot_get_kbd() allocates space for kernel copy of board info data below
1738 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1739 * with the current u-boot board info data.
1745 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1747 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1748 getenv_bootm_mapsize() + getenv_bootm_low());
1754 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1756 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1757 do_bdinfo(NULL, 0, 0, NULL);
1762 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1763 #endif /* !USE_HOSTCC */
1765 #if defined(CONFIG_FIT)
1766 /*****************************************************************************/
1767 /* New uImage format routines */
1768 /*****************************************************************************/
1770 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
1771 ulong *addr, const char **name)
1778 sep = strchr(spec, sepc);
1781 *addr = simple_strtoul(spec, NULL, 16);
1791 * fit_parse_conf - parse FIT configuration spec
1792 * @spec: input string, containing configuration spec
1793 * @add_curr: current image address (to be used as a possible default)
1794 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1796 * @conf_name double pointer to a char, will hold pointer to a configuration
1799 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1800 * where <addr> is a FIT image address that contains configuration
1801 * with a <conf> unit name.
1803 * Address part is optional, and if omitted default add_curr will
1807 * 1 if spec is a valid configuration string,
1808 * addr and conf_name are set accordingly
1811 inline int fit_parse_conf(const char *spec, ulong addr_curr,
1812 ulong *addr, const char **conf_name)
1814 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
1818 * fit_parse_subimage - parse FIT subimage spec
1819 * @spec: input string, containing subimage spec
1820 * @add_curr: current image address (to be used as a possible default)
1821 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1823 * @image_name: double pointer to a char, will hold pointer to a subimage name
1825 * fit_parse_subimage() expects subimage spec in the for of
1826 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1827 * subimage with a <subimg> unit name.
1829 * Address part is optional, and if omitted default add_curr will
1833 * 1 if spec is a valid subimage string,
1834 * addr and image_name are set accordingly
1837 inline int fit_parse_subimage(const char *spec, ulong addr_curr,
1838 ulong *addr, const char **image_name)
1840 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
1842 #endif /* !USE_HOSTCC */
1844 static void fit_get_debug(const void *fit, int noffset,
1845 char *prop_name, int err)
1847 debug("Can't get '%s' property from FIT 0x%08lx, "
1848 "node: offset %d, name %s (%s)\n",
1849 prop_name, (ulong)fit, noffset,
1850 fit_get_name(fit, noffset, NULL),
1855 * fit_print_contents - prints out the contents of the FIT format image
1856 * @fit: pointer to the FIT format image header
1857 * @p: pointer to prefix string
1859 * fit_print_contents() formats a multi line FIT image contents description.
1860 * The routine prints out FIT image properties (root node level) follwed by
1861 * the details of each component image.
1864 * no returned results
1866 void fit_print_contents(const void *fit)
1877 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1887 /* Root node properties */
1888 ret = fit_get_desc(fit, 0, &desc);
1889 printf("%sFIT description: ", p);
1891 printf("unavailable\n");
1893 printf("%s\n", desc);
1895 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1896 ret = fit_get_timestamp(fit, 0, ×tamp);
1897 printf("%sCreated: ", p);
1899 printf("unavailable\n");
1901 genimg_print_time(timestamp);
1904 /* Find images parent node offset */
1905 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1906 if (images_noffset < 0) {
1907 printf("Can't find images parent node '%s' (%s)\n",
1908 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1912 /* Process its subnodes, print out component images details */
1913 for (ndepth = 0, count = 0,
1914 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1915 (noffset >= 0) && (ndepth > 0);
1916 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1919 * Direct child node of the images parent node,
1920 * i.e. component image node.
1922 printf("%s Image %u (%s)\n", p, count++,
1923 fit_get_name(fit, noffset, NULL));
1925 fit_image_print(fit, noffset, p);
1929 /* Find configurations parent node offset */
1930 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1931 if (confs_noffset < 0) {
1932 debug("Can't get configurations parent node '%s' (%s)\n",
1933 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1937 /* get default configuration unit name from default property */
1938 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
1940 printf("%s Default Configuration: '%s'\n", p, uname);
1942 /* Process its subnodes, print out configurations details */
1943 for (ndepth = 0, count = 0,
1944 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1945 (noffset >= 0) && (ndepth > 0);
1946 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1949 * Direct child node of the configurations parent node,
1950 * i.e. configuration node.
1952 printf("%s Configuration %u (%s)\n", p, count++,
1953 fit_get_name(fit, noffset, NULL));
1955 fit_conf_print(fit, noffset, p);
1961 * fit_image_print - prints out the FIT component image details
1962 * @fit: pointer to the FIT format image header
1963 * @image_noffset: offset of the component image node
1964 * @p: pointer to prefix string
1966 * fit_image_print() lists all mandatory properies for the processed component
1967 * image. If present, hash nodes are printed out as well. Load
1968 * address for images of type firmware is also printed out. Since the load
1969 * address is not mandatory for firmware images, it will be output as
1970 * "unavailable" when not present.
1973 * no returned results
1975 void fit_image_print(const void *fit, int image_noffset, const char *p)
1978 uint8_t type, arch, os, comp;
1986 /* Mandatory properties */
1987 ret = fit_get_desc(fit, image_noffset, &desc);
1988 printf("%s Description: ", p);
1990 printf("unavailable\n");
1992 printf("%s\n", desc);
1994 fit_image_get_type(fit, image_noffset, &type);
1995 printf("%s Type: %s\n", p, genimg_get_type_name(type));
1997 fit_image_get_comp(fit, image_noffset, &comp);
1998 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
2000 ret = fit_image_get_data(fit, image_noffset, &data, &size);
2003 printf("%s Data Start: ", p);
2005 printf("unavailable\n");
2007 printf("0x%08lx\n", (ulong)data);
2010 printf("%s Data Size: ", p);
2012 printf("unavailable\n");
2014 genimg_print_size(size);
2016 /* Remaining, type dependent properties */
2017 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2018 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2019 (type == IH_TYPE_FLATDT)) {
2020 fit_image_get_arch(fit, image_noffset, &arch);
2021 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
2024 if (type == IH_TYPE_KERNEL) {
2025 fit_image_get_os(fit, image_noffset, &os);
2026 printf("%s OS: %s\n", p, genimg_get_os_name(os));
2029 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2030 (type == IH_TYPE_FIRMWARE)) {
2031 ret = fit_image_get_load(fit, image_noffset, &load);
2032 printf("%s Load Address: ", p);
2034 printf("unavailable\n");
2036 printf("0x%08lx\n", load);
2039 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
2040 fit_image_get_entry(fit, image_noffset, &entry);
2041 printf("%s Entry Point: ", p);
2043 printf("unavailable\n");
2045 printf("0x%08lx\n", entry);
2048 /* Process all hash subnodes of the component image node */
2049 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2050 (noffset >= 0) && (ndepth > 0);
2051 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2053 /* Direct child node of the component image node */
2054 fit_image_print_hash(fit, noffset, p);
2060 * fit_image_print_hash - prints out the hash node details
2061 * @fit: pointer to the FIT format image header
2062 * @noffset: offset of the hash node
2063 * @p: pointer to prefix string
2065 * fit_image_print_hash() lists properies for the processed hash node
2068 * no returned results
2070 void fit_image_print_hash(const void *fit, int noffset, const char *p)
2078 * Check subnode name, must be equal to "hash".
2079 * Multiple hash nodes require unique unit node
2080 * names, e.g. hash@1, hash@2, etc.
2082 if (strncmp(fit_get_name(fit, noffset, NULL),
2084 strlen(FIT_HASH_NODENAME)) != 0)
2087 debug("%s Hash node: '%s'\n", p,
2088 fit_get_name(fit, noffset, NULL));
2090 printf("%s Hash algo: ", p);
2091 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2092 printf("invalid/unsupported\n");
2095 printf("%s\n", algo);
2097 ret = fit_image_hash_get_value(fit, noffset, &value,
2099 printf("%s Hash value: ", p);
2101 printf("unavailable\n");
2103 for (i = 0; i < value_len; i++)
2104 printf("%02x", value[i]);
2108 debug("%s Hash len: %d\n", p, value_len);
2112 * fit_get_desc - get node description property
2113 * @fit: pointer to the FIT format image header
2114 * @noffset: node offset
2115 * @desc: double pointer to the char, will hold pointer to the descrption
2117 * fit_get_desc() reads description property from a given node, if
2118 * description is found pointer to it is returened in third call argument.
2124 int fit_get_desc(const void *fit, int noffset, char **desc)
2128 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
2129 if (*desc == NULL) {
2130 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
2138 * fit_get_timestamp - get node timestamp property
2139 * @fit: pointer to the FIT format image header
2140 * @noffset: node offset
2141 * @timestamp: pointer to the time_t, will hold read timestamp
2143 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2144 * is found and has a correct size its value is retured in third call
2149 * -1, on property read failure
2150 * -2, on wrong timestamp size
2152 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
2157 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
2159 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
2162 if (len != sizeof(uint32_t)) {
2163 debug("FIT timestamp with incorrect size of (%u)\n", len);
2167 *timestamp = uimage_to_cpu(*((uint32_t *)data));
2172 * fit_image_get_node - get node offset for component image of a given unit name
2173 * @fit: pointer to the FIT format image header
2174 * @image_uname: component image node unit name
2176 * fit_image_get_node() finds a component image (withing the '/images'
2177 * node) of a provided unit name. If image is found its node offset is
2178 * returned to the caller.
2181 * image node offset when found (>=0)
2182 * negative number on failure (FDT_ERR_* code)
2184 int fit_image_get_node(const void *fit, const char *image_uname)
2186 int noffset, images_noffset;
2188 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2189 if (images_noffset < 0) {
2190 debug("Can't find images parent node '%s' (%s)\n",
2191 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2192 return images_noffset;
2195 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
2197 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2198 image_uname, fdt_strerror(noffset));
2205 * fit_image_get_os - get os id for a given component image node
2206 * @fit: pointer to the FIT format image header
2207 * @noffset: component image node offset
2208 * @os: pointer to the uint8_t, will hold os numeric id
2210 * fit_image_get_os() finds os property in a given component image node.
2211 * If the property is found, its (string) value is translated to the numeric
2212 * id which is returned to the caller.
2218 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
2223 /* Get OS name from property data */
2224 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
2226 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
2231 /* Translate OS name to id */
2232 *os = genimg_get_os_id(data);
2237 * fit_image_get_arch - get arch id for a given component image node
2238 * @fit: pointer to the FIT format image header
2239 * @noffset: component image node offset
2240 * @arch: pointer to the uint8_t, will hold arch numeric id
2242 * fit_image_get_arch() finds arch property in a given component image node.
2243 * If the property is found, its (string) value is translated to the numeric
2244 * id which is returned to the caller.
2250 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
2255 /* Get architecture name from property data */
2256 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
2258 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
2263 /* Translate architecture name to id */
2264 *arch = genimg_get_arch_id(data);
2269 * fit_image_get_type - get type id for a given component image node
2270 * @fit: pointer to the FIT format image header
2271 * @noffset: component image node offset
2272 * @type: pointer to the uint8_t, will hold type numeric id
2274 * fit_image_get_type() finds type property in a given component image node.
2275 * If the property is found, its (string) value is translated to the numeric
2276 * id which is returned to the caller.
2282 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
2287 /* Get image type name from property data */
2288 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
2290 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
2295 /* Translate image type name to id */
2296 *type = genimg_get_type_id(data);
2301 * fit_image_get_comp - get comp id for a given component image node
2302 * @fit: pointer to the FIT format image header
2303 * @noffset: component image node offset
2304 * @comp: pointer to the uint8_t, will hold comp numeric id
2306 * fit_image_get_comp() finds comp property in a given component image node.
2307 * If the property is found, its (string) value is translated to the numeric
2308 * id which is returned to the caller.
2314 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
2319 /* Get compression name from property data */
2320 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
2322 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
2327 /* Translate compression name to id */
2328 *comp = genimg_get_comp_id(data);
2333 * fit_image_get_load - get load address property for a given component image node
2334 * @fit: pointer to the FIT format image header
2335 * @noffset: component image node offset
2336 * @load: pointer to the uint32_t, will hold load address
2338 * fit_image_get_load() finds load address property in a given component image node.
2339 * If the property is found, its value is returned to the caller.
2345 int fit_image_get_load(const void *fit, int noffset, ulong *load)
2348 const uint32_t *data;
2350 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
2352 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
2356 *load = uimage_to_cpu(*data);
2361 * fit_image_get_entry - get entry point address property for a given component image node
2362 * @fit: pointer to the FIT format image header
2363 * @noffset: component image node offset
2364 * @entry: pointer to the uint32_t, will hold entry point address
2366 * fit_image_get_entry() finds entry point address property in a given component image node.
2367 * If the property is found, its value is returned to the caller.
2373 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
2376 const uint32_t *data;
2378 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
2380 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
2384 *entry = uimage_to_cpu(*data);
2389 * fit_image_get_data - get data property and its size for a given component image node
2390 * @fit: pointer to the FIT format image header
2391 * @noffset: component image node offset
2392 * @data: double pointer to void, will hold data property's data address
2393 * @size: pointer to size_t, will hold data property's data size
2395 * fit_image_get_data() finds data property in a given component image node.
2396 * If the property is found its data start address and size are returned to
2403 int fit_image_get_data(const void *fit, int noffset,
2404 const void **data, size_t *size)
2408 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
2409 if (*data == NULL) {
2410 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
2420 * fit_image_hash_get_algo - get hash algorithm name
2421 * @fit: pointer to the FIT format image header
2422 * @noffset: hash node offset
2423 * @algo: double pointer to char, will hold pointer to the algorithm name
2425 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2426 * If the property is found its data start address is returned to the caller.
2432 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
2436 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
2437 if (*algo == NULL) {
2438 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
2446 * fit_image_hash_get_value - get hash value and length
2447 * @fit: pointer to the FIT format image header
2448 * @noffset: hash node offset
2449 * @value: double pointer to uint8_t, will hold address of a hash value data
2450 * @value_len: pointer to an int, will hold hash data length
2452 * fit_image_hash_get_value() finds hash value property in a given hash node.
2453 * If the property is found its data start address and size are returned to
2460 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
2465 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
2466 if (*value == NULL) {
2467 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
2477 * fit_set_timestamp - set node timestamp property
2478 * @fit: pointer to the FIT format image header
2479 * @noffset: node offset
2480 * @timestamp: timestamp value to be set
2482 * fit_set_timestamp() attempts to set timestamp property in the requested
2483 * node and returns operation status to the caller.
2487 * -1, on property read failure
2489 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
2494 t = cpu_to_uimage(timestamp);
2495 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2498 printf("Can't set '%s' property for '%s' node (%s)\n",
2499 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2508 * calculate_hash - calculate and return hash for provided input data
2509 * @data: pointer to the input data
2510 * @data_len: data length
2511 * @algo: requested hash algorithm
2512 * @value: pointer to the char, will hold hash value data (caller must
2513 * allocate enough free space)
2514 * value_len: length of the calculated hash
2516 * calculate_hash() computes input data hash according to the requested algorithm.
2517 * Resulting hash value is placed in caller provided 'value' buffer, length
2518 * of the calculated hash is returned via value_len pointer argument.
2522 * -1, when algo is unsupported
2524 static int calculate_hash(const void *data, int data_len, const char *algo,
2525 uint8_t *value, int *value_len)
2527 if (strcmp(algo, "crc32") == 0) {
2528 *((uint32_t *)value) = crc32_wd(0, data, data_len,
2530 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
2532 } else if (strcmp(algo, "sha1") == 0) {
2533 sha1_csum_wd((unsigned char *) data, data_len,
2534 (unsigned char *) value, CHUNKSZ_SHA1);
2536 } else if (strcmp(algo, "md5") == 0) {
2537 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2540 debug("Unsupported hash alogrithm\n");
2548 * fit_set_hashes - process FIT component image nodes and calculate hashes
2549 * @fit: pointer to the FIT format image header
2551 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2552 * Hashes are calculated for all component images which have hash subnodes
2553 * with algorithm property set to one of the supported hash algorithms.
2557 * libfdt error code, on failure
2559 int fit_set_hashes(void *fit)
2566 /* Find images parent node offset */
2567 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2568 if (images_noffset < 0) {
2569 printf("Can't find images parent node '%s' (%s)\n",
2570 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2571 return images_noffset;
2574 /* Process its subnodes, print out component images details */
2575 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
2576 (noffset >= 0) && (ndepth > 0);
2577 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2580 * Direct child node of the images parent node,
2581 * i.e. component image node.
2583 ret = fit_image_set_hashes(fit, noffset);
2593 * fit_image_set_hashes - calculate/set hashes for given component image node
2594 * @fit: pointer to the FIT format image header
2595 * @image_noffset: requested component image node
2597 * fit_image_set_hashes() adds hash values for an component image node. All
2598 * existing hash subnodes are checked, if algorithm property is set to one of
2599 * the supported hash algorithms, hash value is computed and corresponding
2600 * hash node property is set, for example:
2602 * Input component image node structure:
2604 * o image@1 (at image_noffset)
2605 * | - data = [binary data]
2609 * Output component image node structure:
2611 * o image@1 (at image_noffset)
2612 * | - data = [binary data]
2615 * |- value = sha1(data)
2621 int fit_image_set_hashes(void *fit, int image_noffset)
2626 uint8_t value[FIT_MAX_HASH_LEN];
2631 /* Get image data and data length */
2632 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2633 printf("Can't get image data/size\n");
2637 /* Process all hash subnodes of the component image node */
2638 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2639 (noffset >= 0) && (ndepth > 0);
2640 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2642 /* Direct child node of the component image node */
2645 * Check subnode name, must be equal to "hash".
2646 * Multiple hash nodes require unique unit node
2647 * names, e.g. hash@1, hash@2, etc.
2649 if (strncmp(fit_get_name(fit, noffset, NULL),
2651 strlen(FIT_HASH_NODENAME)) != 0) {
2652 /* Not a hash subnode, skip it */
2656 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2657 printf("Can't get hash algo property for "
2658 "'%s' hash node in '%s' image node\n",
2659 fit_get_name(fit, noffset, NULL),
2660 fit_get_name(fit, image_noffset, NULL));
2664 if (calculate_hash(data, size, algo, value,
2666 printf("Unsupported hash algorithm (%s) for "
2667 "'%s' hash node in '%s' image node\n",
2668 algo, fit_get_name(fit, noffset, NULL),
2669 fit_get_name(fit, image_noffset,
2674 if (fit_image_hash_set_value(fit, noffset, value,
2676 printf("Can't set hash value for "
2677 "'%s' hash node in '%s' image node\n",
2678 fit_get_name(fit, noffset, NULL),
2679 fit_get_name(fit, image_noffset, NULL));
2689 * fit_image_hash_set_value - set hash value in requested has node
2690 * @fit: pointer to the FIT format image header
2691 * @noffset: hash node offset
2692 * @value: hash value to be set
2693 * @value_len: hash value length
2695 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2696 * given and returns operation status to the caller.
2702 int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
2707 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
2709 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2710 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2717 #endif /* USE_HOSTCC */
2720 * fit_image_check_hashes - verify data intergity
2721 * @fit: pointer to the FIT format image header
2722 * @image_noffset: component image node offset
2724 * fit_image_check_hashes() goes over component image hash nodes,
2725 * re-calculates each data hash and compares with the value stored in hash
2729 * 1, if all hashes are valid
2730 * 0, otherwise (or on error)
2732 int fit_image_check_hashes(const void *fit, int image_noffset)
2739 uint8_t value[FIT_MAX_HASH_LEN];
2745 /* Get image data and data length */
2746 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2747 printf("Can't get image data/size\n");
2751 /* Process all hash subnodes of the component image node */
2752 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2753 (noffset >= 0) && (ndepth > 0);
2754 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2756 /* Direct child node of the component image node */
2759 * Check subnode name, must be equal to "hash".
2760 * Multiple hash nodes require unique unit node
2761 * names, e.g. hash@1, hash@2, etc.
2763 if (strncmp(fit_get_name(fit, noffset, NULL),
2765 strlen(FIT_HASH_NODENAME)) != 0)
2768 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2769 err_msg = " error!\nCan't get hash algo "
2775 if (fit_image_hash_get_value(fit, noffset, &fit_value,
2777 err_msg = " error!\nCan't get hash value "
2782 if (calculate_hash(data, size, algo, value,
2784 err_msg = " error!\n"
2785 "Unsupported hash algorithm";
2789 if (value_len != fit_value_len) {
2790 err_msg = " error !\nBad hash value len";
2792 } else if (memcmp(value, fit_value, value_len) != 0) {
2793 err_msg = " error!\nBad hash value";
2803 printf("%s for '%s' hash node in '%s' image node\n",
2804 err_msg, fit_get_name(fit, noffset, NULL),
2805 fit_get_name(fit, image_noffset, NULL));
2810 * fit_all_image_check_hashes - verify data intergity for all images
2811 * @fit: pointer to the FIT format image header
2813 * fit_all_image_check_hashes() goes over all images in the FIT and
2814 * for every images checks if all it's hashes are valid.
2817 * 1, if all hashes of all images are valid
2818 * 0, otherwise (or on error)
2820 int fit_all_image_check_hashes(const void *fit)
2827 /* Find images parent node offset */
2828 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2829 if (images_noffset < 0) {
2830 printf("Can't find images parent node '%s' (%s)\n",
2831 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2835 /* Process all image subnodes, check hashes for each */
2836 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
2838 for (ndepth = 0, count = 0,
2839 noffset = fdt_next_node(fit, images_noffset, &ndepth);
2840 (noffset >= 0) && (ndepth > 0);
2841 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2844 * Direct child node of the images parent node,
2845 * i.e. component image node.
2847 printf(" Hash(es) for Image %u (%s): ", count++,
2848 fit_get_name(fit, noffset, NULL));
2850 if (!fit_image_check_hashes(fit, noffset))
2859 * fit_image_check_os - check whether image node is of a given os type
2860 * @fit: pointer to the FIT format image header
2861 * @noffset: component image node offset
2862 * @os: requested image os
2864 * fit_image_check_os() reads image os property and compares its numeric
2865 * id with the requested os. Comparison result is returned to the caller.
2868 * 1 if image is of given os type
2869 * 0 otherwise (or on error)
2871 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
2875 if (fit_image_get_os(fit, noffset, &image_os))
2877 return (os == image_os);
2881 * fit_image_check_arch - check whether image node is of a given arch
2882 * @fit: pointer to the FIT format image header
2883 * @noffset: component image node offset
2884 * @arch: requested imagearch
2886 * fit_image_check_arch() reads image arch property and compares its numeric
2887 * id with the requested arch. Comparison result is returned to the caller.
2890 * 1 if image is of given arch
2891 * 0 otherwise (or on error)
2893 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
2897 if (fit_image_get_arch(fit, noffset, &image_arch))
2899 return (arch == image_arch);
2903 * fit_image_check_type - check whether image node is of a given type
2904 * @fit: pointer to the FIT format image header
2905 * @noffset: component image node offset
2906 * @type: requested image type
2908 * fit_image_check_type() reads image type property and compares its numeric
2909 * id with the requested type. Comparison result is returned to the caller.
2912 * 1 if image is of given type
2913 * 0 otherwise (or on error)
2915 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
2919 if (fit_image_get_type(fit, noffset, &image_type))
2921 return (type == image_type);
2925 * fit_image_check_comp - check whether image node uses given compression
2926 * @fit: pointer to the FIT format image header
2927 * @noffset: component image node offset
2928 * @comp: requested image compression type
2930 * fit_image_check_comp() reads image compression property and compares its
2931 * numeric id with the requested compression type. Comparison result is
2932 * returned to the caller.
2935 * 1 if image uses requested compression
2936 * 0 otherwise (or on error)
2938 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
2942 if (fit_image_get_comp(fit, noffset, &image_comp))
2944 return (comp == image_comp);
2948 * fit_check_format - sanity check FIT image format
2949 * @fit: pointer to the FIT format image header
2951 * fit_check_format() runs a basic sanity FIT image verification.
2952 * Routine checks for mandatory properties, nodes, etc.
2958 int fit_check_format(const void *fit)
2960 /* mandatory / node 'description' property */
2961 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2962 debug("Wrong FIT format: no description\n");
2966 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2967 /* mandatory / node 'timestamp' property */
2968 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2969 debug("Wrong FIT format: no timestamp\n");
2974 /* mandatory subimages parent '/images' node */
2975 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
2976 debug("Wrong FIT format: no images parent node\n");
2984 * fit_conf_get_node - get node offset for configuration of a given unit name
2985 * @fit: pointer to the FIT format image header
2986 * @conf_uname: configuration node unit name
2988 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2989 * parant node) of a provided unit name. If configuration is found its node offset
2990 * is returned to the caller.
2992 * When NULL is provided in second argument fit_conf_get_node() will search
2993 * for a default configuration node instead. Default configuration node unit name
2994 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2997 * configuration node offset when found (>=0)
2998 * negative number on failure (FDT_ERR_* code)
3000 int fit_conf_get_node(const void *fit, const char *conf_uname)
3002 int noffset, confs_noffset;
3005 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3006 if (confs_noffset < 0) {
3007 debug("Can't find configurations parent node '%s' (%s)\n",
3008 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
3009 return confs_noffset;
3012 if (conf_uname == NULL) {
3013 /* get configuration unit name from the default property */
3014 debug("No configuration specified, trying default...\n");
3015 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3016 FIT_DEFAULT_PROP, &len);
3017 if (conf_uname == NULL) {
3018 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3022 debug("Found default configuration: '%s'\n", conf_uname);
3025 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
3027 debug("Can't get node offset for configuration unit name: "
3029 conf_uname, fdt_strerror(noffset));
3035 static int __fit_conf_get_prop_node(const void *fit, int noffset,
3036 const char *prop_name)
3041 /* get kernel image unit name from configuration kernel property */
3042 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
3046 return fit_image_get_node(fit, uname);
3050 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3051 * a given configuration
3052 * @fit: pointer to the FIT format image header
3053 * @noffset: configuration node offset
3055 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3056 * configuration FIT_KERNEL_PROP property and translates it to the node
3060 * image node offset when found (>=0)
3061 * negative number on failure (FDT_ERR_* code)
3063 int fit_conf_get_kernel_node(const void *fit, int noffset)
3065 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
3069 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3070 * a given configuration
3071 * @fit: pointer to the FIT format image header
3072 * @noffset: configuration node offset
3074 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3075 * configuration FIT_KERNEL_PROP property and translates it to the node
3079 * image node offset when found (>=0)
3080 * negative number on failure (FDT_ERR_* code)
3082 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
3084 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
3088 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3089 * a given configuration
3090 * @fit: pointer to the FIT format image header
3091 * @noffset: configuration node offset
3093 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3094 * configuration FIT_KERNEL_PROP property and translates it to the node
3098 * image node offset when found (>=0)
3099 * negative number on failure (FDT_ERR_* code)
3101 int fit_conf_get_fdt_node(const void *fit, int noffset)
3103 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
3107 * fit_conf_print - prints out the FIT configuration details
3108 * @fit: pointer to the FIT format image header
3109 * @noffset: offset of the configuration node
3110 * @p: pointer to prefix string
3112 * fit_conf_print() lists all mandatory properies for the processed
3113 * configuration node.
3116 * no returned results
3118 void fit_conf_print(const void *fit, int noffset, const char *p)
3124 /* Mandatory properties */
3125 ret = fit_get_desc(fit, noffset, &desc);
3126 printf("%s Description: ", p);
3128 printf("unavailable\n");
3130 printf("%s\n", desc);
3132 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3133 printf("%s Kernel: ", p);
3135 printf("unavailable\n");
3137 printf("%s\n", uname);
3139 /* Optional properties */
3140 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
3142 printf("%s Init Ramdisk: %s\n", p, uname);
3144 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
3146 printf("%s FDT: %s\n", p, uname);
3150 * fit_check_ramdisk - verify FIT format ramdisk subimage
3151 * @fit_hdr: pointer to the FIT ramdisk header
3152 * @rd_noffset: ramdisk subimage node offset within FIT image
3153 * @arch: requested ramdisk image architecture type
3154 * @verify: data CRC verification flag
3156 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3157 * specified FIT image.
3164 static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3167 fit_image_print(fit, rd_noffset, " ");
3170 puts(" Verifying Hash Integrity ... ");
3171 if (!fit_image_check_hashes(fit, rd_noffset)) {
3172 puts("Bad Data Hash\n");
3173 show_boot_progress(-125);
3179 show_boot_progress(126);
3180 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3181 !fit_image_check_arch(fit, rd_noffset, arch) ||
3182 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3183 printf("No Linux %s Ramdisk Image\n",
3184 genimg_get_arch_name(arch));
3185 show_boot_progress(-126);
3189 show_boot_progress(127);
3192 #endif /* USE_HOSTCC */
3193 #endif /* CONFIG_FIT */