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_FIT_SPL_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 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
549 * fit_get_desc - get node description property
550 * @fit: pointer to the FIT format image header
551 * @noffset: node offset
552 * @desc: double pointer to the char, will hold pointer to the description
554 * fit_get_desc() reads description property from a given node, if
555 * description is found pointer to it is returned in third call argument.
561 int fit_get_desc(const void *fit, int noffset, char **desc)
565 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
567 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
575 * fit_get_timestamp - get node timestamp property
576 * @fit: pointer to the FIT format image header
577 * @noffset: node offset
578 * @timestamp: pointer to the time_t, will hold read timestamp
580 * fit_get_timestamp() reads timestamp property from given node, if timestamp
581 * is found and has a correct size its value is returned in third call
586 * -1, on property read failure
587 * -2, on wrong timestamp size
589 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
594 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
596 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
599 if (len != sizeof(uint32_t)) {
600 debug("FIT timestamp with incorrect size of (%u)\n", len);
604 *timestamp = uimage_to_cpu(*((uint32_t *)data));
609 * fit_image_get_node - get node offset for component image of a given unit name
610 * @fit: pointer to the FIT format image header
611 * @image_uname: component image node unit name
613 * fit_image_get_node() finds a component image (within the '/images'
614 * node) of a provided unit name. If image is found its node offset is
615 * returned to the caller.
618 * image node offset when found (>=0)
619 * negative number on failure (FDT_ERR_* code)
621 int fit_image_get_node(const void *fit, const char *image_uname)
623 int noffset, images_noffset;
625 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
626 if (images_noffset < 0) {
627 debug("Can't find images parent node '%s' (%s)\n",
628 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
629 return images_noffset;
632 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
634 debug("Can't get node offset for image unit name: '%s' (%s)\n",
635 image_uname, fdt_strerror(noffset));
642 * fit_image_get_os - get os id for a given component image node
643 * @fit: pointer to the FIT format image header
644 * @noffset: component image node offset
645 * @os: pointer to the uint8_t, will hold os numeric id
647 * fit_image_get_os() finds os property in a given component image node.
648 * If the property is found, its (string) value is translated to the numeric
649 * id which is returned to the caller.
655 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
660 /* Get OS name from property data */
661 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
663 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
668 /* Translate OS name to id */
669 *os = genimg_get_os_id(data);
674 * fit_image_get_arch - get arch id for a given component image node
675 * @fit: pointer to the FIT format image header
676 * @noffset: component image node offset
677 * @arch: pointer to the uint8_t, will hold arch numeric id
679 * fit_image_get_arch() finds arch property in a given component image node.
680 * If the property is found, its (string) value is translated to the numeric
681 * id which is returned to the caller.
687 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
692 /* Get architecture name from property data */
693 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
695 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
700 /* Translate architecture name to id */
701 *arch = genimg_get_arch_id(data);
706 * fit_image_get_type - get type id for a given component image node
707 * @fit: pointer to the FIT format image header
708 * @noffset: component image node offset
709 * @type: pointer to the uint8_t, will hold type numeric id
711 * fit_image_get_type() finds type property in a given component image node.
712 * If the property is found, its (string) value is translated to the numeric
713 * id which is returned to the caller.
719 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
724 /* Get image type name from property data */
725 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
727 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
732 /* Translate image type name to id */
733 *type = genimg_get_type_id(data);
738 * fit_image_get_comp - get comp id for a given component image node
739 * @fit: pointer to the FIT format image header
740 * @noffset: component image node offset
741 * @comp: pointer to the uint8_t, will hold comp numeric id
743 * fit_image_get_comp() finds comp property in a given component image node.
744 * If the property is found, its (string) value is translated to the numeric
745 * id which is returned to the caller.
751 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
756 /* Get compression name from property data */
757 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
759 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
764 /* Translate compression name to id */
765 *comp = genimg_get_comp_id(data);
769 static int fit_image_get_address(const void *fit, int noffset, char *name,
776 cell = fdt_getprop(fit, noffset, name, &len);
778 fit_get_debug(fit, noffset, name, len);
782 if (len > sizeof(ulong)) {
783 printf("Unsupported %s address size\n", name);
788 /* Use load64 to avoid compiling warning for 32-bit target */
790 load64 = (load64 << 32) | uimage_to_cpu(*cell);
793 *load = (ulong)load64;
798 * fit_image_get_load() - get load addr property for given component image node
799 * @fit: pointer to the FIT format image header
800 * @noffset: component image node offset
801 * @load: pointer to the uint32_t, will hold load address
803 * fit_image_get_load() finds load address property in a given component
804 * image node. If the property is found, its value is returned to the caller.
810 int fit_image_get_load(const void *fit, int noffset, ulong *load)
812 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
816 * fit_image_get_entry() - get entry point address property
817 * @fit: pointer to the FIT format image header
818 * @noffset: component image node offset
819 * @entry: pointer to the uint32_t, will hold entry point address
821 * This gets the entry point address property for a given component image
824 * fit_image_get_entry() finds entry point address property in a given
825 * component image node. If the property is found, its value is returned
832 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
834 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
838 * fit_image_get_data - get data property and its size for a given component image node
839 * @fit: pointer to the FIT format image header
840 * @noffset: component image node offset
841 * @data: double pointer to void, will hold data property's data address
842 * @size: pointer to size_t, will hold data property's data size
844 * fit_image_get_data() finds data property in a given component image node.
845 * If the property is found its data start address and size are returned to
852 int fit_image_get_data(const void *fit, int noffset,
853 const void **data, size_t *size)
857 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
859 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
869 * Get 'data-offset' property from a given image node.
871 * @fit: pointer to the FIT image header
872 * @noffset: component image node offset
873 * @data_offset: holds the data-offset property
877 * -ENOENT if the property could not be found
879 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
883 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
887 *data_offset = fdt32_to_cpu(*val);
893 * Get 'data-position' property from a given image node.
895 * @fit: pointer to the FIT image header
896 * @noffset: component image node offset
897 * @data_position: holds the data-position property
901 * -ENOENT if the property could not be found
903 int fit_image_get_data_position(const void *fit, int noffset,
908 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
912 *data_position = fdt32_to_cpu(*val);
918 * Get 'data-size' property from a given image node.
920 * @fit: pointer to the FIT image header
921 * @noffset: component image node offset
922 * @data_size: holds the data-size property
926 * -ENOENT if the property could not be found
928 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
932 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
936 *data_size = fdt32_to_cpu(*val);
942 * fit_image_hash_get_algo - get hash algorithm name
943 * @fit: pointer to the FIT format image header
944 * @noffset: hash node offset
945 * @algo: double pointer to char, will hold pointer to the algorithm name
947 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
948 * If the property is found its data start address is returned to the caller.
954 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
958 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
960 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
968 * fit_image_hash_get_value - get hash value and length
969 * @fit: pointer to the FIT format image header
970 * @noffset: hash node offset
971 * @value: double pointer to uint8_t, will hold address of a hash value data
972 * @value_len: pointer to an int, will hold hash data length
974 * fit_image_hash_get_value() finds hash value property in a given hash node.
975 * If the property is found its data start address and size are returned to
982 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
987 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
988 if (*value == NULL) {
989 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
999 * fit_image_hash_get_ignore - get hash ignore flag
1000 * @fit: pointer to the FIT format image header
1001 * @noffset: hash node offset
1002 * @ignore: pointer to an int, will hold hash ignore flag
1004 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1005 * If the property is found and non-zero, the hash algorithm is not verified by
1006 * u-boot automatically.
1009 * 0, on ignore not found
1010 * value, on ignore found
1012 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1017 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1018 if (value == NULL || len != sizeof(int))
1026 ulong fit_get_end(const void *fit)
1028 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1032 * fit_set_timestamp - set node timestamp property
1033 * @fit: pointer to the FIT format image header
1034 * @noffset: node offset
1035 * @timestamp: timestamp value to be set
1037 * fit_set_timestamp() attempts to set timestamp property in the requested
1038 * node and returns operation status to the caller.
1042 * -ENOSPC if no space in device tree, -1 for other error
1044 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1049 t = cpu_to_uimage(timestamp);
1050 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1053 debug("Can't set '%s' property for '%s' node (%s)\n",
1054 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1056 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1063 * calculate_hash - calculate and return hash for provided input data
1064 * @data: pointer to the input data
1065 * @data_len: data length
1066 * @algo: requested hash algorithm
1067 * @value: pointer to the char, will hold hash value data (caller must
1068 * allocate enough free space)
1069 * value_len: length of the calculated hash
1071 * calculate_hash() computes input data hash according to the requested
1073 * Resulting hash value is placed in caller provided 'value' buffer, length
1074 * of the calculated hash is returned via value_len pointer argument.
1078 * -1, when algo is unsupported
1080 int calculate_hash(const void *data, int data_len, const char *algo,
1081 uint8_t *value, int *value_len)
1083 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1084 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1086 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1088 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1089 sha1_csum_wd((unsigned char *)data, data_len,
1090 (unsigned char *)value, CHUNKSZ_SHA1);
1092 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1093 sha256_csum_wd((unsigned char *)data, data_len,
1094 (unsigned char *)value, CHUNKSZ_SHA256);
1095 *value_len = SHA256_SUM_LEN;
1096 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1097 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1100 debug("Unsupported hash alogrithm\n");
1106 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1107 size_t size, char **err_msgp)
1109 uint8_t value[FIT_MAX_HASH_LEN];
1118 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1119 *err_msgp = "Can't get hash algo property";
1124 if (IMAGE_ENABLE_IGNORE) {
1125 fit_image_hash_get_ignore(fit, noffset, &ignore);
1127 printf("-skipped ");
1132 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1134 *err_msgp = "Can't get hash value property";
1138 if (calculate_hash(data, size, algo, value, &value_len)) {
1139 *err_msgp = "Unsupported hash algorithm";
1143 if (value_len != fit_value_len) {
1144 *err_msgp = "Bad hash value len";
1146 } else if (memcmp(value, fit_value, value_len) != 0) {
1147 *err_msgp = "Bad hash value";
1154 int fit_image_verify_with_data(const void *fit, int image_noffset,
1155 const void *data, size_t size)
1162 /* Verify all required signatures */
1163 if (IMAGE_ENABLE_VERIFY &&
1164 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1165 gd_fdt_blob(), &verify_all)) {
1166 err_msg = "Unable to verify required signature";
1170 /* Process all hash subnodes of the component image node */
1171 fdt_for_each_subnode(noffset, fit, image_noffset) {
1172 const char *name = fit_get_name(fit, noffset, NULL);
1175 * Check subnode name, must be equal to "hash".
1176 * Multiple hash nodes require unique unit node
1177 * names, e.g. hash-1, hash-2, etc.
1179 if (!strncmp(name, FIT_HASH_NODENAME,
1180 strlen(FIT_HASH_NODENAME))) {
1181 if (fit_image_check_hash(fit, noffset, data, size,
1185 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1186 !strncmp(name, FIT_SIG_NODENAME,
1187 strlen(FIT_SIG_NODENAME))) {
1188 ret = fit_image_check_sig(fit, noffset, data,
1189 size, -1, &err_msg);
1192 * Show an indication on failure, but do not return
1193 * an error. Only keys marked 'required' can cause
1194 * an image validation failure. See the call to
1195 * fit_image_verify_required_sigs() above.
1204 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1205 err_msg = "Corrupted or truncated tree";
1212 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1213 err_msg, fit_get_name(fit, noffset, NULL),
1214 fit_get_name(fit, image_noffset, NULL));
1219 * fit_image_verify - verify data integrity
1220 * @fit: pointer to the FIT format image header
1221 * @image_noffset: component image node offset
1223 * fit_image_verify() goes over component image hash nodes,
1224 * re-calculates each data hash and compares with the value stored in hash
1228 * 1, if all hashes are valid
1229 * 0, otherwise (or on error)
1231 int fit_image_verify(const void *fit, int image_noffset)
1238 /* Get image data and data length */
1239 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1240 err_msg = "Can't get image data/size";
1241 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1242 err_msg, fit_get_name(fit, noffset, NULL),
1243 fit_get_name(fit, image_noffset, NULL));
1247 return fit_image_verify_with_data(fit, image_noffset, data, size);
1251 * fit_all_image_verify - verify data integrity for all images
1252 * @fit: pointer to the FIT format image header
1254 * fit_all_image_verify() goes over all images in the FIT and
1255 * for every images checks if all it's hashes are valid.
1258 * 1, if all hashes of all images are valid
1259 * 0, otherwise (or on error)
1261 int fit_all_image_verify(const void *fit)
1268 /* Find images parent node offset */
1269 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1270 if (images_noffset < 0) {
1271 printf("Can't find images parent node '%s' (%s)\n",
1272 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1276 /* Process all image subnodes, check hashes for each */
1277 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1279 for (ndepth = 0, count = 0,
1280 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1281 (noffset >= 0) && (ndepth > 0);
1282 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1285 * Direct child node of the images parent node,
1286 * i.e. component image node.
1288 printf(" Hash(es) for Image %u (%s): ", count,
1289 fit_get_name(fit, noffset, NULL));
1292 if (!fit_image_verify(fit, noffset))
1301 * fit_image_check_os - check whether image node is of a given os type
1302 * @fit: pointer to the FIT format image header
1303 * @noffset: component image node offset
1304 * @os: requested image os
1306 * fit_image_check_os() reads image os property and compares its numeric
1307 * id with the requested os. Comparison result is returned to the caller.
1310 * 1 if image is of given os type
1311 * 0 otherwise (or on error)
1313 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1317 if (fit_image_get_os(fit, noffset, &image_os))
1319 return (os == image_os);
1323 * fit_image_check_arch - check whether image node is of a given arch
1324 * @fit: pointer to the FIT format image header
1325 * @noffset: component image node offset
1326 * @arch: requested imagearch
1328 * fit_image_check_arch() reads image arch property and compares its numeric
1329 * id with the requested arch. Comparison result is returned to the caller.
1332 * 1 if image is of given arch
1333 * 0 otherwise (or on error)
1335 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1338 int aarch32_support = 0;
1340 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1341 aarch32_support = 1;
1344 if (fit_image_get_arch(fit, noffset, &image_arch))
1346 return (arch == image_arch) ||
1347 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1348 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1353 * fit_image_check_type - check whether image node is of a given type
1354 * @fit: pointer to the FIT format image header
1355 * @noffset: component image node offset
1356 * @type: requested image type
1358 * fit_image_check_type() reads image type property and compares its numeric
1359 * id with the requested type. Comparison result is returned to the caller.
1362 * 1 if image is of given type
1363 * 0 otherwise (or on error)
1365 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1369 if (fit_image_get_type(fit, noffset, &image_type))
1371 return (type == image_type);
1375 * fit_image_check_comp - check whether image node uses given compression
1376 * @fit: pointer to the FIT format image header
1377 * @noffset: component image node offset
1378 * @comp: requested image compression type
1380 * fit_image_check_comp() reads image compression property and compares its
1381 * numeric id with the requested compression type. Comparison result is
1382 * returned to the caller.
1385 * 1 if image uses requested compression
1386 * 0 otherwise (or on error)
1388 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1392 if (fit_image_get_comp(fit, noffset, &image_comp))
1394 return (comp == image_comp);
1398 * fit_check_format - sanity check FIT image format
1399 * @fit: pointer to the FIT format image header
1401 * fit_check_format() runs a basic sanity FIT image verification.
1402 * Routine checks for mandatory properties, nodes, etc.
1408 int fit_check_format(const void *fit)
1410 /* mandatory / node 'description' property */
1411 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1412 debug("Wrong FIT format: no description\n");
1416 if (IMAGE_ENABLE_TIMESTAMP) {
1417 /* mandatory / node 'timestamp' property */
1418 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1419 debug("Wrong FIT format: no timestamp\n");
1424 /* mandatory subimages parent '/images' node */
1425 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1426 debug("Wrong FIT format: no images parent node\n");
1435 * fit_conf_find_compat
1436 * @fit: pointer to the FIT format image header
1437 * @fdt: pointer to the device tree to compare against
1439 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1440 * most compatible with the passed in device tree.
1449 * |-o configurations
1457 * |-compatible = "foo,bar", "bim,bam"
1460 * |-compatible = "foo,bar",
1463 * |-compatible = "bim,bam", "baz,biz"
1465 * Configuration 1 would be picked because the first string in U-Boot's
1466 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1467 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1470 * offset to the configuration to use if one was found
1473 int fit_conf_find_compat(const void *fit, const void *fdt)
1476 int noffset, confs_noffset, images_noffset;
1477 const void *fdt_compat;
1479 int best_match_offset = 0;
1480 int best_match_pos = 0;
1482 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1483 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1484 if (confs_noffset < 0 || images_noffset < 0) {
1485 debug("Can't find configurations or images nodes.\n");
1489 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1491 debug("Fdt for comparison has no \"compatible\" property.\n");
1496 * Loop over the configurations in the FIT image.
1498 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1499 (noffset >= 0) && (ndepth > 0);
1500 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1502 const char *kfdt_name;
1504 const char *cur_fdt_compat;
1512 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1514 debug("No fdt property found.\n");
1517 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1519 if (kfdt_noffset < 0) {
1520 debug("No image node named \"%s\" found.\n",
1525 * Get a pointer to this configuration's fdt.
1527 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1528 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1532 len = fdt_compat_len;
1533 cur_fdt_compat = fdt_compat;
1535 * Look for a match for each U-Boot compatibility string in
1536 * turn in this configuration's fdt.
1538 for (i = 0; len > 0 &&
1539 (!best_match_offset || best_match_pos > i); i++) {
1540 int cur_len = strlen(cur_fdt_compat) + 1;
1542 if (!fdt_node_check_compatible(kfdt, 0,
1544 best_match_offset = noffset;
1549 cur_fdt_compat += cur_len;
1552 if (!best_match_offset) {
1553 debug("No match found.\n");
1557 return best_match_offset;
1561 * fit_conf_get_node - get node offset for configuration of a given unit name
1562 * @fit: pointer to the FIT format image header
1563 * @conf_uname: configuration node unit name
1565 * fit_conf_get_node() finds a configuration (within the '/configurations'
1566 * parent node) of a provided unit name. If configuration is found its node
1567 * offset is returned to the caller.
1569 * When NULL is provided in second argument fit_conf_get_node() will search
1570 * for a default configuration node instead. Default configuration node unit
1571 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1575 * configuration node offset when found (>=0)
1576 * negative number on failure (FDT_ERR_* code)
1578 int fit_conf_get_node(const void *fit, const char *conf_uname)
1580 int noffset, confs_noffset;
1583 char *conf_uname_copy = NULL;
1585 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1586 if (confs_noffset < 0) {
1587 debug("Can't find configurations parent node '%s' (%s)\n",
1588 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1589 return confs_noffset;
1592 if (conf_uname == NULL) {
1593 /* get configuration unit name from the default property */
1594 debug("No configuration specified, trying default...\n");
1595 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1596 FIT_DEFAULT_PROP, &len);
1597 if (conf_uname == NULL) {
1598 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1602 debug("Found default configuration: '%s'\n", conf_uname);
1605 s = strchr(conf_uname, '#');
1607 len = s - conf_uname;
1608 conf_uname_copy = malloc(len + 1);
1609 if (!conf_uname_copy) {
1610 debug("Can't allocate uname copy: '%s'\n",
1614 memcpy(conf_uname_copy, conf_uname, len);
1615 conf_uname_copy[len] = '\0';
1616 conf_uname = conf_uname_copy;
1619 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1621 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1622 conf_uname, fdt_strerror(noffset));
1625 if (conf_uname_copy)
1626 free(conf_uname_copy);
1631 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1632 const char *prop_name)
1634 return fdt_stringlist_count(fit, noffset, prop_name);
1637 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1638 const char *prop_name, int index)
1643 /* get kernel image unit name from configuration kernel property */
1644 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1648 return fit_image_get_node(fit, uname);
1651 int fit_conf_get_prop_node(const void *fit, int noffset,
1652 const char *prop_name)
1654 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1657 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1659 fit_image_print(fit, rd_noffset, " ");
1662 puts(" Verifying Hash Integrity ... ");
1663 if (!fit_image_verify(fit, rd_noffset)) {
1664 puts("Bad Data Hash\n");
1673 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1680 debug("* %s: using config '%s' from image at 0x%08lx\n",
1681 prop_name, images->fit_uname_cfg, addr);
1683 /* Check whether configuration has this property defined */
1684 fit_hdr = map_sysmem(addr, 0);
1685 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1686 if (cfg_noffset < 0) {
1687 debug("* %s: no such config\n", prop_name);
1691 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1693 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1701 * fit_get_image_type_property() - get property name for IH_TYPE_...
1703 * @return the properly name where we expect to find the image in the
1706 static const char *fit_get_image_type_property(int type)
1709 * This is sort-of available in the uimage_type[] table in image.c
1710 * but we don't have access to the short name, and "fdt" is different
1711 * anyway. So let's just keep it here.
1714 case IH_TYPE_FLATDT:
1715 return FIT_FDT_PROP;
1716 case IH_TYPE_KERNEL:
1717 return FIT_KERNEL_PROP;
1718 case IH_TYPE_RAMDISK:
1719 return FIT_RAMDISK_PROP;
1720 case IH_TYPE_X86_SETUP:
1721 return FIT_SETUP_PROP;
1722 case IH_TYPE_LOADABLE:
1723 return FIT_LOADABLE_PROP;
1725 return FIT_FPGA_PROP;
1731 int fit_image_load(bootm_headers_t *images, ulong addr,
1732 const char **fit_unamep, const char **fit_uname_configp,
1733 int arch, int image_type, int bootstage_id,
1734 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1736 int cfg_noffset, noffset;
1737 const char *fit_uname;
1738 const char *fit_uname_config;
1739 const char *fit_base_uname_config;
1744 ulong load, data, len;
1749 const char *prop_name;
1752 fit = map_sysmem(addr, 0);
1753 fit_uname = fit_unamep ? *fit_unamep : NULL;
1754 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1755 fit_base_uname_config = NULL;
1756 prop_name = fit_get_image_type_property(image_type);
1757 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1759 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1760 if (!fit_check_format(fit)) {
1761 printf("Bad FIT %s image format!\n", prop_name);
1762 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1765 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1767 /* get FIT component image node offset */
1768 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1769 noffset = fit_image_get_node(fit, fit_uname);
1772 * no image node unit name, try to get config
1773 * node first. If config unit node name is NULL
1774 * fit_conf_get_node() will try to find default config node
1776 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1777 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1778 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1780 cfg_noffset = fit_conf_get_node(fit,
1783 if (cfg_noffset < 0) {
1784 puts("Could not find configuration node\n");
1785 bootstage_error(bootstage_id +
1786 BOOTSTAGE_SUB_NO_UNIT_NAME);
1789 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1790 printf(" Using '%s' configuration\n", fit_base_uname_config);
1791 if (image_type == IH_TYPE_KERNEL) {
1792 /* Remember (and possibly verify) this config */
1793 images->fit_uname_cfg = fit_base_uname_config;
1794 if (IMAGE_ENABLE_VERIFY && images->verify) {
1795 puts(" Verifying Hash Integrity ... ");
1796 if (fit_config_verify(fit, cfg_noffset)) {
1797 puts("Bad Data Hash\n");
1798 bootstage_error(bootstage_id +
1799 BOOTSTAGE_SUB_HASH);
1804 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1807 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1809 fit_uname = fit_get_name(fit, noffset, NULL);
1812 puts("Could not find subimage node\n");
1813 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1817 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1819 ret = fit_image_select(fit, noffset, images->verify);
1821 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1825 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1826 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1827 if (!fit_image_check_target_arch(fit, noffset)) {
1828 puts("Unsupported Architecture\n");
1829 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1835 fit_image_get_arch(fit, noffset, &os_arch);
1836 images->os.arch = os_arch;
1839 if (image_type == IH_TYPE_FLATDT &&
1840 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1841 puts("FDT image is compressed");
1842 return -EPROTONOSUPPORT;
1845 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1846 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1847 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1848 (image_type == IH_TYPE_KERNEL &&
1849 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1851 os_ok = image_type == IH_TYPE_FLATDT ||
1852 image_type == IH_TYPE_FPGA ||
1853 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1854 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1855 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1858 * If either of the checks fail, we should report an error, but
1859 * if the image type is coming from the "loadables" field, we
1860 * don't care what it is
1862 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
1863 fit_image_get_os(fit, noffset, &os);
1864 printf("No %s %s %s Image\n",
1865 genimg_get_os_name(os),
1866 genimg_get_arch_name(arch),
1867 genimg_get_type_name(image_type));
1868 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1872 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1874 /* get image data address and length */
1875 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1876 printf("Could not find %s subimage data!\n", prop_name);
1877 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1881 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1882 /* perform any post-processing on the image data */
1883 board_fit_image_post_process((void **)&buf, &size);
1888 /* verify that image data is a proper FDT blob */
1889 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1890 puts("Subimage data is not a FDT");
1894 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1897 * Work-around for eldk-4.2 which gives this warning if we try to
1898 * cast in the unmap_sysmem() call:
1899 * warning: initialization discards qualifiers from pointer target type
1902 void *vbuf = (void *)buf;
1904 data = map_to_sysmem(vbuf);
1907 if (load_op == FIT_LOAD_IGNORED) {
1909 } else if (fit_image_get_load(fit, noffset, &load)) {
1910 if (load_op == FIT_LOAD_REQUIRED) {
1911 printf("Can't get %s subimage load address!\n",
1913 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1916 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1917 ulong image_start, image_end;
1922 * move image data to the load address,
1923 * make sure we don't overwrite initial image
1926 image_end = addr + fit_get_size(fit);
1928 load_end = load + len;
1929 if (image_type != IH_TYPE_KERNEL &&
1930 load < image_end && load_end > image_start) {
1931 printf("Error: %s overwritten\n", prop_name);
1935 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1936 prop_name, data, load);
1938 dst = map_sysmem(load, len);
1939 memmove(dst, buf, len);
1942 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1947 *fit_unamep = (char *)fit_uname;
1948 if (fit_uname_configp)
1949 *fit_uname_configp = (char *)(fit_uname_config ? :
1950 fit_base_uname_config);
1955 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1956 ulong *setup_start, ulong *setup_len)
1963 addr = map_to_sysmem(images->fit_hdr_os);
1964 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1968 ret = fit_image_load(images, addr, NULL, NULL, arch,
1969 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1970 FIT_LOAD_REQUIRED, setup_start, &len);
1976 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
1977 const char **fit_unamep, const char **fit_uname_configp,
1978 int arch, ulong *datap, ulong *lenp)
1980 int fdt_noffset, cfg_noffset, count;
1982 const char *fit_uname = NULL;
1983 const char *fit_uname_config = NULL;
1984 char *fit_uname_config_copy = NULL;
1985 char *next_config = NULL;
1987 #ifdef CONFIG_OF_LIBFDT_OVERLAY
1988 ulong image_start, image_end;
1989 ulong ovload, ovlen;
1990 const char *uconfig;
1993 int i, err, noffset, ov_noffset;
1996 fit_uname = fit_unamep ? *fit_unamep : NULL;
1998 if (fit_uname_configp && *fit_uname_configp) {
1999 fit_uname_config_copy = strdup(*fit_uname_configp);
2000 if (!fit_uname_config_copy)
2003 next_config = strchr(fit_uname_config_copy, '#');
2005 *next_config++ = '\0';
2006 if (next_config - 1 > fit_uname_config_copy)
2007 fit_uname_config = fit_uname_config_copy;
2010 fdt_noffset = fit_image_load(images,
2011 addr, &fit_uname, &fit_uname_config,
2012 arch, IH_TYPE_FLATDT,
2013 BOOTSTAGE_ID_FIT_FDT_START,
2014 FIT_LOAD_OPTIONAL, &load, &len);
2016 if (fdt_noffset < 0)
2019 debug("fit_uname=%s, fit_uname_config=%s\n",
2020 fit_uname ? fit_uname : "<NULL>",
2021 fit_uname_config ? fit_uname_config : "<NULL>");
2023 fit = map_sysmem(addr, 0);
2025 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2027 /* single blob, or error just return as well */
2028 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2029 if (count <= 1 && !next_config)
2032 /* we need to apply overlays */
2034 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2036 image_end = addr + fit_get_size(fit);
2037 /* verify that relocation took place by load address not being in fit */
2038 if (load >= image_start && load < image_end) {
2039 /* check is simplified; fit load checks for overlaps */
2040 printf("Overlayed FDT requires relocation\n");
2041 fdt_noffset = -EBADF;
2045 base = map_sysmem(load, len);
2047 /* apply extra configs in FIT first, followed by args */
2048 for (i = 1; ; i++) {
2050 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2052 uname = fit_get_name(fit, noffset, NULL);
2057 uconfig = next_config;
2058 next_config = strchr(next_config, '#');
2060 *next_config++ = '\0';
2064 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2066 ov_noffset = fit_image_load(images,
2067 addr, &uname, &uconfig,
2068 arch, IH_TYPE_FLATDT,
2069 BOOTSTAGE_ID_FIT_FDT_START,
2070 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2071 if (ov_noffset < 0) {
2072 printf("load of %s failed\n", uname);
2075 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2076 uname, ovload, ovlen);
2077 ov = map_sysmem(ovload, ovlen);
2079 base = map_sysmem(load, len + ovlen);
2080 err = fdt_open_into(base, base, len + ovlen);
2082 printf("failed on fdt_open_into\n");
2086 /* the verbose method prints out messages on error */
2087 err = fdt_overlay_apply_verbose(base, ov);
2093 len = fdt_totalsize(base);
2096 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2097 fdt_noffset = -EBADF;
2106 *fit_unamep = fit_uname;
2107 if (fit_uname_configp)
2108 *fit_uname_configp = fit_uname_config;
2110 if (fit_uname_config_copy)
2111 free(fit_uname_config_copy);