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 int getenv_yesno(char *var)
421 char *s = getenv(var);
422 return (s && (*s == 'n')) ? 0 : 1;
425 ulong getenv_bootm_low(void)
427 char *s = getenv("bootm_low");
429 ulong tmp = simple_strtoul(s, NULL, 16);
433 #if defined(CONFIG_SYS_SDRAM_BASE)
434 return CONFIG_SYS_SDRAM_BASE;
435 #elif defined(CONFIG_ARM)
436 return gd->bd->bi_dram[0].start;
442 phys_size_t getenv_bootm_size(void)
445 char *s = getenv("bootm_size");
447 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
450 s = getenv("bootm_low");
452 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
457 #if defined(CONFIG_ARM)
458 return gd->bd->bi_dram[0].size - tmp;
460 return gd->bd->bi_memsize - tmp;
464 phys_size_t getenv_bootm_mapsize(void)
467 char *s = getenv("bootm_mapsize");
469 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
473 #if defined(CONFIG_SYS_BOOTMAPSZ)
474 return CONFIG_SYS_BOOTMAPSZ;
476 return getenv_bootm_size();
480 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
485 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
487 size_t tail = (len > chunksz) ? chunksz : len;
489 memmove(to, from, tail);
494 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
495 memmove(to, from, len);
496 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
498 #endif /* !USE_HOSTCC */
500 void genimg_print_size(uint32_t size)
503 printf("%d Bytes = ", size);
504 print_size(size, "\n");
506 printf("%d Bytes = %.2f kB = %.2f MB\n",
507 size, (double)size / 1.024e3,
508 (double)size / 1.048576e6);
512 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
513 static void genimg_print_time(time_t timestamp)
518 to_tm(timestamp, &tm);
519 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
520 tm.tm_year, tm.tm_mon, tm.tm_mday,
521 tm.tm_hour, tm.tm_min, tm.tm_sec);
523 printf("%s", ctime(×tamp));
526 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
529 * get_table_entry_name - translate entry id to long name
530 * @table: pointer to a translation table for entries of a specific type
531 * @msg: message to be returned when translation fails
532 * @id: entry id to be translated
534 * get_table_entry_name() will go over translation table trying to find
535 * entry that matches given id. If matching entry is found, its long
536 * name is returned to the caller.
539 * long entry name if translation succeeds
542 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
544 for (; table->id >= 0; ++table) {
546 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
549 return table->lname + gd->reloc_off;
555 const char *genimg_get_os_name(uint8_t os)
557 return (get_table_entry_name(uimage_os, "Unknown OS", os));
560 const char *genimg_get_arch_name(uint8_t arch)
562 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
566 const char *genimg_get_type_name(uint8_t type)
568 return (get_table_entry_name(uimage_type, "Unknown Image", type));
571 const char *genimg_get_comp_name(uint8_t comp)
573 return (get_table_entry_name(uimage_comp, "Unknown Compression",
578 * get_table_entry_id - translate short entry name to id
579 * @table: pointer to a translation table for entries of a specific type
580 * @table_name: to be used in case of error
581 * @name: entry short name to be translated
583 * get_table_entry_id() will go over translation table trying to find
584 * entry that matches given short name. If matching entry is found,
585 * its id returned to the caller.
588 * entry id if translation succeeds
591 int get_table_entry_id(const table_entry_t *table,
592 const char *table_name, const char *name)
594 const table_entry_t *t;
598 for (t = table; t->id >= 0; ++t) {
599 if (t->sname && strcasecmp(t->sname, name) == 0)
603 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
604 for (t = table; t->id >= 0; ++t) {
605 if (t->sname == NULL)
607 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
610 fprintf(stderr, "\n");
612 for (t = table; t->id >= 0; ++t) {
613 #ifdef CONFIG_NEEDS_MANUAL_RELOC
614 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
616 if (t->sname && strcmp(t->sname, name) == 0)
620 debug("Invalid %s Type: %s\n", table_name, name);
621 #endif /* USE_HOSTCC */
625 int genimg_get_os_id(const char *name)
627 return (get_table_entry_id(uimage_os, "OS", name));
630 int genimg_get_arch_id(const char *name)
632 return (get_table_entry_id(uimage_arch, "CPU", name));
635 int genimg_get_type_id(const char *name)
637 return (get_table_entry_id(uimage_type, "Image", name));
640 int genimg_get_comp_id(const char *name)
642 return (get_table_entry_id(uimage_comp, "Compression", name));
647 * genimg_get_format - get image format type
648 * @img_addr: image start address
650 * genimg_get_format() checks whether provided address points to a valid
651 * legacy or FIT image.
653 * New uImage format and FDT blob are based on a libfdt. FDT blob
654 * may be passed directly or embedded in a FIT image. In both situations
655 * genimg_get_format() must be able to dectect libfdt header.
658 * image format type or IMAGE_FORMAT_INVALID if no image is present
660 int genimg_get_format(void *img_addr)
662 ulong format = IMAGE_FORMAT_INVALID;
663 const image_header_t *hdr;
664 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
668 hdr = (const image_header_t *)img_addr;
669 if (image_check_magic(hdr))
670 format = IMAGE_FORMAT_LEGACY;
671 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
673 fit_hdr = (char *)img_addr;
674 if (fdt_check_header(fit_hdr) == 0)
675 format = IMAGE_FORMAT_FIT;
683 * genimg_get_image - get image from special storage (if necessary)
684 * @img_addr: image start address
686 * genimg_get_image() checks if provided image start adddress is located
687 * in a dataflash storage. If so, image is moved to a system RAM memory.
690 * image start address after possible relocation from special storage
692 ulong genimg_get_image(ulong img_addr)
694 ulong ram_addr = img_addr;
696 #ifdef CONFIG_HAS_DATAFLASH
697 ulong h_size, d_size;
699 if (addr_dataflash(img_addr)) {
700 /* ger RAM address */
701 ram_addr = CONFIG_SYS_LOAD_ADDR;
703 /* get header size */
704 h_size = image_get_header_size();
705 #if defined(CONFIG_FIT)
706 if (sizeof(struct fdt_header) > h_size)
707 h_size = sizeof(struct fdt_header);
711 debug(" Reading image header from dataflash address "
712 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
714 read_dataflash(img_addr, h_size, (char *)ram_addr);
717 switch (genimg_get_format((void *)ram_addr)) {
718 case IMAGE_FORMAT_LEGACY:
719 d_size = image_get_data_size(
720 (const image_header_t *)ram_addr);
721 debug(" Legacy format image found at 0x%08lx, "
725 #if defined(CONFIG_FIT)
726 case IMAGE_FORMAT_FIT:
727 d_size = fit_get_size((const void *)ram_addr) - h_size;
728 debug(" FIT/FDT format image found at 0x%08lx, "
734 printf(" No valid image found at 0x%08lx\n",
739 /* read in image data */
740 debug(" Reading image remaining data from dataflash address "
741 "%08lx to RAM address %08lx\n", img_addr + h_size,
744 read_dataflash(img_addr + h_size, d_size,
745 (char *)(ram_addr + h_size));
748 #endif /* CONFIG_HAS_DATAFLASH */
754 * fit_has_config - check if there is a valid FIT configuration
755 * @images: pointer to the bootm command headers structure
757 * fit_has_config() checks if there is a FIT configuration in use
758 * (if FTI support is present).
761 * 0, no FIT support or no configuration found
762 * 1, configuration found
764 int genimg_has_config(bootm_headers_t *images)
766 #if defined(CONFIG_FIT)
767 if (images->fit_uname_cfg)
774 * boot_get_ramdisk - main ramdisk handling routine
775 * @argc: command argument count
776 * @argv: command argument list
777 * @images: pointer to the bootm images structure
778 * @arch: expected ramdisk architecture
779 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
780 * @rd_end: pointer to a ulong variable, will hold ramdisk end
782 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
783 * Curently supported are the following ramdisk sources:
784 * - multicomponent kernel/ramdisk image,
785 * - commandline provided address of decicated ramdisk image.
788 * 0, if ramdisk image was found and valid, or skiped
789 * rd_start and rd_end are set to ramdisk start/end addresses if
790 * ramdisk image is found and valid
792 * 1, if ramdisk image is found but corrupted, or invalid
793 * rd_start and rd_end are set to 0 if no ramdisk exists
795 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
796 uint8_t arch, ulong *rd_start, ulong *rd_end)
798 ulong rd_addr, rd_load;
799 ulong rd_data, rd_len;
800 const image_header_t *rd_hdr;
801 #ifdef CONFIG_SUPPORT_RAW_INITRD
804 #if defined(CONFIG_FIT)
806 const char *fit_uname_config = NULL;
807 const char *fit_uname_ramdisk = NULL;
819 * Look for a '-' which indicates to ignore the
822 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
823 debug("## Skipping init Ramdisk\n");
824 rd_len = rd_data = 0;
825 } else if (argc >= 3 || genimg_has_config(images)) {
826 #if defined(CONFIG_FIT)
829 * If the init ramdisk comes from the FIT image and
830 * the FIT image address is omitted in the command
831 * line argument, try to use os FIT image address or
832 * default load address.
834 if (images->fit_uname_os)
835 default_addr = (ulong)images->fit_hdr_os;
837 default_addr = load_addr;
839 if (fit_parse_conf(argv[2], default_addr,
840 &rd_addr, &fit_uname_config)) {
841 debug("* ramdisk: config '%s' from image at "
843 fit_uname_config, rd_addr);
844 } else if (fit_parse_subimage(argv[2], default_addr,
845 &rd_addr, &fit_uname_ramdisk)) {
846 debug("* ramdisk: subimage '%s' from image at "
848 fit_uname_ramdisk, rd_addr);
852 rd_addr = simple_strtoul(argv[2], NULL, 16);
853 debug("* ramdisk: cmdline image address = "
857 #if defined(CONFIG_FIT)
859 /* use FIT configuration provided in first bootm
862 rd_addr = (ulong)images->fit_hdr_os;
863 fit_uname_config = images->fit_uname_cfg;
864 debug("* ramdisk: using config '%s' from image "
866 fit_uname_config, rd_addr);
869 * Check whether configuration has ramdisk defined,
870 * if not, don't try to use it, quit silently.
872 fit_hdr = (void *)rd_addr;
873 cfg_noffset = fit_conf_get_node(fit_hdr,
875 if (cfg_noffset < 0) {
876 debug("* ramdisk: no such config\n");
880 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
882 if (rd_noffset < 0) {
883 debug("* ramdisk: no ramdisk in config\n");
889 /* copy from dataflash if needed */
890 rd_addr = genimg_get_image(rd_addr);
893 * Check if there is an initrd image at the
894 * address provided in the second bootm argument
895 * check image type, for FIT images get FIT node.
897 switch (genimg_get_format((void *)rd_addr)) {
898 case IMAGE_FORMAT_LEGACY:
899 printf("## Loading init Ramdisk from Legacy "
900 "Image at %08lx ...\n", rd_addr);
902 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
903 rd_hdr = image_get_ramdisk(rd_addr, arch,
909 rd_data = image_get_data(rd_hdr);
910 rd_len = image_get_data_size(rd_hdr);
911 rd_load = image_get_load(rd_hdr);
913 #if defined(CONFIG_FIT)
914 case IMAGE_FORMAT_FIT:
915 fit_hdr = (void *)rd_addr;
916 printf("## Loading init Ramdisk from FIT "
917 "Image at %08lx ...\n", rd_addr);
919 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
920 if (!fit_check_format(fit_hdr)) {
921 puts("Bad FIT ramdisk image format!\n");
923 BOOTSTAGE_ID_FIT_RD_FORMAT);
926 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
928 if (!fit_uname_ramdisk) {
930 * no ramdisk image node unit name, try to get config
931 * node first. If config unit node name is NULL
932 * fit_conf_get_node() will try to find default config node
935 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
936 cfg_noffset = fit_conf_get_node(fit_hdr,
938 if (cfg_noffset < 0) {
939 puts("Could not find configuration "
942 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
945 fit_uname_config = fdt_get_name(fit_hdr,
947 printf(" Using '%s' configuration\n",
950 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
952 fit_uname_ramdisk = fit_get_name(fit_hdr,
955 /* get ramdisk component image node offset */
957 BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
958 rd_noffset = fit_image_get_node(fit_hdr,
961 if (rd_noffset < 0) {
962 puts("Could not find subimage node\n");
963 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
967 printf(" Trying '%s' ramdisk subimage\n",
970 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
971 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
975 /* get ramdisk image data address and length */
976 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
978 puts("Could not find ramdisk subimage data!\n");
979 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
982 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
984 rd_data = (ulong)data;
987 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
988 puts("Can't get ramdisk subimage load "
990 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
993 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
995 images->fit_hdr_rd = fit_hdr;
996 images->fit_uname_rd = fit_uname_ramdisk;
997 images->fit_noffset_rd = rd_noffset;
1001 #ifdef CONFIG_SUPPORT_RAW_INITRD
1002 if (argc >= 3 && (end = strchr(argv[2], ':'))) {
1003 rd_len = simple_strtoul(++end, NULL, 16);
1008 puts("Wrong Ramdisk Image Format\n");
1009 rd_data = rd_len = rd_load = 0;
1013 } else if (images->legacy_hdr_valid &&
1014 image_check_type(&images->legacy_hdr_os_copy,
1018 * Now check if we have a legacy mult-component image,
1019 * get second entry data start address and len.
1021 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1022 printf("## Loading init Ramdisk from multi component "
1023 "Legacy Image at %08lx ...\n",
1024 (ulong)images->legacy_hdr_os);
1026 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1031 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1032 rd_len = rd_data = 0;
1036 debug("## No init Ramdisk\n");
1038 *rd_start = rd_data;
1039 *rd_end = rd_data + rd_len;
1041 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1042 *rd_start, *rd_end);
1047 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1049 * boot_ramdisk_high - relocate init ramdisk
1050 * @lmb: pointer to lmb handle, will be used for memory mgmt
1051 * @rd_data: ramdisk data start address
1052 * @rd_len: ramdisk data length
1053 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1054 * start address (after possible relocation)
1055 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1056 * end address (after possible relocation)
1058 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1059 * variable and if requested ramdisk data is moved to a specified location.
1061 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1062 * start/end addresses if ramdisk image start and len were provided,
1063 * otherwise set initrd_start and initrd_end set to zeros.
1069 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1070 ulong *initrd_start, ulong *initrd_end)
1074 int initrd_copy_to_ram = 1;
1076 if ((s = getenv("initrd_high")) != NULL) {
1077 /* a value of "no" or a similar string will act like 0,
1078 * turning the "load high" feature off. This is intentional.
1080 initrd_high = simple_strtoul(s, NULL, 16);
1081 if (initrd_high == ~0)
1082 initrd_copy_to_ram = 0;
1084 /* not set, no restrictions to load high */
1089 #ifdef CONFIG_LOGBUFFER
1090 /* Prevent initrd from overwriting logbuffer */
1091 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1094 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1095 initrd_high, initrd_copy_to_ram);
1098 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1099 debug(" in-place initrd\n");
1100 *initrd_start = rd_data;
1101 *initrd_end = rd_data + rd_len;
1102 lmb_reserve(lmb, rd_data, rd_len);
1105 *initrd_start = (ulong)lmb_alloc_base(lmb,
1106 rd_len, 0x1000, initrd_high);
1108 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1111 if (*initrd_start == 0) {
1112 puts("ramdisk - allocation error\n");
1115 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1117 *initrd_end = *initrd_start + rd_len;
1118 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
1119 *initrd_start, *initrd_end);
1121 memmove_wd((void *)*initrd_start,
1122 (void *)rd_data, rd_len, CHUNKSZ);
1126 * Ensure the image is flushed to memory to handle
1127 * AMP boot scenarios in which we might not be
1130 flush_cache((unsigned long)*initrd_start, rd_len);
1138 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1139 *initrd_start, *initrd_end);
1146 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1148 #ifdef CONFIG_OF_LIBFDT
1149 static void fdt_error(const char *msg)
1153 puts(" - must RESET the board to recover.\n");
1156 static const image_header_t *image_get_fdt(ulong fdt_addr)
1158 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1160 image_print_contents(fdt_hdr);
1162 puts(" Verifying Checksum ... ");
1163 if (!image_check_hcrc(fdt_hdr)) {
1164 fdt_error("fdt header checksum invalid");
1168 if (!image_check_dcrc(fdt_hdr)) {
1169 fdt_error("fdt checksum invalid");
1174 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1175 fdt_error("uImage is not a fdt");
1178 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1179 fdt_error("uImage is compressed");
1182 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1183 fdt_error("uImage data is not a fdt");
1190 * fit_check_fdt - verify FIT format FDT subimage
1191 * @fit_hdr: pointer to the FIT header
1192 * fdt_noffset: FDT subimage node offset within FIT image
1193 * @verify: data CRC verification flag
1195 * fit_check_fdt() verifies integrity of the FDT subimage and from
1196 * specified FIT image.
1202 #if defined(CONFIG_FIT)
1203 static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
1205 fit_image_print(fit, fdt_noffset, " ");
1208 puts(" Verifying Hash Integrity ... ");
1209 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1210 fdt_error("Bad Data Hash");
1216 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1217 fdt_error("Not a FDT image");
1221 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1222 fdt_error("FDT image is compressed");
1228 #endif /* CONFIG_FIT */
1230 #ifndef CONFIG_SYS_FDT_PAD
1231 #define CONFIG_SYS_FDT_PAD 0x3000
1234 #if defined(CONFIG_OF_LIBFDT)
1236 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1237 * @lmb: pointer to lmb handle, will be used for memory mgmt
1238 * @fdt_blob: pointer to fdt blob base address
1240 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1241 * memreserve regions prevents u-boot from using them to store the initrd
1244 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1246 uint64_t addr, size;
1249 if (fdt_check_header(fdt_blob) != 0)
1252 total = fdt_num_mem_rsv(fdt_blob);
1253 for (i = 0; i < total; i++) {
1254 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1256 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1257 (unsigned long long)addr, (unsigned long long)size);
1258 lmb_reserve(lmb, addr, size);
1263 * boot_relocate_fdt - relocate flat device tree
1264 * @lmb: pointer to lmb handle, will be used for memory mgmt
1265 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1266 * @of_size: pointer to a ulong variable, will hold fdt length
1268 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1269 * relocates the of_flat_tree into that region, even if the fdt is already in
1270 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1273 * of_flat_tree and of_size are set to final (after relocation) values
1279 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1281 void *fdt_blob = *of_flat_tree;
1286 int disable_relocation = 0;
1292 if (fdt_check_header(fdt_blob) != 0) {
1293 fdt_error("image is not a fdt");
1297 /* position on a 4K boundary before the alloc_current */
1298 /* Pad the FDT by a specified amount */
1299 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1301 /* If fdt_high is set use it to select the relocation address */
1302 fdt_high = getenv("fdt_high");
1304 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1306 if (((ulong) desired_addr) == ~0UL) {
1307 /* All ones means use fdt in place */
1308 of_start = fdt_blob;
1309 lmb_reserve(lmb, (ulong)of_start, of_len);
1310 disable_relocation = 1;
1311 } else if (desired_addr) {
1313 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1314 (ulong)desired_addr);
1315 if (of_start == 0) {
1316 puts("Failed using fdt_high value for Device Tree");
1321 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
1325 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1326 getenv_bootm_mapsize()
1327 + getenv_bootm_low());
1330 if (of_start == 0) {
1331 puts("device tree - allocation error\n");
1335 if (disable_relocation) {
1336 /* We assume there is space after the existing fdt to use for padding */
1337 fdt_set_totalsize(of_start, of_len);
1338 printf(" Using Device Tree in place at %p, end %p\n",
1339 of_start, of_start + of_len - 1);
1341 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1342 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1344 printf(" Loading Device Tree to %p, end %p ... ",
1345 of_start, of_start + of_len - 1);
1347 err = fdt_open_into(fdt_blob, of_start, of_len);
1349 fdt_error("fdt move failed");
1355 *of_flat_tree = of_start;
1358 set_working_fdt_addr(*of_flat_tree);
1364 #endif /* CONFIG_OF_LIBFDT */
1367 * boot_get_fdt - main fdt handling routine
1368 * @argc: command argument count
1369 * @argv: command argument list
1370 * @images: pointer to the bootm images structure
1371 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1372 * @of_size: pointer to a ulong variable, will hold fdt length
1374 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1375 * Curently supported are the following ramdisk sources:
1376 * - multicomponent kernel/ramdisk image,
1377 * - commandline provided address of decicated ramdisk image.
1380 * 0, if fdt image was found and valid, or skipped
1381 * of_flat_tree and of_size are set to fdt start address and length if
1382 * fdt image is found and valid
1384 * 1, if fdt image is found but corrupted
1385 * of_flat_tree and of_size are set to 0 if no fdt exists
1387 int boot_get_fdt(int flag, int argc, char * const argv[],
1388 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
1390 const image_header_t *fdt_hdr;
1392 char *fdt_blob = NULL;
1393 ulong image_start, image_data, image_end;
1394 ulong load_start, load_end;
1395 #if defined(CONFIG_FIT)
1397 const char *fit_uname_config = NULL;
1398 const char *fit_uname_fdt = NULL;
1406 *of_flat_tree = NULL;
1409 if (argc > 3 || genimg_has_config(images)) {
1410 #if defined(CONFIG_FIT)
1413 * If the FDT blob comes from the FIT image and the
1414 * FIT image address is omitted in the command line
1415 * argument, try to use ramdisk or os FIT image
1416 * address or default load address.
1418 if (images->fit_uname_rd)
1419 default_addr = (ulong)images->fit_hdr_rd;
1420 else if (images->fit_uname_os)
1421 default_addr = (ulong)images->fit_hdr_os;
1423 default_addr = load_addr;
1425 if (fit_parse_conf(argv[3], default_addr,
1426 &fdt_addr, &fit_uname_config)) {
1427 debug("* fdt: config '%s' from image at "
1429 fit_uname_config, fdt_addr);
1430 } else if (fit_parse_subimage(argv[3], default_addr,
1431 &fdt_addr, &fit_uname_fdt)) {
1432 debug("* fdt: subimage '%s' from image at "
1434 fit_uname_fdt, fdt_addr);
1438 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1439 debug("* fdt: cmdline image address = "
1443 #if defined(CONFIG_FIT)
1445 /* use FIT configuration provided in first bootm
1448 fdt_addr = (ulong)images->fit_hdr_os;
1449 fit_uname_config = images->fit_uname_cfg;
1450 debug("* fdt: using config '%s' from image "
1452 fit_uname_config, fdt_addr);
1455 * Check whether configuration has FDT blob defined,
1456 * if not quit silently.
1458 fit_hdr = (void *)fdt_addr;
1459 cfg_noffset = fit_conf_get_node(fit_hdr,
1461 if (cfg_noffset < 0) {
1462 debug("* fdt: no such config\n");
1466 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
1468 if (fdt_noffset < 0) {
1469 debug("* fdt: no fdt in config\n");
1475 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1478 /* copy from dataflash if needed */
1479 fdt_addr = genimg_get_image(fdt_addr);
1482 * Check if there is an FDT image at the
1483 * address provided in the second bootm argument
1484 * check image type, for FIT images get a FIT node.
1486 switch (genimg_get_format((void *)fdt_addr)) {
1487 case IMAGE_FORMAT_LEGACY:
1488 /* verify fdt_addr points to a valid image header */
1489 printf("## Flattened Device Tree from Legacy Image "
1492 fdt_hdr = image_get_fdt(fdt_addr);
1497 * move image data to the load address,
1498 * make sure we don't overwrite initial image
1500 image_start = (ulong)fdt_hdr;
1501 image_data = (ulong)image_get_data(fdt_hdr);
1502 image_end = image_get_image_end(fdt_hdr);
1504 load_start = image_get_load(fdt_hdr);
1505 load_end = load_start + image_get_data_size(fdt_hdr);
1507 if (load_start == image_start ||
1508 load_start == image_data) {
1509 fdt_blob = (char *)image_data;
1513 if ((load_start < image_end) && (load_end > image_start)) {
1514 fdt_error("fdt overwritten");
1518 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
1519 image_data, load_start);
1521 memmove((void *)load_start,
1523 image_get_data_size(fdt_hdr));
1525 fdt_blob = (char *)load_start;
1527 case IMAGE_FORMAT_FIT:
1529 * This case will catch both: new uImage format
1530 * (libfdt based) and raw FDT blob (also libfdt
1533 #if defined(CONFIG_FIT)
1534 /* check FDT blob vs FIT blob */
1535 if (fit_check_format((const void *)fdt_addr)) {
1539 fit_hdr = (void *)fdt_addr;
1540 printf("## Flattened Device Tree from FIT "
1544 if (!fit_uname_fdt) {
1546 * no FDT blob image node unit name,
1547 * try to get config node first. If
1548 * config unit node name is NULL
1549 * fit_conf_get_node() will try to
1550 * find default config node
1552 cfg_noffset = fit_conf_get_node(fit_hdr,
1555 if (cfg_noffset < 0) {
1556 fdt_error("Could not find "
1562 fit_uname_config = fdt_get_name(fit_hdr,
1564 printf(" Using '%s' configuration\n",
1567 fdt_noffset = fit_conf_get_fdt_node(
1570 fit_uname_fdt = fit_get_name(fit_hdr,
1573 /* get FDT component image node offset */
1574 fdt_noffset = fit_image_get_node(
1578 if (fdt_noffset < 0) {
1579 fdt_error("Could not find subimage "
1584 printf(" Trying '%s' FDT blob subimage\n",
1587 if (!fit_check_fdt(fit_hdr, fdt_noffset,
1591 /* get ramdisk image data address and length */
1592 if (fit_image_get_data(fit_hdr, fdt_noffset,
1594 fdt_error("Could not find FDT "
1599 /* verift that image data is a proper FDT blob */
1600 if (fdt_check_header((char *)data) != 0) {
1601 fdt_error("Subimage data is not a FTD");
1606 * move image data to the load address,
1607 * make sure we don't overwrite initial image
1609 image_start = (ulong)fit_hdr;
1610 image_end = fit_get_end(fit_hdr);
1612 if (fit_image_get_load(fit_hdr, fdt_noffset,
1613 &load_start) == 0) {
1614 load_end = load_start + size;
1616 if ((load_start < image_end) &&
1617 (load_end > image_start)) {
1618 fdt_error("FDT overwritten");
1622 printf(" Loading FDT from 0x%08lx "
1627 memmove((void *)load_start,
1628 (void *)data, size);
1630 fdt_blob = (char *)load_start;
1632 fdt_blob = (char *)data;
1635 images->fit_hdr_fdt = fit_hdr;
1636 images->fit_uname_fdt = fit_uname_fdt;
1637 images->fit_noffset_fdt = fdt_noffset;
1645 fdt_blob = (char *)fdt_addr;
1646 debug("* fdt: raw FDT blob\n");
1647 printf("## Flattened Device Tree blob at "
1648 "%08lx\n", (long)fdt_blob);
1652 puts("ERROR: Did not find a cmdline Flattened Device "
1657 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
1659 } else if (images->legacy_hdr_valid &&
1660 image_check_type(&images->legacy_hdr_os_copy,
1663 ulong fdt_data, fdt_len;
1666 * Now check if we have a legacy multi-component image,
1667 * get second entry data start address and len.
1669 printf("## Flattened Device Tree from multi "
1670 "component Image at %08lX\n",
1671 (ulong)images->legacy_hdr_os);
1673 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1677 fdt_blob = (char *)fdt_data;
1678 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
1680 if (fdt_check_header(fdt_blob) != 0) {
1681 fdt_error("image is not a fdt");
1685 if (fdt_totalsize(fdt_blob) != fdt_len) {
1686 fdt_error("fdt size != image size");
1690 debug("## No Flattened Device Tree\n");
1694 debug("## No Flattened Device Tree\n");
1698 *of_flat_tree = fdt_blob;
1699 *of_size = fdt_totalsize(fdt_blob);
1700 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
1701 (ulong)*of_flat_tree, *of_size);
1710 #endif /* CONFIG_OF_LIBFDT */
1712 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1714 * boot_get_cmdline - allocate and initialize kernel cmdline
1715 * @lmb: pointer to lmb handle, will be used for memory mgmt
1716 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1717 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1719 * boot_get_cmdline() allocates space for kernel command line below
1720 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1721 * variable is present its contents is copied to allocated kernel
1728 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1733 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1734 getenv_bootm_mapsize() + getenv_bootm_low());
1736 if (cmdline == NULL)
1739 if ((s = getenv("bootargs")) == NULL)
1744 *cmd_start = (ulong) & cmdline[0];
1745 *cmd_end = *cmd_start + strlen(cmdline);
1747 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1751 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1753 #ifdef CONFIG_SYS_BOOT_GET_KBD
1755 * boot_get_kbd - allocate and initialize kernel copy of board info
1756 * @lmb: pointer to lmb handle, will be used for memory mgmt
1757 * @kbd: double pointer to board info data
1759 * boot_get_kbd() allocates space for kernel copy of board info data below
1760 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1761 * with the current u-boot board info data.
1767 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1769 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1770 getenv_bootm_mapsize() + getenv_bootm_low());
1776 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1778 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1779 do_bdinfo(NULL, 0, 0, NULL);
1784 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1785 #endif /* !USE_HOSTCC */
1787 #if defined(CONFIG_FIT)
1788 /*****************************************************************************/
1789 /* New uImage format routines */
1790 /*****************************************************************************/
1792 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
1793 ulong *addr, const char **name)
1800 sep = strchr(spec, sepc);
1803 *addr = simple_strtoul(spec, NULL, 16);
1813 * fit_parse_conf - parse FIT configuration spec
1814 * @spec: input string, containing configuration spec
1815 * @add_curr: current image address (to be used as a possible default)
1816 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1818 * @conf_name double pointer to a char, will hold pointer to a configuration
1821 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1822 * where <addr> is a FIT image address that contains configuration
1823 * with a <conf> unit name.
1825 * Address part is optional, and if omitted default add_curr will
1829 * 1 if spec is a valid configuration string,
1830 * addr and conf_name are set accordingly
1833 int fit_parse_conf(const char *spec, ulong addr_curr,
1834 ulong *addr, const char **conf_name)
1836 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
1840 * fit_parse_subimage - parse FIT subimage spec
1841 * @spec: input string, containing subimage spec
1842 * @add_curr: current image address (to be used as a possible default)
1843 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1845 * @image_name: double pointer to a char, will hold pointer to a subimage name
1847 * fit_parse_subimage() expects subimage spec in the for of
1848 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1849 * subimage with a <subimg> unit name.
1851 * Address part is optional, and if omitted default add_curr will
1855 * 1 if spec is a valid subimage string,
1856 * addr and image_name are set accordingly
1859 int fit_parse_subimage(const char *spec, ulong addr_curr,
1860 ulong *addr, const char **image_name)
1862 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
1864 #endif /* !USE_HOSTCC */
1866 static void fit_get_debug(const void *fit, int noffset,
1867 char *prop_name, int err)
1869 debug("Can't get '%s' property from FIT 0x%08lx, "
1870 "node: offset %d, name %s (%s)\n",
1871 prop_name, (ulong)fit, noffset,
1872 fit_get_name(fit, noffset, NULL),
1877 * fit_print_contents - prints out the contents of the FIT format image
1878 * @fit: pointer to the FIT format image header
1879 * @p: pointer to prefix string
1881 * fit_print_contents() formats a multi line FIT image contents description.
1882 * The routine prints out FIT image properties (root node level) follwed by
1883 * the details of each component image.
1886 * no returned results
1888 void fit_print_contents(const void *fit)
1899 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1909 /* Root node properties */
1910 ret = fit_get_desc(fit, 0, &desc);
1911 printf("%sFIT description: ", p);
1913 printf("unavailable\n");
1915 printf("%s\n", desc);
1917 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1918 ret = fit_get_timestamp(fit, 0, ×tamp);
1919 printf("%sCreated: ", p);
1921 printf("unavailable\n");
1923 genimg_print_time(timestamp);
1926 /* Find images parent node offset */
1927 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1928 if (images_noffset < 0) {
1929 printf("Can't find images parent node '%s' (%s)\n",
1930 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1934 /* Process its subnodes, print out component images details */
1935 for (ndepth = 0, count = 0,
1936 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1937 (noffset >= 0) && (ndepth > 0);
1938 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1941 * Direct child node of the images parent node,
1942 * i.e. component image node.
1944 printf("%s Image %u (%s)\n", p, count++,
1945 fit_get_name(fit, noffset, NULL));
1947 fit_image_print(fit, noffset, p);
1951 /* Find configurations parent node offset */
1952 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1953 if (confs_noffset < 0) {
1954 debug("Can't get configurations parent node '%s' (%s)\n",
1955 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1959 /* get default configuration unit name from default property */
1960 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
1962 printf("%s Default Configuration: '%s'\n", p, uname);
1964 /* Process its subnodes, print out configurations details */
1965 for (ndepth = 0, count = 0,
1966 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1967 (noffset >= 0) && (ndepth > 0);
1968 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1971 * Direct child node of the configurations parent node,
1972 * i.e. configuration node.
1974 printf("%s Configuration %u (%s)\n", p, count++,
1975 fit_get_name(fit, noffset, NULL));
1977 fit_conf_print(fit, noffset, p);
1983 * fit_image_print - prints out the FIT component image details
1984 * @fit: pointer to the FIT format image header
1985 * @image_noffset: offset of the component image node
1986 * @p: pointer to prefix string
1988 * fit_image_print() lists all mandatory properies for the processed component
1989 * image. If present, hash nodes are printed out as well. Load
1990 * address for images of type firmware is also printed out. Since the load
1991 * address is not mandatory for firmware images, it will be output as
1992 * "unavailable" when not present.
1995 * no returned results
1997 void fit_image_print(const void *fit, int image_noffset, const char *p)
2000 uint8_t type, arch, os, comp;
2008 /* Mandatory properties */
2009 ret = fit_get_desc(fit, image_noffset, &desc);
2010 printf("%s Description: ", p);
2012 printf("unavailable\n");
2014 printf("%s\n", desc);
2016 fit_image_get_type(fit, image_noffset, &type);
2017 printf("%s Type: %s\n", p, genimg_get_type_name(type));
2019 fit_image_get_comp(fit, image_noffset, &comp);
2020 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
2022 ret = fit_image_get_data(fit, image_noffset, &data, &size);
2025 printf("%s Data Start: ", p);
2027 printf("unavailable\n");
2029 printf("0x%08lx\n", (ulong)data);
2032 printf("%s Data Size: ", p);
2034 printf("unavailable\n");
2036 genimg_print_size(size);
2038 /* Remaining, type dependent properties */
2039 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2040 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2041 (type == IH_TYPE_FLATDT)) {
2042 fit_image_get_arch(fit, image_noffset, &arch);
2043 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
2046 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
2047 fit_image_get_os(fit, image_noffset, &os);
2048 printf("%s OS: %s\n", p, genimg_get_os_name(os));
2051 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2052 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
2053 ret = fit_image_get_load(fit, image_noffset, &load);
2054 printf("%s Load Address: ", p);
2056 printf("unavailable\n");
2058 printf("0x%08lx\n", load);
2061 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2062 (type == IH_TYPE_RAMDISK)) {
2063 fit_image_get_entry(fit, image_noffset, &entry);
2064 printf("%s Entry Point: ", p);
2066 printf("unavailable\n");
2068 printf("0x%08lx\n", entry);
2071 /* Process all hash subnodes of the component image node */
2072 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2073 (noffset >= 0) && (ndepth > 0);
2074 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2076 /* Direct child node of the component image node */
2077 fit_image_print_hash(fit, noffset, p);
2083 * fit_image_print_hash - prints out the hash node details
2084 * @fit: pointer to the FIT format image header
2085 * @noffset: offset of the hash node
2086 * @p: pointer to prefix string
2088 * fit_image_print_hash() lists properies for the processed hash node
2091 * no returned results
2093 void fit_image_print_hash(const void *fit, int noffset, const char *p)
2101 * Check subnode name, must be equal to "hash".
2102 * Multiple hash nodes require unique unit node
2103 * names, e.g. hash@1, hash@2, etc.
2105 if (strncmp(fit_get_name(fit, noffset, NULL),
2107 strlen(FIT_HASH_NODENAME)) != 0)
2110 debug("%s Hash node: '%s'\n", p,
2111 fit_get_name(fit, noffset, NULL));
2113 printf("%s Hash algo: ", p);
2114 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2115 printf("invalid/unsupported\n");
2118 printf("%s\n", algo);
2120 ret = fit_image_hash_get_value(fit, noffset, &value,
2122 printf("%s Hash value: ", p);
2124 printf("unavailable\n");
2126 for (i = 0; i < value_len; i++)
2127 printf("%02x", value[i]);
2131 debug("%s Hash len: %d\n", p, value_len);
2135 * fit_get_desc - get node description property
2136 * @fit: pointer to the FIT format image header
2137 * @noffset: node offset
2138 * @desc: double pointer to the char, will hold pointer to the descrption
2140 * fit_get_desc() reads description property from a given node, if
2141 * description is found pointer to it is returened in third call argument.
2147 int fit_get_desc(const void *fit, int noffset, char **desc)
2151 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
2152 if (*desc == NULL) {
2153 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
2161 * fit_get_timestamp - get node timestamp property
2162 * @fit: pointer to the FIT format image header
2163 * @noffset: node offset
2164 * @timestamp: pointer to the time_t, will hold read timestamp
2166 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2167 * is found and has a correct size its value is retured in third call
2172 * -1, on property read failure
2173 * -2, on wrong timestamp size
2175 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
2180 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
2182 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
2185 if (len != sizeof(uint32_t)) {
2186 debug("FIT timestamp with incorrect size of (%u)\n", len);
2190 *timestamp = uimage_to_cpu(*((uint32_t *)data));
2195 * fit_image_get_node - get node offset for component image of a given unit name
2196 * @fit: pointer to the FIT format image header
2197 * @image_uname: component image node unit name
2199 * fit_image_get_node() finds a component image (withing the '/images'
2200 * node) of a provided unit name. If image is found its node offset is
2201 * returned to the caller.
2204 * image node offset when found (>=0)
2205 * negative number on failure (FDT_ERR_* code)
2207 int fit_image_get_node(const void *fit, const char *image_uname)
2209 int noffset, images_noffset;
2211 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2212 if (images_noffset < 0) {
2213 debug("Can't find images parent node '%s' (%s)\n",
2214 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2215 return images_noffset;
2218 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
2220 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2221 image_uname, fdt_strerror(noffset));
2228 * fit_image_get_os - get os id for a given component image node
2229 * @fit: pointer to the FIT format image header
2230 * @noffset: component image node offset
2231 * @os: pointer to the uint8_t, will hold os numeric id
2233 * fit_image_get_os() finds os property in a given component image node.
2234 * If the property is found, its (string) value is translated to the numeric
2235 * id which is returned to the caller.
2241 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
2246 /* Get OS name from property data */
2247 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
2249 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
2254 /* Translate OS name to id */
2255 *os = genimg_get_os_id(data);
2260 * fit_image_get_arch - get arch id for a given component image node
2261 * @fit: pointer to the FIT format image header
2262 * @noffset: component image node offset
2263 * @arch: pointer to the uint8_t, will hold arch numeric id
2265 * fit_image_get_arch() finds arch property in a given component image node.
2266 * If the property is found, its (string) value is translated to the numeric
2267 * id which is returned to the caller.
2273 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
2278 /* Get architecture name from property data */
2279 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
2281 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
2286 /* Translate architecture name to id */
2287 *arch = genimg_get_arch_id(data);
2292 * fit_image_get_type - get type id for a given component image node
2293 * @fit: pointer to the FIT format image header
2294 * @noffset: component image node offset
2295 * @type: pointer to the uint8_t, will hold type numeric id
2297 * fit_image_get_type() finds type property in a given component image node.
2298 * If the property is found, its (string) value is translated to the numeric
2299 * id which is returned to the caller.
2305 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
2310 /* Get image type name from property data */
2311 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
2313 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
2318 /* Translate image type name to id */
2319 *type = genimg_get_type_id(data);
2324 * fit_image_get_comp - get comp id for a given component image node
2325 * @fit: pointer to the FIT format image header
2326 * @noffset: component image node offset
2327 * @comp: pointer to the uint8_t, will hold comp numeric id
2329 * fit_image_get_comp() finds comp property in a given component image node.
2330 * If the property is found, its (string) value is translated to the numeric
2331 * id which is returned to the caller.
2337 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
2342 /* Get compression name from property data */
2343 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
2345 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
2350 /* Translate compression name to id */
2351 *comp = genimg_get_comp_id(data);
2356 * fit_image_get_load - get load address property for a given component image node
2357 * @fit: pointer to the FIT format image header
2358 * @noffset: component image node offset
2359 * @load: pointer to the uint32_t, will hold load address
2361 * fit_image_get_load() finds load address property in a given component image node.
2362 * If the property is found, its value is returned to the caller.
2368 int fit_image_get_load(const void *fit, int noffset, ulong *load)
2371 const uint32_t *data;
2373 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
2375 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
2379 *load = uimage_to_cpu(*data);
2384 * fit_image_get_entry - get entry point address property for a given component image node
2385 * @fit: pointer to the FIT format image header
2386 * @noffset: component image node offset
2387 * @entry: pointer to the uint32_t, will hold entry point address
2389 * fit_image_get_entry() finds entry point address property in a given component image node.
2390 * If the property is found, its value is returned to the caller.
2396 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
2399 const uint32_t *data;
2401 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
2403 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
2407 *entry = uimage_to_cpu(*data);
2412 * fit_image_get_data - get data property and its size for a given component image node
2413 * @fit: pointer to the FIT format image header
2414 * @noffset: component image node offset
2415 * @data: double pointer to void, will hold data property's data address
2416 * @size: pointer to size_t, will hold data property's data size
2418 * fit_image_get_data() finds data property in a given component image node.
2419 * If the property is found its data start address and size are returned to
2426 int fit_image_get_data(const void *fit, int noffset,
2427 const void **data, size_t *size)
2431 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
2432 if (*data == NULL) {
2433 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
2443 * fit_image_hash_get_algo - get hash algorithm name
2444 * @fit: pointer to the FIT format image header
2445 * @noffset: hash node offset
2446 * @algo: double pointer to char, will hold pointer to the algorithm name
2448 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2449 * If the property is found its data start address is returned to the caller.
2455 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
2459 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
2460 if (*algo == NULL) {
2461 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
2469 * fit_image_hash_get_value - get hash value and length
2470 * @fit: pointer to the FIT format image header
2471 * @noffset: hash node offset
2472 * @value: double pointer to uint8_t, will hold address of a hash value data
2473 * @value_len: pointer to an int, will hold hash data length
2475 * fit_image_hash_get_value() finds hash value property in a given hash node.
2476 * If the property is found its data start address and size are returned to
2483 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
2488 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
2489 if (*value == NULL) {
2490 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
2500 * fit_set_timestamp - set node timestamp property
2501 * @fit: pointer to the FIT format image header
2502 * @noffset: node offset
2503 * @timestamp: timestamp value to be set
2505 * fit_set_timestamp() attempts to set timestamp property in the requested
2506 * node and returns operation status to the caller.
2510 * -1, on property read failure
2512 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
2517 t = cpu_to_uimage(timestamp);
2518 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2521 printf("Can't set '%s' property for '%s' node (%s)\n",
2522 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2531 * calculate_hash - calculate and return hash for provided input data
2532 * @data: pointer to the input data
2533 * @data_len: data length
2534 * @algo: requested hash algorithm
2535 * @value: pointer to the char, will hold hash value data (caller must
2536 * allocate enough free space)
2537 * value_len: length of the calculated hash
2539 * calculate_hash() computes input data hash according to the requested algorithm.
2540 * Resulting hash value is placed in caller provided 'value' buffer, length
2541 * of the calculated hash is returned via value_len pointer argument.
2545 * -1, when algo is unsupported
2547 static int calculate_hash(const void *data, int data_len, const char *algo,
2548 uint8_t *value, int *value_len)
2550 if (strcmp(algo, "crc32") == 0) {
2551 *((uint32_t *)value) = crc32_wd(0, data, data_len,
2553 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
2555 } else if (strcmp(algo, "sha1") == 0) {
2556 sha1_csum_wd((unsigned char *) data, data_len,
2557 (unsigned char *) value, CHUNKSZ_SHA1);
2559 } else if (strcmp(algo, "md5") == 0) {
2560 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2563 debug("Unsupported hash alogrithm\n");
2571 * fit_set_hashes - process FIT component image nodes and calculate hashes
2572 * @fit: pointer to the FIT format image header
2574 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2575 * Hashes are calculated for all component images which have hash subnodes
2576 * with algorithm property set to one of the supported hash algorithms.
2580 * libfdt error code, on failure
2582 int fit_set_hashes(void *fit)
2589 /* Find images parent node offset */
2590 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2591 if (images_noffset < 0) {
2592 printf("Can't find images parent node '%s' (%s)\n",
2593 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2594 return images_noffset;
2597 /* Process its subnodes, print out component images details */
2598 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
2599 (noffset >= 0) && (ndepth > 0);
2600 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2603 * Direct child node of the images parent node,
2604 * i.e. component image node.
2606 ret = fit_image_set_hashes(fit, noffset);
2616 * fit_image_set_hashes - calculate/set hashes for given component image node
2617 * @fit: pointer to the FIT format image header
2618 * @image_noffset: requested component image node
2620 * fit_image_set_hashes() adds hash values for an component image node. All
2621 * existing hash subnodes are checked, if algorithm property is set to one of
2622 * the supported hash algorithms, hash value is computed and corresponding
2623 * hash node property is set, for example:
2625 * Input component image node structure:
2627 * o image@1 (at image_noffset)
2628 * | - data = [binary data]
2632 * Output component image node structure:
2634 * o image@1 (at image_noffset)
2635 * | - data = [binary data]
2638 * |- value = sha1(data)
2644 int fit_image_set_hashes(void *fit, int image_noffset)
2649 uint8_t value[FIT_MAX_HASH_LEN];
2654 /* Get image data and data length */
2655 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2656 printf("Can't get image data/size\n");
2660 /* Process all hash subnodes of the component image node */
2661 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2662 (noffset >= 0) && (ndepth > 0);
2663 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2665 /* Direct child node of the component image node */
2668 * Check subnode name, must be equal to "hash".
2669 * Multiple hash nodes require unique unit node
2670 * names, e.g. hash@1, hash@2, etc.
2672 if (strncmp(fit_get_name(fit, noffset, NULL),
2674 strlen(FIT_HASH_NODENAME)) != 0) {
2675 /* Not a hash subnode, skip it */
2679 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2680 printf("Can't get hash algo property for "
2681 "'%s' hash node in '%s' image node\n",
2682 fit_get_name(fit, noffset, NULL),
2683 fit_get_name(fit, image_noffset, NULL));
2687 if (calculate_hash(data, size, algo, value,
2689 printf("Unsupported hash algorithm (%s) for "
2690 "'%s' hash node in '%s' image node\n",
2691 algo, fit_get_name(fit, noffset, NULL),
2692 fit_get_name(fit, image_noffset,
2697 if (fit_image_hash_set_value(fit, noffset, value,
2699 printf("Can't set hash value for "
2700 "'%s' hash node in '%s' image node\n",
2701 fit_get_name(fit, noffset, NULL),
2702 fit_get_name(fit, image_noffset, NULL));
2712 * fit_image_hash_set_value - set hash value in requested has node
2713 * @fit: pointer to the FIT format image header
2714 * @noffset: hash node offset
2715 * @value: hash value to be set
2716 * @value_len: hash value length
2718 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2719 * given and returns operation status to the caller.
2725 int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
2730 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
2732 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2733 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2740 #endif /* USE_HOSTCC */
2743 * fit_image_check_hashes - verify data intergity
2744 * @fit: pointer to the FIT format image header
2745 * @image_noffset: component image node offset
2747 * fit_image_check_hashes() goes over component image hash nodes,
2748 * re-calculates each data hash and compares with the value stored in hash
2752 * 1, if all hashes are valid
2753 * 0, otherwise (or on error)
2755 int fit_image_check_hashes(const void *fit, int image_noffset)
2762 uint8_t value[FIT_MAX_HASH_LEN];
2768 /* Get image data and data length */
2769 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2770 printf("Can't get image data/size\n");
2774 /* Process all hash subnodes of the component image node */
2775 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2776 (noffset >= 0) && (ndepth > 0);
2777 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2779 /* Direct child node of the component image node */
2782 * Check subnode name, must be equal to "hash".
2783 * Multiple hash nodes require unique unit node
2784 * names, e.g. hash@1, hash@2, etc.
2786 if (strncmp(fit_get_name(fit, noffset, NULL),
2788 strlen(FIT_HASH_NODENAME)) != 0)
2791 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2792 err_msg = " error!\nCan't get hash algo "
2798 if (fit_image_hash_get_value(fit, noffset, &fit_value,
2800 err_msg = " error!\nCan't get hash value "
2805 if (calculate_hash(data, size, algo, value,
2807 err_msg = " error!\n"
2808 "Unsupported hash algorithm";
2812 if (value_len != fit_value_len) {
2813 err_msg = " error !\nBad hash value len";
2815 } else if (memcmp(value, fit_value, value_len) != 0) {
2816 err_msg = " error!\nBad hash value";
2826 printf("%s for '%s' hash node in '%s' image node\n",
2827 err_msg, fit_get_name(fit, noffset, NULL),
2828 fit_get_name(fit, image_noffset, NULL));
2833 * fit_all_image_check_hashes - verify data intergity for all images
2834 * @fit: pointer to the FIT format image header
2836 * fit_all_image_check_hashes() goes over all images in the FIT and
2837 * for every images checks if all it's hashes are valid.
2840 * 1, if all hashes of all images are valid
2841 * 0, otherwise (or on error)
2843 int fit_all_image_check_hashes(const void *fit)
2850 /* Find images parent node offset */
2851 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2852 if (images_noffset < 0) {
2853 printf("Can't find images parent node '%s' (%s)\n",
2854 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2858 /* Process all image subnodes, check hashes for each */
2859 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
2861 for (ndepth = 0, count = 0,
2862 noffset = fdt_next_node(fit, images_noffset, &ndepth);
2863 (noffset >= 0) && (ndepth > 0);
2864 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2867 * Direct child node of the images parent node,
2868 * i.e. component image node.
2870 printf(" Hash(es) for Image %u (%s): ", count++,
2871 fit_get_name(fit, noffset, NULL));
2873 if (!fit_image_check_hashes(fit, noffset))
2882 * fit_image_check_os - check whether image node is of a given os type
2883 * @fit: pointer to the FIT format image header
2884 * @noffset: component image node offset
2885 * @os: requested image os
2887 * fit_image_check_os() reads image os property and compares its numeric
2888 * id with the requested os. Comparison result is returned to the caller.
2891 * 1 if image is of given os type
2892 * 0 otherwise (or on error)
2894 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
2898 if (fit_image_get_os(fit, noffset, &image_os))
2900 return (os == image_os);
2904 * fit_image_check_arch - check whether image node is of a given arch
2905 * @fit: pointer to the FIT format image header
2906 * @noffset: component image node offset
2907 * @arch: requested imagearch
2909 * fit_image_check_arch() reads image arch property and compares its numeric
2910 * id with the requested arch. Comparison result is returned to the caller.
2913 * 1 if image is of given arch
2914 * 0 otherwise (or on error)
2916 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
2920 if (fit_image_get_arch(fit, noffset, &image_arch))
2922 return (arch == image_arch);
2926 * fit_image_check_type - check whether image node is of a given type
2927 * @fit: pointer to the FIT format image header
2928 * @noffset: component image node offset
2929 * @type: requested image type
2931 * fit_image_check_type() reads image type property and compares its numeric
2932 * id with the requested type. Comparison result is returned to the caller.
2935 * 1 if image is of given type
2936 * 0 otherwise (or on error)
2938 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
2942 if (fit_image_get_type(fit, noffset, &image_type))
2944 return (type == image_type);
2948 * fit_image_check_comp - check whether image node uses given compression
2949 * @fit: pointer to the FIT format image header
2950 * @noffset: component image node offset
2951 * @comp: requested image compression type
2953 * fit_image_check_comp() reads image compression property and compares its
2954 * numeric id with the requested compression type. Comparison result is
2955 * returned to the caller.
2958 * 1 if image uses requested compression
2959 * 0 otherwise (or on error)
2961 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
2965 if (fit_image_get_comp(fit, noffset, &image_comp))
2967 return (comp == image_comp);
2971 * fit_check_format - sanity check FIT image format
2972 * @fit: pointer to the FIT format image header
2974 * fit_check_format() runs a basic sanity FIT image verification.
2975 * Routine checks for mandatory properties, nodes, etc.
2981 int fit_check_format(const void *fit)
2983 /* mandatory / node 'description' property */
2984 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2985 debug("Wrong FIT format: no description\n");
2989 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2990 /* mandatory / node 'timestamp' property */
2991 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2992 debug("Wrong FIT format: no timestamp\n");
2997 /* mandatory subimages parent '/images' node */
2998 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
2999 debug("Wrong FIT format: no images parent node\n");
3007 * fit_conf_get_node - get node offset for configuration of a given unit name
3008 * @fit: pointer to the FIT format image header
3009 * @conf_uname: configuration node unit name
3011 * fit_conf_get_node() finds a configuration (withing the '/configurations'
3012 * parant node) of a provided unit name. If configuration is found its node offset
3013 * is returned to the caller.
3015 * When NULL is provided in second argument fit_conf_get_node() will search
3016 * for a default configuration node instead. Default configuration node unit name
3017 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3020 * configuration node offset when found (>=0)
3021 * negative number on failure (FDT_ERR_* code)
3023 int fit_conf_get_node(const void *fit, const char *conf_uname)
3025 int noffset, confs_noffset;
3028 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3029 if (confs_noffset < 0) {
3030 debug("Can't find configurations parent node '%s' (%s)\n",
3031 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
3032 return confs_noffset;
3035 if (conf_uname == NULL) {
3036 /* get configuration unit name from the default property */
3037 debug("No configuration specified, trying default...\n");
3038 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3039 FIT_DEFAULT_PROP, &len);
3040 if (conf_uname == NULL) {
3041 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3045 debug("Found default configuration: '%s'\n", conf_uname);
3048 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
3050 debug("Can't get node offset for configuration unit name: "
3052 conf_uname, fdt_strerror(noffset));
3058 static int __fit_conf_get_prop_node(const void *fit, int noffset,
3059 const char *prop_name)
3064 /* get kernel image unit name from configuration kernel property */
3065 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
3069 return fit_image_get_node(fit, uname);
3073 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3074 * a given configuration
3075 * @fit: pointer to the FIT format image header
3076 * @noffset: configuration node offset
3078 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3079 * configuration FIT_KERNEL_PROP property and translates it to the node
3083 * image node offset when found (>=0)
3084 * negative number on failure (FDT_ERR_* code)
3086 int fit_conf_get_kernel_node(const void *fit, int noffset)
3088 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
3092 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3093 * a given configuration
3094 * @fit: pointer to the FIT format image header
3095 * @noffset: configuration node offset
3097 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3098 * configuration FIT_KERNEL_PROP property and translates it to the node
3102 * image node offset when found (>=0)
3103 * negative number on failure (FDT_ERR_* code)
3105 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
3107 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
3111 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3112 * a given configuration
3113 * @fit: pointer to the FIT format image header
3114 * @noffset: configuration node offset
3116 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3117 * configuration FIT_KERNEL_PROP property and translates it to the node
3121 * image node offset when found (>=0)
3122 * negative number on failure (FDT_ERR_* code)
3124 int fit_conf_get_fdt_node(const void *fit, int noffset)
3126 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
3130 * fit_conf_print - prints out the FIT configuration details
3131 * @fit: pointer to the FIT format image header
3132 * @noffset: offset of the configuration node
3133 * @p: pointer to prefix string
3135 * fit_conf_print() lists all mandatory properies for the processed
3136 * configuration node.
3139 * no returned results
3141 void fit_conf_print(const void *fit, int noffset, const char *p)
3147 /* Mandatory properties */
3148 ret = fit_get_desc(fit, noffset, &desc);
3149 printf("%s Description: ", p);
3151 printf("unavailable\n");
3153 printf("%s\n", desc);
3155 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3156 printf("%s Kernel: ", p);
3158 printf("unavailable\n");
3160 printf("%s\n", uname);
3162 /* Optional properties */
3163 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
3165 printf("%s Init Ramdisk: %s\n", p, uname);
3167 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
3169 printf("%s FDT: %s\n", p, uname);
3173 * fit_check_ramdisk - verify FIT format ramdisk subimage
3174 * @fit_hdr: pointer to the FIT ramdisk header
3175 * @rd_noffset: ramdisk subimage node offset within FIT image
3176 * @arch: requested ramdisk image architecture type
3177 * @verify: data CRC verification flag
3179 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3180 * specified FIT image.
3187 static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3190 fit_image_print(fit, rd_noffset, " ");
3193 puts(" Verifying Hash Integrity ... ");
3194 if (!fit_image_check_hashes(fit, rd_noffset)) {
3195 puts("Bad Data Hash\n");
3196 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
3202 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3203 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3204 !fit_image_check_arch(fit, rd_noffset, arch) ||
3205 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3206 printf("No Linux %s Ramdisk Image\n",
3207 genimg_get_arch_name(arch));
3208 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3212 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
3215 #endif /* USE_HOSTCC */
3216 #endif /* CONFIG_FIT */