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+
16 #include <linux/compiler.h>
21 DECLARE_GLOBAL_DATA_PTR;
22 #endif /* !USE_HOSTCC*/
25 #include <bootstage.h>
26 #include <u-boot/crc.h>
27 #include <u-boot/md5.h>
28 #include <u-boot/sha1.h>
29 #include <u-boot/sha256.h>
31 /*****************************************************************************/
32 /* New uImage format routines */
33 /*****************************************************************************/
35 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
36 ulong *addr, const char **name)
43 sep = strchr(spec, sepc);
46 *addr = simple_strtoul(spec, NULL, 16);
56 * fit_parse_conf - parse FIT configuration spec
57 * @spec: input string, containing configuration spec
58 * @add_curr: current image address (to be used as a possible default)
59 * @addr: pointer to a ulong variable, will hold FIT image address of a given
61 * @conf_name double pointer to a char, will hold pointer to a configuration
64 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
65 * where <addr> is a FIT image address that contains configuration
66 * with a <conf> unit name.
68 * Address part is optional, and if omitted default add_curr will
72 * 1 if spec is a valid configuration string,
73 * addr and conf_name are set accordingly
76 int fit_parse_conf(const char *spec, ulong addr_curr,
77 ulong *addr, const char **conf_name)
79 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
83 * fit_parse_subimage - parse FIT subimage spec
84 * @spec: input string, containing subimage spec
85 * @add_curr: current image address (to be used as a possible default)
86 * @addr: pointer to a ulong variable, will hold FIT image address of a given
88 * @image_name: double pointer to a char, will hold pointer to a subimage name
90 * fit_parse_subimage() expects subimage spec in the form of
91 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
92 * subimage with a <subimg> unit name.
94 * Address part is optional, and if omitted default add_curr will
98 * 1 if spec is a valid subimage string,
99 * addr and image_name are set accordingly
102 int fit_parse_subimage(const char *spec, ulong addr_curr,
103 ulong *addr, const char **image_name)
105 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
107 #endif /* !USE_HOSTCC */
109 static void fit_get_debug(const void *fit, int noffset,
110 char *prop_name, int err)
112 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
113 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
118 * fit_get_subimage_count - get component (sub-image) count
119 * @fit: pointer to the FIT format image header
120 * @images_noffset: offset of images node
123 * number of image components
125 int fit_get_subimage_count(const void *fit, int images_noffset)
131 /* Process its subnodes, print out component images details */
132 for (ndepth = 0, count = 0,
133 noffset = fdt_next_node(fit, images_noffset, &ndepth);
134 (noffset >= 0) && (ndepth > 0);
135 noffset = fdt_next_node(fit, noffset, &ndepth)) {
144 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
146 * fit_print_contents - prints out the contents of the FIT format image
147 * @fit: pointer to the FIT format image header
148 * @p: pointer to prefix string
150 * fit_print_contents() formats a multi line FIT image contents description.
151 * The routine prints out FIT image properties (root node level) followed by
152 * the details of each component image.
155 * no returned results
157 void fit_print_contents(const void *fit)
170 /* Indent string is defined in header image.h */
171 p = IMAGE_INDENT_STRING;
173 /* Root node properties */
174 ret = fit_get_desc(fit, 0, &desc);
175 printf("%sFIT description: ", p);
177 printf("unavailable\n");
179 printf("%s\n", desc);
181 if (IMAGE_ENABLE_TIMESTAMP) {
182 ret = fit_get_timestamp(fit, 0, ×tamp);
183 printf("%sCreated: ", p);
185 printf("unavailable\n");
187 genimg_print_time(timestamp);
190 /* Find images parent node offset */
191 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
192 if (images_noffset < 0) {
193 printf("Can't find images parent node '%s' (%s)\n",
194 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
198 /* Process its subnodes, print out component images details */
199 for (ndepth = 0, count = 0,
200 noffset = fdt_next_node(fit, images_noffset, &ndepth);
201 (noffset >= 0) && (ndepth > 0);
202 noffset = fdt_next_node(fit, noffset, &ndepth)) {
205 * Direct child node of the images parent node,
206 * i.e. component image node.
208 printf("%s Image %u (%s)\n", p, count++,
209 fit_get_name(fit, noffset, NULL));
211 fit_image_print(fit, noffset, p);
215 /* Find configurations parent node offset */
216 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
217 if (confs_noffset < 0) {
218 debug("Can't get configurations parent node '%s' (%s)\n",
219 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
223 /* get default configuration unit name from default property */
224 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
226 printf("%s Default Configuration: '%s'\n", p, uname);
228 /* Process its subnodes, print out configurations details */
229 for (ndepth = 0, count = 0,
230 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
231 (noffset >= 0) && (ndepth > 0);
232 noffset = fdt_next_node(fit, noffset, &ndepth)) {
235 * Direct child node of the configurations parent node,
236 * i.e. configuration node.
238 printf("%s Configuration %u (%s)\n", p, count++,
239 fit_get_name(fit, noffset, NULL));
241 fit_conf_print(fit, noffset, p);
247 * fit_image_print_data() - prints out the hash node details
248 * @fit: pointer to the FIT format image header
249 * @noffset: offset of the hash node
250 * @p: pointer to prefix string
251 * @type: Type of information to print ("hash" or "sign")
253 * fit_image_print_data() lists properties for the processed hash node
255 * This function avoid using puts() since it prints a newline on the host
256 * but does not in U-Boot.
259 * no returned results
261 static void fit_image_print_data(const void *fit, int noffset, const char *p,
271 debug("%s %s node: '%s'\n", p, type,
272 fit_get_name(fit, noffset, NULL));
273 printf("%s %s algo: ", p, type);
274 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
275 printf("invalid/unsupported\n");
279 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
280 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
282 printf(":%s", keyname);
284 printf(" (required)");
287 ret = fit_image_hash_get_value(fit, noffset, &value,
289 printf("%s %s value: ", p, type);
291 printf("unavailable\n");
293 for (i = 0; i < value_len; i++)
294 printf("%02x", value[i]);
298 debug("%s %s len: %d\n", p, type, value_len);
300 /* Signatures have a time stamp */
301 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
304 printf("%s Timestamp: ", p);
305 if (fit_get_timestamp(fit, noffset, ×tamp))
306 printf("unavailable\n");
308 genimg_print_time(timestamp);
313 * fit_image_print_verification_data() - prints out the hash/signature details
314 * @fit: pointer to the FIT format image header
315 * @noffset: offset of the hash or signature node
316 * @p: pointer to prefix string
318 * This lists properties for the processed hash node
321 * no returned results
323 static void fit_image_print_verification_data(const void *fit, int noffset,
329 * Check subnode name, must be equal to "hash" or "signature".
330 * Multiple hash/signature nodes require unique unit node
331 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
333 name = fit_get_name(fit, noffset, NULL);
334 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
335 fit_image_print_data(fit, noffset, p, "Hash");
336 } else if (!strncmp(name, FIT_SIG_NODENAME,
337 strlen(FIT_SIG_NODENAME))) {
338 fit_image_print_data(fit, noffset, p, "Sign");
343 * fit_image_print - prints out the FIT component image details
344 * @fit: pointer to the FIT format image header
345 * @image_noffset: offset of the component image node
346 * @p: pointer to prefix string
348 * fit_image_print() lists all mandatory properties for the processed component
349 * image. If present, hash nodes are printed out as well. Load
350 * address for images of type firmware is also printed out. Since the load
351 * address is not mandatory for firmware images, it will be output as
352 * "unavailable" when not present.
355 * no returned results
357 void fit_image_print(const void *fit, int image_noffset, const char *p)
360 uint8_t type, arch, os, comp;
368 /* Mandatory properties */
369 ret = fit_get_desc(fit, image_noffset, &desc);
370 printf("%s Description: ", p);
372 printf("unavailable\n");
374 printf("%s\n", desc);
376 if (IMAGE_ENABLE_TIMESTAMP) {
379 ret = fit_get_timestamp(fit, 0, ×tamp);
380 printf("%s Created: ", p);
382 printf("unavailable\n");
384 genimg_print_time(timestamp);
387 fit_image_get_type(fit, image_noffset, &type);
388 printf("%s Type: %s\n", p, genimg_get_type_name(type));
390 fit_image_get_comp(fit, image_noffset, &comp);
391 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
393 ret = fit_image_get_data(fit, image_noffset, &data, &size);
396 printf("%s Data Start: ", p);
398 printf("unavailable\n");
400 void *vdata = (void *)data;
402 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
406 printf("%s Data Size: ", p);
408 printf("unavailable\n");
410 genimg_print_size(size);
412 /* Remaining, type dependent properties */
413 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
414 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
415 (type == IH_TYPE_FLATDT)) {
416 fit_image_get_arch(fit, image_noffset, &arch);
417 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
420 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
421 fit_image_get_os(fit, image_noffset, &os);
422 printf("%s OS: %s\n", p, genimg_get_os_name(os));
425 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
426 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
427 (type == IH_TYPE_FPGA)) {
428 ret = fit_image_get_load(fit, image_noffset, &load);
429 printf("%s Load Address: ", p);
431 printf("unavailable\n");
433 printf("0x%08lx\n", load);
436 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
437 (type == IH_TYPE_RAMDISK)) {
438 ret = fit_image_get_entry(fit, image_noffset, &entry);
439 printf("%s Entry Point: ", p);
441 printf("unavailable\n");
443 printf("0x%08lx\n", entry);
446 /* Process all hash subnodes of the component image node */
447 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
448 (noffset >= 0) && (ndepth > 0);
449 noffset = fdt_next_node(fit, noffset, &ndepth)) {
451 /* Direct child node of the component image node */
452 fit_image_print_verification_data(fit, noffset, p);
457 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
460 * fit_get_desc - get node description property
461 * @fit: pointer to the FIT format image header
462 * @noffset: node offset
463 * @desc: double pointer to the char, will hold pointer to the description
465 * fit_get_desc() reads description property from a given node, if
466 * description is found pointer to it is returned in third call argument.
472 int fit_get_desc(const void *fit, int noffset, char **desc)
476 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
478 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
486 * fit_get_timestamp - get node timestamp property
487 * @fit: pointer to the FIT format image header
488 * @noffset: node offset
489 * @timestamp: pointer to the time_t, will hold read timestamp
491 * fit_get_timestamp() reads timestamp property from given node, if timestamp
492 * is found and has a correct size its value is returned in third call
497 * -1, on property read failure
498 * -2, on wrong timestamp size
500 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
505 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
507 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
510 if (len != sizeof(uint32_t)) {
511 debug("FIT timestamp with incorrect size of (%u)\n", len);
515 *timestamp = uimage_to_cpu(*((uint32_t *)data));
520 * fit_image_get_node - get node offset for component image of a given unit name
521 * @fit: pointer to the FIT format image header
522 * @image_uname: component image node unit name
524 * fit_image_get_node() finds a component image (within the '/images'
525 * node) of a provided unit name. If image is found its node offset is
526 * returned to the caller.
529 * image node offset when found (>=0)
530 * negative number on failure (FDT_ERR_* code)
532 int fit_image_get_node(const void *fit, const char *image_uname)
534 int noffset, images_noffset;
536 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
537 if (images_noffset < 0) {
538 debug("Can't find images parent node '%s' (%s)\n",
539 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
540 return images_noffset;
543 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
545 debug("Can't get node offset for image unit name: '%s' (%s)\n",
546 image_uname, fdt_strerror(noffset));
553 * fit_image_get_os - get os id for a given component image node
554 * @fit: pointer to the FIT format image header
555 * @noffset: component image node offset
556 * @os: pointer to the uint8_t, will hold os numeric id
558 * fit_image_get_os() finds os property in a given component image node.
559 * If the property is found, its (string) value is translated to the numeric
560 * id which is returned to the caller.
566 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
571 /* Get OS name from property data */
572 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
574 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
579 /* Translate OS name to id */
580 *os = genimg_get_os_id(data);
585 * fit_image_get_arch - get arch id for a given component image node
586 * @fit: pointer to the FIT format image header
587 * @noffset: component image node offset
588 * @arch: pointer to the uint8_t, will hold arch numeric id
590 * fit_image_get_arch() finds arch property in a given component image node.
591 * If the property is found, its (string) value is translated to the numeric
592 * id which is returned to the caller.
598 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
603 /* Get architecture name from property data */
604 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
606 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
611 /* Translate architecture name to id */
612 *arch = genimg_get_arch_id(data);
617 * fit_image_get_type - get type id for a given component image node
618 * @fit: pointer to the FIT format image header
619 * @noffset: component image node offset
620 * @type: pointer to the uint8_t, will hold type numeric id
622 * fit_image_get_type() finds type property in a given component image node.
623 * If the property is found, its (string) value is translated to the numeric
624 * id which is returned to the caller.
630 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
635 /* Get image type name from property data */
636 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
638 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
643 /* Translate image type name to id */
644 *type = genimg_get_type_id(data);
649 * fit_image_get_comp - get comp id for a given component image node
650 * @fit: pointer to the FIT format image header
651 * @noffset: component image node offset
652 * @comp: pointer to the uint8_t, will hold comp numeric id
654 * fit_image_get_comp() finds comp property in a given component image node.
655 * If the property is found, its (string) value is translated to the numeric
656 * id which is returned to the caller.
662 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
667 /* Get compression name from property data */
668 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
670 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
675 /* Translate compression name to id */
676 *comp = genimg_get_comp_id(data);
680 static int fit_image_get_address(const void *fit, int noffset, char *name,
687 cell = fdt_getprop(fit, noffset, name, &len);
689 fit_get_debug(fit, noffset, name, len);
693 if (len > sizeof(ulong)) {
694 printf("Unsupported %s address size\n", name);
699 /* Use load64 to avoid compiling warning for 32-bit target */
701 load64 = (load64 << 32) | uimage_to_cpu(*cell);
704 *load = (ulong)load64;
709 * fit_image_get_load() - get load addr property for given component image node
710 * @fit: pointer to the FIT format image header
711 * @noffset: component image node offset
712 * @load: pointer to the uint32_t, will hold load address
714 * fit_image_get_load() finds load address property in a given component
715 * image node. If the property is found, its value is returned to the caller.
721 int fit_image_get_load(const void *fit, int noffset, ulong *load)
723 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
727 * fit_image_get_entry() - get entry point address property
728 * @fit: pointer to the FIT format image header
729 * @noffset: component image node offset
730 * @entry: pointer to the uint32_t, will hold entry point address
732 * This gets the entry point address property for a given component image
735 * fit_image_get_entry() finds entry point address property in a given
736 * component image node. If the property is found, its value is returned
743 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
745 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
749 * fit_image_get_data - get data property and its size for a given component image node
750 * @fit: pointer to the FIT format image header
751 * @noffset: component image node offset
752 * @data: double pointer to void, will hold data property's data address
753 * @size: pointer to size_t, will hold data property's data size
755 * fit_image_get_data() finds data property in a given component image node.
756 * If the property is found its data start address and size are returned to
763 int fit_image_get_data(const void *fit, int noffset,
764 const void **data, size_t *size)
768 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
770 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
780 * fit_image_hash_get_algo - get hash algorithm name
781 * @fit: pointer to the FIT format image header
782 * @noffset: hash node offset
783 * @algo: double pointer to char, will hold pointer to the algorithm name
785 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
786 * If the property is found its data start address is returned to the caller.
792 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
796 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
798 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
806 * fit_image_hash_get_value - get hash value and length
807 * @fit: pointer to the FIT format image header
808 * @noffset: hash node offset
809 * @value: double pointer to uint8_t, will hold address of a hash value data
810 * @value_len: pointer to an int, will hold hash data length
812 * fit_image_hash_get_value() finds hash value property in a given hash node.
813 * If the property is found its data start address and size are returned to
820 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
825 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
826 if (*value == NULL) {
827 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
837 * fit_image_hash_get_ignore - get hash ignore flag
838 * @fit: pointer to the FIT format image header
839 * @noffset: hash node offset
840 * @ignore: pointer to an int, will hold hash ignore flag
842 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
843 * If the property is found and non-zero, the hash algorithm is not verified by
844 * u-boot automatically.
847 * 0, on ignore not found
848 * value, on ignore found
850 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
855 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
856 if (value == NULL || len != sizeof(int))
864 ulong fit_get_end(const void *fit)
866 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
870 * fit_set_timestamp - set node timestamp property
871 * @fit: pointer to the FIT format image header
872 * @noffset: node offset
873 * @timestamp: timestamp value to be set
875 * fit_set_timestamp() attempts to set timestamp property in the requested
876 * node and returns operation status to the caller.
880 * -ENOSPC if no space in device tree, -1 for other error
882 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
887 t = cpu_to_uimage(timestamp);
888 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
891 debug("Can't set '%s' property for '%s' node (%s)\n",
892 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
894 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
901 * calculate_hash - calculate and return hash for provided input data
902 * @data: pointer to the input data
903 * @data_len: data length
904 * @algo: requested hash algorithm
905 * @value: pointer to the char, will hold hash value data (caller must
906 * allocate enough free space)
907 * value_len: length of the calculated hash
909 * calculate_hash() computes input data hash according to the requested
911 * Resulting hash value is placed in caller provided 'value' buffer, length
912 * of the calculated hash is returned via value_len pointer argument.
916 * -1, when algo is unsupported
918 int calculate_hash(const void *data, int data_len, const char *algo,
919 uint8_t *value, int *value_len)
921 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
922 *((uint32_t *)value) = crc32_wd(0, data, data_len,
924 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
926 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
927 sha1_csum_wd((unsigned char *)data, data_len,
928 (unsigned char *)value, CHUNKSZ_SHA1);
930 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
931 sha256_csum_wd((unsigned char *)data, data_len,
932 (unsigned char *)value, CHUNKSZ_SHA256);
933 *value_len = SHA256_SUM_LEN;
934 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
935 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
938 debug("Unsupported hash alogrithm\n");
944 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
945 size_t size, char **err_msgp)
947 uint8_t value[FIT_MAX_HASH_LEN];
956 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
957 *err_msgp = "Can't get hash algo property";
962 if (IMAGE_ENABLE_IGNORE) {
963 fit_image_hash_get_ignore(fit, noffset, &ignore);
970 if (fit_image_hash_get_value(fit, noffset, &fit_value,
972 *err_msgp = "Can't get hash value property";
976 if (calculate_hash(data, size, algo, value, &value_len)) {
977 *err_msgp = "Unsupported hash algorithm";
981 if (value_len != fit_value_len) {
982 *err_msgp = "Bad hash value len";
984 } else if (memcmp(value, fit_value, value_len) != 0) {
985 *err_msgp = "Bad hash value";
993 * fit_image_verify - verify data integrity
994 * @fit: pointer to the FIT format image header
995 * @image_noffset: component image node offset
997 * fit_image_verify() goes over component image hash nodes,
998 * re-calculates each data hash and compares with the value stored in hash
1002 * 1, if all hashes are valid
1003 * 0, otherwise (or on error)
1005 int fit_image_verify(const void *fit, int image_noffset)
1014 /* Get image data and data length */
1015 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1016 err_msg = "Can't get image data/size";
1020 /* Verify all required signatures */
1021 if (IMAGE_ENABLE_VERIFY &&
1022 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1023 gd_fdt_blob(), &verify_all)) {
1024 err_msg = "Unable to verify required signature";
1028 /* Process all hash subnodes of the component image node */
1029 fdt_for_each_subnode(fit, noffset, image_noffset) {
1030 const char *name = fit_get_name(fit, noffset, NULL);
1033 * Check subnode name, must be equal to "hash".
1034 * Multiple hash nodes require unique unit node
1035 * names, e.g. hash@1, hash@2, etc.
1037 if (!strncmp(name, FIT_HASH_NODENAME,
1038 strlen(FIT_HASH_NODENAME))) {
1039 if (fit_image_check_hash(fit, noffset, data, size,
1043 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1044 !strncmp(name, FIT_SIG_NODENAME,
1045 strlen(FIT_SIG_NODENAME))) {
1046 ret = fit_image_check_sig(fit, noffset, data,
1047 size, -1, &err_msg);
1050 * Show an indication on failure, but do not return
1051 * an error. Only keys marked 'required' can cause
1052 * an image validation failure. See the call to
1053 * fit_image_verify_required_sigs() above.
1062 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1063 err_msg = "Corrupted or truncated tree";
1070 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1071 err_msg, fit_get_name(fit, noffset, NULL),
1072 fit_get_name(fit, image_noffset, NULL));
1077 * fit_all_image_verify - verify data integrity for all images
1078 * @fit: pointer to the FIT format image header
1080 * fit_all_image_verify() goes over all images in the FIT and
1081 * for every images checks if all it's hashes are valid.
1084 * 1, if all hashes of all images are valid
1085 * 0, otherwise (or on error)
1087 int fit_all_image_verify(const void *fit)
1094 /* Find images parent node offset */
1095 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1096 if (images_noffset < 0) {
1097 printf("Can't find images parent node '%s' (%s)\n",
1098 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1102 /* Process all image subnodes, check hashes for each */
1103 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1105 for (ndepth = 0, count = 0,
1106 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1107 (noffset >= 0) && (ndepth > 0);
1108 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1111 * Direct child node of the images parent node,
1112 * i.e. component image node.
1114 printf(" Hash(es) for Image %u (%s): ", count,
1115 fit_get_name(fit, noffset, NULL));
1118 if (!fit_image_verify(fit, noffset))
1127 * fit_image_check_os - check whether image node is of a given os type
1128 * @fit: pointer to the FIT format image header
1129 * @noffset: component image node offset
1130 * @os: requested image os
1132 * fit_image_check_os() reads image os property and compares its numeric
1133 * id with the requested os. Comparison result is returned to the caller.
1136 * 1 if image is of given os type
1137 * 0 otherwise (or on error)
1139 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1143 if (fit_image_get_os(fit, noffset, &image_os))
1145 return (os == image_os);
1149 * fit_image_check_arch - check whether image node is of a given arch
1150 * @fit: pointer to the FIT format image header
1151 * @noffset: component image node offset
1152 * @arch: requested imagearch
1154 * fit_image_check_arch() reads image arch property and compares its numeric
1155 * id with the requested arch. Comparison result is returned to the caller.
1158 * 1 if image is of given arch
1159 * 0 otherwise (or on error)
1161 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1165 if (fit_image_get_arch(fit, noffset, &image_arch))
1167 return (arch == image_arch) ||
1168 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64);
1172 * fit_image_check_type - check whether image node is of a given type
1173 * @fit: pointer to the FIT format image header
1174 * @noffset: component image node offset
1175 * @type: requested image type
1177 * fit_image_check_type() reads image type property and compares its numeric
1178 * id with the requested type. Comparison result is returned to the caller.
1181 * 1 if image is of given type
1182 * 0 otherwise (or on error)
1184 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1188 if (fit_image_get_type(fit, noffset, &image_type))
1190 return (type == image_type);
1194 * fit_image_check_comp - check whether image node uses given compression
1195 * @fit: pointer to the FIT format image header
1196 * @noffset: component image node offset
1197 * @comp: requested image compression type
1199 * fit_image_check_comp() reads image compression property and compares its
1200 * numeric id with the requested compression type. Comparison result is
1201 * returned to the caller.
1204 * 1 if image uses requested compression
1205 * 0 otherwise (or on error)
1207 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1211 if (fit_image_get_comp(fit, noffset, &image_comp))
1213 return (comp == image_comp);
1217 * fit_check_format - sanity check FIT image format
1218 * @fit: pointer to the FIT format image header
1220 * fit_check_format() runs a basic sanity FIT image verification.
1221 * Routine checks for mandatory properties, nodes, etc.
1227 int fit_check_format(const void *fit)
1229 /* mandatory / node 'description' property */
1230 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1231 debug("Wrong FIT format: no description\n");
1235 if (IMAGE_ENABLE_TIMESTAMP) {
1236 /* mandatory / node 'timestamp' property */
1237 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1238 debug("Wrong FIT format: no timestamp\n");
1243 /* mandatory subimages parent '/images' node */
1244 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1245 debug("Wrong FIT format: no images parent node\n");
1254 * fit_conf_find_compat
1255 * @fit: pointer to the FIT format image header
1256 * @fdt: pointer to the device tree to compare against
1258 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1259 * most compatible with the passed in device tree.
1268 * |-o configurations
1276 * |-compatible = "foo,bar", "bim,bam"
1279 * |-compatible = "foo,bar",
1282 * |-compatible = "bim,bam", "baz,biz"
1284 * Configuration 1 would be picked because the first string in U-Boot's
1285 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1286 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1289 * offset to the configuration to use if one was found
1292 int fit_conf_find_compat(const void *fit, const void *fdt)
1295 int noffset, confs_noffset, images_noffset;
1296 const void *fdt_compat;
1298 int best_match_offset = 0;
1299 int best_match_pos = 0;
1301 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1302 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1303 if (confs_noffset < 0 || images_noffset < 0) {
1304 debug("Can't find configurations or images nodes.\n");
1308 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1310 debug("Fdt for comparison has no \"compatible\" property.\n");
1315 * Loop over the configurations in the FIT image.
1317 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1318 (noffset >= 0) && (ndepth > 0);
1319 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1321 const char *kfdt_name;
1323 const char *cur_fdt_compat;
1331 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1333 debug("No fdt property found.\n");
1336 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1338 if (kfdt_noffset < 0) {
1339 debug("No image node named \"%s\" found.\n",
1344 * Get a pointer to this configuration's fdt.
1346 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1347 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1351 len = fdt_compat_len;
1352 cur_fdt_compat = fdt_compat;
1354 * Look for a match for each U-Boot compatibility string in
1355 * turn in this configuration's fdt.
1357 for (i = 0; len > 0 &&
1358 (!best_match_offset || best_match_pos > i); i++) {
1359 int cur_len = strlen(cur_fdt_compat) + 1;
1361 if (!fdt_node_check_compatible(kfdt, 0,
1363 best_match_offset = noffset;
1368 cur_fdt_compat += cur_len;
1371 if (!best_match_offset) {
1372 debug("No match found.\n");
1376 return best_match_offset;
1380 * fit_conf_get_node - get node offset for configuration of a given unit name
1381 * @fit: pointer to the FIT format image header
1382 * @conf_uname: configuration node unit name
1384 * fit_conf_get_node() finds a configuration (within the '/configurations'
1385 * parent node) of a provided unit name. If configuration is found its node
1386 * offset is returned to the caller.
1388 * When NULL is provided in second argument fit_conf_get_node() will search
1389 * for a default configuration node instead. Default configuration node unit
1390 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1394 * configuration node offset when found (>=0)
1395 * negative number on failure (FDT_ERR_* code)
1397 int fit_conf_get_node(const void *fit, const char *conf_uname)
1399 int noffset, confs_noffset;
1402 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1403 if (confs_noffset < 0) {
1404 debug("Can't find configurations parent node '%s' (%s)\n",
1405 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1406 return confs_noffset;
1409 if (conf_uname == NULL) {
1410 /* get configuration unit name from the default property */
1411 debug("No configuration specified, trying default...\n");
1412 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1413 FIT_DEFAULT_PROP, &len);
1414 if (conf_uname == NULL) {
1415 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1419 debug("Found default configuration: '%s'\n", conf_uname);
1422 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1424 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1425 conf_uname, fdt_strerror(noffset));
1431 int fit_conf_get_prop_node(const void *fit, int noffset,
1432 const char *prop_name)
1437 /* get kernel image unit name from configuration kernel property */
1438 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1442 return fit_image_get_node(fit, uname);
1446 * fit_conf_print - prints out the FIT configuration details
1447 * @fit: pointer to the FIT format image header
1448 * @noffset: offset of the configuration node
1449 * @p: pointer to prefix string
1451 * fit_conf_print() lists all mandatory properties for the processed
1452 * configuration node.
1455 * no returned results
1457 void fit_conf_print(const void *fit, int noffset, const char *p)
1462 int loadables_index;
1464 /* Mandatory properties */
1465 ret = fit_get_desc(fit, noffset, &desc);
1466 printf("%s Description: ", p);
1468 printf("unavailable\n");
1470 printf("%s\n", desc);
1472 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1473 printf("%s Kernel: ", p);
1475 printf("unavailable\n");
1477 printf("%s\n", uname);
1479 /* Optional properties */
1480 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1482 printf("%s Init Ramdisk: %s\n", p, uname);
1484 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1486 printf("%s FDT: %s\n", p, uname);
1488 uname = (char *)fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
1490 printf("%s FPGA: %s\n", p, uname);
1492 /* Print out all of the specified loadables */
1493 for (loadables_index = 0;
1494 fdt_get_string_index(fit, noffset,
1497 (const char **)&uname) == 0;
1500 if (loadables_index == 0) {
1501 printf("%s Loadables: ", p);
1505 printf("%s\n", uname);
1509 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1511 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1517 fit_image_print(fit, rd_noffset, " ");
1520 puts(" Verifying Hash Integrity ... ");
1521 if (!fit_image_verify(fit, rd_noffset)) {
1522 puts("Bad Data Hash\n");
1528 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1529 ret = fit_image_get_data(fit, rd_noffset, &data, &size);
1533 /* perform any post-processing on the image data */
1534 board_fit_image_post_process((void **)&data, &size);
1537 * update U-Boot's understanding of the "data" property start address
1538 * and size according to the performed post-processing
1540 ret = fdt_setprop((void *)fit, rd_noffset, FIT_DATA_PROP, data, size);
1548 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1555 debug("* %s: using config '%s' from image at 0x%08lx\n",
1556 prop_name, images->fit_uname_cfg, addr);
1558 /* Check whether configuration has this property defined */
1559 fit_hdr = map_sysmem(addr, 0);
1560 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1561 if (cfg_noffset < 0) {
1562 debug("* %s: no such config\n", prop_name);
1566 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1568 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1576 * fit_get_image_type_property() - get property name for IH_TYPE_...
1578 * @return the properly name where we expect to find the image in the
1581 static const char *fit_get_image_type_property(int type)
1584 * This is sort-of available in the uimage_type[] table in image.c
1585 * but we don't have access to the short name, and "fdt" is different
1586 * anyway. So let's just keep it here.
1589 case IH_TYPE_FLATDT:
1590 return FIT_FDT_PROP;
1591 case IH_TYPE_KERNEL:
1592 return FIT_KERNEL_PROP;
1593 case IH_TYPE_RAMDISK:
1594 return FIT_RAMDISK_PROP;
1595 case IH_TYPE_X86_SETUP:
1596 return FIT_SETUP_PROP;
1597 case IH_TYPE_LOADABLE:
1598 return FIT_LOADABLE_PROP;
1600 return FIT_FPGA_PROP;
1606 int fit_image_load(bootm_headers_t *images, ulong addr,
1607 const char **fit_unamep, const char **fit_uname_configp,
1608 int arch, int image_type, int bootstage_id,
1609 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1611 int cfg_noffset, noffset;
1612 const char *fit_uname;
1613 const char *fit_uname_config;
1618 ulong load, data, len;
1620 const char *prop_name;
1623 fit = map_sysmem(addr, 0);
1624 fit_uname = fit_unamep ? *fit_unamep : NULL;
1625 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1626 prop_name = fit_get_image_type_property(image_type);
1627 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1629 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1630 if (!fit_check_format(fit)) {
1631 printf("Bad FIT %s image format!\n", prop_name);
1632 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1635 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1637 /* get FIT component image node offset */
1638 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1639 noffset = fit_image_get_node(fit, fit_uname);
1642 * no image node unit name, try to get config
1643 * node first. If config unit node name is NULL
1644 * fit_conf_get_node() will try to find default config node
1646 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1647 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1648 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1650 cfg_noffset = fit_conf_get_node(fit,
1653 if (cfg_noffset < 0) {
1654 puts("Could not find configuration node\n");
1655 bootstage_error(bootstage_id +
1656 BOOTSTAGE_SUB_NO_UNIT_NAME);
1659 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1660 printf(" Using '%s' configuration\n", fit_uname_config);
1661 if (image_type == IH_TYPE_KERNEL) {
1662 /* Remember (and possibly verify) this config */
1663 images->fit_uname_cfg = fit_uname_config;
1664 if (IMAGE_ENABLE_VERIFY && images->verify) {
1665 puts(" Verifying Hash Integrity ... ");
1666 if (fit_config_verify(fit, cfg_noffset)) {
1667 puts("Bad Data Hash\n");
1668 bootstage_error(bootstage_id +
1669 BOOTSTAGE_SUB_HASH);
1674 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1677 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1679 fit_uname = fit_get_name(fit, noffset, NULL);
1682 puts("Could not find subimage node\n");
1683 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1687 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1689 ret = fit_image_select(fit, noffset, images->verify);
1691 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1695 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1696 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1697 if (!fit_image_check_target_arch(fit, noffset)) {
1698 puts("Unsupported Architecture\n");
1699 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1703 if (image_type == IH_TYPE_FLATDT &&
1704 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1705 puts("FDT image is compressed");
1706 return -EPROTONOSUPPORT;
1709 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1710 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1711 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1712 (image_type == IH_TYPE_KERNEL &&
1713 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1715 os_ok = image_type == IH_TYPE_FLATDT ||
1716 image_type == IH_TYPE_FPGA ||
1717 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1718 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1719 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1722 * If either of the checks fail, we should report an error, but
1723 * if the image type is coming from the "loadables" field, we
1724 * don't care what it is
1726 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
1727 fit_image_get_os(fit, noffset, &os);
1728 printf("No %s %s %s Image\n",
1729 genimg_get_os_name(os),
1730 genimg_get_arch_name(arch),
1731 genimg_get_type_name(image_type));
1732 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1736 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1738 /* get image data address and length */
1739 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1740 printf("Could not find %s subimage data!\n", prop_name);
1741 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1746 /* verify that image data is a proper FDT blob */
1747 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1748 puts("Subimage data is not a FDT");
1752 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1755 * Work-around for eldk-4.2 which gives this warning if we try to
1756 * cast in the unmap_sysmem() call:
1757 * warning: initialization discards qualifiers from pointer target type
1760 void *vbuf = (void *)buf;
1762 data = map_to_sysmem(vbuf);
1765 if (load_op == FIT_LOAD_IGNORED) {
1767 } else if (fit_image_get_load(fit, noffset, &load)) {
1768 if (load_op == FIT_LOAD_REQUIRED) {
1769 printf("Can't get %s subimage load address!\n",
1771 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1774 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1775 ulong image_start, image_end;
1780 * move image data to the load address,
1781 * make sure we don't overwrite initial image
1784 image_end = addr + fit_get_size(fit);
1786 load_end = load + len;
1787 if (image_type != IH_TYPE_KERNEL &&
1788 load < image_end && load_end > image_start) {
1789 printf("Error: %s overwritten\n", prop_name);
1793 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1794 prop_name, data, load);
1796 dst = map_sysmem(load, len);
1797 memmove(dst, buf, len);
1800 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1805 *fit_unamep = (char *)fit_uname;
1806 if (fit_uname_configp)
1807 *fit_uname_configp = (char *)fit_uname_config;
1812 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1813 ulong *setup_start, ulong *setup_len)
1820 addr = map_to_sysmem(images->fit_hdr_os);
1821 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1825 ret = fit_image_load(images, addr, NULL, NULL, arch,
1826 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1827 FIT_LOAD_REQUIRED, setup_start, &len);