1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
5 * (C) Copyright 2008 Semihalf
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 #define LOG_CATEGORY LOGC_BOOT
16 #include <linux/libfdt.h>
17 #include <u-boot/crc.h>
19 #include <linux/compiler.h>
20 #include <linux/sizes.h>
27 #include <asm/global_data.h>
28 DECLARE_GLOBAL_DATA_PTR;
29 #endif /* !USE_HOSTCC*/
33 #include <bootstage.h>
34 #include <linux/kconfig.h>
35 #include <u-boot/crc.h>
36 #include <u-boot/md5.h>
37 #include <u-boot/sha1.h>
38 #include <u-boot/sha256.h>
39 #include <u-boot/sha512.h>
41 /*****************************************************************************/
42 /* New uImage format routines */
43 /*****************************************************************************/
45 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
46 ulong *addr, const char **name)
53 sep = strchr(spec, sepc);
56 *addr = simple_strtoul(spec, NULL, 16);
66 * fit_parse_conf - parse FIT configuration spec
67 * @spec: input string, containing configuration spec
68 * @add_curr: current image address (to be used as a possible default)
69 * @addr: pointer to a ulong variable, will hold FIT image address of a given
71 * @conf_name double pointer to a char, will hold pointer to a configuration
74 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
75 * where <addr> is a FIT image address that contains configuration
76 * with a <conf> unit name.
78 * Address part is optional, and if omitted default add_curr will
82 * 1 if spec is a valid configuration string,
83 * addr and conf_name are set accordingly
86 int fit_parse_conf(const char *spec, ulong addr_curr,
87 ulong *addr, const char **conf_name)
89 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
93 * fit_parse_subimage - parse FIT subimage spec
94 * @spec: input string, containing subimage spec
95 * @add_curr: current image address (to be used as a possible default)
96 * @addr: pointer to a ulong variable, will hold FIT image address of a given
98 * @image_name: double pointer to a char, will hold pointer to a subimage name
100 * fit_parse_subimage() expects subimage spec in the form of
101 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
102 * subimage with a <subimg> unit name.
104 * Address part is optional, and if omitted default add_curr will
108 * 1 if spec is a valid subimage string,
109 * addr and image_name are set accordingly
112 int fit_parse_subimage(const char *spec, ulong addr_curr,
113 ulong *addr, const char **image_name)
115 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
117 #endif /* !USE_HOSTCC */
120 /* Host tools use these implementations for Cipher and Signature support */
121 static void *host_blob;
123 void image_set_host_blob(void *blob)
128 void *image_get_host_blob(void)
132 #endif /* USE_HOSTCC */
134 static void fit_get_debug(const void *fit, int noffset,
135 char *prop_name, int err)
137 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
138 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
143 * fit_get_subimage_count - get component (sub-image) count
144 * @fit: pointer to the FIT format image header
145 * @images_noffset: offset of images node
148 * number of image components
150 int fit_get_subimage_count(const void *fit, int images_noffset)
156 /* Process its subnodes, print out component images details */
157 for (ndepth = 0, count = 0,
158 noffset = fdt_next_node(fit, images_noffset, &ndepth);
159 (noffset >= 0) && (ndepth > 0);
160 noffset = fdt_next_node(fit, noffset, &ndepth)) {
169 #if CONFIG_IS_ENABLED(FIT_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT)
171 * fit_image_print_data() - prints out the hash node details
172 * @fit: pointer to the FIT format image header
173 * @noffset: offset of the hash node
174 * @p: pointer to prefix string
175 * @type: Type of information to print ("hash" or "sign")
177 * fit_image_print_data() lists properties for the processed hash node
179 * This function avoid using puts() since it prints a newline on the host
180 * but does not in U-Boot.
183 * no returned results
185 static void fit_image_print_data(const void *fit, int noffset, const char *p,
196 debug("%s %s node: '%s'\n", p, type,
197 fit_get_name(fit, noffset, NULL));
198 printf("%s %s algo: ", p, type);
199 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
200 printf("invalid/unsupported\n");
204 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
205 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
207 printf(":%s", keyname);
209 printf(" (required)");
212 padding = fdt_getprop(fit, noffset, "padding", NULL);
214 printf("%s %s padding: %s\n", p, type, padding);
216 ret = fit_image_hash_get_value(fit, noffset, &value,
218 printf("%s %s value: ", p, type);
220 printf("unavailable\n");
222 for (i = 0; i < value_len; i++)
223 printf("%02x", value[i]);
227 debug("%s %s len: %d\n", p, type, value_len);
229 /* Signatures have a time stamp */
230 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
233 printf("%s Timestamp: ", p);
234 if (fit_get_timestamp(fit, noffset, ×tamp))
235 printf("unavailable\n");
237 genimg_print_time(timestamp);
242 * fit_image_print_verification_data() - prints out the hash/signature details
243 * @fit: pointer to the FIT format image header
244 * @noffset: offset of the hash or signature node
245 * @p: pointer to prefix string
247 * This lists properties for the processed hash node
250 * no returned results
252 static void fit_image_print_verification_data(const void *fit, int noffset,
258 * Check subnode name, must be equal to "hash" or "signature".
259 * Multiple hash/signature nodes require unique unit node
260 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
262 name = fit_get_name(fit, noffset, NULL);
263 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
264 fit_image_print_data(fit, noffset, p, "Hash");
265 } else if (!strncmp(name, FIT_SIG_NODENAME,
266 strlen(FIT_SIG_NODENAME))) {
267 fit_image_print_data(fit, noffset, p, "Sign");
272 * fit_conf_print - prints out the FIT configuration details
273 * @fit: pointer to the FIT format image header
274 * @noffset: offset of the configuration node
275 * @p: pointer to prefix string
277 * fit_conf_print() lists all mandatory properties for the processed
278 * configuration node.
281 * no returned results
283 static void fit_conf_print(const void *fit, int noffset, const char *p)
288 int fdt_index, loadables_index;
291 /* Mandatory properties */
292 ret = fit_get_desc(fit, noffset, &desc);
293 printf("%s Description: ", p);
295 printf("unavailable\n");
297 printf("%s\n", desc);
299 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
300 printf("%s Kernel: ", p);
302 printf("unavailable\n");
304 printf("%s\n", uname);
306 /* Optional properties */
307 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
309 printf("%s Init Ramdisk: %s\n", p, uname);
311 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
313 printf("%s Firmware: %s\n", p, uname);
316 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
317 fdt_index, NULL), uname;
320 printf("%s FDT: ", p);
323 printf("%s\n", uname);
326 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
328 printf("%s FPGA: %s\n", p, uname);
330 /* Print out all of the specified loadables */
331 for (loadables_index = 0;
332 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
333 loadables_index, NULL), uname;
335 if (loadables_index == 0) {
336 printf("%s Loadables: ", p);
340 printf("%s\n", uname);
343 /* Process all hash subnodes of the component configuration node */
344 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
345 (noffset >= 0) && (ndepth > 0);
346 noffset = fdt_next_node(fit, noffset, &ndepth)) {
348 /* Direct child node of the component configuration node */
349 fit_image_print_verification_data(fit, noffset, p);
355 * fit_print_contents - prints out the contents of the FIT format image
356 * @fit: pointer to the FIT format image header
357 * @p: pointer to prefix string
359 * fit_print_contents() formats a multi line FIT image contents description.
360 * The routine prints out FIT image properties (root node level) followed by
361 * the details of each component image.
364 * no returned results
366 void fit_print_contents(const void *fit)
379 /* Indent string is defined in header image.h */
380 p = IMAGE_INDENT_STRING;
382 /* Root node properties */
383 ret = fit_get_desc(fit, 0, &desc);
384 printf("%sFIT description: ", p);
386 printf("unavailable\n");
388 printf("%s\n", desc);
390 if (IMAGE_ENABLE_TIMESTAMP) {
391 ret = fit_get_timestamp(fit, 0, ×tamp);
392 printf("%sCreated: ", p);
394 printf("unavailable\n");
396 genimg_print_time(timestamp);
399 /* Find images parent node offset */
400 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
401 if (images_noffset < 0) {
402 printf("Can't find images parent node '%s' (%s)\n",
403 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
407 /* Process its subnodes, print out component images details */
408 for (ndepth = 0, count = 0,
409 noffset = fdt_next_node(fit, images_noffset, &ndepth);
410 (noffset >= 0) && (ndepth > 0);
411 noffset = fdt_next_node(fit, noffset, &ndepth)) {
414 * Direct child node of the images parent node,
415 * i.e. component image node.
417 printf("%s Image %u (%s)\n", p, count++,
418 fit_get_name(fit, noffset, NULL));
420 fit_image_print(fit, noffset, p);
424 /* Find configurations parent node offset */
425 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
426 if (confs_noffset < 0) {
427 debug("Can't get configurations parent node '%s' (%s)\n",
428 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
432 /* get default configuration unit name from default property */
433 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
435 printf("%s Default Configuration: '%s'\n", p, uname);
437 /* Process its subnodes, print out configurations details */
438 for (ndepth = 0, count = 0,
439 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
440 (noffset >= 0) && (ndepth > 0);
441 noffset = fdt_next_node(fit, noffset, &ndepth)) {
444 * Direct child node of the configurations parent node,
445 * i.e. configuration node.
447 printf("%s Configuration %u (%s)\n", p, count++,
448 fit_get_name(fit, noffset, NULL));
450 fit_conf_print(fit, noffset, p);
456 * fit_image_print - prints out the FIT component image details
457 * @fit: pointer to the FIT format image header
458 * @image_noffset: offset of the component image node
459 * @p: pointer to prefix string
461 * fit_image_print() lists all mandatory properties for the processed component
462 * image. If present, hash nodes are printed out as well. Load
463 * address for images of type firmware is also printed out. Since the load
464 * address is not mandatory for firmware images, it will be output as
465 * "unavailable" when not present.
468 * no returned results
470 void fit_image_print(const void *fit, int image_noffset, const char *p)
473 uint8_t type, arch, os, comp;
481 /* Mandatory properties */
482 ret = fit_get_desc(fit, image_noffset, &desc);
483 printf("%s Description: ", p);
485 printf("unavailable\n");
487 printf("%s\n", desc);
489 if (IMAGE_ENABLE_TIMESTAMP) {
492 ret = fit_get_timestamp(fit, 0, ×tamp);
493 printf("%s Created: ", p);
495 printf("unavailable\n");
497 genimg_print_time(timestamp);
500 fit_image_get_type(fit, image_noffset, &type);
501 printf("%s Type: %s\n", p, genimg_get_type_name(type));
503 fit_image_get_comp(fit, image_noffset, &comp);
504 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
506 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
509 printf("%s Data Start: ", p);
511 printf("unavailable\n");
513 void *vdata = (void *)data;
515 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
519 printf("%s Data Size: ", p);
521 printf("unavailable\n");
523 genimg_print_size(size);
525 /* Remaining, type dependent properties */
526 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
527 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
528 (type == IH_TYPE_FLATDT)) {
529 fit_image_get_arch(fit, image_noffset, &arch);
530 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
533 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
534 (type == IH_TYPE_FIRMWARE)) {
535 fit_image_get_os(fit, image_noffset, &os);
536 printf("%s OS: %s\n", p, genimg_get_os_name(os));
539 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
540 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
541 (type == IH_TYPE_FPGA)) {
542 ret = fit_image_get_load(fit, image_noffset, &load);
543 printf("%s Load Address: ", p);
545 printf("unavailable\n");
547 printf("0x%08lx\n", load);
550 /* optional load address for FDT */
551 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
552 printf("%s Load Address: 0x%08lx\n", p, load);
554 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
555 (type == IH_TYPE_RAMDISK)) {
556 ret = fit_image_get_entry(fit, image_noffset, &entry);
557 printf("%s Entry Point: ", p);
559 printf("unavailable\n");
561 printf("0x%08lx\n", entry);
564 /* Process all hash subnodes of the component image node */
565 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
566 (noffset >= 0) && (ndepth > 0);
567 noffset = fdt_next_node(fit, noffset, &ndepth)) {
569 /* Direct child node of the component image node */
570 fit_image_print_verification_data(fit, noffset, p);
575 void fit_print_contents(const void *fit) { }
576 void fit_image_print(const void *fit, int image_noffset, const char *p) { }
577 #endif /* CONFIG_IS_ENABLED(FIR_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT) */
580 * fit_get_desc - get node description property
581 * @fit: pointer to the FIT format image header
582 * @noffset: node offset
583 * @desc: double pointer to the char, will hold pointer to the description
585 * fit_get_desc() reads description property from a given node, if
586 * description is found pointer to it is returned in third call argument.
592 int fit_get_desc(const void *fit, int noffset, char **desc)
596 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
598 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
606 * fit_get_timestamp - get node timestamp property
607 * @fit: pointer to the FIT format image header
608 * @noffset: node offset
609 * @timestamp: pointer to the time_t, will hold read timestamp
611 * fit_get_timestamp() reads timestamp property from given node, if timestamp
612 * is found and has a correct size its value is returned in third call
617 * -1, on property read failure
618 * -2, on wrong timestamp size
620 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
625 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
627 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
630 if (len != sizeof(uint32_t)) {
631 debug("FIT timestamp with incorrect size of (%u)\n", len);
635 *timestamp = uimage_to_cpu(*((uint32_t *)data));
640 * fit_image_get_node - get node offset for component image of a given unit name
641 * @fit: pointer to the FIT format image header
642 * @image_uname: component image node unit name
644 * fit_image_get_node() finds a component image (within the '/images'
645 * node) of a provided unit name. If image is found its node offset is
646 * returned to the caller.
649 * image node offset when found (>=0)
650 * negative number on failure (FDT_ERR_* code)
652 int fit_image_get_node(const void *fit, const char *image_uname)
654 int noffset, images_noffset;
656 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
657 if (images_noffset < 0) {
658 debug("Can't find images parent node '%s' (%s)\n",
659 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
660 return images_noffset;
663 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
665 debug("Can't get node offset for image unit name: '%s' (%s)\n",
666 image_uname, fdt_strerror(noffset));
673 * fit_image_get_os - get os id for a given component image node
674 * @fit: pointer to the FIT format image header
675 * @noffset: component image node offset
676 * @os: pointer to the uint8_t, will hold os numeric id
678 * fit_image_get_os() finds os property in a given component image node.
679 * If the property is found, its (string) value is translated to the numeric
680 * id which is returned to the caller.
686 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
691 /* Get OS name from property data */
692 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
694 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
699 /* Translate OS name to id */
700 *os = genimg_get_os_id(data);
705 * fit_image_get_arch - get arch id for a given component image node
706 * @fit: pointer to the FIT format image header
707 * @noffset: component image node offset
708 * @arch: pointer to the uint8_t, will hold arch numeric id
710 * fit_image_get_arch() finds arch property in a given component image node.
711 * If the property is found, its (string) value is translated to the numeric
712 * id which is returned to the caller.
718 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
723 /* Get architecture name from property data */
724 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
726 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
731 /* Translate architecture name to id */
732 *arch = genimg_get_arch_id(data);
737 * fit_image_get_type - get type id for a given component image node
738 * @fit: pointer to the FIT format image header
739 * @noffset: component image node offset
740 * @type: pointer to the uint8_t, will hold type numeric id
742 * fit_image_get_type() finds type property in a given component image node.
743 * If the property is found, its (string) value is translated to the numeric
744 * id which is returned to the caller.
750 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
755 /* Get image type name from property data */
756 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
758 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
763 /* Translate image type name to id */
764 *type = genimg_get_type_id(data);
769 * fit_image_get_comp - get comp id for a given component image node
770 * @fit: pointer to the FIT format image header
771 * @noffset: component image node offset
772 * @comp: pointer to the uint8_t, will hold comp numeric id
774 * fit_image_get_comp() finds comp property in a given component image node.
775 * If the property is found, its (string) value is translated to the numeric
776 * id which is returned to the caller.
782 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
787 /* Get compression name from property data */
788 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
790 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
795 /* Translate compression name to id */
796 *comp = genimg_get_comp_id(data);
800 static int fit_image_get_address(const void *fit, int noffset, char *name,
807 cell = fdt_getprop(fit, noffset, name, &len);
809 fit_get_debug(fit, noffset, name, len);
814 /* Use load64 to avoid compiling warning for 32-bit target */
816 load64 = (load64 << 32) | uimage_to_cpu(*cell);
820 if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) {
821 printf("Unsupported %s address size\n", name);
825 *load = (ulong)load64;
830 * fit_image_get_load() - get load addr property for given component image node
831 * @fit: pointer to the FIT format image header
832 * @noffset: component image node offset
833 * @load: pointer to the uint32_t, will hold load address
835 * fit_image_get_load() finds load address property in a given component
836 * image node. If the property is found, its value is returned to the caller.
842 int fit_image_get_load(const void *fit, int noffset, ulong *load)
844 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
848 * fit_image_get_entry() - get entry point address property
849 * @fit: pointer to the FIT format image header
850 * @noffset: component image node offset
851 * @entry: pointer to the uint32_t, will hold entry point address
853 * This gets the entry point address property for a given component image
856 * fit_image_get_entry() finds entry point address property in a given
857 * component image node. If the property is found, its value is returned
864 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
866 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
870 * fit_image_get_data - get data property and its size for a given component image node
871 * @fit: pointer to the FIT format image header
872 * @noffset: component image node offset
873 * @data: double pointer to void, will hold data property's data address
874 * @size: pointer to size_t, will hold data property's data size
876 * fit_image_get_data() finds data property in a given component image node.
877 * If the property is found its data start address and size are returned to
884 int fit_image_get_data(const void *fit, int noffset,
885 const void **data, size_t *size)
889 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
891 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
901 * Get 'data-offset' property from a given image node.
903 * @fit: pointer to the FIT image header
904 * @noffset: component image node offset
905 * @data_offset: holds the data-offset property
909 * -ENOENT if the property could not be found
911 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
915 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
919 *data_offset = fdt32_to_cpu(*val);
925 * Get 'data-position' property from a given image node.
927 * @fit: pointer to the FIT image header
928 * @noffset: component image node offset
929 * @data_position: holds the data-position property
933 * -ENOENT if the property could not be found
935 int fit_image_get_data_position(const void *fit, int noffset,
940 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
944 *data_position = fdt32_to_cpu(*val);
950 * Get 'data-size' property from a given image node.
952 * @fit: pointer to the FIT image header
953 * @noffset: component image node offset
954 * @data_size: holds the data-size property
958 * -ENOENT if the property could not be found
960 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
964 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
968 *data_size = fdt32_to_cpu(*val);
974 * Get 'data-size-unciphered' property from a given image node.
976 * @fit: pointer to the FIT image header
977 * @noffset: component image node offset
978 * @data_size: holds the data-size property
982 * -ENOENT if the property could not be found
984 int fit_image_get_data_size_unciphered(const void *fit, int noffset,
989 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
993 *data_size = (size_t)fdt32_to_cpu(*val);
999 * fit_image_get_data_and_size - get data and its size including
1000 * both embedded and external data
1001 * @fit: pointer to the FIT format image header
1002 * @noffset: component image node offset
1003 * @data: double pointer to void, will hold data property's data address
1004 * @size: pointer to size_t, will hold data property's data size
1006 * fit_image_get_data_and_size() finds data and its size including
1007 * both embedded and external data. If the property is found
1008 * its data start address and size are returned to the caller.
1012 * otherwise, on failure
1014 int fit_image_get_data_and_size(const void *fit, int noffset,
1015 const void **data, size_t *size)
1017 bool external_data = false;
1022 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1023 external_data = true;
1024 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1025 external_data = true;
1027 * For FIT with external data, figure out where
1028 * the external images start. This is the base
1029 * for the data-offset properties in each image.
1031 offset += ((fdt_totalsize(fit) + 3) & ~3);
1034 if (external_data) {
1035 debug("External Data\n");
1036 ret = fit_image_get_data_size(fit, noffset, &len);
1038 *data = fit + offset;
1042 ret = fit_image_get_data(fit, noffset, data, size);
1049 * fit_image_hash_get_algo - get hash algorithm name
1050 * @fit: pointer to the FIT format image header
1051 * @noffset: hash node offset
1052 * @algo: double pointer to char, will hold pointer to the algorithm name
1054 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1055 * If the property is found its data start address is returned to the caller.
1061 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1065 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1066 if (*algo == NULL) {
1067 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1075 * fit_image_hash_get_value - get hash value and length
1076 * @fit: pointer to the FIT format image header
1077 * @noffset: hash node offset
1078 * @value: double pointer to uint8_t, will hold address of a hash value data
1079 * @value_len: pointer to an int, will hold hash data length
1081 * fit_image_hash_get_value() finds hash value property in a given hash node.
1082 * If the property is found its data start address and size are returned to
1089 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1094 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1095 if (*value == NULL) {
1096 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1106 * fit_image_hash_get_ignore - get hash ignore flag
1107 * @fit: pointer to the FIT format image header
1108 * @noffset: hash node offset
1109 * @ignore: pointer to an int, will hold hash ignore flag
1111 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1112 * If the property is found and non-zero, the hash algorithm is not verified by
1113 * u-boot automatically.
1116 * 0, on ignore not found
1117 * value, on ignore found
1119 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1124 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1125 if (value == NULL || len != sizeof(int))
1134 * fit_image_cipher_get_algo - get cipher algorithm name
1135 * @fit: pointer to the FIT format image header
1136 * @noffset: cipher node offset
1137 * @algo: double pointer to char, will hold pointer to the algorithm name
1139 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1140 * cipher node. If the property is found its data start address is returned
1147 int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1151 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1153 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1160 ulong fit_get_end(const void *fit)
1162 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1166 * fit_set_timestamp - set node timestamp property
1167 * @fit: pointer to the FIT format image header
1168 * @noffset: node offset
1169 * @timestamp: timestamp value to be set
1171 * fit_set_timestamp() attempts to set timestamp property in the requested
1172 * node and returns operation status to the caller.
1176 * -ENOSPC if no space in device tree, -1 for other error
1178 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1183 t = cpu_to_uimage(timestamp);
1184 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1187 debug("Can't set '%s' property for '%s' node (%s)\n",
1188 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1190 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1197 * calculate_hash - calculate and return hash for provided input data
1198 * @data: pointer to the input data
1199 * @data_len: data length
1200 * @algo: requested hash algorithm
1201 * @value: pointer to the char, will hold hash value data (caller must
1202 * allocate enough free space)
1203 * value_len: length of the calculated hash
1205 * calculate_hash() computes input data hash according to the requested
1207 * Resulting hash value is placed in caller provided 'value' buffer, length
1208 * of the calculated hash is returned via value_len pointer argument.
1212 * -1, when algo is unsupported
1214 int calculate_hash(const void *data, int data_len, const char *algo,
1215 uint8_t *value, int *value_len)
1217 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1218 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1220 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1222 } else if (CONFIG_IS_ENABLED(SHA1) && strcmp(algo, "sha1") == 0) {
1223 sha1_csum_wd((unsigned char *)data, data_len,
1224 (unsigned char *)value, CHUNKSZ_SHA1);
1226 } else if (CONFIG_IS_ENABLED(SHA256) && strcmp(algo, "sha256") == 0) {
1227 sha256_csum_wd((unsigned char *)data, data_len,
1228 (unsigned char *)value, CHUNKSZ_SHA256);
1229 *value_len = SHA256_SUM_LEN;
1230 } else if (CONFIG_IS_ENABLED(SHA384) && strcmp(algo, "sha384") == 0) {
1231 sha384_csum_wd((unsigned char *)data, data_len,
1232 (unsigned char *)value, CHUNKSZ_SHA384);
1233 *value_len = SHA384_SUM_LEN;
1234 } else if (CONFIG_IS_ENABLED(SHA512) && strcmp(algo, "sha512") == 0) {
1235 sha512_csum_wd((unsigned char *)data, data_len,
1236 (unsigned char *)value, CHUNKSZ_SHA512);
1237 *value_len = SHA512_SUM_LEN;
1238 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1239 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1242 debug("Unsupported hash alogrithm\n");
1248 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1249 size_t size, char **err_msgp)
1251 uint8_t value[FIT_MAX_HASH_LEN];
1260 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1261 *err_msgp = "Can't get hash algo property";
1266 if (IMAGE_ENABLE_IGNORE) {
1267 fit_image_hash_get_ignore(fit, noffset, &ignore);
1269 printf("-skipped ");
1274 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1276 *err_msgp = "Can't get hash value property";
1280 if (calculate_hash(data, size, algo, value, &value_len)) {
1281 *err_msgp = "Unsupported hash algorithm";
1285 if (value_len != fit_value_len) {
1286 *err_msgp = "Bad hash value len";
1288 } else if (memcmp(value, fit_value, value_len) != 0) {
1289 *err_msgp = "Bad hash value";
1296 int fit_image_verify_with_data(const void *fit, int image_noffset,
1297 const void *data, size_t size)
1304 /* Verify all required signatures */
1305 if (FIT_IMAGE_ENABLE_VERIFY &&
1306 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1307 gd_fdt_blob(), &verify_all)) {
1308 err_msg = "Unable to verify required signature";
1312 /* Process all hash subnodes of the component image node */
1313 fdt_for_each_subnode(noffset, fit, image_noffset) {
1314 const char *name = fit_get_name(fit, noffset, NULL);
1317 * Check subnode name, must be equal to "hash".
1318 * Multiple hash nodes require unique unit node
1319 * names, e.g. hash-1, hash-2, etc.
1321 if (!strncmp(name, FIT_HASH_NODENAME,
1322 strlen(FIT_HASH_NODENAME))) {
1323 if (fit_image_check_hash(fit, noffset, data, size,
1327 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
1328 !strncmp(name, FIT_SIG_NODENAME,
1329 strlen(FIT_SIG_NODENAME))) {
1330 ret = fit_image_check_sig(fit, noffset, data,
1331 size, -1, &err_msg);
1334 * Show an indication on failure, but do not return
1335 * an error. Only keys marked 'required' can cause
1336 * an image validation failure. See the call to
1337 * fit_image_verify_required_sigs() above.
1346 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1347 err_msg = "Corrupted or truncated tree";
1354 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1355 err_msg, fit_get_name(fit, noffset, NULL),
1356 fit_get_name(fit, image_noffset, NULL));
1361 * fit_image_verify - verify data integrity
1362 * @fit: pointer to the FIT format image header
1363 * @image_noffset: component image node offset
1365 * fit_image_verify() goes over component image hash nodes,
1366 * re-calculates each data hash and compares with the value stored in hash
1370 * 1, if all hashes are valid
1371 * 0, otherwise (or on error)
1373 int fit_image_verify(const void *fit, int image_noffset)
1375 const char *name = fit_get_name(fit, image_noffset, NULL);
1380 if (strchr(name, '@')) {
1382 * We don't support this since libfdt considers names with the
1383 * name root but different @ suffix to be equal
1385 err_msg = "Node name contains @";
1388 /* Get image data and data length */
1389 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1390 err_msg = "Can't get image data/size";
1394 return fit_image_verify_with_data(fit, image_noffset, data, size);
1397 printf("error!\n%s in '%s' image node\n", err_msg,
1398 fit_get_name(fit, image_noffset, NULL));
1403 * fit_all_image_verify - verify data integrity for all images
1404 * @fit: pointer to the FIT format image header
1406 * fit_all_image_verify() goes over all images in the FIT and
1407 * for every images checks if all it's hashes are valid.
1410 * 1, if all hashes of all images are valid
1411 * 0, otherwise (or on error)
1413 int fit_all_image_verify(const void *fit)
1420 /* Find images parent node offset */
1421 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1422 if (images_noffset < 0) {
1423 printf("Can't find images parent node '%s' (%s)\n",
1424 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1428 /* Process all image subnodes, check hashes for each */
1429 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1431 for (ndepth = 0, count = 0,
1432 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1433 (noffset >= 0) && (ndepth > 0);
1434 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1437 * Direct child node of the images parent node,
1438 * i.e. component image node.
1440 printf(" Hash(es) for Image %u (%s): ", count,
1441 fit_get_name(fit, noffset, NULL));
1444 if (!fit_image_verify(fit, noffset))
1452 static int fit_image_uncipher(const void *fit, int image_noffset,
1453 void **data, size_t *size)
1455 int cipher_noffset, ret;
1459 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1460 FIT_CIPHER_NODENAME);
1461 if (cipher_noffset < 0)
1464 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1465 *data, *size, &dst, &size_dst);
1477 * fit_image_check_os - check whether image node is of a given os type
1478 * @fit: pointer to the FIT format image header
1479 * @noffset: component image node offset
1480 * @os: requested image os
1482 * fit_image_check_os() reads image os property and compares its numeric
1483 * id with the requested os. Comparison result is returned to the caller.
1486 * 1 if image is of given os type
1487 * 0 otherwise (or on error)
1489 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1493 if (fit_image_get_os(fit, noffset, &image_os))
1495 return (os == image_os);
1499 * fit_image_check_arch - check whether image node is of a given arch
1500 * @fit: pointer to the FIT format image header
1501 * @noffset: component image node offset
1502 * @arch: requested imagearch
1504 * fit_image_check_arch() reads image arch property and compares its numeric
1505 * id with the requested arch. Comparison result is returned to the caller.
1508 * 1 if image is of given arch
1509 * 0 otherwise (or on error)
1511 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1514 int aarch32_support = 0;
1516 /* Let's assume that sandbox can load any architecture */
1517 if (IS_ENABLED(CONFIG_SANDBOX))
1520 if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32))
1521 aarch32_support = 1;
1523 if (fit_image_get_arch(fit, noffset, &image_arch))
1525 return (arch == image_arch) ||
1526 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1527 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1532 * fit_image_check_type - check whether image node is of a given type
1533 * @fit: pointer to the FIT format image header
1534 * @noffset: component image node offset
1535 * @type: requested image type
1537 * fit_image_check_type() reads image type property and compares its numeric
1538 * id with the requested type. Comparison result is returned to the caller.
1541 * 1 if image is of given type
1542 * 0 otherwise (or on error)
1544 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1548 if (fit_image_get_type(fit, noffset, &image_type))
1550 return (type == image_type);
1554 * fit_image_check_comp - check whether image node uses given compression
1555 * @fit: pointer to the FIT format image header
1556 * @noffset: component image node offset
1557 * @comp: requested image compression type
1559 * fit_image_check_comp() reads image compression property and compares its
1560 * numeric id with the requested compression type. Comparison result is
1561 * returned to the caller.
1564 * 1 if image uses requested compression
1565 * 0 otherwise (or on error)
1567 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1571 if (fit_image_get_comp(fit, noffset, &image_comp))
1573 return (comp == image_comp);
1577 * fdt_check_no_at() - Check for nodes whose names contain '@'
1579 * This checks the parent node and all subnodes recursively
1581 * @fit: FIT to check
1582 * @parent: Parent node to check
1583 * @return 0 if OK, -EADDRNOTAVAIL is a node has a name containing '@'
1585 static int fdt_check_no_at(const void *fit, int parent)
1591 name = fdt_get_name(fit, parent, NULL);
1592 if (!name || strchr(name, '@'))
1593 return -EADDRNOTAVAIL;
1595 fdt_for_each_subnode(node, fit, parent) {
1596 ret = fdt_check_no_at(fit, node);
1604 int fit_check_format(const void *fit, ulong size)
1608 /* A FIT image must be a valid FDT */
1609 ret = fdt_check_header(fit);
1611 log_debug("Wrong FIT format: not a flattened device tree (err=%d)\n",
1616 if (CONFIG_IS_ENABLED(FIT_FULL_CHECK)) {
1618 * If we are not given the size, make do wtih calculating it.
1619 * This is not as secure, so we should consider a flag to
1622 if (size == IMAGE_SIZE_INVAL)
1623 size = fdt_totalsize(fit);
1624 ret = fdt_check_full(fit, size);
1629 * U-Boot stopped using unit addressed in 2017. Since libfdt
1630 * can match nodes ignoring any unit address, signature
1631 * verification can see the wrong node if one is inserted with
1632 * the same name as a valid node but with a unit address
1633 * attached. Protect against this by disallowing unit addresses.
1635 if (!ret && CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
1636 ret = fdt_check_no_at(fit, 0);
1639 log_debug("FIT check error %d\n", ret);
1644 log_debug("FIT check error %d\n", ret);
1649 /* mandatory / node 'description' property */
1650 if (!fdt_getprop(fit, 0, FIT_DESC_PROP, NULL)) {
1651 log_debug("Wrong FIT format: no description\n");
1655 if (IMAGE_ENABLE_TIMESTAMP) {
1656 /* mandatory / node 'timestamp' property */
1657 if (!fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL)) {
1658 log_debug("Wrong FIT format: no timestamp\n");
1663 /* mandatory subimages parent '/images' node */
1664 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1665 log_debug("Wrong FIT format: no images parent node\n");
1673 * fit_conf_find_compat
1674 * @fit: pointer to the FIT format image header
1675 * @fdt: pointer to the device tree to compare against
1677 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1678 * most compatible with the passed in device tree.
1687 * |-o configurations
1695 * |-compatible = "foo,bar", "bim,bam"
1698 * |-compatible = "foo,bar",
1701 * |-compatible = "bim,bam", "baz,biz"
1703 * Configuration 1 would be picked because the first string in U-Boot's
1704 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1705 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1707 * As an optimization, the compatible property from the FDT's root node can be
1708 * copied into the configuration node in the FIT image. This is required to
1709 * match configurations with compressed FDTs.
1712 * offset to the configuration to use if one was found
1715 int fit_conf_find_compat(const void *fit, const void *fdt)
1718 int noffset, confs_noffset, images_noffset;
1719 const void *fdt_compat;
1721 int best_match_offset = 0;
1722 int best_match_pos = 0;
1724 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1725 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1726 if (confs_noffset < 0 || images_noffset < 0) {
1727 debug("Can't find configurations or images nodes.\n");
1731 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1733 debug("Fdt for comparison has no \"compatible\" property.\n");
1738 * Loop over the configurations in the FIT image.
1740 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1741 (noffset >= 0) && (ndepth > 0);
1742 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1744 const char *kfdt_name;
1745 int kfdt_noffset, compat_noffset;
1746 const char *cur_fdt_compat;
1754 /* If there's a compat property in the config node, use that. */
1755 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1756 fdt = fit; /* search in FIT image */
1757 compat_noffset = noffset; /* search under config node */
1758 } else { /* Otherwise extract it from the kernel FDT. */
1759 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1761 debug("No fdt property found.\n");
1764 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1766 if (kfdt_noffset < 0) {
1767 debug("No image node named \"%s\" found.\n",
1772 if (!fit_image_check_comp(fit, kfdt_noffset,
1774 debug("Can't extract compat from \"%s\" "
1775 "(compressed)\n", kfdt_name);
1779 /* search in this config's kernel FDT */
1780 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1781 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1785 compat_noffset = 0; /* search kFDT under root node */
1788 len = fdt_compat_len;
1789 cur_fdt_compat = fdt_compat;
1791 * Look for a match for each U-Boot compatibility string in
1792 * turn in the compat string property.
1794 for (i = 0; len > 0 &&
1795 (!best_match_offset || best_match_pos > i); i++) {
1796 int cur_len = strlen(cur_fdt_compat) + 1;
1798 if (!fdt_node_check_compatible(fdt, compat_noffset,
1800 best_match_offset = noffset;
1805 cur_fdt_compat += cur_len;
1808 if (!best_match_offset) {
1809 debug("No match found.\n");
1813 return best_match_offset;
1816 int fit_conf_get_node(const void *fit, const char *conf_uname)
1818 int noffset, confs_noffset;
1821 char *conf_uname_copy = NULL;
1823 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1824 if (confs_noffset < 0) {
1825 debug("Can't find configurations parent node '%s' (%s)\n",
1826 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1827 return confs_noffset;
1830 if (conf_uname == NULL) {
1831 /* get configuration unit name from the default property */
1832 debug("No configuration specified, trying default...\n");
1833 if (!host_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) {
1834 noffset = fit_find_config_node(fit);
1837 conf_uname = fdt_get_name(fit, noffset, NULL);
1839 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1840 FIT_DEFAULT_PROP, &len);
1841 if (conf_uname == NULL) {
1842 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1847 debug("Found default configuration: '%s'\n", conf_uname);
1850 s = strchr(conf_uname, '#');
1852 len = s - conf_uname;
1853 conf_uname_copy = malloc(len + 1);
1854 if (!conf_uname_copy) {
1855 debug("Can't allocate uname copy: '%s'\n",
1859 memcpy(conf_uname_copy, conf_uname, len);
1860 conf_uname_copy[len] = '\0';
1861 conf_uname = conf_uname_copy;
1864 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1866 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1867 conf_uname, fdt_strerror(noffset));
1870 if (conf_uname_copy)
1871 free(conf_uname_copy);
1876 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1877 const char *prop_name)
1879 return fdt_stringlist_count(fit, noffset, prop_name);
1882 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1883 const char *prop_name, int index)
1888 /* get kernel image unit name from configuration kernel property */
1889 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1893 return fit_image_get_node(fit, uname);
1896 int fit_conf_get_prop_node(const void *fit, int noffset,
1897 const char *prop_name)
1899 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1902 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1904 fit_image_print(fit, rd_noffset, " ");
1907 puts(" Verifying Hash Integrity ... ");
1908 if (!fit_image_verify(fit, rd_noffset)) {
1909 puts("Bad Data Hash\n");
1918 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1925 debug("* %s: using config '%s' from image at 0x%08lx\n",
1926 prop_name, images->fit_uname_cfg, addr);
1928 /* Check whether configuration has this property defined */
1929 fit_hdr = map_sysmem(addr, 0);
1930 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1931 if (cfg_noffset < 0) {
1932 debug("* %s: no such config\n", prop_name);
1936 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1938 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1946 * fit_get_image_type_property() - get property name for IH_TYPE_...
1948 * @return the properly name where we expect to find the image in the
1951 static const char *fit_get_image_type_property(int type)
1954 * This is sort-of available in the uimage_type[] table in image.c
1955 * but we don't have access to the short name, and "fdt" is different
1956 * anyway. So let's just keep it here.
1959 case IH_TYPE_FLATDT:
1960 return FIT_FDT_PROP;
1961 case IH_TYPE_KERNEL:
1962 return FIT_KERNEL_PROP;
1963 case IH_TYPE_FIRMWARE:
1964 return FIT_FIRMWARE_PROP;
1965 case IH_TYPE_RAMDISK:
1966 return FIT_RAMDISK_PROP;
1967 case IH_TYPE_X86_SETUP:
1968 return FIT_SETUP_PROP;
1969 case IH_TYPE_LOADABLE:
1970 return FIT_LOADABLE_PROP;
1972 return FIT_FPGA_PROP;
1973 case IH_TYPE_STANDALONE:
1974 return FIT_STANDALONE_PROP;
1980 int fit_image_load(bootm_headers_t *images, ulong addr,
1981 const char **fit_unamep, const char **fit_uname_configp,
1982 int arch, int image_type, int bootstage_id,
1983 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1985 int cfg_noffset, noffset;
1986 const char *fit_uname;
1987 const char *fit_uname_config;
1988 const char *fit_base_uname_config;
1994 ulong load, load_end, data, len;
1999 const char *prop_name;
2002 fit = map_sysmem(addr, 0);
2003 fit_uname = fit_unamep ? *fit_unamep : NULL;
2004 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
2005 fit_base_uname_config = NULL;
2006 prop_name = fit_get_image_type_property(image_type);
2007 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
2009 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
2010 ret = fit_check_format(fit, IMAGE_SIZE_INVAL);
2012 printf("Bad FIT %s image format! (err=%d)\n", prop_name, ret);
2013 if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && ret == -EADDRNOTAVAIL)
2014 printf("Signature checking prevents use of unit addresses (@) in nodes\n");
2015 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
2018 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
2020 /* get FIT component image node offset */
2021 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
2022 noffset = fit_image_get_node(fit, fit_uname);
2025 * no image node unit name, try to get config
2026 * node first. If config unit node name is NULL
2027 * fit_conf_get_node() will try to find default config node
2029 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
2030 if (IS_ENABLED(CONFIG_FIT_BEST_MATCH) && !fit_uname_config) {
2031 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
2033 cfg_noffset = fit_conf_get_node(fit,
2036 if (cfg_noffset < 0) {
2037 puts("Could not find configuration node\n");
2038 bootstage_error(bootstage_id +
2039 BOOTSTAGE_SUB_NO_UNIT_NAME);
2043 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
2044 printf(" Using '%s' configuration\n", fit_base_uname_config);
2045 /* Remember this config */
2046 if (image_type == IH_TYPE_KERNEL)
2047 images->fit_uname_cfg = fit_base_uname_config;
2049 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
2050 puts(" Verifying Hash Integrity ... ");
2051 if (fit_config_verify(fit, cfg_noffset)) {
2052 puts("Bad Data Hash\n");
2053 bootstage_error(bootstage_id +
2054 BOOTSTAGE_SUB_HASH);
2060 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
2062 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
2064 fit_uname = fit_get_name(fit, noffset, NULL);
2067 printf("Could not find subimage node type '%s'\n", prop_name);
2068 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
2072 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
2074 ret = fit_image_select(fit, noffset, images->verify);
2076 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2080 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2081 if (!host_build() && IS_ENABLED(CONFIG_SANDBOX)) {
2082 if (!fit_image_check_target_arch(fit, noffset)) {
2083 puts("Unsupported Architecture\n");
2084 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2090 fit_image_get_arch(fit, noffset, &os_arch);
2091 images->os.arch = os_arch;
2094 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2095 type_ok = fit_image_check_type(fit, noffset, image_type) ||
2096 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
2097 fit_image_check_type(fit, noffset, IH_TYPE_TEE) ||
2098 (image_type == IH_TYPE_KERNEL &&
2099 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
2101 os_ok = image_type == IH_TYPE_FLATDT ||
2102 image_type == IH_TYPE_FPGA ||
2103 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
2104 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
2105 fit_image_check_os(fit, noffset, IH_OS_TEE) ||
2106 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
2107 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
2108 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
2111 * If either of the checks fail, we should report an error, but
2112 * if the image type is coming from the "loadables" field, we
2113 * don't care what it is
2115 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
2116 fit_image_get_os(fit, noffset, &os);
2117 printf("No %s %s %s Image\n",
2118 genimg_get_os_name(os),
2119 genimg_get_arch_name(arch),
2120 genimg_get_type_name(image_type));
2121 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2125 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2127 /* get image data address and length */
2128 if (fit_image_get_data_and_size(fit, noffset,
2129 (const void **)&buf, &size)) {
2130 printf("Could not find %s subimage data!\n", prop_name);
2131 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2135 /* Decrypt data before uncompress/move */
2136 if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
2137 puts(" Decrypting Data ... ");
2138 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2145 /* perform any post-processing on the image data */
2146 if (!host_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
2147 board_fit_image_post_process(fit, noffset, &buf, &size);
2151 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2153 data = map_to_sysmem(buf);
2155 if (load_op == FIT_LOAD_IGNORED) {
2157 } else if (fit_image_get_load(fit, noffset, &load)) {
2158 if (load_op == FIT_LOAD_REQUIRED) {
2159 printf("Can't get %s subimage load address!\n",
2161 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2164 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
2165 ulong image_start, image_end;
2168 * move image data to the load address,
2169 * make sure we don't overwrite initial image
2172 image_end = addr + fit_get_size(fit);
2174 load_end = load + len;
2175 if (image_type != IH_TYPE_KERNEL &&
2176 load < image_end && load_end > image_start) {
2177 printf("Error: %s overwritten\n", prop_name);
2181 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2182 prop_name, data, load);
2184 load = data; /* No load address specified */
2187 comp = IH_COMP_NONE;
2189 /* Kernel images get decompressed later in bootm_load_os(). */
2190 if (!fit_image_get_comp(fit, noffset, &comp) &&
2191 comp != IH_COMP_NONE &&
2192 !(image_type == IH_TYPE_KERNEL ||
2193 image_type == IH_TYPE_KERNEL_NOLOAD ||
2194 image_type == IH_TYPE_RAMDISK)) {
2195 ulong max_decomp_len = len * 20;
2197 loadbuf = malloc(max_decomp_len);
2198 load = map_to_sysmem(loadbuf);
2200 loadbuf = map_sysmem(load, max_decomp_len);
2202 if (image_decomp(comp, load, data, image_type,
2203 loadbuf, buf, len, max_decomp_len, &load_end)) {
2204 printf("Error decompressing %s\n", prop_name);
2208 len = load_end - load;
2209 } else if (load != data) {
2210 loadbuf = map_sysmem(load, len);
2211 memcpy(loadbuf, buf, len);
2214 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2215 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2216 " please fix your .its file!\n");
2218 /* verify that image data is a proper FDT blob */
2219 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2220 puts("Subimage data is not a FDT");
2224 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2229 *fit_unamep = (char *)fit_uname;
2230 if (fit_uname_configp)
2231 *fit_uname_configp = (char *)(fit_uname_config ? :
2232 fit_base_uname_config);
2237 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2238 ulong *setup_start, ulong *setup_len)
2245 addr = map_to_sysmem(images->fit_hdr_os);
2246 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2250 ret = fit_image_load(images, addr, NULL, NULL, arch,
2251 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2252 FIT_LOAD_REQUIRED, setup_start, &len);
2258 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2259 const char **fit_unamep, const char **fit_uname_configp,
2260 int arch, ulong *datap, ulong *lenp)
2262 int fdt_noffset, cfg_noffset, count;
2264 const char *fit_uname = NULL;
2265 const char *fit_uname_config = NULL;
2266 char *fit_uname_config_copy = NULL;
2267 char *next_config = NULL;
2269 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2270 ulong image_start, image_end;
2271 ulong ovload, ovlen, ovcopylen;
2272 const char *uconfig;
2274 void *base, *ov, *ovcopy = NULL;
2275 int i, err, noffset, ov_noffset;
2278 fit_uname = fit_unamep ? *fit_unamep : NULL;
2280 if (fit_uname_configp && *fit_uname_configp) {
2281 fit_uname_config_copy = strdup(*fit_uname_configp);
2282 if (!fit_uname_config_copy)
2285 next_config = strchr(fit_uname_config_copy, '#');
2287 *next_config++ = '\0';
2288 if (next_config - 1 > fit_uname_config_copy)
2289 fit_uname_config = fit_uname_config_copy;
2292 fdt_noffset = fit_image_load(images,
2293 addr, &fit_uname, &fit_uname_config,
2294 arch, IH_TYPE_FLATDT,
2295 BOOTSTAGE_ID_FIT_FDT_START,
2296 FIT_LOAD_OPTIONAL, &load, &len);
2298 if (fdt_noffset < 0)
2301 debug("fit_uname=%s, fit_uname_config=%s\n",
2302 fit_uname ? fit_uname : "<NULL>",
2303 fit_uname_config ? fit_uname_config : "<NULL>");
2305 fit = map_sysmem(addr, 0);
2307 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2309 /* single blob, or error just return as well */
2310 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2311 if (count <= 1 && !next_config)
2314 /* we need to apply overlays */
2316 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2318 image_end = addr + fit_get_size(fit);
2319 /* verify that relocation took place by load address not being in fit */
2320 if (load >= image_start && load < image_end) {
2321 /* check is simplified; fit load checks for overlaps */
2322 printf("Overlayed FDT requires relocation\n");
2323 fdt_noffset = -EBADF;
2327 base = map_sysmem(load, len);
2329 /* apply extra configs in FIT first, followed by args */
2330 for (i = 1; ; i++) {
2332 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2334 uname = fit_get_name(fit, noffset, NULL);
2339 uconfig = next_config;
2340 next_config = strchr(next_config, '#');
2342 *next_config++ = '\0';
2346 * fit_image_load() would load the first FDT from the
2347 * extra config only when uconfig is specified.
2348 * Check if the extra config contains multiple FDTs and
2351 cfg_noffset = fit_conf_get_node(fit, uconfig);
2354 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2358 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2360 ov_noffset = fit_image_load(images,
2361 addr, &uname, &uconfig,
2362 arch, IH_TYPE_FLATDT,
2363 BOOTSTAGE_ID_FIT_FDT_START,
2364 FIT_LOAD_IGNORED, &ovload, &ovlen);
2365 if (ov_noffset < 0) {
2366 printf("load of %s failed\n", uname);
2369 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2370 uname, ovload, ovlen);
2371 ov = map_sysmem(ovload, ovlen);
2373 ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K);
2374 ovcopy = malloc(ovcopylen);
2376 printf("failed to duplicate DTO before application\n");
2377 fdt_noffset = -ENOMEM;
2381 err = fdt_open_into(ov, ovcopy, ovcopylen);
2383 printf("failed on fdt_open_into for DTO\n");
2388 base = map_sysmem(load, len + ovlen);
2389 err = fdt_open_into(base, base, len + ovlen);
2391 printf("failed on fdt_open_into\n");
2396 /* the verbose method prints out messages on error */
2397 err = fdt_overlay_apply_verbose(base, ovcopy);
2403 len = fdt_totalsize(base);
2409 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2410 fdt_noffset = -EBADF;
2419 *fit_unamep = fit_uname;
2420 if (fit_uname_configp)
2421 *fit_uname_configp = fit_uname_config;
2423 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2427 if (fit_uname_config_copy)
2428 free(fit_uname_config_copy);