1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
5 * (C) Copyright 2008 Semihalf
7 * (C) Copyright 2000-2006
11 #define LOG_CATEGORY LOGC_BOOT
16 #include <linux/libfdt.h>
17 #include <u-boot/crc.h>
19 #include <linux/compiler.h>
20 #include <linux/sizes.h>
28 #include <asm/global_data.h>
31 #include <u-boot/hash.h>
33 DECLARE_GLOBAL_DATA_PTR;
34 #endif /* !USE_HOSTCC*/
38 #include <bootstage.h>
39 #include <linux/kconfig.h>
40 #include <u-boot/crc.h>
41 #include <u-boot/md5.h>
42 #include <u-boot/sha1.h>
43 #include <u-boot/sha256.h>
44 #include <u-boot/sha512.h>
46 /*****************************************************************************/
47 /* New uImage format routines */
48 /*****************************************************************************/
50 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
51 ulong *addr, const char **name)
58 sep = strchr(spec, sepc);
61 *addr = hextoul(spec, NULL);
71 * fit_parse_conf - parse FIT configuration spec
72 * @spec: input string, containing configuration spec
73 * @add_curr: current image address (to be used as a possible default)
74 * @addr: pointer to a ulong variable, will hold FIT image address of a given
76 * @conf_name double pointer to a char, will hold pointer to a configuration
79 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
80 * where <addr> is a FIT image address that contains configuration
81 * with a <conf> unit name.
83 * Address part is optional, and if omitted default add_curr will
87 * 1 if spec is a valid configuration string,
88 * addr and conf_name are set accordingly
91 int fit_parse_conf(const char *spec, ulong addr_curr,
92 ulong *addr, const char **conf_name)
94 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
98 * fit_parse_subimage - parse FIT subimage spec
99 * @spec: input string, containing subimage spec
100 * @add_curr: current image address (to be used as a possible default)
101 * @addr: pointer to a ulong variable, will hold FIT image address of a given
103 * @image_name: double pointer to a char, will hold pointer to a subimage name
105 * fit_parse_subimage() expects subimage spec in the form of
106 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
107 * subimage with a <subimg> unit name.
109 * Address part is optional, and if omitted default add_curr will
113 * 1 if spec is a valid subimage string,
114 * addr and image_name are set accordingly
117 int fit_parse_subimage(const char *spec, ulong addr_curr,
118 ulong *addr, const char **image_name)
120 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
122 #endif /* !USE_HOSTCC */
125 /* Host tools use these implementations for Cipher and Signature support */
126 static void *host_blob;
128 void image_set_host_blob(void *blob)
133 void *image_get_host_blob(void)
137 #endif /* USE_HOSTCC */
139 static void fit_get_debug(const void *fit, int noffset,
140 char *prop_name, int err)
142 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
143 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
148 * fit_get_subimage_count - get component (sub-image) count
149 * @fit: pointer to the FIT format image header
150 * @images_noffset: offset of images node
153 * number of image components
155 int fit_get_subimage_count(const void *fit, int images_noffset)
161 /* Process its subnodes, print out component images details */
162 for (ndepth = 0, count = 0,
163 noffset = fdt_next_node(fit, images_noffset, &ndepth);
164 (noffset >= 0) && (ndepth > 0);
165 noffset = fdt_next_node(fit, noffset, &ndepth)) {
175 * fit_image_print_data() - prints out the hash node details
176 * @fit: pointer to the FIT format image header
177 * @noffset: offset of the hash node
178 * @p: pointer to prefix string
179 * @type: Type of information to print ("hash" or "sign")
181 * fit_image_print_data() lists properties for the processed hash node
183 * This function avoid using puts() since it prints a newline on the host
184 * but does not in U-Boot.
187 * no returned results
189 static void fit_image_print_data(const void *fit, int noffset, const char *p,
200 debug("%s %s node: '%s'\n", p, type,
201 fit_get_name(fit, noffset, NULL));
202 printf("%s %s algo: ", p, type);
203 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
204 printf("invalid/unsupported\n");
208 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
209 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
211 printf(":%s", keyname);
213 printf(" (required)");
216 padding = fdt_getprop(fit, noffset, "padding", NULL);
218 printf("%s %s padding: %s\n", p, type, padding);
220 ret = fit_image_hash_get_value(fit, noffset, &value,
222 printf("%s %s value: ", p, type);
224 printf("unavailable\n");
226 for (i = 0; i < value_len; i++)
227 printf("%02x", value[i]);
231 debug("%s %s len: %d\n", p, type, value_len);
233 /* Signatures have a time stamp */
234 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
237 printf("%s Timestamp: ", p);
238 if (fit_get_timestamp(fit, noffset, ×tamp))
239 printf("unavailable\n");
241 genimg_print_time(timestamp);
246 * fit_image_print_verification_data() - prints out the hash/signature details
247 * @fit: pointer to the FIT format image header
248 * @noffset: offset of the hash or signature node
249 * @p: pointer to prefix string
251 * This lists properties for the processed hash node
254 * no returned results
256 static void fit_image_print_verification_data(const void *fit, int noffset,
262 * Check subnode name, must be equal to "hash" or "signature".
263 * Multiple hash/signature nodes require unique unit node
264 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
266 name = fit_get_name(fit, noffset, NULL);
267 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
268 fit_image_print_data(fit, noffset, p, "Hash");
269 } else if (!strncmp(name, FIT_SIG_NODENAME,
270 strlen(FIT_SIG_NODENAME))) {
271 fit_image_print_data(fit, noffset, p, "Sign");
276 * fit_conf_print - prints out the FIT configuration details
277 * @fit: pointer to the FIT format image header
278 * @noffset: offset of the configuration node
279 * @p: pointer to prefix string
281 * fit_conf_print() lists all mandatory properties for the processed
282 * configuration node.
285 * no returned results
287 static void fit_conf_print(const void *fit, int noffset, const char *p)
292 int fdt_index, loadables_index;
295 /* Mandatory properties */
296 ret = fit_get_desc(fit, noffset, &desc);
297 printf("%s Description: ", p);
299 printf("unavailable\n");
301 printf("%s\n", desc);
303 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
304 printf("%s Kernel: ", p);
306 printf("unavailable\n");
308 printf("%s\n", uname);
310 /* Optional properties */
311 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
313 printf("%s Init Ramdisk: %s\n", p, uname);
315 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
317 printf("%s Firmware: %s\n", p, uname);
320 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
321 fdt_index, NULL), uname;
324 printf("%s FDT: ", p);
327 printf("%s\n", uname);
330 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
332 printf("%s FPGA: %s\n", p, uname);
334 /* Print out all of the specified loadables */
335 for (loadables_index = 0;
336 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
337 loadables_index, NULL), uname;
339 if (loadables_index == 0) {
340 printf("%s Loadables: ", p);
344 printf("%s\n", uname);
347 /* Process all hash subnodes of the component configuration node */
348 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
349 (noffset >= 0) && (ndepth > 0);
350 noffset = fdt_next_node(fit, noffset, &ndepth)) {
352 /* Direct child node of the component configuration node */
353 fit_image_print_verification_data(fit, noffset, p);
359 * fit_print_contents - prints out the contents of the FIT format image
360 * @fit: pointer to the FIT format image header
361 * @p: pointer to prefix string
363 * fit_print_contents() formats a multi line FIT image contents description.
364 * The routine prints out FIT image properties (root node level) followed by
365 * the details of each component image.
368 * no returned results
370 void fit_print_contents(const void *fit)
383 if (!CONFIG_IS_ENABLED(FIT_PRINT))
386 /* Indent string is defined in header image.h */
387 p = IMAGE_INDENT_STRING;
389 /* Root node properties */
390 ret = fit_get_desc(fit, 0, &desc);
391 printf("%sFIT description: ", p);
393 printf("unavailable\n");
395 printf("%s\n", desc);
397 if (IMAGE_ENABLE_TIMESTAMP) {
398 ret = fit_get_timestamp(fit, 0, ×tamp);
399 printf("%sCreated: ", p);
401 printf("unavailable\n");
403 genimg_print_time(timestamp);
406 /* Find images parent node offset */
407 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
408 if (images_noffset < 0) {
409 printf("Can't find images parent node '%s' (%s)\n",
410 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
414 /* Process its subnodes, print out component images details */
415 for (ndepth = 0, count = 0,
416 noffset = fdt_next_node(fit, images_noffset, &ndepth);
417 (noffset >= 0) && (ndepth > 0);
418 noffset = fdt_next_node(fit, noffset, &ndepth)) {
421 * Direct child node of the images parent node,
422 * i.e. component image node.
424 printf("%s Image %u (%s)\n", p, count++,
425 fit_get_name(fit, noffset, NULL));
427 fit_image_print(fit, noffset, p);
431 /* Find configurations parent node offset */
432 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
433 if (confs_noffset < 0) {
434 debug("Can't get configurations parent node '%s' (%s)\n",
435 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
439 /* get default configuration unit name from default property */
440 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
442 printf("%s Default Configuration: '%s'\n", p, uname);
444 /* Process its subnodes, print out configurations details */
445 for (ndepth = 0, count = 0,
446 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
447 (noffset >= 0) && (ndepth > 0);
448 noffset = fdt_next_node(fit, noffset, &ndepth)) {
451 * Direct child node of the configurations parent node,
452 * i.e. configuration node.
454 printf("%s Configuration %u (%s)\n", p, count++,
455 fit_get_name(fit, noffset, NULL));
457 fit_conf_print(fit, noffset, p);
463 * fit_image_print - prints out the FIT component image details
464 * @fit: pointer to the FIT format image header
465 * @image_noffset: offset of the component image node
466 * @p: pointer to prefix string
468 * fit_image_print() lists all mandatory properties for the processed component
469 * image. If present, hash nodes are printed out as well. Load
470 * address for images of type firmware is also printed out. Since the load
471 * address is not mandatory for firmware images, it will be output as
472 * "unavailable" when not present.
475 * no returned results
477 void fit_image_print(const void *fit, int image_noffset, const char *p)
480 uint8_t type, arch, os, comp = IH_COMP_NONE;
488 if (!CONFIG_IS_ENABLED(FIT_PRINT))
491 /* Mandatory properties */
492 ret = fit_get_desc(fit, image_noffset, &desc);
493 printf("%s Description: ", p);
495 printf("unavailable\n");
497 printf("%s\n", desc);
499 if (IMAGE_ENABLE_TIMESTAMP) {
502 ret = fit_get_timestamp(fit, 0, ×tamp);
503 printf("%s Created: ", p);
505 printf("unavailable\n");
507 genimg_print_time(timestamp);
510 fit_image_get_type(fit, image_noffset, &type);
511 printf("%s Type: %s\n", p, genimg_get_type_name(type));
513 fit_image_get_comp(fit, image_noffset, &comp);
514 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
516 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
518 if (!tools_build()) {
519 printf("%s Data Start: ", p);
521 printf("unavailable\n");
523 void *vdata = (void *)data;
525 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
529 printf("%s Data Size: ", p);
531 printf("unavailable\n");
533 genimg_print_size(size);
535 /* Remaining, type dependent properties */
536 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
537 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
538 (type == IH_TYPE_FLATDT)) {
539 fit_image_get_arch(fit, image_noffset, &arch);
540 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
543 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
544 (type == IH_TYPE_FIRMWARE)) {
545 fit_image_get_os(fit, image_noffset, &os);
546 printf("%s OS: %s\n", p, genimg_get_os_name(os));
549 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
550 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
551 (type == IH_TYPE_FPGA)) {
552 ret = fit_image_get_load(fit, image_noffset, &load);
553 printf("%s Load Address: ", p);
555 printf("unavailable\n");
557 printf("0x%08lx\n", load);
560 /* optional load address for FDT */
561 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
562 printf("%s Load Address: 0x%08lx\n", p, load);
564 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
565 (type == IH_TYPE_RAMDISK)) {
566 ret = fit_image_get_entry(fit, image_noffset, &entry);
567 printf("%s Entry Point: ", p);
569 printf("unavailable\n");
571 printf("0x%08lx\n", entry);
574 /* Process all hash subnodes of the component image node */
575 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
576 (noffset >= 0) && (ndepth > 0);
577 noffset = fdt_next_node(fit, noffset, &ndepth)) {
579 /* Direct child node of the component image node */
580 fit_image_print_verification_data(fit, noffset, p);
586 * fit_get_desc - get node description property
587 * @fit: pointer to the FIT format image header
588 * @noffset: node offset
589 * @desc: double pointer to the char, will hold pointer to the description
591 * fit_get_desc() reads description property from a given node, if
592 * description is found pointer to it is returned in third call argument.
598 int fit_get_desc(const void *fit, int noffset, char **desc)
602 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
604 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
612 * fit_get_timestamp - get node timestamp property
613 * @fit: pointer to the FIT format image header
614 * @noffset: node offset
615 * @timestamp: pointer to the time_t, will hold read timestamp
617 * fit_get_timestamp() reads timestamp property from given node, if timestamp
618 * is found and has a correct size its value is returned in third call
623 * -1, on property read failure
624 * -2, on wrong timestamp size
626 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
631 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
633 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
636 if (len != sizeof(uint32_t)) {
637 debug("FIT timestamp with incorrect size of (%u)\n", len);
641 *timestamp = uimage_to_cpu(*((uint32_t *)data));
646 * fit_image_get_node - get node offset for component image of a given unit name
647 * @fit: pointer to the FIT format image header
648 * @image_uname: component image node unit name
650 * fit_image_get_node() finds a component image (within the '/images'
651 * node) of a provided unit name. If image is found its node offset is
652 * returned to the caller.
655 * image node offset when found (>=0)
656 * negative number on failure (FDT_ERR_* code)
658 int fit_image_get_node(const void *fit, const char *image_uname)
660 int noffset, images_noffset;
662 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
663 if (images_noffset < 0) {
664 debug("Can't find images parent node '%s' (%s)\n",
665 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
666 return images_noffset;
669 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
671 debug("Can't get node offset for image unit name: '%s' (%s)\n",
672 image_uname, fdt_strerror(noffset));
679 * fit_image_get_os - get os id for a given component image node
680 * @fit: pointer to the FIT format image header
681 * @noffset: component image node offset
682 * @os: pointer to the uint8_t, will hold os numeric id
684 * fit_image_get_os() finds os property in a given component image node.
685 * If the property is found, its (string) value is translated to the numeric
686 * id which is returned to the caller.
692 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
697 /* Get OS name from property data */
698 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
700 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
705 /* Translate OS name to id */
706 *os = genimg_get_os_id(data);
711 * fit_image_get_arch - get arch id for a given component image node
712 * @fit: pointer to the FIT format image header
713 * @noffset: component image node offset
714 * @arch: pointer to the uint8_t, will hold arch numeric id
716 * fit_image_get_arch() finds arch property in a given component image node.
717 * If the property is found, its (string) value is translated to the numeric
718 * id which is returned to the caller.
724 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
729 /* Get architecture name from property data */
730 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
732 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
737 /* Translate architecture name to id */
738 *arch = genimg_get_arch_id(data);
743 * fit_image_get_type - get type id for a given component image node
744 * @fit: pointer to the FIT format image header
745 * @noffset: component image node offset
746 * @type: pointer to the uint8_t, will hold type numeric id
748 * fit_image_get_type() finds type property in a given component image node.
749 * If the property is found, its (string) value is translated to the numeric
750 * id which is returned to the caller.
756 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
761 /* Get image type name from property data */
762 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
764 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
769 /* Translate image type name to id */
770 *type = genimg_get_type_id(data);
775 * fit_image_get_comp - get comp id for a given component image node
776 * @fit: pointer to the FIT format image header
777 * @noffset: component image node offset
778 * @comp: pointer to the uint8_t, will hold comp numeric id
780 * fit_image_get_comp() finds comp property in a given component image node.
781 * If the property is found, its (string) value is translated to the numeric
782 * id which is returned to the caller.
788 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
793 /* Get compression name from property data */
794 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
796 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
800 /* Translate compression name to id */
801 *comp = genimg_get_comp_id(data);
806 * fit_image_get_phase() - get the phase for a configuration node
807 * @fit: pointer to the FIT format image header
808 * @offset: configuration-node offset
809 * @phasep: returns the phase
811 * Finds the phase property in a given configuration node. If the property is
812 * found, its (string) value is translated to the numeric id which is returned
815 * Returns: 0 on success, -ENOENT if missing, -EINVAL for invalid value
817 int fit_image_get_phase(const void *fit, int offset, enum image_phase_t *phasep)
822 /* Get phase name from property data */
823 data = fdt_getprop(fit, offset, FIT_PHASE_PROP, &len);
825 fit_get_debug(fit, offset, FIT_PHASE_PROP, len);
830 /* Translate phase name to id */
831 ret = genimg_get_phase_id(data);
839 static int fit_image_get_address(const void *fit, int noffset, char *name,
846 cell = fdt_getprop(fit, noffset, name, &len);
848 fit_get_debug(fit, noffset, name, len);
853 /* Use load64 to avoid compiling warning for 32-bit target */
855 load64 = (load64 << 32) | uimage_to_cpu(*cell);
859 if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) {
860 printf("Unsupported %s address size\n", name);
864 *load = (ulong)load64;
869 * fit_image_get_load() - get load addr property for given component image node
870 * @fit: pointer to the FIT format image header
871 * @noffset: component image node offset
872 * @load: pointer to the uint32_t, will hold load address
874 * fit_image_get_load() finds load address property in a given component
875 * image node. If the property is found, its value is returned to the caller.
881 int fit_image_get_load(const void *fit, int noffset, ulong *load)
883 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
887 * fit_image_get_entry() - get entry point address property
888 * @fit: pointer to the FIT format image header
889 * @noffset: component image node offset
890 * @entry: pointer to the uint32_t, will hold entry point address
892 * This gets the entry point address property for a given component image
895 * fit_image_get_entry() finds entry point address property in a given
896 * component image node. If the property is found, its value is returned
903 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
905 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
909 * fit_image_get_data - get data property and its size for a given component image node
910 * @fit: pointer to the FIT format image header
911 * @noffset: component image node offset
912 * @data: double pointer to void, will hold data property's data address
913 * @size: pointer to size_t, will hold data property's data size
915 * fit_image_get_data() finds data property in a given component image node.
916 * If the property is found its data start address and size are returned to
923 int fit_image_get_data(const void *fit, int noffset,
924 const void **data, size_t *size)
928 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
930 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
940 * Get 'data-offset' property from a given image node.
942 * @fit: pointer to the FIT image header
943 * @noffset: component image node offset
944 * @data_offset: holds the data-offset property
948 * -ENOENT if the property could not be found
950 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
954 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
958 *data_offset = fdt32_to_cpu(*val);
964 * Get 'data-position' property from a given image node.
966 * @fit: pointer to the FIT image header
967 * @noffset: component image node offset
968 * @data_position: holds the data-position property
972 * -ENOENT if the property could not be found
974 int fit_image_get_data_position(const void *fit, int noffset,
979 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
983 *data_position = fdt32_to_cpu(*val);
989 * Get 'data-size' property from a given image node.
991 * @fit: pointer to the FIT image header
992 * @noffset: component image node offset
993 * @data_size: holds the data-size property
997 * -ENOENT if the property could not be found
999 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
1003 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
1007 *data_size = fdt32_to_cpu(*val);
1013 * Get 'data-size-unciphered' property from a given image node.
1015 * @fit: pointer to the FIT image header
1016 * @noffset: component image node offset
1017 * @data_size: holds the data-size property
1021 * -ENOENT if the property could not be found
1023 int fit_image_get_data_size_unciphered(const void *fit, int noffset,
1028 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
1032 *data_size = (size_t)fdt32_to_cpu(*val);
1038 * fit_image_get_data_and_size - get data and its size including
1039 * both embedded and external data
1040 * @fit: pointer to the FIT format image header
1041 * @noffset: component image node offset
1042 * @data: double pointer to void, will hold data property's data address
1043 * @size: pointer to size_t, will hold data property's data size
1045 * fit_image_get_data_and_size() finds data and its size including
1046 * both embedded and external data. If the property is found
1047 * its data start address and size are returned to the caller.
1051 * otherwise, on failure
1053 int fit_image_get_data_and_size(const void *fit, int noffset,
1054 const void **data, size_t *size)
1056 bool external_data = false;
1061 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1062 external_data = true;
1063 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1064 external_data = true;
1066 * For FIT with external data, figure out where
1067 * the external images start. This is the base
1068 * for the data-offset properties in each image.
1070 offset += ((fdt_totalsize(fit) + 3) & ~3);
1073 if (external_data) {
1074 debug("External Data\n");
1075 ret = fit_image_get_data_size(fit, noffset, &len);
1077 *data = fit + offset;
1081 ret = fit_image_get_data(fit, noffset, data, size);
1088 * fit_image_hash_get_algo - get hash algorithm name
1089 * @fit: pointer to the FIT format image header
1090 * @noffset: hash node offset
1091 * @algo: double pointer to char, will hold pointer to the algorithm name
1093 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1094 * If the property is found its data start address is returned to the caller.
1100 int fit_image_hash_get_algo(const void *fit, int noffset, const char **algo)
1104 *algo = (const char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1105 if (*algo == NULL) {
1106 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1114 * fit_image_hash_get_value - get hash value and length
1115 * @fit: pointer to the FIT format image header
1116 * @noffset: hash node offset
1117 * @value: double pointer to uint8_t, will hold address of a hash value data
1118 * @value_len: pointer to an int, will hold hash data length
1120 * fit_image_hash_get_value() finds hash value property in a given hash node.
1121 * If the property is found its data start address and size are returned to
1128 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1133 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1134 if (*value == NULL) {
1135 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1145 * fit_image_hash_get_ignore - get hash ignore flag
1146 * @fit: pointer to the FIT format image header
1147 * @noffset: hash node offset
1148 * @ignore: pointer to an int, will hold hash ignore flag
1150 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1151 * If the property is found and non-zero, the hash algorithm is not verified by
1152 * u-boot automatically.
1155 * 0, on ignore not found
1156 * value, on ignore found
1158 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1163 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1164 if (value == NULL || len != sizeof(int))
1173 * fit_image_cipher_get_algo - get cipher algorithm name
1174 * @fit: pointer to the FIT format image header
1175 * @noffset: cipher node offset
1176 * @algo: double pointer to char, will hold pointer to the algorithm name
1178 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1179 * cipher node. If the property is found its data start address is returned
1186 int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1190 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1192 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1199 ulong fit_get_end(const void *fit)
1201 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1205 * fit_set_timestamp - set node timestamp property
1206 * @fit: pointer to the FIT format image header
1207 * @noffset: node offset
1208 * @timestamp: timestamp value to be set
1210 * fit_set_timestamp() attempts to set timestamp property in the requested
1211 * node and returns operation status to the caller.
1215 * -ENOSPC if no space in device tree, -1 for other error
1217 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1222 t = cpu_to_uimage(timestamp);
1223 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1226 debug("Can't set '%s' property for '%s' node (%s)\n",
1227 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1229 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1236 * calculate_hash - calculate and return hash for provided input data
1237 * @data: pointer to the input data
1238 * @data_len: data length
1239 * @name: requested hash algorithm name
1240 * @value: pointer to the char, will hold hash value data (caller must
1241 * allocate enough free space)
1242 * value_len: length of the calculated hash
1244 * calculate_hash() computes input data hash according to the requested
1246 * Resulting hash value is placed in caller provided 'value' buffer, length
1247 * of the calculated hash is returned via value_len pointer argument.
1251 * -1, when algo is unsupported
1253 int calculate_hash(const void *data, int data_len, const char *name,
1254 uint8_t *value, int *value_len)
1256 #if !defined(USE_HOSTCC) && defined(CONFIG_DM_HASH)
1258 enum HASH_ALGO hash_algo;
1259 struct udevice *dev;
1261 rc = uclass_get_device(UCLASS_HASH, 0, &dev);
1263 debug("failed to get hash device, rc=%d\n", rc);
1267 hash_algo = hash_algo_lookup_by_name(name);
1268 if (hash_algo == HASH_ALGO_INVALID) {
1269 debug("Unsupported hash algorithm\n");
1273 rc = hash_digest_wd(dev, hash_algo, data, data_len, value, CHUNKSZ);
1275 debug("failed to get hash value, rc=%d\n", rc);
1279 *value_len = hash_algo_digest_size(hash_algo);
1281 struct hash_algo *algo;
1284 ret = hash_lookup_algo(name, &algo);
1286 debug("Unsupported hash alogrithm\n");
1290 algo->hash_func_ws(data, data_len, value, algo->chunk_size);
1291 *value_len = algo->digest_size;
1297 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1298 size_t size, char **err_msgp)
1300 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, value, FIT_MAX_HASH_LEN);
1309 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1310 *err_msgp = "Can't get hash algo property";
1315 if (!tools_build()) {
1316 fit_image_hash_get_ignore(fit, noffset, &ignore);
1318 printf("-skipped ");
1323 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1325 *err_msgp = "Can't get hash value property";
1329 if (calculate_hash(data, size, algo, value, &value_len)) {
1330 *err_msgp = "Unsupported hash algorithm";
1334 if (value_len != fit_value_len) {
1335 *err_msgp = "Bad hash value len";
1337 } else if (memcmp(value, fit_value, value_len) != 0) {
1338 *err_msgp = "Bad hash value";
1345 int fit_image_verify_with_data(const void *fit, int image_noffset,
1346 const void *key_blob, const void *data,
1354 /* Verify all required signatures */
1355 if (FIT_IMAGE_ENABLE_VERIFY &&
1356 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1357 key_blob, &verify_all)) {
1358 err_msg = "Unable to verify required signature";
1362 /* Process all hash subnodes of the component image node */
1363 fdt_for_each_subnode(noffset, fit, image_noffset) {
1364 const char *name = fit_get_name(fit, noffset, NULL);
1367 * Check subnode name, must be equal to "hash".
1368 * Multiple hash nodes require unique unit node
1369 * names, e.g. hash-1, hash-2, etc.
1371 if (!strncmp(name, FIT_HASH_NODENAME,
1372 strlen(FIT_HASH_NODENAME))) {
1373 if (fit_image_check_hash(fit, noffset, data, size,
1377 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
1378 !strncmp(name, FIT_SIG_NODENAME,
1379 strlen(FIT_SIG_NODENAME))) {
1380 ret = fit_image_check_sig(fit, noffset, data, size,
1381 gd_fdt_blob(), -1, &err_msg);
1384 * Show an indication on failure, but do not return
1385 * an error. Only keys marked 'required' can cause
1386 * an image validation failure. See the call to
1387 * fit_image_verify_required_sigs() above.
1396 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1397 err_msg = "Corrupted or truncated tree";
1404 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1405 err_msg, fit_get_name(fit, noffset, NULL),
1406 fit_get_name(fit, image_noffset, NULL));
1411 * fit_image_verify - verify data integrity
1412 * @fit: pointer to the FIT format image header
1413 * @image_noffset: component image node offset
1415 * fit_image_verify() goes over component image hash nodes,
1416 * re-calculates each data hash and compares with the value stored in hash
1420 * 1, if all hashes are valid
1421 * 0, otherwise (or on error)
1423 int fit_image_verify(const void *fit, int image_noffset)
1425 const char *name = fit_get_name(fit, image_noffset, NULL);
1430 if (IS_ENABLED(CONFIG_FIT_SIGNATURE) && strchr(name, '@')) {
1432 * We don't support this since libfdt considers names with the
1433 * name root but different @ suffix to be equal
1435 err_msg = "Node name contains @";
1438 /* Get image data and data length */
1439 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1440 err_msg = "Can't get image data/size";
1444 return fit_image_verify_with_data(fit, image_noffset, gd_fdt_blob(),
1448 printf("error!\n%s in '%s' image node\n", err_msg,
1449 fit_get_name(fit, image_noffset, NULL));
1454 * fit_all_image_verify - verify data integrity for all images
1455 * @fit: pointer to the FIT format image header
1457 * fit_all_image_verify() goes over all images in the FIT and
1458 * for every images checks if all it's hashes are valid.
1461 * 1, if all hashes of all images are valid
1462 * 0, otherwise (or on error)
1464 int fit_all_image_verify(const void *fit)
1471 /* Find images parent node offset */
1472 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1473 if (images_noffset < 0) {
1474 printf("Can't find images parent node '%s' (%s)\n",
1475 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1479 /* Process all image subnodes, check hashes for each */
1480 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1482 for (ndepth = 0, count = 0,
1483 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1484 (noffset >= 0) && (ndepth > 0);
1485 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1488 * Direct child node of the images parent node,
1489 * i.e. component image node.
1491 printf(" Hash(es) for Image %u (%s): ", count,
1492 fit_get_name(fit, noffset, NULL));
1495 if (!fit_image_verify(fit, noffset))
1503 static int fit_image_uncipher(const void *fit, int image_noffset,
1504 void **data, size_t *size)
1506 int cipher_noffset, ret;
1510 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1511 FIT_CIPHER_NODENAME);
1512 if (cipher_noffset < 0)
1515 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1516 *data, *size, &dst, &size_dst);
1528 * fit_image_check_os - check whether image node is of a given os type
1529 * @fit: pointer to the FIT format image header
1530 * @noffset: component image node offset
1531 * @os: requested image os
1533 * fit_image_check_os() reads image os property and compares its numeric
1534 * id with the requested os. Comparison result is returned to the caller.
1537 * 1 if image is of given os type
1538 * 0 otherwise (or on error)
1540 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1544 if (fit_image_get_os(fit, noffset, &image_os))
1546 return (os == image_os);
1550 * fit_image_check_arch - check whether image node is of a given arch
1551 * @fit: pointer to the FIT format image header
1552 * @noffset: component image node offset
1553 * @arch: requested imagearch
1555 * fit_image_check_arch() reads image arch property and compares its numeric
1556 * id with the requested arch. Comparison result is returned to the caller.
1559 * 1 if image is of given arch
1560 * 0 otherwise (or on error)
1562 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1565 int aarch32_support = 0;
1567 /* Let's assume that sandbox can load any architecture */
1568 if (IS_ENABLED(CONFIG_SANDBOX))
1571 if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32))
1572 aarch32_support = 1;
1574 if (fit_image_get_arch(fit, noffset, &image_arch))
1576 return (arch == image_arch) ||
1577 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1578 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1583 * fit_image_check_type - check whether image node is of a given type
1584 * @fit: pointer to the FIT format image header
1585 * @noffset: component image node offset
1586 * @type: requested image type
1588 * fit_image_check_type() reads image type property and compares its numeric
1589 * id with the requested type. Comparison result is returned to the caller.
1592 * 1 if image is of given type
1593 * 0 otherwise (or on error)
1595 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1599 if (fit_image_get_type(fit, noffset, &image_type))
1601 return (type == image_type);
1605 * fit_image_check_comp - check whether image node uses given compression
1606 * @fit: pointer to the FIT format image header
1607 * @noffset: component image node offset
1608 * @comp: requested image compression type
1610 * fit_image_check_comp() reads image compression property and compares its
1611 * numeric id with the requested compression type. Comparison result is
1612 * returned to the caller.
1615 * 1 if image uses requested compression
1616 * 0 otherwise (or on error)
1618 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1622 if (fit_image_get_comp(fit, noffset, &image_comp))
1624 return (comp == image_comp);
1628 * fdt_check_no_at() - Check for nodes whose names contain '@'
1630 * This checks the parent node and all subnodes recursively
1632 * @fit: FIT to check
1633 * @parent: Parent node to check
1634 * Return: 0 if OK, -EADDRNOTAVAIL is a node has a name containing '@'
1636 static int fdt_check_no_at(const void *fit, int parent)
1642 name = fdt_get_name(fit, parent, NULL);
1643 if (!name || strchr(name, '@'))
1644 return -EADDRNOTAVAIL;
1646 fdt_for_each_subnode(node, fit, parent) {
1647 ret = fdt_check_no_at(fit, node);
1655 int fit_check_format(const void *fit, ulong size)
1659 /* A FIT image must be a valid FDT */
1660 ret = fdt_check_header(fit);
1662 log_debug("Wrong FIT format: not a flattened device tree (err=%d)\n",
1667 if (CONFIG_IS_ENABLED(FIT_FULL_CHECK)) {
1669 * If we are not given the size, make do wtih calculating it.
1670 * This is not as secure, so we should consider a flag to
1673 if (size == IMAGE_SIZE_INVAL)
1674 size = fdt_totalsize(fit);
1675 ret = fdt_check_full(fit, size);
1680 * U-Boot stopped using unit addressed in 2017. Since libfdt
1681 * can match nodes ignoring any unit address, signature
1682 * verification can see the wrong node if one is inserted with
1683 * the same name as a valid node but with a unit address
1684 * attached. Protect against this by disallowing unit addresses.
1686 if (!ret && CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
1687 ret = fdt_check_no_at(fit, 0);
1690 log_debug("FIT check error %d\n", ret);
1695 log_debug("FIT check error %d\n", ret);
1700 /* mandatory / node 'description' property */
1701 if (!fdt_getprop(fit, 0, FIT_DESC_PROP, NULL)) {
1702 log_debug("Wrong FIT format: no description\n");
1706 if (IMAGE_ENABLE_TIMESTAMP) {
1707 /* mandatory / node 'timestamp' property */
1708 if (!fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL)) {
1709 log_debug("Wrong FIT format: no timestamp\n");
1714 /* mandatory subimages parent '/images' node */
1715 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1716 log_debug("Wrong FIT format: no images parent node\n");
1723 int fit_conf_find_compat(const void *fit, const void *fdt)
1726 int noffset, confs_noffset, images_noffset;
1727 const void *fdt_compat;
1729 int best_match_offset = 0;
1730 int best_match_pos = 0;
1732 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1733 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1734 if (confs_noffset < 0 || images_noffset < 0) {
1735 debug("Can't find configurations or images nodes.\n");
1739 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1741 debug("Fdt for comparison has no \"compatible\" property.\n");
1746 * Loop over the configurations in the FIT image.
1748 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1749 (noffset >= 0) && (ndepth > 0);
1750 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1752 const char *kfdt_name;
1753 int kfdt_noffset, compat_noffset;
1754 const char *cur_fdt_compat;
1762 /* If there's a compat property in the config node, use that. */
1763 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1764 fdt = fit; /* search in FIT image */
1765 compat_noffset = noffset; /* search under config node */
1766 } else { /* Otherwise extract it from the kernel FDT. */
1767 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1769 debug("No fdt property found.\n");
1772 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1774 if (kfdt_noffset < 0) {
1775 debug("No image node named \"%s\" found.\n",
1780 if (!fit_image_check_comp(fit, kfdt_noffset,
1782 debug("Can't extract compat from \"%s\" "
1783 "(compressed)\n", kfdt_name);
1787 /* search in this config's kernel FDT */
1788 if (fit_image_get_data_and_size(fit, kfdt_noffset,
1790 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1794 compat_noffset = 0; /* search kFDT under root node */
1797 len = fdt_compat_len;
1798 cur_fdt_compat = fdt_compat;
1800 * Look for a match for each U-Boot compatibility string in
1801 * turn in the compat string property.
1803 for (i = 0; len > 0 &&
1804 (!best_match_offset || best_match_pos > i); i++) {
1805 int cur_len = strlen(cur_fdt_compat) + 1;
1807 if (!fdt_node_check_compatible(fdt, compat_noffset,
1809 best_match_offset = noffset;
1814 cur_fdt_compat += cur_len;
1817 if (!best_match_offset) {
1818 debug("No match found.\n");
1822 return best_match_offset;
1825 int fit_conf_get_node(const void *fit, const char *conf_uname)
1827 int noffset, confs_noffset;
1830 char *conf_uname_copy = NULL;
1832 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1833 if (confs_noffset < 0) {
1834 debug("Can't find configurations parent node '%s' (%s)\n",
1835 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1836 return confs_noffset;
1839 if (conf_uname == NULL) {
1840 /* get configuration unit name from the default property */
1841 debug("No configuration specified, trying default...\n");
1842 if (!tools_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) {
1843 noffset = fit_find_config_node(fit);
1846 conf_uname = fdt_get_name(fit, noffset, NULL);
1848 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1849 FIT_DEFAULT_PROP, &len);
1850 if (conf_uname == NULL) {
1851 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1856 debug("Found default configuration: '%s'\n", conf_uname);
1859 s = strchr(conf_uname, '#');
1861 len = s - conf_uname;
1862 conf_uname_copy = malloc(len + 1);
1863 if (!conf_uname_copy) {
1864 debug("Can't allocate uname copy: '%s'\n",
1868 memcpy(conf_uname_copy, conf_uname, len);
1869 conf_uname_copy[len] = '\0';
1870 conf_uname = conf_uname_copy;
1873 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1875 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1876 conf_uname, fdt_strerror(noffset));
1879 free(conf_uname_copy);
1884 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1885 const char *prop_name)
1887 return fdt_stringlist_count(fit, noffset, prop_name);
1890 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1891 const char *prop_name, int index)
1896 /* get kernel image unit name from configuration kernel property */
1897 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1901 return fit_image_get_node(fit, uname);
1904 int fit_conf_get_prop_node(const void *fit, int noffset, const char *prop_name,
1905 enum image_phase_t sel_phase)
1909 if (sel_phase == IH_PHASE_NONE)
1910 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1912 count = fit_conf_get_prop_node_count(fit, noffset, prop_name);
1916 /* check each image in the list */
1917 for (i = 0; i < count; i++) {
1918 enum image_phase_t phase;
1921 node = fit_conf_get_prop_node_index(fit, noffset, prop_name, i);
1922 ret = fit_image_get_phase(fit, node, &phase);
1924 /* if the image is for any phase, let's use it */
1930 if (phase == sel_phase)
1937 static int fit_get_data_tail(const void *fit, int noffset,
1938 const void **data, size_t *size)
1945 if (!fit_image_verify(fit, noffset))
1948 if (fit_image_get_data_and_size(fit, noffset, data, size))
1951 if (!fit_get_desc(fit, noffset, &desc))
1952 printf("%s\n", desc);
1957 int fit_get_data_node(const void *fit, const char *image_uname,
1958 const void **data, size_t *size)
1960 int noffset = fit_image_get_node(fit, image_uname);
1962 return fit_get_data_tail(fit, noffset, data, size);
1965 int fit_get_data_conf_prop(const void *fit, const char *prop_name,
1966 const void **data, size_t *size)
1968 int noffset = fit_conf_get_node(fit, NULL);
1970 noffset = fit_conf_get_prop_node(fit, noffset, prop_name,
1972 return fit_get_data_tail(fit, noffset, data, size);
1975 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1977 fit_image_print(fit, rd_noffset, " ");
1980 puts(" Verifying Hash Integrity ... ");
1981 if (!fit_image_verify(fit, rd_noffset)) {
1982 puts("Bad Data Hash\n");
1991 int fit_get_node_from_config(struct bootm_headers *images,
1992 const char *prop_name, ulong addr)
1998 debug("* %s: using config '%s' from image at 0x%08lx\n",
1999 prop_name, images->fit_uname_cfg, addr);
2001 /* Check whether configuration has this property defined */
2002 fit_hdr = map_sysmem(addr, 0);
2003 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
2004 if (cfg_noffset < 0) {
2005 debug("* %s: no such config\n", prop_name);
2009 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name,
2012 debug("* %s: no '%s' in config\n", prop_name, prop_name);
2020 * fit_get_image_type_property() - get property name for IH_TYPE_...
2022 * Return: the properly name where we expect to find the image in the
2025 static const char *fit_get_image_type_property(int type)
2028 * This is sort-of available in the uimage_type[] table in image.c
2029 * but we don't have access to the short name, and "fdt" is different
2030 * anyway. So let's just keep it here.
2033 case IH_TYPE_FLATDT:
2034 return FIT_FDT_PROP;
2035 case IH_TYPE_KERNEL:
2036 return FIT_KERNEL_PROP;
2037 case IH_TYPE_FIRMWARE:
2038 return FIT_FIRMWARE_PROP;
2039 case IH_TYPE_RAMDISK:
2040 return FIT_RAMDISK_PROP;
2041 case IH_TYPE_X86_SETUP:
2042 return FIT_SETUP_PROP;
2043 case IH_TYPE_LOADABLE:
2044 return FIT_LOADABLE_PROP;
2046 return FIT_FPGA_PROP;
2047 case IH_TYPE_STANDALONE:
2048 return FIT_STANDALONE_PROP;
2054 int fit_image_load(struct bootm_headers *images, ulong addr,
2055 const char **fit_unamep, const char **fit_uname_configp,
2056 int arch, int ph_type, int bootstage_id,
2057 enum fit_load_op load_op, ulong *datap, ulong *lenp)
2059 int image_type = image_ph_type(ph_type);
2060 int cfg_noffset, noffset;
2061 const char *fit_uname;
2062 const char *fit_uname_config;
2063 const char *fit_base_uname_config;
2069 ulong load, load_end, data, len;
2071 const char *prop_name;
2074 fit = map_sysmem(addr, 0);
2075 fit_uname = fit_unamep ? *fit_unamep : NULL;
2076 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
2077 fit_base_uname_config = NULL;
2078 prop_name = fit_get_image_type_property(image_type);
2079 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
2081 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
2082 ret = fit_check_format(fit, IMAGE_SIZE_INVAL);
2084 printf("Bad FIT %s image format! (err=%d)\n", prop_name, ret);
2085 if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && ret == -EADDRNOTAVAIL)
2086 printf("Signature checking prevents use of unit addresses (@) in nodes\n");
2087 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
2090 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
2092 /* get FIT component image node offset */
2093 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
2094 noffset = fit_image_get_node(fit, fit_uname);
2097 * no image node unit name, try to get config
2098 * node first. If config unit node name is NULL
2099 * fit_conf_get_node() will try to find default config node
2101 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
2102 if (IS_ENABLED(CONFIG_FIT_BEST_MATCH) && !fit_uname_config) {
2103 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
2105 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2107 if (cfg_noffset < 0) {
2108 puts("Could not find configuration node\n");
2109 bootstage_error(bootstage_id +
2110 BOOTSTAGE_SUB_NO_UNIT_NAME);
2114 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
2115 printf(" Using '%s' configuration\n", fit_base_uname_config);
2116 /* Remember this config */
2117 if (image_type == IH_TYPE_KERNEL)
2118 images->fit_uname_cfg = fit_base_uname_config;
2120 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
2121 puts(" Verifying Hash Integrity ... ");
2122 if (fit_config_verify(fit, cfg_noffset)) {
2123 puts("Bad Data Hash\n");
2124 bootstage_error(bootstage_id +
2125 BOOTSTAGE_SUB_HASH);
2131 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
2133 noffset = fit_conf_get_prop_node(fit, cfg_noffset, prop_name,
2134 image_ph_phase(ph_type));
2135 fit_uname = fit_get_name(fit, noffset, NULL);
2138 printf("Could not find subimage node type '%s'\n", prop_name);
2139 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
2143 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
2145 ret = fit_image_select(fit, noffset, images->verify);
2147 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2151 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2152 if (!tools_build() && IS_ENABLED(CONFIG_SANDBOX)) {
2153 if (!fit_image_check_target_arch(fit, noffset)) {
2154 puts("Unsupported Architecture\n");
2155 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2164 fit_image_get_arch(fit, noffset, &os_arch);
2165 images->os.arch = os_arch;
2169 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2170 type_ok = fit_image_check_type(fit, noffset, image_type) ||
2171 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
2172 fit_image_check_type(fit, noffset, IH_TYPE_TEE) ||
2173 (image_type == IH_TYPE_KERNEL &&
2174 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
2176 os_ok = image_type == IH_TYPE_FLATDT ||
2177 image_type == IH_TYPE_FPGA ||
2178 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
2179 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
2180 fit_image_check_os(fit, noffset, IH_OS_TEE) ||
2181 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
2182 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
2183 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
2186 * If either of the checks fail, we should report an error, but
2187 * if the image type is coming from the "loadables" field, we
2188 * don't care what it is
2190 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
2191 fit_image_get_os(fit, noffset, &os);
2192 printf("No %s %s %s Image\n",
2193 genimg_get_os_name(os),
2194 genimg_get_arch_name(arch),
2195 genimg_get_type_name(image_type));
2196 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2200 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2202 /* get image data address and length */
2203 if (fit_image_get_data_and_size(fit, noffset,
2204 (const void **)&buf, &size)) {
2205 printf("Could not find %s subimage data!\n", prop_name);
2206 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2210 /* Decrypt data before uncompress/move */
2211 if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
2212 puts(" Decrypting Data ... ");
2213 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2220 /* perform any post-processing on the image data */
2221 if (!tools_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
2222 board_fit_image_post_process(fit, noffset, &buf, &size);
2226 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2228 data = map_to_sysmem(buf);
2230 if (load_op == FIT_LOAD_IGNORED) {
2232 } else if (fit_image_get_load(fit, noffset, &load)) {
2233 if (load_op == FIT_LOAD_REQUIRED) {
2234 printf("Can't get %s subimage load address!\n",
2236 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2239 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
2240 ulong image_start, image_end;
2243 * move image data to the load address,
2244 * make sure we don't overwrite initial image
2247 image_end = addr + fit_get_size(fit);
2249 load_end = load + len;
2250 if (image_type != IH_TYPE_KERNEL &&
2251 load < image_end && load_end > image_start) {
2252 printf("Error: %s overwritten\n", prop_name);
2256 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2257 prop_name, data, load);
2259 load = data; /* No load address specified */
2262 comp = IH_COMP_NONE;
2264 /* Kernel images get decompressed later in bootm_load_os(). */
2265 if (!fit_image_get_comp(fit, noffset, &comp) &&
2266 comp != IH_COMP_NONE &&
2267 !(image_type == IH_TYPE_KERNEL ||
2268 image_type == IH_TYPE_KERNEL_NOLOAD ||
2269 image_type == IH_TYPE_RAMDISK)) {
2270 ulong max_decomp_len = len * 20;
2272 loadbuf = malloc(max_decomp_len);
2273 load = map_to_sysmem(loadbuf);
2275 loadbuf = map_sysmem(load, max_decomp_len);
2277 if (image_decomp(comp, load, data, image_type,
2278 loadbuf, buf, len, max_decomp_len, &load_end)) {
2279 printf("Error decompressing %s\n", prop_name);
2283 len = load_end - load;
2284 } else if (load != data) {
2285 loadbuf = map_sysmem(load, len);
2286 memcpy(loadbuf, buf, len);
2289 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2290 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2291 " please fix your .its file!\n");
2293 /* verify that image data is a proper FDT blob */
2294 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2295 puts("Subimage data is not a FDT");
2299 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2304 *fit_unamep = (char *)fit_uname;
2305 if (fit_uname_configp)
2306 *fit_uname_configp = (char *)(fit_uname_config ? :
2307 fit_base_uname_config);
2312 int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch,
2313 ulong *setup_start, ulong *setup_len)
2320 addr = map_to_sysmem(images->fit_hdr_os);
2321 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2325 ret = fit_image_load(images, addr, NULL, NULL, arch,
2326 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2327 FIT_LOAD_REQUIRED, setup_start, &len);
2333 int boot_get_fdt_fit(struct bootm_headers *images, ulong addr,
2334 const char **fit_unamep, const char **fit_uname_configp,
2335 int arch, ulong *datap, ulong *lenp)
2337 int fdt_noffset, cfg_noffset, count;
2339 const char *fit_uname = NULL;
2340 const char *fit_uname_config = NULL;
2341 char *fit_uname_config_copy = NULL;
2342 char *next_config = NULL;
2344 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2345 ulong image_start, image_end;
2346 ulong ovload, ovlen, ovcopylen;
2347 const char *uconfig;
2349 void *base, *ov, *ovcopy = NULL;
2350 int i, err, noffset, ov_noffset;
2353 fit_uname = fit_unamep ? *fit_unamep : NULL;
2355 if (fit_uname_configp && *fit_uname_configp) {
2356 fit_uname_config_copy = strdup(*fit_uname_configp);
2357 if (!fit_uname_config_copy)
2360 next_config = strchr(fit_uname_config_copy, '#');
2362 *next_config++ = '\0';
2363 if (next_config - 1 > fit_uname_config_copy)
2364 fit_uname_config = fit_uname_config_copy;
2367 fdt_noffset = fit_image_load(images,
2368 addr, &fit_uname, &fit_uname_config,
2369 arch, IH_TYPE_FLATDT,
2370 BOOTSTAGE_ID_FIT_FDT_START,
2371 FIT_LOAD_OPTIONAL, &load, &len);
2373 if (fdt_noffset < 0)
2376 debug("fit_uname=%s, fit_uname_config=%s\n",
2377 fit_uname ? fit_uname : "<NULL>",
2378 fit_uname_config ? fit_uname_config : "<NULL>");
2380 fit = map_sysmem(addr, 0);
2382 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2384 /* single blob, or error just return as well */
2385 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2386 if (count <= 1 && !next_config)
2389 /* we need to apply overlays */
2391 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2393 image_end = addr + fit_get_size(fit);
2394 /* verify that relocation took place by load address not being in fit */
2395 if (load >= image_start && load < image_end) {
2396 /* check is simplified; fit load checks for overlaps */
2397 printf("Overlayed FDT requires relocation\n");
2398 fdt_noffset = -EBADF;
2402 base = map_sysmem(load, len);
2404 /* apply extra configs in FIT first, followed by args */
2405 for (i = 1; ; i++) {
2407 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2409 uname = fit_get_name(fit, noffset, NULL);
2414 uconfig = next_config;
2415 next_config = strchr(next_config, '#');
2417 *next_config++ = '\0';
2421 * fit_image_load() would load the first FDT from the
2422 * extra config only when uconfig is specified.
2423 * Check if the extra config contains multiple FDTs and
2426 cfg_noffset = fit_conf_get_node(fit, uconfig);
2429 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2433 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2435 ov_noffset = fit_image_load(images,
2436 addr, &uname, &uconfig,
2437 arch, IH_TYPE_FLATDT,
2438 BOOTSTAGE_ID_FIT_FDT_START,
2439 FIT_LOAD_IGNORED, &ovload, &ovlen);
2440 if (ov_noffset < 0) {
2441 printf("load of %s failed\n", uname);
2444 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2445 uname, ovload, ovlen);
2446 ov = map_sysmem(ovload, ovlen);
2448 ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K);
2449 ovcopy = malloc(ovcopylen);
2451 printf("failed to duplicate DTO before application\n");
2452 fdt_noffset = -ENOMEM;
2456 err = fdt_open_into(ov, ovcopy, ovcopylen);
2458 printf("failed on fdt_open_into for DTO\n");
2463 base = map_sysmem(load, len + ovlen);
2464 err = fdt_open_into(base, base, len + ovlen);
2466 printf("failed on fdt_open_into\n");
2471 /* the verbose method prints out messages on error */
2472 err = fdt_overlay_apply_verbose(base, ovcopy);
2478 len = fdt_totalsize(base);
2481 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2482 fdt_noffset = -EBADF;
2491 *fit_unamep = fit_uname;
2492 if (fit_uname_configp)
2493 *fit_uname_configp = fit_uname_config;
2495 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2498 free(fit_uname_config_copy);