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>
18 #include <linux/kconfig.h>
24 DECLARE_GLOBAL_DATA_PTR;
25 #endif /* !USE_HOSTCC*/
29 #include <bootstage.h>
30 #include <u-boot/crc.h>
31 #include <u-boot/md5.h>
32 #include <u-boot/sha1.h>
33 #include <u-boot/sha256.h>
35 /*****************************************************************************/
36 /* New uImage format routines */
37 /*****************************************************************************/
39 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
40 ulong *addr, const char **name)
47 sep = strchr(spec, sepc);
50 *addr = simple_strtoul(spec, NULL, 16);
60 * fit_parse_conf - parse FIT configuration spec
61 * @spec: input string, containing configuration spec
62 * @add_curr: current image address (to be used as a possible default)
63 * @addr: pointer to a ulong variable, will hold FIT image address of a given
65 * @conf_name double pointer to a char, will hold pointer to a configuration
68 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
69 * where <addr> is a FIT image address that contains configuration
70 * with a <conf> unit name.
72 * Address part is optional, and if omitted default add_curr will
76 * 1 if spec is a valid configuration string,
77 * addr and conf_name are set accordingly
80 int fit_parse_conf(const char *spec, ulong addr_curr,
81 ulong *addr, const char **conf_name)
83 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
87 * fit_parse_subimage - parse FIT subimage spec
88 * @spec: input string, containing subimage spec
89 * @add_curr: current image address (to be used as a possible default)
90 * @addr: pointer to a ulong variable, will hold FIT image address of a given
92 * @image_name: double pointer to a char, will hold pointer to a subimage name
94 * fit_parse_subimage() expects subimage spec in the form of
95 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
96 * subimage with a <subimg> unit name.
98 * Address part is optional, and if omitted default add_curr will
102 * 1 if spec is a valid subimage string,
103 * addr and image_name are set accordingly
106 int fit_parse_subimage(const char *spec, ulong addr_curr,
107 ulong *addr, const char **image_name)
109 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
111 #endif /* !USE_HOSTCC */
113 static void fit_get_debug(const void *fit, int noffset,
114 char *prop_name, int err)
116 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
117 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
122 * fit_get_subimage_count - get component (sub-image) count
123 * @fit: pointer to the FIT format image header
124 * @images_noffset: offset of images node
127 * number of image components
129 int fit_get_subimage_count(const void *fit, int images_noffset)
135 /* Process its subnodes, print out component images details */
136 for (ndepth = 0, count = 0,
137 noffset = fdt_next_node(fit, images_noffset, &ndepth);
138 (noffset >= 0) && (ndepth > 0);
139 noffset = fdt_next_node(fit, noffset, &ndepth)) {
148 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
150 * fit_image_print_data() - prints out the hash node details
151 * @fit: pointer to the FIT format image header
152 * @noffset: offset of the hash node
153 * @p: pointer to prefix string
154 * @type: Type of information to print ("hash" or "sign")
156 * fit_image_print_data() lists properties for the processed hash node
158 * This function avoid using puts() since it prints a newline on the host
159 * but does not in U-Boot.
162 * no returned results
164 static void fit_image_print_data(const void *fit, int noffset, const char *p,
175 debug("%s %s node: '%s'\n", p, type,
176 fit_get_name(fit, noffset, NULL));
177 printf("%s %s algo: ", p, type);
178 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
179 printf("invalid/unsupported\n");
183 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
184 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
186 printf(":%s", keyname);
188 printf(" (required)");
191 padding = fdt_getprop(fit, noffset, "padding", NULL);
193 printf("%s %s padding: %s\n", p, type, padding);
195 ret = fit_image_hash_get_value(fit, noffset, &value,
197 printf("%s %s value: ", p, type);
199 printf("unavailable\n");
201 for (i = 0; i < value_len; i++)
202 printf("%02x", value[i]);
206 debug("%s %s len: %d\n", p, type, value_len);
208 /* Signatures have a time stamp */
209 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
212 printf("%s Timestamp: ", p);
213 if (fit_get_timestamp(fit, noffset, ×tamp))
214 printf("unavailable\n");
216 genimg_print_time(timestamp);
221 * fit_image_print_verification_data() - prints out the hash/signature details
222 * @fit: pointer to the FIT format image header
223 * @noffset: offset of the hash or signature node
224 * @p: pointer to prefix string
226 * This lists properties for the processed hash node
229 * no returned results
231 static void fit_image_print_verification_data(const void *fit, int noffset,
237 * Check subnode name, must be equal to "hash" or "signature".
238 * Multiple hash/signature nodes require unique unit node
239 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
241 name = fit_get_name(fit, noffset, NULL);
242 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
243 fit_image_print_data(fit, noffset, p, "Hash");
244 } else if (!strncmp(name, FIT_SIG_NODENAME,
245 strlen(FIT_SIG_NODENAME))) {
246 fit_image_print_data(fit, noffset, p, "Sign");
251 * fit_conf_print - prints out the FIT configuration details
252 * @fit: pointer to the FIT format image header
253 * @noffset: offset of the configuration node
254 * @p: pointer to prefix string
256 * fit_conf_print() lists all mandatory properties for the processed
257 * configuration node.
260 * no returned results
262 static void fit_conf_print(const void *fit, int noffset, const char *p)
267 int fdt_index, loadables_index;
270 /* Mandatory properties */
271 ret = fit_get_desc(fit, noffset, &desc);
272 printf("%s Description: ", p);
274 printf("unavailable\n");
276 printf("%s\n", desc);
278 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
279 printf("%s Kernel: ", p);
281 printf("unavailable\n");
283 printf("%s\n", uname);
285 /* Optional properties */
286 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
288 printf("%s Init Ramdisk: %s\n", p, uname);
290 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
292 printf("%s Firmware: %s\n", p, uname);
295 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
296 fdt_index, NULL), uname;
299 printf("%s FDT: ", p);
302 printf("%s\n", uname);
305 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
307 printf("%s FPGA: %s\n", p, uname);
309 /* Print out all of the specified loadables */
310 for (loadables_index = 0;
311 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
312 loadables_index, NULL), uname;
314 if (loadables_index == 0) {
315 printf("%s Loadables: ", p);
319 printf("%s\n", uname);
322 /* Process all hash subnodes of the component configuration node */
323 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
324 (noffset >= 0) && (ndepth > 0);
325 noffset = fdt_next_node(fit, noffset, &ndepth)) {
327 /* Direct child node of the component configuration node */
328 fit_image_print_verification_data(fit, noffset, p);
334 * fit_print_contents - prints out the contents of the FIT format image
335 * @fit: pointer to the FIT format image header
336 * @p: pointer to prefix string
338 * fit_print_contents() formats a multi line FIT image contents description.
339 * The routine prints out FIT image properties (root node level) followed by
340 * the details of each component image.
343 * no returned results
345 void fit_print_contents(const void *fit)
358 /* Indent string is defined in header image.h */
359 p = IMAGE_INDENT_STRING;
361 /* Root node properties */
362 ret = fit_get_desc(fit, 0, &desc);
363 printf("%sFIT description: ", p);
365 printf("unavailable\n");
367 printf("%s\n", desc);
369 if (IMAGE_ENABLE_TIMESTAMP) {
370 ret = fit_get_timestamp(fit, 0, ×tamp);
371 printf("%sCreated: ", p);
373 printf("unavailable\n");
375 genimg_print_time(timestamp);
378 /* Find images parent node offset */
379 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
380 if (images_noffset < 0) {
381 printf("Can't find images parent node '%s' (%s)\n",
382 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
386 /* Process its subnodes, print out component images details */
387 for (ndepth = 0, count = 0,
388 noffset = fdt_next_node(fit, images_noffset, &ndepth);
389 (noffset >= 0) && (ndepth > 0);
390 noffset = fdt_next_node(fit, noffset, &ndepth)) {
393 * Direct child node of the images parent node,
394 * i.e. component image node.
396 printf("%s Image %u (%s)\n", p, count++,
397 fit_get_name(fit, noffset, NULL));
399 fit_image_print(fit, noffset, p);
403 /* Find configurations parent node offset */
404 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
405 if (confs_noffset < 0) {
406 debug("Can't get configurations parent node '%s' (%s)\n",
407 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
411 /* get default configuration unit name from default property */
412 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
414 printf("%s Default Configuration: '%s'\n", p, uname);
416 /* Process its subnodes, print out configurations details */
417 for (ndepth = 0, count = 0,
418 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
419 (noffset >= 0) && (ndepth > 0);
420 noffset = fdt_next_node(fit, noffset, &ndepth)) {
423 * Direct child node of the configurations parent node,
424 * i.e. configuration node.
426 printf("%s Configuration %u (%s)\n", p, count++,
427 fit_get_name(fit, noffset, NULL));
429 fit_conf_print(fit, noffset, p);
435 * fit_image_print - prints out the FIT component image details
436 * @fit: pointer to the FIT format image header
437 * @image_noffset: offset of the component image node
438 * @p: pointer to prefix string
440 * fit_image_print() lists all mandatory properties for the processed component
441 * image. If present, hash nodes are printed out as well. Load
442 * address for images of type firmware is also printed out. Since the load
443 * address is not mandatory for firmware images, it will be output as
444 * "unavailable" when not present.
447 * no returned results
449 void fit_image_print(const void *fit, int image_noffset, const char *p)
452 uint8_t type, arch, os, comp;
460 /* Mandatory properties */
461 ret = fit_get_desc(fit, image_noffset, &desc);
462 printf("%s Description: ", p);
464 printf("unavailable\n");
466 printf("%s\n", desc);
468 if (IMAGE_ENABLE_TIMESTAMP) {
471 ret = fit_get_timestamp(fit, 0, ×tamp);
472 printf("%s Created: ", p);
474 printf("unavailable\n");
476 genimg_print_time(timestamp);
479 fit_image_get_type(fit, image_noffset, &type);
480 printf("%s Type: %s\n", p, genimg_get_type_name(type));
482 fit_image_get_comp(fit, image_noffset, &comp);
483 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
485 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
488 printf("%s Data Start: ", p);
490 printf("unavailable\n");
492 void *vdata = (void *)data;
494 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
498 printf("%s Data Size: ", p);
500 printf("unavailable\n");
502 genimg_print_size(size);
504 /* Remaining, type dependent properties */
505 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
506 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
507 (type == IH_TYPE_FLATDT)) {
508 fit_image_get_arch(fit, image_noffset, &arch);
509 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
512 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
513 (type == IH_TYPE_FIRMWARE)) {
514 fit_image_get_os(fit, image_noffset, &os);
515 printf("%s OS: %s\n", p, genimg_get_os_name(os));
518 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
519 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
520 (type == IH_TYPE_FPGA)) {
521 ret = fit_image_get_load(fit, image_noffset, &load);
522 printf("%s Load Address: ", p);
524 printf("unavailable\n");
526 printf("0x%08lx\n", load);
529 /* optional load address for FDT */
530 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
531 printf("%s Load Address: 0x%08lx\n", p, load);
533 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
534 (type == IH_TYPE_RAMDISK)) {
535 ret = fit_image_get_entry(fit, image_noffset, &entry);
536 printf("%s Entry Point: ", p);
538 printf("unavailable\n");
540 printf("0x%08lx\n", entry);
543 /* Process all hash subnodes of the component image node */
544 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
545 (noffset >= 0) && (ndepth > 0);
546 noffset = fdt_next_node(fit, noffset, &ndepth)) {
548 /* Direct child node of the component image node */
549 fit_image_print_verification_data(fit, noffset, p);
554 void fit_print_contents(const void *fit) { }
555 void fit_image_print(const void *fit, int image_noffset, const char *p) { }
556 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
559 * fit_get_desc - get node description property
560 * @fit: pointer to the FIT format image header
561 * @noffset: node offset
562 * @desc: double pointer to the char, will hold pointer to the description
564 * fit_get_desc() reads description property from a given node, if
565 * description is found pointer to it is returned in third call argument.
571 int fit_get_desc(const void *fit, int noffset, char **desc)
575 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
577 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
585 * fit_get_timestamp - get node timestamp property
586 * @fit: pointer to the FIT format image header
587 * @noffset: node offset
588 * @timestamp: pointer to the time_t, will hold read timestamp
590 * fit_get_timestamp() reads timestamp property from given node, if timestamp
591 * is found and has a correct size its value is returned in third call
596 * -1, on property read failure
597 * -2, on wrong timestamp size
599 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
604 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
606 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
609 if (len != sizeof(uint32_t)) {
610 debug("FIT timestamp with incorrect size of (%u)\n", len);
614 *timestamp = uimage_to_cpu(*((uint32_t *)data));
619 * fit_image_get_node - get node offset for component image of a given unit name
620 * @fit: pointer to the FIT format image header
621 * @image_uname: component image node unit name
623 * fit_image_get_node() finds a component image (within the '/images'
624 * node) of a provided unit name. If image is found its node offset is
625 * returned to the caller.
628 * image node offset when found (>=0)
629 * negative number on failure (FDT_ERR_* code)
631 int fit_image_get_node(const void *fit, const char *image_uname)
633 int noffset, images_noffset;
635 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
636 if (images_noffset < 0) {
637 debug("Can't find images parent node '%s' (%s)\n",
638 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
639 return images_noffset;
642 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
644 debug("Can't get node offset for image unit name: '%s' (%s)\n",
645 image_uname, fdt_strerror(noffset));
652 * fit_image_get_os - get os id for a given component image node
653 * @fit: pointer to the FIT format image header
654 * @noffset: component image node offset
655 * @os: pointer to the uint8_t, will hold os numeric id
657 * fit_image_get_os() finds os property in a given component image node.
658 * If the property is found, its (string) value is translated to the numeric
659 * id which is returned to the caller.
665 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
670 /* Get OS name from property data */
671 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
673 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
678 /* Translate OS name to id */
679 *os = genimg_get_os_id(data);
684 * fit_image_get_arch - get arch id for a given component image node
685 * @fit: pointer to the FIT format image header
686 * @noffset: component image node offset
687 * @arch: pointer to the uint8_t, will hold arch numeric id
689 * fit_image_get_arch() finds arch property in a given component image node.
690 * If the property is found, its (string) value is translated to the numeric
691 * id which is returned to the caller.
697 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
702 /* Get architecture name from property data */
703 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
705 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
710 /* Translate architecture name to id */
711 *arch = genimg_get_arch_id(data);
716 * fit_image_get_type - get type id for a given component image node
717 * @fit: pointer to the FIT format image header
718 * @noffset: component image node offset
719 * @type: pointer to the uint8_t, will hold type numeric id
721 * fit_image_get_type() finds type property in a given component image node.
722 * If the property is found, its (string) value is translated to the numeric
723 * id which is returned to the caller.
729 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
734 /* Get image type name from property data */
735 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
737 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
742 /* Translate image type name to id */
743 *type = genimg_get_type_id(data);
748 * fit_image_get_comp - get comp id for a given component image node
749 * @fit: pointer to the FIT format image header
750 * @noffset: component image node offset
751 * @comp: pointer to the uint8_t, will hold comp numeric id
753 * fit_image_get_comp() finds comp property in a given component image node.
754 * If the property is found, its (string) value is translated to the numeric
755 * id which is returned to the caller.
761 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
766 /* Get compression name from property data */
767 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
769 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
774 /* Translate compression name to id */
775 *comp = genimg_get_comp_id(data);
779 static int fit_image_get_address(const void *fit, int noffset, char *name,
786 cell = fdt_getprop(fit, noffset, name, &len);
788 fit_get_debug(fit, noffset, name, len);
792 if (len > sizeof(ulong)) {
793 printf("Unsupported %s address size\n", name);
798 /* Use load64 to avoid compiling warning for 32-bit target */
800 load64 = (load64 << 32) | uimage_to_cpu(*cell);
803 *load = (ulong)load64;
808 * fit_image_get_load() - get load addr property for given component image node
809 * @fit: pointer to the FIT format image header
810 * @noffset: component image node offset
811 * @load: pointer to the uint32_t, will hold load address
813 * fit_image_get_load() finds load address property in a given component
814 * image node. If the property is found, its value is returned to the caller.
820 int fit_image_get_load(const void *fit, int noffset, ulong *load)
822 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
826 * fit_image_get_entry() - get entry point address property
827 * @fit: pointer to the FIT format image header
828 * @noffset: component image node offset
829 * @entry: pointer to the uint32_t, will hold entry point address
831 * This gets the entry point address property for a given component image
834 * fit_image_get_entry() finds entry point address property in a given
835 * component image node. If the property is found, its value is returned
842 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
844 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
848 * fit_image_get_data - get data property and its size for a given component image node
849 * @fit: pointer to the FIT format image header
850 * @noffset: component image node offset
851 * @data: double pointer to void, will hold data property's data address
852 * @size: pointer to size_t, will hold data property's data size
854 * fit_image_get_data() finds data property in a given component image node.
855 * If the property is found its data start address and size are returned to
862 int fit_image_get_data(const void *fit, int noffset,
863 const void **data, size_t *size)
867 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
869 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
879 * Get 'data-offset' property from a given image node.
881 * @fit: pointer to the FIT image header
882 * @noffset: component image node offset
883 * @data_offset: holds the data-offset property
887 * -ENOENT if the property could not be found
889 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
893 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
897 *data_offset = fdt32_to_cpu(*val);
903 * Get 'data-position' property from a given image node.
905 * @fit: pointer to the FIT image header
906 * @noffset: component image node offset
907 * @data_position: holds the data-position property
911 * -ENOENT if the property could not be found
913 int fit_image_get_data_position(const void *fit, int noffset,
918 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
922 *data_position = fdt32_to_cpu(*val);
928 * Get 'data-size' property from a given image node.
930 * @fit: pointer to the FIT image header
931 * @noffset: component image node offset
932 * @data_size: holds the data-size property
936 * -ENOENT if the property could not be found
938 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
942 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
946 *data_size = fdt32_to_cpu(*val);
952 * Get 'data-size-unciphered' property from a given image node.
954 * @fit: pointer to the FIT image header
955 * @noffset: component image node offset
956 * @data_size: holds the data-size property
960 * -ENOENT if the property could not be found
962 int fit_image_get_data_size_unciphered(const void *fit, int noffset,
967 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
971 *data_size = (size_t)fdt32_to_cpu(*val);
977 * fit_image_get_data_and_size - get data and its size including
978 * both embedded and external data
979 * @fit: pointer to the FIT format image header
980 * @noffset: component image node offset
981 * @data: double pointer to void, will hold data property's data address
982 * @size: pointer to size_t, will hold data property's data size
984 * fit_image_get_data_and_size() finds data and its size including
985 * both embedded and external data. If the property is found
986 * its data start address and size are returned to the caller.
990 * otherwise, on failure
992 int fit_image_get_data_and_size(const void *fit, int noffset,
993 const void **data, size_t *size)
995 bool external_data = false;
1000 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1001 external_data = true;
1002 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1003 external_data = true;
1005 * For FIT with external data, figure out where
1006 * the external images start. This is the base
1007 * for the data-offset properties in each image.
1009 offset += ((fdt_totalsize(fit) + 3) & ~3);
1012 if (external_data) {
1013 debug("External Data\n");
1014 ret = fit_image_get_data_size(fit, noffset, &len);
1016 *data = fit + offset;
1020 ret = fit_image_get_data(fit, noffset, data, size);
1027 * fit_image_hash_get_algo - get hash algorithm name
1028 * @fit: pointer to the FIT format image header
1029 * @noffset: hash node offset
1030 * @algo: double pointer to char, will hold pointer to the algorithm name
1032 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1033 * If the property is found its data start address is returned to the caller.
1039 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1043 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1044 if (*algo == NULL) {
1045 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1053 * fit_image_hash_get_value - get hash value and length
1054 * @fit: pointer to the FIT format image header
1055 * @noffset: hash node offset
1056 * @value: double pointer to uint8_t, will hold address of a hash value data
1057 * @value_len: pointer to an int, will hold hash data length
1059 * fit_image_hash_get_value() finds hash value property in a given hash node.
1060 * If the property is found its data start address and size are returned to
1067 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1072 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1073 if (*value == NULL) {
1074 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1084 * fit_image_hash_get_ignore - get hash ignore flag
1085 * @fit: pointer to the FIT format image header
1086 * @noffset: hash node offset
1087 * @ignore: pointer to an int, will hold hash ignore flag
1089 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1090 * If the property is found and non-zero, the hash algorithm is not verified by
1091 * u-boot automatically.
1094 * 0, on ignore not found
1095 * value, on ignore found
1097 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1102 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1103 if (value == NULL || len != sizeof(int))
1112 * fit_image_cipher_get_algo - get cipher algorithm name
1113 * @fit: pointer to the FIT format image header
1114 * @noffset: cipher node offset
1115 * @algo: double pointer to char, will hold pointer to the algorithm name
1117 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1118 * cipher node. If the property is found its data start address is returned
1125 int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1129 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1131 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1138 ulong fit_get_end(const void *fit)
1140 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1144 * fit_set_timestamp - set node timestamp property
1145 * @fit: pointer to the FIT format image header
1146 * @noffset: node offset
1147 * @timestamp: timestamp value to be set
1149 * fit_set_timestamp() attempts to set timestamp property in the requested
1150 * node and returns operation status to the caller.
1154 * -ENOSPC if no space in device tree, -1 for other error
1156 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1161 t = cpu_to_uimage(timestamp);
1162 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1165 debug("Can't set '%s' property for '%s' node (%s)\n",
1166 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1168 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1175 * calculate_hash - calculate and return hash for provided input data
1176 * @data: pointer to the input data
1177 * @data_len: data length
1178 * @algo: requested hash algorithm
1179 * @value: pointer to the char, will hold hash value data (caller must
1180 * allocate enough free space)
1181 * value_len: length of the calculated hash
1183 * calculate_hash() computes input data hash according to the requested
1185 * Resulting hash value is placed in caller provided 'value' buffer, length
1186 * of the calculated hash is returned via value_len pointer argument.
1190 * -1, when algo is unsupported
1192 int calculate_hash(const void *data, int data_len, const char *algo,
1193 uint8_t *value, int *value_len)
1195 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1196 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1198 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1200 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1201 sha1_csum_wd((unsigned char *)data, data_len,
1202 (unsigned char *)value, CHUNKSZ_SHA1);
1204 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1205 sha256_csum_wd((unsigned char *)data, data_len,
1206 (unsigned char *)value, CHUNKSZ_SHA256);
1207 *value_len = SHA256_SUM_LEN;
1208 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1209 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1212 debug("Unsupported hash alogrithm\n");
1218 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1219 size_t size, char **err_msgp)
1221 uint8_t value[FIT_MAX_HASH_LEN];
1230 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1231 *err_msgp = "Can't get hash algo property";
1236 if (IMAGE_ENABLE_IGNORE) {
1237 fit_image_hash_get_ignore(fit, noffset, &ignore);
1239 printf("-skipped ");
1244 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1246 *err_msgp = "Can't get hash value property";
1250 if (calculate_hash(data, size, algo, value, &value_len)) {
1251 *err_msgp = "Unsupported hash algorithm";
1255 if (value_len != fit_value_len) {
1256 *err_msgp = "Bad hash value len";
1258 } else if (memcmp(value, fit_value, value_len) != 0) {
1259 *err_msgp = "Bad hash value";
1266 int fit_image_verify_with_data(const void *fit, int image_noffset,
1267 const void *data, size_t size)
1274 /* Verify all required signatures */
1275 if (FIT_IMAGE_ENABLE_VERIFY &&
1276 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1277 gd_fdt_blob(), &verify_all)) {
1278 err_msg = "Unable to verify required signature";
1282 /* Process all hash subnodes of the component image node */
1283 fdt_for_each_subnode(noffset, fit, image_noffset) {
1284 const char *name = fit_get_name(fit, noffset, NULL);
1287 * Check subnode name, must be equal to "hash".
1288 * Multiple hash nodes require unique unit node
1289 * names, e.g. hash-1, hash-2, etc.
1291 if (!strncmp(name, FIT_HASH_NODENAME,
1292 strlen(FIT_HASH_NODENAME))) {
1293 if (fit_image_check_hash(fit, noffset, data, size,
1297 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
1298 !strncmp(name, FIT_SIG_NODENAME,
1299 strlen(FIT_SIG_NODENAME))) {
1300 ret = fit_image_check_sig(fit, noffset, data,
1301 size, -1, &err_msg);
1304 * Show an indication on failure, but do not return
1305 * an error. Only keys marked 'required' can cause
1306 * an image validation failure. See the call to
1307 * fit_image_verify_required_sigs() above.
1316 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1317 err_msg = "Corrupted or truncated tree";
1324 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1325 err_msg, fit_get_name(fit, noffset, NULL),
1326 fit_get_name(fit, image_noffset, NULL));
1331 * fit_image_verify - verify data integrity
1332 * @fit: pointer to the FIT format image header
1333 * @image_noffset: component image node offset
1335 * fit_image_verify() goes over component image hash nodes,
1336 * re-calculates each data hash and compares with the value stored in hash
1340 * 1, if all hashes are valid
1341 * 0, otherwise (or on error)
1343 int fit_image_verify(const void *fit, int image_noffset)
1350 /* Get image data and data length */
1351 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1352 err_msg = "Can't get image data/size";
1353 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1354 err_msg, fit_get_name(fit, noffset, NULL),
1355 fit_get_name(fit, image_noffset, NULL));
1359 return fit_image_verify_with_data(fit, image_noffset, data, size);
1363 * fit_all_image_verify - verify data integrity for all images
1364 * @fit: pointer to the FIT format image header
1366 * fit_all_image_verify() goes over all images in the FIT and
1367 * for every images checks if all it's hashes are valid.
1370 * 1, if all hashes of all images are valid
1371 * 0, otherwise (or on error)
1373 int fit_all_image_verify(const void *fit)
1380 /* Find images parent node offset */
1381 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1382 if (images_noffset < 0) {
1383 printf("Can't find images parent node '%s' (%s)\n",
1384 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1388 /* Process all image subnodes, check hashes for each */
1389 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1391 for (ndepth = 0, count = 0,
1392 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1393 (noffset >= 0) && (ndepth > 0);
1394 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1397 * Direct child node of the images parent node,
1398 * i.e. component image node.
1400 printf(" Hash(es) for Image %u (%s): ", count,
1401 fit_get_name(fit, noffset, NULL));
1404 if (!fit_image_verify(fit, noffset))
1412 #ifdef CONFIG_FIT_CIPHER
1413 static int fit_image_uncipher(const void *fit, int image_noffset,
1414 void **data, size_t *size)
1416 int cipher_noffset, ret;
1420 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1421 FIT_CIPHER_NODENAME);
1422 if (cipher_noffset < 0)
1425 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1426 *data, *size, &dst, &size_dst);
1436 #endif /* CONFIG_FIT_CIPHER */
1439 * fit_image_check_os - check whether image node is of a given os type
1440 * @fit: pointer to the FIT format image header
1441 * @noffset: component image node offset
1442 * @os: requested image os
1444 * fit_image_check_os() reads image os property and compares its numeric
1445 * id with the requested os. Comparison result is returned to the caller.
1448 * 1 if image is of given os type
1449 * 0 otherwise (or on error)
1451 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1455 if (fit_image_get_os(fit, noffset, &image_os))
1457 return (os == image_os);
1461 * fit_image_check_arch - check whether image node is of a given arch
1462 * @fit: pointer to the FIT format image header
1463 * @noffset: component image node offset
1464 * @arch: requested imagearch
1466 * fit_image_check_arch() reads image arch property and compares its numeric
1467 * id with the requested arch. Comparison result is returned to the caller.
1470 * 1 if image is of given arch
1471 * 0 otherwise (or on error)
1473 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1476 int aarch32_support = 0;
1478 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1479 aarch32_support = 1;
1482 if (fit_image_get_arch(fit, noffset, &image_arch))
1484 return (arch == image_arch) ||
1485 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1486 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1491 * fit_image_check_type - check whether image node is of a given type
1492 * @fit: pointer to the FIT format image header
1493 * @noffset: component image node offset
1494 * @type: requested image type
1496 * fit_image_check_type() reads image type property and compares its numeric
1497 * id with the requested type. Comparison result is returned to the caller.
1500 * 1 if image is of given type
1501 * 0 otherwise (or on error)
1503 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1507 if (fit_image_get_type(fit, noffset, &image_type))
1509 return (type == image_type);
1513 * fit_image_check_comp - check whether image node uses given compression
1514 * @fit: pointer to the FIT format image header
1515 * @noffset: component image node offset
1516 * @comp: requested image compression type
1518 * fit_image_check_comp() reads image compression property and compares its
1519 * numeric id with the requested compression type. Comparison result is
1520 * returned to the caller.
1523 * 1 if image uses requested compression
1524 * 0 otherwise (or on error)
1526 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1530 if (fit_image_get_comp(fit, noffset, &image_comp))
1532 return (comp == image_comp);
1536 * fit_check_format - sanity check FIT image format
1537 * @fit: pointer to the FIT format image header
1539 * fit_check_format() runs a basic sanity FIT image verification.
1540 * Routine checks for mandatory properties, nodes, etc.
1546 int fit_check_format(const void *fit)
1548 /* mandatory / node 'description' property */
1549 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1550 debug("Wrong FIT format: no description\n");
1554 if (IMAGE_ENABLE_TIMESTAMP) {
1555 /* mandatory / node 'timestamp' property */
1556 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1557 debug("Wrong FIT format: no timestamp\n");
1562 /* mandatory subimages parent '/images' node */
1563 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1564 debug("Wrong FIT format: no images parent node\n");
1573 * fit_conf_find_compat
1574 * @fit: pointer to the FIT format image header
1575 * @fdt: pointer to the device tree to compare against
1577 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1578 * most compatible with the passed in device tree.
1587 * |-o configurations
1595 * |-compatible = "foo,bar", "bim,bam"
1598 * |-compatible = "foo,bar",
1601 * |-compatible = "bim,bam", "baz,biz"
1603 * Configuration 1 would be picked because the first string in U-Boot's
1604 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1605 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1607 * As an optimization, the compatible property from the FDT's root node can be
1608 * copied into the configuration node in the FIT image. This is required to
1609 * match configurations with compressed FDTs.
1612 * offset to the configuration to use if one was found
1615 int fit_conf_find_compat(const void *fit, const void *fdt)
1618 int noffset, confs_noffset, images_noffset;
1619 const void *fdt_compat;
1621 int best_match_offset = 0;
1622 int best_match_pos = 0;
1624 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1625 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1626 if (confs_noffset < 0 || images_noffset < 0) {
1627 debug("Can't find configurations or images nodes.\n");
1631 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1633 debug("Fdt for comparison has no \"compatible\" property.\n");
1638 * Loop over the configurations in the FIT image.
1640 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1641 (noffset >= 0) && (ndepth > 0);
1642 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1644 const char *kfdt_name;
1645 int kfdt_noffset, compat_noffset;
1646 const char *cur_fdt_compat;
1654 /* If there's a compat property in the config node, use that. */
1655 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1656 fdt = fit; /* search in FIT image */
1657 compat_noffset = noffset; /* search under config node */
1658 } else { /* Otherwise extract it from the kernel FDT. */
1659 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1661 debug("No fdt property found.\n");
1664 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1666 if (kfdt_noffset < 0) {
1667 debug("No image node named \"%s\" found.\n",
1672 if (!fit_image_check_comp(fit, kfdt_noffset,
1674 debug("Can't extract compat from \"%s\" "
1675 "(compressed)\n", kfdt_name);
1679 /* search in this config's kernel FDT */
1680 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1681 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1685 compat_noffset = 0; /* search kFDT under root node */
1688 len = fdt_compat_len;
1689 cur_fdt_compat = fdt_compat;
1691 * Look for a match for each U-Boot compatibility string in
1692 * turn in the compat string property.
1694 for (i = 0; len > 0 &&
1695 (!best_match_offset || best_match_pos > i); i++) {
1696 int cur_len = strlen(cur_fdt_compat) + 1;
1698 if (!fdt_node_check_compatible(fdt, compat_noffset,
1700 best_match_offset = noffset;
1705 cur_fdt_compat += cur_len;
1708 if (!best_match_offset) {
1709 debug("No match found.\n");
1713 return best_match_offset;
1716 int fit_conf_get_node(const void *fit, const char *conf_uname)
1718 int noffset, confs_noffset;
1721 char *conf_uname_copy = NULL;
1723 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1724 if (confs_noffset < 0) {
1725 debug("Can't find configurations parent node '%s' (%s)\n",
1726 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1727 return confs_noffset;
1730 if (conf_uname == NULL) {
1731 /* get configuration unit name from the default property */
1732 debug("No configuration specified, trying default...\n");
1733 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1734 FIT_DEFAULT_PROP, &len);
1735 if (conf_uname == NULL) {
1736 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1740 debug("Found default configuration: '%s'\n", conf_uname);
1743 s = strchr(conf_uname, '#');
1745 len = s - conf_uname;
1746 conf_uname_copy = malloc(len + 1);
1747 if (!conf_uname_copy) {
1748 debug("Can't allocate uname copy: '%s'\n",
1752 memcpy(conf_uname_copy, conf_uname, len);
1753 conf_uname_copy[len] = '\0';
1754 conf_uname = conf_uname_copy;
1757 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1759 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1760 conf_uname, fdt_strerror(noffset));
1763 if (conf_uname_copy)
1764 free(conf_uname_copy);
1769 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1770 const char *prop_name)
1772 return fdt_stringlist_count(fit, noffset, prop_name);
1775 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1776 const char *prop_name, int index)
1781 /* get kernel image unit name from configuration kernel property */
1782 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1786 return fit_image_get_node(fit, uname);
1789 int fit_conf_get_prop_node(const void *fit, int noffset,
1790 const char *prop_name)
1792 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1795 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1797 fit_image_print(fit, rd_noffset, " ");
1800 puts(" Verifying Hash Integrity ... ");
1801 if (!fit_image_verify(fit, rd_noffset)) {
1802 puts("Bad Data Hash\n");
1811 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1818 debug("* %s: using config '%s' from image at 0x%08lx\n",
1819 prop_name, images->fit_uname_cfg, addr);
1821 /* Check whether configuration has this property defined */
1822 fit_hdr = map_sysmem(addr, 0);
1823 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1824 if (cfg_noffset < 0) {
1825 debug("* %s: no such config\n", prop_name);
1829 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1831 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1839 * fit_get_image_type_property() - get property name for IH_TYPE_...
1841 * @return the properly name where we expect to find the image in the
1844 static const char *fit_get_image_type_property(int type)
1847 * This is sort-of available in the uimage_type[] table in image.c
1848 * but we don't have access to the short name, and "fdt" is different
1849 * anyway. So let's just keep it here.
1852 case IH_TYPE_FLATDT:
1853 return FIT_FDT_PROP;
1854 case IH_TYPE_KERNEL:
1855 return FIT_KERNEL_PROP;
1856 case IH_TYPE_RAMDISK:
1857 return FIT_RAMDISK_PROP;
1858 case IH_TYPE_X86_SETUP:
1859 return FIT_SETUP_PROP;
1860 case IH_TYPE_LOADABLE:
1861 return FIT_LOADABLE_PROP;
1863 return FIT_FPGA_PROP;
1864 case IH_TYPE_STANDALONE:
1865 return FIT_STANDALONE_PROP;
1871 int fit_image_load(bootm_headers_t *images, ulong addr,
1872 const char **fit_unamep, const char **fit_uname_configp,
1873 int arch, int image_type, int bootstage_id,
1874 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1876 int cfg_noffset, noffset;
1877 const char *fit_uname;
1878 const char *fit_uname_config;
1879 const char *fit_base_uname_config;
1885 ulong load, load_end, data, len;
1890 const char *prop_name;
1893 fit = map_sysmem(addr, 0);
1894 fit_uname = fit_unamep ? *fit_unamep : NULL;
1895 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1896 fit_base_uname_config = NULL;
1897 prop_name = fit_get_image_type_property(image_type);
1898 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1900 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1901 if (!fit_check_format(fit)) {
1902 printf("Bad FIT %s image format!\n", prop_name);
1903 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1906 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1908 /* get FIT component image node offset */
1909 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1910 noffset = fit_image_get_node(fit, fit_uname);
1913 * no image node unit name, try to get config
1914 * node first. If config unit node name is NULL
1915 * fit_conf_get_node() will try to find default config node
1917 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1918 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1919 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1921 cfg_noffset = fit_conf_get_node(fit,
1924 if (cfg_noffset < 0) {
1925 puts("Could not find configuration node\n");
1926 bootstage_error(bootstage_id +
1927 BOOTSTAGE_SUB_NO_UNIT_NAME);
1931 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1932 printf(" Using '%s' configuration\n", fit_base_uname_config);
1933 /* Remember this config */
1934 if (image_type == IH_TYPE_KERNEL)
1935 images->fit_uname_cfg = fit_base_uname_config;
1937 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
1938 puts(" Verifying Hash Integrity ... ");
1939 if (fit_config_verify(fit, cfg_noffset)) {
1940 puts("Bad Data Hash\n");
1941 bootstage_error(bootstage_id +
1942 BOOTSTAGE_SUB_HASH);
1948 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1950 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1952 fit_uname = fit_get_name(fit, noffset, NULL);
1955 printf("Could not find subimage node type '%s'\n", prop_name);
1956 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1960 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1962 ret = fit_image_select(fit, noffset, images->verify);
1964 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1968 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1969 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1970 if (!fit_image_check_target_arch(fit, noffset)) {
1971 puts("Unsupported Architecture\n");
1972 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1978 fit_image_get_arch(fit, noffset, &os_arch);
1979 images->os.arch = os_arch;
1982 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1983 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1984 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1985 (image_type == IH_TYPE_KERNEL &&
1986 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1988 os_ok = image_type == IH_TYPE_FLATDT ||
1989 image_type == IH_TYPE_FPGA ||
1990 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1991 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1992 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
1993 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
1994 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
1997 * If either of the checks fail, we should report an error, but
1998 * if the image type is coming from the "loadables" field, we
1999 * don't care what it is
2001 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
2002 fit_image_get_os(fit, noffset, &os);
2003 printf("No %s %s %s Image\n",
2004 genimg_get_os_name(os),
2005 genimg_get_arch_name(arch),
2006 genimg_get_type_name(image_type));
2007 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2011 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2013 /* get image data address and length */
2014 if (fit_image_get_data_and_size(fit, noffset,
2015 (const void **)&buf, &size)) {
2016 printf("Could not find %s subimage data!\n", prop_name);
2017 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2021 #ifdef CONFIG_FIT_CIPHER
2022 /* Decrypt data before uncompress/move */
2023 if (IMAGE_ENABLE_DECRYPT) {
2024 puts(" Decrypting Data ... ");
2025 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2033 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
2034 /* perform any post-processing on the image data */
2035 board_fit_image_post_process(&buf, &size);
2040 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2042 data = map_to_sysmem(buf);
2044 if (load_op == FIT_LOAD_IGNORED) {
2046 } else if (fit_image_get_load(fit, noffset, &load)) {
2047 if (load_op == FIT_LOAD_REQUIRED) {
2048 printf("Can't get %s subimage load address!\n",
2050 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2053 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
2054 ulong image_start, image_end;
2057 * move image data to the load address,
2058 * make sure we don't overwrite initial image
2061 image_end = addr + fit_get_size(fit);
2063 load_end = load + len;
2064 if (image_type != IH_TYPE_KERNEL &&
2065 load < image_end && load_end > image_start) {
2066 printf("Error: %s overwritten\n", prop_name);
2070 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2071 prop_name, data, load);
2073 load = data; /* No load address specified */
2076 comp = IH_COMP_NONE;
2078 /* Kernel images get decompressed later in bootm_load_os(). */
2079 if (!fit_image_get_comp(fit, noffset, &comp) &&
2080 comp != IH_COMP_NONE &&
2081 !(image_type == IH_TYPE_KERNEL ||
2082 image_type == IH_TYPE_KERNEL_NOLOAD ||
2083 image_type == IH_TYPE_RAMDISK)) {
2084 ulong max_decomp_len = len * 20;
2086 loadbuf = malloc(max_decomp_len);
2087 load = map_to_sysmem(loadbuf);
2089 loadbuf = map_sysmem(load, max_decomp_len);
2091 if (image_decomp(comp, load, data, image_type,
2092 loadbuf, buf, len, max_decomp_len, &load_end)) {
2093 printf("Error decompressing %s\n", prop_name);
2097 len = load_end - load;
2098 } else if (load != data) {
2099 loadbuf = map_sysmem(load, len);
2100 memcpy(loadbuf, buf, len);
2103 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2104 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2105 " please fix your .its file!\n");
2107 /* verify that image data is a proper FDT blob */
2108 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2109 puts("Subimage data is not a FDT");
2113 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2118 *fit_unamep = (char *)fit_uname;
2119 if (fit_uname_configp)
2120 *fit_uname_configp = (char *)(fit_uname_config ? :
2121 fit_base_uname_config);
2126 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2127 ulong *setup_start, ulong *setup_len)
2134 addr = map_to_sysmem(images->fit_hdr_os);
2135 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2139 ret = fit_image_load(images, addr, NULL, NULL, arch,
2140 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2141 FIT_LOAD_REQUIRED, setup_start, &len);
2147 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2148 const char **fit_unamep, const char **fit_uname_configp,
2149 int arch, ulong *datap, ulong *lenp)
2151 int fdt_noffset, cfg_noffset, count;
2153 const char *fit_uname = NULL;
2154 const char *fit_uname_config = NULL;
2155 char *fit_uname_config_copy = NULL;
2156 char *next_config = NULL;
2158 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2159 ulong image_start, image_end;
2160 ulong ovload, ovlen;
2161 const char *uconfig;
2164 int i, err, noffset, ov_noffset;
2167 fit_uname = fit_unamep ? *fit_unamep : NULL;
2169 if (fit_uname_configp && *fit_uname_configp) {
2170 fit_uname_config_copy = strdup(*fit_uname_configp);
2171 if (!fit_uname_config_copy)
2174 next_config = strchr(fit_uname_config_copy, '#');
2176 *next_config++ = '\0';
2177 if (next_config - 1 > fit_uname_config_copy)
2178 fit_uname_config = fit_uname_config_copy;
2181 fdt_noffset = fit_image_load(images,
2182 addr, &fit_uname, &fit_uname_config,
2183 arch, IH_TYPE_FLATDT,
2184 BOOTSTAGE_ID_FIT_FDT_START,
2185 FIT_LOAD_OPTIONAL, &load, &len);
2187 if (fdt_noffset < 0)
2190 debug("fit_uname=%s, fit_uname_config=%s\n",
2191 fit_uname ? fit_uname : "<NULL>",
2192 fit_uname_config ? fit_uname_config : "<NULL>");
2194 fit = map_sysmem(addr, 0);
2196 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2198 /* single blob, or error just return as well */
2199 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2200 if (count <= 1 && !next_config)
2203 /* we need to apply overlays */
2205 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2207 image_end = addr + fit_get_size(fit);
2208 /* verify that relocation took place by load address not being in fit */
2209 if (load >= image_start && load < image_end) {
2210 /* check is simplified; fit load checks for overlaps */
2211 printf("Overlayed FDT requires relocation\n");
2212 fdt_noffset = -EBADF;
2216 base = map_sysmem(load, len);
2218 /* apply extra configs in FIT first, followed by args */
2219 for (i = 1; ; i++) {
2221 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2223 uname = fit_get_name(fit, noffset, NULL);
2228 uconfig = next_config;
2229 next_config = strchr(next_config, '#');
2231 *next_config++ = '\0';
2235 * fit_image_load() would load the first FDT from the
2236 * extra config only when uconfig is specified.
2237 * Check if the extra config contains multiple FDTs and
2240 cfg_noffset = fit_conf_get_node(fit, uconfig);
2243 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2247 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2249 ov_noffset = fit_image_load(images,
2250 addr, &uname, &uconfig,
2251 arch, IH_TYPE_FLATDT,
2252 BOOTSTAGE_ID_FIT_FDT_START,
2253 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2254 if (ov_noffset < 0) {
2255 printf("load of %s failed\n", uname);
2258 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2259 uname, ovload, ovlen);
2260 ov = map_sysmem(ovload, ovlen);
2262 base = map_sysmem(load, len + ovlen);
2263 err = fdt_open_into(base, base, len + ovlen);
2265 printf("failed on fdt_open_into\n");
2269 /* the verbose method prints out messages on error */
2270 err = fdt_overlay_apply_verbose(base, ov);
2276 len = fdt_totalsize(base);
2279 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2280 fdt_noffset = -EBADF;
2289 *fit_unamep = fit_uname;
2290 if (fit_uname_configp)
2291 *fit_uname_configp = fit_uname_config;
2293 if (fit_uname_config_copy)
2294 free(fit_uname_config_copy);