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)
46 #include <environment.h>
49 #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 #include <u-boot/crc.h>
79 static const table_entry_t uimage_arch[] = {
80 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
81 { IH_ARCH_ALPHA, "alpha", "Alpha", },
82 { IH_ARCH_ARM, "arm", "ARM", },
83 { IH_ARCH_I386, "x86", "Intel x86", },
84 { IH_ARCH_IA64, "ia64", "IA64", },
85 { IH_ARCH_M68K, "m68k", "M68K", },
86 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
87 { IH_ARCH_MIPS, "mips", "MIPS", },
88 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
89 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
90 { IH_ARCH_PPC, "powerpc", "PowerPC", },
91 { IH_ARCH_PPC, "ppc", "PowerPC", },
92 { IH_ARCH_S390, "s390", "IBM S390", },
93 { IH_ARCH_SH, "sh", "SuperH", },
94 { IH_ARCH_SPARC, "sparc", "SPARC", },
95 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
96 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
97 { IH_ARCH_AVR32, "avr32", "AVR32", },
98 { IH_ARCH_NDS32, "nds32", "NDS32", },
99 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
103 static const table_entry_t uimage_os[] = {
104 { IH_OS_INVALID, NULL, "Invalid OS", },
105 { IH_OS_LINUX, "linux", "Linux", },
106 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
107 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
109 { IH_OS_NETBSD, "netbsd", "NetBSD", },
110 { IH_OS_OSE, "ose", "Enea OSE", },
111 { IH_OS_RTEMS, "rtems", "RTEMS", },
112 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
113 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
114 { IH_OS_QNX, "qnx", "QNX", },
115 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
117 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
118 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
121 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
122 { IH_OS_DELL, "dell", "Dell", },
123 { IH_OS_ESIX, "esix", "Esix", },
124 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
125 { IH_OS_IRIX, "irix", "Irix", },
126 { IH_OS_NCR, "ncr", "NCR", },
127 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
128 { IH_OS_PSOS, "psos", "pSOS", },
129 { IH_OS_SCO, "sco", "SCO", },
130 { IH_OS_SOLARIS, "solaris", "Solaris", },
131 { IH_OS_SVR4, "svr4", "SVR4", },
136 static const table_entry_t uimage_type[] = {
137 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
138 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
139 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
140 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
141 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
142 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
143 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
144 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
145 { IH_TYPE_INVALID, NULL, "Invalid Image", },
146 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
147 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
148 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
149 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
150 { IH_TYPE_SCRIPT, "script", "Script", },
151 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
152 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
156 static const table_entry_t uimage_comp[] = {
157 { IH_COMP_NONE, "none", "uncompressed", },
158 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
159 { IH_COMP_GZIP, "gzip", "gzip compressed", },
160 { IH_COMP_LZMA, "lzma", "lzma compressed", },
161 { IH_COMP_LZO, "lzo", "lzo compressed", },
165 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
166 static void genimg_print_time(time_t timestamp);
169 /*****************************************************************************/
170 /* Legacy format routines */
171 /*****************************************************************************/
172 int image_check_hcrc(const image_header_t *hdr)
175 ulong len = image_get_header_size();
176 image_header_t header;
178 /* Copy header so we can blank CRC field for re-calculation */
179 memmove(&header, (char *)hdr, image_get_header_size());
180 image_set_hcrc(&header, 0);
182 hcrc = crc32(0, (unsigned char *)&header, len);
184 return (hcrc == image_get_hcrc(hdr));
187 int image_check_dcrc(const image_header_t *hdr)
189 ulong data = image_get_data(hdr);
190 ulong len = image_get_data_size(hdr);
191 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
193 return (dcrc == image_get_dcrc(hdr));
197 * image_multi_count - get component (sub-image) count
198 * @hdr: pointer to the header of the multi component image
200 * image_multi_count() returns number of components in a multi
203 * Note: no checking of the image type is done, caller must pass
204 * a valid multi component image.
207 * number of components
209 ulong image_multi_count(const image_header_t *hdr)
214 /* get start of the image payload, which in case of multi
215 * component images that points to a table of component sizes */
216 size = (uint32_t *)image_get_data(hdr);
218 /* count non empty slots */
219 for (i = 0; size[i]; ++i)
226 * image_multi_getimg - get component data address and size
227 * @hdr: pointer to the header of the multi component image
228 * @idx: index of the requested component
229 * @data: pointer to a ulong variable, will hold component data address
230 * @len: pointer to a ulong variable, will hold component size
232 * image_multi_getimg() returns size and data address for the requested
233 * component in a multi component image.
235 * Note: no checking of the image type is done, caller must pass
236 * a valid multi component image.
239 * data address and size of the component, if idx is valid
240 * 0 in data and len, if idx is out of range
242 void image_multi_getimg(const image_header_t *hdr, ulong idx,
243 ulong *data, ulong *len)
247 ulong offset, count, img_data;
249 /* get number of component */
250 count = image_multi_count(hdr);
252 /* get start of the image payload, which in case of multi
253 * component images that points to a table of component sizes */
254 size = (uint32_t *)image_get_data(hdr);
256 /* get address of the proper component data start, which means
257 * skipping sizes table (add 1 for last, null entry) */
258 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
261 *len = uimage_to_cpu(size[idx]);
264 /* go over all indices preceding requested component idx */
265 for (i = 0; i < idx; i++) {
266 /* add up i-th component size, rounding up to 4 bytes */
267 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
270 /* calculate idx-th component data address */
271 *data = img_data + offset;
278 static void image_print_type(const image_header_t *hdr)
280 const char *os, *arch, *type, *comp;
282 os = genimg_get_os_name(image_get_os(hdr));
283 arch = genimg_get_arch_name(image_get_arch(hdr));
284 type = genimg_get_type_name(image_get_type(hdr));
285 comp = genimg_get_comp_name(image_get_comp(hdr));
287 printf("%s %s %s (%s)\n", arch, os, type, comp);
291 * image_print_contents - prints out the contents of the legacy format image
292 * @ptr: pointer to the legacy format image header
293 * @p: pointer to prefix string
295 * image_print_contents() formats a multi line legacy image contents description.
296 * The routine prints out all header fields followed by the size/offset data
297 * for MULTI/SCRIPT images.
300 * no returned results
302 void image_print_contents(const void *ptr)
304 const image_header_t *hdr = (const image_header_t *)ptr;
313 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
314 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
315 printf("%sCreated: ", p);
316 genimg_print_time((time_t)image_get_time(hdr));
318 printf("%sImage Type: ", p);
319 image_print_type(hdr);
320 printf("%sData Size: ", p);
321 genimg_print_size(image_get_data_size(hdr));
322 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
323 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
325 if (image_check_type(hdr, IH_TYPE_MULTI) ||
326 image_check_type(hdr, IH_TYPE_SCRIPT)) {
329 ulong count = image_multi_count(hdr);
331 printf("%sContents:\n", p);
332 for (i = 0; i < count; i++) {
333 image_multi_getimg(hdr, i, &data, &len);
335 printf("%s Image %d: ", p, i);
336 genimg_print_size(len);
338 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
340 * the user may need to know offsets
341 * if planning to do something with
344 printf("%s Offset = 0x%08lx\n", p, data);
353 * image_get_ramdisk - get and verify ramdisk image
354 * @rd_addr: ramdisk image start address
355 * @arch: expected ramdisk architecture
356 * @verify: checksum verification flag
358 * image_get_ramdisk() returns a pointer to the verified ramdisk image
359 * header. Routine receives image start address and expected architecture
360 * flag. Verification done covers data and header integrity and os/type/arch
363 * If dataflash support is enabled routine checks for dataflash addresses
364 * and handles required dataflash reads.
367 * pointer to a ramdisk image header, if image was found and valid
368 * otherwise, return NULL
370 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
373 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
375 if (!image_check_magic(rd_hdr)) {
376 puts("Bad Magic Number\n");
377 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
381 if (!image_check_hcrc(rd_hdr)) {
382 puts("Bad Header Checksum\n");
383 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
387 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
388 image_print_contents(rd_hdr);
391 puts(" Verifying Checksum ... ");
392 if (!image_check_dcrc(rd_hdr)) {
393 puts("Bad Data CRC\n");
394 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
400 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
402 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
403 !image_check_arch(rd_hdr, arch) ||
404 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
405 printf("No Linux %s Ramdisk Image\n",
406 genimg_get_arch_name(arch));
407 bootstage_error(BOOTSTAGE_ID_RAMDISK);
413 #endif /* !USE_HOSTCC */
415 /*****************************************************************************/
416 /* Shared dual-format routines */
417 /*****************************************************************************/
419 ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
420 ulong save_addr; /* Default Save Address */
421 ulong save_size; /* Default Save Size (in bytes) */
423 static int on_loadaddr(const char *name, const char *value, enum env_op op,
428 case env_op_overwrite:
429 load_addr = simple_strtoul(value, NULL, 16);
437 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
439 ulong getenv_bootm_low(void)
441 char *s = getenv("bootm_low");
443 ulong tmp = simple_strtoul(s, NULL, 16);
447 #if defined(CONFIG_SYS_SDRAM_BASE)
448 return CONFIG_SYS_SDRAM_BASE;
449 #elif defined(CONFIG_ARM)
450 return gd->bd->bi_dram[0].start;
456 phys_size_t getenv_bootm_size(void)
459 char *s = getenv("bootm_size");
461 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
464 s = getenv("bootm_low");
466 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
471 #if defined(CONFIG_ARM)
472 return gd->bd->bi_dram[0].size - tmp;
474 return gd->bd->bi_memsize - tmp;
478 phys_size_t getenv_bootm_mapsize(void)
481 char *s = getenv("bootm_mapsize");
483 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
487 #if defined(CONFIG_SYS_BOOTMAPSZ)
488 return CONFIG_SYS_BOOTMAPSZ;
490 return getenv_bootm_size();
494 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
499 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
501 size_t tail = (len > chunksz) ? chunksz : len;
503 memmove(to, from, tail);
508 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
509 memmove(to, from, len);
510 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
512 #endif /* !USE_HOSTCC */
514 void genimg_print_size(uint32_t size)
517 printf("%d Bytes = ", size);
518 print_size(size, "\n");
520 printf("%d Bytes = %.2f kB = %.2f MB\n",
521 size, (double)size / 1.024e3,
522 (double)size / 1.048576e6);
526 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
527 static void genimg_print_time(time_t timestamp)
532 to_tm(timestamp, &tm);
533 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
534 tm.tm_year, tm.tm_mon, tm.tm_mday,
535 tm.tm_hour, tm.tm_min, tm.tm_sec);
537 printf("%s", ctime(×tamp));
540 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
543 * get_table_entry_name - translate entry id to long name
544 * @table: pointer to a translation table for entries of a specific type
545 * @msg: message to be returned when translation fails
546 * @id: entry id to be translated
548 * get_table_entry_name() will go over translation table trying to find
549 * entry that matches given id. If matching entry is found, its long
550 * name is returned to the caller.
553 * long entry name if translation succeeds
556 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
558 for (; table->id >= 0; ++table) {
560 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
563 return table->lname + gd->reloc_off;
569 const char *genimg_get_os_name(uint8_t os)
571 return (get_table_entry_name(uimage_os, "Unknown OS", os));
574 const char *genimg_get_arch_name(uint8_t arch)
576 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
580 const char *genimg_get_type_name(uint8_t type)
582 return (get_table_entry_name(uimage_type, "Unknown Image", type));
585 const char *genimg_get_comp_name(uint8_t comp)
587 return (get_table_entry_name(uimage_comp, "Unknown Compression",
592 * get_table_entry_id - translate short entry name to id
593 * @table: pointer to a translation table for entries of a specific type
594 * @table_name: to be used in case of error
595 * @name: entry short name to be translated
597 * get_table_entry_id() will go over translation table trying to find
598 * entry that matches given short name. If matching entry is found,
599 * its id returned to the caller.
602 * entry id if translation succeeds
605 int get_table_entry_id(const table_entry_t *table,
606 const char *table_name, const char *name)
608 const table_entry_t *t;
612 for (t = table; t->id >= 0; ++t) {
613 if (t->sname && strcasecmp(t->sname, name) == 0)
617 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
618 for (t = table; t->id >= 0; ++t) {
619 if (t->sname == NULL)
621 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
624 fprintf(stderr, "\n");
626 for (t = table; t->id >= 0; ++t) {
627 #ifdef CONFIG_NEEDS_MANUAL_RELOC
628 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
630 if (t->sname && strcmp(t->sname, name) == 0)
634 debug("Invalid %s Type: %s\n", table_name, name);
635 #endif /* USE_HOSTCC */
639 int genimg_get_os_id(const char *name)
641 return (get_table_entry_id(uimage_os, "OS", name));
644 int genimg_get_arch_id(const char *name)
646 return (get_table_entry_id(uimage_arch, "CPU", name));
649 int genimg_get_type_id(const char *name)
651 return (get_table_entry_id(uimage_type, "Image", name));
654 int genimg_get_comp_id(const char *name)
656 return (get_table_entry_id(uimage_comp, "Compression", name));
661 * genimg_get_format - get image format type
662 * @img_addr: image start address
664 * genimg_get_format() checks whether provided address points to a valid
665 * legacy or FIT image.
667 * New uImage format and FDT blob are based on a libfdt. FDT blob
668 * may be passed directly or embedded in a FIT image. In both situations
669 * genimg_get_format() must be able to dectect libfdt header.
672 * image format type or IMAGE_FORMAT_INVALID if no image is present
674 int genimg_get_format(void *img_addr)
676 ulong format = IMAGE_FORMAT_INVALID;
677 const image_header_t *hdr;
678 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
682 hdr = (const image_header_t *)img_addr;
683 if (image_check_magic(hdr))
684 format = IMAGE_FORMAT_LEGACY;
685 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
687 fit_hdr = (char *)img_addr;
688 if (fdt_check_header(fit_hdr) == 0)
689 format = IMAGE_FORMAT_FIT;
697 * genimg_get_image - get image from special storage (if necessary)
698 * @img_addr: image start address
700 * genimg_get_image() checks if provided image start adddress is located
701 * in a dataflash storage. If so, image is moved to a system RAM memory.
704 * image start address after possible relocation from special storage
706 ulong genimg_get_image(ulong img_addr)
708 ulong ram_addr = img_addr;
710 #ifdef CONFIG_HAS_DATAFLASH
711 ulong h_size, d_size;
713 if (addr_dataflash(img_addr)) {
714 /* ger RAM address */
715 ram_addr = CONFIG_SYS_LOAD_ADDR;
717 /* get header size */
718 h_size = image_get_header_size();
719 #if defined(CONFIG_FIT)
720 if (sizeof(struct fdt_header) > h_size)
721 h_size = sizeof(struct fdt_header);
725 debug(" Reading image header from dataflash address "
726 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
728 read_dataflash(img_addr, h_size, (char *)ram_addr);
731 switch (genimg_get_format((void *)ram_addr)) {
732 case IMAGE_FORMAT_LEGACY:
733 d_size = image_get_data_size(
734 (const image_header_t *)ram_addr);
735 debug(" Legacy format image found at 0x%08lx, "
739 #if defined(CONFIG_FIT)
740 case IMAGE_FORMAT_FIT:
741 d_size = fit_get_size((const void *)ram_addr) - h_size;
742 debug(" FIT/FDT format image found at 0x%08lx, "
748 printf(" No valid image found at 0x%08lx\n",
753 /* read in image data */
754 debug(" Reading image remaining data from dataflash address "
755 "%08lx to RAM address %08lx\n", img_addr + h_size,
758 read_dataflash(img_addr + h_size, d_size,
759 (char *)(ram_addr + h_size));
762 #endif /* CONFIG_HAS_DATAFLASH */
768 * fit_has_config - check if there is a valid FIT configuration
769 * @images: pointer to the bootm command headers structure
771 * fit_has_config() checks if there is a FIT configuration in use
772 * (if FTI support is present).
775 * 0, no FIT support or no configuration found
776 * 1, configuration found
778 int genimg_has_config(bootm_headers_t *images)
780 #if defined(CONFIG_FIT)
781 if (images->fit_uname_cfg)
788 * boot_get_ramdisk - main ramdisk handling routine
789 * @argc: command argument count
790 * @argv: command argument list
791 * @images: pointer to the bootm images structure
792 * @arch: expected ramdisk architecture
793 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
794 * @rd_end: pointer to a ulong variable, will hold ramdisk end
796 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
797 * Curently supported are the following ramdisk sources:
798 * - multicomponent kernel/ramdisk image,
799 * - commandline provided address of decicated ramdisk image.
802 * 0, if ramdisk image was found and valid, or skiped
803 * rd_start and rd_end are set to ramdisk start/end addresses if
804 * ramdisk image is found and valid
806 * 1, if ramdisk image is found but corrupted, or invalid
807 * rd_start and rd_end are set to 0 if no ramdisk exists
809 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
810 uint8_t arch, ulong *rd_start, ulong *rd_end)
812 ulong rd_addr, rd_load;
813 ulong rd_data, rd_len;
814 const image_header_t *rd_hdr;
815 #ifdef CONFIG_SUPPORT_RAW_INITRD
818 #if defined(CONFIG_FIT)
820 const char *fit_uname_config = NULL;
821 const char *fit_uname_ramdisk = NULL;
833 * Look for a '-' which indicates to ignore the
836 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
837 debug("## Skipping init Ramdisk\n");
838 rd_len = rd_data = 0;
839 } else if (argc >= 3 || genimg_has_config(images)) {
840 #if defined(CONFIG_FIT)
843 * If the init ramdisk comes from the FIT image and
844 * the FIT image address is omitted in the command
845 * line argument, try to use os FIT image address or
846 * default load address.
848 if (images->fit_uname_os)
849 default_addr = (ulong)images->fit_hdr_os;
851 default_addr = load_addr;
853 if (fit_parse_conf(argv[2], default_addr,
854 &rd_addr, &fit_uname_config)) {
855 debug("* ramdisk: config '%s' from image at "
857 fit_uname_config, rd_addr);
858 } else if (fit_parse_subimage(argv[2], default_addr,
859 &rd_addr, &fit_uname_ramdisk)) {
860 debug("* ramdisk: subimage '%s' from image at "
862 fit_uname_ramdisk, rd_addr);
866 rd_addr = simple_strtoul(argv[2], NULL, 16);
867 debug("* ramdisk: cmdline image address = "
871 #if defined(CONFIG_FIT)
873 /* use FIT configuration provided in first bootm
876 rd_addr = (ulong)images->fit_hdr_os;
877 fit_uname_config = images->fit_uname_cfg;
878 debug("* ramdisk: using config '%s' from image "
880 fit_uname_config, rd_addr);
883 * Check whether configuration has ramdisk defined,
884 * if not, don't try to use it, quit silently.
886 fit_hdr = (void *)rd_addr;
887 cfg_noffset = fit_conf_get_node(fit_hdr,
889 if (cfg_noffset < 0) {
890 debug("* ramdisk: no such config\n");
894 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
896 if (rd_noffset < 0) {
897 debug("* ramdisk: no ramdisk in config\n");
903 /* copy from dataflash if needed */
904 rd_addr = genimg_get_image(rd_addr);
907 * Check if there is an initrd image at the
908 * address provided in the second bootm argument
909 * check image type, for FIT images get FIT node.
911 switch (genimg_get_format((void *)rd_addr)) {
912 case IMAGE_FORMAT_LEGACY:
913 printf("## Loading init Ramdisk from Legacy "
914 "Image at %08lx ...\n", rd_addr);
916 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
917 rd_hdr = image_get_ramdisk(rd_addr, arch,
923 rd_data = image_get_data(rd_hdr);
924 rd_len = image_get_data_size(rd_hdr);
925 rd_load = image_get_load(rd_hdr);
927 #if defined(CONFIG_FIT)
928 case IMAGE_FORMAT_FIT:
929 fit_hdr = (void *)rd_addr;
930 printf("## Loading init Ramdisk from FIT "
931 "Image at %08lx ...\n", rd_addr);
933 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
934 if (!fit_check_format(fit_hdr)) {
935 puts("Bad FIT ramdisk image format!\n");
937 BOOTSTAGE_ID_FIT_RD_FORMAT);
940 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
942 if (!fit_uname_ramdisk) {
944 * no ramdisk image node unit name, try to get config
945 * node first. If config unit node name is NULL
946 * fit_conf_get_node() will try to find default config node
949 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
950 cfg_noffset = fit_conf_get_node(fit_hdr,
952 if (cfg_noffset < 0) {
953 puts("Could not find configuration "
956 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
959 fit_uname_config = fdt_get_name(fit_hdr,
961 printf(" Using '%s' configuration\n",
964 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
966 fit_uname_ramdisk = fit_get_name(fit_hdr,
969 /* get ramdisk component image node offset */
971 BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
972 rd_noffset = fit_image_get_node(fit_hdr,
975 if (rd_noffset < 0) {
976 puts("Could not find subimage node\n");
977 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
981 printf(" Trying '%s' ramdisk subimage\n",
984 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
985 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
989 /* get ramdisk image data address and length */
990 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
992 puts("Could not find ramdisk subimage data!\n");
993 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
996 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
998 rd_data = (ulong)data;
1001 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
1002 puts("Can't get ramdisk subimage load "
1004 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
1007 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
1009 images->fit_hdr_rd = fit_hdr;
1010 images->fit_uname_rd = fit_uname_ramdisk;
1011 images->fit_noffset_rd = rd_noffset;
1015 #ifdef CONFIG_SUPPORT_RAW_INITRD
1016 if (argc >= 3 && (end = strchr(argv[2], ':'))) {
1017 rd_len = simple_strtoul(++end, NULL, 16);
1022 puts("Wrong Ramdisk Image Format\n");
1023 rd_data = rd_len = rd_load = 0;
1027 } else if (images->legacy_hdr_valid &&
1028 image_check_type(&images->legacy_hdr_os_copy,
1032 * Now check if we have a legacy mult-component image,
1033 * get second entry data start address and len.
1035 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1036 printf("## Loading init Ramdisk from multi component "
1037 "Legacy Image at %08lx ...\n",
1038 (ulong)images->legacy_hdr_os);
1040 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1045 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1046 rd_len = rd_data = 0;
1050 debug("## No init Ramdisk\n");
1052 *rd_start = rd_data;
1053 *rd_end = rd_data + rd_len;
1055 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1056 *rd_start, *rd_end);
1061 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1063 * boot_ramdisk_high - relocate init ramdisk
1064 * @lmb: pointer to lmb handle, will be used for memory mgmt
1065 * @rd_data: ramdisk data start address
1066 * @rd_len: ramdisk data length
1067 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1068 * start address (after possible relocation)
1069 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1070 * end address (after possible relocation)
1072 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1073 * variable and if requested ramdisk data is moved to a specified location.
1075 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1076 * start/end addresses if ramdisk image start and len were provided,
1077 * otherwise set initrd_start and initrd_end set to zeros.
1083 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1084 ulong *initrd_start, ulong *initrd_end)
1088 int initrd_copy_to_ram = 1;
1090 if ((s = getenv("initrd_high")) != NULL) {
1091 /* a value of "no" or a similar string will act like 0,
1092 * turning the "load high" feature off. This is intentional.
1094 initrd_high = simple_strtoul(s, NULL, 16);
1095 if (initrd_high == ~0)
1096 initrd_copy_to_ram = 0;
1098 /* not set, no restrictions to load high */
1103 #ifdef CONFIG_LOGBUFFER
1104 /* Prevent initrd from overwriting logbuffer */
1105 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1108 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1109 initrd_high, initrd_copy_to_ram);
1112 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1113 debug(" in-place initrd\n");
1114 *initrd_start = rd_data;
1115 *initrd_end = rd_data + rd_len;
1116 lmb_reserve(lmb, rd_data, rd_len);
1119 *initrd_start = (ulong)lmb_alloc_base(lmb,
1120 rd_len, 0x1000, initrd_high);
1122 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1125 if (*initrd_start == 0) {
1126 puts("ramdisk - allocation error\n");
1129 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1131 *initrd_end = *initrd_start + rd_len;
1132 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
1133 *initrd_start, *initrd_end);
1135 memmove_wd((void *)*initrd_start,
1136 (void *)rd_data, rd_len, CHUNKSZ);
1140 * Ensure the image is flushed to memory to handle
1141 * AMP boot scenarios in which we might not be
1144 flush_cache((unsigned long)*initrd_start, rd_len);
1152 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1153 *initrd_start, *initrd_end);
1160 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1162 #ifdef CONFIG_OF_LIBFDT
1163 static void fdt_error(const char *msg)
1167 puts(" - must RESET the board to recover.\n");
1170 static const image_header_t *image_get_fdt(ulong fdt_addr)
1172 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1174 image_print_contents(fdt_hdr);
1176 puts(" Verifying Checksum ... ");
1177 if (!image_check_hcrc(fdt_hdr)) {
1178 fdt_error("fdt header checksum invalid");
1182 if (!image_check_dcrc(fdt_hdr)) {
1183 fdt_error("fdt checksum invalid");
1188 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1189 fdt_error("uImage is not a fdt");
1192 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1193 fdt_error("uImage is compressed");
1196 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1197 fdt_error("uImage data is not a fdt");
1204 * fit_check_fdt - verify FIT format FDT subimage
1205 * @fit_hdr: pointer to the FIT header
1206 * fdt_noffset: FDT subimage node offset within FIT image
1207 * @verify: data CRC verification flag
1209 * fit_check_fdt() verifies integrity of the FDT subimage and from
1210 * specified FIT image.
1216 #if defined(CONFIG_FIT)
1217 static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
1219 fit_image_print(fit, fdt_noffset, " ");
1222 puts(" Verifying Hash Integrity ... ");
1223 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1224 fdt_error("Bad Data Hash");
1230 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1231 fdt_error("Not a FDT image");
1235 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1236 fdt_error("FDT image is compressed");
1242 #endif /* CONFIG_FIT */
1244 #ifndef CONFIG_SYS_FDT_PAD
1245 #define CONFIG_SYS_FDT_PAD 0x3000
1248 #if defined(CONFIG_OF_LIBFDT)
1250 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1251 * @lmb: pointer to lmb handle, will be used for memory mgmt
1252 * @fdt_blob: pointer to fdt blob base address
1254 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1255 * memreserve regions prevents u-boot from using them to store the initrd
1258 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1260 uint64_t addr, size;
1263 if (fdt_check_header(fdt_blob) != 0)
1266 total = fdt_num_mem_rsv(fdt_blob);
1267 for (i = 0; i < total; i++) {
1268 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1270 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1271 (unsigned long long)addr, (unsigned long long)size);
1272 lmb_reserve(lmb, addr, size);
1277 * boot_relocate_fdt - relocate flat device tree
1278 * @lmb: pointer to lmb handle, will be used for memory mgmt
1279 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1280 * @of_size: pointer to a ulong variable, will hold fdt length
1282 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1283 * relocates the of_flat_tree into that region, even if the fdt is already in
1284 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1287 * of_flat_tree and of_size are set to final (after relocation) values
1293 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1295 void *fdt_blob = *of_flat_tree;
1296 void *of_start = NULL;
1300 int disable_relocation = 0;
1306 if (fdt_check_header(fdt_blob) != 0) {
1307 fdt_error("image is not a fdt");
1311 /* position on a 4K boundary before the alloc_current */
1312 /* Pad the FDT by a specified amount */
1313 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1315 /* If fdt_high is set use it to select the relocation address */
1316 fdt_high = getenv("fdt_high");
1318 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1320 if (((ulong) desired_addr) == ~0UL) {
1321 /* All ones means use fdt in place */
1322 of_start = fdt_blob;
1323 lmb_reserve(lmb, (ulong)of_start, of_len);
1324 disable_relocation = 1;
1325 } else if (desired_addr) {
1327 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1328 (ulong)desired_addr);
1329 if (of_start == NULL) {
1330 puts("Failed using fdt_high value for Device Tree");
1335 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
1339 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1340 getenv_bootm_mapsize()
1341 + getenv_bootm_low());
1344 if (of_start == NULL) {
1345 puts("device tree - allocation error\n");
1349 if (disable_relocation) {
1350 /* We assume there is space after the existing fdt to use for padding */
1351 fdt_set_totalsize(of_start, of_len);
1352 printf(" Using Device Tree in place at %p, end %p\n",
1353 of_start, of_start + of_len - 1);
1355 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1356 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1358 printf(" Loading Device Tree to %p, end %p ... ",
1359 of_start, of_start + of_len - 1);
1361 err = fdt_open_into(fdt_blob, of_start, of_len);
1363 fdt_error("fdt move failed");
1369 *of_flat_tree = of_start;
1372 set_working_fdt_addr(*of_flat_tree);
1378 #endif /* CONFIG_OF_LIBFDT */
1381 * boot_get_fdt - main fdt handling routine
1382 * @argc: command argument count
1383 * @argv: command argument list
1384 * @images: pointer to the bootm images structure
1385 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1386 * @of_size: pointer to a ulong variable, will hold fdt length
1388 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1389 * Curently supported are the following ramdisk sources:
1390 * - multicomponent kernel/ramdisk image,
1391 * - commandline provided address of decicated ramdisk image.
1394 * 0, if fdt image was found and valid, or skipped
1395 * of_flat_tree and of_size are set to fdt start address and length if
1396 * fdt image is found and valid
1398 * 1, if fdt image is found but corrupted
1399 * of_flat_tree and of_size are set to 0 if no fdt exists
1401 int boot_get_fdt(int flag, int argc, char * const argv[],
1402 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
1404 const image_header_t *fdt_hdr;
1406 char *fdt_blob = NULL;
1407 ulong image_start, image_data, image_end;
1408 ulong load_start, load_end;
1409 #if defined(CONFIG_FIT)
1411 const char *fit_uname_config = NULL;
1412 const char *fit_uname_fdt = NULL;
1420 *of_flat_tree = NULL;
1423 if (argc > 3 || genimg_has_config(images)) {
1424 #if defined(CONFIG_FIT)
1427 * If the FDT blob comes from the FIT image and the
1428 * FIT image address is omitted in the command line
1429 * argument, try to use ramdisk or os FIT image
1430 * address or default load address.
1432 if (images->fit_uname_rd)
1433 default_addr = (ulong)images->fit_hdr_rd;
1434 else if (images->fit_uname_os)
1435 default_addr = (ulong)images->fit_hdr_os;
1437 default_addr = load_addr;
1439 if (fit_parse_conf(argv[3], default_addr,
1440 &fdt_addr, &fit_uname_config)) {
1441 debug("* fdt: config '%s' from image at "
1443 fit_uname_config, fdt_addr);
1444 } else if (fit_parse_subimage(argv[3], default_addr,
1445 &fdt_addr, &fit_uname_fdt)) {
1446 debug("* fdt: subimage '%s' from image at "
1448 fit_uname_fdt, fdt_addr);
1452 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1453 debug("* fdt: cmdline image address = "
1457 #if defined(CONFIG_FIT)
1459 /* use FIT configuration provided in first bootm
1462 fdt_addr = (ulong)images->fit_hdr_os;
1463 fit_uname_config = images->fit_uname_cfg;
1464 debug("* fdt: using config '%s' from image "
1466 fit_uname_config, fdt_addr);
1469 * Check whether configuration has FDT blob defined,
1470 * if not quit silently.
1472 fit_hdr = (void *)fdt_addr;
1473 cfg_noffset = fit_conf_get_node(fit_hdr,
1475 if (cfg_noffset < 0) {
1476 debug("* fdt: no such config\n");
1480 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
1482 if (fdt_noffset < 0) {
1483 debug("* fdt: no fdt in config\n");
1489 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1492 /* copy from dataflash if needed */
1493 fdt_addr = genimg_get_image(fdt_addr);
1496 * Check if there is an FDT image at the
1497 * address provided in the second bootm argument
1498 * check image type, for FIT images get a FIT node.
1500 switch (genimg_get_format((void *)fdt_addr)) {
1501 case IMAGE_FORMAT_LEGACY:
1502 /* verify fdt_addr points to a valid image header */
1503 printf("## Flattened Device Tree from Legacy Image "
1506 fdt_hdr = image_get_fdt(fdt_addr);
1511 * move image data to the load address,
1512 * make sure we don't overwrite initial image
1514 image_start = (ulong)fdt_hdr;
1515 image_data = (ulong)image_get_data(fdt_hdr);
1516 image_end = image_get_image_end(fdt_hdr);
1518 load_start = image_get_load(fdt_hdr);
1519 load_end = load_start + image_get_data_size(fdt_hdr);
1521 if (load_start == image_start ||
1522 load_start == image_data) {
1523 fdt_blob = (char *)image_data;
1527 if ((load_start < image_end) && (load_end > image_start)) {
1528 fdt_error("fdt overwritten");
1532 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
1533 image_data, load_start);
1535 memmove((void *)load_start,
1537 image_get_data_size(fdt_hdr));
1539 fdt_blob = (char *)load_start;
1541 case IMAGE_FORMAT_FIT:
1543 * This case will catch both: new uImage format
1544 * (libfdt based) and raw FDT blob (also libfdt
1547 #if defined(CONFIG_FIT)
1548 /* check FDT blob vs FIT blob */
1549 if (fit_check_format((const void *)fdt_addr)) {
1553 fit_hdr = (void *)fdt_addr;
1554 printf("## Flattened Device Tree from FIT "
1558 if (!fit_uname_fdt) {
1560 * no FDT blob image node unit name,
1561 * try to get config node first. If
1562 * config unit node name is NULL
1563 * fit_conf_get_node() will try to
1564 * find default config node
1566 cfg_noffset = fit_conf_get_node(fit_hdr,
1569 if (cfg_noffset < 0) {
1570 fdt_error("Could not find "
1576 fit_uname_config = fdt_get_name(fit_hdr,
1578 printf(" Using '%s' configuration\n",
1581 fdt_noffset = fit_conf_get_fdt_node(
1584 fit_uname_fdt = fit_get_name(fit_hdr,
1587 /* get FDT component image node offset */
1588 fdt_noffset = fit_image_get_node(
1592 if (fdt_noffset < 0) {
1593 fdt_error("Could not find subimage "
1598 printf(" Trying '%s' FDT blob subimage\n",
1601 if (!fit_check_fdt(fit_hdr, fdt_noffset,
1605 /* get ramdisk image data address and length */
1606 if (fit_image_get_data(fit_hdr, fdt_noffset,
1608 fdt_error("Could not find FDT "
1613 /* verift that image data is a proper FDT blob */
1614 if (fdt_check_header((char *)data) != 0) {
1615 fdt_error("Subimage data is not a FTD");
1620 * move image data to the load address,
1621 * make sure we don't overwrite initial image
1623 image_start = (ulong)fit_hdr;
1624 image_end = fit_get_end(fit_hdr);
1626 if (fit_image_get_load(fit_hdr, fdt_noffset,
1627 &load_start) == 0) {
1628 load_end = load_start + size;
1630 if ((load_start < image_end) &&
1631 (load_end > image_start)) {
1632 fdt_error("FDT overwritten");
1636 printf(" Loading FDT from 0x%08lx "
1641 memmove((void *)load_start,
1642 (void *)data, size);
1644 fdt_blob = (char *)load_start;
1646 fdt_blob = (char *)data;
1649 images->fit_hdr_fdt = fit_hdr;
1650 images->fit_uname_fdt = fit_uname_fdt;
1651 images->fit_noffset_fdt = fdt_noffset;
1659 fdt_blob = (char *)fdt_addr;
1660 debug("* fdt: raw FDT blob\n");
1661 printf("## Flattened Device Tree blob at "
1662 "%08lx\n", (long)fdt_blob);
1666 puts("ERROR: Did not find a cmdline Flattened Device "
1671 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
1673 } else if (images->legacy_hdr_valid &&
1674 image_check_type(&images->legacy_hdr_os_copy,
1677 ulong fdt_data, fdt_len;
1680 * Now check if we have a legacy multi-component image,
1681 * get second entry data start address and len.
1683 printf("## Flattened Device Tree from multi "
1684 "component Image at %08lX\n",
1685 (ulong)images->legacy_hdr_os);
1687 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1691 fdt_blob = (char *)fdt_data;
1692 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
1694 if (fdt_check_header(fdt_blob) != 0) {
1695 fdt_error("image is not a fdt");
1699 if (fdt_totalsize(fdt_blob) != fdt_len) {
1700 fdt_error("fdt size != image size");
1704 debug("## No Flattened Device Tree\n");
1708 debug("## No Flattened Device Tree\n");
1712 *of_flat_tree = fdt_blob;
1713 *of_size = fdt_totalsize(fdt_blob);
1714 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
1715 (ulong)*of_flat_tree, *of_size);
1720 *of_flat_tree = NULL;
1724 #endif /* CONFIG_OF_LIBFDT */
1726 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1728 * boot_get_cmdline - allocate and initialize kernel cmdline
1729 * @lmb: pointer to lmb handle, will be used for memory mgmt
1730 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1731 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1733 * boot_get_cmdline() allocates space for kernel command line below
1734 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1735 * variable is present its contents is copied to allocated kernel
1742 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1747 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1748 getenv_bootm_mapsize() + getenv_bootm_low());
1750 if (cmdline == NULL)
1753 if ((s = getenv("bootargs")) == NULL)
1758 *cmd_start = (ulong) & cmdline[0];
1759 *cmd_end = *cmd_start + strlen(cmdline);
1761 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1765 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1767 #ifdef CONFIG_SYS_BOOT_GET_KBD
1769 * boot_get_kbd - allocate and initialize kernel copy of board info
1770 * @lmb: pointer to lmb handle, will be used for memory mgmt
1771 * @kbd: double pointer to board info data
1773 * boot_get_kbd() allocates space for kernel copy of board info data below
1774 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1775 * with the current u-boot board info data.
1781 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1783 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1784 getenv_bootm_mapsize() + getenv_bootm_low());
1790 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1792 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1793 do_bdinfo(NULL, 0, 0, NULL);
1798 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1799 #endif /* !USE_HOSTCC */
1801 #if defined(CONFIG_FIT)
1802 /*****************************************************************************/
1803 /* New uImage format routines */
1804 /*****************************************************************************/
1806 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
1807 ulong *addr, const char **name)
1814 sep = strchr(spec, sepc);
1817 *addr = simple_strtoul(spec, NULL, 16);
1827 * fit_parse_conf - parse FIT configuration spec
1828 * @spec: input string, containing configuration spec
1829 * @add_curr: current image address (to be used as a possible default)
1830 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1832 * @conf_name double pointer to a char, will hold pointer to a configuration
1835 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1836 * where <addr> is a FIT image address that contains configuration
1837 * with a <conf> unit name.
1839 * Address part is optional, and if omitted default add_curr will
1843 * 1 if spec is a valid configuration string,
1844 * addr and conf_name are set accordingly
1847 int fit_parse_conf(const char *spec, ulong addr_curr,
1848 ulong *addr, const char **conf_name)
1850 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
1854 * fit_parse_subimage - parse FIT subimage spec
1855 * @spec: input string, containing subimage spec
1856 * @add_curr: current image address (to be used as a possible default)
1857 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1859 * @image_name: double pointer to a char, will hold pointer to a subimage name
1861 * fit_parse_subimage() expects subimage spec in the for of
1862 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1863 * subimage with a <subimg> unit name.
1865 * Address part is optional, and if omitted default add_curr will
1869 * 1 if spec is a valid subimage string,
1870 * addr and image_name are set accordingly
1873 int fit_parse_subimage(const char *spec, ulong addr_curr,
1874 ulong *addr, const char **image_name)
1876 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
1878 #endif /* !USE_HOSTCC */
1880 static void fit_get_debug(const void *fit, int noffset,
1881 char *prop_name, int err)
1883 debug("Can't get '%s' property from FIT 0x%08lx, "
1884 "node: offset %d, name %s (%s)\n",
1885 prop_name, (ulong)fit, noffset,
1886 fit_get_name(fit, noffset, NULL),
1891 * fit_print_contents - prints out the contents of the FIT format image
1892 * @fit: pointer to the FIT format image header
1893 * @p: pointer to prefix string
1895 * fit_print_contents() formats a multi line FIT image contents description.
1896 * The routine prints out FIT image properties (root node level) follwed by
1897 * the details of each component image.
1900 * no returned results
1902 void fit_print_contents(const void *fit)
1913 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1923 /* Root node properties */
1924 ret = fit_get_desc(fit, 0, &desc);
1925 printf("%sFIT description: ", p);
1927 printf("unavailable\n");
1929 printf("%s\n", desc);
1931 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1932 ret = fit_get_timestamp(fit, 0, ×tamp);
1933 printf("%sCreated: ", p);
1935 printf("unavailable\n");
1937 genimg_print_time(timestamp);
1940 /* Find images parent node offset */
1941 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1942 if (images_noffset < 0) {
1943 printf("Can't find images parent node '%s' (%s)\n",
1944 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1948 /* Process its subnodes, print out component images details */
1949 for (ndepth = 0, count = 0,
1950 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1951 (noffset >= 0) && (ndepth > 0);
1952 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1955 * Direct child node of the images parent node,
1956 * i.e. component image node.
1958 printf("%s Image %u (%s)\n", p, count++,
1959 fit_get_name(fit, noffset, NULL));
1961 fit_image_print(fit, noffset, p);
1965 /* Find configurations parent node offset */
1966 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1967 if (confs_noffset < 0) {
1968 debug("Can't get configurations parent node '%s' (%s)\n",
1969 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1973 /* get default configuration unit name from default property */
1974 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
1976 printf("%s Default Configuration: '%s'\n", p, uname);
1978 /* Process its subnodes, print out configurations details */
1979 for (ndepth = 0, count = 0,
1980 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1981 (noffset >= 0) && (ndepth > 0);
1982 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1985 * Direct child node of the configurations parent node,
1986 * i.e. configuration node.
1988 printf("%s Configuration %u (%s)\n", p, count++,
1989 fit_get_name(fit, noffset, NULL));
1991 fit_conf_print(fit, noffset, p);
1997 * fit_image_print - prints out the FIT component image details
1998 * @fit: pointer to the FIT format image header
1999 * @image_noffset: offset of the component image node
2000 * @p: pointer to prefix string
2002 * fit_image_print() lists all mandatory properies for the processed component
2003 * image. If present, hash nodes are printed out as well. Load
2004 * address for images of type firmware is also printed out. Since the load
2005 * address is not mandatory for firmware images, it will be output as
2006 * "unavailable" when not present.
2009 * no returned results
2011 void fit_image_print(const void *fit, int image_noffset, const char *p)
2014 uint8_t type, arch, os, comp;
2022 /* Mandatory properties */
2023 ret = fit_get_desc(fit, image_noffset, &desc);
2024 printf("%s Description: ", p);
2026 printf("unavailable\n");
2028 printf("%s\n", desc);
2030 fit_image_get_type(fit, image_noffset, &type);
2031 printf("%s Type: %s\n", p, genimg_get_type_name(type));
2033 fit_image_get_comp(fit, image_noffset, &comp);
2034 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
2036 ret = fit_image_get_data(fit, image_noffset, &data, &size);
2039 printf("%s Data Start: ", p);
2041 printf("unavailable\n");
2043 printf("0x%08lx\n", (ulong)data);
2046 printf("%s Data Size: ", p);
2048 printf("unavailable\n");
2050 genimg_print_size(size);
2052 /* Remaining, type dependent properties */
2053 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2054 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2055 (type == IH_TYPE_FLATDT)) {
2056 fit_image_get_arch(fit, image_noffset, &arch);
2057 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
2060 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
2061 fit_image_get_os(fit, image_noffset, &os);
2062 printf("%s OS: %s\n", p, genimg_get_os_name(os));
2065 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2066 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
2067 ret = fit_image_get_load(fit, image_noffset, &load);
2068 printf("%s Load Address: ", p);
2070 printf("unavailable\n");
2072 printf("0x%08lx\n", load);
2075 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2076 (type == IH_TYPE_RAMDISK)) {
2077 fit_image_get_entry(fit, image_noffset, &entry);
2078 printf("%s Entry Point: ", p);
2080 printf("unavailable\n");
2082 printf("0x%08lx\n", entry);
2085 /* Process all hash subnodes of the component image node */
2086 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2087 (noffset >= 0) && (ndepth > 0);
2088 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2090 /* Direct child node of the component image node */
2091 fit_image_print_hash(fit, noffset, p);
2097 * fit_image_print_hash - prints out the hash node details
2098 * @fit: pointer to the FIT format image header
2099 * @noffset: offset of the hash node
2100 * @p: pointer to prefix string
2102 * fit_image_print_hash() lists properies for the processed hash node
2105 * no returned results
2107 void fit_image_print_hash(const void *fit, int noffset, const char *p)
2115 * Check subnode name, must be equal to "hash".
2116 * Multiple hash nodes require unique unit node
2117 * names, e.g. hash@1, hash@2, etc.
2119 if (strncmp(fit_get_name(fit, noffset, NULL),
2121 strlen(FIT_HASH_NODENAME)) != 0)
2124 debug("%s Hash node: '%s'\n", p,
2125 fit_get_name(fit, noffset, NULL));
2127 printf("%s Hash algo: ", p);
2128 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2129 printf("invalid/unsupported\n");
2132 printf("%s\n", algo);
2134 ret = fit_image_hash_get_value(fit, noffset, &value,
2136 printf("%s Hash value: ", p);
2138 printf("unavailable\n");
2140 for (i = 0; i < value_len; i++)
2141 printf("%02x", value[i]);
2145 debug("%s Hash len: %d\n", p, value_len);
2149 * fit_get_desc - get node description property
2150 * @fit: pointer to the FIT format image header
2151 * @noffset: node offset
2152 * @desc: double pointer to the char, will hold pointer to the descrption
2154 * fit_get_desc() reads description property from a given node, if
2155 * description is found pointer to it is returened in third call argument.
2161 int fit_get_desc(const void *fit, int noffset, char **desc)
2165 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
2166 if (*desc == NULL) {
2167 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
2175 * fit_get_timestamp - get node timestamp property
2176 * @fit: pointer to the FIT format image header
2177 * @noffset: node offset
2178 * @timestamp: pointer to the time_t, will hold read timestamp
2180 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2181 * is found and has a correct size its value is retured in third call
2186 * -1, on property read failure
2187 * -2, on wrong timestamp size
2189 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
2194 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
2196 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
2199 if (len != sizeof(uint32_t)) {
2200 debug("FIT timestamp with incorrect size of (%u)\n", len);
2204 *timestamp = uimage_to_cpu(*((uint32_t *)data));
2209 * fit_image_get_node - get node offset for component image of a given unit name
2210 * @fit: pointer to the FIT format image header
2211 * @image_uname: component image node unit name
2213 * fit_image_get_node() finds a component image (withing the '/images'
2214 * node) of a provided unit name. If image is found its node offset is
2215 * returned to the caller.
2218 * image node offset when found (>=0)
2219 * negative number on failure (FDT_ERR_* code)
2221 int fit_image_get_node(const void *fit, const char *image_uname)
2223 int noffset, images_noffset;
2225 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2226 if (images_noffset < 0) {
2227 debug("Can't find images parent node '%s' (%s)\n",
2228 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2229 return images_noffset;
2232 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
2234 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2235 image_uname, fdt_strerror(noffset));
2242 * fit_image_get_os - get os id for a given component image node
2243 * @fit: pointer to the FIT format image header
2244 * @noffset: component image node offset
2245 * @os: pointer to the uint8_t, will hold os numeric id
2247 * fit_image_get_os() finds os property in a given component image node.
2248 * If the property is found, its (string) value is translated to the numeric
2249 * id which is returned to the caller.
2255 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
2260 /* Get OS name from property data */
2261 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
2263 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
2268 /* Translate OS name to id */
2269 *os = genimg_get_os_id(data);
2274 * fit_image_get_arch - get arch id for a given component image node
2275 * @fit: pointer to the FIT format image header
2276 * @noffset: component image node offset
2277 * @arch: pointer to the uint8_t, will hold arch numeric id
2279 * fit_image_get_arch() finds arch property in a given component image node.
2280 * If the property is found, its (string) value is translated to the numeric
2281 * id which is returned to the caller.
2287 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
2292 /* Get architecture name from property data */
2293 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
2295 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
2300 /* Translate architecture name to id */
2301 *arch = genimg_get_arch_id(data);
2306 * fit_image_get_type - get type id for a given component image node
2307 * @fit: pointer to the FIT format image header
2308 * @noffset: component image node offset
2309 * @type: pointer to the uint8_t, will hold type numeric id
2311 * fit_image_get_type() finds type property in a given component image node.
2312 * If the property is found, its (string) value is translated to the numeric
2313 * id which is returned to the caller.
2319 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
2324 /* Get image type name from property data */
2325 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
2327 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
2332 /* Translate image type name to id */
2333 *type = genimg_get_type_id(data);
2338 * fit_image_get_comp - get comp id for a given component image node
2339 * @fit: pointer to the FIT format image header
2340 * @noffset: component image node offset
2341 * @comp: pointer to the uint8_t, will hold comp numeric id
2343 * fit_image_get_comp() finds comp property in a given component image node.
2344 * If the property is found, its (string) value is translated to the numeric
2345 * id which is returned to the caller.
2351 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
2356 /* Get compression name from property data */
2357 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
2359 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
2364 /* Translate compression name to id */
2365 *comp = genimg_get_comp_id(data);
2370 * fit_image_get_load - get load address property for a given component image node
2371 * @fit: pointer to the FIT format image header
2372 * @noffset: component image node offset
2373 * @load: pointer to the uint32_t, will hold load address
2375 * fit_image_get_load() finds load address property in a given component image node.
2376 * If the property is found, its value is returned to the caller.
2382 int fit_image_get_load(const void *fit, int noffset, ulong *load)
2385 const uint32_t *data;
2387 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
2389 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
2393 *load = uimage_to_cpu(*data);
2398 * fit_image_get_entry - get entry point address property for a given component image node
2399 * @fit: pointer to the FIT format image header
2400 * @noffset: component image node offset
2401 * @entry: pointer to the uint32_t, will hold entry point address
2403 * fit_image_get_entry() finds entry point address property in a given component image node.
2404 * If the property is found, its value is returned to the caller.
2410 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
2413 const uint32_t *data;
2415 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
2417 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
2421 *entry = uimage_to_cpu(*data);
2426 * fit_image_get_data - get data property and its size for a given component image node
2427 * @fit: pointer to the FIT format image header
2428 * @noffset: component image node offset
2429 * @data: double pointer to void, will hold data property's data address
2430 * @size: pointer to size_t, will hold data property's data size
2432 * fit_image_get_data() finds data property in a given component image node.
2433 * If the property is found its data start address and size are returned to
2440 int fit_image_get_data(const void *fit, int noffset,
2441 const void **data, size_t *size)
2445 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
2446 if (*data == NULL) {
2447 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
2457 * fit_image_hash_get_algo - get hash algorithm name
2458 * @fit: pointer to the FIT format image header
2459 * @noffset: hash node offset
2460 * @algo: double pointer to char, will hold pointer to the algorithm name
2462 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2463 * If the property is found its data start address is returned to the caller.
2469 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
2473 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
2474 if (*algo == NULL) {
2475 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
2483 * fit_image_hash_get_value - get hash value and length
2484 * @fit: pointer to the FIT format image header
2485 * @noffset: hash node offset
2486 * @value: double pointer to uint8_t, will hold address of a hash value data
2487 * @value_len: pointer to an int, will hold hash data length
2489 * fit_image_hash_get_value() finds hash value property in a given hash node.
2490 * If the property is found its data start address and size are returned to
2497 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
2502 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
2503 if (*value == NULL) {
2504 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
2515 * fit_image_hash_get_ignore - get hash ignore flag
2516 * @fit: pointer to the FIT format image header
2517 * @noffset: hash node offset
2518 * @ignore: pointer to an int, will hold hash ignore flag
2520 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
2521 * If the property is found and non-zero, the hash algorithm is not verified by
2522 * u-boot automatically.
2525 * 0, on ignore not found
2526 * value, on ignore found
2528 int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
2533 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
2534 if (value == NULL || len != sizeof(int))
2544 * fit_set_timestamp - set node timestamp property
2545 * @fit: pointer to the FIT format image header
2546 * @noffset: node offset
2547 * @timestamp: timestamp value to be set
2549 * fit_set_timestamp() attempts to set timestamp property in the requested
2550 * node and returns operation status to the caller.
2554 * -1, on property read failure
2556 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
2561 t = cpu_to_uimage(timestamp);
2562 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2565 printf("Can't set '%s' property for '%s' node (%s)\n",
2566 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2575 * calculate_hash - calculate and return hash for provided input data
2576 * @data: pointer to the input data
2577 * @data_len: data length
2578 * @algo: requested hash algorithm
2579 * @value: pointer to the char, will hold hash value data (caller must
2580 * allocate enough free space)
2581 * value_len: length of the calculated hash
2583 * calculate_hash() computes input data hash according to the requested algorithm.
2584 * Resulting hash value is placed in caller provided 'value' buffer, length
2585 * of the calculated hash is returned via value_len pointer argument.
2589 * -1, when algo is unsupported
2591 static int calculate_hash(const void *data, int data_len, const char *algo,
2592 uint8_t *value, int *value_len)
2594 if (strcmp(algo, "crc32") == 0) {
2595 *((uint32_t *)value) = crc32_wd(0, data, data_len,
2597 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
2599 } else if (strcmp(algo, "sha1") == 0) {
2600 sha1_csum_wd((unsigned char *) data, data_len,
2601 (unsigned char *) value, CHUNKSZ_SHA1);
2603 } else if (strcmp(algo, "md5") == 0) {
2604 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2607 debug("Unsupported hash alogrithm\n");
2615 * fit_set_hashes - process FIT component image nodes and calculate hashes
2616 * @fit: pointer to the FIT format image header
2618 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2619 * Hashes are calculated for all component images which have hash subnodes
2620 * with algorithm property set to one of the supported hash algorithms.
2624 * libfdt error code, on failure
2626 int fit_set_hashes(void *fit)
2633 /* Find images parent node offset */
2634 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2635 if (images_noffset < 0) {
2636 printf("Can't find images parent node '%s' (%s)\n",
2637 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2638 return images_noffset;
2641 /* Process its subnodes, print out component images details */
2642 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
2643 (noffset >= 0) && (ndepth > 0);
2644 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2647 * Direct child node of the images parent node,
2648 * i.e. component image node.
2650 ret = fit_image_set_hashes(fit, noffset);
2660 * fit_image_set_hashes - calculate/set hashes for given component image node
2661 * @fit: pointer to the FIT format image header
2662 * @image_noffset: requested component image node
2664 * fit_image_set_hashes() adds hash values for an component image node. All
2665 * existing hash subnodes are checked, if algorithm property is set to one of
2666 * the supported hash algorithms, hash value is computed and corresponding
2667 * hash node property is set, for example:
2669 * Input component image node structure:
2671 * o image@1 (at image_noffset)
2672 * | - data = [binary data]
2676 * Output component image node structure:
2678 * o image@1 (at image_noffset)
2679 * | - data = [binary data]
2682 * |- value = sha1(data)
2688 int fit_image_set_hashes(void *fit, int image_noffset)
2693 uint8_t value[FIT_MAX_HASH_LEN];
2698 /* Get image data and data length */
2699 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2700 printf("Can't get image data/size\n");
2704 /* Process all hash subnodes of the component image node */
2705 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2706 (noffset >= 0) && (ndepth > 0);
2707 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2709 /* Direct child node of the component image node */
2712 * Check subnode name, must be equal to "hash".
2713 * Multiple hash nodes require unique unit node
2714 * names, e.g. hash@1, hash@2, etc.
2716 if (strncmp(fit_get_name(fit, noffset, NULL),
2718 strlen(FIT_HASH_NODENAME)) != 0) {
2719 /* Not a hash subnode, skip it */
2723 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2724 printf("Can't get hash algo property for "
2725 "'%s' hash node in '%s' image node\n",
2726 fit_get_name(fit, noffset, NULL),
2727 fit_get_name(fit, image_noffset, NULL));
2731 if (calculate_hash(data, size, algo, value,
2733 printf("Unsupported hash algorithm (%s) for "
2734 "'%s' hash node in '%s' image node\n",
2735 algo, fit_get_name(fit, noffset, NULL),
2736 fit_get_name(fit, image_noffset,
2741 if (fit_image_hash_set_value(fit, noffset, value,
2743 printf("Can't set hash value for "
2744 "'%s' hash node in '%s' image node\n",
2745 fit_get_name(fit, noffset, NULL),
2746 fit_get_name(fit, image_noffset, NULL));
2756 * fit_image_hash_set_value - set hash value in requested has node
2757 * @fit: pointer to the FIT format image header
2758 * @noffset: hash node offset
2759 * @value: hash value to be set
2760 * @value_len: hash value length
2762 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2763 * given and returns operation status to the caller.
2769 int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
2774 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
2776 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2777 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2784 #endif /* USE_HOSTCC */
2787 * fit_image_check_hashes - verify data intergity
2788 * @fit: pointer to the FIT format image header
2789 * @image_noffset: component image node offset
2791 * fit_image_check_hashes() goes over component image hash nodes,
2792 * re-calculates each data hash and compares with the value stored in hash
2796 * 1, if all hashes are valid
2797 * 0, otherwise (or on error)
2799 int fit_image_check_hashes(const void *fit, int image_noffset)
2809 uint8_t value[FIT_MAX_HASH_LEN];
2815 /* Get image data and data length */
2816 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2817 printf("Can't get image data/size\n");
2821 /* Process all hash subnodes of the component image node */
2822 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2823 (noffset >= 0) && (ndepth > 0);
2824 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2826 /* Direct child node of the component image node */
2829 * Check subnode name, must be equal to "hash".
2830 * Multiple hash nodes require unique unit node
2831 * names, e.g. hash@1, hash@2, etc.
2833 if (strncmp(fit_get_name(fit, noffset, NULL),
2835 strlen(FIT_HASH_NODENAME)) != 0)
2838 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2839 err_msg = " error!\nCan't get hash algo "
2846 fit_image_hash_get_ignore(fit, noffset, &ignore);
2848 printf("-skipped ");
2853 if (fit_image_hash_get_value(fit, noffset, &fit_value,
2855 err_msg = " error!\nCan't get hash value "
2860 if (calculate_hash(data, size, algo, value,
2862 err_msg = " error!\n"
2863 "Unsupported hash algorithm";
2867 if (value_len != fit_value_len) {
2868 err_msg = " error !\nBad hash value len";
2870 } else if (memcmp(value, fit_value, value_len) != 0) {
2871 err_msg = " error!\nBad hash value";
2878 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
2879 err_msg = " error!\nCorrupted or truncated tree";
2886 printf("%s for '%s' hash node in '%s' image node\n",
2887 err_msg, fit_get_name(fit, noffset, NULL),
2888 fit_get_name(fit, image_noffset, NULL));
2893 * fit_all_image_check_hashes - verify data intergity for all images
2894 * @fit: pointer to the FIT format image header
2896 * fit_all_image_check_hashes() goes over all images in the FIT and
2897 * for every images checks if all it's hashes are valid.
2900 * 1, if all hashes of all images are valid
2901 * 0, otherwise (or on error)
2903 int fit_all_image_check_hashes(const void *fit)
2910 /* Find images parent node offset */
2911 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2912 if (images_noffset < 0) {
2913 printf("Can't find images parent node '%s' (%s)\n",
2914 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2918 /* Process all image subnodes, check hashes for each */
2919 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
2921 for (ndepth = 0, count = 0,
2922 noffset = fdt_next_node(fit, images_noffset, &ndepth);
2923 (noffset >= 0) && (ndepth > 0);
2924 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2927 * Direct child node of the images parent node,
2928 * i.e. component image node.
2930 printf(" Hash(es) for Image %u (%s): ", count++,
2931 fit_get_name(fit, noffset, NULL));
2933 if (!fit_image_check_hashes(fit, noffset))
2942 * fit_image_check_os - check whether image node is of a given os type
2943 * @fit: pointer to the FIT format image header
2944 * @noffset: component image node offset
2945 * @os: requested image os
2947 * fit_image_check_os() reads image os property and compares its numeric
2948 * id with the requested os. Comparison result is returned to the caller.
2951 * 1 if image is of given os type
2952 * 0 otherwise (or on error)
2954 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
2958 if (fit_image_get_os(fit, noffset, &image_os))
2960 return (os == image_os);
2964 * fit_image_check_arch - check whether image node is of a given arch
2965 * @fit: pointer to the FIT format image header
2966 * @noffset: component image node offset
2967 * @arch: requested imagearch
2969 * fit_image_check_arch() reads image arch property and compares its numeric
2970 * id with the requested arch. Comparison result is returned to the caller.
2973 * 1 if image is of given arch
2974 * 0 otherwise (or on error)
2976 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
2980 if (fit_image_get_arch(fit, noffset, &image_arch))
2982 return (arch == image_arch);
2986 * fit_image_check_type - check whether image node is of a given type
2987 * @fit: pointer to the FIT format image header
2988 * @noffset: component image node offset
2989 * @type: requested image type
2991 * fit_image_check_type() reads image type property and compares its numeric
2992 * id with the requested type. Comparison result is returned to the caller.
2995 * 1 if image is of given type
2996 * 0 otherwise (or on error)
2998 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
3002 if (fit_image_get_type(fit, noffset, &image_type))
3004 return (type == image_type);
3008 * fit_image_check_comp - check whether image node uses given compression
3009 * @fit: pointer to the FIT format image header
3010 * @noffset: component image node offset
3011 * @comp: requested image compression type
3013 * fit_image_check_comp() reads image compression property and compares its
3014 * numeric id with the requested compression type. Comparison result is
3015 * returned to the caller.
3018 * 1 if image uses requested compression
3019 * 0 otherwise (or on error)
3021 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
3025 if (fit_image_get_comp(fit, noffset, &image_comp))
3027 return (comp == image_comp);
3031 * fit_check_format - sanity check FIT image format
3032 * @fit: pointer to the FIT format image header
3034 * fit_check_format() runs a basic sanity FIT image verification.
3035 * Routine checks for mandatory properties, nodes, etc.
3041 int fit_check_format(const void *fit)
3043 /* mandatory / node 'description' property */
3044 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
3045 debug("Wrong FIT format: no description\n");
3049 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
3050 /* mandatory / node 'timestamp' property */
3051 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
3052 debug("Wrong FIT format: no timestamp\n");
3057 /* mandatory subimages parent '/images' node */
3058 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
3059 debug("Wrong FIT format: no images parent node\n");
3068 * fit_conf_find_compat
3069 * @fit: pointer to the FIT format image header
3070 * @fdt: pointer to the device tree to compare against
3072 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
3073 * most compatible with the passed in device tree.
3082 * |-o configurations
3090 * |-compatible = "foo,bar", "bim,bam"
3093 * |-compatible = "foo,bar",
3096 * |-compatible = "bim,bam", "baz,biz"
3098 * Configuration 1 would be picked because the first string in U-Boot's
3099 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
3100 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
3103 * offset to the configuration to use if one was found
3106 int fit_conf_find_compat(const void *fit, const void *fdt)
3109 int noffset, confs_noffset, images_noffset;
3110 const void *fdt_compat;
3112 int best_match_offset = 0;
3113 int best_match_pos = 0;
3115 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3116 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
3117 if (confs_noffset < 0 || images_noffset < 0) {
3118 debug("Can't find configurations or images nodes.\n");
3122 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
3124 debug("Fdt for comparison has no \"compatible\" property.\n");
3129 * Loop over the configurations in the FIT image.
3131 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
3132 (noffset >= 0) && (ndepth > 0);
3133 noffset = fdt_next_node(fit, noffset, &ndepth)) {
3135 const char *kfdt_name;
3137 const char *cur_fdt_compat;
3145 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
3147 debug("No fdt property found.\n");
3150 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
3152 if (kfdt_noffset < 0) {
3153 debug("No image node named \"%s\" found.\n",
3158 * Get a pointer to this configuration's fdt.
3160 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
3161 debug("Failed to get fdt \"%s\".\n", kfdt_name);
3165 len = fdt_compat_len;
3166 cur_fdt_compat = fdt_compat;
3168 * Look for a match for each U-Boot compatibility string in
3169 * turn in this configuration's fdt.
3171 for (i = 0; len > 0 &&
3172 (!best_match_offset || best_match_pos > i); i++) {
3173 int cur_len = strlen(cur_fdt_compat) + 1;
3175 if (!fdt_node_check_compatible(kfdt, 0,
3177 best_match_offset = noffset;
3182 cur_fdt_compat += cur_len;
3185 if (!best_match_offset) {
3186 debug("No match found.\n");
3190 return best_match_offset;
3194 * fit_conf_get_node - get node offset for configuration of a given unit name
3195 * @fit: pointer to the FIT format image header
3196 * @conf_uname: configuration node unit name
3198 * fit_conf_get_node() finds a configuration (withing the '/configurations'
3199 * parant node) of a provided unit name. If configuration is found its node offset
3200 * is returned to the caller.
3202 * When NULL is provided in second argument fit_conf_get_node() will search
3203 * for a default configuration node instead. Default configuration node unit name
3204 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3207 * configuration node offset when found (>=0)
3208 * negative number on failure (FDT_ERR_* code)
3210 int fit_conf_get_node(const void *fit, const char *conf_uname)
3212 int noffset, confs_noffset;
3215 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3216 if (confs_noffset < 0) {
3217 debug("Can't find configurations parent node '%s' (%s)\n",
3218 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
3219 return confs_noffset;
3222 if (conf_uname == NULL) {
3223 /* get configuration unit name from the default property */
3224 debug("No configuration specified, trying default...\n");
3225 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3226 FIT_DEFAULT_PROP, &len);
3227 if (conf_uname == NULL) {
3228 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3232 debug("Found default configuration: '%s'\n", conf_uname);
3235 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
3237 debug("Can't get node offset for configuration unit name: "
3239 conf_uname, fdt_strerror(noffset));
3245 static int __fit_conf_get_prop_node(const void *fit, int noffset,
3246 const char *prop_name)
3251 /* get kernel image unit name from configuration kernel property */
3252 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
3256 return fit_image_get_node(fit, uname);
3260 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3261 * a given configuration
3262 * @fit: pointer to the FIT format image header
3263 * @noffset: configuration node offset
3265 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3266 * configuration FIT_KERNEL_PROP property and translates it to the node
3270 * image node offset when found (>=0)
3271 * negative number on failure (FDT_ERR_* code)
3273 int fit_conf_get_kernel_node(const void *fit, int noffset)
3275 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
3279 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3280 * a given configuration
3281 * @fit: pointer to the FIT format image header
3282 * @noffset: configuration node offset
3284 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3285 * configuration FIT_KERNEL_PROP property and translates it to the node
3289 * image node offset when found (>=0)
3290 * negative number on failure (FDT_ERR_* code)
3292 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
3294 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
3298 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3299 * a given configuration
3300 * @fit: pointer to the FIT format image header
3301 * @noffset: configuration node offset
3303 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3304 * configuration FIT_KERNEL_PROP property and translates it to the node
3308 * image node offset when found (>=0)
3309 * negative number on failure (FDT_ERR_* code)
3311 int fit_conf_get_fdt_node(const void *fit, int noffset)
3313 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
3317 * fit_conf_print - prints out the FIT configuration details
3318 * @fit: pointer to the FIT format image header
3319 * @noffset: offset of the configuration node
3320 * @p: pointer to prefix string
3322 * fit_conf_print() lists all mandatory properies for the processed
3323 * configuration node.
3326 * no returned results
3328 void fit_conf_print(const void *fit, int noffset, const char *p)
3334 /* Mandatory properties */
3335 ret = fit_get_desc(fit, noffset, &desc);
3336 printf("%s Description: ", p);
3338 printf("unavailable\n");
3340 printf("%s\n", desc);
3342 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3343 printf("%s Kernel: ", p);
3345 printf("unavailable\n");
3347 printf("%s\n", uname);
3349 /* Optional properties */
3350 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
3352 printf("%s Init Ramdisk: %s\n", p, uname);
3354 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
3356 printf("%s FDT: %s\n", p, uname);
3360 * fit_check_ramdisk - verify FIT format ramdisk subimage
3361 * @fit_hdr: pointer to the FIT ramdisk header
3362 * @rd_noffset: ramdisk subimage node offset within FIT image
3363 * @arch: requested ramdisk image architecture type
3364 * @verify: data CRC verification flag
3366 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3367 * specified FIT image.
3374 static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3377 fit_image_print(fit, rd_noffset, " ");
3380 puts(" Verifying Hash Integrity ... ");
3381 if (!fit_image_check_hashes(fit, rd_noffset)) {
3382 puts("Bad Data Hash\n");
3383 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
3389 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3390 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3391 !fit_image_check_arch(fit, rd_noffset, arch) ||
3392 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3393 printf("No Linux %s Ramdisk Image\n",
3394 genimg_get_arch_name(arch));
3395 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3399 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
3402 #endif /* USE_HOSTCC */
3403 #endif /* CONFIG_FIT */