1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
5 * (C) Copyright 2008 Semihalf
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 #include <linux/libfdt.h>
15 #include <u-boot/crc.h>
17 #include <linux/compiler.h>
24 DECLARE_GLOBAL_DATA_PTR;
25 #endif /* !USE_HOSTCC*/
29 #include <bootstage.h>
30 #include <linux/kconfig.h>
31 #include <u-boot/crc.h>
32 #include <u-boot/md5.h>
33 #include <u-boot/sha1.h>
34 #include <u-boot/sha256.h>
35 #include <u-boot/sha512.h>
37 /*****************************************************************************/
38 /* New uImage format routines */
39 /*****************************************************************************/
41 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
42 ulong *addr, const char **name)
49 sep = strchr(spec, sepc);
52 *addr = simple_strtoul(spec, NULL, 16);
62 * fit_parse_conf - parse FIT configuration spec
63 * @spec: input string, containing configuration spec
64 * @add_curr: current image address (to be used as a possible default)
65 * @addr: pointer to a ulong variable, will hold FIT image address of a given
67 * @conf_name double pointer to a char, will hold pointer to a configuration
70 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
71 * where <addr> is a FIT image address that contains configuration
72 * with a <conf> unit name.
74 * Address part is optional, and if omitted default add_curr will
78 * 1 if spec is a valid configuration string,
79 * addr and conf_name are set accordingly
82 int fit_parse_conf(const char *spec, ulong addr_curr,
83 ulong *addr, const char **conf_name)
85 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
89 * fit_parse_subimage - parse FIT subimage spec
90 * @spec: input string, containing subimage spec
91 * @add_curr: current image address (to be used as a possible default)
92 * @addr: pointer to a ulong variable, will hold FIT image address of a given
94 * @image_name: double pointer to a char, will hold pointer to a subimage name
96 * fit_parse_subimage() expects subimage spec in the form of
97 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
98 * subimage with a <subimg> unit name.
100 * Address part is optional, and if omitted default add_curr will
104 * 1 if spec is a valid subimage string,
105 * addr and image_name are set accordingly
108 int fit_parse_subimage(const char *spec, ulong addr_curr,
109 ulong *addr, const char **image_name)
111 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
113 #endif /* !USE_HOSTCC */
116 /* Host tools use these implementations for Cipher and Signature support */
117 static void *host_blob;
119 void image_set_host_blob(void *blob)
124 void *image_get_host_blob(void)
128 #endif /* USE_HOSTCC */
130 static void fit_get_debug(const void *fit, int noffset,
131 char *prop_name, int err)
133 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
134 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
139 * fit_get_subimage_count - get component (sub-image) count
140 * @fit: pointer to the FIT format image header
141 * @images_noffset: offset of images node
144 * number of image components
146 int fit_get_subimage_count(const void *fit, int images_noffset)
152 /* Process its subnodes, print out component images details */
153 for (ndepth = 0, count = 0,
154 noffset = fdt_next_node(fit, images_noffset, &ndepth);
155 (noffset >= 0) && (ndepth > 0);
156 noffset = fdt_next_node(fit, noffset, &ndepth)) {
165 #if CONFIG_IS_ENABLED(FIT_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT)
167 * fit_image_print_data() - prints out the hash node details
168 * @fit: pointer to the FIT format image header
169 * @noffset: offset of the hash node
170 * @p: pointer to prefix string
171 * @type: Type of information to print ("hash" or "sign")
173 * fit_image_print_data() lists properties for the processed hash node
175 * This function avoid using puts() since it prints a newline on the host
176 * but does not in U-Boot.
179 * no returned results
181 static void fit_image_print_data(const void *fit, int noffset, const char *p,
192 debug("%s %s node: '%s'\n", p, type,
193 fit_get_name(fit, noffset, NULL));
194 printf("%s %s algo: ", p, type);
195 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
196 printf("invalid/unsupported\n");
200 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
201 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
203 printf(":%s", keyname);
205 printf(" (required)");
208 padding = fdt_getprop(fit, noffset, "padding", NULL);
210 printf("%s %s padding: %s\n", p, type, padding);
212 ret = fit_image_hash_get_value(fit, noffset, &value,
214 printf("%s %s value: ", p, type);
216 printf("unavailable\n");
218 for (i = 0; i < value_len; i++)
219 printf("%02x", value[i]);
223 debug("%s %s len: %d\n", p, type, value_len);
225 /* Signatures have a time stamp */
226 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
229 printf("%s Timestamp: ", p);
230 if (fit_get_timestamp(fit, noffset, ×tamp))
231 printf("unavailable\n");
233 genimg_print_time(timestamp);
238 * fit_image_print_verification_data() - prints out the hash/signature details
239 * @fit: pointer to the FIT format image header
240 * @noffset: offset of the hash or signature node
241 * @p: pointer to prefix string
243 * This lists properties for the processed hash node
246 * no returned results
248 static void fit_image_print_verification_data(const void *fit, int noffset,
254 * Check subnode name, must be equal to "hash" or "signature".
255 * Multiple hash/signature nodes require unique unit node
256 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
258 name = fit_get_name(fit, noffset, NULL);
259 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
260 fit_image_print_data(fit, noffset, p, "Hash");
261 } else if (!strncmp(name, FIT_SIG_NODENAME,
262 strlen(FIT_SIG_NODENAME))) {
263 fit_image_print_data(fit, noffset, p, "Sign");
268 * fit_conf_print - prints out the FIT configuration details
269 * @fit: pointer to the FIT format image header
270 * @noffset: offset of the configuration node
271 * @p: pointer to prefix string
273 * fit_conf_print() lists all mandatory properties for the processed
274 * configuration node.
277 * no returned results
279 static void fit_conf_print(const void *fit, int noffset, const char *p)
284 int fdt_index, loadables_index;
287 /* Mandatory properties */
288 ret = fit_get_desc(fit, noffset, &desc);
289 printf("%s Description: ", p);
291 printf("unavailable\n");
293 printf("%s\n", desc);
295 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
296 printf("%s Kernel: ", p);
298 printf("unavailable\n");
300 printf("%s\n", uname);
302 /* Optional properties */
303 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
305 printf("%s Init Ramdisk: %s\n", p, uname);
307 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
309 printf("%s Firmware: %s\n", p, uname);
312 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
313 fdt_index, NULL), uname;
316 printf("%s FDT: ", p);
319 printf("%s\n", uname);
322 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
324 printf("%s FPGA: %s\n", p, uname);
326 /* Print out all of the specified loadables */
327 for (loadables_index = 0;
328 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
329 loadables_index, NULL), uname;
331 if (loadables_index == 0) {
332 printf("%s Loadables: ", p);
336 printf("%s\n", uname);
339 /* Process all hash subnodes of the component configuration node */
340 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
341 (noffset >= 0) && (ndepth > 0);
342 noffset = fdt_next_node(fit, noffset, &ndepth)) {
344 /* Direct child node of the component configuration node */
345 fit_image_print_verification_data(fit, noffset, p);
351 * fit_print_contents - prints out the contents of the FIT format image
352 * @fit: pointer to the FIT format image header
353 * @p: pointer to prefix string
355 * fit_print_contents() formats a multi line FIT image contents description.
356 * The routine prints out FIT image properties (root node level) followed by
357 * the details of each component image.
360 * no returned results
362 void fit_print_contents(const void *fit)
375 /* Indent string is defined in header image.h */
376 p = IMAGE_INDENT_STRING;
378 /* Root node properties */
379 ret = fit_get_desc(fit, 0, &desc);
380 printf("%sFIT description: ", p);
382 printf("unavailable\n");
384 printf("%s\n", desc);
386 if (IMAGE_ENABLE_TIMESTAMP) {
387 ret = fit_get_timestamp(fit, 0, ×tamp);
388 printf("%sCreated: ", p);
390 printf("unavailable\n");
392 genimg_print_time(timestamp);
395 /* Find images parent node offset */
396 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
397 if (images_noffset < 0) {
398 printf("Can't find images parent node '%s' (%s)\n",
399 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
403 /* Process its subnodes, print out component images details */
404 for (ndepth = 0, count = 0,
405 noffset = fdt_next_node(fit, images_noffset, &ndepth);
406 (noffset >= 0) && (ndepth > 0);
407 noffset = fdt_next_node(fit, noffset, &ndepth)) {
410 * Direct child node of the images parent node,
411 * i.e. component image node.
413 printf("%s Image %u (%s)\n", p, count++,
414 fit_get_name(fit, noffset, NULL));
416 fit_image_print(fit, noffset, p);
420 /* Find configurations parent node offset */
421 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
422 if (confs_noffset < 0) {
423 debug("Can't get configurations parent node '%s' (%s)\n",
424 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
428 /* get default configuration unit name from default property */
429 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
431 printf("%s Default Configuration: '%s'\n", p, uname);
433 /* Process its subnodes, print out configurations details */
434 for (ndepth = 0, count = 0,
435 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
436 (noffset >= 0) && (ndepth > 0);
437 noffset = fdt_next_node(fit, noffset, &ndepth)) {
440 * Direct child node of the configurations parent node,
441 * i.e. configuration node.
443 printf("%s Configuration %u (%s)\n", p, count++,
444 fit_get_name(fit, noffset, NULL));
446 fit_conf_print(fit, noffset, p);
452 * fit_image_print - prints out the FIT component image details
453 * @fit: pointer to the FIT format image header
454 * @image_noffset: offset of the component image node
455 * @p: pointer to prefix string
457 * fit_image_print() lists all mandatory properties for the processed component
458 * image. If present, hash nodes are printed out as well. Load
459 * address for images of type firmware is also printed out. Since the load
460 * address is not mandatory for firmware images, it will be output as
461 * "unavailable" when not present.
464 * no returned results
466 void fit_image_print(const void *fit, int image_noffset, const char *p)
469 uint8_t type, arch, os, comp;
477 /* Mandatory properties */
478 ret = fit_get_desc(fit, image_noffset, &desc);
479 printf("%s Description: ", p);
481 printf("unavailable\n");
483 printf("%s\n", desc);
485 if (IMAGE_ENABLE_TIMESTAMP) {
488 ret = fit_get_timestamp(fit, 0, ×tamp);
489 printf("%s Created: ", p);
491 printf("unavailable\n");
493 genimg_print_time(timestamp);
496 fit_image_get_type(fit, image_noffset, &type);
497 printf("%s Type: %s\n", p, genimg_get_type_name(type));
499 fit_image_get_comp(fit, image_noffset, &comp);
500 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
502 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
505 printf("%s Data Start: ", p);
507 printf("unavailable\n");
509 void *vdata = (void *)data;
511 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
515 printf("%s Data Size: ", p);
517 printf("unavailable\n");
519 genimg_print_size(size);
521 /* Remaining, type dependent properties */
522 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
523 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
524 (type == IH_TYPE_FLATDT)) {
525 fit_image_get_arch(fit, image_noffset, &arch);
526 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
529 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
530 (type == IH_TYPE_FIRMWARE)) {
531 fit_image_get_os(fit, image_noffset, &os);
532 printf("%s OS: %s\n", p, genimg_get_os_name(os));
535 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
536 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
537 (type == IH_TYPE_FPGA)) {
538 ret = fit_image_get_load(fit, image_noffset, &load);
539 printf("%s Load Address: ", p);
541 printf("unavailable\n");
543 printf("0x%08lx\n", load);
546 /* optional load address for FDT */
547 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
548 printf("%s Load Address: 0x%08lx\n", p, load);
550 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
551 (type == IH_TYPE_RAMDISK)) {
552 ret = fit_image_get_entry(fit, image_noffset, &entry);
553 printf("%s Entry Point: ", p);
555 printf("unavailable\n");
557 printf("0x%08lx\n", entry);
560 /* Process all hash subnodes of the component image node */
561 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
562 (noffset >= 0) && (ndepth > 0);
563 noffset = fdt_next_node(fit, noffset, &ndepth)) {
565 /* Direct child node of the component image node */
566 fit_image_print_verification_data(fit, noffset, p);
571 void fit_print_contents(const void *fit) { }
572 void fit_image_print(const void *fit, int image_noffset, const char *p) { }
573 #endif /* CONFIG_IS_ENABLED(FIR_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT) */
576 * fit_get_desc - get node description property
577 * @fit: pointer to the FIT format image header
578 * @noffset: node offset
579 * @desc: double pointer to the char, will hold pointer to the description
581 * fit_get_desc() reads description property from a given node, if
582 * description is found pointer to it is returned in third call argument.
588 int fit_get_desc(const void *fit, int noffset, char **desc)
592 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
594 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
602 * fit_get_timestamp - get node timestamp property
603 * @fit: pointer to the FIT format image header
604 * @noffset: node offset
605 * @timestamp: pointer to the time_t, will hold read timestamp
607 * fit_get_timestamp() reads timestamp property from given node, if timestamp
608 * is found and has a correct size its value is returned in third call
613 * -1, on property read failure
614 * -2, on wrong timestamp size
616 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
621 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
623 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
626 if (len != sizeof(uint32_t)) {
627 debug("FIT timestamp with incorrect size of (%u)\n", len);
631 *timestamp = uimage_to_cpu(*((uint32_t *)data));
636 * fit_image_get_node - get node offset for component image of a given unit name
637 * @fit: pointer to the FIT format image header
638 * @image_uname: component image node unit name
640 * fit_image_get_node() finds a component image (within the '/images'
641 * node) of a provided unit name. If image is found its node offset is
642 * returned to the caller.
645 * image node offset when found (>=0)
646 * negative number on failure (FDT_ERR_* code)
648 int fit_image_get_node(const void *fit, const char *image_uname)
650 int noffset, images_noffset;
652 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
653 if (images_noffset < 0) {
654 debug("Can't find images parent node '%s' (%s)\n",
655 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
656 return images_noffset;
659 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
661 debug("Can't get node offset for image unit name: '%s' (%s)\n",
662 image_uname, fdt_strerror(noffset));
669 * fit_image_get_os - get os id for a given component image node
670 * @fit: pointer to the FIT format image header
671 * @noffset: component image node offset
672 * @os: pointer to the uint8_t, will hold os numeric id
674 * fit_image_get_os() finds os property in a given component image node.
675 * If the property is found, its (string) value is translated to the numeric
676 * id which is returned to the caller.
682 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
687 /* Get OS name from property data */
688 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
690 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
695 /* Translate OS name to id */
696 *os = genimg_get_os_id(data);
701 * fit_image_get_arch - get arch id for a given component image node
702 * @fit: pointer to the FIT format image header
703 * @noffset: component image node offset
704 * @arch: pointer to the uint8_t, will hold arch numeric id
706 * fit_image_get_arch() finds arch property in a given component image node.
707 * If the property is found, its (string) value is translated to the numeric
708 * id which is returned to the caller.
714 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
719 /* Get architecture name from property data */
720 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
722 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
727 /* Translate architecture name to id */
728 *arch = genimg_get_arch_id(data);
733 * fit_image_get_type - get type id for a given component image node
734 * @fit: pointer to the FIT format image header
735 * @noffset: component image node offset
736 * @type: pointer to the uint8_t, will hold type numeric id
738 * fit_image_get_type() finds type property in a given component image node.
739 * If the property is found, its (string) value is translated to the numeric
740 * id which is returned to the caller.
746 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
751 /* Get image type name from property data */
752 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
754 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
759 /* Translate image type name to id */
760 *type = genimg_get_type_id(data);
765 * fit_image_get_comp - get comp id for a given component image node
766 * @fit: pointer to the FIT format image header
767 * @noffset: component image node offset
768 * @comp: pointer to the uint8_t, will hold comp numeric id
770 * fit_image_get_comp() finds comp property in a given component image node.
771 * If the property is found, its (string) value is translated to the numeric
772 * id which is returned to the caller.
778 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
783 /* Get compression name from property data */
784 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
786 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
791 /* Translate compression name to id */
792 *comp = genimg_get_comp_id(data);
796 static int fit_image_get_address(const void *fit, int noffset, char *name,
803 cell = fdt_getprop(fit, noffset, name, &len);
805 fit_get_debug(fit, noffset, name, len);
810 /* Use load64 to avoid compiling warning for 32-bit target */
812 load64 = (load64 << 32) | uimage_to_cpu(*cell);
816 if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) {
817 printf("Unsupported %s address size\n", name);
821 *load = (ulong)load64;
826 * fit_image_get_load() - get load addr property for given component image node
827 * @fit: pointer to the FIT format image header
828 * @noffset: component image node offset
829 * @load: pointer to the uint32_t, will hold load address
831 * fit_image_get_load() finds load address property in a given component
832 * image node. If the property is found, its value is returned to the caller.
838 int fit_image_get_load(const void *fit, int noffset, ulong *load)
840 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
844 * fit_image_get_entry() - get entry point address property
845 * @fit: pointer to the FIT format image header
846 * @noffset: component image node offset
847 * @entry: pointer to the uint32_t, will hold entry point address
849 * This gets the entry point address property for a given component image
852 * fit_image_get_entry() finds entry point address property in a given
853 * component image node. If the property is found, its value is returned
860 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
862 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
866 * fit_image_get_data - get data property and its size for a given component image node
867 * @fit: pointer to the FIT format image header
868 * @noffset: component image node offset
869 * @data: double pointer to void, will hold data property's data address
870 * @size: pointer to size_t, will hold data property's data size
872 * fit_image_get_data() finds data property in a given component image node.
873 * If the property is found its data start address and size are returned to
880 int fit_image_get_data(const void *fit, int noffset,
881 const void **data, size_t *size)
885 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
887 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
897 * Get 'data-offset' property from a given image node.
899 * @fit: pointer to the FIT image header
900 * @noffset: component image node offset
901 * @data_offset: holds the data-offset property
905 * -ENOENT if the property could not be found
907 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
911 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
915 *data_offset = fdt32_to_cpu(*val);
921 * Get 'data-position' property from a given image node.
923 * @fit: pointer to the FIT image header
924 * @noffset: component image node offset
925 * @data_position: holds the data-position property
929 * -ENOENT if the property could not be found
931 int fit_image_get_data_position(const void *fit, int noffset,
936 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
940 *data_position = fdt32_to_cpu(*val);
946 * Get 'data-size' property from a given image node.
948 * @fit: pointer to the FIT image header
949 * @noffset: component image node offset
950 * @data_size: holds the data-size property
954 * -ENOENT if the property could not be found
956 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
960 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
964 *data_size = fdt32_to_cpu(*val);
970 * Get 'data-size-unciphered' property from a given image node.
972 * @fit: pointer to the FIT image header
973 * @noffset: component image node offset
974 * @data_size: holds the data-size property
978 * -ENOENT if the property could not be found
980 int fit_image_get_data_size_unciphered(const void *fit, int noffset,
985 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
989 *data_size = (size_t)fdt32_to_cpu(*val);
995 * fit_image_get_data_and_size - get data and its size including
996 * both embedded and external data
997 * @fit: pointer to the FIT format image header
998 * @noffset: component image node offset
999 * @data: double pointer to void, will hold data property's data address
1000 * @size: pointer to size_t, will hold data property's data size
1002 * fit_image_get_data_and_size() finds data and its size including
1003 * both embedded and external data. If the property is found
1004 * its data start address and size are returned to the caller.
1008 * otherwise, on failure
1010 int fit_image_get_data_and_size(const void *fit, int noffset,
1011 const void **data, size_t *size)
1013 bool external_data = false;
1018 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1019 external_data = true;
1020 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1021 external_data = true;
1023 * For FIT with external data, figure out where
1024 * the external images start. This is the base
1025 * for the data-offset properties in each image.
1027 offset += ((fdt_totalsize(fit) + 3) & ~3);
1030 if (external_data) {
1031 debug("External Data\n");
1032 ret = fit_image_get_data_size(fit, noffset, &len);
1034 *data = fit + offset;
1038 ret = fit_image_get_data(fit, noffset, data, size);
1045 * fit_image_hash_get_algo - get hash algorithm name
1046 * @fit: pointer to the FIT format image header
1047 * @noffset: hash node offset
1048 * @algo: double pointer to char, will hold pointer to the algorithm name
1050 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1051 * If the property is found its data start address is returned to the caller.
1057 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1061 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1062 if (*algo == NULL) {
1063 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1071 * fit_image_hash_get_value - get hash value and length
1072 * @fit: pointer to the FIT format image header
1073 * @noffset: hash node offset
1074 * @value: double pointer to uint8_t, will hold address of a hash value data
1075 * @value_len: pointer to an int, will hold hash data length
1077 * fit_image_hash_get_value() finds hash value property in a given hash node.
1078 * If the property is found its data start address and size are returned to
1085 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1090 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1091 if (*value == NULL) {
1092 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1102 * fit_image_hash_get_ignore - get hash ignore flag
1103 * @fit: pointer to the FIT format image header
1104 * @noffset: hash node offset
1105 * @ignore: pointer to an int, will hold hash ignore flag
1107 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1108 * If the property is found and non-zero, the hash algorithm is not verified by
1109 * u-boot automatically.
1112 * 0, on ignore not found
1113 * value, on ignore found
1115 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1120 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1121 if (value == NULL || len != sizeof(int))
1130 * fit_image_cipher_get_algo - get cipher algorithm name
1131 * @fit: pointer to the FIT format image header
1132 * @noffset: cipher node offset
1133 * @algo: double pointer to char, will hold pointer to the algorithm name
1135 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1136 * cipher node. If the property is found its data start address is returned
1143 int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1147 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1149 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1156 ulong fit_get_end(const void *fit)
1158 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1162 * fit_set_timestamp - set node timestamp property
1163 * @fit: pointer to the FIT format image header
1164 * @noffset: node offset
1165 * @timestamp: timestamp value to be set
1167 * fit_set_timestamp() attempts to set timestamp property in the requested
1168 * node and returns operation status to the caller.
1172 * -ENOSPC if no space in device tree, -1 for other error
1174 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1179 t = cpu_to_uimage(timestamp);
1180 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1183 debug("Can't set '%s' property for '%s' node (%s)\n",
1184 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1186 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1193 * calculate_hash - calculate and return hash for provided input data
1194 * @data: pointer to the input data
1195 * @data_len: data length
1196 * @algo: requested hash algorithm
1197 * @value: pointer to the char, will hold hash value data (caller must
1198 * allocate enough free space)
1199 * value_len: length of the calculated hash
1201 * calculate_hash() computes input data hash according to the requested
1203 * Resulting hash value is placed in caller provided 'value' buffer, length
1204 * of the calculated hash is returned via value_len pointer argument.
1208 * -1, when algo is unsupported
1210 int calculate_hash(const void *data, int data_len, const char *algo,
1211 uint8_t *value, int *value_len)
1213 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1214 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1216 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1218 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1219 sha1_csum_wd((unsigned char *)data, data_len,
1220 (unsigned char *)value, CHUNKSZ_SHA1);
1222 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1223 sha256_csum_wd((unsigned char *)data, data_len,
1224 (unsigned char *)value, CHUNKSZ_SHA256);
1225 *value_len = SHA256_SUM_LEN;
1226 } else if (IMAGE_ENABLE_SHA384 && strcmp(algo, "sha384") == 0) {
1227 sha384_csum_wd((unsigned char *)data, data_len,
1228 (unsigned char *)value, CHUNKSZ_SHA384);
1229 *value_len = SHA384_SUM_LEN;
1230 } else if (IMAGE_ENABLE_SHA512 && strcmp(algo, "sha512") == 0) {
1231 sha512_csum_wd((unsigned char *)data, data_len,
1232 (unsigned char *)value, CHUNKSZ_SHA512);
1233 *value_len = SHA512_SUM_LEN;
1234 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1235 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1238 debug("Unsupported hash alogrithm\n");
1244 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1245 size_t size, char **err_msgp)
1247 uint8_t value[FIT_MAX_HASH_LEN];
1256 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1257 *err_msgp = "Can't get hash algo property";
1262 if (IMAGE_ENABLE_IGNORE) {
1263 fit_image_hash_get_ignore(fit, noffset, &ignore);
1265 printf("-skipped ");
1270 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1272 *err_msgp = "Can't get hash value property";
1276 if (calculate_hash(data, size, algo, value, &value_len)) {
1277 *err_msgp = "Unsupported hash algorithm";
1281 if (value_len != fit_value_len) {
1282 *err_msgp = "Bad hash value len";
1284 } else if (memcmp(value, fit_value, value_len) != 0) {
1285 *err_msgp = "Bad hash value";
1292 int fit_image_verify_with_data(const void *fit, int image_noffset,
1293 const void *data, size_t size)
1300 /* Verify all required signatures */
1301 if (FIT_IMAGE_ENABLE_VERIFY &&
1302 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1303 gd_fdt_blob(), &verify_all)) {
1304 err_msg = "Unable to verify required signature";
1308 /* Process all hash subnodes of the component image node */
1309 fdt_for_each_subnode(noffset, fit, image_noffset) {
1310 const char *name = fit_get_name(fit, noffset, NULL);
1313 * Check subnode name, must be equal to "hash".
1314 * Multiple hash nodes require unique unit node
1315 * names, e.g. hash-1, hash-2, etc.
1317 if (!strncmp(name, FIT_HASH_NODENAME,
1318 strlen(FIT_HASH_NODENAME))) {
1319 if (fit_image_check_hash(fit, noffset, data, size,
1323 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
1324 !strncmp(name, FIT_SIG_NODENAME,
1325 strlen(FIT_SIG_NODENAME))) {
1326 ret = fit_image_check_sig(fit, noffset, data,
1327 size, -1, &err_msg);
1330 * Show an indication on failure, but do not return
1331 * an error. Only keys marked 'required' can cause
1332 * an image validation failure. See the call to
1333 * fit_image_verify_required_sigs() above.
1342 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1343 err_msg = "Corrupted or truncated tree";
1350 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1351 err_msg, fit_get_name(fit, noffset, NULL),
1352 fit_get_name(fit, image_noffset, NULL));
1357 * fit_image_verify - verify data integrity
1358 * @fit: pointer to the FIT format image header
1359 * @image_noffset: component image node offset
1361 * fit_image_verify() goes over component image hash nodes,
1362 * re-calculates each data hash and compares with the value stored in hash
1366 * 1, if all hashes are valid
1367 * 0, otherwise (or on error)
1369 int fit_image_verify(const void *fit, int image_noffset)
1376 /* Get image data and data length */
1377 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1378 err_msg = "Can't get image data/size";
1379 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1380 err_msg, fit_get_name(fit, noffset, NULL),
1381 fit_get_name(fit, image_noffset, NULL));
1385 return fit_image_verify_with_data(fit, image_noffset, data, size);
1389 * fit_all_image_verify - verify data integrity for all images
1390 * @fit: pointer to the FIT format image header
1392 * fit_all_image_verify() goes over all images in the FIT and
1393 * for every images checks if all it's hashes are valid.
1396 * 1, if all hashes of all images are valid
1397 * 0, otherwise (or on error)
1399 int fit_all_image_verify(const void *fit)
1406 /* Find images parent node offset */
1407 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1408 if (images_noffset < 0) {
1409 printf("Can't find images parent node '%s' (%s)\n",
1410 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1414 /* Process all image subnodes, check hashes for each */
1415 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1417 for (ndepth = 0, count = 0,
1418 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1419 (noffset >= 0) && (ndepth > 0);
1420 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1423 * Direct child node of the images parent node,
1424 * i.e. component image node.
1426 printf(" Hash(es) for Image %u (%s): ", count,
1427 fit_get_name(fit, noffset, NULL));
1430 if (!fit_image_verify(fit, noffset))
1438 static int fit_image_uncipher(const void *fit, int image_noffset,
1439 void **data, size_t *size)
1441 int cipher_noffset, ret;
1445 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1446 FIT_CIPHER_NODENAME);
1447 if (cipher_noffset < 0)
1450 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1451 *data, *size, &dst, &size_dst);
1463 * fit_image_check_os - check whether image node is of a given os type
1464 * @fit: pointer to the FIT format image header
1465 * @noffset: component image node offset
1466 * @os: requested image os
1468 * fit_image_check_os() reads image os property and compares its numeric
1469 * id with the requested os. Comparison result is returned to the caller.
1472 * 1 if image is of given os type
1473 * 0 otherwise (or on error)
1475 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1479 if (fit_image_get_os(fit, noffset, &image_os))
1481 return (os == image_os);
1485 * fit_image_check_arch - check whether image node is of a given arch
1486 * @fit: pointer to the FIT format image header
1487 * @noffset: component image node offset
1488 * @arch: requested imagearch
1490 * fit_image_check_arch() reads image arch property and compares its numeric
1491 * id with the requested arch. Comparison result is returned to the caller.
1494 * 1 if image is of given arch
1495 * 0 otherwise (or on error)
1497 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1500 int aarch32_support = 0;
1502 if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32))
1503 aarch32_support = 1;
1505 if (fit_image_get_arch(fit, noffset, &image_arch))
1507 return (arch == image_arch) ||
1508 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1509 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1514 * fit_image_check_type - check whether image node is of a given type
1515 * @fit: pointer to the FIT format image header
1516 * @noffset: component image node offset
1517 * @type: requested image type
1519 * fit_image_check_type() reads image type property and compares its numeric
1520 * id with the requested type. Comparison result is returned to the caller.
1523 * 1 if image is of given type
1524 * 0 otherwise (or on error)
1526 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1530 if (fit_image_get_type(fit, noffset, &image_type))
1532 return (type == image_type);
1536 * fit_image_check_comp - check whether image node uses given compression
1537 * @fit: pointer to the FIT format image header
1538 * @noffset: component image node offset
1539 * @comp: requested image compression type
1541 * fit_image_check_comp() reads image compression property and compares its
1542 * numeric id with the requested compression type. Comparison result is
1543 * returned to the caller.
1546 * 1 if image uses requested compression
1547 * 0 otherwise (or on error)
1549 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1553 if (fit_image_get_comp(fit, noffset, &image_comp))
1555 return (comp == image_comp);
1559 * fit_check_format - sanity check FIT image format
1560 * @fit: pointer to the FIT format image header
1562 * fit_check_format() runs a basic sanity FIT image verification.
1563 * Routine checks for mandatory properties, nodes, etc.
1569 int fit_check_format(const void *fit)
1571 /* A FIT image must be a valid FDT */
1572 if (fdt_check_header(fit)) {
1573 debug("Wrong FIT format: not a flattened device tree\n");
1577 /* mandatory / node 'description' property */
1578 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1579 debug("Wrong FIT format: no description\n");
1583 if (IMAGE_ENABLE_TIMESTAMP) {
1584 /* mandatory / node 'timestamp' property */
1585 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1586 debug("Wrong FIT format: no timestamp\n");
1591 /* mandatory subimages parent '/images' node */
1592 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1593 debug("Wrong FIT format: no images parent node\n");
1602 * fit_conf_find_compat
1603 * @fit: pointer to the FIT format image header
1604 * @fdt: pointer to the device tree to compare against
1606 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1607 * most compatible with the passed in device tree.
1616 * |-o configurations
1624 * |-compatible = "foo,bar", "bim,bam"
1627 * |-compatible = "foo,bar",
1630 * |-compatible = "bim,bam", "baz,biz"
1632 * Configuration 1 would be picked because the first string in U-Boot's
1633 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1634 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1636 * As an optimization, the compatible property from the FDT's root node can be
1637 * copied into the configuration node in the FIT image. This is required to
1638 * match configurations with compressed FDTs.
1641 * offset to the configuration to use if one was found
1644 int fit_conf_find_compat(const void *fit, const void *fdt)
1647 int noffset, confs_noffset, images_noffset;
1648 const void *fdt_compat;
1650 int best_match_offset = 0;
1651 int best_match_pos = 0;
1653 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1654 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1655 if (confs_noffset < 0 || images_noffset < 0) {
1656 debug("Can't find configurations or images nodes.\n");
1660 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1662 debug("Fdt for comparison has no \"compatible\" property.\n");
1667 * Loop over the configurations in the FIT image.
1669 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1670 (noffset >= 0) && (ndepth > 0);
1671 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1673 const char *kfdt_name;
1674 int kfdt_noffset, compat_noffset;
1675 const char *cur_fdt_compat;
1683 /* If there's a compat property in the config node, use that. */
1684 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1685 fdt = fit; /* search in FIT image */
1686 compat_noffset = noffset; /* search under config node */
1687 } else { /* Otherwise extract it from the kernel FDT. */
1688 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1690 debug("No fdt property found.\n");
1693 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1695 if (kfdt_noffset < 0) {
1696 debug("No image node named \"%s\" found.\n",
1701 if (!fit_image_check_comp(fit, kfdt_noffset,
1703 debug("Can't extract compat from \"%s\" "
1704 "(compressed)\n", kfdt_name);
1708 /* search in this config's kernel FDT */
1709 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1710 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1714 compat_noffset = 0; /* search kFDT under root node */
1717 len = fdt_compat_len;
1718 cur_fdt_compat = fdt_compat;
1720 * Look for a match for each U-Boot compatibility string in
1721 * turn in the compat string property.
1723 for (i = 0; len > 0 &&
1724 (!best_match_offset || best_match_pos > i); i++) {
1725 int cur_len = strlen(cur_fdt_compat) + 1;
1727 if (!fdt_node_check_compatible(fdt, compat_noffset,
1729 best_match_offset = noffset;
1734 cur_fdt_compat += cur_len;
1737 if (!best_match_offset) {
1738 debug("No match found.\n");
1742 return best_match_offset;
1745 int fit_conf_get_node(const void *fit, const char *conf_uname)
1747 int noffset, confs_noffset;
1750 char *conf_uname_copy = NULL;
1752 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1753 if (confs_noffset < 0) {
1754 debug("Can't find configurations parent node '%s' (%s)\n",
1755 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1756 return confs_noffset;
1759 if (conf_uname == NULL) {
1760 /* get configuration unit name from the default property */
1761 debug("No configuration specified, trying default...\n");
1762 if (!host_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) {
1763 noffset = fit_find_config_node(fit);
1766 conf_uname = fdt_get_name(fit, noffset, NULL);
1768 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1769 FIT_DEFAULT_PROP, &len);
1770 if (conf_uname == NULL) {
1771 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1776 debug("Found default configuration: '%s'\n", conf_uname);
1779 s = strchr(conf_uname, '#');
1781 len = s - conf_uname;
1782 conf_uname_copy = malloc(len + 1);
1783 if (!conf_uname_copy) {
1784 debug("Can't allocate uname copy: '%s'\n",
1788 memcpy(conf_uname_copy, conf_uname, len);
1789 conf_uname_copy[len] = '\0';
1790 conf_uname = conf_uname_copy;
1793 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1795 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1796 conf_uname, fdt_strerror(noffset));
1799 if (conf_uname_copy)
1800 free(conf_uname_copy);
1805 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1806 const char *prop_name)
1808 return fdt_stringlist_count(fit, noffset, prop_name);
1811 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1812 const char *prop_name, int index)
1817 /* get kernel image unit name from configuration kernel property */
1818 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1822 return fit_image_get_node(fit, uname);
1825 int fit_conf_get_prop_node(const void *fit, int noffset,
1826 const char *prop_name)
1828 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1831 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1833 fit_image_print(fit, rd_noffset, " ");
1836 puts(" Verifying Hash Integrity ... ");
1837 if (!fit_image_verify(fit, rd_noffset)) {
1838 puts("Bad Data Hash\n");
1847 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1854 debug("* %s: using config '%s' from image at 0x%08lx\n",
1855 prop_name, images->fit_uname_cfg, addr);
1857 /* Check whether configuration has this property defined */
1858 fit_hdr = map_sysmem(addr, 0);
1859 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1860 if (cfg_noffset < 0) {
1861 debug("* %s: no such config\n", prop_name);
1865 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1867 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1875 * fit_get_image_type_property() - get property name for IH_TYPE_...
1877 * @return the properly name where we expect to find the image in the
1880 static const char *fit_get_image_type_property(int type)
1883 * This is sort-of available in the uimage_type[] table in image.c
1884 * but we don't have access to the short name, and "fdt" is different
1885 * anyway. So let's just keep it here.
1888 case IH_TYPE_FLATDT:
1889 return FIT_FDT_PROP;
1890 case IH_TYPE_KERNEL:
1891 return FIT_KERNEL_PROP;
1892 case IH_TYPE_RAMDISK:
1893 return FIT_RAMDISK_PROP;
1894 case IH_TYPE_X86_SETUP:
1895 return FIT_SETUP_PROP;
1896 case IH_TYPE_LOADABLE:
1897 return FIT_LOADABLE_PROP;
1899 return FIT_FPGA_PROP;
1900 case IH_TYPE_STANDALONE:
1901 return FIT_STANDALONE_PROP;
1907 int fit_image_load(bootm_headers_t *images, ulong addr,
1908 const char **fit_unamep, const char **fit_uname_configp,
1909 int arch, int image_type, int bootstage_id,
1910 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1912 int cfg_noffset, noffset;
1913 const char *fit_uname;
1914 const char *fit_uname_config;
1915 const char *fit_base_uname_config;
1921 ulong load, load_end, data, len;
1926 const char *prop_name;
1929 fit = map_sysmem(addr, 0);
1930 fit_uname = fit_unamep ? *fit_unamep : NULL;
1931 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1932 fit_base_uname_config = NULL;
1933 prop_name = fit_get_image_type_property(image_type);
1934 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1936 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1937 if (!fit_check_format(fit)) {
1938 printf("Bad FIT %s image format!\n", prop_name);
1939 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1942 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1944 /* get FIT component image node offset */
1945 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1946 noffset = fit_image_get_node(fit, fit_uname);
1949 * no image node unit name, try to get config
1950 * node first. If config unit node name is NULL
1951 * fit_conf_get_node() will try to find default config node
1953 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1954 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1955 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1957 cfg_noffset = fit_conf_get_node(fit,
1960 if (cfg_noffset < 0) {
1961 puts("Could not find configuration node\n");
1962 bootstage_error(bootstage_id +
1963 BOOTSTAGE_SUB_NO_UNIT_NAME);
1967 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1968 printf(" Using '%s' configuration\n", fit_base_uname_config);
1969 /* Remember this config */
1970 if (image_type == IH_TYPE_KERNEL)
1971 images->fit_uname_cfg = fit_base_uname_config;
1973 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
1974 puts(" Verifying Hash Integrity ... ");
1975 if (fit_config_verify(fit, cfg_noffset)) {
1976 puts("Bad Data Hash\n");
1977 bootstage_error(bootstage_id +
1978 BOOTSTAGE_SUB_HASH);
1984 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1986 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1988 fit_uname = fit_get_name(fit, noffset, NULL);
1991 printf("Could not find subimage node type '%s'\n", prop_name);
1992 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1996 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1998 ret = fit_image_select(fit, noffset, images->verify);
2000 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2004 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2005 if (!host_build() && IS_ENABLED(CONFIG_SANDBOX)) {
2006 if (!fit_image_check_target_arch(fit, noffset)) {
2007 puts("Unsupported Architecture\n");
2008 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2014 fit_image_get_arch(fit, noffset, &os_arch);
2015 images->os.arch = os_arch;
2018 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2019 type_ok = fit_image_check_type(fit, noffset, image_type) ||
2020 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
2021 (image_type == IH_TYPE_KERNEL &&
2022 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
2024 os_ok = image_type == IH_TYPE_FLATDT ||
2025 image_type == IH_TYPE_FPGA ||
2026 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
2027 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
2028 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
2029 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
2030 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
2033 * If either of the checks fail, we should report an error, but
2034 * if the image type is coming from the "loadables" field, we
2035 * don't care what it is
2037 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
2038 fit_image_get_os(fit, noffset, &os);
2039 printf("No %s %s %s Image\n",
2040 genimg_get_os_name(os),
2041 genimg_get_arch_name(arch),
2042 genimg_get_type_name(image_type));
2043 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2047 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2049 /* get image data address and length */
2050 if (fit_image_get_data_and_size(fit, noffset,
2051 (const void **)&buf, &size)) {
2052 printf("Could not find %s subimage data!\n", prop_name);
2053 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2057 /* Decrypt data before uncompress/move */
2058 if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
2059 puts(" Decrypting Data ... ");
2060 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2067 /* perform any post-processing on the image data */
2068 if (!host_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
2069 board_fit_image_post_process(&buf, &size);
2073 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2075 data = map_to_sysmem(buf);
2077 if (load_op == FIT_LOAD_IGNORED) {
2079 } else if (fit_image_get_load(fit, noffset, &load)) {
2080 if (load_op == FIT_LOAD_REQUIRED) {
2081 printf("Can't get %s subimage load address!\n",
2083 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2086 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
2087 ulong image_start, image_end;
2090 * move image data to the load address,
2091 * make sure we don't overwrite initial image
2094 image_end = addr + fit_get_size(fit);
2096 load_end = load + len;
2097 if (image_type != IH_TYPE_KERNEL &&
2098 load < image_end && load_end > image_start) {
2099 printf("Error: %s overwritten\n", prop_name);
2103 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2104 prop_name, data, load);
2106 load = data; /* No load address specified */
2109 comp = IH_COMP_NONE;
2111 /* Kernel images get decompressed later in bootm_load_os(). */
2112 if (!fit_image_get_comp(fit, noffset, &comp) &&
2113 comp != IH_COMP_NONE &&
2114 !(image_type == IH_TYPE_KERNEL ||
2115 image_type == IH_TYPE_KERNEL_NOLOAD ||
2116 image_type == IH_TYPE_RAMDISK)) {
2117 ulong max_decomp_len = len * 20;
2119 loadbuf = malloc(max_decomp_len);
2120 load = map_to_sysmem(loadbuf);
2122 loadbuf = map_sysmem(load, max_decomp_len);
2124 if (image_decomp(comp, load, data, image_type,
2125 loadbuf, buf, len, max_decomp_len, &load_end)) {
2126 printf("Error decompressing %s\n", prop_name);
2130 len = load_end - load;
2131 } else if (load != data) {
2132 loadbuf = map_sysmem(load, len);
2133 memcpy(loadbuf, buf, len);
2136 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2137 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2138 " please fix your .its file!\n");
2140 /* verify that image data is a proper FDT blob */
2141 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2142 puts("Subimage data is not a FDT");
2146 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2151 *fit_unamep = (char *)fit_uname;
2152 if (fit_uname_configp)
2153 *fit_uname_configp = (char *)(fit_uname_config ? :
2154 fit_base_uname_config);
2159 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2160 ulong *setup_start, ulong *setup_len)
2167 addr = map_to_sysmem(images->fit_hdr_os);
2168 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2172 ret = fit_image_load(images, addr, NULL, NULL, arch,
2173 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2174 FIT_LOAD_REQUIRED, setup_start, &len);
2180 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2181 const char **fit_unamep, const char **fit_uname_configp,
2182 int arch, ulong *datap, ulong *lenp)
2184 int fdt_noffset, cfg_noffset, count;
2186 const char *fit_uname = NULL;
2187 const char *fit_uname_config = NULL;
2188 char *fit_uname_config_copy = NULL;
2189 char *next_config = NULL;
2191 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2192 ulong image_start, image_end;
2193 ulong ovload, ovlen;
2194 const char *uconfig;
2197 int i, err, noffset, ov_noffset;
2200 fit_uname = fit_unamep ? *fit_unamep : NULL;
2202 if (fit_uname_configp && *fit_uname_configp) {
2203 fit_uname_config_copy = strdup(*fit_uname_configp);
2204 if (!fit_uname_config_copy)
2207 next_config = strchr(fit_uname_config_copy, '#');
2209 *next_config++ = '\0';
2210 if (next_config - 1 > fit_uname_config_copy)
2211 fit_uname_config = fit_uname_config_copy;
2214 fdt_noffset = fit_image_load(images,
2215 addr, &fit_uname, &fit_uname_config,
2216 arch, IH_TYPE_FLATDT,
2217 BOOTSTAGE_ID_FIT_FDT_START,
2218 FIT_LOAD_OPTIONAL, &load, &len);
2220 if (fdt_noffset < 0)
2223 debug("fit_uname=%s, fit_uname_config=%s\n",
2224 fit_uname ? fit_uname : "<NULL>",
2225 fit_uname_config ? fit_uname_config : "<NULL>");
2227 fit = map_sysmem(addr, 0);
2229 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2231 /* single blob, or error just return as well */
2232 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2233 if (count <= 1 && !next_config)
2236 /* we need to apply overlays */
2238 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2240 image_end = addr + fit_get_size(fit);
2241 /* verify that relocation took place by load address not being in fit */
2242 if (load >= image_start && load < image_end) {
2243 /* check is simplified; fit load checks for overlaps */
2244 printf("Overlayed FDT requires relocation\n");
2245 fdt_noffset = -EBADF;
2249 base = map_sysmem(load, len);
2251 /* apply extra configs in FIT first, followed by args */
2252 for (i = 1; ; i++) {
2254 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2256 uname = fit_get_name(fit, noffset, NULL);
2261 uconfig = next_config;
2262 next_config = strchr(next_config, '#');
2264 *next_config++ = '\0';
2268 * fit_image_load() would load the first FDT from the
2269 * extra config only when uconfig is specified.
2270 * Check if the extra config contains multiple FDTs and
2273 cfg_noffset = fit_conf_get_node(fit, uconfig);
2276 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2280 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2282 ov_noffset = fit_image_load(images,
2283 addr, &uname, &uconfig,
2284 arch, IH_TYPE_FLATDT,
2285 BOOTSTAGE_ID_FIT_FDT_START,
2286 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2287 if (ov_noffset < 0) {
2288 printf("load of %s failed\n", uname);
2291 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2292 uname, ovload, ovlen);
2293 ov = map_sysmem(ovload, ovlen);
2295 base = map_sysmem(load, len + ovlen);
2296 err = fdt_open_into(base, base, len + ovlen);
2298 printf("failed on fdt_open_into\n");
2302 /* the verbose method prints out messages on error */
2303 err = fdt_overlay_apply_verbose(base, ov);
2309 len = fdt_totalsize(base);
2312 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2313 fdt_noffset = -EBADF;
2322 *fit_unamep = fit_uname;
2323 if (fit_uname_configp)
2324 *fit_uname_configp = fit_uname_config;
2326 if (fit_uname_config_copy)
2327 free(fit_uname_config_copy);