2 * Copyright (c) 2013, Google Inc.
4 * (C) Copyright 2008 Semihalf
6 * (C) Copyright 2000-2006
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * SPDX-License-Identifier: GPL-2.0+
20 DECLARE_GLOBAL_DATA_PTR
;
21 #endif /* !USE_HOSTCC*/
23 #include <bootstage.h>
24 #include <u-boot/crc.h>
25 #include <u-boot/md5.h>
26 #include <u-boot/sha1.h>
27 #include <u-boot/sha256.h>
29 /*****************************************************************************/
30 /* New uImage format routines */
31 /*****************************************************************************/
33 static int fit_parse_spec(const char *spec
, char sepc
, ulong addr_curr
,
34 ulong
*addr
, const char **name
)
41 sep
= strchr(spec
, sepc
);
44 *addr
= simple_strtoul(spec
, NULL
, 16);
54 * fit_parse_conf - parse FIT configuration spec
55 * @spec: input string, containing configuration spec
56 * @add_curr: current image address (to be used as a possible default)
57 * @addr: pointer to a ulong variable, will hold FIT image address of a given
59 * @conf_name double pointer to a char, will hold pointer to a configuration
62 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
63 * where <addr> is a FIT image address that contains configuration
64 * with a <conf> unit name.
66 * Address part is optional, and if omitted default add_curr will
70 * 1 if spec is a valid configuration string,
71 * addr and conf_name are set accordingly
74 int fit_parse_conf(const char *spec
, ulong addr_curr
,
75 ulong
*addr
, const char **conf_name
)
77 return fit_parse_spec(spec
, '#', addr_curr
, addr
, conf_name
);
81 * fit_parse_subimage - parse FIT subimage spec
82 * @spec: input string, containing subimage spec
83 * @add_curr: current image address (to be used as a possible default)
84 * @addr: pointer to a ulong variable, will hold FIT image address of a given
86 * @image_name: double pointer to a char, will hold pointer to a subimage name
88 * fit_parse_subimage() expects subimage spec in the form of
89 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
90 * subimage with a <subimg> unit name.
92 * Address part is optional, and if omitted default add_curr will
96 * 1 if spec is a valid subimage string,
97 * addr and image_name are set accordingly
100 int fit_parse_subimage(const char *spec
, ulong addr_curr
,
101 ulong
*addr
, const char **image_name
)
103 return fit_parse_spec(spec
, ':', addr_curr
, addr
, image_name
);
105 #endif /* !USE_HOSTCC */
107 static void fit_get_debug(const void *fit
, int noffset
,
108 char *prop_name
, int err
)
110 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
111 prop_name
, (ulong
)fit
, noffset
, fit_get_name(fit
, noffset
, NULL
),
116 * fit_get_subimage_count - get component (sub-image) count
117 * @fit: pointer to the FIT format image header
118 * @images_noffset: offset of images node
121 * number of image components
123 int fit_get_subimage_count(const void *fit
, int images_noffset
)
129 /* Process its subnodes, print out component images details */
130 for (ndepth
= 0, count
= 0,
131 noffset
= fdt_next_node(fit
, images_noffset
, &ndepth
);
132 (noffset
>= 0) && (ndepth
> 0);
133 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
142 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
144 * fit_print_contents - prints out the contents of the FIT format image
145 * @fit: pointer to the FIT format image header
146 * @p: pointer to prefix string
148 * fit_print_contents() formats a multi line FIT image contents description.
149 * The routine prints out FIT image properties (root node level) follwed by
150 * the details of each component image.
153 * no returned results
155 void fit_print_contents(const void *fit
)
168 /* Indent string is defined in header image.h */
169 p
= IMAGE_INDENT_STRING
;
171 /* Root node properties */
172 ret
= fit_get_desc(fit
, 0, &desc
);
173 printf("%sFIT description: ", p
);
175 printf("unavailable\n");
177 printf("%s\n", desc
);
179 if (IMAGE_ENABLE_TIMESTAMP
) {
180 ret
= fit_get_timestamp(fit
, 0, ×tamp
);
181 printf("%sCreated: ", p
);
183 printf("unavailable\n");
185 genimg_print_time(timestamp
);
188 /* Find images parent node offset */
189 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
190 if (images_noffset
< 0) {
191 printf("Can't find images parent node '%s' (%s)\n",
192 FIT_IMAGES_PATH
, fdt_strerror(images_noffset
));
196 /* Process its subnodes, print out component images details */
197 for (ndepth
= 0, count
= 0,
198 noffset
= fdt_next_node(fit
, images_noffset
, &ndepth
);
199 (noffset
>= 0) && (ndepth
> 0);
200 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
203 * Direct child node of the images parent node,
204 * i.e. component image node.
206 printf("%s Image %u (%s)\n", p
, count
++,
207 fit_get_name(fit
, noffset
, NULL
));
209 fit_image_print(fit
, noffset
, p
);
213 /* Find configurations parent node offset */
214 confs_noffset
= fdt_path_offset(fit
, FIT_CONFS_PATH
);
215 if (confs_noffset
< 0) {
216 debug("Can't get configurations parent node '%s' (%s)\n",
217 FIT_CONFS_PATH
, fdt_strerror(confs_noffset
));
221 /* get default configuration unit name from default property */
222 uname
= (char *)fdt_getprop(fit
, noffset
, FIT_DEFAULT_PROP
, NULL
);
224 printf("%s Default Configuration: '%s'\n", p
, uname
);
226 /* Process its subnodes, print out configurations details */
227 for (ndepth
= 0, count
= 0,
228 noffset
= fdt_next_node(fit
, confs_noffset
, &ndepth
);
229 (noffset
>= 0) && (ndepth
> 0);
230 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
233 * Direct child node of the configurations parent node,
234 * i.e. configuration node.
236 printf("%s Configuration %u (%s)\n", p
, count
++,
237 fit_get_name(fit
, noffset
, NULL
));
239 fit_conf_print(fit
, noffset
, p
);
245 * fit_image_print_data() - prints out the hash node details
246 * @fit: pointer to the FIT format image header
247 * @noffset: offset of the hash node
248 * @p: pointer to prefix string
249 * @type: Type of information to print ("hash" or "sign")
251 * fit_image_print_data() lists properies for the processed hash node
253 * This function avoid using puts() since it prints a newline on the host
254 * but does not in U-Boot.
257 * no returned results
259 static void fit_image_print_data(const void *fit
, int noffset
, const char *p
,
269 debug("%s %s node: '%s'\n", p
, type
,
270 fit_get_name(fit
, noffset
, NULL
));
271 printf("%s %s algo: ", p
, type
);
272 if (fit_image_hash_get_algo(fit
, noffset
, &algo
)) {
273 printf("invalid/unsupported\n");
277 keyname
= fdt_getprop(fit
, noffset
, "key-name-hint", NULL
);
278 required
= fdt_getprop(fit
, noffset
, "required", NULL
) != NULL
;
280 printf(":%s", keyname
);
282 printf(" (required)");
285 ret
= fit_image_hash_get_value(fit
, noffset
, &value
,
287 printf("%s %s value: ", p
, type
);
289 printf("unavailable\n");
291 for (i
= 0; i
< value_len
; i
++)
292 printf("%02x", value
[i
]);
296 debug("%s %s len: %d\n", p
, type
, value_len
);
298 /* Signatures have a time stamp */
299 if (IMAGE_ENABLE_TIMESTAMP
&& keyname
) {
302 printf("%s Timestamp: ", p
);
303 if (fit_get_timestamp(fit
, noffset
, ×tamp
))
304 printf("unavailable\n");
306 genimg_print_time(timestamp
);
311 * fit_image_print_verification_data() - prints out the hash/signature details
312 * @fit: pointer to the FIT format image header
313 * @noffset: offset of the hash or signature node
314 * @p: pointer to prefix string
316 * This lists properies for the processed hash node
319 * no returned results
321 static void fit_image_print_verification_data(const void *fit
, int noffset
,
327 * Check subnode name, must be equal to "hash" or "signature".
328 * Multiple hash/signature nodes require unique unit node
329 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
331 name
= fit_get_name(fit
, noffset
, NULL
);
332 if (!strncmp(name
, FIT_HASH_NODENAME
, strlen(FIT_HASH_NODENAME
))) {
333 fit_image_print_data(fit
, noffset
, p
, "Hash");
334 } else if (!strncmp(name
, FIT_SIG_NODENAME
,
335 strlen(FIT_SIG_NODENAME
))) {
336 fit_image_print_data(fit
, noffset
, p
, "Sign");
341 * fit_image_print - prints out the FIT component image details
342 * @fit: pointer to the FIT format image header
343 * @image_noffset: offset of the component image node
344 * @p: pointer to prefix string
346 * fit_image_print() lists all mandatory properies for the processed component
347 * image. If present, hash nodes are printed out as well. Load
348 * address for images of type firmware is also printed out. Since the load
349 * address is not mandatory for firmware images, it will be output as
350 * "unavailable" when not present.
353 * no returned results
355 void fit_image_print(const void *fit
, int image_noffset
, const char *p
)
358 uint8_t type
, arch
, os
, comp
;
366 /* Mandatory properties */
367 ret
= fit_get_desc(fit
, image_noffset
, &desc
);
368 printf("%s Description: ", p
);
370 printf("unavailable\n");
372 printf("%s\n", desc
);
374 if (IMAGE_ENABLE_TIMESTAMP
) {
377 ret
= fit_get_timestamp(fit
, 0, ×tamp
);
378 printf("%s Created: ", p
);
380 printf("unavailable\n");
382 genimg_print_time(timestamp
);
385 fit_image_get_type(fit
, image_noffset
, &type
);
386 printf("%s Type: %s\n", p
, genimg_get_type_name(type
));
388 fit_image_get_comp(fit
, image_noffset
, &comp
);
389 printf("%s Compression: %s\n", p
, genimg_get_comp_name(comp
));
391 ret
= fit_image_get_data(fit
, image_noffset
, &data
, &size
);
394 printf("%s Data Start: ", p
);
396 printf("unavailable\n");
398 void *vdata
= (void *)data
;
400 printf("0x%08lx\n", (ulong
)map_to_sysmem(vdata
));
404 printf("%s Data Size: ", p
);
406 printf("unavailable\n");
408 genimg_print_size(size
);
410 /* Remaining, type dependent properties */
411 if ((type
== IH_TYPE_KERNEL
) || (type
== IH_TYPE_STANDALONE
) ||
412 (type
== IH_TYPE_RAMDISK
) || (type
== IH_TYPE_FIRMWARE
) ||
413 (type
== IH_TYPE_FLATDT
)) {
414 fit_image_get_arch(fit
, image_noffset
, &arch
);
415 printf("%s Architecture: %s\n", p
, genimg_get_arch_name(arch
));
418 if ((type
== IH_TYPE_KERNEL
) || (type
== IH_TYPE_RAMDISK
)) {
419 fit_image_get_os(fit
, image_noffset
, &os
);
420 printf("%s OS: %s\n", p
, genimg_get_os_name(os
));
423 if ((type
== IH_TYPE_KERNEL
) || (type
== IH_TYPE_STANDALONE
) ||
424 (type
== IH_TYPE_FIRMWARE
) || (type
== IH_TYPE_RAMDISK
)) {
425 ret
= fit_image_get_load(fit
, image_noffset
, &load
);
426 printf("%s Load Address: ", p
);
428 printf("unavailable\n");
430 printf("0x%08lx\n", load
);
433 if ((type
== IH_TYPE_KERNEL
) || (type
== IH_TYPE_STANDALONE
) ||
434 (type
== IH_TYPE_RAMDISK
)) {
435 fit_image_get_entry(fit
, image_noffset
, &entry
);
436 printf("%s Entry Point: ", p
);
438 printf("unavailable\n");
440 printf("0x%08lx\n", entry
);
443 /* Process all hash subnodes of the component image node */
444 for (ndepth
= 0, noffset
= fdt_next_node(fit
, image_noffset
, &ndepth
);
445 (noffset
>= 0) && (ndepth
> 0);
446 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
448 /* Direct child node of the component image node */
449 fit_image_print_verification_data(fit
, noffset
, p
);
454 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
457 * fit_get_desc - get node description property
458 * @fit: pointer to the FIT format image header
459 * @noffset: node offset
460 * @desc: double pointer to the char, will hold pointer to the descrption
462 * fit_get_desc() reads description property from a given node, if
463 * description is found pointer to it is returened in third call argument.
469 int fit_get_desc(const void *fit
, int noffset
, char **desc
)
473 *desc
= (char *)fdt_getprop(fit
, noffset
, FIT_DESC_PROP
, &len
);
475 fit_get_debug(fit
, noffset
, FIT_DESC_PROP
, len
);
483 * fit_get_timestamp - get node timestamp property
484 * @fit: pointer to the FIT format image header
485 * @noffset: node offset
486 * @timestamp: pointer to the time_t, will hold read timestamp
488 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
489 * is found and has a correct size its value is retured in third call
494 * -1, on property read failure
495 * -2, on wrong timestamp size
497 int fit_get_timestamp(const void *fit
, int noffset
, time_t *timestamp
)
502 data
= fdt_getprop(fit
, noffset
, FIT_TIMESTAMP_PROP
, &len
);
504 fit_get_debug(fit
, noffset
, FIT_TIMESTAMP_PROP
, len
);
507 if (len
!= sizeof(uint32_t)) {
508 debug("FIT timestamp with incorrect size of (%u)\n", len
);
512 *timestamp
= uimage_to_cpu(*((uint32_t *)data
));
517 * fit_image_get_node - get node offset for component image of a given unit name
518 * @fit: pointer to the FIT format image header
519 * @image_uname: component image node unit name
521 * fit_image_get_node() finds a component image (withing the '/images'
522 * node) of a provided unit name. If image is found its node offset is
523 * returned to the caller.
526 * image node offset when found (>=0)
527 * negative number on failure (FDT_ERR_* code)
529 int fit_image_get_node(const void *fit
, const char *image_uname
)
531 int noffset
, images_noffset
;
533 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
534 if (images_noffset
< 0) {
535 debug("Can't find images parent node '%s' (%s)\n",
536 FIT_IMAGES_PATH
, fdt_strerror(images_noffset
));
537 return images_noffset
;
540 noffset
= fdt_subnode_offset(fit
, images_noffset
, image_uname
);
542 debug("Can't get node offset for image unit name: '%s' (%s)\n",
543 image_uname
, fdt_strerror(noffset
));
550 * fit_image_get_os - get os id for a given component image node
551 * @fit: pointer to the FIT format image header
552 * @noffset: component image node offset
553 * @os: pointer to the uint8_t, will hold os numeric id
555 * fit_image_get_os() finds os property in a given component image node.
556 * If the property is found, its (string) value is translated to the numeric
557 * id which is returned to the caller.
563 int fit_image_get_os(const void *fit
, int noffset
, uint8_t *os
)
568 /* Get OS name from property data */
569 data
= fdt_getprop(fit
, noffset
, FIT_OS_PROP
, &len
);
571 fit_get_debug(fit
, noffset
, FIT_OS_PROP
, len
);
576 /* Translate OS name to id */
577 *os
= genimg_get_os_id(data
);
582 * fit_image_get_arch - get arch id for a given component image node
583 * @fit: pointer to the FIT format image header
584 * @noffset: component image node offset
585 * @arch: pointer to the uint8_t, will hold arch numeric id
587 * fit_image_get_arch() finds arch property in a given component image node.
588 * If the property is found, its (string) value is translated to the numeric
589 * id which is returned to the caller.
595 int fit_image_get_arch(const void *fit
, int noffset
, uint8_t *arch
)
600 /* Get architecture name from property data */
601 data
= fdt_getprop(fit
, noffset
, FIT_ARCH_PROP
, &len
);
603 fit_get_debug(fit
, noffset
, FIT_ARCH_PROP
, len
);
608 /* Translate architecture name to id */
609 *arch
= genimg_get_arch_id(data
);
614 * fit_image_get_type - get type id for a given component image node
615 * @fit: pointer to the FIT format image header
616 * @noffset: component image node offset
617 * @type: pointer to the uint8_t, will hold type numeric id
619 * fit_image_get_type() finds type property in a given component image node.
620 * If the property is found, its (string) value is translated to the numeric
621 * id which is returned to the caller.
627 int fit_image_get_type(const void *fit
, int noffset
, uint8_t *type
)
632 /* Get image type name from property data */
633 data
= fdt_getprop(fit
, noffset
, FIT_TYPE_PROP
, &len
);
635 fit_get_debug(fit
, noffset
, FIT_TYPE_PROP
, len
);
640 /* Translate image type name to id */
641 *type
= genimg_get_type_id(data
);
646 * fit_image_get_comp - get comp id for a given component image node
647 * @fit: pointer to the FIT format image header
648 * @noffset: component image node offset
649 * @comp: pointer to the uint8_t, will hold comp numeric id
651 * fit_image_get_comp() finds comp property in a given component image node.
652 * If the property is found, its (string) value is translated to the numeric
653 * id which is returned to the caller.
659 int fit_image_get_comp(const void *fit
, int noffset
, uint8_t *comp
)
664 /* Get compression name from property data */
665 data
= fdt_getprop(fit
, noffset
, FIT_COMP_PROP
, &len
);
667 fit_get_debug(fit
, noffset
, FIT_COMP_PROP
, len
);
672 /* Translate compression name to id */
673 *comp
= genimg_get_comp_id(data
);
678 * fit_image_get_load() - get load addr property for given component image node
679 * @fit: pointer to the FIT format image header
680 * @noffset: component image node offset
681 * @load: pointer to the uint32_t, will hold load address
683 * fit_image_get_load() finds load address property in a given component
684 * image node. If the property is found, its value is returned to the caller.
690 int fit_image_get_load(const void *fit
, int noffset
, ulong
*load
)
693 const uint32_t *data
;
695 data
= fdt_getprop(fit
, noffset
, FIT_LOAD_PROP
, &len
);
697 fit_get_debug(fit
, noffset
, FIT_LOAD_PROP
, len
);
701 *load
= uimage_to_cpu(*data
);
706 * fit_image_get_entry() - get entry point address property
707 * @fit: pointer to the FIT format image header
708 * @noffset: component image node offset
709 * @entry: pointer to the uint32_t, will hold entry point address
711 * This gets the entry point address property for a given component image
714 * fit_image_get_entry() finds entry point address property in a given
715 * component image node. If the property is found, its value is returned
722 int fit_image_get_entry(const void *fit
, int noffset
, ulong
*entry
)
725 const uint32_t *data
;
727 data
= fdt_getprop(fit
, noffset
, FIT_ENTRY_PROP
, &len
);
729 fit_get_debug(fit
, noffset
, FIT_ENTRY_PROP
, len
);
733 *entry
= uimage_to_cpu(*data
);
738 * fit_image_get_data - get data property and its size for a given component image node
739 * @fit: pointer to the FIT format image header
740 * @noffset: component image node offset
741 * @data: double pointer to void, will hold data property's data address
742 * @size: pointer to size_t, will hold data property's data size
744 * fit_image_get_data() finds data property in a given component image node.
745 * If the property is found its data start address and size are returned to
752 int fit_image_get_data(const void *fit
, int noffset
,
753 const void **data
, size_t *size
)
757 *data
= fdt_getprop(fit
, noffset
, FIT_DATA_PROP
, &len
);
759 fit_get_debug(fit
, noffset
, FIT_DATA_PROP
, len
);
769 * fit_image_hash_get_algo - get hash algorithm name
770 * @fit: pointer to the FIT format image header
771 * @noffset: hash node offset
772 * @algo: double pointer to char, will hold pointer to the algorithm name
774 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
775 * If the property is found its data start address is returned to the caller.
781 int fit_image_hash_get_algo(const void *fit
, int noffset
, char **algo
)
785 *algo
= (char *)fdt_getprop(fit
, noffset
, FIT_ALGO_PROP
, &len
);
787 fit_get_debug(fit
, noffset
, FIT_ALGO_PROP
, len
);
795 * fit_image_hash_get_value - get hash value and length
796 * @fit: pointer to the FIT format image header
797 * @noffset: hash node offset
798 * @value: double pointer to uint8_t, will hold address of a hash value data
799 * @value_len: pointer to an int, will hold hash data length
801 * fit_image_hash_get_value() finds hash value property in a given hash node.
802 * If the property is found its data start address and size are returned to
809 int fit_image_hash_get_value(const void *fit
, int noffset
, uint8_t **value
,
814 *value
= (uint8_t *)fdt_getprop(fit
, noffset
, FIT_VALUE_PROP
, &len
);
815 if (*value
== NULL
) {
816 fit_get_debug(fit
, noffset
, FIT_VALUE_PROP
, len
);
826 * fit_image_hash_get_ignore - get hash ignore flag
827 * @fit: pointer to the FIT format image header
828 * @noffset: hash node offset
829 * @ignore: pointer to an int, will hold hash ignore flag
831 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
832 * If the property is found and non-zero, the hash algorithm is not verified by
833 * u-boot automatically.
836 * 0, on ignore not found
837 * value, on ignore found
839 static int fit_image_hash_get_ignore(const void *fit
, int noffset
, int *ignore
)
844 value
= (int *)fdt_getprop(fit
, noffset
, FIT_IGNORE_PROP
, &len
);
845 if (value
== NULL
|| len
!= sizeof(int))
854 * fit_set_timestamp - set node timestamp property
855 * @fit: pointer to the FIT format image header
856 * @noffset: node offset
857 * @timestamp: timestamp value to be set
859 * fit_set_timestamp() attempts to set timestamp property in the requested
860 * node and returns operation status to the caller.
864 * -ENOSPC if no space in device tree, -1 for other error
866 int fit_set_timestamp(void *fit
, int noffset
, time_t timestamp
)
871 t
= cpu_to_uimage(timestamp
);
872 ret
= fdt_setprop(fit
, noffset
, FIT_TIMESTAMP_PROP
, &t
,
875 printf("Can't set '%s' property for '%s' node (%s)\n",
876 FIT_TIMESTAMP_PROP
, fit_get_name(fit
, noffset
, NULL
),
878 return ret
== -FDT_ERR_NOSPACE
? -ENOSPC
: -1;
885 * calculate_hash - calculate and return hash for provided input data
886 * @data: pointer to the input data
887 * @data_len: data length
888 * @algo: requested hash algorithm
889 * @value: pointer to the char, will hold hash value data (caller must
890 * allocate enough free space)
891 * value_len: length of the calculated hash
893 * calculate_hash() computes input data hash according to the requested
895 * Resulting hash value is placed in caller provided 'value' buffer, length
896 * of the calculated hash is returned via value_len pointer argument.
900 * -1, when algo is unsupported
902 int calculate_hash(const void *data
, int data_len
, const char *algo
,
903 uint8_t *value
, int *value_len
)
905 if (IMAGE_ENABLE_CRC32
&& strcmp(algo
, "crc32") == 0) {
906 *((uint32_t *)value
) = crc32_wd(0, data
, data_len
,
908 *((uint32_t *)value
) = cpu_to_uimage(*((uint32_t *)value
));
910 } else if (IMAGE_ENABLE_SHA1
&& strcmp(algo
, "sha1") == 0) {
911 sha1_csum_wd((unsigned char *)data
, data_len
,
912 (unsigned char *)value
, CHUNKSZ_SHA1
);
914 } else if (IMAGE_ENABLE_SHA256
&& strcmp(algo
, "sha256") == 0) {
915 sha256_csum_wd((unsigned char *)data
, data_len
,
916 (unsigned char *)value
, CHUNKSZ_SHA256
);
917 *value_len
= SHA256_SUM_LEN
;
918 } else if (IMAGE_ENABLE_MD5
&& strcmp(algo
, "md5") == 0) {
919 md5_wd((unsigned char *)data
, data_len
, value
, CHUNKSZ_MD5
);
922 debug("Unsupported hash alogrithm\n");
928 static int fit_image_check_hash(const void *fit
, int noffset
, const void *data
,
929 size_t size
, char **err_msgp
)
931 uint8_t value
[FIT_MAX_HASH_LEN
];
940 if (fit_image_hash_get_algo(fit
, noffset
, &algo
)) {
941 *err_msgp
= "Can't get hash algo property";
946 if (IMAGE_ENABLE_IGNORE
) {
947 fit_image_hash_get_ignore(fit
, noffset
, &ignore
);
954 if (fit_image_hash_get_value(fit
, noffset
, &fit_value
,
956 *err_msgp
= "Can't get hash value property";
960 if (calculate_hash(data
, size
, algo
, value
, &value_len
)) {
961 *err_msgp
= "Unsupported hash algorithm";
965 if (value_len
!= fit_value_len
) {
966 *err_msgp
= "Bad hash value len";
968 } else if (memcmp(value
, fit_value
, value_len
) != 0) {
969 *err_msgp
= "Bad hash value";
977 * fit_image_verify - verify data intergity
978 * @fit: pointer to the FIT format image header
979 * @image_noffset: component image node offset
981 * fit_image_verify() goes over component image hash nodes,
982 * re-calculates each data hash and compares with the value stored in hash
986 * 1, if all hashes are valid
987 * 0, otherwise (or on error)
989 int fit_image_verify(const void *fit
, int image_noffset
)
998 /* Get image data and data length */
999 if (fit_image_get_data(fit
, image_noffset
, &data
, &size
)) {
1000 err_msg
= "Can't get image data/size";
1004 /* Verify all required signatures */
1005 if (IMAGE_ENABLE_VERIFY
&&
1006 fit_image_verify_required_sigs(fit
, image_noffset
, data
, size
,
1007 gd_fdt_blob(), &verify_all
)) {
1008 err_msg
= "Unable to verify required signature";
1012 /* Process all hash subnodes of the component image node */
1013 for (noffset
= fdt_first_subnode(fit
, image_noffset
);
1015 noffset
= fdt_next_subnode(fit
, noffset
)) {
1016 const char *name
= fit_get_name(fit
, noffset
, NULL
);
1019 * Check subnode name, must be equal to "hash".
1020 * Multiple hash nodes require unique unit node
1021 * names, e.g. hash@1, hash@2, etc.
1023 if (!strncmp(name
, FIT_HASH_NODENAME
,
1024 strlen(FIT_HASH_NODENAME
))) {
1025 if (fit_image_check_hash(fit
, noffset
, data
, size
,
1029 } else if (IMAGE_ENABLE_VERIFY
&& verify_all
&&
1030 !strncmp(name
, FIT_SIG_NODENAME
,
1031 strlen(FIT_SIG_NODENAME
))) {
1032 ret
= fit_image_check_sig(fit
, noffset
, data
,
1033 size
, -1, &err_msg
);
1041 if (noffset
== -FDT_ERR_TRUNCATED
|| noffset
== -FDT_ERR_BADSTRUCTURE
) {
1042 err_msg
= "Corrupted or truncated tree";
1049 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1050 err_msg
, fit_get_name(fit
, noffset
, NULL
),
1051 fit_get_name(fit
, image_noffset
, NULL
));
1056 * fit_all_image_verify - verify data intergity for all images
1057 * @fit: pointer to the FIT format image header
1059 * fit_all_image_verify() goes over all images in the FIT and
1060 * for every images checks if all it's hashes are valid.
1063 * 1, if all hashes of all images are valid
1064 * 0, otherwise (or on error)
1066 int fit_all_image_verify(const void *fit
)
1073 /* Find images parent node offset */
1074 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
1075 if (images_noffset
< 0) {
1076 printf("Can't find images parent node '%s' (%s)\n",
1077 FIT_IMAGES_PATH
, fdt_strerror(images_noffset
));
1081 /* Process all image subnodes, check hashes for each */
1082 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1084 for (ndepth
= 0, count
= 0,
1085 noffset
= fdt_next_node(fit
, images_noffset
, &ndepth
);
1086 (noffset
>= 0) && (ndepth
> 0);
1087 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
1090 * Direct child node of the images parent node,
1091 * i.e. component image node.
1093 printf(" Hash(es) for Image %u (%s): ", count
++,
1094 fit_get_name(fit
, noffset
, NULL
));
1096 if (!fit_image_verify(fit
, noffset
))
1105 * fit_image_check_os - check whether image node is of a given os type
1106 * @fit: pointer to the FIT format image header
1107 * @noffset: component image node offset
1108 * @os: requested image os
1110 * fit_image_check_os() reads image os property and compares its numeric
1111 * id with the requested os. Comparison result is returned to the caller.
1114 * 1 if image is of given os type
1115 * 0 otherwise (or on error)
1117 int fit_image_check_os(const void *fit
, int noffset
, uint8_t os
)
1121 if (fit_image_get_os(fit
, noffset
, &image_os
))
1123 return (os
== image_os
);
1127 * fit_image_check_arch - check whether image node is of a given arch
1128 * @fit: pointer to the FIT format image header
1129 * @noffset: component image node offset
1130 * @arch: requested imagearch
1132 * fit_image_check_arch() reads image arch property and compares its numeric
1133 * id with the requested arch. Comparison result is returned to the caller.
1136 * 1 if image is of given arch
1137 * 0 otherwise (or on error)
1139 int fit_image_check_arch(const void *fit
, int noffset
, uint8_t arch
)
1143 if (fit_image_get_arch(fit
, noffset
, &image_arch
))
1145 return (arch
== image_arch
) ||
1146 (arch
== IH_ARCH_I386
&& image_arch
== IH_ARCH_X86_64
);
1150 * fit_image_check_type - check whether image node is of a given type
1151 * @fit: pointer to the FIT format image header
1152 * @noffset: component image node offset
1153 * @type: requested image type
1155 * fit_image_check_type() reads image type property and compares its numeric
1156 * id with the requested type. Comparison result is returned to the caller.
1159 * 1 if image is of given type
1160 * 0 otherwise (or on error)
1162 int fit_image_check_type(const void *fit
, int noffset
, uint8_t type
)
1166 if (fit_image_get_type(fit
, noffset
, &image_type
))
1168 return (type
== image_type
);
1172 * fit_image_check_comp - check whether image node uses given compression
1173 * @fit: pointer to the FIT format image header
1174 * @noffset: component image node offset
1175 * @comp: requested image compression type
1177 * fit_image_check_comp() reads image compression property and compares its
1178 * numeric id with the requested compression type. Comparison result is
1179 * returned to the caller.
1182 * 1 if image uses requested compression
1183 * 0 otherwise (or on error)
1185 int fit_image_check_comp(const void *fit
, int noffset
, uint8_t comp
)
1189 if (fit_image_get_comp(fit
, noffset
, &image_comp
))
1191 return (comp
== image_comp
);
1195 * fit_check_format - sanity check FIT image format
1196 * @fit: pointer to the FIT format image header
1198 * fit_check_format() runs a basic sanity FIT image verification.
1199 * Routine checks for mandatory properties, nodes, etc.
1205 int fit_check_format(const void *fit
)
1207 /* mandatory / node 'description' property */
1208 if (fdt_getprop(fit
, 0, FIT_DESC_PROP
, NULL
) == NULL
) {
1209 debug("Wrong FIT format: no description\n");
1213 if (IMAGE_ENABLE_TIMESTAMP
) {
1214 /* mandatory / node 'timestamp' property */
1215 if (fdt_getprop(fit
, 0, FIT_TIMESTAMP_PROP
, NULL
) == NULL
) {
1216 debug("Wrong FIT format: no timestamp\n");
1221 /* mandatory subimages parent '/images' node */
1222 if (fdt_path_offset(fit
, FIT_IMAGES_PATH
) < 0) {
1223 debug("Wrong FIT format: no images parent node\n");
1232 * fit_conf_find_compat
1233 * @fit: pointer to the FIT format image header
1234 * @fdt: pointer to the device tree to compare against
1236 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1237 * most compatible with the passed in device tree.
1246 * |-o configurations
1254 * |-compatible = "foo,bar", "bim,bam"
1257 * |-compatible = "foo,bar",
1260 * |-compatible = "bim,bam", "baz,biz"
1262 * Configuration 1 would be picked because the first string in U-Boot's
1263 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1264 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1267 * offset to the configuration to use if one was found
1270 int fit_conf_find_compat(const void *fit
, const void *fdt
)
1273 int noffset
, confs_noffset
, images_noffset
;
1274 const void *fdt_compat
;
1276 int best_match_offset
= 0;
1277 int best_match_pos
= 0;
1279 confs_noffset
= fdt_path_offset(fit
, FIT_CONFS_PATH
);
1280 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
1281 if (confs_noffset
< 0 || images_noffset
< 0) {
1282 debug("Can't find configurations or images nodes.\n");
1286 fdt_compat
= fdt_getprop(fdt
, 0, "compatible", &fdt_compat_len
);
1288 debug("Fdt for comparison has no \"compatible\" property.\n");
1293 * Loop over the configurations in the FIT image.
1295 for (noffset
= fdt_next_node(fit
, confs_noffset
, &ndepth
);
1296 (noffset
>= 0) && (ndepth
> 0);
1297 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
1299 const char *kfdt_name
;
1301 const char *cur_fdt_compat
;
1309 kfdt_name
= fdt_getprop(fit
, noffset
, "fdt", &len
);
1311 debug("No fdt property found.\n");
1314 kfdt_noffset
= fdt_subnode_offset(fit
, images_noffset
,
1316 if (kfdt_noffset
< 0) {
1317 debug("No image node named \"%s\" found.\n",
1322 * Get a pointer to this configuration's fdt.
1324 if (fit_image_get_data(fit
, kfdt_noffset
, &kfdt
, &size
)) {
1325 debug("Failed to get fdt \"%s\".\n", kfdt_name
);
1329 len
= fdt_compat_len
;
1330 cur_fdt_compat
= fdt_compat
;
1332 * Look for a match for each U-Boot compatibility string in
1333 * turn in this configuration's fdt.
1335 for (i
= 0; len
> 0 &&
1336 (!best_match_offset
|| best_match_pos
> i
); i
++) {
1337 int cur_len
= strlen(cur_fdt_compat
) + 1;
1339 if (!fdt_node_check_compatible(kfdt
, 0,
1341 best_match_offset
= noffset
;
1346 cur_fdt_compat
+= cur_len
;
1349 if (!best_match_offset
) {
1350 debug("No match found.\n");
1354 return best_match_offset
;
1358 * fit_conf_get_node - get node offset for configuration of a given unit name
1359 * @fit: pointer to the FIT format image header
1360 * @conf_uname: configuration node unit name
1362 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1363 * parant node) of a provided unit name. If configuration is found its node
1364 * offset is returned to the caller.
1366 * When NULL is provided in second argument fit_conf_get_node() will search
1367 * for a default configuration node instead. Default configuration node unit
1368 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1372 * configuration node offset when found (>=0)
1373 * negative number on failure (FDT_ERR_* code)
1375 int fit_conf_get_node(const void *fit
, const char *conf_uname
)
1377 int noffset
, confs_noffset
;
1380 confs_noffset
= fdt_path_offset(fit
, FIT_CONFS_PATH
);
1381 if (confs_noffset
< 0) {
1382 debug("Can't find configurations parent node '%s' (%s)\n",
1383 FIT_CONFS_PATH
, fdt_strerror(confs_noffset
));
1384 return confs_noffset
;
1387 if (conf_uname
== NULL
) {
1388 /* get configuration unit name from the default property */
1389 debug("No configuration specified, trying default...\n");
1390 conf_uname
= (char *)fdt_getprop(fit
, confs_noffset
,
1391 FIT_DEFAULT_PROP
, &len
);
1392 if (conf_uname
== NULL
) {
1393 fit_get_debug(fit
, confs_noffset
, FIT_DEFAULT_PROP
,
1397 debug("Found default configuration: '%s'\n", conf_uname
);
1400 noffset
= fdt_subnode_offset(fit
, confs_noffset
, conf_uname
);
1402 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1403 conf_uname
, fdt_strerror(noffset
));
1409 int fit_conf_get_prop_node(const void *fit
, int noffset
,
1410 const char *prop_name
)
1415 /* get kernel image unit name from configuration kernel property */
1416 uname
= (char *)fdt_getprop(fit
, noffset
, prop_name
, &len
);
1420 return fit_image_get_node(fit
, uname
);
1424 * fit_conf_print - prints out the FIT configuration details
1425 * @fit: pointer to the FIT format image header
1426 * @noffset: offset of the configuration node
1427 * @p: pointer to prefix string
1429 * fit_conf_print() lists all mandatory properies for the processed
1430 * configuration node.
1433 * no returned results
1435 void fit_conf_print(const void *fit
, int noffset
, const char *p
)
1441 /* Mandatory properties */
1442 ret
= fit_get_desc(fit
, noffset
, &desc
);
1443 printf("%s Description: ", p
);
1445 printf("unavailable\n");
1447 printf("%s\n", desc
);
1449 uname
= (char *)fdt_getprop(fit
, noffset
, FIT_KERNEL_PROP
, NULL
);
1450 printf("%s Kernel: ", p
);
1452 printf("unavailable\n");
1454 printf("%s\n", uname
);
1456 /* Optional properties */
1457 uname
= (char *)fdt_getprop(fit
, noffset
, FIT_RAMDISK_PROP
, NULL
);
1459 printf("%s Init Ramdisk: %s\n", p
, uname
);
1461 uname
= (char *)fdt_getprop(fit
, noffset
, FIT_FDT_PROP
, NULL
);
1463 printf("%s FDT: %s\n", p
, uname
);
1466 static int fit_image_select(const void *fit
, int rd_noffset
, int verify
)
1468 fit_image_print(fit
, rd_noffset
, " ");
1471 puts(" Verifying Hash Integrity ... ");
1472 if (!fit_image_verify(fit
, rd_noffset
)) {
1473 puts("Bad Data Hash\n");
1482 int fit_get_node_from_config(bootm_headers_t
*images
, const char *prop_name
,
1489 debug("* %s: using config '%s' from image at 0x%08lx\n",
1490 prop_name
, images
->fit_uname_cfg
, addr
);
1492 /* Check whether configuration has this property defined */
1493 fit_hdr
= map_sysmem(addr
, 0);
1494 cfg_noffset
= fit_conf_get_node(fit_hdr
, images
->fit_uname_cfg
);
1495 if (cfg_noffset
< 0) {
1496 debug("* %s: no such config\n", prop_name
);
1500 noffset
= fit_conf_get_prop_node(fit_hdr
, cfg_noffset
, prop_name
);
1502 debug("* %s: no '%s' in config\n", prop_name
, prop_name
);
1510 * fit_get_image_type_property() - get property name for IH_TYPE_...
1512 * @return the properly name where we expect to find the image in the
1515 static const char *fit_get_image_type_property(int type
)
1518 * This is sort-of available in the uimage_type[] table in image.c
1519 * but we don't have access to the sohrt name, and "fdt" is different
1520 * anyway. So let's just keep it here.
1523 case IH_TYPE_FLATDT
:
1524 return FIT_FDT_PROP
;
1525 case IH_TYPE_KERNEL
:
1526 return FIT_KERNEL_PROP
;
1527 case IH_TYPE_RAMDISK
:
1528 return FIT_RAMDISK_PROP
;
1529 case IH_TYPE_X86_SETUP
:
1530 return FIT_SETUP_PROP
;
1536 int fit_image_load(bootm_headers_t
*images
, ulong addr
,
1537 const char **fit_unamep
, const char **fit_uname_configp
,
1538 int arch
, int image_type
, int bootstage_id
,
1539 enum fit_load_op load_op
, ulong
*datap
, ulong
*lenp
)
1541 int cfg_noffset
, noffset
;
1542 const char *fit_uname
;
1543 const char *fit_uname_config
;
1548 ulong load
, data
, len
;
1550 const char *prop_name
;
1553 fit
= map_sysmem(addr
, 0);
1554 fit_uname
= fit_unamep
? *fit_unamep
: NULL
;
1555 fit_uname_config
= fit_uname_configp
? *fit_uname_configp
: NULL
;
1556 prop_name
= fit_get_image_type_property(image_type
);
1557 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name
, addr
);
1559 bootstage_mark(bootstage_id
+ BOOTSTAGE_SUB_FORMAT
);
1560 if (!fit_check_format(fit
)) {
1561 printf("Bad FIT %s image format!\n", prop_name
);
1562 bootstage_error(bootstage_id
+ BOOTSTAGE_SUB_FORMAT
);
1565 bootstage_mark(bootstage_id
+ BOOTSTAGE_SUB_FORMAT_OK
);
1567 /* get FIT component image node offset */
1568 bootstage_mark(bootstage_id
+ BOOTSTAGE_SUB_UNIT_NAME
);
1569 noffset
= fit_image_get_node(fit
, fit_uname
);
1572 * no image node unit name, try to get config
1573 * node first. If config unit node name is NULL
1574 * fit_conf_get_node() will try to find default config node
1576 bootstage_mark(bootstage_id
+ BOOTSTAGE_SUB_NO_UNIT_NAME
);
1577 if (IMAGE_ENABLE_BEST_MATCH
&& !fit_uname_config
) {
1578 cfg_noffset
= fit_conf_find_compat(fit
, gd_fdt_blob());
1580 cfg_noffset
= fit_conf_get_node(fit
,
1583 if (cfg_noffset
< 0) {
1584 puts("Could not find configuration node\n");
1585 bootstage_error(bootstage_id
+
1586 BOOTSTAGE_SUB_NO_UNIT_NAME
);
1589 fit_uname_config
= fdt_get_name(fit
, cfg_noffset
, NULL
);
1590 printf(" Using '%s' configuration\n", fit_uname_config
);
1591 if (image_type
== IH_TYPE_KERNEL
) {
1592 /* Remember (and possibly verify) this config */
1593 images
->fit_uname_cfg
= fit_uname_config
;
1594 if (IMAGE_ENABLE_VERIFY
&& images
->verify
) {
1595 puts(" Verifying Hash Integrity ... ");
1596 if (fit_config_verify(fit
, cfg_noffset
)) {
1597 puts("Bad Data Hash\n");
1598 bootstage_error(bootstage_id
+
1599 BOOTSTAGE_SUB_HASH
);
1604 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG
);
1607 noffset
= fit_conf_get_prop_node(fit
, cfg_noffset
,
1609 fit_uname
= fit_get_name(fit
, noffset
, NULL
);
1612 puts("Could not find subimage node\n");
1613 bootstage_error(bootstage_id
+ BOOTSTAGE_SUB_SUBNODE
);
1617 printf(" Trying '%s' %s subimage\n", fit_uname
, prop_name
);
1619 ret
= fit_image_select(fit
, noffset
, images
->verify
);
1621 bootstage_error(bootstage_id
+ BOOTSTAGE_SUB_HASH
);
1625 bootstage_mark(bootstage_id
+ BOOTSTAGE_SUB_CHECK_ARCH
);
1626 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1627 if (!fit_image_check_target_arch(fit
, noffset
)) {
1628 puts("Unsupported Architecture\n");
1629 bootstage_error(bootstage_id
+ BOOTSTAGE_SUB_CHECK_ARCH
);
1633 if (image_type
== IH_TYPE_FLATDT
&&
1634 !fit_image_check_comp(fit
, noffset
, IH_COMP_NONE
)) {
1635 puts("FDT image is compressed");
1636 return -EPROTONOSUPPORT
;
1639 bootstage_mark(bootstage_id
+ BOOTSTAGE_SUB_CHECK_ALL
);
1640 type_ok
= fit_image_check_type(fit
, noffset
, image_type
) ||
1641 (image_type
== IH_TYPE_KERNEL
&&
1642 fit_image_check_type(fit
, noffset
,
1643 IH_TYPE_KERNEL_NOLOAD
));
1645 os_ok
= image_type
== IH_TYPE_FLATDT
||
1646 fit_image_check_os(fit
, noffset
, IH_OS_LINUX
) ||
1647 fit_image_check_os(fit
, noffset
, IH_OS_OPENRTOS
);
1648 if (!type_ok
|| !os_ok
) {
1649 fit_image_get_os(fit
, noffset
, &os
);
1650 printf("No %s %s %s Image\n",
1651 genimg_get_os_name(os
),
1652 genimg_get_arch_name(arch
),
1653 genimg_get_type_name(image_type
));
1654 bootstage_error(bootstage_id
+ BOOTSTAGE_SUB_CHECK_ALL
);
1658 bootstage_mark(bootstage_id
+ BOOTSTAGE_SUB_CHECK_ALL_OK
);
1660 /* get image data address and length */
1661 if (fit_image_get_data(fit
, noffset
, &buf
, &size
)) {
1662 printf("Could not find %s subimage data!\n", prop_name
);
1663 bootstage_error(bootstage_id
+ BOOTSTAGE_SUB_GET_DATA
);
1668 /* verify that image data is a proper FDT blob */
1669 if (image_type
== IH_TYPE_FLATDT
&& fdt_check_header(buf
)) {
1670 puts("Subimage data is not a FDT");
1674 bootstage_mark(bootstage_id
+ BOOTSTAGE_SUB_GET_DATA_OK
);
1677 * Work-around for eldk-4.2 which gives this warning if we try to
1678 * cast in the unmap_sysmem() call:
1679 * warning: initialization discards qualifiers from pointer target type
1682 void *vbuf
= (void *)buf
;
1684 data
= map_to_sysmem(vbuf
);
1687 if (load_op
== FIT_LOAD_IGNORED
) {
1689 } else if (fit_image_get_load(fit
, noffset
, &load
)) {
1690 if (load_op
== FIT_LOAD_REQUIRED
) {
1691 printf("Can't get %s subimage load address!\n",
1693 bootstage_error(bootstage_id
+ BOOTSTAGE_SUB_LOAD
);
1696 } else if (load_op
!= FIT_LOAD_OPTIONAL_NON_ZERO
|| load
) {
1697 ulong image_start
, image_end
;
1702 * move image data to the load address,
1703 * make sure we don't overwrite initial image
1706 image_end
= addr
+ fit_get_size(fit
);
1708 load_end
= load
+ len
;
1709 if (image_type
!= IH_TYPE_KERNEL
&&
1710 load
< image_end
&& load_end
> image_start
) {
1711 printf("Error: %s overwritten\n", prop_name
);
1715 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1716 prop_name
, data
, load
);
1718 dst
= map_sysmem(load
, len
);
1719 memmove(dst
, buf
, len
);
1722 bootstage_mark(bootstage_id
+ BOOTSTAGE_SUB_LOAD
);
1727 *fit_unamep
= (char *)fit_uname
;
1728 if (fit_uname_configp
)
1729 *fit_uname_configp
= (char *)fit_uname_config
;
1734 int boot_get_setup_fit(bootm_headers_t
*images
, uint8_t arch
,
1735 ulong
*setup_start
, ulong
*setup_len
)
1742 addr
= map_to_sysmem(images
->fit_hdr_os
);
1743 noffset
= fit_get_node_from_config(images
, FIT_SETUP_PROP
, addr
);
1747 ret
= fit_image_load(images
, addr
, NULL
, NULL
, arch
,
1748 IH_TYPE_X86_SETUP
, BOOTSTAGE_ID_FIT_SETUP_START
,
1749 FIT_LOAD_REQUIRED
, setup_start
, &len
);