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>
25 DECLARE_GLOBAL_DATA_PTR;
26 #endif /* !USE_HOSTCC*/
30 #include <bootstage.h>
31 #include <u-boot/crc.h>
32 #include <u-boot/md5.h>
33 #include <u-boot/sha1.h>
34 #include <u-boot/sha256.h>
35 #include <u-boot/sha512.h>
37 /*****************************************************************************/
38 /* New uImage format routines */
39 /*****************************************************************************/
41 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
42 ulong *addr, const char **name)
49 sep = strchr(spec, sepc);
52 *addr = simple_strtoul(spec, NULL, 16);
62 * fit_parse_conf - parse FIT configuration spec
63 * @spec: input string, containing configuration spec
64 * @add_curr: current image address (to be used as a possible default)
65 * @addr: pointer to a ulong variable, will hold FIT image address of a given
67 * @conf_name double pointer to a char, will hold pointer to a configuration
70 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
71 * where <addr> is a FIT image address that contains configuration
72 * with a <conf> unit name.
74 * Address part is optional, and if omitted default add_curr will
78 * 1 if spec is a valid configuration string,
79 * addr and conf_name are set accordingly
82 int fit_parse_conf(const char *spec, ulong addr_curr,
83 ulong *addr, const char **conf_name)
85 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
89 * fit_parse_subimage - parse FIT subimage spec
90 * @spec: input string, containing subimage spec
91 * @add_curr: current image address (to be used as a possible default)
92 * @addr: pointer to a ulong variable, will hold FIT image address of a given
94 * @image_name: double pointer to a char, will hold pointer to a subimage name
96 * fit_parse_subimage() expects subimage spec in the form of
97 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
98 * subimage with a <subimg> unit name.
100 * Address part is optional, and if omitted default add_curr will
104 * 1 if spec is a valid subimage string,
105 * addr and image_name are set accordingly
108 int fit_parse_subimage(const char *spec, ulong addr_curr,
109 ulong *addr, const char **image_name)
111 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
113 #endif /* !USE_HOSTCC */
115 static void fit_get_debug(const void *fit, int noffset,
116 char *prop_name, int err)
118 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
119 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
124 * fit_get_subimage_count - get component (sub-image) count
125 * @fit: pointer to the FIT format image header
126 * @images_noffset: offset of images node
129 * number of image components
131 int fit_get_subimage_count(const void *fit, int images_noffset)
137 /* Process its subnodes, print out component images details */
138 for (ndepth = 0, count = 0,
139 noffset = fdt_next_node(fit, images_noffset, &ndepth);
140 (noffset >= 0) && (ndepth > 0);
141 noffset = fdt_next_node(fit, noffset, &ndepth)) {
150 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
152 * fit_image_print_data() - prints out the hash node details
153 * @fit: pointer to the FIT format image header
154 * @noffset: offset of the hash node
155 * @p: pointer to prefix string
156 * @type: Type of information to print ("hash" or "sign")
158 * fit_image_print_data() lists properties for the processed hash node
160 * This function avoid using puts() since it prints a newline on the host
161 * but does not in U-Boot.
164 * no returned results
166 static void fit_image_print_data(const void *fit, int noffset, const char *p,
177 debug("%s %s node: '%s'\n", p, type,
178 fit_get_name(fit, noffset, NULL));
179 printf("%s %s algo: ", p, type);
180 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
181 printf("invalid/unsupported\n");
185 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
186 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
188 printf(":%s", keyname);
190 printf(" (required)");
193 padding = fdt_getprop(fit, noffset, "padding", NULL);
195 printf("%s %s padding: %s\n", p, type, padding);
197 ret = fit_image_hash_get_value(fit, noffset, &value,
199 printf("%s %s value: ", p, type);
201 printf("unavailable\n");
203 for (i = 0; i < value_len; i++)
204 printf("%02x", value[i]);
208 debug("%s %s len: %d\n", p, type, value_len);
210 /* Signatures have a time stamp */
211 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
214 printf("%s Timestamp: ", p);
215 if (fit_get_timestamp(fit, noffset, ×tamp))
216 printf("unavailable\n");
218 genimg_print_time(timestamp);
223 * fit_image_print_verification_data() - prints out the hash/signature details
224 * @fit: pointer to the FIT format image header
225 * @noffset: offset of the hash or signature node
226 * @p: pointer to prefix string
228 * This lists properties for the processed hash node
231 * no returned results
233 static void fit_image_print_verification_data(const void *fit, int noffset,
239 * Check subnode name, must be equal to "hash" or "signature".
240 * Multiple hash/signature nodes require unique unit node
241 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
243 name = fit_get_name(fit, noffset, NULL);
244 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
245 fit_image_print_data(fit, noffset, p, "Hash");
246 } else if (!strncmp(name, FIT_SIG_NODENAME,
247 strlen(FIT_SIG_NODENAME))) {
248 fit_image_print_data(fit, noffset, p, "Sign");
253 * fit_conf_print - prints out the FIT configuration details
254 * @fit: pointer to the FIT format image header
255 * @noffset: offset of the configuration node
256 * @p: pointer to prefix string
258 * fit_conf_print() lists all mandatory properties for the processed
259 * configuration node.
262 * no returned results
264 static void fit_conf_print(const void *fit, int noffset, const char *p)
269 int fdt_index, loadables_index;
272 /* Mandatory properties */
273 ret = fit_get_desc(fit, noffset, &desc);
274 printf("%s Description: ", p);
276 printf("unavailable\n");
278 printf("%s\n", desc);
280 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
281 printf("%s Kernel: ", p);
283 printf("unavailable\n");
285 printf("%s\n", uname);
287 /* Optional properties */
288 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
290 printf("%s Init Ramdisk: %s\n", p, uname);
292 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
294 printf("%s Firmware: %s\n", p, uname);
297 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
298 fdt_index, NULL), uname;
301 printf("%s FDT: ", p);
304 printf("%s\n", uname);
307 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
309 printf("%s FPGA: %s\n", p, uname);
311 /* Print out all of the specified loadables */
312 for (loadables_index = 0;
313 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
314 loadables_index, NULL), uname;
316 if (loadables_index == 0) {
317 printf("%s Loadables: ", p);
321 printf("%s\n", uname);
324 /* Process all hash subnodes of the component configuration node */
325 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
326 (noffset >= 0) && (ndepth > 0);
327 noffset = fdt_next_node(fit, noffset, &ndepth)) {
329 /* Direct child node of the component configuration node */
330 fit_image_print_verification_data(fit, noffset, p);
336 * fit_print_contents - prints out the contents of the FIT format image
337 * @fit: pointer to the FIT format image header
338 * @p: pointer to prefix string
340 * fit_print_contents() formats a multi line FIT image contents description.
341 * The routine prints out FIT image properties (root node level) followed by
342 * the details of each component image.
345 * no returned results
347 void fit_print_contents(const void *fit)
360 /* Indent string is defined in header image.h */
361 p = IMAGE_INDENT_STRING;
363 /* Root node properties */
364 ret = fit_get_desc(fit, 0, &desc);
365 printf("%sFIT description: ", p);
367 printf("unavailable\n");
369 printf("%s\n", desc);
371 if (IMAGE_ENABLE_TIMESTAMP) {
372 ret = fit_get_timestamp(fit, 0, ×tamp);
373 printf("%sCreated: ", p);
375 printf("unavailable\n");
377 genimg_print_time(timestamp);
380 /* Find images parent node offset */
381 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
382 if (images_noffset < 0) {
383 printf("Can't find images parent node '%s' (%s)\n",
384 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
388 /* Process its subnodes, print out component images details */
389 for (ndepth = 0, count = 0,
390 noffset = fdt_next_node(fit, images_noffset, &ndepth);
391 (noffset >= 0) && (ndepth > 0);
392 noffset = fdt_next_node(fit, noffset, &ndepth)) {
395 * Direct child node of the images parent node,
396 * i.e. component image node.
398 printf("%s Image %u (%s)\n", p, count++,
399 fit_get_name(fit, noffset, NULL));
401 fit_image_print(fit, noffset, p);
405 /* Find configurations parent node offset */
406 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
407 if (confs_noffset < 0) {
408 debug("Can't get configurations parent node '%s' (%s)\n",
409 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
413 /* get default configuration unit name from default property */
414 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
416 printf("%s Default Configuration: '%s'\n", p, uname);
418 /* Process its subnodes, print out configurations details */
419 for (ndepth = 0, count = 0,
420 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
421 (noffset >= 0) && (ndepth > 0);
422 noffset = fdt_next_node(fit, noffset, &ndepth)) {
425 * Direct child node of the configurations parent node,
426 * i.e. configuration node.
428 printf("%s Configuration %u (%s)\n", p, count++,
429 fit_get_name(fit, noffset, NULL));
431 fit_conf_print(fit, noffset, p);
437 * fit_image_print - prints out the FIT component image details
438 * @fit: pointer to the FIT format image header
439 * @image_noffset: offset of the component image node
440 * @p: pointer to prefix string
442 * fit_image_print() lists all mandatory properties for the processed component
443 * image. If present, hash nodes are printed out as well. Load
444 * address for images of type firmware is also printed out. Since the load
445 * address is not mandatory for firmware images, it will be output as
446 * "unavailable" when not present.
449 * no returned results
451 void fit_image_print(const void *fit, int image_noffset, const char *p)
454 uint8_t type, arch, os, comp;
462 /* Mandatory properties */
463 ret = fit_get_desc(fit, image_noffset, &desc);
464 printf("%s Description: ", p);
466 printf("unavailable\n");
468 printf("%s\n", desc);
470 if (IMAGE_ENABLE_TIMESTAMP) {
473 ret = fit_get_timestamp(fit, 0, ×tamp);
474 printf("%s Created: ", p);
476 printf("unavailable\n");
478 genimg_print_time(timestamp);
481 fit_image_get_type(fit, image_noffset, &type);
482 printf("%s Type: %s\n", p, genimg_get_type_name(type));
484 fit_image_get_comp(fit, image_noffset, &comp);
485 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
487 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
490 printf("%s Data Start: ", p);
492 printf("unavailable\n");
494 void *vdata = (void *)data;
496 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
500 printf("%s Data Size: ", p);
502 printf("unavailable\n");
504 genimg_print_size(size);
506 /* Remaining, type dependent properties */
507 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
508 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
509 (type == IH_TYPE_FLATDT)) {
510 fit_image_get_arch(fit, image_noffset, &arch);
511 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
514 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
515 (type == IH_TYPE_FIRMWARE)) {
516 fit_image_get_os(fit, image_noffset, &os);
517 printf("%s OS: %s\n", p, genimg_get_os_name(os));
520 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
521 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
522 (type == IH_TYPE_FPGA)) {
523 ret = fit_image_get_load(fit, image_noffset, &load);
524 printf("%s Load Address: ", p);
526 printf("unavailable\n");
528 printf("0x%08lx\n", load);
531 /* optional load address for FDT */
532 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
533 printf("%s Load Address: 0x%08lx\n", p, load);
535 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
536 (type == IH_TYPE_RAMDISK)) {
537 ret = fit_image_get_entry(fit, image_noffset, &entry);
538 printf("%s Entry Point: ", p);
540 printf("unavailable\n");
542 printf("0x%08lx\n", entry);
545 /* Process all hash subnodes of the component image node */
546 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
547 (noffset >= 0) && (ndepth > 0);
548 noffset = fdt_next_node(fit, noffset, &ndepth)) {
550 /* Direct child node of the component image node */
551 fit_image_print_verification_data(fit, noffset, p);
556 void fit_print_contents(const void *fit) { }
557 void fit_image_print(const void *fit, int image_noffset, const char *p) { }
558 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
561 * fit_get_desc - get node description property
562 * @fit: pointer to the FIT format image header
563 * @noffset: node offset
564 * @desc: double pointer to the char, will hold pointer to the description
566 * fit_get_desc() reads description property from a given node, if
567 * description is found pointer to it is returned in third call argument.
573 int fit_get_desc(const void *fit, int noffset, char **desc)
577 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
579 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
587 * fit_get_timestamp - get node timestamp property
588 * @fit: pointer to the FIT format image header
589 * @noffset: node offset
590 * @timestamp: pointer to the time_t, will hold read timestamp
592 * fit_get_timestamp() reads timestamp property from given node, if timestamp
593 * is found and has a correct size its value is returned in third call
598 * -1, on property read failure
599 * -2, on wrong timestamp size
601 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
606 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
608 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
611 if (len != sizeof(uint32_t)) {
612 debug("FIT timestamp with incorrect size of (%u)\n", len);
616 *timestamp = uimage_to_cpu(*((uint32_t *)data));
621 * fit_image_get_node - get node offset for component image of a given unit name
622 * @fit: pointer to the FIT format image header
623 * @image_uname: component image node unit name
625 * fit_image_get_node() finds a component image (within the '/images'
626 * node) of a provided unit name. If image is found its node offset is
627 * returned to the caller.
630 * image node offset when found (>=0)
631 * negative number on failure (FDT_ERR_* code)
633 int fit_image_get_node(const void *fit, const char *image_uname)
635 int noffset, images_noffset;
637 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
638 if (images_noffset < 0) {
639 debug("Can't find images parent node '%s' (%s)\n",
640 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
641 return images_noffset;
644 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
646 debug("Can't get node offset for image unit name: '%s' (%s)\n",
647 image_uname, fdt_strerror(noffset));
654 * fit_image_get_os - get os id for a given component image node
655 * @fit: pointer to the FIT format image header
656 * @noffset: component image node offset
657 * @os: pointer to the uint8_t, will hold os numeric id
659 * fit_image_get_os() finds os property in a given component image node.
660 * If the property is found, its (string) value is translated to the numeric
661 * id which is returned to the caller.
667 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
672 /* Get OS name from property data */
673 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
675 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
680 /* Translate OS name to id */
681 *os = genimg_get_os_id(data);
686 * fit_image_get_arch - get arch id for a given component image node
687 * @fit: pointer to the FIT format image header
688 * @noffset: component image node offset
689 * @arch: pointer to the uint8_t, will hold arch numeric id
691 * fit_image_get_arch() finds arch property in a given component image node.
692 * If the property is found, its (string) value is translated to the numeric
693 * id which is returned to the caller.
699 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
704 /* Get architecture name from property data */
705 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
707 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
712 /* Translate architecture name to id */
713 *arch = genimg_get_arch_id(data);
718 * fit_image_get_type - get type id for a given component image node
719 * @fit: pointer to the FIT format image header
720 * @noffset: component image node offset
721 * @type: pointer to the uint8_t, will hold type numeric id
723 * fit_image_get_type() finds type property in a given component image node.
724 * If the property is found, its (string) value is translated to the numeric
725 * id which is returned to the caller.
731 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
736 /* Get image type name from property data */
737 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
739 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
744 /* Translate image type name to id */
745 *type = genimg_get_type_id(data);
750 * fit_image_get_comp - get comp id for a given component image node
751 * @fit: pointer to the FIT format image header
752 * @noffset: component image node offset
753 * @comp: pointer to the uint8_t, will hold comp numeric id
755 * fit_image_get_comp() finds comp property in a given component image node.
756 * If the property is found, its (string) value is translated to the numeric
757 * id which is returned to the caller.
763 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
768 /* Get compression name from property data */
769 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
771 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
776 /* Translate compression name to id */
777 *comp = genimg_get_comp_id(data);
781 static int fit_image_get_address(const void *fit, int noffset, char *name,
788 cell = fdt_getprop(fit, noffset, name, &len);
790 fit_get_debug(fit, noffset, name, len);
795 /* Use load64 to avoid compiling warning for 32-bit target */
797 load64 = (load64 << 32) | uimage_to_cpu(*cell);
801 if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) {
802 printf("Unsupported %s address size\n", name);
806 *load = (ulong)load64;
811 * fit_image_get_load() - get load addr property for given component image node
812 * @fit: pointer to the FIT format image header
813 * @noffset: component image node offset
814 * @load: pointer to the uint32_t, will hold load address
816 * fit_image_get_load() finds load address property in a given component
817 * image node. If the property is found, its value is returned to the caller.
823 int fit_image_get_load(const void *fit, int noffset, ulong *load)
825 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
829 * fit_image_get_entry() - get entry point address property
830 * @fit: pointer to the FIT format image header
831 * @noffset: component image node offset
832 * @entry: pointer to the uint32_t, will hold entry point address
834 * This gets the entry point address property for a given component image
837 * fit_image_get_entry() finds entry point address property in a given
838 * component image node. If the property is found, its value is returned
845 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
847 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
851 * fit_image_get_data - get data property and its size for a given component image node
852 * @fit: pointer to the FIT format image header
853 * @noffset: component image node offset
854 * @data: double pointer to void, will hold data property's data address
855 * @size: pointer to size_t, will hold data property's data size
857 * fit_image_get_data() finds data property in a given component image node.
858 * If the property is found its data start address and size are returned to
865 int fit_image_get_data(const void *fit, int noffset,
866 const void **data, size_t *size)
870 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
872 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
882 * Get 'data-offset' property from a given image node.
884 * @fit: pointer to the FIT image header
885 * @noffset: component image node offset
886 * @data_offset: holds the data-offset property
890 * -ENOENT if the property could not be found
892 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
896 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
900 *data_offset = fdt32_to_cpu(*val);
906 * Get 'data-position' property from a given image node.
908 * @fit: pointer to the FIT image header
909 * @noffset: component image node offset
910 * @data_position: holds the data-position property
914 * -ENOENT if the property could not be found
916 int fit_image_get_data_position(const void *fit, int noffset,
921 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
925 *data_position = fdt32_to_cpu(*val);
931 * Get 'data-size' property from a given image node.
933 * @fit: pointer to the FIT image header
934 * @noffset: component image node offset
935 * @data_size: holds the data-size property
939 * -ENOENT if the property could not be found
941 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
945 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
949 *data_size = fdt32_to_cpu(*val);
955 * Get 'data-size-unciphered' property from a given image node.
957 * @fit: pointer to the FIT image header
958 * @noffset: component image node offset
959 * @data_size: holds the data-size property
963 * -ENOENT if the property could not be found
965 int fit_image_get_data_size_unciphered(const void *fit, int noffset,
970 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
974 *data_size = (size_t)fdt32_to_cpu(*val);
980 * fit_image_get_data_and_size - get data and its size including
981 * both embedded and external data
982 * @fit: pointer to the FIT format image header
983 * @noffset: component image node offset
984 * @data: double pointer to void, will hold data property's data address
985 * @size: pointer to size_t, will hold data property's data size
987 * fit_image_get_data_and_size() finds data and its size including
988 * both embedded and external data. If the property is found
989 * its data start address and size are returned to the caller.
993 * otherwise, on failure
995 int fit_image_get_data_and_size(const void *fit, int noffset,
996 const void **data, size_t *size)
998 bool external_data = false;
1003 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1004 external_data = true;
1005 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1006 external_data = true;
1008 * For FIT with external data, figure out where
1009 * the external images start. This is the base
1010 * for the data-offset properties in each image.
1012 offset += ((fdt_totalsize(fit) + 3) & ~3);
1015 if (external_data) {
1016 debug("External Data\n");
1017 ret = fit_image_get_data_size(fit, noffset, &len);
1019 *data = fit + offset;
1023 ret = fit_image_get_data(fit, noffset, data, size);
1030 * fit_image_hash_get_algo - get hash algorithm name
1031 * @fit: pointer to the FIT format image header
1032 * @noffset: hash node offset
1033 * @algo: double pointer to char, will hold pointer to the algorithm name
1035 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1036 * If the property is found its data start address is returned to the caller.
1042 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1046 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1047 if (*algo == NULL) {
1048 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1056 * fit_image_hash_get_value - get hash value and length
1057 * @fit: pointer to the FIT format image header
1058 * @noffset: hash node offset
1059 * @value: double pointer to uint8_t, will hold address of a hash value data
1060 * @value_len: pointer to an int, will hold hash data length
1062 * fit_image_hash_get_value() finds hash value property in a given hash node.
1063 * If the property is found its data start address and size are returned to
1070 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1075 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1076 if (*value == NULL) {
1077 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1087 * fit_image_hash_get_ignore - get hash ignore flag
1088 * @fit: pointer to the FIT format image header
1089 * @noffset: hash node offset
1090 * @ignore: pointer to an int, will hold hash ignore flag
1092 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1093 * If the property is found and non-zero, the hash algorithm is not verified by
1094 * u-boot automatically.
1097 * 0, on ignore not found
1098 * value, on ignore found
1100 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1105 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1106 if (value == NULL || len != sizeof(int))
1115 * fit_image_cipher_get_algo - get cipher algorithm name
1116 * @fit: pointer to the FIT format image header
1117 * @noffset: cipher node offset
1118 * @algo: double pointer to char, will hold pointer to the algorithm name
1120 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1121 * cipher node. If the property is found its data start address is returned
1128 int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1132 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1134 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1141 ulong fit_get_end(const void *fit)
1143 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1147 * fit_set_timestamp - set node timestamp property
1148 * @fit: pointer to the FIT format image header
1149 * @noffset: node offset
1150 * @timestamp: timestamp value to be set
1152 * fit_set_timestamp() attempts to set timestamp property in the requested
1153 * node and returns operation status to the caller.
1157 * -ENOSPC if no space in device tree, -1 for other error
1159 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1164 t = cpu_to_uimage(timestamp);
1165 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1168 debug("Can't set '%s' property for '%s' node (%s)\n",
1169 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1171 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1178 * calculate_hash - calculate and return hash for provided input data
1179 * @data: pointer to the input data
1180 * @data_len: data length
1181 * @algo: requested hash algorithm
1182 * @value: pointer to the char, will hold hash value data (caller must
1183 * allocate enough free space)
1184 * value_len: length of the calculated hash
1186 * calculate_hash() computes input data hash according to the requested
1188 * Resulting hash value is placed in caller provided 'value' buffer, length
1189 * of the calculated hash is returned via value_len pointer argument.
1193 * -1, when algo is unsupported
1195 int calculate_hash(const void *data, int data_len, const char *algo,
1196 uint8_t *value, int *value_len)
1198 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1199 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1201 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1203 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1204 sha1_csum_wd((unsigned char *)data, data_len,
1205 (unsigned char *)value, CHUNKSZ_SHA1);
1207 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1208 sha256_csum_wd((unsigned char *)data, data_len,
1209 (unsigned char *)value, CHUNKSZ_SHA256);
1210 *value_len = SHA256_SUM_LEN;
1211 } else if (IMAGE_ENABLE_SHA384 && strcmp(algo, "sha384") == 0) {
1212 sha384_csum_wd((unsigned char *)data, data_len,
1213 (unsigned char *)value, CHUNKSZ_SHA384);
1214 *value_len = SHA384_SUM_LEN;
1215 } else if (IMAGE_ENABLE_SHA512 && strcmp(algo, "sha512") == 0) {
1216 sha512_csum_wd((unsigned char *)data, data_len,
1217 (unsigned char *)value, CHUNKSZ_SHA512);
1218 *value_len = SHA512_SUM_LEN;
1219 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1220 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1223 debug("Unsupported hash alogrithm\n");
1229 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1230 size_t size, char **err_msgp)
1232 uint8_t value[FIT_MAX_HASH_LEN];
1241 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1242 *err_msgp = "Can't get hash algo property";
1247 if (IMAGE_ENABLE_IGNORE) {
1248 fit_image_hash_get_ignore(fit, noffset, &ignore);
1250 printf("-skipped ");
1255 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1257 *err_msgp = "Can't get hash value property";
1261 if (calculate_hash(data, size, algo, value, &value_len)) {
1262 *err_msgp = "Unsupported hash algorithm";
1266 if (value_len != fit_value_len) {
1267 *err_msgp = "Bad hash value len";
1269 } else if (memcmp(value, fit_value, value_len) != 0) {
1270 *err_msgp = "Bad hash value";
1277 int fit_image_verify_with_data(const void *fit, int image_noffset,
1278 const void *data, size_t size)
1285 /* Verify all required signatures */
1286 if (FIT_IMAGE_ENABLE_VERIFY &&
1287 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1288 gd_fdt_blob(), &verify_all)) {
1289 err_msg = "Unable to verify required signature";
1293 /* Process all hash subnodes of the component image node */
1294 fdt_for_each_subnode(noffset, fit, image_noffset) {
1295 const char *name = fit_get_name(fit, noffset, NULL);
1298 * Check subnode name, must be equal to "hash".
1299 * Multiple hash nodes require unique unit node
1300 * names, e.g. hash-1, hash-2, etc.
1302 if (!strncmp(name, FIT_HASH_NODENAME,
1303 strlen(FIT_HASH_NODENAME))) {
1304 if (fit_image_check_hash(fit, noffset, data, size,
1308 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
1309 !strncmp(name, FIT_SIG_NODENAME,
1310 strlen(FIT_SIG_NODENAME))) {
1311 ret = fit_image_check_sig(fit, noffset, data,
1312 size, -1, &err_msg);
1315 * Show an indication on failure, but do not return
1316 * an error. Only keys marked 'required' can cause
1317 * an image validation failure. See the call to
1318 * fit_image_verify_required_sigs() above.
1327 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1328 err_msg = "Corrupted or truncated tree";
1335 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1336 err_msg, fit_get_name(fit, noffset, NULL),
1337 fit_get_name(fit, image_noffset, NULL));
1342 * fit_image_verify - verify data integrity
1343 * @fit: pointer to the FIT format image header
1344 * @image_noffset: component image node offset
1346 * fit_image_verify() goes over component image hash nodes,
1347 * re-calculates each data hash and compares with the value stored in hash
1351 * 1, if all hashes are valid
1352 * 0, otherwise (or on error)
1354 int fit_image_verify(const void *fit, int image_noffset)
1361 /* Get image data and data length */
1362 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1363 err_msg = "Can't get image data/size";
1364 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1365 err_msg, fit_get_name(fit, noffset, NULL),
1366 fit_get_name(fit, image_noffset, NULL));
1370 return fit_image_verify_with_data(fit, image_noffset, data, size);
1374 * fit_all_image_verify - verify data integrity for all images
1375 * @fit: pointer to the FIT format image header
1377 * fit_all_image_verify() goes over all images in the FIT and
1378 * for every images checks if all it's hashes are valid.
1381 * 1, if all hashes of all images are valid
1382 * 0, otherwise (or on error)
1384 int fit_all_image_verify(const void *fit)
1391 /* Find images parent node offset */
1392 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1393 if (images_noffset < 0) {
1394 printf("Can't find images parent node '%s' (%s)\n",
1395 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1399 /* Process all image subnodes, check hashes for each */
1400 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1402 for (ndepth = 0, count = 0,
1403 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1404 (noffset >= 0) && (ndepth > 0);
1405 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1408 * Direct child node of the images parent node,
1409 * i.e. component image node.
1411 printf(" Hash(es) for Image %u (%s): ", count,
1412 fit_get_name(fit, noffset, NULL));
1415 if (!fit_image_verify(fit, noffset))
1423 #ifdef CONFIG_FIT_CIPHER
1424 static int fit_image_uncipher(const void *fit, int image_noffset,
1425 void **data, size_t *size)
1427 int cipher_noffset, ret;
1431 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1432 FIT_CIPHER_NODENAME);
1433 if (cipher_noffset < 0)
1436 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1437 *data, *size, &dst, &size_dst);
1447 #endif /* CONFIG_FIT_CIPHER */
1450 * fit_image_check_os - check whether image node is of a given os type
1451 * @fit: pointer to the FIT format image header
1452 * @noffset: component image node offset
1453 * @os: requested image os
1455 * fit_image_check_os() reads image os property and compares its numeric
1456 * id with the requested os. Comparison result is returned to the caller.
1459 * 1 if image is of given os type
1460 * 0 otherwise (or on error)
1462 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1466 if (fit_image_get_os(fit, noffset, &image_os))
1468 return (os == image_os);
1472 * fit_image_check_arch - check whether image node is of a given arch
1473 * @fit: pointer to the FIT format image header
1474 * @noffset: component image node offset
1475 * @arch: requested imagearch
1477 * fit_image_check_arch() reads image arch property and compares its numeric
1478 * id with the requested arch. Comparison result is returned to the caller.
1481 * 1 if image is of given arch
1482 * 0 otherwise (or on error)
1484 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1487 int aarch32_support = 0;
1489 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1490 aarch32_support = 1;
1493 if (fit_image_get_arch(fit, noffset, &image_arch))
1495 return (arch == image_arch) ||
1496 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1497 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1502 * fit_image_check_type - check whether image node is of a given type
1503 * @fit: pointer to the FIT format image header
1504 * @noffset: component image node offset
1505 * @type: requested image type
1507 * fit_image_check_type() reads image type property and compares its numeric
1508 * id with the requested type. Comparison result is returned to the caller.
1511 * 1 if image is of given type
1512 * 0 otherwise (or on error)
1514 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1518 if (fit_image_get_type(fit, noffset, &image_type))
1520 return (type == image_type);
1524 * fit_image_check_comp - check whether image node uses given compression
1525 * @fit: pointer to the FIT format image header
1526 * @noffset: component image node offset
1527 * @comp: requested image compression type
1529 * fit_image_check_comp() reads image compression property and compares its
1530 * numeric id with the requested compression type. Comparison result is
1531 * returned to the caller.
1534 * 1 if image uses requested compression
1535 * 0 otherwise (or on error)
1537 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1541 if (fit_image_get_comp(fit, noffset, &image_comp))
1543 return (comp == image_comp);
1547 * fit_check_format - sanity check FIT image format
1548 * @fit: pointer to the FIT format image header
1550 * fit_check_format() runs a basic sanity FIT image verification.
1551 * Routine checks for mandatory properties, nodes, etc.
1557 int fit_check_format(const void *fit)
1559 /* mandatory / node 'description' property */
1560 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1561 debug("Wrong FIT format: no description\n");
1565 if (IMAGE_ENABLE_TIMESTAMP) {
1566 /* mandatory / node 'timestamp' property */
1567 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1568 debug("Wrong FIT format: no timestamp\n");
1573 /* mandatory subimages parent '/images' node */
1574 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1575 debug("Wrong FIT format: no images parent node\n");
1584 * fit_conf_find_compat
1585 * @fit: pointer to the FIT format image header
1586 * @fdt: pointer to the device tree to compare against
1588 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1589 * most compatible with the passed in device tree.
1598 * |-o configurations
1606 * |-compatible = "foo,bar", "bim,bam"
1609 * |-compatible = "foo,bar",
1612 * |-compatible = "bim,bam", "baz,biz"
1614 * Configuration 1 would be picked because the first string in U-Boot's
1615 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1616 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1618 * As an optimization, the compatible property from the FDT's root node can be
1619 * copied into the configuration node in the FIT image. This is required to
1620 * match configurations with compressed FDTs.
1623 * offset to the configuration to use if one was found
1626 int fit_conf_find_compat(const void *fit, const void *fdt)
1629 int noffset, confs_noffset, images_noffset;
1630 const void *fdt_compat;
1632 int best_match_offset = 0;
1633 int best_match_pos = 0;
1635 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1636 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1637 if (confs_noffset < 0 || images_noffset < 0) {
1638 debug("Can't find configurations or images nodes.\n");
1642 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1644 debug("Fdt for comparison has no \"compatible\" property.\n");
1649 * Loop over the configurations in the FIT image.
1651 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1652 (noffset >= 0) && (ndepth > 0);
1653 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1655 const char *kfdt_name;
1656 int kfdt_noffset, compat_noffset;
1657 const char *cur_fdt_compat;
1665 /* If there's a compat property in the config node, use that. */
1666 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1667 fdt = fit; /* search in FIT image */
1668 compat_noffset = noffset; /* search under config node */
1669 } else { /* Otherwise extract it from the kernel FDT. */
1670 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1672 debug("No fdt property found.\n");
1675 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1677 if (kfdt_noffset < 0) {
1678 debug("No image node named \"%s\" found.\n",
1683 if (!fit_image_check_comp(fit, kfdt_noffset,
1685 debug("Can't extract compat from \"%s\" "
1686 "(compressed)\n", kfdt_name);
1690 /* search in this config's kernel FDT */
1691 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1692 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1696 compat_noffset = 0; /* search kFDT under root node */
1699 len = fdt_compat_len;
1700 cur_fdt_compat = fdt_compat;
1702 * Look for a match for each U-Boot compatibility string in
1703 * turn in the compat string property.
1705 for (i = 0; len > 0 &&
1706 (!best_match_offset || best_match_pos > i); i++) {
1707 int cur_len = strlen(cur_fdt_compat) + 1;
1709 if (!fdt_node_check_compatible(fdt, compat_noffset,
1711 best_match_offset = noffset;
1716 cur_fdt_compat += cur_len;
1719 if (!best_match_offset) {
1720 debug("No match found.\n");
1724 return best_match_offset;
1727 int fit_conf_get_node(const void *fit, const char *conf_uname)
1729 int noffset, confs_noffset;
1732 char *conf_uname_copy = NULL;
1734 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1735 if (confs_noffset < 0) {
1736 debug("Can't find configurations parent node '%s' (%s)\n",
1737 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1738 return confs_noffset;
1741 if (conf_uname == NULL) {
1742 /* get configuration unit name from the default property */
1743 debug("No configuration specified, trying default...\n");
1744 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1745 FIT_DEFAULT_PROP, &len);
1746 if (conf_uname == NULL) {
1747 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1751 debug("Found default configuration: '%s'\n", conf_uname);
1754 s = strchr(conf_uname, '#');
1756 len = s - conf_uname;
1757 conf_uname_copy = malloc(len + 1);
1758 if (!conf_uname_copy) {
1759 debug("Can't allocate uname copy: '%s'\n",
1763 memcpy(conf_uname_copy, conf_uname, len);
1764 conf_uname_copy[len] = '\0';
1765 conf_uname = conf_uname_copy;
1768 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1770 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1771 conf_uname, fdt_strerror(noffset));
1774 if (conf_uname_copy)
1775 free(conf_uname_copy);
1780 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1781 const char *prop_name)
1783 return fdt_stringlist_count(fit, noffset, prop_name);
1786 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1787 const char *prop_name, int index)
1792 /* get kernel image unit name from configuration kernel property */
1793 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1797 return fit_image_get_node(fit, uname);
1800 int fit_conf_get_prop_node(const void *fit, int noffset,
1801 const char *prop_name)
1803 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1806 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1808 fit_image_print(fit, rd_noffset, " ");
1811 puts(" Verifying Hash Integrity ... ");
1812 if (!fit_image_verify(fit, rd_noffset)) {
1813 puts("Bad Data Hash\n");
1822 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1829 debug("* %s: using config '%s' from image at 0x%08lx\n",
1830 prop_name, images->fit_uname_cfg, addr);
1832 /* Check whether configuration has this property defined */
1833 fit_hdr = map_sysmem(addr, 0);
1834 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1835 if (cfg_noffset < 0) {
1836 debug("* %s: no such config\n", prop_name);
1840 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1842 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1850 * fit_get_image_type_property() - get property name for IH_TYPE_...
1852 * @return the properly name where we expect to find the image in the
1855 static const char *fit_get_image_type_property(int type)
1858 * This is sort-of available in the uimage_type[] table in image.c
1859 * but we don't have access to the short name, and "fdt" is different
1860 * anyway. So let's just keep it here.
1863 case IH_TYPE_FLATDT:
1864 return FIT_FDT_PROP;
1865 case IH_TYPE_KERNEL:
1866 return FIT_KERNEL_PROP;
1867 case IH_TYPE_RAMDISK:
1868 return FIT_RAMDISK_PROP;
1869 case IH_TYPE_X86_SETUP:
1870 return FIT_SETUP_PROP;
1871 case IH_TYPE_LOADABLE:
1872 return FIT_LOADABLE_PROP;
1874 return FIT_FPGA_PROP;
1875 case IH_TYPE_STANDALONE:
1876 return FIT_STANDALONE_PROP;
1882 int fit_image_load(bootm_headers_t *images, ulong addr,
1883 const char **fit_unamep, const char **fit_uname_configp,
1884 int arch, int image_type, int bootstage_id,
1885 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1887 int cfg_noffset, noffset;
1888 const char *fit_uname;
1889 const char *fit_uname_config;
1890 const char *fit_base_uname_config;
1896 ulong load, load_end, data, len;
1901 const char *prop_name;
1904 fit = map_sysmem(addr, 0);
1905 fit_uname = fit_unamep ? *fit_unamep : NULL;
1906 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1907 fit_base_uname_config = NULL;
1908 prop_name = fit_get_image_type_property(image_type);
1909 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1911 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1912 if (!fit_check_format(fit)) {
1913 printf("Bad FIT %s image format!\n", prop_name);
1914 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1917 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1919 /* get FIT component image node offset */
1920 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1921 noffset = fit_image_get_node(fit, fit_uname);
1924 * no image node unit name, try to get config
1925 * node first. If config unit node name is NULL
1926 * fit_conf_get_node() will try to find default config node
1928 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1929 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1930 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1932 cfg_noffset = fit_conf_get_node(fit,
1935 if (cfg_noffset < 0) {
1936 puts("Could not find configuration node\n");
1937 bootstage_error(bootstage_id +
1938 BOOTSTAGE_SUB_NO_UNIT_NAME);
1942 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1943 printf(" Using '%s' configuration\n", fit_base_uname_config);
1944 /* Remember this config */
1945 if (image_type == IH_TYPE_KERNEL)
1946 images->fit_uname_cfg = fit_base_uname_config;
1948 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
1949 puts(" Verifying Hash Integrity ... ");
1950 if (fit_config_verify(fit, cfg_noffset)) {
1951 puts("Bad Data Hash\n");
1952 bootstage_error(bootstage_id +
1953 BOOTSTAGE_SUB_HASH);
1959 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1961 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1963 fit_uname = fit_get_name(fit, noffset, NULL);
1966 printf("Could not find subimage node type '%s'\n", prop_name);
1967 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1971 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1973 ret = fit_image_select(fit, noffset, images->verify);
1975 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1979 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1980 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1981 if (!fit_image_check_target_arch(fit, noffset)) {
1982 puts("Unsupported Architecture\n");
1983 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1989 fit_image_get_arch(fit, noffset, &os_arch);
1990 images->os.arch = os_arch;
1993 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1994 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1995 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1996 (image_type == IH_TYPE_KERNEL &&
1997 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1999 os_ok = image_type == IH_TYPE_FLATDT ||
2000 image_type == IH_TYPE_FPGA ||
2001 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
2002 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
2003 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
2004 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
2005 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
2008 * If either of the checks fail, we should report an error, but
2009 * if the image type is coming from the "loadables" field, we
2010 * don't care what it is
2012 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
2013 fit_image_get_os(fit, noffset, &os);
2014 printf("No %s %s %s Image\n",
2015 genimg_get_os_name(os),
2016 genimg_get_arch_name(arch),
2017 genimg_get_type_name(image_type));
2018 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2022 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2024 /* get image data address and length */
2025 if (fit_image_get_data_and_size(fit, noffset,
2026 (const void **)&buf, &size)) {
2027 printf("Could not find %s subimage data!\n", prop_name);
2028 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2032 #ifdef CONFIG_FIT_CIPHER
2033 /* Decrypt data before uncompress/move */
2034 if (IMAGE_ENABLE_DECRYPT) {
2035 puts(" Decrypting Data ... ");
2036 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2044 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
2045 /* perform any post-processing on the image data */
2046 board_fit_image_post_process(&buf, &size);
2051 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2053 data = map_to_sysmem(buf);
2055 if (load_op == FIT_LOAD_IGNORED) {
2057 } else if (fit_image_get_load(fit, noffset, &load)) {
2058 if (load_op == FIT_LOAD_REQUIRED) {
2059 printf("Can't get %s subimage load address!\n",
2061 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2064 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
2065 ulong image_start, image_end;
2068 * move image data to the load address,
2069 * make sure we don't overwrite initial image
2072 image_end = addr + fit_get_size(fit);
2074 load_end = load + len;
2075 if (image_type != IH_TYPE_KERNEL &&
2076 load < image_end && load_end > image_start) {
2077 printf("Error: %s overwritten\n", prop_name);
2081 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2082 prop_name, data, load);
2084 load = data; /* No load address specified */
2087 comp = IH_COMP_NONE;
2089 /* Kernel images get decompressed later in bootm_load_os(). */
2090 if (!fit_image_get_comp(fit, noffset, &comp) &&
2091 comp != IH_COMP_NONE &&
2092 !(image_type == IH_TYPE_KERNEL ||
2093 image_type == IH_TYPE_KERNEL_NOLOAD ||
2094 image_type == IH_TYPE_RAMDISK)) {
2095 ulong max_decomp_len = len * 20;
2097 loadbuf = malloc(max_decomp_len);
2098 load = map_to_sysmem(loadbuf);
2100 loadbuf = map_sysmem(load, max_decomp_len);
2102 if (image_decomp(comp, load, data, image_type,
2103 loadbuf, buf, len, max_decomp_len, &load_end)) {
2104 printf("Error decompressing %s\n", prop_name);
2108 len = load_end - load;
2109 } else if (load != data) {
2110 loadbuf = map_sysmem(load, len);
2111 memcpy(loadbuf, buf, len);
2114 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2115 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2116 " please fix your .its file!\n");
2118 /* verify that image data is a proper FDT blob */
2119 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2120 puts("Subimage data is not a FDT");
2124 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2129 *fit_unamep = (char *)fit_uname;
2130 if (fit_uname_configp)
2131 *fit_uname_configp = (char *)(fit_uname_config ? :
2132 fit_base_uname_config);
2137 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2138 ulong *setup_start, ulong *setup_len)
2145 addr = map_to_sysmem(images->fit_hdr_os);
2146 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2150 ret = fit_image_load(images, addr, NULL, NULL, arch,
2151 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2152 FIT_LOAD_REQUIRED, setup_start, &len);
2158 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2159 const char **fit_unamep, const char **fit_uname_configp,
2160 int arch, ulong *datap, ulong *lenp)
2162 int fdt_noffset, cfg_noffset, count;
2164 const char *fit_uname = NULL;
2165 const char *fit_uname_config = NULL;
2166 char *fit_uname_config_copy = NULL;
2167 char *next_config = NULL;
2169 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2170 ulong image_start, image_end;
2171 ulong ovload, ovlen;
2172 const char *uconfig;
2175 int i, err, noffset, ov_noffset;
2178 fit_uname = fit_unamep ? *fit_unamep : NULL;
2180 if (fit_uname_configp && *fit_uname_configp) {
2181 fit_uname_config_copy = strdup(*fit_uname_configp);
2182 if (!fit_uname_config_copy)
2185 next_config = strchr(fit_uname_config_copy, '#');
2187 *next_config++ = '\0';
2188 if (next_config - 1 > fit_uname_config_copy)
2189 fit_uname_config = fit_uname_config_copy;
2192 fdt_noffset = fit_image_load(images,
2193 addr, &fit_uname, &fit_uname_config,
2194 arch, IH_TYPE_FLATDT,
2195 BOOTSTAGE_ID_FIT_FDT_START,
2196 FIT_LOAD_OPTIONAL, &load, &len);
2198 if (fdt_noffset < 0)
2201 debug("fit_uname=%s, fit_uname_config=%s\n",
2202 fit_uname ? fit_uname : "<NULL>",
2203 fit_uname_config ? fit_uname_config : "<NULL>");
2205 fit = map_sysmem(addr, 0);
2207 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2209 /* single blob, or error just return as well */
2210 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2211 if (count <= 1 && !next_config)
2214 /* we need to apply overlays */
2216 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2218 image_end = addr + fit_get_size(fit);
2219 /* verify that relocation took place by load address not being in fit */
2220 if (load >= image_start && load < image_end) {
2221 /* check is simplified; fit load checks for overlaps */
2222 printf("Overlayed FDT requires relocation\n");
2223 fdt_noffset = -EBADF;
2227 base = map_sysmem(load, len);
2229 /* apply extra configs in FIT first, followed by args */
2230 for (i = 1; ; i++) {
2232 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2234 uname = fit_get_name(fit, noffset, NULL);
2239 uconfig = next_config;
2240 next_config = strchr(next_config, '#');
2242 *next_config++ = '\0';
2246 * fit_image_load() would load the first FDT from the
2247 * extra config only when uconfig is specified.
2248 * Check if the extra config contains multiple FDTs and
2251 cfg_noffset = fit_conf_get_node(fit, uconfig);
2254 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2258 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2260 ov_noffset = fit_image_load(images,
2261 addr, &uname, &uconfig,
2262 arch, IH_TYPE_FLATDT,
2263 BOOTSTAGE_ID_FIT_FDT_START,
2264 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2265 if (ov_noffset < 0) {
2266 printf("load of %s failed\n", uname);
2269 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2270 uname, ovload, ovlen);
2271 ov = map_sysmem(ovload, ovlen);
2273 base = map_sysmem(load, len + ovlen);
2274 err = fdt_open_into(base, base, len + ovlen);
2276 printf("failed on fdt_open_into\n");
2280 /* the verbose method prints out messages on error */
2281 err = fdt_overlay_apply_verbose(base, ov);
2287 len = fdt_totalsize(base);
2290 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2291 fdt_noffset = -EBADF;
2300 *fit_unamep = fit_uname;
2301 if (fit_uname_configp)
2302 *fit_uname_configp = fit_uname_config;
2304 if (fit_uname_config_copy)
2305 free(fit_uname_config_copy);