2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #ifdef CONFIG_SHOW_BOOT_PROGRESS
31 #include <status_led.h>
34 #ifdef CONFIG_HAS_DATAFLASH
35 #include <dataflash.h>
38 #ifdef CONFIG_LOGBUFFER
42 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
48 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
51 #include <fdt_support.h>
54 #if defined(CONFIG_FIT)
55 #include <u-boot/md5.h>
58 static int fit_check_ramdisk(const void *fit, int os_noffset,
59 uint8_t arch, int verify);
63 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
66 DECLARE_GLOBAL_DATA_PTR;
68 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
72 #include <u-boot/md5.h>
75 #endif /* !USE_HOSTCC*/
77 static const table_entry_t uimage_arch[] = {
78 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
79 { IH_ARCH_ALPHA, "alpha", "Alpha", },
80 { IH_ARCH_ARM, "arm", "ARM", },
81 { IH_ARCH_I386, "x86", "Intel x86", },
82 { IH_ARCH_IA64, "ia64", "IA64", },
83 { IH_ARCH_M68K, "m68k", "M68K", },
84 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
85 { IH_ARCH_MIPS, "mips", "MIPS", },
86 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
87 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
88 { IH_ARCH_PPC, "powerpc", "PowerPC", },
89 { IH_ARCH_PPC, "ppc", "PowerPC", },
90 { IH_ARCH_S390, "s390", "IBM S390", },
91 { IH_ARCH_SH, "sh", "SuperH", },
92 { IH_ARCH_SPARC, "sparc", "SPARC", },
93 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
94 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
95 { IH_ARCH_AVR32, "avr32", "AVR32", },
96 { IH_ARCH_NDS32, "nds32", "NDS32", },
100 static const table_entry_t uimage_os[] = {
101 { IH_OS_INVALID, NULL, "Invalid OS", },
102 { IH_OS_LINUX, "linux", "Linux", },
103 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
104 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
106 { IH_OS_NETBSD, "netbsd", "NetBSD", },
107 { IH_OS_OSE, "ose", "Enea OSE", },
108 { IH_OS_RTEMS, "rtems", "RTEMS", },
109 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
110 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
111 { IH_OS_QNX, "qnx", "QNX", },
112 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
114 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
115 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
118 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
119 { IH_OS_DELL, "dell", "Dell", },
120 { IH_OS_ESIX, "esix", "Esix", },
121 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
122 { IH_OS_IRIX, "irix", "Irix", },
123 { IH_OS_NCR, "ncr", "NCR", },
124 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
125 { IH_OS_PSOS, "psos", "pSOS", },
126 { IH_OS_SCO, "sco", "SCO", },
127 { IH_OS_SOLARIS, "solaris", "Solaris", },
128 { IH_OS_SVR4, "svr4", "SVR4", },
133 static const table_entry_t uimage_type[] = {
134 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
135 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
136 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
137 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
138 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
139 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
140 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
141 { IH_TYPE_INVALID, NULL, "Invalid Image", },
142 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
143 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
144 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
145 { IH_TYPE_SCRIPT, "script", "Script", },
146 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
147 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
151 static const table_entry_t uimage_comp[] = {
152 { IH_COMP_NONE, "none", "uncompressed", },
153 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
154 { IH_COMP_GZIP, "gzip", "gzip compressed", },
155 { IH_COMP_LZMA, "lzma", "lzma compressed", },
156 { IH_COMP_LZO, "lzo", "lzo compressed", },
160 uint32_t crc32(uint32_t, const unsigned char *, uint);
161 uint32_t crc32_wd(uint32_t, const unsigned char *, uint, uint);
162 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
163 static void genimg_print_time(time_t timestamp);
166 /*****************************************************************************/
167 /* Legacy format routines */
168 /*****************************************************************************/
169 int image_check_hcrc(const image_header_t *hdr)
172 ulong len = image_get_header_size();
173 image_header_t header;
175 /* Copy header so we can blank CRC field for re-calculation */
176 memmove(&header, (char *)hdr, image_get_header_size());
177 image_set_hcrc(&header, 0);
179 hcrc = crc32(0, (unsigned char *)&header, len);
181 return (hcrc == image_get_hcrc(hdr));
184 int image_check_dcrc(const image_header_t *hdr)
186 ulong data = image_get_data(hdr);
187 ulong len = image_get_data_size(hdr);
188 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
190 return (dcrc == image_get_dcrc(hdr));
194 * image_multi_count - get component (sub-image) count
195 * @hdr: pointer to the header of the multi component image
197 * image_multi_count() returns number of components in a multi
200 * Note: no checking of the image type is done, caller must pass
201 * a valid multi component image.
204 * number of components
206 ulong image_multi_count(const image_header_t *hdr)
211 /* get start of the image payload, which in case of multi
212 * component images that points to a table of component sizes */
213 size = (uint32_t *)image_get_data(hdr);
215 /* count non empty slots */
216 for (i = 0; size[i]; ++i)
223 * image_multi_getimg - get component data address and size
224 * @hdr: pointer to the header of the multi component image
225 * @idx: index of the requested component
226 * @data: pointer to a ulong variable, will hold component data address
227 * @len: pointer to a ulong variable, will hold component size
229 * image_multi_getimg() returns size and data address for the requested
230 * component in a multi component image.
232 * Note: no checking of the image type is done, caller must pass
233 * a valid multi component image.
236 * data address and size of the component, if idx is valid
237 * 0 in data and len, if idx is out of range
239 void image_multi_getimg(const image_header_t *hdr, ulong idx,
240 ulong *data, ulong *len)
244 ulong offset, count, img_data;
246 /* get number of component */
247 count = image_multi_count(hdr);
249 /* get start of the image payload, which in case of multi
250 * component images that points to a table of component sizes */
251 size = (uint32_t *)image_get_data(hdr);
253 /* get address of the proper component data start, which means
254 * skipping sizes table (add 1 for last, null entry) */
255 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
258 *len = uimage_to_cpu(size[idx]);
261 /* go over all indices preceding requested component idx */
262 for (i = 0; i < idx; i++) {
263 /* add up i-th component size, rounding up to 4 bytes */
264 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
267 /* calculate idx-th component data address */
268 *data = img_data + offset;
275 static void image_print_type(const image_header_t *hdr)
277 const char *os, *arch, *type, *comp;
279 os = genimg_get_os_name(image_get_os(hdr));
280 arch = genimg_get_arch_name(image_get_arch(hdr));
281 type = genimg_get_type_name(image_get_type(hdr));
282 comp = genimg_get_comp_name(image_get_comp(hdr));
284 printf("%s %s %s (%s)\n", arch, os, type, comp);
288 * image_print_contents - prints out the contents of the legacy format image
289 * @ptr: pointer to the legacy format image header
290 * @p: pointer to prefix string
292 * image_print_contents() formats a multi line legacy image contents description.
293 * The routine prints out all header fields followed by the size/offset data
294 * for MULTI/SCRIPT images.
297 * no returned results
299 void image_print_contents(const void *ptr)
301 const image_header_t *hdr = (const image_header_t *)ptr;
310 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
311 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
312 printf("%sCreated: ", p);
313 genimg_print_time((time_t)image_get_time(hdr));
315 printf("%sImage Type: ", p);
316 image_print_type(hdr);
317 printf("%sData Size: ", p);
318 genimg_print_size(image_get_data_size(hdr));
319 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
320 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
322 if (image_check_type(hdr, IH_TYPE_MULTI) ||
323 image_check_type(hdr, IH_TYPE_SCRIPT)) {
326 ulong count = image_multi_count(hdr);
328 printf("%sContents:\n", p);
329 for (i = 0; i < count; i++) {
330 image_multi_getimg(hdr, i, &data, &len);
332 printf("%s Image %d: ", p, i);
333 genimg_print_size(len);
335 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
337 * the user may need to know offsets
338 * if planning to do something with
341 printf("%s Offset = 0x%08lx\n", p, data);
350 * image_get_ramdisk - get and verify ramdisk image
351 * @rd_addr: ramdisk image start address
352 * @arch: expected ramdisk architecture
353 * @verify: checksum verification flag
355 * image_get_ramdisk() returns a pointer to the verified ramdisk image
356 * header. Routine receives image start address and expected architecture
357 * flag. Verification done covers data and header integrity and os/type/arch
360 * If dataflash support is enabled routine checks for dataflash addresses
361 * and handles required dataflash reads.
364 * pointer to a ramdisk image header, if image was found and valid
365 * otherwise, return NULL
367 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
370 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
372 if (!image_check_magic(rd_hdr)) {
373 puts("Bad Magic Number\n");
374 show_boot_progress(-10);
378 if (!image_check_hcrc(rd_hdr)) {
379 puts("Bad Header Checksum\n");
380 show_boot_progress(-11);
384 show_boot_progress(10);
385 image_print_contents(rd_hdr);
388 puts(" Verifying Checksum ... ");
389 if (!image_check_dcrc(rd_hdr)) {
390 puts("Bad Data CRC\n");
391 show_boot_progress(-12);
397 show_boot_progress(11);
399 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
400 !image_check_arch(rd_hdr, arch) ||
401 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
402 printf("No Linux %s Ramdisk Image\n",
403 genimg_get_arch_name(arch));
404 show_boot_progress(-13);
410 #endif /* !USE_HOSTCC */
412 /*****************************************************************************/
413 /* Shared dual-format routines */
414 /*****************************************************************************/
416 int getenv_yesno(char *var)
418 char *s = getenv(var);
419 return (s && (*s == 'n')) ? 0 : 1;
422 ulong getenv_bootm_low(void)
424 char *s = getenv("bootm_low");
426 ulong tmp = simple_strtoul(s, NULL, 16);
430 #if defined(CONFIG_SYS_SDRAM_BASE)
431 return CONFIG_SYS_SDRAM_BASE;
432 #elif defined(CONFIG_ARM)
433 return gd->bd->bi_dram[0].start;
439 phys_size_t getenv_bootm_size(void)
442 char *s = getenv("bootm_size");
444 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
447 s = getenv("bootm_low");
449 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
454 #if defined(CONFIG_ARM)
455 return gd->bd->bi_dram[0].size - tmp;
457 return gd->bd->bi_memsize - tmp;
461 phys_size_t getenv_bootm_mapsize(void)
464 char *s = getenv("bootm_mapsize");
466 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
470 #if defined(CONFIG_SYS_BOOTMAPSZ)
471 return CONFIG_SYS_BOOTMAPSZ;
473 return getenv_bootm_size();
477 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
482 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
484 size_t tail = (len > chunksz) ? chunksz : len;
486 memmove(to, from, tail);
491 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
492 memmove(to, from, len);
493 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
495 #endif /* !USE_HOSTCC */
497 void genimg_print_size(uint32_t size)
500 printf("%d Bytes = ", size);
501 print_size(size, "\n");
503 printf("%d Bytes = %.2f kB = %.2f MB\n",
504 size, (double)size / 1.024e3,
505 (double)size / 1.048576e6);
509 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
510 static void genimg_print_time(time_t timestamp)
515 to_tm(timestamp, &tm);
516 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
517 tm.tm_year, tm.tm_mon, tm.tm_mday,
518 tm.tm_hour, tm.tm_min, tm.tm_sec);
520 printf("%s", ctime(×tamp));
523 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
526 * get_table_entry_name - translate entry id to long name
527 * @table: pointer to a translation table for entries of a specific type
528 * @msg: message to be returned when translation fails
529 * @id: entry id to be translated
531 * get_table_entry_name() will go over translation table trying to find
532 * entry that matches given id. If matching entry is found, its long
533 * name is returned to the caller.
536 * long entry name if translation succeeds
539 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
541 for (; table->id >= 0; ++table) {
543 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
546 return table->lname + gd->reloc_off;
552 const char *genimg_get_os_name(uint8_t os)
554 return (get_table_entry_name(uimage_os, "Unknown OS", os));
557 const char *genimg_get_arch_name(uint8_t arch)
559 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
563 const char *genimg_get_type_name(uint8_t type)
565 return (get_table_entry_name(uimage_type, "Unknown Image", type));
568 const char *genimg_get_comp_name(uint8_t comp)
570 return (get_table_entry_name(uimage_comp, "Unknown Compression",
575 * get_table_entry_id - translate short entry name to id
576 * @table: pointer to a translation table for entries of a specific type
577 * @table_name: to be used in case of error
578 * @name: entry short name to be translated
580 * get_table_entry_id() will go over translation table trying to find
581 * entry that matches given short name. If matching entry is found,
582 * its id returned to the caller.
585 * entry id if translation succeeds
588 int get_table_entry_id(const table_entry_t *table,
589 const char *table_name, const char *name)
591 const table_entry_t *t;
595 for (t = table; t->id >= 0; ++t) {
596 if (t->sname && strcasecmp(t->sname, name) == 0)
600 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
601 for (t = table; t->id >= 0; ++t) {
602 if (t->sname == NULL)
604 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
607 fprintf(stderr, "\n");
609 for (t = table; t->id >= 0; ++t) {
610 #ifdef CONFIG_NEEDS_MANUAL_RELOC
611 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
613 if (t->sname && strcmp(t->sname, name) == 0)
617 debug("Invalid %s Type: %s\n", table_name, name);
618 #endif /* USE_HOSTCC */
622 int genimg_get_os_id(const char *name)
624 return (get_table_entry_id(uimage_os, "OS", name));
627 int genimg_get_arch_id(const char *name)
629 return (get_table_entry_id(uimage_arch, "CPU", name));
632 int genimg_get_type_id(const char *name)
634 return (get_table_entry_id(uimage_type, "Image", name));
637 int genimg_get_comp_id(const char *name)
639 return (get_table_entry_id(uimage_comp, "Compression", name));
644 * genimg_get_format - get image format type
645 * @img_addr: image start address
647 * genimg_get_format() checks whether provided address points to a valid
648 * legacy or FIT image.
650 * New uImage format and FDT blob are based on a libfdt. FDT blob
651 * may be passed directly or embedded in a FIT image. In both situations
652 * genimg_get_format() must be able to dectect libfdt header.
655 * image format type or IMAGE_FORMAT_INVALID if no image is present
657 int genimg_get_format(void *img_addr)
659 ulong format = IMAGE_FORMAT_INVALID;
660 const image_header_t *hdr;
661 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
665 hdr = (const image_header_t *)img_addr;
666 if (image_check_magic(hdr))
667 format = IMAGE_FORMAT_LEGACY;
668 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
670 fit_hdr = (char *)img_addr;
671 if (fdt_check_header(fit_hdr) == 0)
672 format = IMAGE_FORMAT_FIT;
680 * genimg_get_image - get image from special storage (if necessary)
681 * @img_addr: image start address
683 * genimg_get_image() checks if provided image start adddress is located
684 * in a dataflash storage. If so, image is moved to a system RAM memory.
687 * image start address after possible relocation from special storage
689 ulong genimg_get_image(ulong img_addr)
691 ulong ram_addr = img_addr;
693 #ifdef CONFIG_HAS_DATAFLASH
694 ulong h_size, d_size;
696 if (addr_dataflash(img_addr)) {
697 /* ger RAM address */
698 ram_addr = CONFIG_SYS_LOAD_ADDR;
700 /* get header size */
701 h_size = image_get_header_size();
702 #if defined(CONFIG_FIT)
703 if (sizeof(struct fdt_header) > h_size)
704 h_size = sizeof(struct fdt_header);
708 debug(" Reading image header from dataflash address "
709 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
711 read_dataflash(img_addr, h_size, (char *)ram_addr);
714 switch (genimg_get_format((void *)ram_addr)) {
715 case IMAGE_FORMAT_LEGACY:
716 d_size = image_get_data_size(
717 (const image_header_t *)ram_addr);
718 debug(" Legacy format image found at 0x%08lx, "
722 #if defined(CONFIG_FIT)
723 case IMAGE_FORMAT_FIT:
724 d_size = fit_get_size((const void *)ram_addr) - h_size;
725 debug(" FIT/FDT format image found at 0x%08lx, "
731 printf(" No valid image found at 0x%08lx\n",
736 /* read in image data */
737 debug(" Reading image remaining data from dataflash address "
738 "%08lx to RAM address %08lx\n", img_addr + h_size,
741 read_dataflash(img_addr + h_size, d_size,
742 (char *)(ram_addr + h_size));
745 #endif /* CONFIG_HAS_DATAFLASH */
751 * fit_has_config - check if there is a valid FIT configuration
752 * @images: pointer to the bootm command headers structure
754 * fit_has_config() checks if there is a FIT configuration in use
755 * (if FTI support is present).
758 * 0, no FIT support or no configuration found
759 * 1, configuration found
761 int genimg_has_config(bootm_headers_t *images)
763 #if defined(CONFIG_FIT)
764 if (images->fit_uname_cfg)
771 * boot_get_ramdisk - main ramdisk handling routine
772 * @argc: command argument count
773 * @argv: command argument list
774 * @images: pointer to the bootm images structure
775 * @arch: expected ramdisk architecture
776 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
777 * @rd_end: pointer to a ulong variable, will hold ramdisk end
779 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
780 * Curently supported are the following ramdisk sources:
781 * - multicomponent kernel/ramdisk image,
782 * - commandline provided address of decicated ramdisk image.
785 * 0, if ramdisk image was found and valid, or skiped
786 * rd_start and rd_end are set to ramdisk start/end addresses if
787 * ramdisk image is found and valid
789 * 1, if ramdisk image is found but corrupted, or invalid
790 * rd_start and rd_end are set to 0 if no ramdisk exists
792 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
793 uint8_t arch, ulong *rd_start, ulong *rd_end)
795 ulong rd_addr, rd_load;
796 ulong rd_data, rd_len;
797 const image_header_t *rd_hdr;
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 show_boot_progress(9);
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 show_boot_progress(120);
914 if (!fit_check_format(fit_hdr)) {
915 puts("Bad FIT ramdisk image format!\n");
916 show_boot_progress(-120);
919 show_boot_progress(121);
921 if (!fit_uname_ramdisk) {
923 * no ramdisk image node unit name, try to get config
924 * node first. If config unit node name is NULL
925 * fit_conf_get_node() will try to find default config node
927 show_boot_progress(122);
928 cfg_noffset = fit_conf_get_node(fit_hdr,
930 if (cfg_noffset < 0) {
931 puts("Could not find configuration "
933 show_boot_progress(-122);
936 fit_uname_config = fdt_get_name(fit_hdr,
938 printf(" Using '%s' configuration\n",
941 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
943 fit_uname_ramdisk = fit_get_name(fit_hdr,
946 /* get ramdisk component image node offset */
947 show_boot_progress(123);
948 rd_noffset = fit_image_get_node(fit_hdr,
951 if (rd_noffset < 0) {
952 puts("Could not find subimage node\n");
953 show_boot_progress(-124);
957 printf(" Trying '%s' ramdisk subimage\n",
960 show_boot_progress(125);
961 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
965 /* get ramdisk image data address and length */
966 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
968 puts("Could not find ramdisk subimage data!\n");
969 show_boot_progress(-127);
972 show_boot_progress(128);
974 rd_data = (ulong)data;
977 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
978 puts("Can't get ramdisk subimage load "
980 show_boot_progress(-129);
983 show_boot_progress(129);
985 images->fit_hdr_rd = fit_hdr;
986 images->fit_uname_rd = fit_uname_ramdisk;
987 images->fit_noffset_rd = rd_noffset;
991 puts("Wrong Ramdisk Image Format\n");
992 rd_data = rd_len = rd_load = 0;
995 } else if (images->legacy_hdr_valid &&
996 image_check_type(&images->legacy_hdr_os_copy,
1000 * Now check if we have a legacy mult-component image,
1001 * get second entry data start address and len.
1003 show_boot_progress(13);
1004 printf("## Loading init Ramdisk from multi component "
1005 "Legacy Image at %08lx ...\n",
1006 (ulong)images->legacy_hdr_os);
1008 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1013 show_boot_progress(14);
1014 rd_len = rd_data = 0;
1018 debug("## No init Ramdisk\n");
1020 *rd_start = rd_data;
1021 *rd_end = rd_data + rd_len;
1023 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1024 *rd_start, *rd_end);
1029 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1031 * boot_ramdisk_high - relocate init ramdisk
1032 * @lmb: pointer to lmb handle, will be used for memory mgmt
1033 * @rd_data: ramdisk data start address
1034 * @rd_len: ramdisk data length
1035 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1036 * start address (after possible relocation)
1037 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1038 * end address (after possible relocation)
1040 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1041 * variable and if requested ramdisk data is moved to a specified location.
1043 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1044 * start/end addresses if ramdisk image start and len were provided,
1045 * otherwise set initrd_start and initrd_end set to zeros.
1051 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1052 ulong *initrd_start, ulong *initrd_end)
1056 int initrd_copy_to_ram = 1;
1058 if ((s = getenv("initrd_high")) != NULL) {
1059 /* a value of "no" or a similar string will act like 0,
1060 * turning the "load high" feature off. This is intentional.
1062 initrd_high = simple_strtoul(s, NULL, 16);
1063 if (initrd_high == ~0)
1064 initrd_copy_to_ram = 0;
1066 /* not set, no restrictions to load high */
1071 #ifdef CONFIG_LOGBUFFER
1072 /* Prevent initrd from overwriting logbuffer */
1073 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1076 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1077 initrd_high, initrd_copy_to_ram);
1080 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1081 debug(" in-place initrd\n");
1082 *initrd_start = rd_data;
1083 *initrd_end = rd_data + rd_len;
1084 lmb_reserve(lmb, rd_data, rd_len);
1087 *initrd_start = (ulong)lmb_alloc_base(lmb,
1088 rd_len, 0x1000, initrd_high);
1090 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1093 if (*initrd_start == 0) {
1094 puts("ramdisk - allocation error\n");
1097 show_boot_progress(12);
1099 *initrd_end = *initrd_start + rd_len;
1100 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
1101 *initrd_start, *initrd_end);
1103 memmove_wd((void *)*initrd_start,
1104 (void *)rd_data, rd_len, CHUNKSZ);
1112 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1113 *initrd_start, *initrd_end);
1120 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1122 #ifdef CONFIG_OF_LIBFDT
1123 static void fdt_error(const char *msg)
1127 puts(" - must RESET the board to recover.\n");
1130 static const image_header_t *image_get_fdt(ulong fdt_addr)
1132 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1134 image_print_contents(fdt_hdr);
1136 puts(" Verifying Checksum ... ");
1137 if (!image_check_hcrc(fdt_hdr)) {
1138 fdt_error("fdt header checksum invalid");
1142 if (!image_check_dcrc(fdt_hdr)) {
1143 fdt_error("fdt checksum invalid");
1148 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1149 fdt_error("uImage is not a fdt");
1152 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1153 fdt_error("uImage is compressed");
1156 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1157 fdt_error("uImage data is not a fdt");
1164 * fit_check_fdt - verify FIT format FDT subimage
1165 * @fit_hdr: pointer to the FIT header
1166 * fdt_noffset: FDT subimage node offset within FIT image
1167 * @verify: data CRC verification flag
1169 * fit_check_fdt() verifies integrity of the FDT subimage and from
1170 * specified FIT image.
1176 #if defined(CONFIG_FIT)
1177 static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
1179 fit_image_print(fit, fdt_noffset, " ");
1182 puts(" Verifying Hash Integrity ... ");
1183 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1184 fdt_error("Bad Data Hash");
1190 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1191 fdt_error("Not a FDT image");
1195 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1196 fdt_error("FDT image is compressed");
1202 #endif /* CONFIG_FIT */
1204 #ifndef CONFIG_SYS_FDT_PAD
1205 #define CONFIG_SYS_FDT_PAD 0x3000
1208 #if defined(CONFIG_OF_LIBFDT)
1210 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1211 * @lmb: pointer to lmb handle, will be used for memory mgmt
1212 * @fdt_blob: pointer to fdt blob base address
1214 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1215 * memreserve regions prevents u-boot from using them to store the initrd
1218 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1220 uint64_t addr, size;
1223 if (fdt_check_header(fdt_blob) != 0)
1226 total = fdt_num_mem_rsv(fdt_blob);
1227 for (i = 0; i < total; i++) {
1228 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1230 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1231 (unsigned long long)addr, (unsigned long long)size);
1232 lmb_reserve(lmb, addr, size);
1237 * boot_relocate_fdt - relocate flat device tree
1238 * @lmb: pointer to lmb handle, will be used for memory mgmt
1239 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1240 * @of_size: pointer to a ulong variable, will hold fdt length
1242 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1243 * relocates the of_flat_tree into that region, even if the fdt is already in
1244 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1247 * of_flat_tree and of_size are set to final (after relocation) values
1253 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1255 void *fdt_blob = *of_flat_tree;
1260 int disable_relocation = 0;
1266 if (fdt_check_header(fdt_blob) != 0) {
1267 fdt_error("image is not a fdt");
1271 /* position on a 4K boundary before the alloc_current */
1272 /* Pad the FDT by a specified amount */
1273 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1275 /* If fdt_high is set use it to select the relocation address */
1276 fdt_high = getenv("fdt_high");
1278 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1280 if (((ulong) desired_addr) == ~0UL) {
1281 /* All ones means use fdt in place */
1282 desired_addr = fdt_blob;
1283 disable_relocation = 1;
1287 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1291 if (desired_addr && of_start != desired_addr) {
1292 puts("Failed using fdt_high value for Device Tree");
1297 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
1301 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1302 getenv_bootm_mapsize()
1303 + getenv_bootm_low());
1306 if (of_start == 0) {
1307 puts("device tree - allocation error\n");
1311 if (disable_relocation) {
1312 /* We assume there is space after the existing fdt to use for padding */
1313 fdt_set_totalsize(of_start, of_len);
1314 printf(" Using Device Tree in place at %p, end %p\n",
1315 of_start, of_start + of_len - 1);
1317 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1318 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1320 printf(" Loading Device Tree to %p, end %p ... ",
1321 of_start, of_start + of_len - 1);
1323 err = fdt_open_into(fdt_blob, of_start, of_len);
1325 fdt_error("fdt move failed");
1331 *of_flat_tree = of_start;
1334 set_working_fdt_addr(*of_flat_tree);
1340 #endif /* CONFIG_OF_LIBFDT */
1343 * boot_get_fdt - main fdt handling routine
1344 * @argc: command argument count
1345 * @argv: command argument list
1346 * @images: pointer to the bootm images structure
1347 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1348 * @of_size: pointer to a ulong variable, will hold fdt length
1350 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1351 * Curently supported are the following ramdisk sources:
1352 * - multicomponent kernel/ramdisk image,
1353 * - commandline provided address of decicated ramdisk image.
1356 * 0, if fdt image was found and valid, or skipped
1357 * of_flat_tree and of_size are set to fdt start address and length if
1358 * fdt image is found and valid
1360 * 1, if fdt image is found but corrupted
1361 * of_flat_tree and of_size are set to 0 if no fdt exists
1363 int boot_get_fdt(int flag, int argc, char * const argv[],
1364 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
1366 const image_header_t *fdt_hdr;
1368 char *fdt_blob = NULL;
1369 ulong image_start, image_end;
1370 ulong load_start, load_end;
1371 #if defined(CONFIG_FIT)
1373 const char *fit_uname_config = NULL;
1374 const char *fit_uname_fdt = NULL;
1382 *of_flat_tree = NULL;
1385 if (argc > 3 || genimg_has_config(images)) {
1386 #if defined(CONFIG_FIT)
1389 * If the FDT blob comes from the FIT image and the
1390 * FIT image address is omitted in the command line
1391 * argument, try to use ramdisk or os FIT image
1392 * address or default load address.
1394 if (images->fit_uname_rd)
1395 default_addr = (ulong)images->fit_hdr_rd;
1396 else if (images->fit_uname_os)
1397 default_addr = (ulong)images->fit_hdr_os;
1399 default_addr = load_addr;
1401 if (fit_parse_conf(argv[3], default_addr,
1402 &fdt_addr, &fit_uname_config)) {
1403 debug("* fdt: config '%s' from image at "
1405 fit_uname_config, fdt_addr);
1406 } else if (fit_parse_subimage(argv[3], default_addr,
1407 &fdt_addr, &fit_uname_fdt)) {
1408 debug("* fdt: subimage '%s' from image at "
1410 fit_uname_fdt, fdt_addr);
1414 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1415 debug("* fdt: cmdline image address = "
1419 #if defined(CONFIG_FIT)
1421 /* use FIT configuration provided in first bootm
1424 fdt_addr = (ulong)images->fit_hdr_os;
1425 fit_uname_config = images->fit_uname_cfg;
1426 debug("* fdt: using config '%s' from image "
1428 fit_uname_config, fdt_addr);
1431 * Check whether configuration has FDT blob defined,
1432 * if not quit silently.
1434 fit_hdr = (void *)fdt_addr;
1435 cfg_noffset = fit_conf_get_node(fit_hdr,
1437 if (cfg_noffset < 0) {
1438 debug("* fdt: no such config\n");
1442 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
1444 if (fdt_noffset < 0) {
1445 debug("* fdt: no fdt in config\n");
1451 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1454 /* copy from dataflash if needed */
1455 fdt_addr = genimg_get_image(fdt_addr);
1458 * Check if there is an FDT image at the
1459 * address provided in the second bootm argument
1460 * check image type, for FIT images get a FIT node.
1462 switch (genimg_get_format((void *)fdt_addr)) {
1463 case IMAGE_FORMAT_LEGACY:
1464 /* verify fdt_addr points to a valid image header */
1465 printf("## Flattened Device Tree from Legacy Image "
1468 fdt_hdr = image_get_fdt(fdt_addr);
1473 * move image data to the load address,
1474 * make sure we don't overwrite initial image
1476 image_start = (ulong)fdt_hdr;
1477 image_end = image_get_image_end(fdt_hdr);
1479 load_start = image_get_load(fdt_hdr);
1480 load_end = load_start + image_get_data_size(fdt_hdr);
1482 if ((load_start < image_end) && (load_end > image_start)) {
1483 fdt_error("fdt overwritten");
1487 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
1488 image_get_data(fdt_hdr), load_start);
1490 memmove((void *)load_start,
1491 (void *)image_get_data(fdt_hdr),
1492 image_get_data_size(fdt_hdr));
1494 fdt_blob = (char *)load_start;
1496 case IMAGE_FORMAT_FIT:
1498 * This case will catch both: new uImage format
1499 * (libfdt based) and raw FDT blob (also libfdt
1502 #if defined(CONFIG_FIT)
1503 /* check FDT blob vs FIT blob */
1504 if (fit_check_format((const void *)fdt_addr)) {
1508 fit_hdr = (void *)fdt_addr;
1509 printf("## Flattened Device Tree from FIT "
1513 if (!fit_uname_fdt) {
1515 * no FDT blob image node unit name,
1516 * try to get config node first. If
1517 * config unit node name is NULL
1518 * fit_conf_get_node() will try to
1519 * find default config node
1521 cfg_noffset = fit_conf_get_node(fit_hdr,
1524 if (cfg_noffset < 0) {
1525 fdt_error("Could not find "
1531 fit_uname_config = fdt_get_name(fit_hdr,
1533 printf(" Using '%s' configuration\n",
1536 fdt_noffset = fit_conf_get_fdt_node(
1539 fit_uname_fdt = fit_get_name(fit_hdr,
1542 /* get FDT component image node offset */
1543 fdt_noffset = fit_image_get_node(
1547 if (fdt_noffset < 0) {
1548 fdt_error("Could not find subimage "
1553 printf(" Trying '%s' FDT blob subimage\n",
1556 if (!fit_check_fdt(fit_hdr, fdt_noffset,
1560 /* get ramdisk image data address and length */
1561 if (fit_image_get_data(fit_hdr, fdt_noffset,
1563 fdt_error("Could not find FDT "
1568 /* verift that image data is a proper FDT blob */
1569 if (fdt_check_header((char *)data) != 0) {
1570 fdt_error("Subimage data is not a FTD");
1575 * move image data to the load address,
1576 * make sure we don't overwrite initial image
1578 image_start = (ulong)fit_hdr;
1579 image_end = fit_get_end(fit_hdr);
1581 if (fit_image_get_load(fit_hdr, fdt_noffset,
1582 &load_start) == 0) {
1583 load_end = load_start + size;
1585 if ((load_start < image_end) &&
1586 (load_end > image_start)) {
1587 fdt_error("FDT overwritten");
1591 printf(" Loading FDT from 0x%08lx "
1596 memmove((void *)load_start,
1597 (void *)data, size);
1599 fdt_blob = (char *)load_start;
1601 fdt_blob = (char *)data;
1604 images->fit_hdr_fdt = fit_hdr;
1605 images->fit_uname_fdt = fit_uname_fdt;
1606 images->fit_noffset_fdt = fdt_noffset;
1614 fdt_blob = (char *)fdt_addr;
1615 debug("* fdt: raw FDT blob\n");
1616 printf("## Flattened Device Tree blob at "
1617 "%08lx\n", (long)fdt_blob);
1621 puts("ERROR: Did not find a cmdline Flattened Device "
1626 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
1628 } else if (images->legacy_hdr_valid &&
1629 image_check_type(&images->legacy_hdr_os_copy,
1632 ulong fdt_data, fdt_len;
1635 * Now check if we have a legacy multi-component image,
1636 * get second entry data start address and len.
1638 printf("## Flattened Device Tree from multi "
1639 "component Image at %08lX\n",
1640 (ulong)images->legacy_hdr_os);
1642 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1646 fdt_blob = (char *)fdt_data;
1647 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
1649 if (fdt_check_header(fdt_blob) != 0) {
1650 fdt_error("image is not a fdt");
1654 if (fdt_totalsize(fdt_blob) != fdt_len) {
1655 fdt_error("fdt size != image size");
1659 debug("## No Flattened Device Tree\n");
1663 debug("## No Flattened Device Tree\n");
1667 *of_flat_tree = fdt_blob;
1668 *of_size = fdt_totalsize(fdt_blob);
1669 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
1670 (ulong)*of_flat_tree, *of_size);
1679 #endif /* CONFIG_OF_LIBFDT */
1681 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1683 * boot_get_cmdline - allocate and initialize kernel cmdline
1684 * @lmb: pointer to lmb handle, will be used for memory mgmt
1685 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1686 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1688 * boot_get_cmdline() allocates space for kernel command line below
1689 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1690 * variable is present its contents is copied to allocated kernel
1697 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1702 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1703 getenv_bootm_mapsize() + getenv_bootm_low());
1705 if (cmdline == NULL)
1708 if ((s = getenv("bootargs")) == NULL)
1713 *cmd_start = (ulong) & cmdline[0];
1714 *cmd_end = *cmd_start + strlen(cmdline);
1716 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1720 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1722 #ifdef CONFIG_SYS_BOOT_GET_KBD
1724 * boot_get_kbd - allocate and initialize kernel copy of board info
1725 * @lmb: pointer to lmb handle, will be used for memory mgmt
1726 * @kbd: double pointer to board info data
1728 * boot_get_kbd() allocates space for kernel copy of board info data below
1729 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1730 * with the current u-boot board info data.
1736 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1738 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1739 getenv_bootm_mapsize() + getenv_bootm_low());
1745 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1747 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1748 do_bdinfo(NULL, 0, 0, NULL);
1753 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1754 #endif /* !USE_HOSTCC */
1756 #if defined(CONFIG_FIT)
1757 /*****************************************************************************/
1758 /* New uImage format routines */
1759 /*****************************************************************************/
1761 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
1762 ulong *addr, const char **name)
1769 sep = strchr(spec, sepc);
1772 *addr = simple_strtoul(spec, NULL, 16);
1782 * fit_parse_conf - parse FIT configuration spec
1783 * @spec: input string, containing configuration spec
1784 * @add_curr: current image address (to be used as a possible default)
1785 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1787 * @conf_name double pointer to a char, will hold pointer to a configuration
1790 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1791 * where <addr> is a FIT image address that contains configuration
1792 * with a <conf> unit name.
1794 * Address part is optional, and if omitted default add_curr will
1798 * 1 if spec is a valid configuration string,
1799 * addr and conf_name are set accordingly
1802 inline int fit_parse_conf(const char *spec, ulong addr_curr,
1803 ulong *addr, const char **conf_name)
1805 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
1809 * fit_parse_subimage - parse FIT subimage spec
1810 * @spec: input string, containing subimage spec
1811 * @add_curr: current image address (to be used as a possible default)
1812 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1814 * @image_name: double pointer to a char, will hold pointer to a subimage name
1816 * fit_parse_subimage() expects subimage spec in the for of
1817 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1818 * subimage with a <subimg> unit name.
1820 * Address part is optional, and if omitted default add_curr will
1824 * 1 if spec is a valid subimage string,
1825 * addr and image_name are set accordingly
1828 inline int fit_parse_subimage(const char *spec, ulong addr_curr,
1829 ulong *addr, const char **image_name)
1831 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
1833 #endif /* !USE_HOSTCC */
1835 static void fit_get_debug(const void *fit, int noffset,
1836 char *prop_name, int err)
1838 debug("Can't get '%s' property from FIT 0x%08lx, "
1839 "node: offset %d, name %s (%s)\n",
1840 prop_name, (ulong)fit, noffset,
1841 fit_get_name(fit, noffset, NULL),
1846 * fit_print_contents - prints out the contents of the FIT format image
1847 * @fit: pointer to the FIT format image header
1848 * @p: pointer to prefix string
1850 * fit_print_contents() formats a multi line FIT image contents description.
1851 * The routine prints out FIT image properties (root node level) follwed by
1852 * the details of each component image.
1855 * no returned results
1857 void fit_print_contents(const void *fit)
1868 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1878 /* Root node properties */
1879 ret = fit_get_desc(fit, 0, &desc);
1880 printf("%sFIT description: ", p);
1882 printf("unavailable\n");
1884 printf("%s\n", desc);
1886 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1887 ret = fit_get_timestamp(fit, 0, ×tamp);
1888 printf("%sCreated: ", p);
1890 printf("unavailable\n");
1892 genimg_print_time(timestamp);
1895 /* Find images parent node offset */
1896 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1897 if (images_noffset < 0) {
1898 printf("Can't find images parent node '%s' (%s)\n",
1899 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1903 /* Process its subnodes, print out component images details */
1904 for (ndepth = 0, count = 0,
1905 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1906 (noffset >= 0) && (ndepth > 0);
1907 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1910 * Direct child node of the images parent node,
1911 * i.e. component image node.
1913 printf("%s Image %u (%s)\n", p, count++,
1914 fit_get_name(fit, noffset, NULL));
1916 fit_image_print(fit, noffset, p);
1920 /* Find configurations parent node offset */
1921 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1922 if (confs_noffset < 0) {
1923 debug("Can't get configurations parent node '%s' (%s)\n",
1924 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1928 /* get default configuration unit name from default property */
1929 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
1931 printf("%s Default Configuration: '%s'\n", p, uname);
1933 /* Process its subnodes, print out configurations details */
1934 for (ndepth = 0, count = 0,
1935 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1936 (noffset >= 0) && (ndepth > 0);
1937 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1940 * Direct child node of the configurations parent node,
1941 * i.e. configuration node.
1943 printf("%s Configuration %u (%s)\n", p, count++,
1944 fit_get_name(fit, noffset, NULL));
1946 fit_conf_print(fit, noffset, p);
1952 * fit_image_print - prints out the FIT component image details
1953 * @fit: pointer to the FIT format image header
1954 * @image_noffset: offset of the component image node
1955 * @p: pointer to prefix string
1957 * fit_image_print() lists all mandatory properies for the processed component
1958 * image. If present, hash nodes are printed out as well. Load
1959 * address for images of type firmware is also printed out. Since the load
1960 * address is not mandatory for firmware images, it will be output as
1961 * "unavailable" when not present.
1964 * no returned results
1966 void fit_image_print(const void *fit, int image_noffset, const char *p)
1969 uint8_t type, arch, os, comp;
1977 /* Mandatory properties */
1978 ret = fit_get_desc(fit, image_noffset, &desc);
1979 printf("%s Description: ", p);
1981 printf("unavailable\n");
1983 printf("%s\n", desc);
1985 fit_image_get_type(fit, image_noffset, &type);
1986 printf("%s Type: %s\n", p, genimg_get_type_name(type));
1988 fit_image_get_comp(fit, image_noffset, &comp);
1989 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
1991 ret = fit_image_get_data(fit, image_noffset, &data, &size);
1994 printf("%s Data Start: ", p);
1996 printf("unavailable\n");
1998 printf("0x%08lx\n", (ulong)data);
2001 printf("%s Data Size: ", p);
2003 printf("unavailable\n");
2005 genimg_print_size(size);
2007 /* Remaining, type dependent properties */
2008 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2009 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2010 (type == IH_TYPE_FLATDT)) {
2011 fit_image_get_arch(fit, image_noffset, &arch);
2012 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
2015 if (type == IH_TYPE_KERNEL) {
2016 fit_image_get_os(fit, image_noffset, &os);
2017 printf("%s OS: %s\n", p, genimg_get_os_name(os));
2020 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2021 (type == IH_TYPE_FIRMWARE)) {
2022 ret = fit_image_get_load(fit, image_noffset, &load);
2023 printf("%s Load Address: ", p);
2025 printf("unavailable\n");
2027 printf("0x%08lx\n", load);
2030 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
2031 fit_image_get_entry(fit, image_noffset, &entry);
2032 printf("%s Entry Point: ", p);
2034 printf("unavailable\n");
2036 printf("0x%08lx\n", entry);
2039 /* Process all hash subnodes of the component image node */
2040 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2041 (noffset >= 0) && (ndepth > 0);
2042 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2044 /* Direct child node of the component image node */
2045 fit_image_print_hash(fit, noffset, p);
2051 * fit_image_print_hash - prints out the hash node details
2052 * @fit: pointer to the FIT format image header
2053 * @noffset: offset of the hash node
2054 * @p: pointer to prefix string
2056 * fit_image_print_hash() lists properies for the processed hash node
2059 * no returned results
2061 void fit_image_print_hash(const void *fit, int noffset, const char *p)
2069 * Check subnode name, must be equal to "hash".
2070 * Multiple hash nodes require unique unit node
2071 * names, e.g. hash@1, hash@2, etc.
2073 if (strncmp(fit_get_name(fit, noffset, NULL),
2075 strlen(FIT_HASH_NODENAME)) != 0)
2078 debug("%s Hash node: '%s'\n", p,
2079 fit_get_name(fit, noffset, NULL));
2081 printf("%s Hash algo: ", p);
2082 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2083 printf("invalid/unsupported\n");
2086 printf("%s\n", algo);
2088 ret = fit_image_hash_get_value(fit, noffset, &value,
2090 printf("%s Hash value: ", p);
2092 printf("unavailable\n");
2094 for (i = 0; i < value_len; i++)
2095 printf("%02x", value[i]);
2099 debug("%s Hash len: %d\n", p, value_len);
2103 * fit_get_desc - get node description property
2104 * @fit: pointer to the FIT format image header
2105 * @noffset: node offset
2106 * @desc: double pointer to the char, will hold pointer to the descrption
2108 * fit_get_desc() reads description property from a given node, if
2109 * description is found pointer to it is returened in third call argument.
2115 int fit_get_desc(const void *fit, int noffset, char **desc)
2119 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
2120 if (*desc == NULL) {
2121 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
2129 * fit_get_timestamp - get node timestamp property
2130 * @fit: pointer to the FIT format image header
2131 * @noffset: node offset
2132 * @timestamp: pointer to the time_t, will hold read timestamp
2134 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2135 * is found and has a correct size its value is retured in third call
2140 * -1, on property read failure
2141 * -2, on wrong timestamp size
2143 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
2148 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
2150 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
2153 if (len != sizeof(uint32_t)) {
2154 debug("FIT timestamp with incorrect size of (%u)\n", len);
2158 *timestamp = uimage_to_cpu(*((uint32_t *)data));
2163 * fit_image_get_node - get node offset for component image of a given unit name
2164 * @fit: pointer to the FIT format image header
2165 * @image_uname: component image node unit name
2167 * fit_image_get_node() finds a component image (withing the '/images'
2168 * node) of a provided unit name. If image is found its node offset is
2169 * returned to the caller.
2172 * image node offset when found (>=0)
2173 * negative number on failure (FDT_ERR_* code)
2175 int fit_image_get_node(const void *fit, const char *image_uname)
2177 int noffset, images_noffset;
2179 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2180 if (images_noffset < 0) {
2181 debug("Can't find images parent node '%s' (%s)\n",
2182 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2183 return images_noffset;
2186 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
2188 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2189 image_uname, fdt_strerror(noffset));
2196 * fit_image_get_os - get os id for a given component image node
2197 * @fit: pointer to the FIT format image header
2198 * @noffset: component image node offset
2199 * @os: pointer to the uint8_t, will hold os numeric id
2201 * fit_image_get_os() finds os property in a given component image node.
2202 * If the property is found, its (string) value is translated to the numeric
2203 * id which is returned to the caller.
2209 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
2214 /* Get OS name from property data */
2215 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
2217 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
2222 /* Translate OS name to id */
2223 *os = genimg_get_os_id(data);
2228 * fit_image_get_arch - get arch id for a given component image node
2229 * @fit: pointer to the FIT format image header
2230 * @noffset: component image node offset
2231 * @arch: pointer to the uint8_t, will hold arch numeric id
2233 * fit_image_get_arch() finds arch 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_arch(const void *fit, int noffset, uint8_t *arch)
2246 /* Get architecture name from property data */
2247 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
2249 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
2254 /* Translate architecture name to id */
2255 *arch = genimg_get_arch_id(data);
2260 * fit_image_get_type - get type id for a given component image node
2261 * @fit: pointer to the FIT format image header
2262 * @noffset: component image node offset
2263 * @type: pointer to the uint8_t, will hold type numeric id
2265 * fit_image_get_type() finds type 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_type(const void *fit, int noffset, uint8_t *type)
2278 /* Get image type name from property data */
2279 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
2281 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
2286 /* Translate image type name to id */
2287 *type = genimg_get_type_id(data);
2292 * fit_image_get_comp - get comp id for a given component image node
2293 * @fit: pointer to the FIT format image header
2294 * @noffset: component image node offset
2295 * @comp: pointer to the uint8_t, will hold comp numeric id
2297 * fit_image_get_comp() finds comp 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_comp(const void *fit, int noffset, uint8_t *comp)
2310 /* Get compression name from property data */
2311 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
2313 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
2318 /* Translate compression name to id */
2319 *comp = genimg_get_comp_id(data);
2324 * fit_image_get_load - get load address property for a given component image node
2325 * @fit: pointer to the FIT format image header
2326 * @noffset: component image node offset
2327 * @load: pointer to the uint32_t, will hold load address
2329 * fit_image_get_load() finds load address property in a given component image node.
2330 * If the property is found, its value is returned to the caller.
2336 int fit_image_get_load(const void *fit, int noffset, ulong *load)
2339 const uint32_t *data;
2341 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
2343 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
2347 *load = uimage_to_cpu(*data);
2352 * fit_image_get_entry - get entry point address property for a given component image node
2353 * @fit: pointer to the FIT format image header
2354 * @noffset: component image node offset
2355 * @entry: pointer to the uint32_t, will hold entry point address
2357 * fit_image_get_entry() finds entry point address property in a given component image node.
2358 * If the property is found, its value is returned to the caller.
2364 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
2367 const uint32_t *data;
2369 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
2371 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
2375 *entry = uimage_to_cpu(*data);
2380 * fit_image_get_data - get data property and its size for a given component image node
2381 * @fit: pointer to the FIT format image header
2382 * @noffset: component image node offset
2383 * @data: double pointer to void, will hold data property's data address
2384 * @size: pointer to size_t, will hold data property's data size
2386 * fit_image_get_data() finds data property in a given component image node.
2387 * If the property is found its data start address and size are returned to
2394 int fit_image_get_data(const void *fit, int noffset,
2395 const void **data, size_t *size)
2399 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
2400 if (*data == NULL) {
2401 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
2411 * fit_image_hash_get_algo - get hash algorithm name
2412 * @fit: pointer to the FIT format image header
2413 * @noffset: hash node offset
2414 * @algo: double pointer to char, will hold pointer to the algorithm name
2416 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2417 * If the property is found its data start address is returned to the caller.
2423 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
2427 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
2428 if (*algo == NULL) {
2429 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
2437 * fit_image_hash_get_value - get hash value and length
2438 * @fit: pointer to the FIT format image header
2439 * @noffset: hash node offset
2440 * @value: double pointer to uint8_t, will hold address of a hash value data
2441 * @value_len: pointer to an int, will hold hash data length
2443 * fit_image_hash_get_value() finds hash value property in a given hash node.
2444 * If the property is found its data start address and size are returned to
2451 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
2456 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
2457 if (*value == NULL) {
2458 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
2468 * fit_set_timestamp - set node timestamp property
2469 * @fit: pointer to the FIT format image header
2470 * @noffset: node offset
2471 * @timestamp: timestamp value to be set
2473 * fit_set_timestamp() attempts to set timestamp property in the requested
2474 * node and returns operation status to the caller.
2478 * -1, on property read failure
2480 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
2485 t = cpu_to_uimage(timestamp);
2486 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2489 printf("Can't set '%s' property for '%s' node (%s)\n",
2490 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2499 * calculate_hash - calculate and return hash for provided input data
2500 * @data: pointer to the input data
2501 * @data_len: data length
2502 * @algo: requested hash algorithm
2503 * @value: pointer to the char, will hold hash value data (caller must
2504 * allocate enough free space)
2505 * value_len: length of the calculated hash
2507 * calculate_hash() computes input data hash according to the requested algorithm.
2508 * Resulting hash value is placed in caller provided 'value' buffer, length
2509 * of the calculated hash is returned via value_len pointer argument.
2513 * -1, when algo is unsupported
2515 static int calculate_hash(const void *data, int data_len, const char *algo,
2516 uint8_t *value, int *value_len)
2518 if (strcmp(algo, "crc32") == 0) {
2519 *((uint32_t *)value) = crc32_wd(0, data, data_len,
2521 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
2523 } else if (strcmp(algo, "sha1") == 0) {
2524 sha1_csum_wd((unsigned char *) data, data_len,
2525 (unsigned char *) value, CHUNKSZ_SHA1);
2527 } else if (strcmp(algo, "md5") == 0) {
2528 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2531 debug("Unsupported hash alogrithm\n");
2539 * fit_set_hashes - process FIT component image nodes and calculate hashes
2540 * @fit: pointer to the FIT format image header
2542 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2543 * Hashes are calculated for all component images which have hash subnodes
2544 * with algorithm property set to one of the supported hash algorithms.
2548 * libfdt error code, on failure
2550 int fit_set_hashes(void *fit)
2557 /* Find images parent node offset */
2558 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2559 if (images_noffset < 0) {
2560 printf("Can't find images parent node '%s' (%s)\n",
2561 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2562 return images_noffset;
2565 /* Process its subnodes, print out component images details */
2566 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
2567 (noffset >= 0) && (ndepth > 0);
2568 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2571 * Direct child node of the images parent node,
2572 * i.e. component image node.
2574 ret = fit_image_set_hashes(fit, noffset);
2584 * fit_image_set_hashes - calculate/set hashes for given component image node
2585 * @fit: pointer to the FIT format image header
2586 * @image_noffset: requested component image node
2588 * fit_image_set_hashes() adds hash values for an component image node. All
2589 * existing hash subnodes are checked, if algorithm property is set to one of
2590 * the supported hash algorithms, hash value is computed and corresponding
2591 * hash node property is set, for example:
2593 * Input component image node structure:
2595 * o image@1 (at image_noffset)
2596 * | - data = [binary data]
2600 * Output component image node structure:
2602 * o image@1 (at image_noffset)
2603 * | - data = [binary data]
2606 * |- value = sha1(data)
2612 int fit_image_set_hashes(void *fit, int image_noffset)
2617 uint8_t value[FIT_MAX_HASH_LEN];
2622 /* Get image data and data length */
2623 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2624 printf("Can't get image data/size\n");
2628 /* Process all hash subnodes of the component image node */
2629 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2630 (noffset >= 0) && (ndepth > 0);
2631 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2633 /* Direct child node of the component image node */
2636 * Check subnode name, must be equal to "hash".
2637 * Multiple hash nodes require unique unit node
2638 * names, e.g. hash@1, hash@2, etc.
2640 if (strncmp(fit_get_name(fit, noffset, NULL),
2642 strlen(FIT_HASH_NODENAME)) != 0) {
2643 /* Not a hash subnode, skip it */
2647 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2648 printf("Can't get hash algo property for "
2649 "'%s' hash node in '%s' image node\n",
2650 fit_get_name(fit, noffset, NULL),
2651 fit_get_name(fit, image_noffset, NULL));
2655 if (calculate_hash(data, size, algo, value,
2657 printf("Unsupported hash algorithm (%s) for "
2658 "'%s' hash node in '%s' image node\n",
2659 algo, fit_get_name(fit, noffset, NULL),
2660 fit_get_name(fit, image_noffset,
2665 if (fit_image_hash_set_value(fit, noffset, value,
2667 printf("Can't set hash value for "
2668 "'%s' hash node in '%s' image node\n",
2669 fit_get_name(fit, noffset, NULL),
2670 fit_get_name(fit, image_noffset, NULL));
2680 * fit_image_hash_set_value - set hash value in requested has node
2681 * @fit: pointer to the FIT format image header
2682 * @noffset: hash node offset
2683 * @value: hash value to be set
2684 * @value_len: hash value length
2686 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2687 * given and returns operation status to the caller.
2693 int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
2698 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
2700 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2701 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2708 #endif /* USE_HOSTCC */
2711 * fit_image_check_hashes - verify data intergity
2712 * @fit: pointer to the FIT format image header
2713 * @image_noffset: component image node offset
2715 * fit_image_check_hashes() goes over component image hash nodes,
2716 * re-calculates each data hash and compares with the value stored in hash
2720 * 1, if all hashes are valid
2721 * 0, otherwise (or on error)
2723 int fit_image_check_hashes(const void *fit, int image_noffset)
2730 uint8_t value[FIT_MAX_HASH_LEN];
2736 /* Get image data and data length */
2737 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2738 printf("Can't get image data/size\n");
2742 /* Process all hash subnodes of the component image node */
2743 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2744 (noffset >= 0) && (ndepth > 0);
2745 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2747 /* Direct child node of the component image node */
2750 * Check subnode name, must be equal to "hash".
2751 * Multiple hash nodes require unique unit node
2752 * names, e.g. hash@1, hash@2, etc.
2754 if (strncmp(fit_get_name(fit, noffset, NULL),
2756 strlen(FIT_HASH_NODENAME)) != 0)
2759 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2760 err_msg = " error!\nCan't get hash algo "
2766 if (fit_image_hash_get_value(fit, noffset, &fit_value,
2768 err_msg = " error!\nCan't get hash value "
2773 if (calculate_hash(data, size, algo, value,
2775 err_msg = " error!\n"
2776 "Unsupported hash algorithm";
2780 if (value_len != fit_value_len) {
2781 err_msg = " error !\nBad hash value len";
2783 } else if (memcmp(value, fit_value, value_len) != 0) {
2784 err_msg = " error!\nBad hash value";
2794 printf("%s for '%s' hash node in '%s' image node\n",
2795 err_msg, fit_get_name(fit, noffset, NULL),
2796 fit_get_name(fit, image_noffset, NULL));
2801 * fit_all_image_check_hashes - verify data intergity for all images
2802 * @fit: pointer to the FIT format image header
2804 * fit_all_image_check_hashes() goes over all images in the FIT and
2805 * for every images checks if all it's hashes are valid.
2808 * 1, if all hashes of all images are valid
2809 * 0, otherwise (or on error)
2811 int fit_all_image_check_hashes(const void *fit)
2818 /* Find images parent node offset */
2819 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2820 if (images_noffset < 0) {
2821 printf("Can't find images parent node '%s' (%s)\n",
2822 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2826 /* Process all image subnodes, check hashes for each */
2827 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
2829 for (ndepth = 0, count = 0,
2830 noffset = fdt_next_node(fit, images_noffset, &ndepth);
2831 (noffset >= 0) && (ndepth > 0);
2832 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2835 * Direct child node of the images parent node,
2836 * i.e. component image node.
2838 printf(" Hash(es) for Image %u (%s): ", count++,
2839 fit_get_name(fit, noffset, NULL));
2841 if (!fit_image_check_hashes(fit, noffset))
2850 * fit_image_check_os - check whether image node is of a given os type
2851 * @fit: pointer to the FIT format image header
2852 * @noffset: component image node offset
2853 * @os: requested image os
2855 * fit_image_check_os() reads image os property and compares its numeric
2856 * id with the requested os. Comparison result is returned to the caller.
2859 * 1 if image is of given os type
2860 * 0 otherwise (or on error)
2862 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
2866 if (fit_image_get_os(fit, noffset, &image_os))
2868 return (os == image_os);
2872 * fit_image_check_arch - check whether image node is of a given arch
2873 * @fit: pointer to the FIT format image header
2874 * @noffset: component image node offset
2875 * @arch: requested imagearch
2877 * fit_image_check_arch() reads image arch property and compares its numeric
2878 * id with the requested arch. Comparison result is returned to the caller.
2881 * 1 if image is of given arch
2882 * 0 otherwise (or on error)
2884 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
2888 if (fit_image_get_arch(fit, noffset, &image_arch))
2890 return (arch == image_arch);
2894 * fit_image_check_type - check whether image node is of a given type
2895 * @fit: pointer to the FIT format image header
2896 * @noffset: component image node offset
2897 * @type: requested image type
2899 * fit_image_check_type() reads image type property and compares its numeric
2900 * id with the requested type. Comparison result is returned to the caller.
2903 * 1 if image is of given type
2904 * 0 otherwise (or on error)
2906 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
2910 if (fit_image_get_type(fit, noffset, &image_type))
2912 return (type == image_type);
2916 * fit_image_check_comp - check whether image node uses given compression
2917 * @fit: pointer to the FIT format image header
2918 * @noffset: component image node offset
2919 * @comp: requested image compression type
2921 * fit_image_check_comp() reads image compression property and compares its
2922 * numeric id with the requested compression type. Comparison result is
2923 * returned to the caller.
2926 * 1 if image uses requested compression
2927 * 0 otherwise (or on error)
2929 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
2933 if (fit_image_get_comp(fit, noffset, &image_comp))
2935 return (comp == image_comp);
2939 * fit_check_format - sanity check FIT image format
2940 * @fit: pointer to the FIT format image header
2942 * fit_check_format() runs a basic sanity FIT image verification.
2943 * Routine checks for mandatory properties, nodes, etc.
2949 int fit_check_format(const void *fit)
2951 /* mandatory / node 'description' property */
2952 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2953 debug("Wrong FIT format: no description\n");
2957 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2958 /* mandatory / node 'timestamp' property */
2959 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2960 debug("Wrong FIT format: no timestamp\n");
2965 /* mandatory subimages parent '/images' node */
2966 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
2967 debug("Wrong FIT format: no images parent node\n");
2975 * fit_conf_get_node - get node offset for configuration of a given unit name
2976 * @fit: pointer to the FIT format image header
2977 * @conf_uname: configuration node unit name
2979 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2980 * parant node) of a provided unit name. If configuration is found its node offset
2981 * is returned to the caller.
2983 * When NULL is provided in second argument fit_conf_get_node() will search
2984 * for a default configuration node instead. Default configuration node unit name
2985 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2988 * configuration node offset when found (>=0)
2989 * negative number on failure (FDT_ERR_* code)
2991 int fit_conf_get_node(const void *fit, const char *conf_uname)
2993 int noffset, confs_noffset;
2996 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
2997 if (confs_noffset < 0) {
2998 debug("Can't find configurations parent node '%s' (%s)\n",
2999 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
3000 return confs_noffset;
3003 if (conf_uname == NULL) {
3004 /* get configuration unit name from the default property */
3005 debug("No configuration specified, trying default...\n");
3006 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3007 FIT_DEFAULT_PROP, &len);
3008 if (conf_uname == NULL) {
3009 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3013 debug("Found default configuration: '%s'\n", conf_uname);
3016 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
3018 debug("Can't get node offset for configuration unit name: "
3020 conf_uname, fdt_strerror(noffset));
3026 static int __fit_conf_get_prop_node(const void *fit, int noffset,
3027 const char *prop_name)
3032 /* get kernel image unit name from configuration kernel property */
3033 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
3037 return fit_image_get_node(fit, uname);
3041 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3042 * a given configuration
3043 * @fit: pointer to the FIT format image header
3044 * @noffset: configuration node offset
3046 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3047 * configuration FIT_KERNEL_PROP property and translates it to the node
3051 * image node offset when found (>=0)
3052 * negative number on failure (FDT_ERR_* code)
3054 int fit_conf_get_kernel_node(const void *fit, int noffset)
3056 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
3060 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3061 * a given configuration
3062 * @fit: pointer to the FIT format image header
3063 * @noffset: configuration node offset
3065 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3066 * configuration FIT_KERNEL_PROP property and translates it to the node
3070 * image node offset when found (>=0)
3071 * negative number on failure (FDT_ERR_* code)
3073 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
3075 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
3079 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3080 * a given configuration
3081 * @fit: pointer to the FIT format image header
3082 * @noffset: configuration node offset
3084 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3085 * configuration FIT_KERNEL_PROP property and translates it to the node
3089 * image node offset when found (>=0)
3090 * negative number on failure (FDT_ERR_* code)
3092 int fit_conf_get_fdt_node(const void *fit, int noffset)
3094 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
3098 * fit_conf_print - prints out the FIT configuration details
3099 * @fit: pointer to the FIT format image header
3100 * @noffset: offset of the configuration node
3101 * @p: pointer to prefix string
3103 * fit_conf_print() lists all mandatory properies for the processed
3104 * configuration node.
3107 * no returned results
3109 void fit_conf_print(const void *fit, int noffset, const char *p)
3115 /* Mandatory properties */
3116 ret = fit_get_desc(fit, noffset, &desc);
3117 printf("%s Description: ", p);
3119 printf("unavailable\n");
3121 printf("%s\n", desc);
3123 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3124 printf("%s Kernel: ", p);
3126 printf("unavailable\n");
3128 printf("%s\n", uname);
3130 /* Optional properties */
3131 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
3133 printf("%s Init Ramdisk: %s\n", p, uname);
3135 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
3137 printf("%s FDT: %s\n", p, uname);
3141 * fit_check_ramdisk - verify FIT format ramdisk subimage
3142 * @fit_hdr: pointer to the FIT ramdisk header
3143 * @rd_noffset: ramdisk subimage node offset within FIT image
3144 * @arch: requested ramdisk image architecture type
3145 * @verify: data CRC verification flag
3147 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3148 * specified FIT image.
3155 static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3158 fit_image_print(fit, rd_noffset, " ");
3161 puts(" Verifying Hash Integrity ... ");
3162 if (!fit_image_check_hashes(fit, rd_noffset)) {
3163 puts("Bad Data Hash\n");
3164 show_boot_progress(-125);
3170 show_boot_progress(126);
3171 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3172 !fit_image_check_arch(fit, rd_noffset, arch) ||
3173 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3174 printf("No Linux %s Ramdisk Image\n",
3175 genimg_get_arch_name(arch));
3176 show_boot_progress(-126);
3180 show_boot_progress(127);
3183 #endif /* USE_HOSTCC */
3184 #endif /* CONFIG_FIT */