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>
26 #include <asm/global_data.h>
27 DECLARE_GLOBAL_DATA_PTR;
28 #endif /* !USE_HOSTCC*/
32 #include <bootstage.h>
33 #include <linux/kconfig.h>
34 #include <u-boot/crc.h>
35 #include <u-boot/md5.h>
36 #include <u-boot/sha1.h>
37 #include <u-boot/sha256.h>
38 #include <u-boot/sha512.h>
40 /*****************************************************************************/
41 /* New uImage format routines */
42 /*****************************************************************************/
44 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
45 ulong *addr, const char **name)
52 sep = strchr(spec, sepc);
55 *addr = simple_strtoul(spec, NULL, 16);
65 * fit_parse_conf - parse FIT configuration spec
66 * @spec: input string, containing configuration spec
67 * @add_curr: current image address (to be used as a possible default)
68 * @addr: pointer to a ulong variable, will hold FIT image address of a given
70 * @conf_name double pointer to a char, will hold pointer to a configuration
73 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
74 * where <addr> is a FIT image address that contains configuration
75 * with a <conf> unit name.
77 * Address part is optional, and if omitted default add_curr will
81 * 1 if spec is a valid configuration string,
82 * addr and conf_name are set accordingly
85 int fit_parse_conf(const char *spec, ulong addr_curr,
86 ulong *addr, const char **conf_name)
88 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
92 * fit_parse_subimage - parse FIT subimage spec
93 * @spec: input string, containing subimage spec
94 * @add_curr: current image address (to be used as a possible default)
95 * @addr: pointer to a ulong variable, will hold FIT image address of a given
97 * @image_name: double pointer to a char, will hold pointer to a subimage name
99 * fit_parse_subimage() expects subimage spec in the form of
100 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
101 * subimage with a <subimg> unit name.
103 * Address part is optional, and if omitted default add_curr will
107 * 1 if spec is a valid subimage string,
108 * addr and image_name are set accordingly
111 int fit_parse_subimage(const char *spec, ulong addr_curr,
112 ulong *addr, const char **image_name)
114 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
116 #endif /* !USE_HOSTCC */
119 /* Host tools use these implementations for Cipher and Signature support */
120 static void *host_blob;
122 void image_set_host_blob(void *blob)
127 void *image_get_host_blob(void)
131 #endif /* USE_HOSTCC */
133 static void fit_get_debug(const void *fit, int noffset,
134 char *prop_name, int err)
136 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
137 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
142 * fit_get_subimage_count - get component (sub-image) count
143 * @fit: pointer to the FIT format image header
144 * @images_noffset: offset of images node
147 * number of image components
149 int fit_get_subimage_count(const void *fit, int images_noffset)
155 /* Process its subnodes, print out component images details */
156 for (ndepth = 0, count = 0,
157 noffset = fdt_next_node(fit, images_noffset, &ndepth);
158 (noffset >= 0) && (ndepth > 0);
159 noffset = fdt_next_node(fit, noffset, &ndepth)) {
168 #if CONFIG_IS_ENABLED(FIT_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT)
170 * fit_image_print_data() - prints out the hash node details
171 * @fit: pointer to the FIT format image header
172 * @noffset: offset of the hash node
173 * @p: pointer to prefix string
174 * @type: Type of information to print ("hash" or "sign")
176 * fit_image_print_data() lists properties for the processed hash node
178 * This function avoid using puts() since it prints a newline on the host
179 * but does not in U-Boot.
182 * no returned results
184 static void fit_image_print_data(const void *fit, int noffset, const char *p,
195 debug("%s %s node: '%s'\n", p, type,
196 fit_get_name(fit, noffset, NULL));
197 printf("%s %s algo: ", p, type);
198 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
199 printf("invalid/unsupported\n");
203 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
204 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
206 printf(":%s", keyname);
208 printf(" (required)");
211 padding = fdt_getprop(fit, noffset, "padding", NULL);
213 printf("%s %s padding: %s\n", p, type, padding);
215 ret = fit_image_hash_get_value(fit, noffset, &value,
217 printf("%s %s value: ", p, type);
219 printf("unavailable\n");
221 for (i = 0; i < value_len; i++)
222 printf("%02x", value[i]);
226 debug("%s %s len: %d\n", p, type, value_len);
228 /* Signatures have a time stamp */
229 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
232 printf("%s Timestamp: ", p);
233 if (fit_get_timestamp(fit, noffset, ×tamp))
234 printf("unavailable\n");
236 genimg_print_time(timestamp);
241 * fit_image_print_verification_data() - prints out the hash/signature details
242 * @fit: pointer to the FIT format image header
243 * @noffset: offset of the hash or signature node
244 * @p: pointer to prefix string
246 * This lists properties for the processed hash node
249 * no returned results
251 static void fit_image_print_verification_data(const void *fit, int noffset,
257 * Check subnode name, must be equal to "hash" or "signature".
258 * Multiple hash/signature nodes require unique unit node
259 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
261 name = fit_get_name(fit, noffset, NULL);
262 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
263 fit_image_print_data(fit, noffset, p, "Hash");
264 } else if (!strncmp(name, FIT_SIG_NODENAME,
265 strlen(FIT_SIG_NODENAME))) {
266 fit_image_print_data(fit, noffset, p, "Sign");
271 * fit_conf_print - prints out the FIT configuration details
272 * @fit: pointer to the FIT format image header
273 * @noffset: offset of the configuration node
274 * @p: pointer to prefix string
276 * fit_conf_print() lists all mandatory properties for the processed
277 * configuration node.
280 * no returned results
282 static void fit_conf_print(const void *fit, int noffset, const char *p)
287 int fdt_index, loadables_index;
290 /* Mandatory properties */
291 ret = fit_get_desc(fit, noffset, &desc);
292 printf("%s Description: ", p);
294 printf("unavailable\n");
296 printf("%s\n", desc);
298 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
299 printf("%s Kernel: ", p);
301 printf("unavailable\n");
303 printf("%s\n", uname);
305 /* Optional properties */
306 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
308 printf("%s Init Ramdisk: %s\n", p, uname);
310 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
312 printf("%s Firmware: %s\n", p, uname);
315 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
316 fdt_index, NULL), uname;
319 printf("%s FDT: ", p);
322 printf("%s\n", uname);
325 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
327 printf("%s FPGA: %s\n", p, uname);
329 /* Print out all of the specified loadables */
330 for (loadables_index = 0;
331 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
332 loadables_index, NULL), uname;
334 if (loadables_index == 0) {
335 printf("%s Loadables: ", p);
339 printf("%s\n", uname);
342 /* Process all hash subnodes of the component configuration node */
343 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
344 (noffset >= 0) && (ndepth > 0);
345 noffset = fdt_next_node(fit, noffset, &ndepth)) {
347 /* Direct child node of the component configuration node */
348 fit_image_print_verification_data(fit, noffset, p);
354 * fit_print_contents - prints out the contents of the FIT format image
355 * @fit: pointer to the FIT format image header
356 * @p: pointer to prefix string
358 * fit_print_contents() formats a multi line FIT image contents description.
359 * The routine prints out FIT image properties (root node level) followed by
360 * the details of each component image.
363 * no returned results
365 void fit_print_contents(const void *fit)
378 /* Indent string is defined in header image.h */
379 p = IMAGE_INDENT_STRING;
381 /* Root node properties */
382 ret = fit_get_desc(fit, 0, &desc);
383 printf("%sFIT description: ", p);
385 printf("unavailable\n");
387 printf("%s\n", desc);
389 if (IMAGE_ENABLE_TIMESTAMP) {
390 ret = fit_get_timestamp(fit, 0, ×tamp);
391 printf("%sCreated: ", p);
393 printf("unavailable\n");
395 genimg_print_time(timestamp);
398 /* Find images parent node offset */
399 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
400 if (images_noffset < 0) {
401 printf("Can't find images parent node '%s' (%s)\n",
402 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
406 /* Process its subnodes, print out component images details */
407 for (ndepth = 0, count = 0,
408 noffset = fdt_next_node(fit, images_noffset, &ndepth);
409 (noffset >= 0) && (ndepth > 0);
410 noffset = fdt_next_node(fit, noffset, &ndepth)) {
413 * Direct child node of the images parent node,
414 * i.e. component image node.
416 printf("%s Image %u (%s)\n", p, count++,
417 fit_get_name(fit, noffset, NULL));
419 fit_image_print(fit, noffset, p);
423 /* Find configurations parent node offset */
424 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
425 if (confs_noffset < 0) {
426 debug("Can't get configurations parent node '%s' (%s)\n",
427 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
431 /* get default configuration unit name from default property */
432 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
434 printf("%s Default Configuration: '%s'\n", p, uname);
436 /* Process its subnodes, print out configurations details */
437 for (ndepth = 0, count = 0,
438 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
439 (noffset >= 0) && (ndepth > 0);
440 noffset = fdt_next_node(fit, noffset, &ndepth)) {
443 * Direct child node of the configurations parent node,
444 * i.e. configuration node.
446 printf("%s Configuration %u (%s)\n", p, count++,
447 fit_get_name(fit, noffset, NULL));
449 fit_conf_print(fit, noffset, p);
455 * fit_image_print - prints out the FIT component image details
456 * @fit: pointer to the FIT format image header
457 * @image_noffset: offset of the component image node
458 * @p: pointer to prefix string
460 * fit_image_print() lists all mandatory properties for the processed component
461 * image. If present, hash nodes are printed out as well. Load
462 * address for images of type firmware is also printed out. Since the load
463 * address is not mandatory for firmware images, it will be output as
464 * "unavailable" when not present.
467 * no returned results
469 void fit_image_print(const void *fit, int image_noffset, const char *p)
472 uint8_t type, arch, os, comp;
480 /* Mandatory properties */
481 ret = fit_get_desc(fit, image_noffset, &desc);
482 printf("%s Description: ", p);
484 printf("unavailable\n");
486 printf("%s\n", desc);
488 if (IMAGE_ENABLE_TIMESTAMP) {
491 ret = fit_get_timestamp(fit, 0, ×tamp);
492 printf("%s Created: ", p);
494 printf("unavailable\n");
496 genimg_print_time(timestamp);
499 fit_image_get_type(fit, image_noffset, &type);
500 printf("%s Type: %s\n", p, genimg_get_type_name(type));
502 fit_image_get_comp(fit, image_noffset, &comp);
503 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
505 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
508 printf("%s Data Start: ", p);
510 printf("unavailable\n");
512 void *vdata = (void *)data;
514 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
518 printf("%s Data Size: ", p);
520 printf("unavailable\n");
522 genimg_print_size(size);
524 /* Remaining, type dependent properties */
525 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
526 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
527 (type == IH_TYPE_FLATDT)) {
528 fit_image_get_arch(fit, image_noffset, &arch);
529 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
532 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
533 (type == IH_TYPE_FIRMWARE)) {
534 fit_image_get_os(fit, image_noffset, &os);
535 printf("%s OS: %s\n", p, genimg_get_os_name(os));
538 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
539 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
540 (type == IH_TYPE_FPGA)) {
541 ret = fit_image_get_load(fit, image_noffset, &load);
542 printf("%s Load Address: ", p);
544 printf("unavailable\n");
546 printf("0x%08lx\n", load);
549 /* optional load address for FDT */
550 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
551 printf("%s Load Address: 0x%08lx\n", p, load);
553 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
554 (type == IH_TYPE_RAMDISK)) {
555 ret = fit_image_get_entry(fit, image_noffset, &entry);
556 printf("%s Entry Point: ", p);
558 printf("unavailable\n");
560 printf("0x%08lx\n", entry);
563 /* Process all hash subnodes of the component image node */
564 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
565 (noffset >= 0) && (ndepth > 0);
566 noffset = fdt_next_node(fit, noffset, &ndepth)) {
568 /* Direct child node of the component image node */
569 fit_image_print_verification_data(fit, noffset, p);
574 void fit_print_contents(const void *fit) { }
575 void fit_image_print(const void *fit, int image_noffset, const char *p) { }
576 #endif /* CONFIG_IS_ENABLED(FIR_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT) */
579 * fit_get_desc - get node description property
580 * @fit: pointer to the FIT format image header
581 * @noffset: node offset
582 * @desc: double pointer to the char, will hold pointer to the description
584 * fit_get_desc() reads description property from a given node, if
585 * description is found pointer to it is returned in third call argument.
591 int fit_get_desc(const void *fit, int noffset, char **desc)
595 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
597 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
605 * fit_get_timestamp - get node timestamp property
606 * @fit: pointer to the FIT format image header
607 * @noffset: node offset
608 * @timestamp: pointer to the time_t, will hold read timestamp
610 * fit_get_timestamp() reads timestamp property from given node, if timestamp
611 * is found and has a correct size its value is returned in third call
616 * -1, on property read failure
617 * -2, on wrong timestamp size
619 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
624 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
626 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
629 if (len != sizeof(uint32_t)) {
630 debug("FIT timestamp with incorrect size of (%u)\n", len);
634 *timestamp = uimage_to_cpu(*((uint32_t *)data));
639 * fit_image_get_node - get node offset for component image of a given unit name
640 * @fit: pointer to the FIT format image header
641 * @image_uname: component image node unit name
643 * fit_image_get_node() finds a component image (within the '/images'
644 * node) of a provided unit name. If image is found its node offset is
645 * returned to the caller.
648 * image node offset when found (>=0)
649 * negative number on failure (FDT_ERR_* code)
651 int fit_image_get_node(const void *fit, const char *image_uname)
653 int noffset, images_noffset;
655 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
656 if (images_noffset < 0) {
657 debug("Can't find images parent node '%s' (%s)\n",
658 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
659 return images_noffset;
662 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
664 debug("Can't get node offset for image unit name: '%s' (%s)\n",
665 image_uname, fdt_strerror(noffset));
672 * fit_image_get_os - get os id for a given component image node
673 * @fit: pointer to the FIT format image header
674 * @noffset: component image node offset
675 * @os: pointer to the uint8_t, will hold os numeric id
677 * fit_image_get_os() finds os property in a given component image node.
678 * If the property is found, its (string) value is translated to the numeric
679 * id which is returned to the caller.
685 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
690 /* Get OS name from property data */
691 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
693 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
698 /* Translate OS name to id */
699 *os = genimg_get_os_id(data);
704 * fit_image_get_arch - get arch id for a given component image node
705 * @fit: pointer to the FIT format image header
706 * @noffset: component image node offset
707 * @arch: pointer to the uint8_t, will hold arch numeric id
709 * fit_image_get_arch() finds arch property in a given component image node.
710 * If the property is found, its (string) value is translated to the numeric
711 * id which is returned to the caller.
717 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
722 /* Get architecture name from property data */
723 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
725 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
730 /* Translate architecture name to id */
731 *arch = genimg_get_arch_id(data);
736 * fit_image_get_type - get type id for a given component image node
737 * @fit: pointer to the FIT format image header
738 * @noffset: component image node offset
739 * @type: pointer to the uint8_t, will hold type numeric id
741 * fit_image_get_type() finds type property in a given component image node.
742 * If the property is found, its (string) value is translated to the numeric
743 * id which is returned to the caller.
749 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
754 /* Get image type name from property data */
755 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
757 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
762 /* Translate image type name to id */
763 *type = genimg_get_type_id(data);
768 * fit_image_get_comp - get comp id for a given component image node
769 * @fit: pointer to the FIT format image header
770 * @noffset: component image node offset
771 * @comp: pointer to the uint8_t, will hold comp numeric id
773 * fit_image_get_comp() finds comp property in a given component image node.
774 * If the property is found, its (string) value is translated to the numeric
775 * id which is returned to the caller.
781 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
786 /* Get compression name from property data */
787 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
789 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
794 /* Translate compression name to id */
795 *comp = genimg_get_comp_id(data);
799 static int fit_image_get_address(const void *fit, int noffset, char *name,
806 cell = fdt_getprop(fit, noffset, name, &len);
808 fit_get_debug(fit, noffset, name, len);
813 /* Use load64 to avoid compiling warning for 32-bit target */
815 load64 = (load64 << 32) | uimage_to_cpu(*cell);
819 if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) {
820 printf("Unsupported %s address size\n", name);
824 *load = (ulong)load64;
829 * fit_image_get_load() - get load addr property for given component image node
830 * @fit: pointer to the FIT format image header
831 * @noffset: component image node offset
832 * @load: pointer to the uint32_t, will hold load address
834 * fit_image_get_load() finds load address property in a given component
835 * image node. If the property is found, its value is returned to the caller.
841 int fit_image_get_load(const void *fit, int noffset, ulong *load)
843 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
847 * fit_image_get_entry() - get entry point address property
848 * @fit: pointer to the FIT format image header
849 * @noffset: component image node offset
850 * @entry: pointer to the uint32_t, will hold entry point address
852 * This gets the entry point address property for a given component image
855 * fit_image_get_entry() finds entry point address property in a given
856 * component image node. If the property is found, its value is returned
863 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
865 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
869 * fit_image_get_data - get data property and its size for a given component image node
870 * @fit: pointer to the FIT format image header
871 * @noffset: component image node offset
872 * @data: double pointer to void, will hold data property's data address
873 * @size: pointer to size_t, will hold data property's data size
875 * fit_image_get_data() finds data property in a given component image node.
876 * If the property is found its data start address and size are returned to
883 int fit_image_get_data(const void *fit, int noffset,
884 const void **data, size_t *size)
888 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
890 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
900 * Get 'data-offset' property from a given image node.
902 * @fit: pointer to the FIT image header
903 * @noffset: component image node offset
904 * @data_offset: holds the data-offset property
908 * -ENOENT if the property could not be found
910 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
914 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
918 *data_offset = fdt32_to_cpu(*val);
924 * Get 'data-position' property from a given image node.
926 * @fit: pointer to the FIT image header
927 * @noffset: component image node offset
928 * @data_position: holds the data-position property
932 * -ENOENT if the property could not be found
934 int fit_image_get_data_position(const void *fit, int noffset,
939 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
943 *data_position = fdt32_to_cpu(*val);
949 * Get 'data-size' property from a given image node.
951 * @fit: pointer to the FIT image header
952 * @noffset: component image node offset
953 * @data_size: holds the data-size property
957 * -ENOENT if the property could not be found
959 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
963 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
967 *data_size = fdt32_to_cpu(*val);
973 * Get 'data-size-unciphered' property from a given image node.
975 * @fit: pointer to the FIT image header
976 * @noffset: component image node offset
977 * @data_size: holds the data-size property
981 * -ENOENT if the property could not be found
983 int fit_image_get_data_size_unciphered(const void *fit, int noffset,
988 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
992 *data_size = (size_t)fdt32_to_cpu(*val);
998 * fit_image_get_data_and_size - get data and its size including
999 * both embedded and external data
1000 * @fit: pointer to the FIT format image header
1001 * @noffset: component image node offset
1002 * @data: double pointer to void, will hold data property's data address
1003 * @size: pointer to size_t, will hold data property's data size
1005 * fit_image_get_data_and_size() finds data and its size including
1006 * both embedded and external data. If the property is found
1007 * its data start address and size are returned to the caller.
1011 * otherwise, on failure
1013 int fit_image_get_data_and_size(const void *fit, int noffset,
1014 const void **data, size_t *size)
1016 bool external_data = false;
1021 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1022 external_data = true;
1023 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1024 external_data = true;
1026 * For FIT with external data, figure out where
1027 * the external images start. This is the base
1028 * for the data-offset properties in each image.
1030 offset += ((fdt_totalsize(fit) + 3) & ~3);
1033 if (external_data) {
1034 debug("External Data\n");
1035 ret = fit_image_get_data_size(fit, noffset, &len);
1037 *data = fit + offset;
1041 ret = fit_image_get_data(fit, noffset, data, size);
1048 * fit_image_hash_get_algo - get hash algorithm name
1049 * @fit: pointer to the FIT format image header
1050 * @noffset: hash node offset
1051 * @algo: double pointer to char, will hold pointer to the algorithm name
1053 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1054 * If the property is found its data start address is returned to the caller.
1060 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1064 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1065 if (*algo == NULL) {
1066 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1074 * fit_image_hash_get_value - get hash value and length
1075 * @fit: pointer to the FIT format image header
1076 * @noffset: hash node offset
1077 * @value: double pointer to uint8_t, will hold address of a hash value data
1078 * @value_len: pointer to an int, will hold hash data length
1080 * fit_image_hash_get_value() finds hash value property in a given hash node.
1081 * If the property is found its data start address and size are returned to
1088 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1093 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1094 if (*value == NULL) {
1095 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1105 * fit_image_hash_get_ignore - get hash ignore flag
1106 * @fit: pointer to the FIT format image header
1107 * @noffset: hash node offset
1108 * @ignore: pointer to an int, will hold hash ignore flag
1110 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1111 * If the property is found and non-zero, the hash algorithm is not verified by
1112 * u-boot automatically.
1115 * 0, on ignore not found
1116 * value, on ignore found
1118 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1123 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1124 if (value == NULL || len != sizeof(int))
1133 * fit_image_cipher_get_algo - get cipher algorithm name
1134 * @fit: pointer to the FIT format image header
1135 * @noffset: cipher node offset
1136 * @algo: double pointer to char, will hold pointer to the algorithm name
1138 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1139 * cipher node. If the property is found its data start address is returned
1146 int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1150 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1152 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1159 ulong fit_get_end(const void *fit)
1161 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1165 * fit_set_timestamp - set node timestamp property
1166 * @fit: pointer to the FIT format image header
1167 * @noffset: node offset
1168 * @timestamp: timestamp value to be set
1170 * fit_set_timestamp() attempts to set timestamp property in the requested
1171 * node and returns operation status to the caller.
1175 * -ENOSPC if no space in device tree, -1 for other error
1177 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1182 t = cpu_to_uimage(timestamp);
1183 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1186 debug("Can't set '%s' property for '%s' node (%s)\n",
1187 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1189 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1196 * calculate_hash - calculate and return hash for provided input data
1197 * @data: pointer to the input data
1198 * @data_len: data length
1199 * @algo: requested hash algorithm
1200 * @value: pointer to the char, will hold hash value data (caller must
1201 * allocate enough free space)
1202 * value_len: length of the calculated hash
1204 * calculate_hash() computes input data hash according to the requested
1206 * Resulting hash value is placed in caller provided 'value' buffer, length
1207 * of the calculated hash is returned via value_len pointer argument.
1211 * -1, when algo is unsupported
1213 int calculate_hash(const void *data, int data_len, const char *algo,
1214 uint8_t *value, int *value_len)
1216 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1217 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1219 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1221 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1222 sha1_csum_wd((unsigned char *)data, data_len,
1223 (unsigned char *)value, CHUNKSZ_SHA1);
1225 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1226 sha256_csum_wd((unsigned char *)data, data_len,
1227 (unsigned char *)value, CHUNKSZ_SHA256);
1228 *value_len = SHA256_SUM_LEN;
1229 } else if (IMAGE_ENABLE_SHA384 && strcmp(algo, "sha384") == 0) {
1230 sha384_csum_wd((unsigned char *)data, data_len,
1231 (unsigned char *)value, CHUNKSZ_SHA384);
1232 *value_len = SHA384_SUM_LEN;
1233 } else if (IMAGE_ENABLE_SHA512 && strcmp(algo, "sha512") == 0) {
1234 sha512_csum_wd((unsigned char *)data, data_len,
1235 (unsigned char *)value, CHUNKSZ_SHA512);
1236 *value_len = SHA512_SUM_LEN;
1237 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1238 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1241 debug("Unsupported hash alogrithm\n");
1247 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1248 size_t size, char **err_msgp)
1250 uint8_t value[FIT_MAX_HASH_LEN];
1259 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1260 *err_msgp = "Can't get hash algo property";
1265 if (IMAGE_ENABLE_IGNORE) {
1266 fit_image_hash_get_ignore(fit, noffset, &ignore);
1268 printf("-skipped ");
1273 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1275 *err_msgp = "Can't get hash value property";
1279 if (calculate_hash(data, size, algo, value, &value_len)) {
1280 *err_msgp = "Unsupported hash algorithm";
1284 if (value_len != fit_value_len) {
1285 *err_msgp = "Bad hash value len";
1287 } else if (memcmp(value, fit_value, value_len) != 0) {
1288 *err_msgp = "Bad hash value";
1295 int fit_image_verify_with_data(const void *fit, int image_noffset,
1296 const void *data, size_t size)
1303 /* Verify all required signatures */
1304 if (FIT_IMAGE_ENABLE_VERIFY &&
1305 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1306 gd_fdt_blob(), &verify_all)) {
1307 err_msg = "Unable to verify required signature";
1311 /* Process all hash subnodes of the component image node */
1312 fdt_for_each_subnode(noffset, fit, image_noffset) {
1313 const char *name = fit_get_name(fit, noffset, NULL);
1316 * Check subnode name, must be equal to "hash".
1317 * Multiple hash nodes require unique unit node
1318 * names, e.g. hash-1, hash-2, etc.
1320 if (!strncmp(name, FIT_HASH_NODENAME,
1321 strlen(FIT_HASH_NODENAME))) {
1322 if (fit_image_check_hash(fit, noffset, data, size,
1326 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
1327 !strncmp(name, FIT_SIG_NODENAME,
1328 strlen(FIT_SIG_NODENAME))) {
1329 ret = fit_image_check_sig(fit, noffset, data,
1330 size, -1, &err_msg);
1333 * Show an indication on failure, but do not return
1334 * an error. Only keys marked 'required' can cause
1335 * an image validation failure. See the call to
1336 * fit_image_verify_required_sigs() above.
1345 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1346 err_msg = "Corrupted or truncated tree";
1353 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1354 err_msg, fit_get_name(fit, noffset, NULL),
1355 fit_get_name(fit, image_noffset, NULL));
1360 * fit_image_verify - verify data integrity
1361 * @fit: pointer to the FIT format image header
1362 * @image_noffset: component image node offset
1364 * fit_image_verify() goes over component image hash nodes,
1365 * re-calculates each data hash and compares with the value stored in hash
1369 * 1, if all hashes are valid
1370 * 0, otherwise (or on error)
1372 int fit_image_verify(const void *fit, int image_noffset)
1374 const char *name = fit_get_name(fit, image_noffset, NULL);
1379 if (strchr(name, '@')) {
1381 * We don't support this since libfdt considers names with the
1382 * name root but different @ suffix to be equal
1384 err_msg = "Node name contains @";
1387 /* Get image data and data length */
1388 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1389 err_msg = "Can't get image data/size";
1393 return fit_image_verify_with_data(fit, image_noffset, data, size);
1396 printf("error!\n%s in '%s' image node\n", err_msg,
1397 fit_get_name(fit, image_noffset, NULL));
1402 * fit_all_image_verify - verify data integrity for all images
1403 * @fit: pointer to the FIT format image header
1405 * fit_all_image_verify() goes over all images in the FIT and
1406 * for every images checks if all it's hashes are valid.
1409 * 1, if all hashes of all images are valid
1410 * 0, otherwise (or on error)
1412 int fit_all_image_verify(const void *fit)
1419 /* Find images parent node offset */
1420 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1421 if (images_noffset < 0) {
1422 printf("Can't find images parent node '%s' (%s)\n",
1423 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1427 /* Process all image subnodes, check hashes for each */
1428 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1430 for (ndepth = 0, count = 0,
1431 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1432 (noffset >= 0) && (ndepth > 0);
1433 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1436 * Direct child node of the images parent node,
1437 * i.e. component image node.
1439 printf(" Hash(es) for Image %u (%s): ", count,
1440 fit_get_name(fit, noffset, NULL));
1443 if (!fit_image_verify(fit, noffset))
1451 static int fit_image_uncipher(const void *fit, int image_noffset,
1452 void **data, size_t *size)
1454 int cipher_noffset, ret;
1458 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1459 FIT_CIPHER_NODENAME);
1460 if (cipher_noffset < 0)
1463 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1464 *data, *size, &dst, &size_dst);
1476 * fit_image_check_os - check whether image node is of a given os type
1477 * @fit: pointer to the FIT format image header
1478 * @noffset: component image node offset
1479 * @os: requested image os
1481 * fit_image_check_os() reads image os property and compares its numeric
1482 * id with the requested os. Comparison result is returned to the caller.
1485 * 1 if image is of given os type
1486 * 0 otherwise (or on error)
1488 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1492 if (fit_image_get_os(fit, noffset, &image_os))
1494 return (os == image_os);
1498 * fit_image_check_arch - check whether image node is of a given arch
1499 * @fit: pointer to the FIT format image header
1500 * @noffset: component image node offset
1501 * @arch: requested imagearch
1503 * fit_image_check_arch() reads image arch property and compares its numeric
1504 * id with the requested arch. Comparison result is returned to the caller.
1507 * 1 if image is of given arch
1508 * 0 otherwise (or on error)
1510 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1513 int aarch32_support = 0;
1515 if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32))
1516 aarch32_support = 1;
1518 if (fit_image_get_arch(fit, noffset, &image_arch))
1520 return (arch == image_arch) ||
1521 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1522 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1527 * fit_image_check_type - check whether image node is of a given type
1528 * @fit: pointer to the FIT format image header
1529 * @noffset: component image node offset
1530 * @type: requested image type
1532 * fit_image_check_type() reads image type property and compares its numeric
1533 * id with the requested type. Comparison result is returned to the caller.
1536 * 1 if image is of given type
1537 * 0 otherwise (or on error)
1539 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1543 if (fit_image_get_type(fit, noffset, &image_type))
1545 return (type == image_type);
1549 * fit_image_check_comp - check whether image node uses given compression
1550 * @fit: pointer to the FIT format image header
1551 * @noffset: component image node offset
1552 * @comp: requested image compression type
1554 * fit_image_check_comp() reads image compression property and compares its
1555 * numeric id with the requested compression type. Comparison result is
1556 * returned to the caller.
1559 * 1 if image uses requested compression
1560 * 0 otherwise (or on error)
1562 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1566 if (fit_image_get_comp(fit, noffset, &image_comp))
1568 return (comp == image_comp);
1572 * fdt_check_no_at() - Check for nodes whose names contain '@'
1574 * This checks the parent node and all subnodes recursively
1576 * @fit: FIT to check
1577 * @parent: Parent node to check
1578 * @return 0 if OK, -EADDRNOTAVAIL is a node has a name containing '@'
1580 static int fdt_check_no_at(const void *fit, int parent)
1586 name = fdt_get_name(fit, parent, NULL);
1587 if (!name || strchr(name, '@'))
1588 return -EADDRNOTAVAIL;
1590 fdt_for_each_subnode(node, fit, parent) {
1591 ret = fdt_check_no_at(fit, node);
1599 int fit_check_format(const void *fit, ulong size)
1603 /* A FIT image must be a valid FDT */
1604 ret = fdt_check_header(fit);
1606 log_debug("Wrong FIT format: not a flattened device tree (err=%d)\n",
1611 if (CONFIG_IS_ENABLED(FIT_FULL_CHECK)) {
1613 * If we are not given the size, make do wtih calculating it.
1614 * This is not as secure, so we should consider a flag to
1617 if (size == IMAGE_SIZE_INVAL)
1618 size = fdt_totalsize(fit);
1619 ret = fdt_check_full(fit, size);
1624 * U-Boot stopped using unit addressed in 2017. Since libfdt
1625 * can match nodes ignoring any unit address, signature
1626 * verification can see the wrong node if one is inserted with
1627 * the same name as a valid node but with a unit address
1628 * attached. Protect against this by disallowing unit addresses.
1630 if (!ret && CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
1631 ret = fdt_check_no_at(fit, 0);
1634 log_debug("FIT check error %d\n", ret);
1639 log_debug("FIT check error %d\n", ret);
1644 /* mandatory / node 'description' property */
1645 if (!fdt_getprop(fit, 0, FIT_DESC_PROP, NULL)) {
1646 log_debug("Wrong FIT format: no description\n");
1650 if (IMAGE_ENABLE_TIMESTAMP) {
1651 /* mandatory / node 'timestamp' property */
1652 if (!fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL)) {
1653 log_debug("Wrong FIT format: no timestamp\n");
1658 /* mandatory subimages parent '/images' node */
1659 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1660 log_debug("Wrong FIT format: no images parent node\n");
1668 * fit_conf_find_compat
1669 * @fit: pointer to the FIT format image header
1670 * @fdt: pointer to the device tree to compare against
1672 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1673 * most compatible with the passed in device tree.
1682 * |-o configurations
1690 * |-compatible = "foo,bar", "bim,bam"
1693 * |-compatible = "foo,bar",
1696 * |-compatible = "bim,bam", "baz,biz"
1698 * Configuration 1 would be picked because the first string in U-Boot's
1699 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1700 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1702 * As an optimization, the compatible property from the FDT's root node can be
1703 * copied into the configuration node in the FIT image. This is required to
1704 * match configurations with compressed FDTs.
1707 * offset to the configuration to use if one was found
1710 int fit_conf_find_compat(const void *fit, const void *fdt)
1713 int noffset, confs_noffset, images_noffset;
1714 const void *fdt_compat;
1716 int best_match_offset = 0;
1717 int best_match_pos = 0;
1719 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1720 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1721 if (confs_noffset < 0 || images_noffset < 0) {
1722 debug("Can't find configurations or images nodes.\n");
1726 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1728 debug("Fdt for comparison has no \"compatible\" property.\n");
1733 * Loop over the configurations in the FIT image.
1735 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1736 (noffset >= 0) && (ndepth > 0);
1737 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1739 const char *kfdt_name;
1740 int kfdt_noffset, compat_noffset;
1741 const char *cur_fdt_compat;
1749 /* If there's a compat property in the config node, use that. */
1750 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1751 fdt = fit; /* search in FIT image */
1752 compat_noffset = noffset; /* search under config node */
1753 } else { /* Otherwise extract it from the kernel FDT. */
1754 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1756 debug("No fdt property found.\n");
1759 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1761 if (kfdt_noffset < 0) {
1762 debug("No image node named \"%s\" found.\n",
1767 if (!fit_image_check_comp(fit, kfdt_noffset,
1769 debug("Can't extract compat from \"%s\" "
1770 "(compressed)\n", kfdt_name);
1774 /* search in this config's kernel FDT */
1775 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1776 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1780 compat_noffset = 0; /* search kFDT under root node */
1783 len = fdt_compat_len;
1784 cur_fdt_compat = fdt_compat;
1786 * Look for a match for each U-Boot compatibility string in
1787 * turn in the compat string property.
1789 for (i = 0; len > 0 &&
1790 (!best_match_offset || best_match_pos > i); i++) {
1791 int cur_len = strlen(cur_fdt_compat) + 1;
1793 if (!fdt_node_check_compatible(fdt, compat_noffset,
1795 best_match_offset = noffset;
1800 cur_fdt_compat += cur_len;
1803 if (!best_match_offset) {
1804 debug("No match found.\n");
1808 return best_match_offset;
1811 int fit_conf_get_node(const void *fit, const char *conf_uname)
1813 int noffset, confs_noffset;
1816 char *conf_uname_copy = NULL;
1818 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1819 if (confs_noffset < 0) {
1820 debug("Can't find configurations parent node '%s' (%s)\n",
1821 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1822 return confs_noffset;
1825 if (conf_uname == NULL) {
1826 /* get configuration unit name from the default property */
1827 debug("No configuration specified, trying default...\n");
1828 if (!host_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) {
1829 noffset = fit_find_config_node(fit);
1832 conf_uname = fdt_get_name(fit, noffset, NULL);
1834 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1835 FIT_DEFAULT_PROP, &len);
1836 if (conf_uname == NULL) {
1837 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1842 debug("Found default configuration: '%s'\n", conf_uname);
1845 s = strchr(conf_uname, '#');
1847 len = s - conf_uname;
1848 conf_uname_copy = malloc(len + 1);
1849 if (!conf_uname_copy) {
1850 debug("Can't allocate uname copy: '%s'\n",
1854 memcpy(conf_uname_copy, conf_uname, len);
1855 conf_uname_copy[len] = '\0';
1856 conf_uname = conf_uname_copy;
1859 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1861 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1862 conf_uname, fdt_strerror(noffset));
1865 if (conf_uname_copy)
1866 free(conf_uname_copy);
1871 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1872 const char *prop_name)
1874 return fdt_stringlist_count(fit, noffset, prop_name);
1877 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1878 const char *prop_name, int index)
1883 /* get kernel image unit name from configuration kernel property */
1884 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1888 return fit_image_get_node(fit, uname);
1891 int fit_conf_get_prop_node(const void *fit, int noffset,
1892 const char *prop_name)
1894 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1897 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1899 fit_image_print(fit, rd_noffset, " ");
1902 puts(" Verifying Hash Integrity ... ");
1903 if (!fit_image_verify(fit, rd_noffset)) {
1904 puts("Bad Data Hash\n");
1913 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1920 debug("* %s: using config '%s' from image at 0x%08lx\n",
1921 prop_name, images->fit_uname_cfg, addr);
1923 /* Check whether configuration has this property defined */
1924 fit_hdr = map_sysmem(addr, 0);
1925 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1926 if (cfg_noffset < 0) {
1927 debug("* %s: no such config\n", prop_name);
1931 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1933 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1941 * fit_get_image_type_property() - get property name for IH_TYPE_...
1943 * @return the properly name where we expect to find the image in the
1946 static const char *fit_get_image_type_property(int type)
1949 * This is sort-of available in the uimage_type[] table in image.c
1950 * but we don't have access to the short name, and "fdt" is different
1951 * anyway. So let's just keep it here.
1954 case IH_TYPE_FLATDT:
1955 return FIT_FDT_PROP;
1956 case IH_TYPE_KERNEL:
1957 return FIT_KERNEL_PROP;
1958 case IH_TYPE_RAMDISK:
1959 return FIT_RAMDISK_PROP;
1960 case IH_TYPE_X86_SETUP:
1961 return FIT_SETUP_PROP;
1962 case IH_TYPE_LOADABLE:
1963 return FIT_LOADABLE_PROP;
1965 return FIT_FPGA_PROP;
1966 case IH_TYPE_STANDALONE:
1967 return FIT_STANDALONE_PROP;
1973 int fit_image_load(bootm_headers_t *images, ulong addr,
1974 const char **fit_unamep, const char **fit_uname_configp,
1975 int arch, int image_type, int bootstage_id,
1976 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1978 int cfg_noffset, noffset;
1979 const char *fit_uname;
1980 const char *fit_uname_config;
1981 const char *fit_base_uname_config;
1987 ulong load, load_end, data, len;
1992 const char *prop_name;
1995 fit = map_sysmem(addr, 0);
1996 fit_uname = fit_unamep ? *fit_unamep : NULL;
1997 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1998 fit_base_uname_config = NULL;
1999 prop_name = fit_get_image_type_property(image_type);
2000 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
2002 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
2003 ret = fit_check_format(fit, IMAGE_SIZE_INVAL);
2005 printf("Bad FIT %s image format! (err=%d)\n", prop_name, ret);
2006 if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && ret == -EADDRNOTAVAIL)
2007 printf("Signature checking prevents use of unit addresses (@) in nodes\n");
2008 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
2011 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
2013 /* get FIT component image node offset */
2014 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
2015 noffset = fit_image_get_node(fit, fit_uname);
2018 * no image node unit name, try to get config
2019 * node first. If config unit node name is NULL
2020 * fit_conf_get_node() will try to find default config node
2022 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
2023 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
2024 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
2026 cfg_noffset = fit_conf_get_node(fit,
2029 if (cfg_noffset < 0) {
2030 puts("Could not find configuration node\n");
2031 bootstage_error(bootstage_id +
2032 BOOTSTAGE_SUB_NO_UNIT_NAME);
2036 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
2037 printf(" Using '%s' configuration\n", fit_base_uname_config);
2038 /* Remember this config */
2039 if (image_type == IH_TYPE_KERNEL)
2040 images->fit_uname_cfg = fit_base_uname_config;
2042 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
2043 puts(" Verifying Hash Integrity ... ");
2044 if (fit_config_verify(fit, cfg_noffset)) {
2045 puts("Bad Data Hash\n");
2046 bootstage_error(bootstage_id +
2047 BOOTSTAGE_SUB_HASH);
2053 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
2055 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
2057 fit_uname = fit_get_name(fit, noffset, NULL);
2060 printf("Could not find subimage node type '%s'\n", prop_name);
2061 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
2065 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
2067 ret = fit_image_select(fit, noffset, images->verify);
2069 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2073 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2074 if (!host_build() && IS_ENABLED(CONFIG_SANDBOX)) {
2075 if (!fit_image_check_target_arch(fit, noffset)) {
2076 puts("Unsupported Architecture\n");
2077 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2083 fit_image_get_arch(fit, noffset, &os_arch);
2084 images->os.arch = os_arch;
2087 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2088 type_ok = fit_image_check_type(fit, noffset, image_type) ||
2089 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
2090 (image_type == IH_TYPE_KERNEL &&
2091 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
2093 os_ok = image_type == IH_TYPE_FLATDT ||
2094 image_type == IH_TYPE_FPGA ||
2095 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
2096 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
2097 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
2098 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
2099 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
2102 * If either of the checks fail, we should report an error, but
2103 * if the image type is coming from the "loadables" field, we
2104 * don't care what it is
2106 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
2107 fit_image_get_os(fit, noffset, &os);
2108 printf("No %s %s %s Image\n",
2109 genimg_get_os_name(os),
2110 genimg_get_arch_name(arch),
2111 genimg_get_type_name(image_type));
2112 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2116 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2118 /* get image data address and length */
2119 if (fit_image_get_data_and_size(fit, noffset,
2120 (const void **)&buf, &size)) {
2121 printf("Could not find %s subimage data!\n", prop_name);
2122 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2126 /* Decrypt data before uncompress/move */
2127 if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
2128 puts(" Decrypting Data ... ");
2129 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2136 /* perform any post-processing on the image data */
2137 if (!host_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
2138 board_fit_image_post_process(&buf, &size);
2142 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2144 data = map_to_sysmem(buf);
2146 if (load_op == FIT_LOAD_IGNORED) {
2148 } else if (fit_image_get_load(fit, noffset, &load)) {
2149 if (load_op == FIT_LOAD_REQUIRED) {
2150 printf("Can't get %s subimage load address!\n",
2152 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2155 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
2156 ulong image_start, image_end;
2159 * move image data to the load address,
2160 * make sure we don't overwrite initial image
2163 image_end = addr + fit_get_size(fit);
2165 load_end = load + len;
2166 if (image_type != IH_TYPE_KERNEL &&
2167 load < image_end && load_end > image_start) {
2168 printf("Error: %s overwritten\n", prop_name);
2172 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2173 prop_name, data, load);
2175 load = data; /* No load address specified */
2178 comp = IH_COMP_NONE;
2180 /* Kernel images get decompressed later in bootm_load_os(). */
2181 if (!fit_image_get_comp(fit, noffset, &comp) &&
2182 comp != IH_COMP_NONE &&
2183 !(image_type == IH_TYPE_KERNEL ||
2184 image_type == IH_TYPE_KERNEL_NOLOAD ||
2185 image_type == IH_TYPE_RAMDISK)) {
2186 ulong max_decomp_len = len * 20;
2188 loadbuf = malloc(max_decomp_len);
2189 load = map_to_sysmem(loadbuf);
2191 loadbuf = map_sysmem(load, max_decomp_len);
2193 if (image_decomp(comp, load, data, image_type,
2194 loadbuf, buf, len, max_decomp_len, &load_end)) {
2195 printf("Error decompressing %s\n", prop_name);
2199 len = load_end - load;
2200 } else if (load != data) {
2201 loadbuf = map_sysmem(load, len);
2202 memcpy(loadbuf, buf, len);
2205 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2206 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2207 " please fix your .its file!\n");
2209 /* verify that image data is a proper FDT blob */
2210 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2211 puts("Subimage data is not a FDT");
2215 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2220 *fit_unamep = (char *)fit_uname;
2221 if (fit_uname_configp)
2222 *fit_uname_configp = (char *)(fit_uname_config ? :
2223 fit_base_uname_config);
2228 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2229 ulong *setup_start, ulong *setup_len)
2236 addr = map_to_sysmem(images->fit_hdr_os);
2237 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2241 ret = fit_image_load(images, addr, NULL, NULL, arch,
2242 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2243 FIT_LOAD_REQUIRED, setup_start, &len);
2249 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2250 const char **fit_unamep, const char **fit_uname_configp,
2251 int arch, ulong *datap, ulong *lenp)
2253 int fdt_noffset, cfg_noffset, count;
2255 const char *fit_uname = NULL;
2256 const char *fit_uname_config = NULL;
2257 char *fit_uname_config_copy = NULL;
2258 char *next_config = NULL;
2260 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2261 ulong image_start, image_end;
2262 ulong ovload, ovlen;
2263 const char *uconfig;
2266 int i, err, noffset, ov_noffset;
2269 fit_uname = fit_unamep ? *fit_unamep : NULL;
2271 if (fit_uname_configp && *fit_uname_configp) {
2272 fit_uname_config_copy = strdup(*fit_uname_configp);
2273 if (!fit_uname_config_copy)
2276 next_config = strchr(fit_uname_config_copy, '#');
2278 *next_config++ = '\0';
2279 if (next_config - 1 > fit_uname_config_copy)
2280 fit_uname_config = fit_uname_config_copy;
2283 fdt_noffset = fit_image_load(images,
2284 addr, &fit_uname, &fit_uname_config,
2285 arch, IH_TYPE_FLATDT,
2286 BOOTSTAGE_ID_FIT_FDT_START,
2287 FIT_LOAD_OPTIONAL, &load, &len);
2289 if (fdt_noffset < 0)
2292 debug("fit_uname=%s, fit_uname_config=%s\n",
2293 fit_uname ? fit_uname : "<NULL>",
2294 fit_uname_config ? fit_uname_config : "<NULL>");
2296 fit = map_sysmem(addr, 0);
2298 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2300 /* single blob, or error just return as well */
2301 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2302 if (count <= 1 && !next_config)
2305 /* we need to apply overlays */
2307 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2309 image_end = addr + fit_get_size(fit);
2310 /* verify that relocation took place by load address not being in fit */
2311 if (load >= image_start && load < image_end) {
2312 /* check is simplified; fit load checks for overlaps */
2313 printf("Overlayed FDT requires relocation\n");
2314 fdt_noffset = -EBADF;
2318 base = map_sysmem(load, len);
2320 /* apply extra configs in FIT first, followed by args */
2321 for (i = 1; ; i++) {
2323 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2325 uname = fit_get_name(fit, noffset, NULL);
2330 uconfig = next_config;
2331 next_config = strchr(next_config, '#');
2333 *next_config++ = '\0';
2337 * fit_image_load() would load the first FDT from the
2338 * extra config only when uconfig is specified.
2339 * Check if the extra config contains multiple FDTs and
2342 cfg_noffset = fit_conf_get_node(fit, uconfig);
2345 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2349 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2351 ov_noffset = fit_image_load(images,
2352 addr, &uname, &uconfig,
2353 arch, IH_TYPE_FLATDT,
2354 BOOTSTAGE_ID_FIT_FDT_START,
2355 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2356 if (ov_noffset < 0) {
2357 printf("load of %s failed\n", uname);
2360 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2361 uname, ovload, ovlen);
2362 ov = map_sysmem(ovload, ovlen);
2364 base = map_sysmem(load, len + ovlen);
2365 err = fdt_open_into(base, base, len + ovlen);
2367 printf("failed on fdt_open_into\n");
2371 /* the verbose method prints out messages on error */
2372 err = fdt_overlay_apply_verbose(base, ov);
2378 len = fdt_totalsize(base);
2381 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2382 fdt_noffset = -EBADF;
2391 *fit_unamep = fit_uname;
2392 if (fit_uname_configp)
2393 *fit_uname_configp = fit_uname_config;
2395 if (fit_uname_config_copy)
2396 free(fit_uname_config_copy);