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.
15 #include <linux/compiler.h>
16 #include <linux/kconfig.h>
22 DECLARE_GLOBAL_DATA_PTR;
23 #endif /* !USE_HOSTCC*/
26 #include <bootstage.h>
27 #include <u-boot/crc.h>
28 #include <u-boot/md5.h>
29 #include <u-boot/sha1.h>
30 #include <u-boot/sha256.h>
32 /*****************************************************************************/
33 /* New uImage format routines */
34 /*****************************************************************************/
36 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
37 ulong *addr, const char **name)
44 sep = strchr(spec, sepc);
47 *addr = simple_strtoul(spec, NULL, 16);
57 * fit_parse_conf - parse FIT configuration spec
58 * @spec: input string, containing configuration spec
59 * @add_curr: current image address (to be used as a possible default)
60 * @addr: pointer to a ulong variable, will hold FIT image address of a given
62 * @conf_name double pointer to a char, will hold pointer to a configuration
65 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
66 * where <addr> is a FIT image address that contains configuration
67 * with a <conf> unit name.
69 * Address part is optional, and if omitted default add_curr will
73 * 1 if spec is a valid configuration string,
74 * addr and conf_name are set accordingly
77 int fit_parse_conf(const char *spec, ulong addr_curr,
78 ulong *addr, const char **conf_name)
80 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
84 * fit_parse_subimage - parse FIT subimage spec
85 * @spec: input string, containing subimage spec
86 * @add_curr: current image address (to be used as a possible default)
87 * @addr: pointer to a ulong variable, will hold FIT image address of a given
89 * @image_name: double pointer to a char, will hold pointer to a subimage name
91 * fit_parse_subimage() expects subimage spec in the form of
92 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
93 * subimage with a <subimg> unit name.
95 * Address part is optional, and if omitted default add_curr will
99 * 1 if spec is a valid subimage string,
100 * addr and image_name are set accordingly
103 int fit_parse_subimage(const char *spec, ulong addr_curr,
104 ulong *addr, const char **image_name)
106 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
108 #endif /* !USE_HOSTCC */
110 static void fit_get_debug(const void *fit, int noffset,
111 char *prop_name, int err)
113 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
114 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
119 * fit_get_subimage_count - get component (sub-image) count
120 * @fit: pointer to the FIT format image header
121 * @images_noffset: offset of images node
124 * number of image components
126 int fit_get_subimage_count(const void *fit, int images_noffset)
132 /* Process its subnodes, print out component images details */
133 for (ndepth = 0, count = 0,
134 noffset = fdt_next_node(fit, images_noffset, &ndepth);
135 (noffset >= 0) && (ndepth > 0);
136 noffset = fdt_next_node(fit, noffset, &ndepth)) {
145 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
147 * fit_image_print_data() - prints out the hash node details
148 * @fit: pointer to the FIT format image header
149 * @noffset: offset of the hash node
150 * @p: pointer to prefix string
151 * @type: Type of information to print ("hash" or "sign")
153 * fit_image_print_data() lists properties for the processed hash node
155 * This function avoid using puts() since it prints a newline on the host
156 * but does not in U-Boot.
159 * no returned results
161 static void fit_image_print_data(const void *fit, int noffset, const char *p,
171 debug("%s %s node: '%s'\n", p, type,
172 fit_get_name(fit, noffset, NULL));
173 printf("%s %s algo: ", p, type);
174 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
175 printf("invalid/unsupported\n");
179 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
180 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
182 printf(":%s", keyname);
184 printf(" (required)");
187 ret = fit_image_hash_get_value(fit, noffset, &value,
189 printf("%s %s value: ", p, type);
191 printf("unavailable\n");
193 for (i = 0; i < value_len; i++)
194 printf("%02x", value[i]);
198 debug("%s %s len: %d\n", p, type, value_len);
200 /* Signatures have a time stamp */
201 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
204 printf("%s Timestamp: ", p);
205 if (fit_get_timestamp(fit, noffset, ×tamp))
206 printf("unavailable\n");
208 genimg_print_time(timestamp);
213 * fit_image_print_verification_data() - prints out the hash/signature details
214 * @fit: pointer to the FIT format image header
215 * @noffset: offset of the hash or signature node
216 * @p: pointer to prefix string
218 * This lists properties for the processed hash node
221 * no returned results
223 static void fit_image_print_verification_data(const void *fit, int noffset,
229 * Check subnode name, must be equal to "hash" or "signature".
230 * Multiple hash/signature nodes require unique unit node
231 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
233 name = fit_get_name(fit, noffset, NULL);
234 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
235 fit_image_print_data(fit, noffset, p, "Hash");
236 } else if (!strncmp(name, FIT_SIG_NODENAME,
237 strlen(FIT_SIG_NODENAME))) {
238 fit_image_print_data(fit, noffset, p, "Sign");
243 * fit_conf_print - prints out the FIT configuration details
244 * @fit: pointer to the FIT format image header
245 * @noffset: offset of the configuration node
246 * @p: pointer to prefix string
248 * fit_conf_print() lists all mandatory properties for the processed
249 * configuration node.
252 * no returned results
254 static void fit_conf_print(const void *fit, int noffset, const char *p)
259 int fdt_index, loadables_index;
262 /* Mandatory properties */
263 ret = fit_get_desc(fit, noffset, &desc);
264 printf("%s Description: ", p);
266 printf("unavailable\n");
268 printf("%s\n", desc);
270 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
271 printf("%s Kernel: ", p);
273 printf("unavailable\n");
275 printf("%s\n", uname);
277 /* Optional properties */
278 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
280 printf("%s Init Ramdisk: %s\n", p, uname);
282 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
284 printf("%s Firmware: %s\n", p, uname);
287 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
288 fdt_index, NULL), uname;
291 printf("%s FDT: ", p);
294 printf("%s\n", uname);
297 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
299 printf("%s FPGA: %s\n", p, uname);
301 /* Print out all of the specified loadables */
302 for (loadables_index = 0;
303 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
304 loadables_index, NULL), uname;
306 if (loadables_index == 0) {
307 printf("%s Loadables: ", p);
311 printf("%s\n", uname);
314 /* Process all hash subnodes of the component configuration node */
315 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
316 (noffset >= 0) && (ndepth > 0);
317 noffset = fdt_next_node(fit, noffset, &ndepth)) {
319 /* Direct child node of the component configuration node */
320 fit_image_print_verification_data(fit, noffset, p);
326 * fit_print_contents - prints out the contents of the FIT format image
327 * @fit: pointer to the FIT format image header
328 * @p: pointer to prefix string
330 * fit_print_contents() formats a multi line FIT image contents description.
331 * The routine prints out FIT image properties (root node level) followed by
332 * the details of each component image.
335 * no returned results
337 void fit_print_contents(const void *fit)
350 /* Indent string is defined in header image.h */
351 p = IMAGE_INDENT_STRING;
353 /* Root node properties */
354 ret = fit_get_desc(fit, 0, &desc);
355 printf("%sFIT description: ", p);
357 printf("unavailable\n");
359 printf("%s\n", desc);
361 if (IMAGE_ENABLE_TIMESTAMP) {
362 ret = fit_get_timestamp(fit, 0, ×tamp);
363 printf("%sCreated: ", p);
365 printf("unavailable\n");
367 genimg_print_time(timestamp);
370 /* Find images parent node offset */
371 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
372 if (images_noffset < 0) {
373 printf("Can't find images parent node '%s' (%s)\n",
374 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
378 /* Process its subnodes, print out component images details */
379 for (ndepth = 0, count = 0,
380 noffset = fdt_next_node(fit, images_noffset, &ndepth);
381 (noffset >= 0) && (ndepth > 0);
382 noffset = fdt_next_node(fit, noffset, &ndepth)) {
385 * Direct child node of the images parent node,
386 * i.e. component image node.
388 printf("%s Image %u (%s)\n", p, count++,
389 fit_get_name(fit, noffset, NULL));
391 fit_image_print(fit, noffset, p);
395 /* Find configurations parent node offset */
396 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
397 if (confs_noffset < 0) {
398 debug("Can't get configurations parent node '%s' (%s)\n",
399 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
403 /* get default configuration unit name from default property */
404 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
406 printf("%s Default Configuration: '%s'\n", p, uname);
408 /* Process its subnodes, print out configurations details */
409 for (ndepth = 0, count = 0,
410 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
411 (noffset >= 0) && (ndepth > 0);
412 noffset = fdt_next_node(fit, noffset, &ndepth)) {
415 * Direct child node of the configurations parent node,
416 * i.e. configuration node.
418 printf("%s Configuration %u (%s)\n", p, count++,
419 fit_get_name(fit, noffset, NULL));
421 fit_conf_print(fit, noffset, p);
427 * fit_image_print - prints out the FIT component image details
428 * @fit: pointer to the FIT format image header
429 * @image_noffset: offset of the component image node
430 * @p: pointer to prefix string
432 * fit_image_print() lists all mandatory properties for the processed component
433 * image. If present, hash nodes are printed out as well. Load
434 * address for images of type firmware is also printed out. Since the load
435 * address is not mandatory for firmware images, it will be output as
436 * "unavailable" when not present.
439 * no returned results
441 void fit_image_print(const void *fit, int image_noffset, const char *p)
444 uint8_t type, arch, os, comp;
452 /* Mandatory properties */
453 ret = fit_get_desc(fit, image_noffset, &desc);
454 printf("%s Description: ", p);
456 printf("unavailable\n");
458 printf("%s\n", desc);
460 if (IMAGE_ENABLE_TIMESTAMP) {
463 ret = fit_get_timestamp(fit, 0, ×tamp);
464 printf("%s Created: ", p);
466 printf("unavailable\n");
468 genimg_print_time(timestamp);
471 fit_image_get_type(fit, image_noffset, &type);
472 printf("%s Type: %s\n", p, genimg_get_type_name(type));
474 fit_image_get_comp(fit, image_noffset, &comp);
475 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
477 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
480 printf("%s Data Start: ", p);
482 printf("unavailable\n");
484 void *vdata = (void *)data;
486 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
490 printf("%s Data Size: ", p);
492 printf("unavailable\n");
494 genimg_print_size(size);
496 /* Remaining, type dependent properties */
497 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
498 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
499 (type == IH_TYPE_FLATDT)) {
500 fit_image_get_arch(fit, image_noffset, &arch);
501 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
504 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
505 (type == IH_TYPE_FIRMWARE)) {
506 fit_image_get_os(fit, image_noffset, &os);
507 printf("%s OS: %s\n", p, genimg_get_os_name(os));
510 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
511 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
512 (type == IH_TYPE_FPGA)) {
513 ret = fit_image_get_load(fit, image_noffset, &load);
514 printf("%s Load Address: ", p);
516 printf("unavailable\n");
518 printf("0x%08lx\n", load);
521 /* optional load address for FDT */
522 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
523 printf("%s Load Address: 0x%08lx\n", p, load);
525 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
526 (type == IH_TYPE_RAMDISK)) {
527 ret = fit_image_get_entry(fit, image_noffset, &entry);
528 printf("%s Entry Point: ", p);
530 printf("unavailable\n");
532 printf("0x%08lx\n", entry);
535 /* Process all hash subnodes of the component image node */
536 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
537 (noffset >= 0) && (ndepth > 0);
538 noffset = fdt_next_node(fit, noffset, &ndepth)) {
540 /* Direct child node of the component image node */
541 fit_image_print_verification_data(fit, noffset, p);
546 void fit_print_contents(const void *fit) { }
547 void fit_image_print(const void *fit, int image_noffset, const char *p) { }
548 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
551 * fit_get_desc - get node description property
552 * @fit: pointer to the FIT format image header
553 * @noffset: node offset
554 * @desc: double pointer to the char, will hold pointer to the description
556 * fit_get_desc() reads description property from a given node, if
557 * description is found pointer to it is returned in third call argument.
563 int fit_get_desc(const void *fit, int noffset, char **desc)
567 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
569 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
577 * fit_get_timestamp - get node timestamp property
578 * @fit: pointer to the FIT format image header
579 * @noffset: node offset
580 * @timestamp: pointer to the time_t, will hold read timestamp
582 * fit_get_timestamp() reads timestamp property from given node, if timestamp
583 * is found and has a correct size its value is returned in third call
588 * -1, on property read failure
589 * -2, on wrong timestamp size
591 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
596 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
598 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
601 if (len != sizeof(uint32_t)) {
602 debug("FIT timestamp with incorrect size of (%u)\n", len);
606 *timestamp = uimage_to_cpu(*((uint32_t *)data));
611 * fit_image_get_node - get node offset for component image of a given unit name
612 * @fit: pointer to the FIT format image header
613 * @image_uname: component image node unit name
615 * fit_image_get_node() finds a component image (within the '/images'
616 * node) of a provided unit name. If image is found its node offset is
617 * returned to the caller.
620 * image node offset when found (>=0)
621 * negative number on failure (FDT_ERR_* code)
623 int fit_image_get_node(const void *fit, const char *image_uname)
625 int noffset, images_noffset;
627 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
628 if (images_noffset < 0) {
629 debug("Can't find images parent node '%s' (%s)\n",
630 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
631 return images_noffset;
634 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
636 debug("Can't get node offset for image unit name: '%s' (%s)\n",
637 image_uname, fdt_strerror(noffset));
644 * fit_image_get_os - get os id for a given component image node
645 * @fit: pointer to the FIT format image header
646 * @noffset: component image node offset
647 * @os: pointer to the uint8_t, will hold os numeric id
649 * fit_image_get_os() finds os property in a given component image node.
650 * If the property is found, its (string) value is translated to the numeric
651 * id which is returned to the caller.
657 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
662 /* Get OS name from property data */
663 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
665 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
670 /* Translate OS name to id */
671 *os = genimg_get_os_id(data);
676 * fit_image_get_arch - get arch id for a given component image node
677 * @fit: pointer to the FIT format image header
678 * @noffset: component image node offset
679 * @arch: pointer to the uint8_t, will hold arch numeric id
681 * fit_image_get_arch() finds arch property in a given component image node.
682 * If the property is found, its (string) value is translated to the numeric
683 * id which is returned to the caller.
689 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
694 /* Get architecture name from property data */
695 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
697 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
702 /* Translate architecture name to id */
703 *arch = genimg_get_arch_id(data);
708 * fit_image_get_type - get type id for a given component image node
709 * @fit: pointer to the FIT format image header
710 * @noffset: component image node offset
711 * @type: pointer to the uint8_t, will hold type numeric id
713 * fit_image_get_type() finds type property in a given component image node.
714 * If the property is found, its (string) value is translated to the numeric
715 * id which is returned to the caller.
721 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
726 /* Get image type name from property data */
727 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
729 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
734 /* Translate image type name to id */
735 *type = genimg_get_type_id(data);
740 * fit_image_get_comp - get comp id for a given component image node
741 * @fit: pointer to the FIT format image header
742 * @noffset: component image node offset
743 * @comp: pointer to the uint8_t, will hold comp numeric id
745 * fit_image_get_comp() finds comp property in a given component image node.
746 * If the property is found, its (string) value is translated to the numeric
747 * id which is returned to the caller.
753 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
758 /* Get compression name from property data */
759 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
761 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
766 /* Translate compression name to id */
767 *comp = genimg_get_comp_id(data);
771 static int fit_image_get_address(const void *fit, int noffset, char *name,
778 cell = fdt_getprop(fit, noffset, name, &len);
780 fit_get_debug(fit, noffset, name, len);
784 if (len > sizeof(ulong)) {
785 printf("Unsupported %s address size\n", name);
790 /* Use load64 to avoid compiling warning for 32-bit target */
792 load64 = (load64 << 32) | uimage_to_cpu(*cell);
795 *load = (ulong)load64;
800 * fit_image_get_load() - get load addr property for given component image node
801 * @fit: pointer to the FIT format image header
802 * @noffset: component image node offset
803 * @load: pointer to the uint32_t, will hold load address
805 * fit_image_get_load() finds load address property in a given component
806 * image node. If the property is found, its value is returned to the caller.
812 int fit_image_get_load(const void *fit, int noffset, ulong *load)
814 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
818 * fit_image_get_entry() - get entry point address property
819 * @fit: pointer to the FIT format image header
820 * @noffset: component image node offset
821 * @entry: pointer to the uint32_t, will hold entry point address
823 * This gets the entry point address property for a given component image
826 * fit_image_get_entry() finds entry point address property in a given
827 * component image node. If the property is found, its value is returned
834 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
836 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
840 * fit_image_get_data - get data property and its size for a given component image node
841 * @fit: pointer to the FIT format image header
842 * @noffset: component image node offset
843 * @data: double pointer to void, will hold data property's data address
844 * @size: pointer to size_t, will hold data property's data size
846 * fit_image_get_data() finds data property in a given component image node.
847 * If the property is found its data start address and size are returned to
854 int fit_image_get_data(const void *fit, int noffset,
855 const void **data, size_t *size)
859 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
861 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
871 * Get 'data-offset' property from a given image node.
873 * @fit: pointer to the FIT image header
874 * @noffset: component image node offset
875 * @data_offset: holds the data-offset property
879 * -ENOENT if the property could not be found
881 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
885 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
889 *data_offset = fdt32_to_cpu(*val);
895 * Get 'data-position' property from a given image node.
897 * @fit: pointer to the FIT image header
898 * @noffset: component image node offset
899 * @data_position: holds the data-position property
903 * -ENOENT if the property could not be found
905 int fit_image_get_data_position(const void *fit, int noffset,
910 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
914 *data_position = fdt32_to_cpu(*val);
920 * Get 'data-size' property from a given image node.
922 * @fit: pointer to the FIT image header
923 * @noffset: component image node offset
924 * @data_size: holds the data-size property
928 * -ENOENT if the property could not be found
930 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
934 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
938 *data_size = fdt32_to_cpu(*val);
944 * fit_image_get_data_and_size - get data and its size including
945 * both embedded and external data
946 * @fit: pointer to the FIT format image header
947 * @noffset: component image node offset
948 * @data: double pointer to void, will hold data property's data address
949 * @size: pointer to size_t, will hold data property's data size
951 * fit_image_get_data_and_size() finds data and its size including
952 * both embedded and external data. If the property is found
953 * its data start address and size are returned to the caller.
957 * otherwise, on failure
959 int fit_image_get_data_and_size(const void *fit, int noffset,
960 const void **data, size_t *size)
962 bool external_data = false;
967 if (!fit_image_get_data_position(fit, noffset, &offset)) {
968 external_data = true;
969 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
970 external_data = true;
972 * For FIT with external data, figure out where
973 * the external images start. This is the base
974 * for the data-offset properties in each image.
976 offset += ((fdt_totalsize(fit) + 3) & ~3);
980 debug("External Data\n");
981 ret = fit_image_get_data_size(fit, noffset, &len);
982 *data = fit + offset;
985 ret = fit_image_get_data(fit, noffset, data, size);
992 * fit_image_hash_get_algo - get hash algorithm name
993 * @fit: pointer to the FIT format image header
994 * @noffset: hash node offset
995 * @algo: double pointer to char, will hold pointer to the algorithm name
997 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
998 * If the property is found its data start address is returned to the caller.
1004 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1008 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1009 if (*algo == NULL) {
1010 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1018 * fit_image_hash_get_value - get hash value and length
1019 * @fit: pointer to the FIT format image header
1020 * @noffset: hash node offset
1021 * @value: double pointer to uint8_t, will hold address of a hash value data
1022 * @value_len: pointer to an int, will hold hash data length
1024 * fit_image_hash_get_value() finds hash value property in a given hash node.
1025 * If the property is found its data start address and size are returned to
1032 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1037 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1038 if (*value == NULL) {
1039 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1049 * fit_image_hash_get_ignore - get hash ignore flag
1050 * @fit: pointer to the FIT format image header
1051 * @noffset: hash node offset
1052 * @ignore: pointer to an int, will hold hash ignore flag
1054 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1055 * If the property is found and non-zero, the hash algorithm is not verified by
1056 * u-boot automatically.
1059 * 0, on ignore not found
1060 * value, on ignore found
1062 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1067 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1068 if (value == NULL || len != sizeof(int))
1076 ulong fit_get_end(const void *fit)
1078 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1082 * fit_set_timestamp - set node timestamp property
1083 * @fit: pointer to the FIT format image header
1084 * @noffset: node offset
1085 * @timestamp: timestamp value to be set
1087 * fit_set_timestamp() attempts to set timestamp property in the requested
1088 * node and returns operation status to the caller.
1092 * -ENOSPC if no space in device tree, -1 for other error
1094 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1099 t = cpu_to_uimage(timestamp);
1100 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1103 debug("Can't set '%s' property for '%s' node (%s)\n",
1104 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1106 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1113 * calculate_hash - calculate and return hash for provided input data
1114 * @data: pointer to the input data
1115 * @data_len: data length
1116 * @algo: requested hash algorithm
1117 * @value: pointer to the char, will hold hash value data (caller must
1118 * allocate enough free space)
1119 * value_len: length of the calculated hash
1121 * calculate_hash() computes input data hash according to the requested
1123 * Resulting hash value is placed in caller provided 'value' buffer, length
1124 * of the calculated hash is returned via value_len pointer argument.
1128 * -1, when algo is unsupported
1130 int calculate_hash(const void *data, int data_len, const char *algo,
1131 uint8_t *value, int *value_len)
1133 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1134 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1136 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1138 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1139 sha1_csum_wd((unsigned char *)data, data_len,
1140 (unsigned char *)value, CHUNKSZ_SHA1);
1142 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1143 sha256_csum_wd((unsigned char *)data, data_len,
1144 (unsigned char *)value, CHUNKSZ_SHA256);
1145 *value_len = SHA256_SUM_LEN;
1146 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1147 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1150 debug("Unsupported hash alogrithm\n");
1156 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1157 size_t size, char **err_msgp)
1159 uint8_t value[FIT_MAX_HASH_LEN];
1168 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1169 *err_msgp = "Can't get hash algo property";
1174 if (IMAGE_ENABLE_IGNORE) {
1175 fit_image_hash_get_ignore(fit, noffset, &ignore);
1177 printf("-skipped ");
1182 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1184 *err_msgp = "Can't get hash value property";
1188 if (calculate_hash(data, size, algo, value, &value_len)) {
1189 *err_msgp = "Unsupported hash algorithm";
1193 if (value_len != fit_value_len) {
1194 *err_msgp = "Bad hash value len";
1196 } else if (memcmp(value, fit_value, value_len) != 0) {
1197 *err_msgp = "Bad hash value";
1204 int fit_image_verify_with_data(const void *fit, int image_noffset,
1205 const void *data, size_t size)
1212 /* Verify all required signatures */
1213 if (IMAGE_ENABLE_VERIFY &&
1214 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1215 gd_fdt_blob(), &verify_all)) {
1216 err_msg = "Unable to verify required signature";
1220 /* Process all hash subnodes of the component image node */
1221 fdt_for_each_subnode(noffset, fit, image_noffset) {
1222 const char *name = fit_get_name(fit, noffset, NULL);
1225 * Check subnode name, must be equal to "hash".
1226 * Multiple hash nodes require unique unit node
1227 * names, e.g. hash-1, hash-2, etc.
1229 if (!strncmp(name, FIT_HASH_NODENAME,
1230 strlen(FIT_HASH_NODENAME))) {
1231 if (fit_image_check_hash(fit, noffset, data, size,
1235 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1236 !strncmp(name, FIT_SIG_NODENAME,
1237 strlen(FIT_SIG_NODENAME))) {
1238 ret = fit_image_check_sig(fit, noffset, data,
1239 size, -1, &err_msg);
1242 * Show an indication on failure, but do not return
1243 * an error. Only keys marked 'required' can cause
1244 * an image validation failure. See the call to
1245 * fit_image_verify_required_sigs() above.
1254 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1255 err_msg = "Corrupted or truncated tree";
1262 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1263 err_msg, fit_get_name(fit, noffset, NULL),
1264 fit_get_name(fit, image_noffset, NULL));
1269 * fit_image_verify - verify data integrity
1270 * @fit: pointer to the FIT format image header
1271 * @image_noffset: component image node offset
1273 * fit_image_verify() goes over component image hash nodes,
1274 * re-calculates each data hash and compares with the value stored in hash
1278 * 1, if all hashes are valid
1279 * 0, otherwise (or on error)
1281 int fit_image_verify(const void *fit, int image_noffset)
1288 /* Get image data and data length */
1289 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1290 err_msg = "Can't get image data/size";
1291 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1292 err_msg, fit_get_name(fit, noffset, NULL),
1293 fit_get_name(fit, image_noffset, NULL));
1297 return fit_image_verify_with_data(fit, image_noffset, data, size);
1301 * fit_all_image_verify - verify data integrity for all images
1302 * @fit: pointer to the FIT format image header
1304 * fit_all_image_verify() goes over all images in the FIT and
1305 * for every images checks if all it's hashes are valid.
1308 * 1, if all hashes of all images are valid
1309 * 0, otherwise (or on error)
1311 int fit_all_image_verify(const void *fit)
1318 /* Find images parent node offset */
1319 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1320 if (images_noffset < 0) {
1321 printf("Can't find images parent node '%s' (%s)\n",
1322 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1326 /* Process all image subnodes, check hashes for each */
1327 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1329 for (ndepth = 0, count = 0,
1330 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1331 (noffset >= 0) && (ndepth > 0);
1332 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1335 * Direct child node of the images parent node,
1336 * i.e. component image node.
1338 printf(" Hash(es) for Image %u (%s): ", count,
1339 fit_get_name(fit, noffset, NULL));
1342 if (!fit_image_verify(fit, noffset))
1351 * fit_image_check_os - check whether image node is of a given os type
1352 * @fit: pointer to the FIT format image header
1353 * @noffset: component image node offset
1354 * @os: requested image os
1356 * fit_image_check_os() reads image os property and compares its numeric
1357 * id with the requested os. Comparison result is returned to the caller.
1360 * 1 if image is of given os type
1361 * 0 otherwise (or on error)
1363 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1367 if (fit_image_get_os(fit, noffset, &image_os))
1369 return (os == image_os);
1373 * fit_image_check_arch - check whether image node is of a given arch
1374 * @fit: pointer to the FIT format image header
1375 * @noffset: component image node offset
1376 * @arch: requested imagearch
1378 * fit_image_check_arch() reads image arch property and compares its numeric
1379 * id with the requested arch. Comparison result is returned to the caller.
1382 * 1 if image is of given arch
1383 * 0 otherwise (or on error)
1385 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1388 int aarch32_support = 0;
1390 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1391 aarch32_support = 1;
1394 if (fit_image_get_arch(fit, noffset, &image_arch))
1396 return (arch == image_arch) ||
1397 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1398 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1403 * fit_image_check_type - check whether image node is of a given type
1404 * @fit: pointer to the FIT format image header
1405 * @noffset: component image node offset
1406 * @type: requested image type
1408 * fit_image_check_type() reads image type property and compares its numeric
1409 * id with the requested type. Comparison result is returned to the caller.
1412 * 1 if image is of given type
1413 * 0 otherwise (or on error)
1415 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1419 if (fit_image_get_type(fit, noffset, &image_type))
1421 return (type == image_type);
1425 * fit_image_check_comp - check whether image node uses given compression
1426 * @fit: pointer to the FIT format image header
1427 * @noffset: component image node offset
1428 * @comp: requested image compression type
1430 * fit_image_check_comp() reads image compression property and compares its
1431 * numeric id with the requested compression type. Comparison result is
1432 * returned to the caller.
1435 * 1 if image uses requested compression
1436 * 0 otherwise (or on error)
1438 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1442 if (fit_image_get_comp(fit, noffset, &image_comp))
1444 return (comp == image_comp);
1448 * fit_check_format - sanity check FIT image format
1449 * @fit: pointer to the FIT format image header
1451 * fit_check_format() runs a basic sanity FIT image verification.
1452 * Routine checks for mandatory properties, nodes, etc.
1458 int fit_check_format(const void *fit)
1460 /* mandatory / node 'description' property */
1461 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1462 debug("Wrong FIT format: no description\n");
1466 if (IMAGE_ENABLE_TIMESTAMP) {
1467 /* mandatory / node 'timestamp' property */
1468 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1469 debug("Wrong FIT format: no timestamp\n");
1474 /* mandatory subimages parent '/images' node */
1475 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1476 debug("Wrong FIT format: no images parent node\n");
1485 * fit_conf_find_compat
1486 * @fit: pointer to the FIT format image header
1487 * @fdt: pointer to the device tree to compare against
1489 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1490 * most compatible with the passed in device tree.
1499 * |-o configurations
1507 * |-compatible = "foo,bar", "bim,bam"
1510 * |-compatible = "foo,bar",
1513 * |-compatible = "bim,bam", "baz,biz"
1515 * Configuration 1 would be picked because the first string in U-Boot's
1516 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1517 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1520 * offset to the configuration to use if one was found
1523 int fit_conf_find_compat(const void *fit, const void *fdt)
1526 int noffset, confs_noffset, images_noffset;
1527 const void *fdt_compat;
1529 int best_match_offset = 0;
1530 int best_match_pos = 0;
1532 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1533 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1534 if (confs_noffset < 0 || images_noffset < 0) {
1535 debug("Can't find configurations or images nodes.\n");
1539 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1541 debug("Fdt for comparison has no \"compatible\" property.\n");
1546 * Loop over the configurations in the FIT image.
1548 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1549 (noffset >= 0) && (ndepth > 0);
1550 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1552 const char *kfdt_name;
1554 const char *cur_fdt_compat;
1562 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1564 debug("No fdt property found.\n");
1567 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1569 if (kfdt_noffset < 0) {
1570 debug("No image node named \"%s\" found.\n",
1575 * Get a pointer to this configuration's fdt.
1577 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1578 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1582 len = fdt_compat_len;
1583 cur_fdt_compat = fdt_compat;
1585 * Look for a match for each U-Boot compatibility string in
1586 * turn in this configuration's fdt.
1588 for (i = 0; len > 0 &&
1589 (!best_match_offset || best_match_pos > i); i++) {
1590 int cur_len = strlen(cur_fdt_compat) + 1;
1592 if (!fdt_node_check_compatible(kfdt, 0,
1594 best_match_offset = noffset;
1599 cur_fdt_compat += cur_len;
1602 if (!best_match_offset) {
1603 debug("No match found.\n");
1607 return best_match_offset;
1611 * fit_conf_get_node - get node offset for configuration of a given unit name
1612 * @fit: pointer to the FIT format image header
1613 * @conf_uname: configuration node unit name
1615 * fit_conf_get_node() finds a configuration (within the '/configurations'
1616 * parent node) of a provided unit name. If configuration is found its node
1617 * offset is returned to the caller.
1619 * When NULL is provided in second argument fit_conf_get_node() will search
1620 * for a default configuration node instead. Default configuration node unit
1621 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1625 * configuration node offset when found (>=0)
1626 * negative number on failure (FDT_ERR_* code)
1628 int fit_conf_get_node(const void *fit, const char *conf_uname)
1630 int noffset, confs_noffset;
1633 char *conf_uname_copy = NULL;
1635 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1636 if (confs_noffset < 0) {
1637 debug("Can't find configurations parent node '%s' (%s)\n",
1638 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1639 return confs_noffset;
1642 if (conf_uname == NULL) {
1643 /* get configuration unit name from the default property */
1644 debug("No configuration specified, trying default...\n");
1645 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1646 FIT_DEFAULT_PROP, &len);
1647 if (conf_uname == NULL) {
1648 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1652 debug("Found default configuration: '%s'\n", conf_uname);
1655 s = strchr(conf_uname, '#');
1657 len = s - conf_uname;
1658 conf_uname_copy = malloc(len + 1);
1659 if (!conf_uname_copy) {
1660 debug("Can't allocate uname copy: '%s'\n",
1664 memcpy(conf_uname_copy, conf_uname, len);
1665 conf_uname_copy[len] = '\0';
1666 conf_uname = conf_uname_copy;
1669 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1671 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1672 conf_uname, fdt_strerror(noffset));
1675 if (conf_uname_copy)
1676 free(conf_uname_copy);
1681 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1682 const char *prop_name)
1684 return fdt_stringlist_count(fit, noffset, prop_name);
1687 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1688 const char *prop_name, int index)
1693 /* get kernel image unit name from configuration kernel property */
1694 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1698 return fit_image_get_node(fit, uname);
1701 int fit_conf_get_prop_node(const void *fit, int noffset,
1702 const char *prop_name)
1704 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1707 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1709 fit_image_print(fit, rd_noffset, " ");
1712 puts(" Verifying Hash Integrity ... ");
1713 if (!fit_image_verify(fit, rd_noffset)) {
1714 puts("Bad Data Hash\n");
1723 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1730 debug("* %s: using config '%s' from image at 0x%08lx\n",
1731 prop_name, images->fit_uname_cfg, addr);
1733 /* Check whether configuration has this property defined */
1734 fit_hdr = map_sysmem(addr, 0);
1735 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1736 if (cfg_noffset < 0) {
1737 debug("* %s: no such config\n", prop_name);
1741 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1743 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1751 * fit_get_image_type_property() - get property name for IH_TYPE_...
1753 * @return the properly name where we expect to find the image in the
1756 static const char *fit_get_image_type_property(int type)
1759 * This is sort-of available in the uimage_type[] table in image.c
1760 * but we don't have access to the short name, and "fdt" is different
1761 * anyway. So let's just keep it here.
1764 case IH_TYPE_FLATDT:
1765 return FIT_FDT_PROP;
1766 case IH_TYPE_KERNEL:
1767 return FIT_KERNEL_PROP;
1768 case IH_TYPE_RAMDISK:
1769 return FIT_RAMDISK_PROP;
1770 case IH_TYPE_X86_SETUP:
1771 return FIT_SETUP_PROP;
1772 case IH_TYPE_LOADABLE:
1773 return FIT_LOADABLE_PROP;
1775 return FIT_FPGA_PROP;
1776 case IH_TYPE_STANDALONE:
1777 return FIT_STANDALONE_PROP;
1783 int fit_image_load(bootm_headers_t *images, ulong addr,
1784 const char **fit_unamep, const char **fit_uname_configp,
1785 int arch, int image_type, int bootstage_id,
1786 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1788 int cfg_noffset, noffset;
1789 const char *fit_uname;
1790 const char *fit_uname_config;
1791 const char *fit_base_uname_config;
1796 ulong load, data, len;
1801 const char *prop_name;
1804 fit = map_sysmem(addr, 0);
1805 fit_uname = fit_unamep ? *fit_unamep : NULL;
1806 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1807 fit_base_uname_config = NULL;
1808 prop_name = fit_get_image_type_property(image_type);
1809 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1811 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1812 if (!fit_check_format(fit)) {
1813 printf("Bad FIT %s image format!\n", prop_name);
1814 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1817 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1819 /* get FIT component image node offset */
1820 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1821 noffset = fit_image_get_node(fit, fit_uname);
1824 * no image node unit name, try to get config
1825 * node first. If config unit node name is NULL
1826 * fit_conf_get_node() will try to find default config node
1828 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1829 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1830 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1832 cfg_noffset = fit_conf_get_node(fit,
1835 if (cfg_noffset < 0) {
1836 puts("Could not find configuration node\n");
1837 bootstage_error(bootstage_id +
1838 BOOTSTAGE_SUB_NO_UNIT_NAME);
1842 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1843 printf(" Using '%s' configuration\n", fit_base_uname_config);
1844 /* Remember this config */
1845 if (image_type == IH_TYPE_KERNEL)
1846 images->fit_uname_cfg = fit_base_uname_config;
1848 if (IMAGE_ENABLE_VERIFY && images->verify) {
1849 puts(" Verifying Hash Integrity ... ");
1850 if (fit_config_verify(fit, cfg_noffset)) {
1851 puts("Bad Data Hash\n");
1852 bootstage_error(bootstage_id +
1853 BOOTSTAGE_SUB_HASH);
1859 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1861 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1863 fit_uname = fit_get_name(fit, noffset, NULL);
1866 puts("Could not find subimage node\n");
1867 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1871 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1873 ret = fit_image_select(fit, noffset, images->verify);
1875 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1879 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1880 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1881 if (!fit_image_check_target_arch(fit, noffset)) {
1882 puts("Unsupported Architecture\n");
1883 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1889 fit_image_get_arch(fit, noffset, &os_arch);
1890 images->os.arch = os_arch;
1893 if (image_type == IH_TYPE_FLATDT &&
1894 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1895 puts("FDT image is compressed");
1896 return -EPROTONOSUPPORT;
1899 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1900 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1901 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1902 (image_type == IH_TYPE_KERNEL &&
1903 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1905 os_ok = image_type == IH_TYPE_FLATDT ||
1906 image_type == IH_TYPE_FPGA ||
1907 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1908 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1909 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1912 * If either of the checks fail, we should report an error, but
1913 * if the image type is coming from the "loadables" field, we
1914 * don't care what it is
1916 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
1917 fit_image_get_os(fit, noffset, &os);
1918 printf("No %s %s %s Image\n",
1919 genimg_get_os_name(os),
1920 genimg_get_arch_name(arch),
1921 genimg_get_type_name(image_type));
1922 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1926 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1928 /* get image data address and length */
1929 if (fit_image_get_data_and_size(fit, noffset, &buf, &size)) {
1930 printf("Could not find %s subimage data!\n", prop_name);
1931 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1935 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1936 /* perform any post-processing on the image data */
1937 board_fit_image_post_process((void **)&buf, &size);
1942 /* verify that image data is a proper FDT blob */
1943 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1944 puts("Subimage data is not a FDT");
1948 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1951 * Work-around for eldk-4.2 which gives this warning if we try to
1952 * cast in the unmap_sysmem() call:
1953 * warning: initialization discards qualifiers from pointer target type
1956 void *vbuf = (void *)buf;
1958 data = map_to_sysmem(vbuf);
1961 if (load_op == FIT_LOAD_IGNORED) {
1963 } else if (fit_image_get_load(fit, noffset, &load)) {
1964 if (load_op == FIT_LOAD_REQUIRED) {
1965 printf("Can't get %s subimage load address!\n",
1967 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1970 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1971 ulong image_start, image_end;
1976 * move image data to the load address,
1977 * make sure we don't overwrite initial image
1980 image_end = addr + fit_get_size(fit);
1982 load_end = load + len;
1983 if (image_type != IH_TYPE_KERNEL &&
1984 load < image_end && load_end > image_start) {
1985 printf("Error: %s overwritten\n", prop_name);
1989 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1990 prop_name, data, load);
1992 dst = map_sysmem(load, len);
1993 memmove(dst, buf, len);
1996 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2001 *fit_unamep = (char *)fit_uname;
2002 if (fit_uname_configp)
2003 *fit_uname_configp = (char *)(fit_uname_config ? :
2004 fit_base_uname_config);
2009 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2010 ulong *setup_start, ulong *setup_len)
2017 addr = map_to_sysmem(images->fit_hdr_os);
2018 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2022 ret = fit_image_load(images, addr, NULL, NULL, arch,
2023 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2024 FIT_LOAD_REQUIRED, setup_start, &len);
2030 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2031 const char **fit_unamep, const char **fit_uname_configp,
2032 int arch, ulong *datap, ulong *lenp)
2034 int fdt_noffset, cfg_noffset, count;
2036 const char *fit_uname = NULL;
2037 const char *fit_uname_config = NULL;
2038 char *fit_uname_config_copy = NULL;
2039 char *next_config = NULL;
2041 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2042 ulong image_start, image_end;
2043 ulong ovload, ovlen;
2044 const char *uconfig;
2047 int i, err, noffset, ov_noffset;
2050 fit_uname = fit_unamep ? *fit_unamep : NULL;
2052 if (fit_uname_configp && *fit_uname_configp) {
2053 fit_uname_config_copy = strdup(*fit_uname_configp);
2054 if (!fit_uname_config_copy)
2057 next_config = strchr(fit_uname_config_copy, '#');
2059 *next_config++ = '\0';
2060 if (next_config - 1 > fit_uname_config_copy)
2061 fit_uname_config = fit_uname_config_copy;
2064 fdt_noffset = fit_image_load(images,
2065 addr, &fit_uname, &fit_uname_config,
2066 arch, IH_TYPE_FLATDT,
2067 BOOTSTAGE_ID_FIT_FDT_START,
2068 FIT_LOAD_OPTIONAL, &load, &len);
2070 if (fdt_noffset < 0)
2073 debug("fit_uname=%s, fit_uname_config=%s\n",
2074 fit_uname ? fit_uname : "<NULL>",
2075 fit_uname_config ? fit_uname_config : "<NULL>");
2077 fit = map_sysmem(addr, 0);
2079 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2081 /* single blob, or error just return as well */
2082 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2083 if (count <= 1 && !next_config)
2086 /* we need to apply overlays */
2088 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2090 image_end = addr + fit_get_size(fit);
2091 /* verify that relocation took place by load address not being in fit */
2092 if (load >= image_start && load < image_end) {
2093 /* check is simplified; fit load checks for overlaps */
2094 printf("Overlayed FDT requires relocation\n");
2095 fdt_noffset = -EBADF;
2099 base = map_sysmem(load, len);
2101 /* apply extra configs in FIT first, followed by args */
2102 for (i = 1; ; i++) {
2104 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2106 uname = fit_get_name(fit, noffset, NULL);
2111 uconfig = next_config;
2112 next_config = strchr(next_config, '#');
2114 *next_config++ = '\0';
2118 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2120 ov_noffset = fit_image_load(images,
2121 addr, &uname, &uconfig,
2122 arch, IH_TYPE_FLATDT,
2123 BOOTSTAGE_ID_FIT_FDT_START,
2124 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2125 if (ov_noffset < 0) {
2126 printf("load of %s failed\n", uname);
2129 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2130 uname, ovload, ovlen);
2131 ov = map_sysmem(ovload, ovlen);
2133 base = map_sysmem(load, len + ovlen);
2134 err = fdt_open_into(base, base, len + ovlen);
2136 printf("failed on fdt_open_into\n");
2140 /* the verbose method prints out messages on error */
2141 err = fdt_overlay_apply_verbose(base, ov);
2147 len = fdt_totalsize(base);
2150 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2151 fdt_noffset = -EBADF;
2160 *fit_unamep = fit_uname;
2161 if (fit_uname_configp)
2162 *fit_uname_configp = fit_uname_config;
2164 if (fit_uname_config_copy)
2165 free(fit_uname_config_copy);