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_RAMDISK
, "ramdisk", "RAMDisk Image", },
147 { IH_TYPE_SCRIPT
, "script", "Script", },
148 { IH_TYPE_STANDALONE
, "standalone", "Standalone Program", },
149 { IH_TYPE_UBLIMAGE
, "ublimage", "Davinci UBL image",},
153 static const table_entry_t uimage_comp
[] = {
154 { IH_COMP_NONE
, "none", "uncompressed", },
155 { IH_COMP_BZIP2
, "bzip2", "bzip2 compressed", },
156 { IH_COMP_GZIP
, "gzip", "gzip compressed", },
157 { IH_COMP_LZMA
, "lzma", "lzma compressed", },
158 { IH_COMP_LZO
, "lzo", "lzo compressed", },
162 uint32_t crc32(uint32_t, const unsigned char *, uint
);
163 uint32_t crc32_wd(uint32_t, const unsigned char *, uint
, uint
);
164 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
165 static void genimg_print_time(time_t timestamp
);
168 /*****************************************************************************/
169 /* Legacy format routines */
170 /*****************************************************************************/
171 int image_check_hcrc(const image_header_t
*hdr
)
174 ulong len
= image_get_header_size();
175 image_header_t header
;
177 /* Copy header so we can blank CRC field for re-calculation */
178 memmove(&header
, (char *)hdr
, image_get_header_size());
179 image_set_hcrc(&header
, 0);
181 hcrc
= crc32(0, (unsigned char *)&header
, len
);
183 return (hcrc
== image_get_hcrc(hdr
));
186 int image_check_dcrc(const image_header_t
*hdr
)
188 ulong data
= image_get_data(hdr
);
189 ulong len
= image_get_data_size(hdr
);
190 ulong dcrc
= crc32_wd(0, (unsigned char *)data
, len
, CHUNKSZ_CRC32
);
192 return (dcrc
== image_get_dcrc(hdr
));
196 * image_multi_count - get component (sub-image) count
197 * @hdr: pointer to the header of the multi component image
199 * image_multi_count() returns number of components in a multi
202 * Note: no checking of the image type is done, caller must pass
203 * a valid multi component image.
206 * number of components
208 ulong
image_multi_count(const image_header_t
*hdr
)
213 /* get start of the image payload, which in case of multi
214 * component images that points to a table of component sizes */
215 size
= (uint32_t *)image_get_data(hdr
);
217 /* count non empty slots */
218 for (i
= 0; size
[i
]; ++i
)
225 * image_multi_getimg - get component data address and size
226 * @hdr: pointer to the header of the multi component image
227 * @idx: index of the requested component
228 * @data: pointer to a ulong variable, will hold component data address
229 * @len: pointer to a ulong variable, will hold component size
231 * image_multi_getimg() returns size and data address for the requested
232 * component in a multi component image.
234 * Note: no checking of the image type is done, caller must pass
235 * a valid multi component image.
238 * data address and size of the component, if idx is valid
239 * 0 in data and len, if idx is out of range
241 void image_multi_getimg(const image_header_t
*hdr
, ulong idx
,
242 ulong
*data
, ulong
*len
)
246 ulong offset
, count
, img_data
;
248 /* get number of component */
249 count
= image_multi_count(hdr
);
251 /* get start of the image payload, which in case of multi
252 * component images that points to a table of component sizes */
253 size
= (uint32_t *)image_get_data(hdr
);
255 /* get address of the proper component data start, which means
256 * skipping sizes table (add 1 for last, null entry) */
257 img_data
= image_get_data(hdr
) + (count
+ 1) * sizeof(uint32_t);
260 *len
= uimage_to_cpu(size
[idx
]);
263 /* go over all indices preceding requested component idx */
264 for (i
= 0; i
< idx
; i
++) {
265 /* add up i-th component size, rounding up to 4 bytes */
266 offset
+= (uimage_to_cpu(size
[i
]) + 3) & ~3 ;
269 /* calculate idx-th component data address */
270 *data
= img_data
+ offset
;
277 static void image_print_type(const image_header_t
*hdr
)
279 const char *os
, *arch
, *type
, *comp
;
281 os
= genimg_get_os_name(image_get_os(hdr
));
282 arch
= genimg_get_arch_name(image_get_arch(hdr
));
283 type
= genimg_get_type_name(image_get_type(hdr
));
284 comp
= genimg_get_comp_name(image_get_comp(hdr
));
286 printf("%s %s %s (%s)\n", arch
, os
, type
, comp
);
290 * image_print_contents - prints out the contents of the legacy format image
291 * @ptr: pointer to the legacy format image header
292 * @p: pointer to prefix string
294 * image_print_contents() formats a multi line legacy image contents description.
295 * The routine prints out all header fields followed by the size/offset data
296 * for MULTI/SCRIPT images.
299 * no returned results
301 void image_print_contents(const void *ptr
)
303 const image_header_t
*hdr
= (const image_header_t
*)ptr
;
312 printf("%sImage Name: %.*s\n", p
, IH_NMLEN
, image_get_name(hdr
));
313 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
314 printf("%sCreated: ", p
);
315 genimg_print_time((time_t)image_get_time(hdr
));
317 printf("%sImage Type: ", p
);
318 image_print_type(hdr
);
319 printf("%sData Size: ", p
);
320 genimg_print_size(image_get_data_size(hdr
));
321 printf("%sLoad Address: %08x\n", p
, image_get_load(hdr
));
322 printf("%sEntry Point: %08x\n", p
, image_get_ep(hdr
));
324 if (image_check_type(hdr
, IH_TYPE_MULTI
) ||
325 image_check_type(hdr
, IH_TYPE_SCRIPT
)) {
328 ulong count
= image_multi_count(hdr
);
330 printf("%sContents:\n", p
);
331 for (i
= 0; i
< count
; i
++) {
332 image_multi_getimg(hdr
, i
, &data
, &len
);
334 printf("%s Image %d: ", p
, i
);
335 genimg_print_size(len
);
337 if (image_check_type(hdr
, IH_TYPE_SCRIPT
) && i
> 0) {
339 * the user may need to know offsets
340 * if planning to do something with
343 printf("%s Offset = 0x%08lx\n", p
, data
);
352 * image_get_ramdisk - get and verify ramdisk image
353 * @rd_addr: ramdisk image start address
354 * @arch: expected ramdisk architecture
355 * @verify: checksum verification flag
357 * image_get_ramdisk() returns a pointer to the verified ramdisk image
358 * header. Routine receives image start address and expected architecture
359 * flag. Verification done covers data and header integrity and os/type/arch
362 * If dataflash support is enabled routine checks for dataflash addresses
363 * and handles required dataflash reads.
366 * pointer to a ramdisk image header, if image was found and valid
367 * otherwise, return NULL
369 static const image_header_t
*image_get_ramdisk(ulong rd_addr
, uint8_t arch
,
372 const image_header_t
*rd_hdr
= (const image_header_t
*)rd_addr
;
374 if (!image_check_magic(rd_hdr
)) {
375 puts("Bad Magic Number\n");
376 bootstage_error(BOOTSTAGE_ID_RD_MAGIC
);
380 if (!image_check_hcrc(rd_hdr
)) {
381 puts("Bad Header Checksum\n");
382 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM
);
386 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC
);
387 image_print_contents(rd_hdr
);
390 puts(" Verifying Checksum ... ");
391 if (!image_check_dcrc(rd_hdr
)) {
392 puts("Bad Data CRC\n");
393 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM
);
399 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM
);
401 if (!image_check_os(rd_hdr
, IH_OS_LINUX
) ||
402 !image_check_arch(rd_hdr
, arch
) ||
403 !image_check_type(rd_hdr
, IH_TYPE_RAMDISK
)) {
404 printf("No Linux %s Ramdisk Image\n",
405 genimg_get_arch_name(arch
));
406 bootstage_error(BOOTSTAGE_ID_RAMDISK
);
412 #endif /* !USE_HOSTCC */
414 /*****************************************************************************/
415 /* Shared dual-format routines */
416 /*****************************************************************************/
418 int getenv_yesno(char *var
)
420 char *s
= getenv(var
);
421 return (s
&& (*s
== 'n')) ? 0 : 1;
424 ulong
getenv_bootm_low(void)
426 char *s
= getenv("bootm_low");
428 ulong tmp
= simple_strtoul(s
, NULL
, 16);
432 #if defined(CONFIG_SYS_SDRAM_BASE)
433 return CONFIG_SYS_SDRAM_BASE
;
434 #elif defined(CONFIG_ARM)
435 return gd
->bd
->bi_dram
[0].start
;
441 phys_size_t
getenv_bootm_size(void)
444 char *s
= getenv("bootm_size");
446 tmp
= (phys_size_t
)simple_strtoull(s
, NULL
, 16);
449 s
= getenv("bootm_low");
451 tmp
= (phys_size_t
)simple_strtoull(s
, NULL
, 16);
456 #if defined(CONFIG_ARM)
457 return gd
->bd
->bi_dram
[0].size
- tmp
;
459 return gd
->bd
->bi_memsize
- tmp
;
463 phys_size_t
getenv_bootm_mapsize(void)
466 char *s
= getenv("bootm_mapsize");
468 tmp
= (phys_size_t
)simple_strtoull(s
, NULL
, 16);
472 #if defined(CONFIG_SYS_BOOTMAPSZ)
473 return CONFIG_SYS_BOOTMAPSZ
;
475 return getenv_bootm_size();
479 void memmove_wd(void *to
, void *from
, size_t len
, ulong chunksz
)
484 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
486 size_t tail
= (len
> chunksz
) ? chunksz
: len
;
488 memmove(to
, from
, tail
);
493 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
494 memmove(to
, from
, len
);
495 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
497 #endif /* !USE_HOSTCC */
499 void genimg_print_size(uint32_t size
)
502 printf("%d Bytes = ", size
);
503 print_size(size
, "\n");
505 printf("%d Bytes = %.2f kB = %.2f MB\n",
506 size
, (double)size
/ 1.024e3
,
507 (double)size
/ 1.048576e6
);
511 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
512 static void genimg_print_time(time_t timestamp
)
517 to_tm(timestamp
, &tm
);
518 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
519 tm
.tm_year
, tm
.tm_mon
, tm
.tm_mday
,
520 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
522 printf("%s", ctime(×tamp
));
525 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
528 * get_table_entry_name - translate entry id to long name
529 * @table: pointer to a translation table for entries of a specific type
530 * @msg: message to be returned when translation fails
531 * @id: entry id to be translated
533 * get_table_entry_name() will go over translation table trying to find
534 * entry that matches given id. If matching entry is found, its long
535 * name is returned to the caller.
538 * long entry name if translation succeeds
541 char *get_table_entry_name(const table_entry_t
*table
, char *msg
, int id
)
543 for (; table
->id
>= 0; ++table
) {
545 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
548 return table
->lname
+ gd
->reloc_off
;
554 const char *genimg_get_os_name(uint8_t os
)
556 return (get_table_entry_name(uimage_os
, "Unknown OS", os
));
559 const char *genimg_get_arch_name(uint8_t arch
)
561 return (get_table_entry_name(uimage_arch
, "Unknown Architecture",
565 const char *genimg_get_type_name(uint8_t type
)
567 return (get_table_entry_name(uimage_type
, "Unknown Image", type
));
570 const char *genimg_get_comp_name(uint8_t comp
)
572 return (get_table_entry_name(uimage_comp
, "Unknown Compression",
577 * get_table_entry_id - translate short entry name to id
578 * @table: pointer to a translation table for entries of a specific type
579 * @table_name: to be used in case of error
580 * @name: entry short name to be translated
582 * get_table_entry_id() will go over translation table trying to find
583 * entry that matches given short name. If matching entry is found,
584 * its id returned to the caller.
587 * entry id if translation succeeds
590 int get_table_entry_id(const table_entry_t
*table
,
591 const char *table_name
, const char *name
)
593 const table_entry_t
*t
;
597 for (t
= table
; t
->id
>= 0; ++t
) {
598 if (t
->sname
&& strcasecmp(t
->sname
, name
) == 0)
602 fprintf(stderr
, "\nInvalid %s Type - valid names are", table_name
);
603 for (t
= table
; t
->id
>= 0; ++t
) {
604 if (t
->sname
== NULL
)
606 fprintf(stderr
, "%c %s", (first
) ? ':' : ',', t
->sname
);
609 fprintf(stderr
, "\n");
611 for (t
= table
; t
->id
>= 0; ++t
) {
612 #ifdef CONFIG_NEEDS_MANUAL_RELOC
613 if (t
->sname
&& strcmp(t
->sname
+ gd
->reloc_off
, name
) == 0)
615 if (t
->sname
&& strcmp(t
->sname
, name
) == 0)
619 debug("Invalid %s Type: %s\n", table_name
, name
);
620 #endif /* USE_HOSTCC */
624 int genimg_get_os_id(const char *name
)
626 return (get_table_entry_id(uimage_os
, "OS", name
));
629 int genimg_get_arch_id(const char *name
)
631 return (get_table_entry_id(uimage_arch
, "CPU", name
));
634 int genimg_get_type_id(const char *name
)
636 return (get_table_entry_id(uimage_type
, "Image", name
));
639 int genimg_get_comp_id(const char *name
)
641 return (get_table_entry_id(uimage_comp
, "Compression", name
));
646 * genimg_get_format - get image format type
647 * @img_addr: image start address
649 * genimg_get_format() checks whether provided address points to a valid
650 * legacy or FIT image.
652 * New uImage format and FDT blob are based on a libfdt. FDT blob
653 * may be passed directly or embedded in a FIT image. In both situations
654 * genimg_get_format() must be able to dectect libfdt header.
657 * image format type or IMAGE_FORMAT_INVALID if no image is present
659 int genimg_get_format(void *img_addr
)
661 ulong format
= IMAGE_FORMAT_INVALID
;
662 const image_header_t
*hdr
;
663 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
667 hdr
= (const image_header_t
*)img_addr
;
668 if (image_check_magic(hdr
))
669 format
= IMAGE_FORMAT_LEGACY
;
670 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
672 fit_hdr
= (char *)img_addr
;
673 if (fdt_check_header(fit_hdr
) == 0)
674 format
= IMAGE_FORMAT_FIT
;
682 * genimg_get_image - get image from special storage (if necessary)
683 * @img_addr: image start address
685 * genimg_get_image() checks if provided image start adddress is located
686 * in a dataflash storage. If so, image is moved to a system RAM memory.
689 * image start address after possible relocation from special storage
691 ulong
genimg_get_image(ulong img_addr
)
693 ulong ram_addr
= img_addr
;
695 #ifdef CONFIG_HAS_DATAFLASH
696 ulong h_size
, d_size
;
698 if (addr_dataflash(img_addr
)) {
699 /* ger RAM address */
700 ram_addr
= CONFIG_SYS_LOAD_ADDR
;
702 /* get header size */
703 h_size
= image_get_header_size();
704 #if defined(CONFIG_FIT)
705 if (sizeof(struct fdt_header
) > h_size
)
706 h_size
= sizeof(struct fdt_header
);
710 debug(" Reading image header from dataflash address "
711 "%08lx to RAM address %08lx\n", img_addr
, ram_addr
);
713 read_dataflash(img_addr
, h_size
, (char *)ram_addr
);
716 switch (genimg_get_format((void *)ram_addr
)) {
717 case IMAGE_FORMAT_LEGACY
:
718 d_size
= image_get_data_size(
719 (const image_header_t
*)ram_addr
);
720 debug(" Legacy format image found at 0x%08lx, "
724 #if defined(CONFIG_FIT)
725 case IMAGE_FORMAT_FIT
:
726 d_size
= fit_get_size((const void *)ram_addr
) - h_size
;
727 debug(" FIT/FDT format image found at 0x%08lx, "
733 printf(" No valid image found at 0x%08lx\n",
738 /* read in image data */
739 debug(" Reading image remaining data from dataflash address "
740 "%08lx to RAM address %08lx\n", img_addr
+ h_size
,
743 read_dataflash(img_addr
+ h_size
, d_size
,
744 (char *)(ram_addr
+ h_size
));
747 #endif /* CONFIG_HAS_DATAFLASH */
753 * fit_has_config - check if there is a valid FIT configuration
754 * @images: pointer to the bootm command headers structure
756 * fit_has_config() checks if there is a FIT configuration in use
757 * (if FTI support is present).
760 * 0, no FIT support or no configuration found
761 * 1, configuration found
763 int genimg_has_config(bootm_headers_t
*images
)
765 #if defined(CONFIG_FIT)
766 if (images
->fit_uname_cfg
)
773 * boot_get_ramdisk - main ramdisk handling routine
774 * @argc: command argument count
775 * @argv: command argument list
776 * @images: pointer to the bootm images structure
777 * @arch: expected ramdisk architecture
778 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
779 * @rd_end: pointer to a ulong variable, will hold ramdisk end
781 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
782 * Curently supported are the following ramdisk sources:
783 * - multicomponent kernel/ramdisk image,
784 * - commandline provided address of decicated ramdisk image.
787 * 0, if ramdisk image was found and valid, or skiped
788 * rd_start and rd_end are set to ramdisk start/end addresses if
789 * ramdisk image is found and valid
791 * 1, if ramdisk image is found but corrupted, or invalid
792 * rd_start and rd_end are set to 0 if no ramdisk exists
794 int boot_get_ramdisk(int argc
, char * const argv
[], bootm_headers_t
*images
,
795 uint8_t arch
, ulong
*rd_start
, ulong
*rd_end
)
797 ulong rd_addr
, rd_load
;
798 ulong rd_data
, rd_len
;
799 const image_header_t
*rd_hdr
;
800 #if defined(CONFIG_FIT)
802 const char *fit_uname_config
= NULL
;
803 const char *fit_uname_ramdisk
= NULL
;
815 * Look for a '-' which indicates to ignore the
818 if ((argc
>= 3) && (strcmp(argv
[2], "-") == 0)) {
819 debug("## Skipping init Ramdisk\n");
820 rd_len
= rd_data
= 0;
821 } else if (argc
>= 3 || genimg_has_config(images
)) {
822 #if defined(CONFIG_FIT)
825 * If the init ramdisk comes from the FIT image and
826 * the FIT image address is omitted in the command
827 * line argument, try to use os FIT image address or
828 * default load address.
830 if (images
->fit_uname_os
)
831 default_addr
= (ulong
)images
->fit_hdr_os
;
833 default_addr
= load_addr
;
835 if (fit_parse_conf(argv
[2], default_addr
,
836 &rd_addr
, &fit_uname_config
)) {
837 debug("* ramdisk: config '%s' from image at "
839 fit_uname_config
, rd_addr
);
840 } else if (fit_parse_subimage(argv
[2], default_addr
,
841 &rd_addr
, &fit_uname_ramdisk
)) {
842 debug("* ramdisk: subimage '%s' from image at "
844 fit_uname_ramdisk
, rd_addr
);
848 rd_addr
= simple_strtoul(argv
[2], NULL
, 16);
849 debug("* ramdisk: cmdline image address = "
853 #if defined(CONFIG_FIT)
855 /* use FIT configuration provided in first bootm
858 rd_addr
= (ulong
)images
->fit_hdr_os
;
859 fit_uname_config
= images
->fit_uname_cfg
;
860 debug("* ramdisk: using config '%s' from image "
862 fit_uname_config
, rd_addr
);
865 * Check whether configuration has ramdisk defined,
866 * if not, don't try to use it, quit silently.
868 fit_hdr
= (void *)rd_addr
;
869 cfg_noffset
= fit_conf_get_node(fit_hdr
,
871 if (cfg_noffset
< 0) {
872 debug("* ramdisk: no such config\n");
876 rd_noffset
= fit_conf_get_ramdisk_node(fit_hdr
,
878 if (rd_noffset
< 0) {
879 debug("* ramdisk: no ramdisk in config\n");
885 /* copy from dataflash if needed */
886 rd_addr
= genimg_get_image(rd_addr
);
889 * Check if there is an initrd image at the
890 * address provided in the second bootm argument
891 * check image type, for FIT images get FIT node.
893 switch (genimg_get_format((void *)rd_addr
)) {
894 case IMAGE_FORMAT_LEGACY
:
895 printf("## Loading init Ramdisk from Legacy "
896 "Image at %08lx ...\n", rd_addr
);
898 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK
);
899 rd_hdr
= image_get_ramdisk(rd_addr
, arch
,
905 rd_data
= image_get_data(rd_hdr
);
906 rd_len
= image_get_data_size(rd_hdr
);
907 rd_load
= image_get_load(rd_hdr
);
909 #if defined(CONFIG_FIT)
910 case IMAGE_FORMAT_FIT
:
911 fit_hdr
= (void *)rd_addr
;
912 printf("## Loading init Ramdisk from FIT "
913 "Image at %08lx ...\n", rd_addr
);
915 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT
);
916 if (!fit_check_format(fit_hdr
)) {
917 puts("Bad FIT ramdisk image format!\n");
919 BOOTSTAGE_ID_FIT_RD_FORMAT
);
922 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK
);
924 if (!fit_uname_ramdisk
) {
926 * no ramdisk image node unit name, try to get config
927 * node first. If config unit node name is NULL
928 * fit_conf_get_node() will try to find default config node
931 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME
);
932 cfg_noffset
= fit_conf_get_node(fit_hdr
,
934 if (cfg_noffset
< 0) {
935 puts("Could not find configuration "
938 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME
);
941 fit_uname_config
= fdt_get_name(fit_hdr
,
943 printf(" Using '%s' configuration\n",
946 rd_noffset
= fit_conf_get_ramdisk_node(fit_hdr
,
948 fit_uname_ramdisk
= fit_get_name(fit_hdr
,
951 /* get ramdisk component image node offset */
953 BOOTSTAGE_ID_FIT_RD_UNIT_NAME
);
954 rd_noffset
= fit_image_get_node(fit_hdr
,
957 if (rd_noffset
< 0) {
958 puts("Could not find subimage node\n");
959 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE
);
963 printf(" Trying '%s' ramdisk subimage\n",
966 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK
);
967 if (!fit_check_ramdisk(fit_hdr
, rd_noffset
, arch
,
971 /* get ramdisk image data address and length */
972 if (fit_image_get_data(fit_hdr
, rd_noffset
, &data
,
974 puts("Could not find ramdisk subimage data!\n");
975 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA
);
978 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK
);
980 rd_data
= (ulong
)data
;
983 if (fit_image_get_load(fit_hdr
, rd_noffset
, &rd_load
)) {
984 puts("Can't get ramdisk subimage load "
986 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD
);
989 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD
);
991 images
->fit_hdr_rd
= fit_hdr
;
992 images
->fit_uname_rd
= fit_uname_ramdisk
;
993 images
->fit_noffset_rd
= rd_noffset
;
997 puts("Wrong Ramdisk Image Format\n");
998 rd_data
= rd_len
= rd_load
= 0;
1001 } else if (images
->legacy_hdr_valid
&&
1002 image_check_type(&images
->legacy_hdr_os_copy
,
1006 * Now check if we have a legacy mult-component image,
1007 * get second entry data start address and len.
1009 bootstage_mark(BOOTSTAGE_ID_RAMDISK
);
1010 printf("## Loading init Ramdisk from multi component "
1011 "Legacy Image at %08lx ...\n",
1012 (ulong
)images
->legacy_hdr_os
);
1014 image_multi_getimg(images
->legacy_hdr_os
, 1, &rd_data
, &rd_len
);
1019 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK
);
1020 rd_len
= rd_data
= 0;
1024 debug("## No init Ramdisk\n");
1026 *rd_start
= rd_data
;
1027 *rd_end
= rd_data
+ rd_len
;
1029 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1030 *rd_start
, *rd_end
);
1035 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1037 * boot_ramdisk_high - relocate init ramdisk
1038 * @lmb: pointer to lmb handle, will be used for memory mgmt
1039 * @rd_data: ramdisk data start address
1040 * @rd_len: ramdisk data length
1041 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1042 * start address (after possible relocation)
1043 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1044 * end address (after possible relocation)
1046 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1047 * variable and if requested ramdisk data is moved to a specified location.
1049 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1050 * start/end addresses if ramdisk image start and len were provided,
1051 * otherwise set initrd_start and initrd_end set to zeros.
1057 int boot_ramdisk_high(struct lmb
*lmb
, ulong rd_data
, ulong rd_len
,
1058 ulong
*initrd_start
, ulong
*initrd_end
)
1062 int initrd_copy_to_ram
= 1;
1064 if ((s
= getenv("initrd_high")) != NULL
) {
1065 /* a value of "no" or a similar string will act like 0,
1066 * turning the "load high" feature off. This is intentional.
1068 initrd_high
= simple_strtoul(s
, NULL
, 16);
1069 if (initrd_high
== ~0)
1070 initrd_copy_to_ram
= 0;
1072 /* not set, no restrictions to load high */
1077 #ifdef CONFIG_LOGBUFFER
1078 /* Prevent initrd from overwriting logbuffer */
1079 lmb_reserve(lmb
, logbuffer_base() - LOGBUFF_OVERHEAD
, LOGBUFF_RESERVE
);
1082 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1083 initrd_high
, initrd_copy_to_ram
);
1086 if (!initrd_copy_to_ram
) { /* zero-copy ramdisk support */
1087 debug(" in-place initrd\n");
1088 *initrd_start
= rd_data
;
1089 *initrd_end
= rd_data
+ rd_len
;
1090 lmb_reserve(lmb
, rd_data
, rd_len
);
1093 *initrd_start
= (ulong
)lmb_alloc_base(lmb
,
1094 rd_len
, 0x1000, initrd_high
);
1096 *initrd_start
= (ulong
)lmb_alloc(lmb
, rd_len
,
1099 if (*initrd_start
== 0) {
1100 puts("ramdisk - allocation error\n");
1103 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK
);
1105 *initrd_end
= *initrd_start
+ rd_len
;
1106 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
1107 *initrd_start
, *initrd_end
);
1109 memmove_wd((void *)*initrd_start
,
1110 (void *)rd_data
, rd_len
, CHUNKSZ
);
1114 * Ensure the image is flushed to memory to handle
1115 * AMP boot scenarios in which we might not be
1118 flush_cache((unsigned long)*initrd_start
, rd_len
);
1126 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1127 *initrd_start
, *initrd_end
);
1134 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1136 #ifdef CONFIG_OF_LIBFDT
1137 static void fdt_error(const char *msg
)
1141 puts(" - must RESET the board to recover.\n");
1144 static const image_header_t
*image_get_fdt(ulong fdt_addr
)
1146 const image_header_t
*fdt_hdr
= (const image_header_t
*)fdt_addr
;
1148 image_print_contents(fdt_hdr
);
1150 puts(" Verifying Checksum ... ");
1151 if (!image_check_hcrc(fdt_hdr
)) {
1152 fdt_error("fdt header checksum invalid");
1156 if (!image_check_dcrc(fdt_hdr
)) {
1157 fdt_error("fdt checksum invalid");
1162 if (!image_check_type(fdt_hdr
, IH_TYPE_FLATDT
)) {
1163 fdt_error("uImage is not a fdt");
1166 if (image_get_comp(fdt_hdr
) != IH_COMP_NONE
) {
1167 fdt_error("uImage is compressed");
1170 if (fdt_check_header((char *)image_get_data(fdt_hdr
)) != 0) {
1171 fdt_error("uImage data is not a fdt");
1178 * fit_check_fdt - verify FIT format FDT subimage
1179 * @fit_hdr: pointer to the FIT header
1180 * fdt_noffset: FDT subimage node offset within FIT image
1181 * @verify: data CRC verification flag
1183 * fit_check_fdt() verifies integrity of the FDT subimage and from
1184 * specified FIT image.
1190 #if defined(CONFIG_FIT)
1191 static int fit_check_fdt(const void *fit
, int fdt_noffset
, int verify
)
1193 fit_image_print(fit
, fdt_noffset
, " ");
1196 puts(" Verifying Hash Integrity ... ");
1197 if (!fit_image_check_hashes(fit
, fdt_noffset
)) {
1198 fdt_error("Bad Data Hash");
1204 if (!fit_image_check_type(fit
, fdt_noffset
, IH_TYPE_FLATDT
)) {
1205 fdt_error("Not a FDT image");
1209 if (!fit_image_check_comp(fit
, fdt_noffset
, IH_COMP_NONE
)) {
1210 fdt_error("FDT image is compressed");
1216 #endif /* CONFIG_FIT */
1218 #ifndef CONFIG_SYS_FDT_PAD
1219 #define CONFIG_SYS_FDT_PAD 0x3000
1222 #if defined(CONFIG_OF_LIBFDT)
1224 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1225 * @lmb: pointer to lmb handle, will be used for memory mgmt
1226 * @fdt_blob: pointer to fdt blob base address
1228 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1229 * memreserve regions prevents u-boot from using them to store the initrd
1232 void boot_fdt_add_mem_rsv_regions(struct lmb
*lmb
, void *fdt_blob
)
1234 uint64_t addr
, size
;
1237 if (fdt_check_header(fdt_blob
) != 0)
1240 total
= fdt_num_mem_rsv(fdt_blob
);
1241 for (i
= 0; i
< total
; i
++) {
1242 if (fdt_get_mem_rsv(fdt_blob
, i
, &addr
, &size
) != 0)
1244 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1245 (unsigned long long)addr
, (unsigned long long)size
);
1246 lmb_reserve(lmb
, addr
, size
);
1251 * boot_relocate_fdt - relocate flat device tree
1252 * @lmb: pointer to lmb handle, will be used for memory mgmt
1253 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1254 * @of_size: pointer to a ulong variable, will hold fdt length
1256 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1257 * relocates the of_flat_tree into that region, even if the fdt is already in
1258 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1261 * of_flat_tree and of_size are set to final (after relocation) values
1267 int boot_relocate_fdt(struct lmb
*lmb
, char **of_flat_tree
, ulong
*of_size
)
1269 void *fdt_blob
= *of_flat_tree
;
1274 int disable_relocation
= 0;
1280 if (fdt_check_header(fdt_blob
) != 0) {
1281 fdt_error("image is not a fdt");
1285 /* position on a 4K boundary before the alloc_current */
1286 /* Pad the FDT by a specified amount */
1287 of_len
= *of_size
+ CONFIG_SYS_FDT_PAD
;
1289 /* If fdt_high is set use it to select the relocation address */
1290 fdt_high
= getenv("fdt_high");
1292 void *desired_addr
= (void *)simple_strtoul(fdt_high
, NULL
, 16);
1294 if (((ulong
) desired_addr
) == ~0UL) {
1295 /* All ones means use fdt in place */
1296 of_start
= fdt_blob
;
1297 lmb_reserve(lmb
, (ulong
)of_start
, of_len
);
1298 disable_relocation
= 1;
1299 } else if (desired_addr
) {
1301 (void *)(ulong
) lmb_alloc_base(lmb
, of_len
, 0x1000,
1302 (ulong
)desired_addr
);
1303 if (of_start
== 0) {
1304 puts("Failed using fdt_high value for Device Tree");
1309 (void *)(ulong
) lmb_alloc(lmb
, of_len
, 0x1000);
1313 (void *)(ulong
) lmb_alloc_base(lmb
, of_len
, 0x1000,
1314 getenv_bootm_mapsize()
1315 + getenv_bootm_low());
1318 if (of_start
== 0) {
1319 puts("device tree - allocation error\n");
1323 if (disable_relocation
) {
1324 /* We assume there is space after the existing fdt to use for padding */
1325 fdt_set_totalsize(of_start
, of_len
);
1326 printf(" Using Device Tree in place at %p, end %p\n",
1327 of_start
, of_start
+ of_len
- 1);
1329 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1330 fdt_blob
, fdt_blob
+ *of_size
- 1, of_len
, of_len
);
1332 printf(" Loading Device Tree to %p, end %p ... ",
1333 of_start
, of_start
+ of_len
- 1);
1335 err
= fdt_open_into(fdt_blob
, of_start
, of_len
);
1337 fdt_error("fdt move failed");
1343 *of_flat_tree
= of_start
;
1346 set_working_fdt_addr(*of_flat_tree
);
1352 #endif /* CONFIG_OF_LIBFDT */
1355 * boot_get_fdt - main fdt handling routine
1356 * @argc: command argument count
1357 * @argv: command argument list
1358 * @images: pointer to the bootm images structure
1359 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1360 * @of_size: pointer to a ulong variable, will hold fdt length
1362 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1363 * Curently supported are the following ramdisk sources:
1364 * - multicomponent kernel/ramdisk image,
1365 * - commandline provided address of decicated ramdisk image.
1368 * 0, if fdt image was found and valid, or skipped
1369 * of_flat_tree and of_size are set to fdt start address and length if
1370 * fdt image is found and valid
1372 * 1, if fdt image is found but corrupted
1373 * of_flat_tree and of_size are set to 0 if no fdt exists
1375 int boot_get_fdt(int flag
, int argc
, char * const argv
[],
1376 bootm_headers_t
*images
, char **of_flat_tree
, ulong
*of_size
)
1378 const image_header_t
*fdt_hdr
;
1380 char *fdt_blob
= NULL
;
1381 ulong image_start
, image_data
, image_end
;
1382 ulong load_start
, load_end
;
1383 #if defined(CONFIG_FIT)
1385 const char *fit_uname_config
= NULL
;
1386 const char *fit_uname_fdt
= NULL
;
1394 *of_flat_tree
= NULL
;
1397 if (argc
> 3 || genimg_has_config(images
)) {
1398 #if defined(CONFIG_FIT)
1401 * If the FDT blob comes from the FIT image and the
1402 * FIT image address is omitted in the command line
1403 * argument, try to use ramdisk or os FIT image
1404 * address or default load address.
1406 if (images
->fit_uname_rd
)
1407 default_addr
= (ulong
)images
->fit_hdr_rd
;
1408 else if (images
->fit_uname_os
)
1409 default_addr
= (ulong
)images
->fit_hdr_os
;
1411 default_addr
= load_addr
;
1413 if (fit_parse_conf(argv
[3], default_addr
,
1414 &fdt_addr
, &fit_uname_config
)) {
1415 debug("* fdt: config '%s' from image at "
1417 fit_uname_config
, fdt_addr
);
1418 } else if (fit_parse_subimage(argv
[3], default_addr
,
1419 &fdt_addr
, &fit_uname_fdt
)) {
1420 debug("* fdt: subimage '%s' from image at "
1422 fit_uname_fdt
, fdt_addr
);
1426 fdt_addr
= simple_strtoul(argv
[3], NULL
, 16);
1427 debug("* fdt: cmdline image address = "
1431 #if defined(CONFIG_FIT)
1433 /* use FIT configuration provided in first bootm
1436 fdt_addr
= (ulong
)images
->fit_hdr_os
;
1437 fit_uname_config
= images
->fit_uname_cfg
;
1438 debug("* fdt: using config '%s' from image "
1440 fit_uname_config
, fdt_addr
);
1443 * Check whether configuration has FDT blob defined,
1444 * if not quit silently.
1446 fit_hdr
= (void *)fdt_addr
;
1447 cfg_noffset
= fit_conf_get_node(fit_hdr
,
1449 if (cfg_noffset
< 0) {
1450 debug("* fdt: no such config\n");
1454 fdt_noffset
= fit_conf_get_fdt_node(fit_hdr
,
1456 if (fdt_noffset
< 0) {
1457 debug("* fdt: no fdt in config\n");
1463 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1466 /* copy from dataflash if needed */
1467 fdt_addr
= genimg_get_image(fdt_addr
);
1470 * Check if there is an FDT image at the
1471 * address provided in the second bootm argument
1472 * check image type, for FIT images get a FIT node.
1474 switch (genimg_get_format((void *)fdt_addr
)) {
1475 case IMAGE_FORMAT_LEGACY
:
1476 /* verify fdt_addr points to a valid image header */
1477 printf("## Flattened Device Tree from Legacy Image "
1480 fdt_hdr
= image_get_fdt(fdt_addr
);
1485 * move image data to the load address,
1486 * make sure we don't overwrite initial image
1488 image_start
= (ulong
)fdt_hdr
;
1489 image_data
= (ulong
)image_get_data(fdt_hdr
);
1490 image_end
= image_get_image_end(fdt_hdr
);
1492 load_start
= image_get_load(fdt_hdr
);
1493 load_end
= load_start
+ image_get_data_size(fdt_hdr
);
1495 if (load_start
== image_start
||
1496 load_start
== image_data
) {
1497 fdt_blob
= (char *)image_data
;
1501 if ((load_start
< image_end
) && (load_end
> image_start
)) {
1502 fdt_error("fdt overwritten");
1506 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
1507 image_data
, load_start
);
1509 memmove((void *)load_start
,
1511 image_get_data_size(fdt_hdr
));
1513 fdt_blob
= (char *)load_start
;
1515 case IMAGE_FORMAT_FIT
:
1517 * This case will catch both: new uImage format
1518 * (libfdt based) and raw FDT blob (also libfdt
1521 #if defined(CONFIG_FIT)
1522 /* check FDT blob vs FIT blob */
1523 if (fit_check_format((const void *)fdt_addr
)) {
1527 fit_hdr
= (void *)fdt_addr
;
1528 printf("## Flattened Device Tree from FIT "
1532 if (!fit_uname_fdt
) {
1534 * no FDT blob image node unit name,
1535 * try to get config node first. If
1536 * config unit node name is NULL
1537 * fit_conf_get_node() will try to
1538 * find default config node
1540 cfg_noffset
= fit_conf_get_node(fit_hdr
,
1543 if (cfg_noffset
< 0) {
1544 fdt_error("Could not find "
1550 fit_uname_config
= fdt_get_name(fit_hdr
,
1552 printf(" Using '%s' configuration\n",
1555 fdt_noffset
= fit_conf_get_fdt_node(
1558 fit_uname_fdt
= fit_get_name(fit_hdr
,
1561 /* get FDT component image node offset */
1562 fdt_noffset
= fit_image_get_node(
1566 if (fdt_noffset
< 0) {
1567 fdt_error("Could not find subimage "
1572 printf(" Trying '%s' FDT blob subimage\n",
1575 if (!fit_check_fdt(fit_hdr
, fdt_noffset
,
1579 /* get ramdisk image data address and length */
1580 if (fit_image_get_data(fit_hdr
, fdt_noffset
,
1582 fdt_error("Could not find FDT "
1587 /* verift that image data is a proper FDT blob */
1588 if (fdt_check_header((char *)data
) != 0) {
1589 fdt_error("Subimage data is not a FTD");
1594 * move image data to the load address,
1595 * make sure we don't overwrite initial image
1597 image_start
= (ulong
)fit_hdr
;
1598 image_end
= fit_get_end(fit_hdr
);
1600 if (fit_image_get_load(fit_hdr
, fdt_noffset
,
1601 &load_start
) == 0) {
1602 load_end
= load_start
+ size
;
1604 if ((load_start
< image_end
) &&
1605 (load_end
> image_start
)) {
1606 fdt_error("FDT overwritten");
1610 printf(" Loading FDT from 0x%08lx "
1615 memmove((void *)load_start
,
1616 (void *)data
, size
);
1618 fdt_blob
= (char *)load_start
;
1620 fdt_blob
= (char *)data
;
1623 images
->fit_hdr_fdt
= fit_hdr
;
1624 images
->fit_uname_fdt
= fit_uname_fdt
;
1625 images
->fit_noffset_fdt
= fdt_noffset
;
1633 fdt_blob
= (char *)fdt_addr
;
1634 debug("* fdt: raw FDT blob\n");
1635 printf("## Flattened Device Tree blob at "
1636 "%08lx\n", (long)fdt_blob
);
1640 puts("ERROR: Did not find a cmdline Flattened Device "
1645 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob
);
1647 } else if (images
->legacy_hdr_valid
&&
1648 image_check_type(&images
->legacy_hdr_os_copy
,
1651 ulong fdt_data
, fdt_len
;
1654 * Now check if we have a legacy multi-component image,
1655 * get second entry data start address and len.
1657 printf("## Flattened Device Tree from multi "
1658 "component Image at %08lX\n",
1659 (ulong
)images
->legacy_hdr_os
);
1661 image_multi_getimg(images
->legacy_hdr_os
, 2, &fdt_data
,
1665 fdt_blob
= (char *)fdt_data
;
1666 printf(" Booting using the fdt at 0x%p\n", fdt_blob
);
1668 if (fdt_check_header(fdt_blob
) != 0) {
1669 fdt_error("image is not a fdt");
1673 if (fdt_totalsize(fdt_blob
) != fdt_len
) {
1674 fdt_error("fdt size != image size");
1678 debug("## No Flattened Device Tree\n");
1682 debug("## No Flattened Device Tree\n");
1686 *of_flat_tree
= fdt_blob
;
1687 *of_size
= fdt_totalsize(fdt_blob
);
1688 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
1689 (ulong
)*of_flat_tree
, *of_size
);
1698 #endif /* CONFIG_OF_LIBFDT */
1700 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1702 * boot_get_cmdline - allocate and initialize kernel cmdline
1703 * @lmb: pointer to lmb handle, will be used for memory mgmt
1704 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1705 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1707 * boot_get_cmdline() allocates space for kernel command line below
1708 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1709 * variable is present its contents is copied to allocated kernel
1716 int boot_get_cmdline(struct lmb
*lmb
, ulong
*cmd_start
, ulong
*cmd_end
)
1721 cmdline
= (char *)(ulong
)lmb_alloc_base(lmb
, CONFIG_SYS_BARGSIZE
, 0xf,
1722 getenv_bootm_mapsize() + getenv_bootm_low());
1724 if (cmdline
== NULL
)
1727 if ((s
= getenv("bootargs")) == NULL
)
1732 *cmd_start
= (ulong
) & cmdline
[0];
1733 *cmd_end
= *cmd_start
+ strlen(cmdline
);
1735 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start
, *cmd_end
);
1739 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1741 #ifdef CONFIG_SYS_BOOT_GET_KBD
1743 * boot_get_kbd - allocate and initialize kernel copy of board info
1744 * @lmb: pointer to lmb handle, will be used for memory mgmt
1745 * @kbd: double pointer to board info data
1747 * boot_get_kbd() allocates space for kernel copy of board info data below
1748 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1749 * with the current u-boot board info data.
1755 int boot_get_kbd(struct lmb
*lmb
, bd_t
**kbd
)
1757 *kbd
= (bd_t
*)(ulong
)lmb_alloc_base(lmb
, sizeof(bd_t
), 0xf,
1758 getenv_bootm_mapsize() + getenv_bootm_low());
1764 debug("## kernel board info at 0x%08lx\n", (ulong
)*kbd
);
1766 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1767 do_bdinfo(NULL
, 0, 0, NULL
);
1772 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1773 #endif /* !USE_HOSTCC */
1775 #if defined(CONFIG_FIT)
1776 /*****************************************************************************/
1777 /* New uImage format routines */
1778 /*****************************************************************************/
1780 static int fit_parse_spec(const char *spec
, char sepc
, ulong addr_curr
,
1781 ulong
*addr
, const char **name
)
1788 sep
= strchr(spec
, sepc
);
1791 *addr
= simple_strtoul(spec
, NULL
, 16);
1801 * fit_parse_conf - parse FIT configuration spec
1802 * @spec: input string, containing configuration spec
1803 * @add_curr: current image address (to be used as a possible default)
1804 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1806 * @conf_name double pointer to a char, will hold pointer to a configuration
1809 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1810 * where <addr> is a FIT image address that contains configuration
1811 * with a <conf> unit name.
1813 * Address part is optional, and if omitted default add_curr will
1817 * 1 if spec is a valid configuration string,
1818 * addr and conf_name are set accordingly
1821 inline int fit_parse_conf(const char *spec
, ulong addr_curr
,
1822 ulong
*addr
, const char **conf_name
)
1824 return fit_parse_spec(spec
, '#', addr_curr
, addr
, conf_name
);
1828 * fit_parse_subimage - parse FIT subimage spec
1829 * @spec: input string, containing subimage spec
1830 * @add_curr: current image address (to be used as a possible default)
1831 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1833 * @image_name: double pointer to a char, will hold pointer to a subimage name
1835 * fit_parse_subimage() expects subimage spec in the for of
1836 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1837 * subimage with a <subimg> unit name.
1839 * Address part is optional, and if omitted default add_curr will
1843 * 1 if spec is a valid subimage string,
1844 * addr and image_name are set accordingly
1847 inline int fit_parse_subimage(const char *spec
, ulong addr_curr
,
1848 ulong
*addr
, const char **image_name
)
1850 return fit_parse_spec(spec
, ':', addr_curr
, addr
, image_name
);
1852 #endif /* !USE_HOSTCC */
1854 static void fit_get_debug(const void *fit
, int noffset
,
1855 char *prop_name
, int err
)
1857 debug("Can't get '%s' property from FIT 0x%08lx, "
1858 "node: offset %d, name %s (%s)\n",
1859 prop_name
, (ulong
)fit
, noffset
,
1860 fit_get_name(fit
, noffset
, NULL
),
1865 * fit_print_contents - prints out the contents of the FIT format image
1866 * @fit: pointer to the FIT format image header
1867 * @p: pointer to prefix string
1869 * fit_print_contents() formats a multi line FIT image contents description.
1870 * The routine prints out FIT image properties (root node level) follwed by
1871 * the details of each component image.
1874 * no returned results
1876 void fit_print_contents(const void *fit
)
1887 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1897 /* Root node properties */
1898 ret
= fit_get_desc(fit
, 0, &desc
);
1899 printf("%sFIT description: ", p
);
1901 printf("unavailable\n");
1903 printf("%s\n", desc
);
1905 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1906 ret
= fit_get_timestamp(fit
, 0, ×tamp
);
1907 printf("%sCreated: ", p
);
1909 printf("unavailable\n");
1911 genimg_print_time(timestamp
);
1914 /* Find images parent node offset */
1915 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
1916 if (images_noffset
< 0) {
1917 printf("Can't find images parent node '%s' (%s)\n",
1918 FIT_IMAGES_PATH
, fdt_strerror(images_noffset
));
1922 /* Process its subnodes, print out component images details */
1923 for (ndepth
= 0, count
= 0,
1924 noffset
= fdt_next_node(fit
, images_noffset
, &ndepth
);
1925 (noffset
>= 0) && (ndepth
> 0);
1926 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
1929 * Direct child node of the images parent node,
1930 * i.e. component image node.
1932 printf("%s Image %u (%s)\n", p
, count
++,
1933 fit_get_name(fit
, noffset
, NULL
));
1935 fit_image_print(fit
, noffset
, p
);
1939 /* Find configurations parent node offset */
1940 confs_noffset
= fdt_path_offset(fit
, FIT_CONFS_PATH
);
1941 if (confs_noffset
< 0) {
1942 debug("Can't get configurations parent node '%s' (%s)\n",
1943 FIT_CONFS_PATH
, fdt_strerror(confs_noffset
));
1947 /* get default configuration unit name from default property */
1948 uname
= (char *)fdt_getprop(fit
, noffset
, FIT_DEFAULT_PROP
, NULL
);
1950 printf("%s Default Configuration: '%s'\n", p
, uname
);
1952 /* Process its subnodes, print out configurations details */
1953 for (ndepth
= 0, count
= 0,
1954 noffset
= fdt_next_node(fit
, confs_noffset
, &ndepth
);
1955 (noffset
>= 0) && (ndepth
> 0);
1956 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
1959 * Direct child node of the configurations parent node,
1960 * i.e. configuration node.
1962 printf("%s Configuration %u (%s)\n", p
, count
++,
1963 fit_get_name(fit
, noffset
, NULL
));
1965 fit_conf_print(fit
, noffset
, p
);
1971 * fit_image_print - prints out the FIT component image details
1972 * @fit: pointer to the FIT format image header
1973 * @image_noffset: offset of the component image node
1974 * @p: pointer to prefix string
1976 * fit_image_print() lists all mandatory properies for the processed component
1977 * image. If present, hash nodes are printed out as well. Load
1978 * address for images of type firmware is also printed out. Since the load
1979 * address is not mandatory for firmware images, it will be output as
1980 * "unavailable" when not present.
1983 * no returned results
1985 void fit_image_print(const void *fit
, int image_noffset
, const char *p
)
1988 uint8_t type
, arch
, os
, comp
;
1996 /* Mandatory properties */
1997 ret
= fit_get_desc(fit
, image_noffset
, &desc
);
1998 printf("%s Description: ", p
);
2000 printf("unavailable\n");
2002 printf("%s\n", desc
);
2004 fit_image_get_type(fit
, image_noffset
, &type
);
2005 printf("%s Type: %s\n", p
, genimg_get_type_name(type
));
2007 fit_image_get_comp(fit
, image_noffset
, &comp
);
2008 printf("%s Compression: %s\n", p
, genimg_get_comp_name(comp
));
2010 ret
= fit_image_get_data(fit
, image_noffset
, &data
, &size
);
2013 printf("%s Data Start: ", p
);
2015 printf("unavailable\n");
2017 printf("0x%08lx\n", (ulong
)data
);
2020 printf("%s Data Size: ", p
);
2022 printf("unavailable\n");
2024 genimg_print_size(size
);
2026 /* Remaining, type dependent properties */
2027 if ((type
== IH_TYPE_KERNEL
) || (type
== IH_TYPE_STANDALONE
) ||
2028 (type
== IH_TYPE_RAMDISK
) || (type
== IH_TYPE_FIRMWARE
) ||
2029 (type
== IH_TYPE_FLATDT
)) {
2030 fit_image_get_arch(fit
, image_noffset
, &arch
);
2031 printf("%s Architecture: %s\n", p
, genimg_get_arch_name(arch
));
2034 if (type
== IH_TYPE_KERNEL
) {
2035 fit_image_get_os(fit
, image_noffset
, &os
);
2036 printf("%s OS: %s\n", p
, genimg_get_os_name(os
));
2039 if ((type
== IH_TYPE_KERNEL
) || (type
== IH_TYPE_STANDALONE
) ||
2040 (type
== IH_TYPE_FIRMWARE
)) {
2041 ret
= fit_image_get_load(fit
, image_noffset
, &load
);
2042 printf("%s Load Address: ", p
);
2044 printf("unavailable\n");
2046 printf("0x%08lx\n", load
);
2049 if ((type
== IH_TYPE_KERNEL
) || (type
== IH_TYPE_STANDALONE
)) {
2050 fit_image_get_entry(fit
, image_noffset
, &entry
);
2051 printf("%s Entry Point: ", p
);
2053 printf("unavailable\n");
2055 printf("0x%08lx\n", entry
);
2058 /* Process all hash subnodes of the component image node */
2059 for (ndepth
= 0, noffset
= fdt_next_node(fit
, image_noffset
, &ndepth
);
2060 (noffset
>= 0) && (ndepth
> 0);
2061 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
2063 /* Direct child node of the component image node */
2064 fit_image_print_hash(fit
, noffset
, p
);
2070 * fit_image_print_hash - prints out the hash node details
2071 * @fit: pointer to the FIT format image header
2072 * @noffset: offset of the hash node
2073 * @p: pointer to prefix string
2075 * fit_image_print_hash() lists properies for the processed hash node
2078 * no returned results
2080 void fit_image_print_hash(const void *fit
, int noffset
, const char *p
)
2088 * Check subnode name, must be equal to "hash".
2089 * Multiple hash nodes require unique unit node
2090 * names, e.g. hash@1, hash@2, etc.
2092 if (strncmp(fit_get_name(fit
, noffset
, NULL
),
2094 strlen(FIT_HASH_NODENAME
)) != 0)
2097 debug("%s Hash node: '%s'\n", p
,
2098 fit_get_name(fit
, noffset
, NULL
));
2100 printf("%s Hash algo: ", p
);
2101 if (fit_image_hash_get_algo(fit
, noffset
, &algo
)) {
2102 printf("invalid/unsupported\n");
2105 printf("%s\n", algo
);
2107 ret
= fit_image_hash_get_value(fit
, noffset
, &value
,
2109 printf("%s Hash value: ", p
);
2111 printf("unavailable\n");
2113 for (i
= 0; i
< value_len
; i
++)
2114 printf("%02x", value
[i
]);
2118 debug("%s Hash len: %d\n", p
, value_len
);
2122 * fit_get_desc - get node description property
2123 * @fit: pointer to the FIT format image header
2124 * @noffset: node offset
2125 * @desc: double pointer to the char, will hold pointer to the descrption
2127 * fit_get_desc() reads description property from a given node, if
2128 * description is found pointer to it is returened in third call argument.
2134 int fit_get_desc(const void *fit
, int noffset
, char **desc
)
2138 *desc
= (char *)fdt_getprop(fit
, noffset
, FIT_DESC_PROP
, &len
);
2139 if (*desc
== NULL
) {
2140 fit_get_debug(fit
, noffset
, FIT_DESC_PROP
, len
);
2148 * fit_get_timestamp - get node timestamp property
2149 * @fit: pointer to the FIT format image header
2150 * @noffset: node offset
2151 * @timestamp: pointer to the time_t, will hold read timestamp
2153 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2154 * is found and has a correct size its value is retured in third call
2159 * -1, on property read failure
2160 * -2, on wrong timestamp size
2162 int fit_get_timestamp(const void *fit
, int noffset
, time_t *timestamp
)
2167 data
= fdt_getprop(fit
, noffset
, FIT_TIMESTAMP_PROP
, &len
);
2169 fit_get_debug(fit
, noffset
, FIT_TIMESTAMP_PROP
, len
);
2172 if (len
!= sizeof(uint32_t)) {
2173 debug("FIT timestamp with incorrect size of (%u)\n", len
);
2177 *timestamp
= uimage_to_cpu(*((uint32_t *)data
));
2182 * fit_image_get_node - get node offset for component image of a given unit name
2183 * @fit: pointer to the FIT format image header
2184 * @image_uname: component image node unit name
2186 * fit_image_get_node() finds a component image (withing the '/images'
2187 * node) of a provided unit name. If image is found its node offset is
2188 * returned to the caller.
2191 * image node offset when found (>=0)
2192 * negative number on failure (FDT_ERR_* code)
2194 int fit_image_get_node(const void *fit
, const char *image_uname
)
2196 int noffset
, images_noffset
;
2198 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
2199 if (images_noffset
< 0) {
2200 debug("Can't find images parent node '%s' (%s)\n",
2201 FIT_IMAGES_PATH
, fdt_strerror(images_noffset
));
2202 return images_noffset
;
2205 noffset
= fdt_subnode_offset(fit
, images_noffset
, image_uname
);
2207 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2208 image_uname
, fdt_strerror(noffset
));
2215 * fit_image_get_os - get os id for a given component image node
2216 * @fit: pointer to the FIT format image header
2217 * @noffset: component image node offset
2218 * @os: pointer to the uint8_t, will hold os numeric id
2220 * fit_image_get_os() finds os property in a given component image node.
2221 * If the property is found, its (string) value is translated to the numeric
2222 * id which is returned to the caller.
2228 int fit_image_get_os(const void *fit
, int noffset
, uint8_t *os
)
2233 /* Get OS name from property data */
2234 data
= fdt_getprop(fit
, noffset
, FIT_OS_PROP
, &len
);
2236 fit_get_debug(fit
, noffset
, FIT_OS_PROP
, len
);
2241 /* Translate OS name to id */
2242 *os
= genimg_get_os_id(data
);
2247 * fit_image_get_arch - get arch id for a given component image node
2248 * @fit: pointer to the FIT format image header
2249 * @noffset: component image node offset
2250 * @arch: pointer to the uint8_t, will hold arch numeric id
2252 * fit_image_get_arch() finds arch property in a given component image node.
2253 * If the property is found, its (string) value is translated to the numeric
2254 * id which is returned to the caller.
2260 int fit_image_get_arch(const void *fit
, int noffset
, uint8_t *arch
)
2265 /* Get architecture name from property data */
2266 data
= fdt_getprop(fit
, noffset
, FIT_ARCH_PROP
, &len
);
2268 fit_get_debug(fit
, noffset
, FIT_ARCH_PROP
, len
);
2273 /* Translate architecture name to id */
2274 *arch
= genimg_get_arch_id(data
);
2279 * fit_image_get_type - get type id for a given component image node
2280 * @fit: pointer to the FIT format image header
2281 * @noffset: component image node offset
2282 * @type: pointer to the uint8_t, will hold type numeric id
2284 * fit_image_get_type() finds type property in a given component image node.
2285 * If the property is found, its (string) value is translated to the numeric
2286 * id which is returned to the caller.
2292 int fit_image_get_type(const void *fit
, int noffset
, uint8_t *type
)
2297 /* Get image type name from property data */
2298 data
= fdt_getprop(fit
, noffset
, FIT_TYPE_PROP
, &len
);
2300 fit_get_debug(fit
, noffset
, FIT_TYPE_PROP
, len
);
2305 /* Translate image type name to id */
2306 *type
= genimg_get_type_id(data
);
2311 * fit_image_get_comp - get comp id for a given component image node
2312 * @fit: pointer to the FIT format image header
2313 * @noffset: component image node offset
2314 * @comp: pointer to the uint8_t, will hold comp numeric id
2316 * fit_image_get_comp() finds comp property in a given component image node.
2317 * If the property is found, its (string) value is translated to the numeric
2318 * id which is returned to the caller.
2324 int fit_image_get_comp(const void *fit
, int noffset
, uint8_t *comp
)
2329 /* Get compression name from property data */
2330 data
= fdt_getprop(fit
, noffset
, FIT_COMP_PROP
, &len
);
2332 fit_get_debug(fit
, noffset
, FIT_COMP_PROP
, len
);
2337 /* Translate compression name to id */
2338 *comp
= genimg_get_comp_id(data
);
2343 * fit_image_get_load - get load address property for a given component image node
2344 * @fit: pointer to the FIT format image header
2345 * @noffset: component image node offset
2346 * @load: pointer to the uint32_t, will hold load address
2348 * fit_image_get_load() finds load address property in a given component image node.
2349 * If the property is found, its value is returned to the caller.
2355 int fit_image_get_load(const void *fit
, int noffset
, ulong
*load
)
2358 const uint32_t *data
;
2360 data
= fdt_getprop(fit
, noffset
, FIT_LOAD_PROP
, &len
);
2362 fit_get_debug(fit
, noffset
, FIT_LOAD_PROP
, len
);
2366 *load
= uimage_to_cpu(*data
);
2371 * fit_image_get_entry - get entry point address property for a given component image node
2372 * @fit: pointer to the FIT format image header
2373 * @noffset: component image node offset
2374 * @entry: pointer to the uint32_t, will hold entry point address
2376 * fit_image_get_entry() finds entry point address property in a given component image node.
2377 * If the property is found, its value is returned to the caller.
2383 int fit_image_get_entry(const void *fit
, int noffset
, ulong
*entry
)
2386 const uint32_t *data
;
2388 data
= fdt_getprop(fit
, noffset
, FIT_ENTRY_PROP
, &len
);
2390 fit_get_debug(fit
, noffset
, FIT_ENTRY_PROP
, len
);
2394 *entry
= uimage_to_cpu(*data
);
2399 * fit_image_get_data - get data property and its size for a given component image node
2400 * @fit: pointer to the FIT format image header
2401 * @noffset: component image node offset
2402 * @data: double pointer to void, will hold data property's data address
2403 * @size: pointer to size_t, will hold data property's data size
2405 * fit_image_get_data() finds data property in a given component image node.
2406 * If the property is found its data start address and size are returned to
2413 int fit_image_get_data(const void *fit
, int noffset
,
2414 const void **data
, size_t *size
)
2418 *data
= fdt_getprop(fit
, noffset
, FIT_DATA_PROP
, &len
);
2419 if (*data
== NULL
) {
2420 fit_get_debug(fit
, noffset
, FIT_DATA_PROP
, len
);
2430 * fit_image_hash_get_algo - get hash algorithm name
2431 * @fit: pointer to the FIT format image header
2432 * @noffset: hash node offset
2433 * @algo: double pointer to char, will hold pointer to the algorithm name
2435 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2436 * If the property is found its data start address is returned to the caller.
2442 int fit_image_hash_get_algo(const void *fit
, int noffset
, char **algo
)
2446 *algo
= (char *)fdt_getprop(fit
, noffset
, FIT_ALGO_PROP
, &len
);
2447 if (*algo
== NULL
) {
2448 fit_get_debug(fit
, noffset
, FIT_ALGO_PROP
, len
);
2456 * fit_image_hash_get_value - get hash value and length
2457 * @fit: pointer to the FIT format image header
2458 * @noffset: hash node offset
2459 * @value: double pointer to uint8_t, will hold address of a hash value data
2460 * @value_len: pointer to an int, will hold hash data length
2462 * fit_image_hash_get_value() finds hash value property in a given hash node.
2463 * If the property is found its data start address and size are returned to
2470 int fit_image_hash_get_value(const void *fit
, int noffset
, uint8_t **value
,
2475 *value
= (uint8_t *)fdt_getprop(fit
, noffset
, FIT_VALUE_PROP
, &len
);
2476 if (*value
== NULL
) {
2477 fit_get_debug(fit
, noffset
, FIT_VALUE_PROP
, len
);
2487 * fit_set_timestamp - set node timestamp property
2488 * @fit: pointer to the FIT format image header
2489 * @noffset: node offset
2490 * @timestamp: timestamp value to be set
2492 * fit_set_timestamp() attempts to set timestamp property in the requested
2493 * node and returns operation status to the caller.
2497 * -1, on property read failure
2499 int fit_set_timestamp(void *fit
, int noffset
, time_t timestamp
)
2504 t
= cpu_to_uimage(timestamp
);
2505 ret
= fdt_setprop(fit
, noffset
, FIT_TIMESTAMP_PROP
, &t
,
2508 printf("Can't set '%s' property for '%s' node (%s)\n",
2509 FIT_TIMESTAMP_PROP
, fit_get_name(fit
, noffset
, NULL
),
2518 * calculate_hash - calculate and return hash for provided input data
2519 * @data: pointer to the input data
2520 * @data_len: data length
2521 * @algo: requested hash algorithm
2522 * @value: pointer to the char, will hold hash value data (caller must
2523 * allocate enough free space)
2524 * value_len: length of the calculated hash
2526 * calculate_hash() computes input data hash according to the requested algorithm.
2527 * Resulting hash value is placed in caller provided 'value' buffer, length
2528 * of the calculated hash is returned via value_len pointer argument.
2532 * -1, when algo is unsupported
2534 static int calculate_hash(const void *data
, int data_len
, const char *algo
,
2535 uint8_t *value
, int *value_len
)
2537 if (strcmp(algo
, "crc32") == 0) {
2538 *((uint32_t *)value
) = crc32_wd(0, data
, data_len
,
2540 *((uint32_t *)value
) = cpu_to_uimage(*((uint32_t *)value
));
2542 } else if (strcmp(algo
, "sha1") == 0) {
2543 sha1_csum_wd((unsigned char *) data
, data_len
,
2544 (unsigned char *) value
, CHUNKSZ_SHA1
);
2546 } else if (strcmp(algo
, "md5") == 0) {
2547 md5_wd((unsigned char *)data
, data_len
, value
, CHUNKSZ_MD5
);
2550 debug("Unsupported hash alogrithm\n");
2558 * fit_set_hashes - process FIT component image nodes and calculate hashes
2559 * @fit: pointer to the FIT format image header
2561 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2562 * Hashes are calculated for all component images which have hash subnodes
2563 * with algorithm property set to one of the supported hash algorithms.
2567 * libfdt error code, on failure
2569 int fit_set_hashes(void *fit
)
2576 /* Find images parent node offset */
2577 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
2578 if (images_noffset
< 0) {
2579 printf("Can't find images parent node '%s' (%s)\n",
2580 FIT_IMAGES_PATH
, fdt_strerror(images_noffset
));
2581 return images_noffset
;
2584 /* Process its subnodes, print out component images details */
2585 for (ndepth
= 0, noffset
= fdt_next_node(fit
, images_noffset
, &ndepth
);
2586 (noffset
>= 0) && (ndepth
> 0);
2587 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
2590 * Direct child node of the images parent node,
2591 * i.e. component image node.
2593 ret
= fit_image_set_hashes(fit
, noffset
);
2603 * fit_image_set_hashes - calculate/set hashes for given component image node
2604 * @fit: pointer to the FIT format image header
2605 * @image_noffset: requested component image node
2607 * fit_image_set_hashes() adds hash values for an component image node. All
2608 * existing hash subnodes are checked, if algorithm property is set to one of
2609 * the supported hash algorithms, hash value is computed and corresponding
2610 * hash node property is set, for example:
2612 * Input component image node structure:
2614 * o image@1 (at image_noffset)
2615 * | - data = [binary data]
2619 * Output component image node structure:
2621 * o image@1 (at image_noffset)
2622 * | - data = [binary data]
2625 * |- value = sha1(data)
2631 int fit_image_set_hashes(void *fit
, int image_noffset
)
2636 uint8_t value
[FIT_MAX_HASH_LEN
];
2641 /* Get image data and data length */
2642 if (fit_image_get_data(fit
, image_noffset
, &data
, &size
)) {
2643 printf("Can't get image data/size\n");
2647 /* Process all hash subnodes of the component image node */
2648 for (ndepth
= 0, noffset
= fdt_next_node(fit
, image_noffset
, &ndepth
);
2649 (noffset
>= 0) && (ndepth
> 0);
2650 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
2652 /* Direct child node of the component image node */
2655 * Check subnode name, must be equal to "hash".
2656 * Multiple hash nodes require unique unit node
2657 * names, e.g. hash@1, hash@2, etc.
2659 if (strncmp(fit_get_name(fit
, noffset
, NULL
),
2661 strlen(FIT_HASH_NODENAME
)) != 0) {
2662 /* Not a hash subnode, skip it */
2666 if (fit_image_hash_get_algo(fit
, noffset
, &algo
)) {
2667 printf("Can't get hash algo property 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
));
2674 if (calculate_hash(data
, size
, algo
, value
,
2676 printf("Unsupported hash algorithm (%s) for "
2677 "'%s' hash node in '%s' image node\n",
2678 algo
, fit_get_name(fit
, noffset
, NULL
),
2679 fit_get_name(fit
, image_noffset
,
2684 if (fit_image_hash_set_value(fit
, noffset
, value
,
2686 printf("Can't set hash value for "
2687 "'%s' hash node in '%s' image node\n",
2688 fit_get_name(fit
, noffset
, NULL
),
2689 fit_get_name(fit
, image_noffset
, NULL
));
2699 * fit_image_hash_set_value - set hash value in requested has node
2700 * @fit: pointer to the FIT format image header
2701 * @noffset: hash node offset
2702 * @value: hash value to be set
2703 * @value_len: hash value length
2705 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2706 * given and returns operation status to the caller.
2712 int fit_image_hash_set_value(void *fit
, int noffset
, uint8_t *value
,
2717 ret
= fdt_setprop(fit
, noffset
, FIT_VALUE_PROP
, value
, value_len
);
2719 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2720 FIT_VALUE_PROP
, fit_get_name(fit
, noffset
, NULL
),
2727 #endif /* USE_HOSTCC */
2730 * fit_image_check_hashes - verify data intergity
2731 * @fit: pointer to the FIT format image header
2732 * @image_noffset: component image node offset
2734 * fit_image_check_hashes() goes over component image hash nodes,
2735 * re-calculates each data hash and compares with the value stored in hash
2739 * 1, if all hashes are valid
2740 * 0, otherwise (or on error)
2742 int fit_image_check_hashes(const void *fit
, int image_noffset
)
2749 uint8_t value
[FIT_MAX_HASH_LEN
];
2755 /* Get image data and data length */
2756 if (fit_image_get_data(fit
, image_noffset
, &data
, &size
)) {
2757 printf("Can't get image data/size\n");
2761 /* Process all hash subnodes of the component image node */
2762 for (ndepth
= 0, noffset
= fdt_next_node(fit
, image_noffset
, &ndepth
);
2763 (noffset
>= 0) && (ndepth
> 0);
2764 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
2766 /* Direct child node of the component image node */
2769 * Check subnode name, must be equal to "hash".
2770 * Multiple hash nodes require unique unit node
2771 * names, e.g. hash@1, hash@2, etc.
2773 if (strncmp(fit_get_name(fit
, noffset
, NULL
),
2775 strlen(FIT_HASH_NODENAME
)) != 0)
2778 if (fit_image_hash_get_algo(fit
, noffset
, &algo
)) {
2779 err_msg
= " error!\nCan't get hash algo "
2785 if (fit_image_hash_get_value(fit
, noffset
, &fit_value
,
2787 err_msg
= " error!\nCan't get hash value "
2792 if (calculate_hash(data
, size
, algo
, value
,
2794 err_msg
= " error!\n"
2795 "Unsupported hash algorithm";
2799 if (value_len
!= fit_value_len
) {
2800 err_msg
= " error !\nBad hash value len";
2802 } else if (memcmp(value
, fit_value
, value_len
) != 0) {
2803 err_msg
= " error!\nBad hash value";
2813 printf("%s for '%s' hash node in '%s' image node\n",
2814 err_msg
, fit_get_name(fit
, noffset
, NULL
),
2815 fit_get_name(fit
, image_noffset
, NULL
));
2820 * fit_all_image_check_hashes - verify data intergity for all images
2821 * @fit: pointer to the FIT format image header
2823 * fit_all_image_check_hashes() goes over all images in the FIT and
2824 * for every images checks if all it's hashes are valid.
2827 * 1, if all hashes of all images are valid
2828 * 0, otherwise (or on error)
2830 int fit_all_image_check_hashes(const void *fit
)
2837 /* Find images parent node offset */
2838 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
2839 if (images_noffset
< 0) {
2840 printf("Can't find images parent node '%s' (%s)\n",
2841 FIT_IMAGES_PATH
, fdt_strerror(images_noffset
));
2845 /* Process all image subnodes, check hashes for each */
2846 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
2848 for (ndepth
= 0, count
= 0,
2849 noffset
= fdt_next_node(fit
, images_noffset
, &ndepth
);
2850 (noffset
>= 0) && (ndepth
> 0);
2851 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
2854 * Direct child node of the images parent node,
2855 * i.e. component image node.
2857 printf(" Hash(es) for Image %u (%s): ", count
++,
2858 fit_get_name(fit
, noffset
, NULL
));
2860 if (!fit_image_check_hashes(fit
, noffset
))
2869 * fit_image_check_os - check whether image node is of a given os type
2870 * @fit: pointer to the FIT format image header
2871 * @noffset: component image node offset
2872 * @os: requested image os
2874 * fit_image_check_os() reads image os property and compares its numeric
2875 * id with the requested os. Comparison result is returned to the caller.
2878 * 1 if image is of given os type
2879 * 0 otherwise (or on error)
2881 int fit_image_check_os(const void *fit
, int noffset
, uint8_t os
)
2885 if (fit_image_get_os(fit
, noffset
, &image_os
))
2887 return (os
== image_os
);
2891 * fit_image_check_arch - check whether image node is of a given arch
2892 * @fit: pointer to the FIT format image header
2893 * @noffset: component image node offset
2894 * @arch: requested imagearch
2896 * fit_image_check_arch() reads image arch property and compares its numeric
2897 * id with the requested arch. Comparison result is returned to the caller.
2900 * 1 if image is of given arch
2901 * 0 otherwise (or on error)
2903 int fit_image_check_arch(const void *fit
, int noffset
, uint8_t arch
)
2907 if (fit_image_get_arch(fit
, noffset
, &image_arch
))
2909 return (arch
== image_arch
);
2913 * fit_image_check_type - check whether image node is of a given type
2914 * @fit: pointer to the FIT format image header
2915 * @noffset: component image node offset
2916 * @type: requested image type
2918 * fit_image_check_type() reads image type property and compares its numeric
2919 * id with the requested type. Comparison result is returned to the caller.
2922 * 1 if image is of given type
2923 * 0 otherwise (or on error)
2925 int fit_image_check_type(const void *fit
, int noffset
, uint8_t type
)
2929 if (fit_image_get_type(fit
, noffset
, &image_type
))
2931 return (type
== image_type
);
2935 * fit_image_check_comp - check whether image node uses given compression
2936 * @fit: pointer to the FIT format image header
2937 * @noffset: component image node offset
2938 * @comp: requested image compression type
2940 * fit_image_check_comp() reads image compression property and compares its
2941 * numeric id with the requested compression type. Comparison result is
2942 * returned to the caller.
2945 * 1 if image uses requested compression
2946 * 0 otherwise (or on error)
2948 int fit_image_check_comp(const void *fit
, int noffset
, uint8_t comp
)
2952 if (fit_image_get_comp(fit
, noffset
, &image_comp
))
2954 return (comp
== image_comp
);
2958 * fit_check_format - sanity check FIT image format
2959 * @fit: pointer to the FIT format image header
2961 * fit_check_format() runs a basic sanity FIT image verification.
2962 * Routine checks for mandatory properties, nodes, etc.
2968 int fit_check_format(const void *fit
)
2970 /* mandatory / node 'description' property */
2971 if (fdt_getprop(fit
, 0, FIT_DESC_PROP
, NULL
) == NULL
) {
2972 debug("Wrong FIT format: no description\n");
2976 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2977 /* mandatory / node 'timestamp' property */
2978 if (fdt_getprop(fit
, 0, FIT_TIMESTAMP_PROP
, NULL
) == NULL
) {
2979 debug("Wrong FIT format: no timestamp\n");
2984 /* mandatory subimages parent '/images' node */
2985 if (fdt_path_offset(fit
, FIT_IMAGES_PATH
) < 0) {
2986 debug("Wrong FIT format: no images parent node\n");
2994 * fit_conf_get_node - get node offset for configuration of a given unit name
2995 * @fit: pointer to the FIT format image header
2996 * @conf_uname: configuration node unit name
2998 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2999 * parant node) of a provided unit name. If configuration is found its node offset
3000 * is returned to the caller.
3002 * When NULL is provided in second argument fit_conf_get_node() will search
3003 * for a default configuration node instead. Default configuration node unit name
3004 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3007 * configuration node offset when found (>=0)
3008 * negative number on failure (FDT_ERR_* code)
3010 int fit_conf_get_node(const void *fit
, const char *conf_uname
)
3012 int noffset
, confs_noffset
;
3015 confs_noffset
= fdt_path_offset(fit
, FIT_CONFS_PATH
);
3016 if (confs_noffset
< 0) {
3017 debug("Can't find configurations parent node '%s' (%s)\n",
3018 FIT_CONFS_PATH
, fdt_strerror(confs_noffset
));
3019 return confs_noffset
;
3022 if (conf_uname
== NULL
) {
3023 /* get configuration unit name from the default property */
3024 debug("No configuration specified, trying default...\n");
3025 conf_uname
= (char *)fdt_getprop(fit
, confs_noffset
,
3026 FIT_DEFAULT_PROP
, &len
);
3027 if (conf_uname
== NULL
) {
3028 fit_get_debug(fit
, confs_noffset
, FIT_DEFAULT_PROP
,
3032 debug("Found default configuration: '%s'\n", conf_uname
);
3035 noffset
= fdt_subnode_offset(fit
, confs_noffset
, conf_uname
);
3037 debug("Can't get node offset for configuration unit name: "
3039 conf_uname
, fdt_strerror(noffset
));
3045 static int __fit_conf_get_prop_node(const void *fit
, int noffset
,
3046 const char *prop_name
)
3051 /* get kernel image unit name from configuration kernel property */
3052 uname
= (char *)fdt_getprop(fit
, noffset
, prop_name
, &len
);
3056 return fit_image_get_node(fit
, uname
);
3060 * fit_conf_get_kernel_node - get kernel 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_kernel_node() retrives kernel 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_kernel_node(const void *fit
, int noffset
)
3075 return __fit_conf_get_prop_node(fit
, noffset
, FIT_KERNEL_PROP
);
3079 * fit_conf_get_ramdisk_node - get ramdisk 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_ramdisk_node() retrives ramdisk 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_ramdisk_node(const void *fit
, int noffset
)
3094 return __fit_conf_get_prop_node(fit
, noffset
, FIT_RAMDISK_PROP
);
3098 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3099 * a given configuration
3100 * @fit: pointer to the FIT format image header
3101 * @noffset: configuration node offset
3103 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3104 * configuration FIT_KERNEL_PROP property and translates it to the node
3108 * image node offset when found (>=0)
3109 * negative number on failure (FDT_ERR_* code)
3111 int fit_conf_get_fdt_node(const void *fit
, int noffset
)
3113 return __fit_conf_get_prop_node(fit
, noffset
, FIT_FDT_PROP
);
3117 * fit_conf_print - prints out the FIT configuration details
3118 * @fit: pointer to the FIT format image header
3119 * @noffset: offset of the configuration node
3120 * @p: pointer to prefix string
3122 * fit_conf_print() lists all mandatory properies for the processed
3123 * configuration node.
3126 * no returned results
3128 void fit_conf_print(const void *fit
, int noffset
, const char *p
)
3134 /* Mandatory properties */
3135 ret
= fit_get_desc(fit
, noffset
, &desc
);
3136 printf("%s Description: ", p
);
3138 printf("unavailable\n");
3140 printf("%s\n", desc
);
3142 uname
= (char *)fdt_getprop(fit
, noffset
, FIT_KERNEL_PROP
, NULL
);
3143 printf("%s Kernel: ", p
);
3145 printf("unavailable\n");
3147 printf("%s\n", uname
);
3149 /* Optional properties */
3150 uname
= (char *)fdt_getprop(fit
, noffset
, FIT_RAMDISK_PROP
, NULL
);
3152 printf("%s Init Ramdisk: %s\n", p
, uname
);
3154 uname
= (char *)fdt_getprop(fit
, noffset
, FIT_FDT_PROP
, NULL
);
3156 printf("%s FDT: %s\n", p
, uname
);
3160 * fit_check_ramdisk - verify FIT format ramdisk subimage
3161 * @fit_hdr: pointer to the FIT ramdisk header
3162 * @rd_noffset: ramdisk subimage node offset within FIT image
3163 * @arch: requested ramdisk image architecture type
3164 * @verify: data CRC verification flag
3166 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3167 * specified FIT image.
3174 static int fit_check_ramdisk(const void *fit
, int rd_noffset
, uint8_t arch
,
3177 fit_image_print(fit
, rd_noffset
, " ");
3180 puts(" Verifying Hash Integrity ... ");
3181 if (!fit_image_check_hashes(fit
, rd_noffset
)) {
3182 puts("Bad Data Hash\n");
3183 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH
);
3189 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL
);
3190 if (!fit_image_check_os(fit
, rd_noffset
, IH_OS_LINUX
) ||
3191 !fit_image_check_arch(fit
, rd_noffset
, arch
) ||
3192 !fit_image_check_type(fit
, rd_noffset
, IH_TYPE_RAMDISK
)) {
3193 printf("No Linux %s Ramdisk Image\n",
3194 genimg_get_arch_name(arch
));
3195 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL
);
3199 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK
);
3202 #endif /* USE_HOSTCC */
3203 #endif /* CONFIG_FIT */