1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
5 * (C) Copyright 2008 Semihalf
7 * (C) Copyright 2000-2006
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>
28 #include <asm/global_data.h>
31 #include <u-boot/hash.h>
33 DECLARE_GLOBAL_DATA_PTR;
34 #endif /* !USE_HOSTCC*/
38 #include <bootstage.h>
39 #include <linux/kconfig.h>
40 #include <u-boot/crc.h>
41 #include <u-boot/md5.h>
42 #include <u-boot/sha1.h>
43 #include <u-boot/sha256.h>
44 #include <u-boot/sha512.h>
46 /*****************************************************************************/
47 /* New uImage format routines */
48 /*****************************************************************************/
50 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
51 ulong *addr, const char **name)
58 sep = strchr(spec, sepc);
61 *addr = hextoul(spec, NULL);
71 * fit_parse_conf - parse FIT configuration spec
72 * @spec: input string, containing configuration spec
73 * @add_curr: current image address (to be used as a possible default)
74 * @addr: pointer to a ulong variable, will hold FIT image address of a given
76 * @conf_name double pointer to a char, will hold pointer to a configuration
79 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
80 * where <addr> is a FIT image address that contains configuration
81 * with a <conf> unit name.
83 * Address part is optional, and if omitted default add_curr will
87 * 1 if spec is a valid configuration string,
88 * addr and conf_name are set accordingly
91 int fit_parse_conf(const char *spec, ulong addr_curr,
92 ulong *addr, const char **conf_name)
94 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
98 * fit_parse_subimage - parse FIT subimage spec
99 * @spec: input string, containing subimage spec
100 * @add_curr: current image address (to be used as a possible default)
101 * @addr: pointer to a ulong variable, will hold FIT image address of a given
103 * @image_name: double pointer to a char, will hold pointer to a subimage name
105 * fit_parse_subimage() expects subimage spec in the form of
106 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
107 * subimage with a <subimg> unit name.
109 * Address part is optional, and if omitted default add_curr will
113 * 1 if spec is a valid subimage string,
114 * addr and image_name are set accordingly
117 int fit_parse_subimage(const char *spec, ulong addr_curr,
118 ulong *addr, const char **image_name)
120 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
122 #endif /* !USE_HOSTCC */
125 /* Host tools use these implementations for Cipher and Signature support */
126 static void *host_blob;
128 void image_set_host_blob(void *blob)
133 void *image_get_host_blob(void)
137 #endif /* USE_HOSTCC */
139 static void fit_get_debug(const void *fit, int noffset,
140 char *prop_name, int err)
142 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
143 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
148 * fit_get_subimage_count - get component (sub-image) count
149 * @fit: pointer to the FIT format image header
150 * @images_noffset: offset of images node
153 * number of image components
155 int fit_get_subimage_count(const void *fit, int images_noffset)
161 /* Process its subnodes, print out component images details */
162 for (ndepth = 0, count = 0,
163 noffset = fdt_next_node(fit, images_noffset, &ndepth);
164 (noffset >= 0) && (ndepth > 0);
165 noffset = fdt_next_node(fit, noffset, &ndepth)) {
175 * fit_image_print_data() - prints out the hash node details
176 * @fit: pointer to the FIT format image header
177 * @noffset: offset of the hash node
178 * @p: pointer to prefix string
179 * @type: Type of information to print ("hash" or "sign")
181 * fit_image_print_data() lists properties for the processed hash node
183 * This function avoid using puts() since it prints a newline on the host
184 * but does not in U-Boot.
187 * no returned results
189 static void fit_image_print_data(const void *fit, int noffset, const char *p,
200 debug("%s %s node: '%s'\n", p, type,
201 fit_get_name(fit, noffset, NULL));
202 printf("%s %s algo: ", p, type);
203 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
204 printf("invalid/unsupported\n");
208 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
209 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
211 printf(":%s", keyname);
213 printf(" (required)");
216 padding = fdt_getprop(fit, noffset, "padding", NULL);
218 printf("%s %s padding: %s\n", p, type, padding);
220 ret = fit_image_hash_get_value(fit, noffset, &value,
222 printf("%s %s value: ", p, type);
224 printf("unavailable\n");
226 for (i = 0; i < value_len; i++)
227 printf("%02x", value[i]);
231 debug("%s %s len: %d\n", p, type, value_len);
233 /* Signatures have a time stamp */
234 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
237 printf("%s Timestamp: ", p);
238 if (fit_get_timestamp(fit, noffset, ×tamp))
239 printf("unavailable\n");
241 genimg_print_time(timestamp);
246 * fit_image_print_verification_data() - prints out the hash/signature details
247 * @fit: pointer to the FIT format image header
248 * @noffset: offset of the hash or signature node
249 * @p: pointer to prefix string
251 * This lists properties for the processed hash node
254 * no returned results
256 static void fit_image_print_verification_data(const void *fit, int noffset,
262 * Check subnode name, must be equal to "hash" or "signature".
263 * Multiple hash/signature nodes require unique unit node
264 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
266 name = fit_get_name(fit, noffset, NULL);
267 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
268 fit_image_print_data(fit, noffset, p, "Hash");
269 } else if (!strncmp(name, FIT_SIG_NODENAME,
270 strlen(FIT_SIG_NODENAME))) {
271 fit_image_print_data(fit, noffset, p, "Sign");
276 * fit_conf_print - prints out the FIT configuration details
277 * @fit: pointer to the FIT format image header
278 * @noffset: offset of the configuration node
279 * @p: pointer to prefix string
281 * fit_conf_print() lists all mandatory properties for the processed
282 * configuration node.
285 * no returned results
287 static void fit_conf_print(const void *fit, int noffset, const char *p)
292 int fdt_index, loadables_index;
295 /* Mandatory properties */
296 ret = fit_get_desc(fit, noffset, &desc);
297 printf("%s Description: ", p);
299 printf("unavailable\n");
301 printf("%s\n", desc);
303 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
304 printf("%s Kernel: ", p);
306 printf("unavailable\n");
308 printf("%s\n", uname);
310 /* Optional properties */
311 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
313 printf("%s Init Ramdisk: %s\n", p, uname);
315 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
317 printf("%s Firmware: %s\n", p, uname);
320 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
321 fdt_index, NULL), uname;
324 printf("%s FDT: ", p);
327 printf("%s\n", uname);
330 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
332 printf("%s FPGA: %s\n", p, uname);
334 /* Print out all of the specified loadables */
335 for (loadables_index = 0;
336 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
337 loadables_index, NULL), uname;
339 if (loadables_index == 0) {
340 printf("%s Loadables: ", p);
344 printf("%s\n", uname);
347 /* Process all hash subnodes of the component configuration node */
348 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
349 (noffset >= 0) && (ndepth > 0);
350 noffset = fdt_next_node(fit, noffset, &ndepth)) {
352 /* Direct child node of the component configuration node */
353 fit_image_print_verification_data(fit, noffset, p);
359 * fit_print_contents - prints out the contents of the FIT format image
360 * @fit: pointer to the FIT format image header
361 * @p: pointer to prefix string
363 * fit_print_contents() formats a multi line FIT image contents description.
364 * The routine prints out FIT image properties (root node level) followed by
365 * the details of each component image.
368 * no returned results
370 void fit_print_contents(const void *fit)
383 if (!CONFIG_IS_ENABLED(FIT_PRINT))
386 /* Indent string is defined in header image.h */
387 p = IMAGE_INDENT_STRING;
389 /* Root node properties */
390 ret = fit_get_desc(fit, 0, &desc);
391 printf("%sFIT description: ", p);
393 printf("unavailable\n");
395 printf("%s\n", desc);
397 if (IMAGE_ENABLE_TIMESTAMP) {
398 ret = fit_get_timestamp(fit, 0, ×tamp);
399 printf("%sCreated: ", p);
401 printf("unavailable\n");
403 genimg_print_time(timestamp);
406 /* Find images parent node offset */
407 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
408 if (images_noffset < 0) {
409 printf("Can't find images parent node '%s' (%s)\n",
410 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
414 /* Process its subnodes, print out component images details */
415 for (ndepth = 0, count = 0,
416 noffset = fdt_next_node(fit, images_noffset, &ndepth);
417 (noffset >= 0) && (ndepth > 0);
418 noffset = fdt_next_node(fit, noffset, &ndepth)) {
421 * Direct child node of the images parent node,
422 * i.e. component image node.
424 printf("%s Image %u (%s)\n", p, count++,
425 fit_get_name(fit, noffset, NULL));
427 fit_image_print(fit, noffset, p);
431 /* Find configurations parent node offset */
432 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
433 if (confs_noffset < 0) {
434 debug("Can't get configurations parent node '%s' (%s)\n",
435 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
439 /* get default configuration unit name from default property */
440 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
442 printf("%s Default Configuration: '%s'\n", p, uname);
444 /* Process its subnodes, print out configurations details */
445 for (ndepth = 0, count = 0,
446 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
447 (noffset >= 0) && (ndepth > 0);
448 noffset = fdt_next_node(fit, noffset, &ndepth)) {
451 * Direct child node of the configurations parent node,
452 * i.e. configuration node.
454 printf("%s Configuration %u (%s)\n", p, count++,
455 fit_get_name(fit, noffset, NULL));
457 fit_conf_print(fit, noffset, p);
463 * fit_image_print - prints out the FIT component image details
464 * @fit: pointer to the FIT format image header
465 * @image_noffset: offset of the component image node
466 * @p: pointer to prefix string
468 * fit_image_print() lists all mandatory properties for the processed component
469 * image. If present, hash nodes are printed out as well. Load
470 * address for images of type firmware is also printed out. Since the load
471 * address is not mandatory for firmware images, it will be output as
472 * "unavailable" when not present.
475 * no returned results
477 void fit_image_print(const void *fit, int image_noffset, const char *p)
480 uint8_t type, arch, os, comp;
488 if (!CONFIG_IS_ENABLED(FIT_PRINT))
491 /* Mandatory properties */
492 ret = fit_get_desc(fit, image_noffset, &desc);
493 printf("%s Description: ", p);
495 printf("unavailable\n");
497 printf("%s\n", desc);
499 if (IMAGE_ENABLE_TIMESTAMP) {
502 ret = fit_get_timestamp(fit, 0, ×tamp);
503 printf("%s Created: ", p);
505 printf("unavailable\n");
507 genimg_print_time(timestamp);
510 fit_image_get_type(fit, image_noffset, &type);
511 printf("%s Type: %s\n", p, genimg_get_type_name(type));
513 fit_image_get_comp(fit, image_noffset, &comp);
514 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
516 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
518 if (!tools_build()) {
519 printf("%s Data Start: ", p);
521 printf("unavailable\n");
523 void *vdata = (void *)data;
525 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
529 printf("%s Data Size: ", p);
531 printf("unavailable\n");
533 genimg_print_size(size);
535 /* Remaining, type dependent properties */
536 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
537 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
538 (type == IH_TYPE_FLATDT)) {
539 fit_image_get_arch(fit, image_noffset, &arch);
540 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
543 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
544 (type == IH_TYPE_FIRMWARE)) {
545 fit_image_get_os(fit, image_noffset, &os);
546 printf("%s OS: %s\n", p, genimg_get_os_name(os));
549 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
550 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
551 (type == IH_TYPE_FPGA)) {
552 ret = fit_image_get_load(fit, image_noffset, &load);
553 printf("%s Load Address: ", p);
555 printf("unavailable\n");
557 printf("0x%08lx\n", load);
560 /* optional load address for FDT */
561 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
562 printf("%s Load Address: 0x%08lx\n", p, load);
564 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
565 (type == IH_TYPE_RAMDISK)) {
566 ret = fit_image_get_entry(fit, image_noffset, &entry);
567 printf("%s Entry Point: ", p);
569 printf("unavailable\n");
571 printf("0x%08lx\n", entry);
574 /* Process all hash subnodes of the component image node */
575 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
576 (noffset >= 0) && (ndepth > 0);
577 noffset = fdt_next_node(fit, noffset, &ndepth)) {
579 /* Direct child node of the component image node */
580 fit_image_print_verification_data(fit, noffset, p);
586 * fit_get_desc - get node description property
587 * @fit: pointer to the FIT format image header
588 * @noffset: node offset
589 * @desc: double pointer to the char, will hold pointer to the description
591 * fit_get_desc() reads description property from a given node, if
592 * description is found pointer to it is returned in third call argument.
598 int fit_get_desc(const void *fit, int noffset, char **desc)
602 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
604 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
612 * fit_get_timestamp - get node timestamp property
613 * @fit: pointer to the FIT format image header
614 * @noffset: node offset
615 * @timestamp: pointer to the time_t, will hold read timestamp
617 * fit_get_timestamp() reads timestamp property from given node, if timestamp
618 * is found and has a correct size its value is returned in third call
623 * -1, on property read failure
624 * -2, on wrong timestamp size
626 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
631 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
633 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
636 if (len != sizeof(uint32_t)) {
637 debug("FIT timestamp with incorrect size of (%u)\n", len);
641 *timestamp = uimage_to_cpu(*((uint32_t *)data));
646 * fit_image_get_node - get node offset for component image of a given unit name
647 * @fit: pointer to the FIT format image header
648 * @image_uname: component image node unit name
650 * fit_image_get_node() finds a component image (within the '/images'
651 * node) of a provided unit name. If image is found its node offset is
652 * returned to the caller.
655 * image node offset when found (>=0)
656 * negative number on failure (FDT_ERR_* code)
658 int fit_image_get_node(const void *fit, const char *image_uname)
660 int noffset, images_noffset;
662 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
663 if (images_noffset < 0) {
664 debug("Can't find images parent node '%s' (%s)\n",
665 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
666 return images_noffset;
669 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
671 debug("Can't get node offset for image unit name: '%s' (%s)\n",
672 image_uname, fdt_strerror(noffset));
679 * fit_image_get_os - get os id for a given component image node
680 * @fit: pointer to the FIT format image header
681 * @noffset: component image node offset
682 * @os: pointer to the uint8_t, will hold os numeric id
684 * fit_image_get_os() finds os property in a given component image node.
685 * If the property is found, its (string) value is translated to the numeric
686 * id which is returned to the caller.
692 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
697 /* Get OS name from property data */
698 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
700 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
705 /* Translate OS name to id */
706 *os = genimg_get_os_id(data);
711 * fit_image_get_arch - get arch id for a given component image node
712 * @fit: pointer to the FIT format image header
713 * @noffset: component image node offset
714 * @arch: pointer to the uint8_t, will hold arch numeric id
716 * fit_image_get_arch() finds arch property in a given component image node.
717 * If the property is found, its (string) value is translated to the numeric
718 * id which is returned to the caller.
724 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
729 /* Get architecture name from property data */
730 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
732 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
737 /* Translate architecture name to id */
738 *arch = genimg_get_arch_id(data);
743 * fit_image_get_type - get type id for a given component image node
744 * @fit: pointer to the FIT format image header
745 * @noffset: component image node offset
746 * @type: pointer to the uint8_t, will hold type numeric id
748 * fit_image_get_type() finds type property in a given component image node.
749 * If the property is found, its (string) value is translated to the numeric
750 * id which is returned to the caller.
756 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
761 /* Get image type name from property data */
762 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
764 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
769 /* Translate image type name to id */
770 *type = genimg_get_type_id(data);
775 * fit_image_get_comp - get comp id for a given component image node
776 * @fit: pointer to the FIT format image header
777 * @noffset: component image node offset
778 * @comp: pointer to the uint8_t, will hold comp numeric id
780 * fit_image_get_comp() finds comp property in a given component image node.
781 * If the property is found, its (string) value is translated to the numeric
782 * id which is returned to the caller.
788 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
793 /* Get compression name from property data */
794 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
796 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
801 /* Translate compression name to id */
802 *comp = genimg_get_comp_id(data);
806 static int fit_image_get_address(const void *fit, int noffset, char *name,
813 cell = fdt_getprop(fit, noffset, name, &len);
815 fit_get_debug(fit, noffset, name, len);
820 /* Use load64 to avoid compiling warning for 32-bit target */
822 load64 = (load64 << 32) | uimage_to_cpu(*cell);
826 if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) {
827 printf("Unsupported %s address size\n", name);
831 *load = (ulong)load64;
836 * fit_image_get_load() - get load addr property for given component image node
837 * @fit: pointer to the FIT format image header
838 * @noffset: component image node offset
839 * @load: pointer to the uint32_t, will hold load address
841 * fit_image_get_load() finds load address property in a given component
842 * image node. If the property is found, its value is returned to the caller.
848 int fit_image_get_load(const void *fit, int noffset, ulong *load)
850 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
854 * fit_image_get_entry() - get entry point address property
855 * @fit: pointer to the FIT format image header
856 * @noffset: component image node offset
857 * @entry: pointer to the uint32_t, will hold entry point address
859 * This gets the entry point address property for a given component image
862 * fit_image_get_entry() finds entry point address property in a given
863 * component image node. If the property is found, its value is returned
870 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
872 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
876 * fit_image_get_data - get data property and its size for a given component image node
877 * @fit: pointer to the FIT format image header
878 * @noffset: component image node offset
879 * @data: double pointer to void, will hold data property's data address
880 * @size: pointer to size_t, will hold data property's data size
882 * fit_image_get_data() finds data property in a given component image node.
883 * If the property is found its data start address and size are returned to
890 int fit_image_get_data(const void *fit, int noffset,
891 const void **data, size_t *size)
895 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
897 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
907 * Get 'data-offset' property from a given image node.
909 * @fit: pointer to the FIT image header
910 * @noffset: component image node offset
911 * @data_offset: holds the data-offset property
915 * -ENOENT if the property could not be found
917 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
921 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
925 *data_offset = fdt32_to_cpu(*val);
931 * Get 'data-position' property from a given image node.
933 * @fit: pointer to the FIT image header
934 * @noffset: component image node offset
935 * @data_position: holds the data-position property
939 * -ENOENT if the property could not be found
941 int fit_image_get_data_position(const void *fit, int noffset,
946 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
950 *data_position = fdt32_to_cpu(*val);
956 * Get 'data-size' property from a given image node.
958 * @fit: pointer to the FIT image header
959 * @noffset: component image node offset
960 * @data_size: holds the data-size property
964 * -ENOENT if the property could not be found
966 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
970 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
974 *data_size = fdt32_to_cpu(*val);
980 * Get 'data-size-unciphered' property from a given image node.
982 * @fit: pointer to the FIT image header
983 * @noffset: component image node offset
984 * @data_size: holds the data-size property
988 * -ENOENT if the property could not be found
990 int fit_image_get_data_size_unciphered(const void *fit, int noffset,
995 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
999 *data_size = (size_t)fdt32_to_cpu(*val);
1005 * fit_image_get_data_and_size - get data and its size including
1006 * both embedded and external data
1007 * @fit: pointer to the FIT format image header
1008 * @noffset: component image node offset
1009 * @data: double pointer to void, will hold data property's data address
1010 * @size: pointer to size_t, will hold data property's data size
1012 * fit_image_get_data_and_size() finds data and its size including
1013 * both embedded and external data. If the property is found
1014 * its data start address and size are returned to the caller.
1018 * otherwise, on failure
1020 int fit_image_get_data_and_size(const void *fit, int noffset,
1021 const void **data, size_t *size)
1023 bool external_data = false;
1028 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1029 external_data = true;
1030 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1031 external_data = true;
1033 * For FIT with external data, figure out where
1034 * the external images start. This is the base
1035 * for the data-offset properties in each image.
1037 offset += ((fdt_totalsize(fit) + 3) & ~3);
1040 if (external_data) {
1041 debug("External Data\n");
1042 ret = fit_image_get_data_size(fit, noffset, &len);
1044 *data = fit + offset;
1048 ret = fit_image_get_data(fit, noffset, data, size);
1055 * fit_image_hash_get_algo - get hash algorithm name
1056 * @fit: pointer to the FIT format image header
1057 * @noffset: hash node offset
1058 * @algo: double pointer to char, will hold pointer to the algorithm name
1060 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1061 * If the property is found its data start address is returned to the caller.
1067 int fit_image_hash_get_algo(const void *fit, int noffset, const char **algo)
1071 *algo = (const char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1072 if (*algo == NULL) {
1073 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1081 * fit_image_hash_get_value - get hash value and length
1082 * @fit: pointer to the FIT format image header
1083 * @noffset: hash node offset
1084 * @value: double pointer to uint8_t, will hold address of a hash value data
1085 * @value_len: pointer to an int, will hold hash data length
1087 * fit_image_hash_get_value() finds hash value property in a given hash node.
1088 * If the property is found its data start address and size are returned to
1095 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1100 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1101 if (*value == NULL) {
1102 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1112 * fit_image_hash_get_ignore - get hash ignore flag
1113 * @fit: pointer to the FIT format image header
1114 * @noffset: hash node offset
1115 * @ignore: pointer to an int, will hold hash ignore flag
1117 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1118 * If the property is found and non-zero, the hash algorithm is not verified by
1119 * u-boot automatically.
1122 * 0, on ignore not found
1123 * value, on ignore found
1125 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1130 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1131 if (value == NULL || len != sizeof(int))
1140 * fit_image_cipher_get_algo - get cipher algorithm name
1141 * @fit: pointer to the FIT format image header
1142 * @noffset: cipher node offset
1143 * @algo: double pointer to char, will hold pointer to the algorithm name
1145 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1146 * cipher node. If the property is found its data start address is returned
1153 int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1157 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1159 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1166 ulong fit_get_end(const void *fit)
1168 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1172 * fit_set_timestamp - set node timestamp property
1173 * @fit: pointer to the FIT format image header
1174 * @noffset: node offset
1175 * @timestamp: timestamp value to be set
1177 * fit_set_timestamp() attempts to set timestamp property in the requested
1178 * node and returns operation status to the caller.
1182 * -ENOSPC if no space in device tree, -1 for other error
1184 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1189 t = cpu_to_uimage(timestamp);
1190 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1193 debug("Can't set '%s' property for '%s' node (%s)\n",
1194 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1196 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1203 * calculate_hash - calculate and return hash for provided input data
1204 * @data: pointer to the input data
1205 * @data_len: data length
1206 * @name: requested hash algorithm name
1207 * @value: pointer to the char, will hold hash value data (caller must
1208 * allocate enough free space)
1209 * value_len: length of the calculated hash
1211 * calculate_hash() computes input data hash according to the requested
1213 * Resulting hash value is placed in caller provided 'value' buffer, length
1214 * of the calculated hash is returned via value_len pointer argument.
1218 * -1, when algo is unsupported
1220 int calculate_hash(const void *data, int data_len, const char *name,
1221 uint8_t *value, int *value_len)
1223 #if !defined(USE_HOSTCC) && defined(CONFIG_DM_HASH)
1225 enum HASH_ALGO hash_algo;
1226 struct udevice *dev;
1228 rc = uclass_get_device(UCLASS_HASH, 0, &dev);
1230 debug("failed to get hash device, rc=%d\n", rc);
1234 hash_algo = hash_algo_lookup_by_name(name);
1235 if (hash_algo == HASH_ALGO_INVALID) {
1236 debug("Unsupported hash algorithm\n");
1240 rc = hash_digest_wd(dev, hash_algo, data, data_len, value, CHUNKSZ);
1242 debug("failed to get hash value, rc=%d\n", rc);
1246 *value_len = hash_algo_digest_size(hash_algo);
1248 struct hash_algo *algo;
1251 ret = hash_lookup_algo(name, &algo);
1253 debug("Unsupported hash alogrithm\n");
1257 algo->hash_func_ws(data, data_len, value, algo->chunk_size);
1258 *value_len = algo->digest_size;
1264 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1265 size_t size, char **err_msgp)
1267 DEFINE_ALIGN_BUFFER(uint8_t, value, FIT_MAX_HASH_LEN,
1277 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1278 *err_msgp = "Can't get hash algo property";
1283 if (!tools_build()) {
1284 fit_image_hash_get_ignore(fit, noffset, &ignore);
1286 printf("-skipped ");
1291 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1293 *err_msgp = "Can't get hash value property";
1297 if (calculate_hash(data, size, algo, value, &value_len)) {
1298 *err_msgp = "Unsupported hash algorithm";
1302 if (value_len != fit_value_len) {
1303 *err_msgp = "Bad hash value len";
1305 } else if (memcmp(value, fit_value, value_len) != 0) {
1306 *err_msgp = "Bad hash value";
1313 int fit_image_verify_with_data(const void *fit, int image_noffset,
1314 const void *key_blob, const void *data,
1322 /* Verify all required signatures */
1323 if (FIT_IMAGE_ENABLE_VERIFY &&
1324 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1325 key_blob, &verify_all)) {
1326 err_msg = "Unable to verify required signature";
1330 /* Process all hash subnodes of the component image node */
1331 fdt_for_each_subnode(noffset, fit, image_noffset) {
1332 const char *name = fit_get_name(fit, noffset, NULL);
1335 * Check subnode name, must be equal to "hash".
1336 * Multiple hash nodes require unique unit node
1337 * names, e.g. hash-1, hash-2, etc.
1339 if (!strncmp(name, FIT_HASH_NODENAME,
1340 strlen(FIT_HASH_NODENAME))) {
1341 if (fit_image_check_hash(fit, noffset, data, size,
1345 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
1346 !strncmp(name, FIT_SIG_NODENAME,
1347 strlen(FIT_SIG_NODENAME))) {
1348 ret = fit_image_check_sig(fit, noffset, data, size,
1349 gd_fdt_blob(), -1, &err_msg);
1352 * Show an indication on failure, but do not return
1353 * an error. Only keys marked 'required' can cause
1354 * an image validation failure. See the call to
1355 * fit_image_verify_required_sigs() above.
1364 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1365 err_msg = "Corrupted or truncated tree";
1372 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1373 err_msg, fit_get_name(fit, noffset, NULL),
1374 fit_get_name(fit, image_noffset, NULL));
1379 * fit_image_verify - verify data integrity
1380 * @fit: pointer to the FIT format image header
1381 * @image_noffset: component image node offset
1383 * fit_image_verify() goes over component image hash nodes,
1384 * re-calculates each data hash and compares with the value stored in hash
1388 * 1, if all hashes are valid
1389 * 0, otherwise (or on error)
1391 int fit_image_verify(const void *fit, int image_noffset)
1393 const char *name = fit_get_name(fit, image_noffset, NULL);
1398 if (IS_ENABLED(CONFIG_FIT_SIGNATURE) && strchr(name, '@')) {
1400 * We don't support this since libfdt considers names with the
1401 * name root but different @ suffix to be equal
1403 err_msg = "Node name contains @";
1406 /* Get image data and data length */
1407 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1408 err_msg = "Can't get image data/size";
1412 return fit_image_verify_with_data(fit, image_noffset, gd_fdt_blob(),
1416 printf("error!\n%s in '%s' image node\n", err_msg,
1417 fit_get_name(fit, image_noffset, NULL));
1422 * fit_all_image_verify - verify data integrity for all images
1423 * @fit: pointer to the FIT format image header
1425 * fit_all_image_verify() goes over all images in the FIT and
1426 * for every images checks if all it's hashes are valid.
1429 * 1, if all hashes of all images are valid
1430 * 0, otherwise (or on error)
1432 int fit_all_image_verify(const void *fit)
1439 /* Find images parent node offset */
1440 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1441 if (images_noffset < 0) {
1442 printf("Can't find images parent node '%s' (%s)\n",
1443 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1447 /* Process all image subnodes, check hashes for each */
1448 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1450 for (ndepth = 0, count = 0,
1451 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1452 (noffset >= 0) && (ndepth > 0);
1453 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1456 * Direct child node of the images parent node,
1457 * i.e. component image node.
1459 printf(" Hash(es) for Image %u (%s): ", count,
1460 fit_get_name(fit, noffset, NULL));
1463 if (!fit_image_verify(fit, noffset))
1471 static int fit_image_uncipher(const void *fit, int image_noffset,
1472 void **data, size_t *size)
1474 int cipher_noffset, ret;
1478 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1479 FIT_CIPHER_NODENAME);
1480 if (cipher_noffset < 0)
1483 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1484 *data, *size, &dst, &size_dst);
1496 * fit_image_check_os - check whether image node is of a given os type
1497 * @fit: pointer to the FIT format image header
1498 * @noffset: component image node offset
1499 * @os: requested image os
1501 * fit_image_check_os() reads image os property and compares its numeric
1502 * id with the requested os. Comparison result is returned to the caller.
1505 * 1 if image is of given os type
1506 * 0 otherwise (or on error)
1508 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1512 if (fit_image_get_os(fit, noffset, &image_os))
1514 return (os == image_os);
1518 * fit_image_check_arch - check whether image node is of a given arch
1519 * @fit: pointer to the FIT format image header
1520 * @noffset: component image node offset
1521 * @arch: requested imagearch
1523 * fit_image_check_arch() reads image arch property and compares its numeric
1524 * id with the requested arch. Comparison result is returned to the caller.
1527 * 1 if image is of given arch
1528 * 0 otherwise (or on error)
1530 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1533 int aarch32_support = 0;
1535 /* Let's assume that sandbox can load any architecture */
1536 if (IS_ENABLED(CONFIG_SANDBOX))
1539 if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32))
1540 aarch32_support = 1;
1542 if (fit_image_get_arch(fit, noffset, &image_arch))
1544 return (arch == image_arch) ||
1545 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1546 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1551 * fit_image_check_type - check whether image node is of a given type
1552 * @fit: pointer to the FIT format image header
1553 * @noffset: component image node offset
1554 * @type: requested image type
1556 * fit_image_check_type() reads image type property and compares its numeric
1557 * id with the requested type. Comparison result is returned to the caller.
1560 * 1 if image is of given type
1561 * 0 otherwise (or on error)
1563 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1567 if (fit_image_get_type(fit, noffset, &image_type))
1569 return (type == image_type);
1573 * fit_image_check_comp - check whether image node uses given compression
1574 * @fit: pointer to the FIT format image header
1575 * @noffset: component image node offset
1576 * @comp: requested image compression type
1578 * fit_image_check_comp() reads image compression property and compares its
1579 * numeric id with the requested compression type. Comparison result is
1580 * returned to the caller.
1583 * 1 if image uses requested compression
1584 * 0 otherwise (or on error)
1586 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1590 if (fit_image_get_comp(fit, noffset, &image_comp))
1592 return (comp == image_comp);
1596 * fdt_check_no_at() - Check for nodes whose names contain '@'
1598 * This checks the parent node and all subnodes recursively
1600 * @fit: FIT to check
1601 * @parent: Parent node to check
1602 * Return: 0 if OK, -EADDRNOTAVAIL is a node has a name containing '@'
1604 static int fdt_check_no_at(const void *fit, int parent)
1610 name = fdt_get_name(fit, parent, NULL);
1611 if (!name || strchr(name, '@'))
1612 return -EADDRNOTAVAIL;
1614 fdt_for_each_subnode(node, fit, parent) {
1615 ret = fdt_check_no_at(fit, node);
1623 int fit_check_format(const void *fit, ulong size)
1627 /* A FIT image must be a valid FDT */
1628 ret = fdt_check_header(fit);
1630 log_debug("Wrong FIT format: not a flattened device tree (err=%d)\n",
1635 if (CONFIG_IS_ENABLED(FIT_FULL_CHECK)) {
1637 * If we are not given the size, make do wtih calculating it.
1638 * This is not as secure, so we should consider a flag to
1641 if (size == IMAGE_SIZE_INVAL)
1642 size = fdt_totalsize(fit);
1643 ret = fdt_check_full(fit, size);
1648 * U-Boot stopped using unit addressed in 2017. Since libfdt
1649 * can match nodes ignoring any unit address, signature
1650 * verification can see the wrong node if one is inserted with
1651 * the same name as a valid node but with a unit address
1652 * attached. Protect against this by disallowing unit addresses.
1654 if (!ret && CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
1655 ret = fdt_check_no_at(fit, 0);
1658 log_debug("FIT check error %d\n", ret);
1663 log_debug("FIT check error %d\n", ret);
1668 /* mandatory / node 'description' property */
1669 if (!fdt_getprop(fit, 0, FIT_DESC_PROP, NULL)) {
1670 log_debug("Wrong FIT format: no description\n");
1674 if (IMAGE_ENABLE_TIMESTAMP) {
1675 /* mandatory / node 'timestamp' property */
1676 if (!fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL)) {
1677 log_debug("Wrong FIT format: no timestamp\n");
1682 /* mandatory subimages parent '/images' node */
1683 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1684 log_debug("Wrong FIT format: no images parent node\n");
1692 * fit_conf_find_compat
1693 * @fit: pointer to the FIT format image header
1694 * @fdt: pointer to the device tree to compare against
1696 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1697 * most compatible with the passed in device tree.
1706 * |-o configurations
1714 * |-compatible = "foo,bar", "bim,bam"
1717 * |-compatible = "foo,bar",
1720 * |-compatible = "bim,bam", "baz,biz"
1722 * Configuration 1 would be picked because the first string in U-Boot's
1723 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1724 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1726 * As an optimization, the compatible property from the FDT's root node can be
1727 * copied into the configuration node in the FIT image. This is required to
1728 * match configurations with compressed FDTs.
1731 * offset to the configuration to use if one was found
1734 int fit_conf_find_compat(const void *fit, const void *fdt)
1737 int noffset, confs_noffset, images_noffset;
1738 const void *fdt_compat;
1740 int best_match_offset = 0;
1741 int best_match_pos = 0;
1743 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1744 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1745 if (confs_noffset < 0 || images_noffset < 0) {
1746 debug("Can't find configurations or images nodes.\n");
1750 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1752 debug("Fdt for comparison has no \"compatible\" property.\n");
1757 * Loop over the configurations in the FIT image.
1759 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1760 (noffset >= 0) && (ndepth > 0);
1761 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1763 const char *kfdt_name;
1764 int kfdt_noffset, compat_noffset;
1765 const char *cur_fdt_compat;
1773 /* If there's a compat property in the config node, use that. */
1774 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1775 fdt = fit; /* search in FIT image */
1776 compat_noffset = noffset; /* search under config node */
1777 } else { /* Otherwise extract it from the kernel FDT. */
1778 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1780 debug("No fdt property found.\n");
1783 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1785 if (kfdt_noffset < 0) {
1786 debug("No image node named \"%s\" found.\n",
1791 if (!fit_image_check_comp(fit, kfdt_noffset,
1793 debug("Can't extract compat from \"%s\" "
1794 "(compressed)\n", kfdt_name);
1798 /* search in this config's kernel FDT */
1799 if (fit_image_get_data_and_size(fit, kfdt_noffset,
1801 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1805 compat_noffset = 0; /* search kFDT under root node */
1808 len = fdt_compat_len;
1809 cur_fdt_compat = fdt_compat;
1811 * Look for a match for each U-Boot compatibility string in
1812 * turn in the compat string property.
1814 for (i = 0; len > 0 &&
1815 (!best_match_offset || best_match_pos > i); i++) {
1816 int cur_len = strlen(cur_fdt_compat) + 1;
1818 if (!fdt_node_check_compatible(fdt, compat_noffset,
1820 best_match_offset = noffset;
1825 cur_fdt_compat += cur_len;
1828 if (!best_match_offset) {
1829 debug("No match found.\n");
1833 return best_match_offset;
1836 int fit_conf_get_node(const void *fit, const char *conf_uname)
1838 int noffset, confs_noffset;
1841 char *conf_uname_copy = NULL;
1843 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1844 if (confs_noffset < 0) {
1845 debug("Can't find configurations parent node '%s' (%s)\n",
1846 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1847 return confs_noffset;
1850 if (conf_uname == NULL) {
1851 /* get configuration unit name from the default property */
1852 debug("No configuration specified, trying default...\n");
1853 if (!tools_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) {
1854 noffset = fit_find_config_node(fit);
1857 conf_uname = fdt_get_name(fit, noffset, NULL);
1859 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1860 FIT_DEFAULT_PROP, &len);
1861 if (conf_uname == NULL) {
1862 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1867 debug("Found default configuration: '%s'\n", conf_uname);
1870 s = strchr(conf_uname, '#');
1872 len = s - conf_uname;
1873 conf_uname_copy = malloc(len + 1);
1874 if (!conf_uname_copy) {
1875 debug("Can't allocate uname copy: '%s'\n",
1879 memcpy(conf_uname_copy, conf_uname, len);
1880 conf_uname_copy[len] = '\0';
1881 conf_uname = conf_uname_copy;
1884 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1886 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1887 conf_uname, fdt_strerror(noffset));
1890 if (conf_uname_copy)
1891 free(conf_uname_copy);
1896 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1897 const char *prop_name)
1899 return fdt_stringlist_count(fit, noffset, prop_name);
1902 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1903 const char *prop_name, int index)
1908 /* get kernel image unit name from configuration kernel property */
1909 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1913 return fit_image_get_node(fit, uname);
1916 int fit_conf_get_prop_node(const void *fit, int noffset,
1917 const char *prop_name)
1919 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1922 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1924 fit_image_print(fit, rd_noffset, " ");
1927 puts(" Verifying Hash Integrity ... ");
1928 if (!fit_image_verify(fit, rd_noffset)) {
1929 puts("Bad Data Hash\n");
1938 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1945 debug("* %s: using config '%s' from image at 0x%08lx\n",
1946 prop_name, images->fit_uname_cfg, addr);
1948 /* Check whether configuration has this property defined */
1949 fit_hdr = map_sysmem(addr, 0);
1950 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1951 if (cfg_noffset < 0) {
1952 debug("* %s: no such config\n", prop_name);
1956 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1958 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1966 * fit_get_image_type_property() - get property name for IH_TYPE_...
1968 * Return: the properly name where we expect to find the image in the
1971 static const char *fit_get_image_type_property(int type)
1974 * This is sort-of available in the uimage_type[] table in image.c
1975 * but we don't have access to the short name, and "fdt" is different
1976 * anyway. So let's just keep it here.
1979 case IH_TYPE_FLATDT:
1980 return FIT_FDT_PROP;
1981 case IH_TYPE_KERNEL:
1982 return FIT_KERNEL_PROP;
1983 case IH_TYPE_FIRMWARE:
1984 return FIT_FIRMWARE_PROP;
1985 case IH_TYPE_RAMDISK:
1986 return FIT_RAMDISK_PROP;
1987 case IH_TYPE_X86_SETUP:
1988 return FIT_SETUP_PROP;
1989 case IH_TYPE_LOADABLE:
1990 return FIT_LOADABLE_PROP;
1992 return FIT_FPGA_PROP;
1993 case IH_TYPE_STANDALONE:
1994 return FIT_STANDALONE_PROP;
2000 int fit_image_load(bootm_headers_t *images, ulong addr,
2001 const char **fit_unamep, const char **fit_uname_configp,
2002 int arch, int image_type, int bootstage_id,
2003 enum fit_load_op load_op, ulong *datap, ulong *lenp)
2005 int cfg_noffset, noffset;
2006 const char *fit_uname;
2007 const char *fit_uname_config;
2008 const char *fit_base_uname_config;
2014 ulong load, load_end, data, len;
2016 const char *prop_name;
2019 fit = map_sysmem(addr, 0);
2020 fit_uname = fit_unamep ? *fit_unamep : NULL;
2021 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
2022 fit_base_uname_config = NULL;
2023 prop_name = fit_get_image_type_property(image_type);
2024 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
2026 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
2027 ret = fit_check_format(fit, IMAGE_SIZE_INVAL);
2029 printf("Bad FIT %s image format! (err=%d)\n", prop_name, ret);
2030 if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && ret == -EADDRNOTAVAIL)
2031 printf("Signature checking prevents use of unit addresses (@) in nodes\n");
2032 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
2035 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
2037 /* get FIT component image node offset */
2038 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
2039 noffset = fit_image_get_node(fit, fit_uname);
2042 * no image node unit name, try to get config
2043 * node first. If config unit node name is NULL
2044 * fit_conf_get_node() will try to find default config node
2046 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
2047 if (IS_ENABLED(CONFIG_FIT_BEST_MATCH) && !fit_uname_config) {
2048 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
2050 cfg_noffset = fit_conf_get_node(fit,
2053 if (cfg_noffset < 0) {
2054 puts("Could not find configuration node\n");
2055 bootstage_error(bootstage_id +
2056 BOOTSTAGE_SUB_NO_UNIT_NAME);
2060 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
2061 printf(" Using '%s' configuration\n", fit_base_uname_config);
2062 /* Remember this config */
2063 if (image_type == IH_TYPE_KERNEL)
2064 images->fit_uname_cfg = fit_base_uname_config;
2066 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
2067 puts(" Verifying Hash Integrity ... ");
2068 if (fit_config_verify(fit, cfg_noffset)) {
2069 puts("Bad Data Hash\n");
2070 bootstage_error(bootstage_id +
2071 BOOTSTAGE_SUB_HASH);
2077 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
2079 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
2081 fit_uname = fit_get_name(fit, noffset, NULL);
2084 printf("Could not find subimage node type '%s'\n", prop_name);
2085 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
2089 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
2091 ret = fit_image_select(fit, noffset, images->verify);
2093 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2097 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2098 if (!tools_build() && IS_ENABLED(CONFIG_SANDBOX)) {
2099 if (!fit_image_check_target_arch(fit, noffset)) {
2100 puts("Unsupported Architecture\n");
2101 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2110 fit_image_get_arch(fit, noffset, &os_arch);
2111 images->os.arch = os_arch;
2115 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2116 type_ok = fit_image_check_type(fit, noffset, image_type) ||
2117 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
2118 fit_image_check_type(fit, noffset, IH_TYPE_TEE) ||
2119 (image_type == IH_TYPE_KERNEL &&
2120 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
2122 os_ok = image_type == IH_TYPE_FLATDT ||
2123 image_type == IH_TYPE_FPGA ||
2124 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
2125 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
2126 fit_image_check_os(fit, noffset, IH_OS_TEE) ||
2127 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
2128 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
2129 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
2132 * If either of the checks fail, we should report an error, but
2133 * if the image type is coming from the "loadables" field, we
2134 * don't care what it is
2136 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
2137 fit_image_get_os(fit, noffset, &os);
2138 printf("No %s %s %s Image\n",
2139 genimg_get_os_name(os),
2140 genimg_get_arch_name(arch),
2141 genimg_get_type_name(image_type));
2142 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2146 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2148 /* get image data address and length */
2149 if (fit_image_get_data_and_size(fit, noffset,
2150 (const void **)&buf, &size)) {
2151 printf("Could not find %s subimage data!\n", prop_name);
2152 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2156 /* Decrypt data before uncompress/move */
2157 if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
2158 puts(" Decrypting Data ... ");
2159 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2166 /* perform any post-processing on the image data */
2167 if (!tools_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
2168 board_fit_image_post_process(fit, noffset, &buf, &size);
2172 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2174 data = map_to_sysmem(buf);
2176 if (load_op == FIT_LOAD_IGNORED) {
2178 } else if (fit_image_get_load(fit, noffset, &load)) {
2179 if (load_op == FIT_LOAD_REQUIRED) {
2180 printf("Can't get %s subimage load address!\n",
2182 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2185 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
2186 ulong image_start, image_end;
2189 * move image data to the load address,
2190 * make sure we don't overwrite initial image
2193 image_end = addr + fit_get_size(fit);
2195 load_end = load + len;
2196 if (image_type != IH_TYPE_KERNEL &&
2197 load < image_end && load_end > image_start) {
2198 printf("Error: %s overwritten\n", prop_name);
2202 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2203 prop_name, data, load);
2205 load = data; /* No load address specified */
2208 comp = IH_COMP_NONE;
2210 /* Kernel images get decompressed later in bootm_load_os(). */
2211 if (!fit_image_get_comp(fit, noffset, &comp) &&
2212 comp != IH_COMP_NONE &&
2213 !(image_type == IH_TYPE_KERNEL ||
2214 image_type == IH_TYPE_KERNEL_NOLOAD ||
2215 image_type == IH_TYPE_RAMDISK)) {
2216 ulong max_decomp_len = len * 20;
2218 loadbuf = malloc(max_decomp_len);
2219 load = map_to_sysmem(loadbuf);
2221 loadbuf = map_sysmem(load, max_decomp_len);
2223 if (image_decomp(comp, load, data, image_type,
2224 loadbuf, buf, len, max_decomp_len, &load_end)) {
2225 printf("Error decompressing %s\n", prop_name);
2229 len = load_end - load;
2230 } else if (load != data) {
2231 loadbuf = map_sysmem(load, len);
2232 memcpy(loadbuf, buf, len);
2235 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2236 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2237 " please fix your .its file!\n");
2239 /* verify that image data is a proper FDT blob */
2240 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2241 puts("Subimage data is not a FDT");
2245 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2250 *fit_unamep = (char *)fit_uname;
2251 if (fit_uname_configp)
2252 *fit_uname_configp = (char *)(fit_uname_config ? :
2253 fit_base_uname_config);
2258 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2259 ulong *setup_start, ulong *setup_len)
2266 addr = map_to_sysmem(images->fit_hdr_os);
2267 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2271 ret = fit_image_load(images, addr, NULL, NULL, arch,
2272 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2273 FIT_LOAD_REQUIRED, setup_start, &len);
2279 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2280 const char **fit_unamep, const char **fit_uname_configp,
2281 int arch, ulong *datap, ulong *lenp)
2283 int fdt_noffset, cfg_noffset, count;
2285 const char *fit_uname = NULL;
2286 const char *fit_uname_config = NULL;
2287 char *fit_uname_config_copy = NULL;
2288 char *next_config = NULL;
2290 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2291 ulong image_start, image_end;
2292 ulong ovload, ovlen, ovcopylen;
2293 const char *uconfig;
2295 void *base, *ov, *ovcopy = NULL;
2296 int i, err, noffset, ov_noffset;
2299 fit_uname = fit_unamep ? *fit_unamep : NULL;
2301 if (fit_uname_configp && *fit_uname_configp) {
2302 fit_uname_config_copy = strdup(*fit_uname_configp);
2303 if (!fit_uname_config_copy)
2306 next_config = strchr(fit_uname_config_copy, '#');
2308 *next_config++ = '\0';
2309 if (next_config - 1 > fit_uname_config_copy)
2310 fit_uname_config = fit_uname_config_copy;
2313 fdt_noffset = fit_image_load(images,
2314 addr, &fit_uname, &fit_uname_config,
2315 arch, IH_TYPE_FLATDT,
2316 BOOTSTAGE_ID_FIT_FDT_START,
2317 FIT_LOAD_OPTIONAL, &load, &len);
2319 if (fdt_noffset < 0)
2322 debug("fit_uname=%s, fit_uname_config=%s\n",
2323 fit_uname ? fit_uname : "<NULL>",
2324 fit_uname_config ? fit_uname_config : "<NULL>");
2326 fit = map_sysmem(addr, 0);
2328 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2330 /* single blob, or error just return as well */
2331 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2332 if (count <= 1 && !next_config)
2335 /* we need to apply overlays */
2337 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2339 image_end = addr + fit_get_size(fit);
2340 /* verify that relocation took place by load address not being in fit */
2341 if (load >= image_start && load < image_end) {
2342 /* check is simplified; fit load checks for overlaps */
2343 printf("Overlayed FDT requires relocation\n");
2344 fdt_noffset = -EBADF;
2348 base = map_sysmem(load, len);
2350 /* apply extra configs in FIT first, followed by args */
2351 for (i = 1; ; i++) {
2353 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2355 uname = fit_get_name(fit, noffset, NULL);
2360 uconfig = next_config;
2361 next_config = strchr(next_config, '#');
2363 *next_config++ = '\0';
2367 * fit_image_load() would load the first FDT from the
2368 * extra config only when uconfig is specified.
2369 * Check if the extra config contains multiple FDTs and
2372 cfg_noffset = fit_conf_get_node(fit, uconfig);
2375 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2379 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2381 ov_noffset = fit_image_load(images,
2382 addr, &uname, &uconfig,
2383 arch, IH_TYPE_FLATDT,
2384 BOOTSTAGE_ID_FIT_FDT_START,
2385 FIT_LOAD_IGNORED, &ovload, &ovlen);
2386 if (ov_noffset < 0) {
2387 printf("load of %s failed\n", uname);
2390 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2391 uname, ovload, ovlen);
2392 ov = map_sysmem(ovload, ovlen);
2394 ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K);
2395 ovcopy = malloc(ovcopylen);
2397 printf("failed to duplicate DTO before application\n");
2398 fdt_noffset = -ENOMEM;
2402 err = fdt_open_into(ov, ovcopy, ovcopylen);
2404 printf("failed on fdt_open_into for DTO\n");
2409 base = map_sysmem(load, len + ovlen);
2410 err = fdt_open_into(base, base, len + ovlen);
2412 printf("failed on fdt_open_into\n");
2417 /* the verbose method prints out messages on error */
2418 err = fdt_overlay_apply_verbose(base, ovcopy);
2424 len = fdt_totalsize(base);
2430 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2431 fdt_noffset = -EBADF;
2440 *fit_unamep = fit_uname;
2441 if (fit_uname_configp)
2442 *fit_uname_configp = fit_uname_config;
2444 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2448 if (fit_uname_config_copy)
2449 free(fit_uname_config_copy);