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(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_hash_get_algo - get hash algorithm name
945 * @fit: pointer to the FIT format image header
946 * @noffset: hash node offset
947 * @algo: double pointer to char, will hold pointer to the algorithm name
949 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
950 * If the property is found its data start address is returned to the caller.
956 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
960 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
962 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
970 * fit_image_hash_get_value - get hash value and length
971 * @fit: pointer to the FIT format image header
972 * @noffset: hash node offset
973 * @value: double pointer to uint8_t, will hold address of a hash value data
974 * @value_len: pointer to an int, will hold hash data length
976 * fit_image_hash_get_value() finds hash value property in a given hash node.
977 * If the property is found its data start address and size are returned to
984 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
989 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
990 if (*value == NULL) {
991 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1001 * fit_image_hash_get_ignore - get hash ignore flag
1002 * @fit: pointer to the FIT format image header
1003 * @noffset: hash node offset
1004 * @ignore: pointer to an int, will hold hash ignore flag
1006 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1007 * If the property is found and non-zero, the hash algorithm is not verified by
1008 * u-boot automatically.
1011 * 0, on ignore not found
1012 * value, on ignore found
1014 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1019 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1020 if (value == NULL || len != sizeof(int))
1028 ulong fit_get_end(const void *fit)
1030 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1034 * fit_set_timestamp - set node timestamp property
1035 * @fit: pointer to the FIT format image header
1036 * @noffset: node offset
1037 * @timestamp: timestamp value to be set
1039 * fit_set_timestamp() attempts to set timestamp property in the requested
1040 * node and returns operation status to the caller.
1044 * -ENOSPC if no space in device tree, -1 for other error
1046 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1051 t = cpu_to_uimage(timestamp);
1052 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1055 debug("Can't set '%s' property for '%s' node (%s)\n",
1056 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1058 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1065 * calculate_hash - calculate and return hash for provided input data
1066 * @data: pointer to the input data
1067 * @data_len: data length
1068 * @algo: requested hash algorithm
1069 * @value: pointer to the char, will hold hash value data (caller must
1070 * allocate enough free space)
1071 * value_len: length of the calculated hash
1073 * calculate_hash() computes input data hash according to the requested
1075 * Resulting hash value is placed in caller provided 'value' buffer, length
1076 * of the calculated hash is returned via value_len pointer argument.
1080 * -1, when algo is unsupported
1082 int calculate_hash(const void *data, int data_len, const char *algo,
1083 uint8_t *value, int *value_len)
1085 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1086 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1088 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1090 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1091 sha1_csum_wd((unsigned char *)data, data_len,
1092 (unsigned char *)value, CHUNKSZ_SHA1);
1094 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1095 sha256_csum_wd((unsigned char *)data, data_len,
1096 (unsigned char *)value, CHUNKSZ_SHA256);
1097 *value_len = SHA256_SUM_LEN;
1098 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1099 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1102 debug("Unsupported hash alogrithm\n");
1108 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1109 size_t size, char **err_msgp)
1111 uint8_t value[FIT_MAX_HASH_LEN];
1120 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1121 *err_msgp = "Can't get hash algo property";
1126 if (IMAGE_ENABLE_IGNORE) {
1127 fit_image_hash_get_ignore(fit, noffset, &ignore);
1129 printf("-skipped ");
1134 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1136 *err_msgp = "Can't get hash value property";
1140 if (calculate_hash(data, size, algo, value, &value_len)) {
1141 *err_msgp = "Unsupported hash algorithm";
1145 if (value_len != fit_value_len) {
1146 *err_msgp = "Bad hash value len";
1148 } else if (memcmp(value, fit_value, value_len) != 0) {
1149 *err_msgp = "Bad hash value";
1156 int fit_image_verify_with_data(const void *fit, int image_noffset,
1157 const void *data, size_t size)
1164 /* Verify all required signatures */
1165 if (IMAGE_ENABLE_VERIFY &&
1166 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1167 gd_fdt_blob(), &verify_all)) {
1168 err_msg = "Unable to verify required signature";
1172 /* Process all hash subnodes of the component image node */
1173 fdt_for_each_subnode(noffset, fit, image_noffset) {
1174 const char *name = fit_get_name(fit, noffset, NULL);
1177 * Check subnode name, must be equal to "hash".
1178 * Multiple hash nodes require unique unit node
1179 * names, e.g. hash-1, hash-2, etc.
1181 if (!strncmp(name, FIT_HASH_NODENAME,
1182 strlen(FIT_HASH_NODENAME))) {
1183 if (fit_image_check_hash(fit, noffset, data, size,
1187 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1188 !strncmp(name, FIT_SIG_NODENAME,
1189 strlen(FIT_SIG_NODENAME))) {
1190 ret = fit_image_check_sig(fit, noffset, data,
1191 size, -1, &err_msg);
1194 * Show an indication on failure, but do not return
1195 * an error. Only keys marked 'required' can cause
1196 * an image validation failure. See the call to
1197 * fit_image_verify_required_sigs() above.
1206 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1207 err_msg = "Corrupted or truncated tree";
1214 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1215 err_msg, fit_get_name(fit, noffset, NULL),
1216 fit_get_name(fit, image_noffset, NULL));
1221 * fit_image_verify - verify data integrity
1222 * @fit: pointer to the FIT format image header
1223 * @image_noffset: component image node offset
1225 * fit_image_verify() goes over component image hash nodes,
1226 * re-calculates each data hash and compares with the value stored in hash
1230 * 1, if all hashes are valid
1231 * 0, otherwise (or on error)
1233 int fit_image_verify(const void *fit, int image_noffset)
1240 /* Get image data and data length */
1241 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1242 err_msg = "Can't get image data/size";
1243 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1244 err_msg, fit_get_name(fit, noffset, NULL),
1245 fit_get_name(fit, image_noffset, NULL));
1249 return fit_image_verify_with_data(fit, image_noffset, data, size);
1253 * fit_all_image_verify - verify data integrity for all images
1254 * @fit: pointer to the FIT format image header
1256 * fit_all_image_verify() goes over all images in the FIT and
1257 * for every images checks if all it's hashes are valid.
1260 * 1, if all hashes of all images are valid
1261 * 0, otherwise (or on error)
1263 int fit_all_image_verify(const void *fit)
1270 /* Find images parent node offset */
1271 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1272 if (images_noffset < 0) {
1273 printf("Can't find images parent node '%s' (%s)\n",
1274 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1278 /* Process all image subnodes, check hashes for each */
1279 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1281 for (ndepth = 0, count = 0,
1282 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1283 (noffset >= 0) && (ndepth > 0);
1284 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1287 * Direct child node of the images parent node,
1288 * i.e. component image node.
1290 printf(" Hash(es) for Image %u (%s): ", count,
1291 fit_get_name(fit, noffset, NULL));
1294 if (!fit_image_verify(fit, noffset))
1303 * fit_image_check_os - check whether image node is of a given os type
1304 * @fit: pointer to the FIT format image header
1305 * @noffset: component image node offset
1306 * @os: requested image os
1308 * fit_image_check_os() reads image os property and compares its numeric
1309 * id with the requested os. Comparison result is returned to the caller.
1312 * 1 if image is of given os type
1313 * 0 otherwise (or on error)
1315 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1319 if (fit_image_get_os(fit, noffset, &image_os))
1321 return (os == image_os);
1325 * fit_image_check_arch - check whether image node is of a given arch
1326 * @fit: pointer to the FIT format image header
1327 * @noffset: component image node offset
1328 * @arch: requested imagearch
1330 * fit_image_check_arch() reads image arch property and compares its numeric
1331 * id with the requested arch. Comparison result is returned to the caller.
1334 * 1 if image is of given arch
1335 * 0 otherwise (or on error)
1337 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1340 int aarch32_support = 0;
1342 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1343 aarch32_support = 1;
1346 if (fit_image_get_arch(fit, noffset, &image_arch))
1348 return (arch == image_arch) ||
1349 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1350 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1355 * fit_image_check_type - check whether image node is of a given type
1356 * @fit: pointer to the FIT format image header
1357 * @noffset: component image node offset
1358 * @type: requested image type
1360 * fit_image_check_type() reads image type property and compares its numeric
1361 * id with the requested type. Comparison result is returned to the caller.
1364 * 1 if image is of given type
1365 * 0 otherwise (or on error)
1367 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1371 if (fit_image_get_type(fit, noffset, &image_type))
1373 return (type == image_type);
1377 * fit_image_check_comp - check whether image node uses given compression
1378 * @fit: pointer to the FIT format image header
1379 * @noffset: component image node offset
1380 * @comp: requested image compression type
1382 * fit_image_check_comp() reads image compression property and compares its
1383 * numeric id with the requested compression type. Comparison result is
1384 * returned to the caller.
1387 * 1 if image uses requested compression
1388 * 0 otherwise (or on error)
1390 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1394 if (fit_image_get_comp(fit, noffset, &image_comp))
1396 return (comp == image_comp);
1400 * fit_check_format - sanity check FIT image format
1401 * @fit: pointer to the FIT format image header
1403 * fit_check_format() runs a basic sanity FIT image verification.
1404 * Routine checks for mandatory properties, nodes, etc.
1410 int fit_check_format(const void *fit)
1412 /* mandatory / node 'description' property */
1413 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1414 debug("Wrong FIT format: no description\n");
1418 if (IMAGE_ENABLE_TIMESTAMP) {
1419 /* mandatory / node 'timestamp' property */
1420 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1421 debug("Wrong FIT format: no timestamp\n");
1426 /* mandatory subimages parent '/images' node */
1427 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1428 debug("Wrong FIT format: no images parent node\n");
1437 * fit_conf_find_compat
1438 * @fit: pointer to the FIT format image header
1439 * @fdt: pointer to the device tree to compare against
1441 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1442 * most compatible with the passed in device tree.
1451 * |-o configurations
1459 * |-compatible = "foo,bar", "bim,bam"
1462 * |-compatible = "foo,bar",
1465 * |-compatible = "bim,bam", "baz,biz"
1467 * Configuration 1 would be picked because the first string in U-Boot's
1468 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1469 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1472 * offset to the configuration to use if one was found
1475 int fit_conf_find_compat(const void *fit, const void *fdt)
1478 int noffset, confs_noffset, images_noffset;
1479 const void *fdt_compat;
1481 int best_match_offset = 0;
1482 int best_match_pos = 0;
1484 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1485 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1486 if (confs_noffset < 0 || images_noffset < 0) {
1487 debug("Can't find configurations or images nodes.\n");
1491 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1493 debug("Fdt for comparison has no \"compatible\" property.\n");
1498 * Loop over the configurations in the FIT image.
1500 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1501 (noffset >= 0) && (ndepth > 0);
1502 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1504 const char *kfdt_name;
1506 const char *cur_fdt_compat;
1514 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1516 debug("No fdt property found.\n");
1519 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1521 if (kfdt_noffset < 0) {
1522 debug("No image node named \"%s\" found.\n",
1527 * Get a pointer to this configuration's fdt.
1529 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1530 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1534 len = fdt_compat_len;
1535 cur_fdt_compat = fdt_compat;
1537 * Look for a match for each U-Boot compatibility string in
1538 * turn in this configuration's fdt.
1540 for (i = 0; len > 0 &&
1541 (!best_match_offset || best_match_pos > i); i++) {
1542 int cur_len = strlen(cur_fdt_compat) + 1;
1544 if (!fdt_node_check_compatible(kfdt, 0,
1546 best_match_offset = noffset;
1551 cur_fdt_compat += cur_len;
1554 if (!best_match_offset) {
1555 debug("No match found.\n");
1559 return best_match_offset;
1563 * fit_conf_get_node - get node offset for configuration of a given unit name
1564 * @fit: pointer to the FIT format image header
1565 * @conf_uname: configuration node unit name
1567 * fit_conf_get_node() finds a configuration (within the '/configurations'
1568 * parent node) of a provided unit name. If configuration is found its node
1569 * offset is returned to the caller.
1571 * When NULL is provided in second argument fit_conf_get_node() will search
1572 * for a default configuration node instead. Default configuration node unit
1573 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1577 * configuration node offset when found (>=0)
1578 * negative number on failure (FDT_ERR_* code)
1580 int fit_conf_get_node(const void *fit, const char *conf_uname)
1582 int noffset, confs_noffset;
1585 char *conf_uname_copy = NULL;
1587 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1588 if (confs_noffset < 0) {
1589 debug("Can't find configurations parent node '%s' (%s)\n",
1590 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1591 return confs_noffset;
1594 if (conf_uname == NULL) {
1595 /* get configuration unit name from the default property */
1596 debug("No configuration specified, trying default...\n");
1597 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1598 FIT_DEFAULT_PROP, &len);
1599 if (conf_uname == NULL) {
1600 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1604 debug("Found default configuration: '%s'\n", conf_uname);
1607 s = strchr(conf_uname, '#');
1609 len = s - conf_uname;
1610 conf_uname_copy = malloc(len + 1);
1611 if (!conf_uname_copy) {
1612 debug("Can't allocate uname copy: '%s'\n",
1616 memcpy(conf_uname_copy, conf_uname, len);
1617 conf_uname_copy[len] = '\0';
1618 conf_uname = conf_uname_copy;
1621 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1623 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1624 conf_uname, fdt_strerror(noffset));
1627 if (conf_uname_copy)
1628 free(conf_uname_copy);
1633 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1634 const char *prop_name)
1636 return fdt_stringlist_count(fit, noffset, prop_name);
1639 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1640 const char *prop_name, int index)
1645 /* get kernel image unit name from configuration kernel property */
1646 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1650 return fit_image_get_node(fit, uname);
1653 int fit_conf_get_prop_node(const void *fit, int noffset,
1654 const char *prop_name)
1656 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1659 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1661 fit_image_print(fit, rd_noffset, " ");
1664 puts(" Verifying Hash Integrity ... ");
1665 if (!fit_image_verify(fit, rd_noffset)) {
1666 puts("Bad Data Hash\n");
1675 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1682 debug("* %s: using config '%s' from image at 0x%08lx\n",
1683 prop_name, images->fit_uname_cfg, addr);
1685 /* Check whether configuration has this property defined */
1686 fit_hdr = map_sysmem(addr, 0);
1687 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1688 if (cfg_noffset < 0) {
1689 debug("* %s: no such config\n", prop_name);
1693 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1695 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1703 * fit_get_image_type_property() - get property name for IH_TYPE_...
1705 * @return the properly name where we expect to find the image in the
1708 static const char *fit_get_image_type_property(int type)
1711 * This is sort-of available in the uimage_type[] table in image.c
1712 * but we don't have access to the short name, and "fdt" is different
1713 * anyway. So let's just keep it here.
1716 case IH_TYPE_FLATDT:
1717 return FIT_FDT_PROP;
1718 case IH_TYPE_KERNEL:
1719 return FIT_KERNEL_PROP;
1720 case IH_TYPE_RAMDISK:
1721 return FIT_RAMDISK_PROP;
1722 case IH_TYPE_X86_SETUP:
1723 return FIT_SETUP_PROP;
1724 case IH_TYPE_LOADABLE:
1725 return FIT_LOADABLE_PROP;
1727 return FIT_FPGA_PROP;
1728 case IH_TYPE_STANDALONE:
1729 return FIT_STANDALONE_PROP;
1735 int fit_image_load(bootm_headers_t *images, ulong addr,
1736 const char **fit_unamep, const char **fit_uname_configp,
1737 int arch, int image_type, int bootstage_id,
1738 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1740 int cfg_noffset, noffset;
1741 const char *fit_uname;
1742 const char *fit_uname_config;
1743 const char *fit_base_uname_config;
1748 ulong load, data, len;
1753 const char *prop_name;
1756 fit = map_sysmem(addr, 0);
1757 fit_uname = fit_unamep ? *fit_unamep : NULL;
1758 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1759 fit_base_uname_config = NULL;
1760 prop_name = fit_get_image_type_property(image_type);
1761 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1763 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1764 if (!fit_check_format(fit)) {
1765 printf("Bad FIT %s image format!\n", prop_name);
1766 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1769 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1771 /* get FIT component image node offset */
1772 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1773 noffset = fit_image_get_node(fit, fit_uname);
1776 * no image node unit name, try to get config
1777 * node first. If config unit node name is NULL
1778 * fit_conf_get_node() will try to find default config node
1780 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1781 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1782 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1784 cfg_noffset = fit_conf_get_node(fit,
1787 if (cfg_noffset < 0) {
1788 puts("Could not find configuration node\n");
1789 bootstage_error(bootstage_id +
1790 BOOTSTAGE_SUB_NO_UNIT_NAME);
1793 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1794 printf(" Using '%s' configuration\n", fit_base_uname_config);
1795 if (image_type == IH_TYPE_KERNEL) {
1796 /* Remember (and possibly verify) this config */
1797 images->fit_uname_cfg = fit_base_uname_config;
1798 if (IMAGE_ENABLE_VERIFY && images->verify) {
1799 puts(" Verifying Hash Integrity ... ");
1800 if (fit_config_verify(fit, cfg_noffset)) {
1801 puts("Bad Data Hash\n");
1802 bootstage_error(bootstage_id +
1803 BOOTSTAGE_SUB_HASH);
1808 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1811 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1813 fit_uname = fit_get_name(fit, noffset, NULL);
1816 puts("Could not find subimage node\n");
1817 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1821 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1823 ret = fit_image_select(fit, noffset, images->verify);
1825 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1829 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1830 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1831 if (!fit_image_check_target_arch(fit, noffset)) {
1832 puts("Unsupported Architecture\n");
1833 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1839 fit_image_get_arch(fit, noffset, &os_arch);
1840 images->os.arch = os_arch;
1843 if (image_type == IH_TYPE_FLATDT &&
1844 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1845 puts("FDT image is compressed");
1846 return -EPROTONOSUPPORT;
1849 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1850 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1851 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1852 (image_type == IH_TYPE_KERNEL &&
1853 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1855 os_ok = image_type == IH_TYPE_FLATDT ||
1856 image_type == IH_TYPE_FPGA ||
1857 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1858 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1859 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1862 * If either of the checks fail, we should report an error, but
1863 * if the image type is coming from the "loadables" field, we
1864 * don't care what it is
1866 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
1867 fit_image_get_os(fit, noffset, &os);
1868 printf("No %s %s %s Image\n",
1869 genimg_get_os_name(os),
1870 genimg_get_arch_name(arch),
1871 genimg_get_type_name(image_type));
1872 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1876 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1878 /* get image data address and length */
1879 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1880 printf("Could not find %s subimage data!\n", prop_name);
1881 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1885 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1886 /* perform any post-processing on the image data */
1887 board_fit_image_post_process((void **)&buf, &size);
1892 /* verify that image data is a proper FDT blob */
1893 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1894 puts("Subimage data is not a FDT");
1898 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1901 * Work-around for eldk-4.2 which gives this warning if we try to
1902 * cast in the unmap_sysmem() call:
1903 * warning: initialization discards qualifiers from pointer target type
1906 void *vbuf = (void *)buf;
1908 data = map_to_sysmem(vbuf);
1911 if (load_op == FIT_LOAD_IGNORED) {
1913 } else if (fit_image_get_load(fit, noffset, &load)) {
1914 if (load_op == FIT_LOAD_REQUIRED) {
1915 printf("Can't get %s subimage load address!\n",
1917 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1920 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1921 ulong image_start, image_end;
1926 * move image data to the load address,
1927 * make sure we don't overwrite initial image
1930 image_end = addr + fit_get_size(fit);
1932 load_end = load + len;
1933 if (image_type != IH_TYPE_KERNEL &&
1934 load < image_end && load_end > image_start) {
1935 printf("Error: %s overwritten\n", prop_name);
1939 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1940 prop_name, data, load);
1942 dst = map_sysmem(load, len);
1943 memmove(dst, buf, len);
1946 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1951 *fit_unamep = (char *)fit_uname;
1952 if (fit_uname_configp)
1953 *fit_uname_configp = (char *)(fit_uname_config ? :
1954 fit_base_uname_config);
1959 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1960 ulong *setup_start, ulong *setup_len)
1967 addr = map_to_sysmem(images->fit_hdr_os);
1968 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1972 ret = fit_image_load(images, addr, NULL, NULL, arch,
1973 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1974 FIT_LOAD_REQUIRED, setup_start, &len);
1980 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
1981 const char **fit_unamep, const char **fit_uname_configp,
1982 int arch, ulong *datap, ulong *lenp)
1984 int fdt_noffset, cfg_noffset, count;
1986 const char *fit_uname = NULL;
1987 const char *fit_uname_config = NULL;
1988 char *fit_uname_config_copy = NULL;
1989 char *next_config = NULL;
1991 #ifdef CONFIG_OF_LIBFDT_OVERLAY
1992 ulong image_start, image_end;
1993 ulong ovload, ovlen;
1994 const char *uconfig;
1997 int i, err, noffset, ov_noffset;
2000 fit_uname = fit_unamep ? *fit_unamep : NULL;
2002 if (fit_uname_configp && *fit_uname_configp) {
2003 fit_uname_config_copy = strdup(*fit_uname_configp);
2004 if (!fit_uname_config_copy)
2007 next_config = strchr(fit_uname_config_copy, '#');
2009 *next_config++ = '\0';
2010 if (next_config - 1 > fit_uname_config_copy)
2011 fit_uname_config = fit_uname_config_copy;
2014 fdt_noffset = fit_image_load(images,
2015 addr, &fit_uname, &fit_uname_config,
2016 arch, IH_TYPE_FLATDT,
2017 BOOTSTAGE_ID_FIT_FDT_START,
2018 FIT_LOAD_OPTIONAL, &load, &len);
2020 if (fdt_noffset < 0)
2023 debug("fit_uname=%s, fit_uname_config=%s\n",
2024 fit_uname ? fit_uname : "<NULL>",
2025 fit_uname_config ? fit_uname_config : "<NULL>");
2027 fit = map_sysmem(addr, 0);
2029 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2031 /* single blob, or error just return as well */
2032 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2033 if (count <= 1 && !next_config)
2036 /* we need to apply overlays */
2038 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2040 image_end = addr + fit_get_size(fit);
2041 /* verify that relocation took place by load address not being in fit */
2042 if (load >= image_start && load < image_end) {
2043 /* check is simplified; fit load checks for overlaps */
2044 printf("Overlayed FDT requires relocation\n");
2045 fdt_noffset = -EBADF;
2049 base = map_sysmem(load, len);
2051 /* apply extra configs in FIT first, followed by args */
2052 for (i = 1; ; i++) {
2054 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2056 uname = fit_get_name(fit, noffset, NULL);
2061 uconfig = next_config;
2062 next_config = strchr(next_config, '#');
2064 *next_config++ = '\0';
2068 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2070 ov_noffset = fit_image_load(images,
2071 addr, &uname, &uconfig,
2072 arch, IH_TYPE_FLATDT,
2073 BOOTSTAGE_ID_FIT_FDT_START,
2074 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2075 if (ov_noffset < 0) {
2076 printf("load of %s failed\n", uname);
2079 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2080 uname, ovload, ovlen);
2081 ov = map_sysmem(ovload, ovlen);
2083 base = map_sysmem(load, len + ovlen);
2084 err = fdt_open_into(base, base, len + ovlen);
2086 printf("failed on fdt_open_into\n");
2090 /* the verbose method prints out messages on error */
2091 err = fdt_overlay_apply_verbose(base, ov);
2097 len = fdt_totalsize(base);
2100 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2101 fdt_noffset = -EBADF;
2110 *fit_unamep = fit_uname;
2111 if (fit_uname_configp)
2112 *fit_uname_configp = fit_uname_config;
2114 if (fit_uname_config_copy)
2115 free(fit_uname_config_copy);