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", },
97 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
101 static const table_entry_t uimage_os[] = {
102 { IH_OS_INVALID, NULL, "Invalid OS", },
103 { IH_OS_LINUX, "linux", "Linux", },
104 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
105 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
107 { IH_OS_NETBSD, "netbsd", "NetBSD", },
108 { IH_OS_OSE, "ose", "Enea OSE", },
109 { IH_OS_RTEMS, "rtems", "RTEMS", },
110 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
111 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
112 { IH_OS_QNX, "qnx", "QNX", },
113 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
115 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
116 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
119 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
120 { IH_OS_DELL, "dell", "Dell", },
121 { IH_OS_ESIX, "esix", "Esix", },
122 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
123 { IH_OS_IRIX, "irix", "Irix", },
124 { IH_OS_NCR, "ncr", "NCR", },
125 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
126 { IH_OS_PSOS, "psos", "pSOS", },
127 { IH_OS_SCO, "sco", "SCO", },
128 { IH_OS_SOLARIS, "solaris", "Solaris", },
129 { IH_OS_SVR4, "svr4", "SVR4", },
134 static const table_entry_t uimage_type[] = {
135 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
136 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
137 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
138 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
139 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
140 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
141 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
142 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
143 { IH_TYPE_INVALID, NULL, "Invalid Image", },
144 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
145 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
146 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
147 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
148 { IH_TYPE_SCRIPT, "script", "Script", },
149 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
150 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
154 static const table_entry_t uimage_comp[] = {
155 { IH_COMP_NONE, "none", "uncompressed", },
156 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
157 { IH_COMP_GZIP, "gzip", "gzip compressed", },
158 { IH_COMP_LZMA, "lzma", "lzma compressed", },
159 { IH_COMP_LZO, "lzo", "lzo compressed", },
163 uint32_t crc32(uint32_t, const unsigned char *, uint);
164 uint32_t crc32_wd(uint32_t, const unsigned char *, uint, uint);
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 getenv_bootm_low(void)
421 char *s = getenv("bootm_low");
423 ulong tmp = simple_strtoul(s, NULL, 16);
427 #if defined(CONFIG_SYS_SDRAM_BASE)
428 return CONFIG_SYS_SDRAM_BASE;
429 #elif defined(CONFIG_ARM)
430 return gd->bd->bi_dram[0].start;
436 phys_size_t getenv_bootm_size(void)
439 char *s = getenv("bootm_size");
441 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
444 s = getenv("bootm_low");
446 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
451 #if defined(CONFIG_ARM)
452 return gd->bd->bi_dram[0].size - tmp;
454 return gd->bd->bi_memsize - tmp;
458 phys_size_t getenv_bootm_mapsize(void)
461 char *s = getenv("bootm_mapsize");
463 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
467 #if defined(CONFIG_SYS_BOOTMAPSZ)
468 return CONFIG_SYS_BOOTMAPSZ;
470 return getenv_bootm_size();
474 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
479 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
481 size_t tail = (len > chunksz) ? chunksz : len;
483 memmove(to, from, tail);
488 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
489 memmove(to, from, len);
490 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
492 #endif /* !USE_HOSTCC */
494 void genimg_print_size(uint32_t size)
497 printf("%d Bytes = ", size);
498 print_size(size, "\n");
500 printf("%d Bytes = %.2f kB = %.2f MB\n",
501 size, (double)size / 1.024e3,
502 (double)size / 1.048576e6);
506 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
507 static void genimg_print_time(time_t timestamp)
512 to_tm(timestamp, &tm);
513 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
514 tm.tm_year, tm.tm_mon, tm.tm_mday,
515 tm.tm_hour, tm.tm_min, tm.tm_sec);
517 printf("%s", ctime(×tamp));
520 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
523 * get_table_entry_name - translate entry id to long name
524 * @table: pointer to a translation table for entries of a specific type
525 * @msg: message to be returned when translation fails
526 * @id: entry id to be translated
528 * get_table_entry_name() will go over translation table trying to find
529 * entry that matches given id. If matching entry is found, its long
530 * name is returned to the caller.
533 * long entry name if translation succeeds
536 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
538 for (; table->id >= 0; ++table) {
540 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
543 return table->lname + gd->reloc_off;
549 const char *genimg_get_os_name(uint8_t os)
551 return (get_table_entry_name(uimage_os, "Unknown OS", os));
554 const char *genimg_get_arch_name(uint8_t arch)
556 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
560 const char *genimg_get_type_name(uint8_t type)
562 return (get_table_entry_name(uimage_type, "Unknown Image", type));
565 const char *genimg_get_comp_name(uint8_t comp)
567 return (get_table_entry_name(uimage_comp, "Unknown Compression",
572 * get_table_entry_id - translate short entry name to id
573 * @table: pointer to a translation table for entries of a specific type
574 * @table_name: to be used in case of error
575 * @name: entry short name to be translated
577 * get_table_entry_id() will go over translation table trying to find
578 * entry that matches given short name. If matching entry is found,
579 * its id returned to the caller.
582 * entry id if translation succeeds
585 int get_table_entry_id(const table_entry_t *table,
586 const char *table_name, const char *name)
588 const table_entry_t *t;
592 for (t = table; t->id >= 0; ++t) {
593 if (t->sname && strcasecmp(t->sname, name) == 0)
597 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
598 for (t = table; t->id >= 0; ++t) {
599 if (t->sname == NULL)
601 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
604 fprintf(stderr, "\n");
606 for (t = table; t->id >= 0; ++t) {
607 #ifdef CONFIG_NEEDS_MANUAL_RELOC
608 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
610 if (t->sname && strcmp(t->sname, name) == 0)
614 debug("Invalid %s Type: %s\n", table_name, name);
615 #endif /* USE_HOSTCC */
619 int genimg_get_os_id(const char *name)
621 return (get_table_entry_id(uimage_os, "OS", name));
624 int genimg_get_arch_id(const char *name)
626 return (get_table_entry_id(uimage_arch, "CPU", name));
629 int genimg_get_type_id(const char *name)
631 return (get_table_entry_id(uimage_type, "Image", name));
634 int genimg_get_comp_id(const char *name)
636 return (get_table_entry_id(uimage_comp, "Compression", name));
641 * genimg_get_format - get image format type
642 * @img_addr: image start address
644 * genimg_get_format() checks whether provided address points to a valid
645 * legacy or FIT image.
647 * New uImage format and FDT blob are based on a libfdt. FDT blob
648 * may be passed directly or embedded in a FIT image. In both situations
649 * genimg_get_format() must be able to dectect libfdt header.
652 * image format type or IMAGE_FORMAT_INVALID if no image is present
654 int genimg_get_format(void *img_addr)
656 ulong format = IMAGE_FORMAT_INVALID;
657 const image_header_t *hdr;
658 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
662 hdr = (const image_header_t *)img_addr;
663 if (image_check_magic(hdr))
664 format = IMAGE_FORMAT_LEGACY;
665 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
667 fit_hdr = (char *)img_addr;
668 if (fdt_check_header(fit_hdr) == 0)
669 format = IMAGE_FORMAT_FIT;
677 * genimg_get_image - get image from special storage (if necessary)
678 * @img_addr: image start address
680 * genimg_get_image() checks if provided image start adddress is located
681 * in a dataflash storage. If so, image is moved to a system RAM memory.
684 * image start address after possible relocation from special storage
686 ulong genimg_get_image(ulong img_addr)
688 ulong ram_addr = img_addr;
690 #ifdef CONFIG_HAS_DATAFLASH
691 ulong h_size, d_size;
693 if (addr_dataflash(img_addr)) {
694 /* ger RAM address */
695 ram_addr = CONFIG_SYS_LOAD_ADDR;
697 /* get header size */
698 h_size = image_get_header_size();
699 #if defined(CONFIG_FIT)
700 if (sizeof(struct fdt_header) > h_size)
701 h_size = sizeof(struct fdt_header);
705 debug(" Reading image header from dataflash address "
706 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
708 read_dataflash(img_addr, h_size, (char *)ram_addr);
711 switch (genimg_get_format((void *)ram_addr)) {
712 case IMAGE_FORMAT_LEGACY:
713 d_size = image_get_data_size(
714 (const image_header_t *)ram_addr);
715 debug(" Legacy format image found at 0x%08lx, "
719 #if defined(CONFIG_FIT)
720 case IMAGE_FORMAT_FIT:
721 d_size = fit_get_size((const void *)ram_addr) - h_size;
722 debug(" FIT/FDT format image found at 0x%08lx, "
728 printf(" No valid image found at 0x%08lx\n",
733 /* read in image data */
734 debug(" Reading image remaining data from dataflash address "
735 "%08lx to RAM address %08lx\n", img_addr + h_size,
738 read_dataflash(img_addr + h_size, d_size,
739 (char *)(ram_addr + h_size));
742 #endif /* CONFIG_HAS_DATAFLASH */
748 * fit_has_config - check if there is a valid FIT configuration
749 * @images: pointer to the bootm command headers structure
751 * fit_has_config() checks if there is a FIT configuration in use
752 * (if FTI support is present).
755 * 0, no FIT support or no configuration found
756 * 1, configuration found
758 int genimg_has_config(bootm_headers_t *images)
760 #if defined(CONFIG_FIT)
761 if (images->fit_uname_cfg)
768 * boot_get_ramdisk - main ramdisk handling routine
769 * @argc: command argument count
770 * @argv: command argument list
771 * @images: pointer to the bootm images structure
772 * @arch: expected ramdisk architecture
773 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
774 * @rd_end: pointer to a ulong variable, will hold ramdisk end
776 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
777 * Curently supported are the following ramdisk sources:
778 * - multicomponent kernel/ramdisk image,
779 * - commandline provided address of decicated ramdisk image.
782 * 0, if ramdisk image was found and valid, or skiped
783 * rd_start and rd_end are set to ramdisk start/end addresses if
784 * ramdisk image is found and valid
786 * 1, if ramdisk image is found but corrupted, or invalid
787 * rd_start and rd_end are set to 0 if no ramdisk exists
789 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
790 uint8_t arch, ulong *rd_start, ulong *rd_end)
792 ulong rd_addr, rd_load;
793 ulong rd_data, rd_len;
794 const image_header_t *rd_hdr;
795 #ifdef CONFIG_SUPPORT_RAW_INITRD
798 #if defined(CONFIG_FIT)
800 const char *fit_uname_config = NULL;
801 const char *fit_uname_ramdisk = NULL;
813 * Look for a '-' which indicates to ignore the
816 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
817 debug("## Skipping init Ramdisk\n");
818 rd_len = rd_data = 0;
819 } else if (argc >= 3 || genimg_has_config(images)) {
820 #if defined(CONFIG_FIT)
823 * If the init ramdisk comes from the FIT image and
824 * the FIT image address is omitted in the command
825 * line argument, try to use os FIT image address or
826 * default load address.
828 if (images->fit_uname_os)
829 default_addr = (ulong)images->fit_hdr_os;
831 default_addr = load_addr;
833 if (fit_parse_conf(argv[2], default_addr,
834 &rd_addr, &fit_uname_config)) {
835 debug("* ramdisk: config '%s' from image at "
837 fit_uname_config, rd_addr);
838 } else if (fit_parse_subimage(argv[2], default_addr,
839 &rd_addr, &fit_uname_ramdisk)) {
840 debug("* ramdisk: subimage '%s' from image at "
842 fit_uname_ramdisk, rd_addr);
846 rd_addr = simple_strtoul(argv[2], NULL, 16);
847 debug("* ramdisk: cmdline image address = "
851 #if defined(CONFIG_FIT)
853 /* use FIT configuration provided in first bootm
856 rd_addr = (ulong)images->fit_hdr_os;
857 fit_uname_config = images->fit_uname_cfg;
858 debug("* ramdisk: using config '%s' from image "
860 fit_uname_config, rd_addr);
863 * Check whether configuration has ramdisk defined,
864 * if not, don't try to use it, quit silently.
866 fit_hdr = (void *)rd_addr;
867 cfg_noffset = fit_conf_get_node(fit_hdr,
869 if (cfg_noffset < 0) {
870 debug("* ramdisk: no such config\n");
874 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
876 if (rd_noffset < 0) {
877 debug("* ramdisk: no ramdisk in config\n");
883 /* copy from dataflash if needed */
884 rd_addr = genimg_get_image(rd_addr);
887 * Check if there is an initrd image at the
888 * address provided in the second bootm argument
889 * check image type, for FIT images get FIT node.
891 switch (genimg_get_format((void *)rd_addr)) {
892 case IMAGE_FORMAT_LEGACY:
893 printf("## Loading init Ramdisk from Legacy "
894 "Image at %08lx ...\n", rd_addr);
896 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
897 rd_hdr = image_get_ramdisk(rd_addr, arch,
903 rd_data = image_get_data(rd_hdr);
904 rd_len = image_get_data_size(rd_hdr);
905 rd_load = image_get_load(rd_hdr);
907 #if defined(CONFIG_FIT)
908 case IMAGE_FORMAT_FIT:
909 fit_hdr = (void *)rd_addr;
910 printf("## Loading init Ramdisk from FIT "
911 "Image at %08lx ...\n", rd_addr);
913 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
914 if (!fit_check_format(fit_hdr)) {
915 puts("Bad FIT ramdisk image format!\n");
917 BOOTSTAGE_ID_FIT_RD_FORMAT);
920 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
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
929 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
930 cfg_noffset = fit_conf_get_node(fit_hdr,
932 if (cfg_noffset < 0) {
933 puts("Could not find configuration "
936 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
939 fit_uname_config = fdt_get_name(fit_hdr,
941 printf(" Using '%s' configuration\n",
944 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
946 fit_uname_ramdisk = fit_get_name(fit_hdr,
949 /* get ramdisk component image node offset */
951 BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
952 rd_noffset = fit_image_get_node(fit_hdr,
955 if (rd_noffset < 0) {
956 puts("Could not find subimage node\n");
957 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
961 printf(" Trying '%s' ramdisk subimage\n",
964 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
965 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
969 /* get ramdisk image data address and length */
970 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
972 puts("Could not find ramdisk subimage data!\n");
973 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
976 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
978 rd_data = (ulong)data;
981 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
982 puts("Can't get ramdisk subimage load "
984 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
987 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
989 images->fit_hdr_rd = fit_hdr;
990 images->fit_uname_rd = fit_uname_ramdisk;
991 images->fit_noffset_rd = rd_noffset;
995 #ifdef CONFIG_SUPPORT_RAW_INITRD
996 if (argc >= 3 && (end = strchr(argv[2], ':'))) {
997 rd_len = simple_strtoul(++end, NULL, 16);
1002 puts("Wrong Ramdisk Image Format\n");
1003 rd_data = rd_len = rd_load = 0;
1007 } else if (images->legacy_hdr_valid &&
1008 image_check_type(&images->legacy_hdr_os_copy,
1012 * Now check if we have a legacy mult-component image,
1013 * get second entry data start address and len.
1015 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1016 printf("## Loading init Ramdisk from multi component "
1017 "Legacy Image at %08lx ...\n",
1018 (ulong)images->legacy_hdr_os);
1020 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1025 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1026 rd_len = rd_data = 0;
1030 debug("## No init Ramdisk\n");
1032 *rd_start = rd_data;
1033 *rd_end = rd_data + rd_len;
1035 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1036 *rd_start, *rd_end);
1041 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1043 * boot_ramdisk_high - relocate init ramdisk
1044 * @lmb: pointer to lmb handle, will be used for memory mgmt
1045 * @rd_data: ramdisk data start address
1046 * @rd_len: ramdisk data length
1047 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1048 * start address (after possible relocation)
1049 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1050 * end address (after possible relocation)
1052 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1053 * variable and if requested ramdisk data is moved to a specified location.
1055 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1056 * start/end addresses if ramdisk image start and len were provided,
1057 * otherwise set initrd_start and initrd_end set to zeros.
1063 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1064 ulong *initrd_start, ulong *initrd_end)
1068 int initrd_copy_to_ram = 1;
1070 if ((s = getenv("initrd_high")) != NULL) {
1071 /* a value of "no" or a similar string will act like 0,
1072 * turning the "load high" feature off. This is intentional.
1074 initrd_high = simple_strtoul(s, NULL, 16);
1075 if (initrd_high == ~0)
1076 initrd_copy_to_ram = 0;
1078 /* not set, no restrictions to load high */
1083 #ifdef CONFIG_LOGBUFFER
1084 /* Prevent initrd from overwriting logbuffer */
1085 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1088 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1089 initrd_high, initrd_copy_to_ram);
1092 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1093 debug(" in-place initrd\n");
1094 *initrd_start = rd_data;
1095 *initrd_end = rd_data + rd_len;
1096 lmb_reserve(lmb, rd_data, rd_len);
1099 *initrd_start = (ulong)lmb_alloc_base(lmb,
1100 rd_len, 0x1000, initrd_high);
1102 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1105 if (*initrd_start == 0) {
1106 puts("ramdisk - allocation error\n");
1109 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1111 *initrd_end = *initrd_start + rd_len;
1112 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
1113 *initrd_start, *initrd_end);
1115 memmove_wd((void *)*initrd_start,
1116 (void *)rd_data, rd_len, CHUNKSZ);
1120 * Ensure the image is flushed to memory to handle
1121 * AMP boot scenarios in which we might not be
1124 flush_cache((unsigned long)*initrd_start, rd_len);
1132 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1133 *initrd_start, *initrd_end);
1140 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1142 #ifdef CONFIG_OF_LIBFDT
1143 static void fdt_error(const char *msg)
1147 puts(" - must RESET the board to recover.\n");
1150 static const image_header_t *image_get_fdt(ulong fdt_addr)
1152 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1154 image_print_contents(fdt_hdr);
1156 puts(" Verifying Checksum ... ");
1157 if (!image_check_hcrc(fdt_hdr)) {
1158 fdt_error("fdt header checksum invalid");
1162 if (!image_check_dcrc(fdt_hdr)) {
1163 fdt_error("fdt checksum invalid");
1168 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1169 fdt_error("uImage is not a fdt");
1172 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1173 fdt_error("uImage is compressed");
1176 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1177 fdt_error("uImage data is not a fdt");
1184 * fit_check_fdt - verify FIT format FDT subimage
1185 * @fit_hdr: pointer to the FIT header
1186 * fdt_noffset: FDT subimage node offset within FIT image
1187 * @verify: data CRC verification flag
1189 * fit_check_fdt() verifies integrity of the FDT subimage and from
1190 * specified FIT image.
1196 #if defined(CONFIG_FIT)
1197 static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
1199 fit_image_print(fit, fdt_noffset, " ");
1202 puts(" Verifying Hash Integrity ... ");
1203 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1204 fdt_error("Bad Data Hash");
1210 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1211 fdt_error("Not a FDT image");
1215 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1216 fdt_error("FDT image is compressed");
1222 #endif /* CONFIG_FIT */
1224 #ifndef CONFIG_SYS_FDT_PAD
1225 #define CONFIG_SYS_FDT_PAD 0x3000
1228 #if defined(CONFIG_OF_LIBFDT)
1230 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1231 * @lmb: pointer to lmb handle, will be used for memory mgmt
1232 * @fdt_blob: pointer to fdt blob base address
1234 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1235 * memreserve regions prevents u-boot from using them to store the initrd
1238 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1240 uint64_t addr, size;
1243 if (fdt_check_header(fdt_blob) != 0)
1246 total = fdt_num_mem_rsv(fdt_blob);
1247 for (i = 0; i < total; i++) {
1248 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1250 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1251 (unsigned long long)addr, (unsigned long long)size);
1252 lmb_reserve(lmb, addr, size);
1257 * boot_relocate_fdt - relocate flat device tree
1258 * @lmb: pointer to lmb handle, will be used for memory mgmt
1259 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1260 * @of_size: pointer to a ulong variable, will hold fdt length
1262 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1263 * relocates the of_flat_tree into that region, even if the fdt is already in
1264 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1267 * of_flat_tree and of_size are set to final (after relocation) values
1273 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1275 void *fdt_blob = *of_flat_tree;
1276 void *of_start = NULL;
1280 int disable_relocation = 0;
1286 if (fdt_check_header(fdt_blob) != 0) {
1287 fdt_error("image is not a fdt");
1291 /* position on a 4K boundary before the alloc_current */
1292 /* Pad the FDT by a specified amount */
1293 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1295 /* If fdt_high is set use it to select the relocation address */
1296 fdt_high = getenv("fdt_high");
1298 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1300 if (((ulong) desired_addr) == ~0UL) {
1301 /* All ones means use fdt in place */
1302 of_start = fdt_blob;
1303 lmb_reserve(lmb, (ulong)of_start, of_len);
1304 disable_relocation = 1;
1305 } else if (desired_addr) {
1307 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1308 (ulong)desired_addr);
1309 if (of_start == NULL) {
1310 puts("Failed using fdt_high value for Device Tree");
1315 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
1319 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1320 getenv_bootm_mapsize()
1321 + getenv_bootm_low());
1324 if (of_start == NULL) {
1325 puts("device tree - allocation error\n");
1329 if (disable_relocation) {
1330 /* We assume there is space after the existing fdt to use for padding */
1331 fdt_set_totalsize(of_start, of_len);
1332 printf(" Using Device Tree in place at %p, end %p\n",
1333 of_start, of_start + of_len - 1);
1335 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1336 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1338 printf(" Loading Device Tree to %p, end %p ... ",
1339 of_start, of_start + of_len - 1);
1341 err = fdt_open_into(fdt_blob, of_start, of_len);
1343 fdt_error("fdt move failed");
1349 *of_flat_tree = of_start;
1352 set_working_fdt_addr(*of_flat_tree);
1358 #endif /* CONFIG_OF_LIBFDT */
1361 * boot_get_fdt - main fdt handling routine
1362 * @argc: command argument count
1363 * @argv: command argument list
1364 * @images: pointer to the bootm images structure
1365 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1366 * @of_size: pointer to a ulong variable, will hold fdt length
1368 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1369 * Curently supported are the following ramdisk sources:
1370 * - multicomponent kernel/ramdisk image,
1371 * - commandline provided address of decicated ramdisk image.
1374 * 0, if fdt image was found and valid, or skipped
1375 * of_flat_tree and of_size are set to fdt start address and length if
1376 * fdt image is found and valid
1378 * 1, if fdt image is found but corrupted
1379 * of_flat_tree and of_size are set to 0 if no fdt exists
1381 int boot_get_fdt(int flag, int argc, char * const argv[],
1382 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
1384 const image_header_t *fdt_hdr;
1386 char *fdt_blob = NULL;
1387 ulong image_start, image_data, image_end;
1388 ulong load_start, load_end;
1389 #if defined(CONFIG_FIT)
1391 const char *fit_uname_config = NULL;
1392 const char *fit_uname_fdt = NULL;
1400 *of_flat_tree = NULL;
1403 if (argc > 3 || genimg_has_config(images)) {
1404 #if defined(CONFIG_FIT)
1407 * If the FDT blob comes from the FIT image and the
1408 * FIT image address is omitted in the command line
1409 * argument, try to use ramdisk or os FIT image
1410 * address or default load address.
1412 if (images->fit_uname_rd)
1413 default_addr = (ulong)images->fit_hdr_rd;
1414 else if (images->fit_uname_os)
1415 default_addr = (ulong)images->fit_hdr_os;
1417 default_addr = load_addr;
1419 if (fit_parse_conf(argv[3], default_addr,
1420 &fdt_addr, &fit_uname_config)) {
1421 debug("* fdt: config '%s' from image at "
1423 fit_uname_config, fdt_addr);
1424 } else if (fit_parse_subimage(argv[3], default_addr,
1425 &fdt_addr, &fit_uname_fdt)) {
1426 debug("* fdt: subimage '%s' from image at "
1428 fit_uname_fdt, fdt_addr);
1432 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1433 debug("* fdt: cmdline image address = "
1437 #if defined(CONFIG_FIT)
1439 /* use FIT configuration provided in first bootm
1442 fdt_addr = (ulong)images->fit_hdr_os;
1443 fit_uname_config = images->fit_uname_cfg;
1444 debug("* fdt: using config '%s' from image "
1446 fit_uname_config, fdt_addr);
1449 * Check whether configuration has FDT blob defined,
1450 * if not quit silently.
1452 fit_hdr = (void *)fdt_addr;
1453 cfg_noffset = fit_conf_get_node(fit_hdr,
1455 if (cfg_noffset < 0) {
1456 debug("* fdt: no such config\n");
1460 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
1462 if (fdt_noffset < 0) {
1463 debug("* fdt: no fdt in config\n");
1469 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1472 /* copy from dataflash if needed */
1473 fdt_addr = genimg_get_image(fdt_addr);
1476 * Check if there is an FDT image at the
1477 * address provided in the second bootm argument
1478 * check image type, for FIT images get a FIT node.
1480 switch (genimg_get_format((void *)fdt_addr)) {
1481 case IMAGE_FORMAT_LEGACY:
1482 /* verify fdt_addr points to a valid image header */
1483 printf("## Flattened Device Tree from Legacy Image "
1486 fdt_hdr = image_get_fdt(fdt_addr);
1491 * move image data to the load address,
1492 * make sure we don't overwrite initial image
1494 image_start = (ulong)fdt_hdr;
1495 image_data = (ulong)image_get_data(fdt_hdr);
1496 image_end = image_get_image_end(fdt_hdr);
1498 load_start = image_get_load(fdt_hdr);
1499 load_end = load_start + image_get_data_size(fdt_hdr);
1501 if (load_start == image_start ||
1502 load_start == image_data) {
1503 fdt_blob = (char *)image_data;
1507 if ((load_start < image_end) && (load_end > image_start)) {
1508 fdt_error("fdt overwritten");
1512 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
1513 image_data, load_start);
1515 memmove((void *)load_start,
1517 image_get_data_size(fdt_hdr));
1519 fdt_blob = (char *)load_start;
1521 case IMAGE_FORMAT_FIT:
1523 * This case will catch both: new uImage format
1524 * (libfdt based) and raw FDT blob (also libfdt
1527 #if defined(CONFIG_FIT)
1528 /* check FDT blob vs FIT blob */
1529 if (fit_check_format((const void *)fdt_addr)) {
1533 fit_hdr = (void *)fdt_addr;
1534 printf("## Flattened Device Tree from FIT "
1538 if (!fit_uname_fdt) {
1540 * no FDT blob image node unit name,
1541 * try to get config node first. If
1542 * config unit node name is NULL
1543 * fit_conf_get_node() will try to
1544 * find default config node
1546 cfg_noffset = fit_conf_get_node(fit_hdr,
1549 if (cfg_noffset < 0) {
1550 fdt_error("Could not find "
1556 fit_uname_config = fdt_get_name(fit_hdr,
1558 printf(" Using '%s' configuration\n",
1561 fdt_noffset = fit_conf_get_fdt_node(
1564 fit_uname_fdt = fit_get_name(fit_hdr,
1567 /* get FDT component image node offset */
1568 fdt_noffset = fit_image_get_node(
1572 if (fdt_noffset < 0) {
1573 fdt_error("Could not find subimage "
1578 printf(" Trying '%s' FDT blob subimage\n",
1581 if (!fit_check_fdt(fit_hdr, fdt_noffset,
1585 /* get ramdisk image data address and length */
1586 if (fit_image_get_data(fit_hdr, fdt_noffset,
1588 fdt_error("Could not find FDT "
1593 /* verift that image data is a proper FDT blob */
1594 if (fdt_check_header((char *)data) != 0) {
1595 fdt_error("Subimage data is not a FTD");
1600 * move image data to the load address,
1601 * make sure we don't overwrite initial image
1603 image_start = (ulong)fit_hdr;
1604 image_end = fit_get_end(fit_hdr);
1606 if (fit_image_get_load(fit_hdr, fdt_noffset,
1607 &load_start) == 0) {
1608 load_end = load_start + size;
1610 if ((load_start < image_end) &&
1611 (load_end > image_start)) {
1612 fdt_error("FDT overwritten");
1616 printf(" Loading FDT from 0x%08lx "
1621 memmove((void *)load_start,
1622 (void *)data, size);
1624 fdt_blob = (char *)load_start;
1626 fdt_blob = (char *)data;
1629 images->fit_hdr_fdt = fit_hdr;
1630 images->fit_uname_fdt = fit_uname_fdt;
1631 images->fit_noffset_fdt = fdt_noffset;
1639 fdt_blob = (char *)fdt_addr;
1640 debug("* fdt: raw FDT blob\n");
1641 printf("## Flattened Device Tree blob at "
1642 "%08lx\n", (long)fdt_blob);
1646 puts("ERROR: Did not find a cmdline Flattened Device "
1651 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
1653 } else if (images->legacy_hdr_valid &&
1654 image_check_type(&images->legacy_hdr_os_copy,
1657 ulong fdt_data, fdt_len;
1660 * Now check if we have a legacy multi-component image,
1661 * get second entry data start address and len.
1663 printf("## Flattened Device Tree from multi "
1664 "component Image at %08lX\n",
1665 (ulong)images->legacy_hdr_os);
1667 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1671 fdt_blob = (char *)fdt_data;
1672 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
1674 if (fdt_check_header(fdt_blob) != 0) {
1675 fdt_error("image is not a fdt");
1679 if (fdt_totalsize(fdt_blob) != fdt_len) {
1680 fdt_error("fdt size != image size");
1684 debug("## No Flattened Device Tree\n");
1688 debug("## No Flattened Device Tree\n");
1692 *of_flat_tree = fdt_blob;
1693 *of_size = fdt_totalsize(fdt_blob);
1694 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
1695 (ulong)*of_flat_tree, *of_size);
1700 *of_flat_tree = NULL;
1704 #endif /* CONFIG_OF_LIBFDT */
1706 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1708 * boot_get_cmdline - allocate and initialize kernel cmdline
1709 * @lmb: pointer to lmb handle, will be used for memory mgmt
1710 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1711 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1713 * boot_get_cmdline() allocates space for kernel command line below
1714 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1715 * variable is present its contents is copied to allocated kernel
1722 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1727 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1728 getenv_bootm_mapsize() + getenv_bootm_low());
1730 if (cmdline == NULL)
1733 if ((s = getenv("bootargs")) == NULL)
1738 *cmd_start = (ulong) & cmdline[0];
1739 *cmd_end = *cmd_start + strlen(cmdline);
1741 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1745 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1747 #ifdef CONFIG_SYS_BOOT_GET_KBD
1749 * boot_get_kbd - allocate and initialize kernel copy of board info
1750 * @lmb: pointer to lmb handle, will be used for memory mgmt
1751 * @kbd: double pointer to board info data
1753 * boot_get_kbd() allocates space for kernel copy of board info data below
1754 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1755 * with the current u-boot board info data.
1761 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1763 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1764 getenv_bootm_mapsize() + getenv_bootm_low());
1770 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1772 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1773 do_bdinfo(NULL, 0, 0, NULL);
1778 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1779 #endif /* !USE_HOSTCC */
1781 #if defined(CONFIG_FIT)
1782 /*****************************************************************************/
1783 /* New uImage format routines */
1784 /*****************************************************************************/
1786 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
1787 ulong *addr, const char **name)
1794 sep = strchr(spec, sepc);
1797 *addr = simple_strtoul(spec, NULL, 16);
1807 * fit_parse_conf - parse FIT configuration spec
1808 * @spec: input string, containing configuration spec
1809 * @add_curr: current image address (to be used as a possible default)
1810 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1812 * @conf_name double pointer to a char, will hold pointer to a configuration
1815 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1816 * where <addr> is a FIT image address that contains configuration
1817 * with a <conf> unit name.
1819 * Address part is optional, and if omitted default add_curr will
1823 * 1 if spec is a valid configuration string,
1824 * addr and conf_name are set accordingly
1827 int fit_parse_conf(const char *spec, ulong addr_curr,
1828 ulong *addr, const char **conf_name)
1830 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
1834 * fit_parse_subimage - parse FIT subimage spec
1835 * @spec: input string, containing subimage spec
1836 * @add_curr: current image address (to be used as a possible default)
1837 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1839 * @image_name: double pointer to a char, will hold pointer to a subimage name
1841 * fit_parse_subimage() expects subimage spec in the for of
1842 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1843 * subimage with a <subimg> unit name.
1845 * Address part is optional, and if omitted default add_curr will
1849 * 1 if spec is a valid subimage string,
1850 * addr and image_name are set accordingly
1853 int fit_parse_subimage(const char *spec, ulong addr_curr,
1854 ulong *addr, const char **image_name)
1856 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
1858 #endif /* !USE_HOSTCC */
1860 static void fit_get_debug(const void *fit, int noffset,
1861 char *prop_name, int err)
1863 debug("Can't get '%s' property from FIT 0x%08lx, "
1864 "node: offset %d, name %s (%s)\n",
1865 prop_name, (ulong)fit, noffset,
1866 fit_get_name(fit, noffset, NULL),
1871 * fit_print_contents - prints out the contents of the FIT format image
1872 * @fit: pointer to the FIT format image header
1873 * @p: pointer to prefix string
1875 * fit_print_contents() formats a multi line FIT image contents description.
1876 * The routine prints out FIT image properties (root node level) follwed by
1877 * the details of each component image.
1880 * no returned results
1882 void fit_print_contents(const void *fit)
1893 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1903 /* Root node properties */
1904 ret = fit_get_desc(fit, 0, &desc);
1905 printf("%sFIT description: ", p);
1907 printf("unavailable\n");
1909 printf("%s\n", desc);
1911 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1912 ret = fit_get_timestamp(fit, 0, ×tamp);
1913 printf("%sCreated: ", p);
1915 printf("unavailable\n");
1917 genimg_print_time(timestamp);
1920 /* Find images parent node offset */
1921 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1922 if (images_noffset < 0) {
1923 printf("Can't find images parent node '%s' (%s)\n",
1924 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1928 /* Process its subnodes, print out component images details */
1929 for (ndepth = 0, count = 0,
1930 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1931 (noffset >= 0) && (ndepth > 0);
1932 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1935 * Direct child node of the images parent node,
1936 * i.e. component image node.
1938 printf("%s Image %u (%s)\n", p, count++,
1939 fit_get_name(fit, noffset, NULL));
1941 fit_image_print(fit, noffset, p);
1945 /* Find configurations parent node offset */
1946 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1947 if (confs_noffset < 0) {
1948 debug("Can't get configurations parent node '%s' (%s)\n",
1949 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1953 /* get default configuration unit name from default property */
1954 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
1956 printf("%s Default Configuration: '%s'\n", p, uname);
1958 /* Process its subnodes, print out configurations details */
1959 for (ndepth = 0, count = 0,
1960 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1961 (noffset >= 0) && (ndepth > 0);
1962 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1965 * Direct child node of the configurations parent node,
1966 * i.e. configuration node.
1968 printf("%s Configuration %u (%s)\n", p, count++,
1969 fit_get_name(fit, noffset, NULL));
1971 fit_conf_print(fit, noffset, p);
1977 * fit_image_print - prints out the FIT component image details
1978 * @fit: pointer to the FIT format image header
1979 * @image_noffset: offset of the component image node
1980 * @p: pointer to prefix string
1982 * fit_image_print() lists all mandatory properies for the processed component
1983 * image. If present, hash nodes are printed out as well. Load
1984 * address for images of type firmware is also printed out. Since the load
1985 * address is not mandatory for firmware images, it will be output as
1986 * "unavailable" when not present.
1989 * no returned results
1991 void fit_image_print(const void *fit, int image_noffset, const char *p)
1994 uint8_t type, arch, os, comp;
2002 /* Mandatory properties */
2003 ret = fit_get_desc(fit, image_noffset, &desc);
2004 printf("%s Description: ", p);
2006 printf("unavailable\n");
2008 printf("%s\n", desc);
2010 fit_image_get_type(fit, image_noffset, &type);
2011 printf("%s Type: %s\n", p, genimg_get_type_name(type));
2013 fit_image_get_comp(fit, image_noffset, &comp);
2014 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
2016 ret = fit_image_get_data(fit, image_noffset, &data, &size);
2019 printf("%s Data Start: ", p);
2021 printf("unavailable\n");
2023 printf("0x%08lx\n", (ulong)data);
2026 printf("%s Data Size: ", p);
2028 printf("unavailable\n");
2030 genimg_print_size(size);
2032 /* Remaining, type dependent properties */
2033 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2034 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2035 (type == IH_TYPE_FLATDT)) {
2036 fit_image_get_arch(fit, image_noffset, &arch);
2037 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
2040 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
2041 fit_image_get_os(fit, image_noffset, &os);
2042 printf("%s OS: %s\n", p, genimg_get_os_name(os));
2045 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2046 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
2047 ret = fit_image_get_load(fit, image_noffset, &load);
2048 printf("%s Load Address: ", p);
2050 printf("unavailable\n");
2052 printf("0x%08lx\n", load);
2055 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2056 (type == IH_TYPE_RAMDISK)) {
2057 fit_image_get_entry(fit, image_noffset, &entry);
2058 printf("%s Entry Point: ", p);
2060 printf("unavailable\n");
2062 printf("0x%08lx\n", entry);
2065 /* Process all hash subnodes of the component image node */
2066 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2067 (noffset >= 0) && (ndepth > 0);
2068 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2070 /* Direct child node of the component image node */
2071 fit_image_print_hash(fit, noffset, p);
2077 * fit_image_print_hash - prints out the hash node details
2078 * @fit: pointer to the FIT format image header
2079 * @noffset: offset of the hash node
2080 * @p: pointer to prefix string
2082 * fit_image_print_hash() lists properies for the processed hash node
2085 * no returned results
2087 void fit_image_print_hash(const void *fit, int noffset, const char *p)
2095 * Check subnode name, must be equal to "hash".
2096 * Multiple hash nodes require unique unit node
2097 * names, e.g. hash@1, hash@2, etc.
2099 if (strncmp(fit_get_name(fit, noffset, NULL),
2101 strlen(FIT_HASH_NODENAME)) != 0)
2104 debug("%s Hash node: '%s'\n", p,
2105 fit_get_name(fit, noffset, NULL));
2107 printf("%s Hash algo: ", p);
2108 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2109 printf("invalid/unsupported\n");
2112 printf("%s\n", algo);
2114 ret = fit_image_hash_get_value(fit, noffset, &value,
2116 printf("%s Hash value: ", p);
2118 printf("unavailable\n");
2120 for (i = 0; i < value_len; i++)
2121 printf("%02x", value[i]);
2125 debug("%s Hash len: %d\n", p, value_len);
2129 * fit_get_desc - get node description property
2130 * @fit: pointer to the FIT format image header
2131 * @noffset: node offset
2132 * @desc: double pointer to the char, will hold pointer to the descrption
2134 * fit_get_desc() reads description property from a given node, if
2135 * description is found pointer to it is returened in third call argument.
2141 int fit_get_desc(const void *fit, int noffset, char **desc)
2145 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
2146 if (*desc == NULL) {
2147 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
2155 * fit_get_timestamp - get node timestamp property
2156 * @fit: pointer to the FIT format image header
2157 * @noffset: node offset
2158 * @timestamp: pointer to the time_t, will hold read timestamp
2160 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2161 * is found and has a correct size its value is retured in third call
2166 * -1, on property read failure
2167 * -2, on wrong timestamp size
2169 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
2174 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
2176 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
2179 if (len != sizeof(uint32_t)) {
2180 debug("FIT timestamp with incorrect size of (%u)\n", len);
2184 *timestamp = uimage_to_cpu(*((uint32_t *)data));
2189 * fit_image_get_node - get node offset for component image of a given unit name
2190 * @fit: pointer to the FIT format image header
2191 * @image_uname: component image node unit name
2193 * fit_image_get_node() finds a component image (withing the '/images'
2194 * node) of a provided unit name. If image is found its node offset is
2195 * returned to the caller.
2198 * image node offset when found (>=0)
2199 * negative number on failure (FDT_ERR_* code)
2201 int fit_image_get_node(const void *fit, const char *image_uname)
2203 int noffset, images_noffset;
2205 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2206 if (images_noffset < 0) {
2207 debug("Can't find images parent node '%s' (%s)\n",
2208 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2209 return images_noffset;
2212 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
2214 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2215 image_uname, fdt_strerror(noffset));
2222 * fit_image_get_os - get os id for a given component image node
2223 * @fit: pointer to the FIT format image header
2224 * @noffset: component image node offset
2225 * @os: pointer to the uint8_t, will hold os numeric id
2227 * fit_image_get_os() finds os property in a given component image node.
2228 * If the property is found, its (string) value is translated to the numeric
2229 * id which is returned to the caller.
2235 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
2240 /* Get OS name from property data */
2241 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
2243 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
2248 /* Translate OS name to id */
2249 *os = genimg_get_os_id(data);
2254 * fit_image_get_arch - get arch id for a given component image node
2255 * @fit: pointer to the FIT format image header
2256 * @noffset: component image node offset
2257 * @arch: pointer to the uint8_t, will hold arch numeric id
2259 * fit_image_get_arch() finds arch property in a given component image node.
2260 * If the property is found, its (string) value is translated to the numeric
2261 * id which is returned to the caller.
2267 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
2272 /* Get architecture name from property data */
2273 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
2275 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
2280 /* Translate architecture name to id */
2281 *arch = genimg_get_arch_id(data);
2286 * fit_image_get_type - get type id for a given component image node
2287 * @fit: pointer to the FIT format image header
2288 * @noffset: component image node offset
2289 * @type: pointer to the uint8_t, will hold type numeric id
2291 * fit_image_get_type() finds type property in a given component image node.
2292 * If the property is found, its (string) value is translated to the numeric
2293 * id which is returned to the caller.
2299 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
2304 /* Get image type name from property data */
2305 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
2307 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
2312 /* Translate image type name to id */
2313 *type = genimg_get_type_id(data);
2318 * fit_image_get_comp - get comp id for a given component image node
2319 * @fit: pointer to the FIT format image header
2320 * @noffset: component image node offset
2321 * @comp: pointer to the uint8_t, will hold comp numeric id
2323 * fit_image_get_comp() finds comp property in a given component image node.
2324 * If the property is found, its (string) value is translated to the numeric
2325 * id which is returned to the caller.
2331 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
2336 /* Get compression name from property data */
2337 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
2339 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
2344 /* Translate compression name to id */
2345 *comp = genimg_get_comp_id(data);
2350 * fit_image_get_load - get load address property for a given component image node
2351 * @fit: pointer to the FIT format image header
2352 * @noffset: component image node offset
2353 * @load: pointer to the uint32_t, will hold load address
2355 * fit_image_get_load() finds load address property in a given component image node.
2356 * If the property is found, its value is returned to the caller.
2362 int fit_image_get_load(const void *fit, int noffset, ulong *load)
2365 const uint32_t *data;
2367 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
2369 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
2373 *load = uimage_to_cpu(*data);
2378 * fit_image_get_entry - get entry point address property for a given component image node
2379 * @fit: pointer to the FIT format image header
2380 * @noffset: component image node offset
2381 * @entry: pointer to the uint32_t, will hold entry point address
2383 * fit_image_get_entry() finds entry point address property in a given component image node.
2384 * If the property is found, its value is returned to the caller.
2390 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
2393 const uint32_t *data;
2395 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
2397 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
2401 *entry = uimage_to_cpu(*data);
2406 * fit_image_get_data - get data property and its size for a given component image node
2407 * @fit: pointer to the FIT format image header
2408 * @noffset: component image node offset
2409 * @data: double pointer to void, will hold data property's data address
2410 * @size: pointer to size_t, will hold data property's data size
2412 * fit_image_get_data() finds data property in a given component image node.
2413 * If the property is found its data start address and size are returned to
2420 int fit_image_get_data(const void *fit, int noffset,
2421 const void **data, size_t *size)
2425 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
2426 if (*data == NULL) {
2427 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
2437 * fit_image_hash_get_algo - get hash algorithm name
2438 * @fit: pointer to the FIT format image header
2439 * @noffset: hash node offset
2440 * @algo: double pointer to char, will hold pointer to the algorithm name
2442 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2443 * If the property is found its data start address is returned to the caller.
2449 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
2453 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
2454 if (*algo == NULL) {
2455 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
2463 * fit_image_hash_get_value - get hash value and length
2464 * @fit: pointer to the FIT format image header
2465 * @noffset: hash node offset
2466 * @value: double pointer to uint8_t, will hold address of a hash value data
2467 * @value_len: pointer to an int, will hold hash data length
2469 * fit_image_hash_get_value() finds hash value property in a given hash node.
2470 * If the property is found its data start address and size are returned to
2477 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
2482 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
2483 if (*value == NULL) {
2484 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
2495 * fit_image_hash_get_ignore - get hash ignore flag
2496 * @fit: pointer to the FIT format image header
2497 * @noffset: hash node offset
2498 * @ignore: pointer to an int, will hold hash ignore flag
2500 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
2501 * If the property is found and non-zero, the hash algorithm is not verified by
2502 * u-boot automatically.
2505 * 0, on ignore not found
2506 * value, on ignore found
2508 int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
2513 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
2514 if (value == NULL || len != sizeof(int))
2524 * fit_set_timestamp - set node timestamp property
2525 * @fit: pointer to the FIT format image header
2526 * @noffset: node offset
2527 * @timestamp: timestamp value to be set
2529 * fit_set_timestamp() attempts to set timestamp property in the requested
2530 * node and returns operation status to the caller.
2534 * -1, on property read failure
2536 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
2541 t = cpu_to_uimage(timestamp);
2542 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2545 printf("Can't set '%s' property for '%s' node (%s)\n",
2546 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2555 * calculate_hash - calculate and return hash for provided input data
2556 * @data: pointer to the input data
2557 * @data_len: data length
2558 * @algo: requested hash algorithm
2559 * @value: pointer to the char, will hold hash value data (caller must
2560 * allocate enough free space)
2561 * value_len: length of the calculated hash
2563 * calculate_hash() computes input data hash according to the requested algorithm.
2564 * Resulting hash value is placed in caller provided 'value' buffer, length
2565 * of the calculated hash is returned via value_len pointer argument.
2569 * -1, when algo is unsupported
2571 static int calculate_hash(const void *data, int data_len, const char *algo,
2572 uint8_t *value, int *value_len)
2574 if (strcmp(algo, "crc32") == 0) {
2575 *((uint32_t *)value) = crc32_wd(0, data, data_len,
2577 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
2579 } else if (strcmp(algo, "sha1") == 0) {
2580 sha1_csum_wd((unsigned char *) data, data_len,
2581 (unsigned char *) value, CHUNKSZ_SHA1);
2583 } else if (strcmp(algo, "md5") == 0) {
2584 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2587 debug("Unsupported hash alogrithm\n");
2595 * fit_set_hashes - process FIT component image nodes and calculate hashes
2596 * @fit: pointer to the FIT format image header
2598 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2599 * Hashes are calculated for all component images which have hash subnodes
2600 * with algorithm property set to one of the supported hash algorithms.
2604 * libfdt error code, on failure
2606 int fit_set_hashes(void *fit)
2613 /* Find images parent node offset */
2614 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2615 if (images_noffset < 0) {
2616 printf("Can't find images parent node '%s' (%s)\n",
2617 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2618 return images_noffset;
2621 /* Process its subnodes, print out component images details */
2622 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
2623 (noffset >= 0) && (ndepth > 0);
2624 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2627 * Direct child node of the images parent node,
2628 * i.e. component image node.
2630 ret = fit_image_set_hashes(fit, noffset);
2640 * fit_image_set_hashes - calculate/set hashes for given component image node
2641 * @fit: pointer to the FIT format image header
2642 * @image_noffset: requested component image node
2644 * fit_image_set_hashes() adds hash values for an component image node. All
2645 * existing hash subnodes are checked, if algorithm property is set to one of
2646 * the supported hash algorithms, hash value is computed and corresponding
2647 * hash node property is set, for example:
2649 * Input component image node structure:
2651 * o image@1 (at image_noffset)
2652 * | - data = [binary data]
2656 * Output component image node structure:
2658 * o image@1 (at image_noffset)
2659 * | - data = [binary data]
2662 * |- value = sha1(data)
2668 int fit_image_set_hashes(void *fit, int image_noffset)
2673 uint8_t value[FIT_MAX_HASH_LEN];
2678 /* Get image data and data length */
2679 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2680 printf("Can't get image data/size\n");
2684 /* Process all hash subnodes of the component image node */
2685 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2686 (noffset >= 0) && (ndepth > 0);
2687 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2689 /* Direct child node of the component image node */
2692 * Check subnode name, must be equal to "hash".
2693 * Multiple hash nodes require unique unit node
2694 * names, e.g. hash@1, hash@2, etc.
2696 if (strncmp(fit_get_name(fit, noffset, NULL),
2698 strlen(FIT_HASH_NODENAME)) != 0) {
2699 /* Not a hash subnode, skip it */
2703 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2704 printf("Can't get hash algo property for "
2705 "'%s' hash node in '%s' image node\n",
2706 fit_get_name(fit, noffset, NULL),
2707 fit_get_name(fit, image_noffset, NULL));
2711 if (calculate_hash(data, size, algo, value,
2713 printf("Unsupported hash algorithm (%s) for "
2714 "'%s' hash node in '%s' image node\n",
2715 algo, fit_get_name(fit, noffset, NULL),
2716 fit_get_name(fit, image_noffset,
2721 if (fit_image_hash_set_value(fit, noffset, value,
2723 printf("Can't set hash value for "
2724 "'%s' hash node in '%s' image node\n",
2725 fit_get_name(fit, noffset, NULL),
2726 fit_get_name(fit, image_noffset, NULL));
2736 * fit_image_hash_set_value - set hash value in requested has node
2737 * @fit: pointer to the FIT format image header
2738 * @noffset: hash node offset
2739 * @value: hash value to be set
2740 * @value_len: hash value length
2742 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2743 * given and returns operation status to the caller.
2749 int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
2754 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
2756 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2757 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2764 #endif /* USE_HOSTCC */
2767 * fit_image_check_hashes - verify data intergity
2768 * @fit: pointer to the FIT format image header
2769 * @image_noffset: component image node offset
2771 * fit_image_check_hashes() goes over component image hash nodes,
2772 * re-calculates each data hash and compares with the value stored in hash
2776 * 1, if all hashes are valid
2777 * 0, otherwise (or on error)
2779 int fit_image_check_hashes(const void *fit, int image_noffset)
2789 uint8_t value[FIT_MAX_HASH_LEN];
2795 /* Get image data and data length */
2796 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2797 printf("Can't get image data/size\n");
2801 /* Process all hash subnodes of the component image node */
2802 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2803 (noffset >= 0) && (ndepth > 0);
2804 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2806 /* Direct child node of the component image node */
2809 * Check subnode name, must be equal to "hash".
2810 * Multiple hash nodes require unique unit node
2811 * names, e.g. hash@1, hash@2, etc.
2813 if (strncmp(fit_get_name(fit, noffset, NULL),
2815 strlen(FIT_HASH_NODENAME)) != 0)
2818 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2819 err_msg = " error!\nCan't get hash algo "
2826 fit_image_hash_get_ignore(fit, noffset, &ignore);
2828 printf("-skipped ");
2833 if (fit_image_hash_get_value(fit, noffset, &fit_value,
2835 err_msg = " error!\nCan't get hash value "
2840 if (calculate_hash(data, size, algo, value,
2842 err_msg = " error!\n"
2843 "Unsupported hash algorithm";
2847 if (value_len != fit_value_len) {
2848 err_msg = " error !\nBad hash value len";
2850 } else if (memcmp(value, fit_value, value_len) != 0) {
2851 err_msg = " error!\nBad hash value";
2858 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
2859 err_msg = " error!\nCorrupted or truncated tree";
2866 printf("%s for '%s' hash node in '%s' image node\n",
2867 err_msg, fit_get_name(fit, noffset, NULL),
2868 fit_get_name(fit, image_noffset, NULL));
2873 * fit_all_image_check_hashes - verify data intergity for all images
2874 * @fit: pointer to the FIT format image header
2876 * fit_all_image_check_hashes() goes over all images in the FIT and
2877 * for every images checks if all it's hashes are valid.
2880 * 1, if all hashes of all images are valid
2881 * 0, otherwise (or on error)
2883 int fit_all_image_check_hashes(const void *fit)
2890 /* Find images parent node offset */
2891 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2892 if (images_noffset < 0) {
2893 printf("Can't find images parent node '%s' (%s)\n",
2894 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2898 /* Process all image subnodes, check hashes for each */
2899 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
2901 for (ndepth = 0, count = 0,
2902 noffset = fdt_next_node(fit, images_noffset, &ndepth);
2903 (noffset >= 0) && (ndepth > 0);
2904 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2907 * Direct child node of the images parent node,
2908 * i.e. component image node.
2910 printf(" Hash(es) for Image %u (%s): ", count++,
2911 fit_get_name(fit, noffset, NULL));
2913 if (!fit_image_check_hashes(fit, noffset))
2922 * fit_image_check_os - check whether image node is of a given os type
2923 * @fit: pointer to the FIT format image header
2924 * @noffset: component image node offset
2925 * @os: requested image os
2927 * fit_image_check_os() reads image os property and compares its numeric
2928 * id with the requested os. Comparison result is returned to the caller.
2931 * 1 if image is of given os type
2932 * 0 otherwise (or on error)
2934 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
2938 if (fit_image_get_os(fit, noffset, &image_os))
2940 return (os == image_os);
2944 * fit_image_check_arch - check whether image node is of a given arch
2945 * @fit: pointer to the FIT format image header
2946 * @noffset: component image node offset
2947 * @arch: requested imagearch
2949 * fit_image_check_arch() reads image arch property and compares its numeric
2950 * id with the requested arch. Comparison result is returned to the caller.
2953 * 1 if image is of given arch
2954 * 0 otherwise (or on error)
2956 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
2960 if (fit_image_get_arch(fit, noffset, &image_arch))
2962 return (arch == image_arch);
2966 * fit_image_check_type - check whether image node is of a given type
2967 * @fit: pointer to the FIT format image header
2968 * @noffset: component image node offset
2969 * @type: requested image type
2971 * fit_image_check_type() reads image type property and compares its numeric
2972 * id with the requested type. Comparison result is returned to the caller.
2975 * 1 if image is of given type
2976 * 0 otherwise (or on error)
2978 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
2982 if (fit_image_get_type(fit, noffset, &image_type))
2984 return (type == image_type);
2988 * fit_image_check_comp - check whether image node uses given compression
2989 * @fit: pointer to the FIT format image header
2990 * @noffset: component image node offset
2991 * @comp: requested image compression type
2993 * fit_image_check_comp() reads image compression property and compares its
2994 * numeric id with the requested compression type. Comparison result is
2995 * returned to the caller.
2998 * 1 if image uses requested compression
2999 * 0 otherwise (or on error)
3001 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
3005 if (fit_image_get_comp(fit, noffset, &image_comp))
3007 return (comp == image_comp);
3011 * fit_check_format - sanity check FIT image format
3012 * @fit: pointer to the FIT format image header
3014 * fit_check_format() runs a basic sanity FIT image verification.
3015 * Routine checks for mandatory properties, nodes, etc.
3021 int fit_check_format(const void *fit)
3023 /* mandatory / node 'description' property */
3024 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
3025 debug("Wrong FIT format: no description\n");
3029 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
3030 /* mandatory / node 'timestamp' property */
3031 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
3032 debug("Wrong FIT format: no timestamp\n");
3037 /* mandatory subimages parent '/images' node */
3038 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
3039 debug("Wrong FIT format: no images parent node\n");
3048 * fit_conf_find_compat
3049 * @fit: pointer to the FIT format image header
3050 * @fdt: pointer to the device tree to compare against
3052 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
3053 * most compatible with the passed in device tree.
3062 * |-o configurations
3070 * |-compatible = "foo,bar", "bim,bam"
3073 * |-compatible = "foo,bar",
3076 * |-compatible = "bim,bam", "baz,biz"
3078 * Configuration 1 would be picked because the first string in U-Boot's
3079 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
3080 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
3083 * offset to the configuration to use if one was found
3086 int fit_conf_find_compat(const void *fit, const void *fdt)
3089 int noffset, confs_noffset, images_noffset;
3090 const void *fdt_compat;
3092 int best_match_offset = 0;
3093 int best_match_pos = 0;
3095 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3096 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
3097 if (confs_noffset < 0 || images_noffset < 0) {
3098 debug("Can't find configurations or images nodes.\n");
3102 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
3104 debug("Fdt for comparison has no \"compatible\" property.\n");
3109 * Loop over the configurations in the FIT image.
3111 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
3112 (noffset >= 0) && (ndepth > 0);
3113 noffset = fdt_next_node(fit, noffset, &ndepth)) {
3115 const char *kfdt_name;
3117 const char *cur_fdt_compat;
3125 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
3127 debug("No fdt property found.\n");
3130 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
3132 if (kfdt_noffset < 0) {
3133 debug("No image node named \"%s\" found.\n",
3138 * Get a pointer to this configuration's fdt.
3140 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
3141 debug("Failed to get fdt \"%s\".\n", kfdt_name);
3145 len = fdt_compat_len;
3146 cur_fdt_compat = fdt_compat;
3148 * Look for a match for each U-Boot compatibility string in
3149 * turn in this configuration's fdt.
3151 for (i = 0; len > 0 &&
3152 (!best_match_offset || best_match_pos > i); i++) {
3153 int cur_len = strlen(cur_fdt_compat) + 1;
3155 if (!fdt_node_check_compatible(kfdt, 0,
3157 best_match_offset = noffset;
3162 cur_fdt_compat += cur_len;
3165 if (!best_match_offset) {
3166 debug("No match found.\n");
3170 return best_match_offset;
3174 * fit_conf_get_node - get node offset for configuration of a given unit name
3175 * @fit: pointer to the FIT format image header
3176 * @conf_uname: configuration node unit name
3178 * fit_conf_get_node() finds a configuration (withing the '/configurations'
3179 * parant node) of a provided unit name. If configuration is found its node offset
3180 * is returned to the caller.
3182 * When NULL is provided in second argument fit_conf_get_node() will search
3183 * for a default configuration node instead. Default configuration node unit name
3184 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3187 * configuration node offset when found (>=0)
3188 * negative number on failure (FDT_ERR_* code)
3190 int fit_conf_get_node(const void *fit, const char *conf_uname)
3192 int noffset, confs_noffset;
3195 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3196 if (confs_noffset < 0) {
3197 debug("Can't find configurations parent node '%s' (%s)\n",
3198 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
3199 return confs_noffset;
3202 if (conf_uname == NULL) {
3203 /* get configuration unit name from the default property */
3204 debug("No configuration specified, trying default...\n");
3205 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3206 FIT_DEFAULT_PROP, &len);
3207 if (conf_uname == NULL) {
3208 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3212 debug("Found default configuration: '%s'\n", conf_uname);
3215 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
3217 debug("Can't get node offset for configuration unit name: "
3219 conf_uname, fdt_strerror(noffset));
3225 static int __fit_conf_get_prop_node(const void *fit, int noffset,
3226 const char *prop_name)
3231 /* get kernel image unit name from configuration kernel property */
3232 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
3236 return fit_image_get_node(fit, uname);
3240 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3241 * a given configuration
3242 * @fit: pointer to the FIT format image header
3243 * @noffset: configuration node offset
3245 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3246 * configuration FIT_KERNEL_PROP property and translates it to the node
3250 * image node offset when found (>=0)
3251 * negative number on failure (FDT_ERR_* code)
3253 int fit_conf_get_kernel_node(const void *fit, int noffset)
3255 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
3259 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3260 * a given configuration
3261 * @fit: pointer to the FIT format image header
3262 * @noffset: configuration node offset
3264 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3265 * configuration FIT_KERNEL_PROP property and translates it to the node
3269 * image node offset when found (>=0)
3270 * negative number on failure (FDT_ERR_* code)
3272 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
3274 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
3278 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3279 * a given configuration
3280 * @fit: pointer to the FIT format image header
3281 * @noffset: configuration node offset
3283 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3284 * configuration FIT_KERNEL_PROP property and translates it to the node
3288 * image node offset when found (>=0)
3289 * negative number on failure (FDT_ERR_* code)
3291 int fit_conf_get_fdt_node(const void *fit, int noffset)
3293 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
3297 * fit_conf_print - prints out the FIT configuration details
3298 * @fit: pointer to the FIT format image header
3299 * @noffset: offset of the configuration node
3300 * @p: pointer to prefix string
3302 * fit_conf_print() lists all mandatory properies for the processed
3303 * configuration node.
3306 * no returned results
3308 void fit_conf_print(const void *fit, int noffset, const char *p)
3314 /* Mandatory properties */
3315 ret = fit_get_desc(fit, noffset, &desc);
3316 printf("%s Description: ", p);
3318 printf("unavailable\n");
3320 printf("%s\n", desc);
3322 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3323 printf("%s Kernel: ", p);
3325 printf("unavailable\n");
3327 printf("%s\n", uname);
3329 /* Optional properties */
3330 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
3332 printf("%s Init Ramdisk: %s\n", p, uname);
3334 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
3336 printf("%s FDT: %s\n", p, uname);
3340 * fit_check_ramdisk - verify FIT format ramdisk subimage
3341 * @fit_hdr: pointer to the FIT ramdisk header
3342 * @rd_noffset: ramdisk subimage node offset within FIT image
3343 * @arch: requested ramdisk image architecture type
3344 * @verify: data CRC verification flag
3346 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3347 * specified FIT image.
3354 static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3357 fit_image_print(fit, rd_noffset, " ");
3360 puts(" Verifying Hash Integrity ... ");
3361 if (!fit_image_check_hashes(fit, rd_noffset)) {
3362 puts("Bad Data Hash\n");
3363 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
3369 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3370 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3371 !fit_image_check_arch(fit, rd_noffset, arch) ||
3372 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3373 printf("No Linux %s Ramdisk Image\n",
3374 genimg_get_arch_name(arch));
3375 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3379 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
3382 #endif /* USE_HOSTCC */
3383 #endif /* CONFIG_FIT */