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,
172 debug("%s %s node: '%s'\n", p, type,
173 fit_get_name(fit, noffset, NULL));
174 printf("%s %s algo: ", p, type);
175 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
176 printf("invalid/unsupported\n");
180 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
181 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
183 printf(":%s", keyname);
185 printf(" (required)");
188 padding = fdt_getprop(fit, noffset, "padding", NULL);
190 printf("%s %s padding: %s\n", p, type, padding);
192 ret = fit_image_hash_get_value(fit, noffset, &value,
194 printf("%s %s value: ", p, type);
196 printf("unavailable\n");
198 for (i = 0; i < value_len; i++)
199 printf("%02x", value[i]);
203 debug("%s %s len: %d\n", p, type, value_len);
205 /* Signatures have a time stamp */
206 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
209 printf("%s Timestamp: ", p);
210 if (fit_get_timestamp(fit, noffset, ×tamp))
211 printf("unavailable\n");
213 genimg_print_time(timestamp);
218 * fit_image_print_verification_data() - prints out the hash/signature details
219 * @fit: pointer to the FIT format image header
220 * @noffset: offset of the hash or signature node
221 * @p: pointer to prefix string
223 * This lists properties for the processed hash node
226 * no returned results
228 static void fit_image_print_verification_data(const void *fit, int noffset,
234 * Check subnode name, must be equal to "hash" or "signature".
235 * Multiple hash/signature nodes require unique unit node
236 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
238 name = fit_get_name(fit, noffset, NULL);
239 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
240 fit_image_print_data(fit, noffset, p, "Hash");
241 } else if (!strncmp(name, FIT_SIG_NODENAME,
242 strlen(FIT_SIG_NODENAME))) {
243 fit_image_print_data(fit, noffset, p, "Sign");
248 * fit_conf_print - prints out the FIT configuration details
249 * @fit: pointer to the FIT format image header
250 * @noffset: offset of the configuration node
251 * @p: pointer to prefix string
253 * fit_conf_print() lists all mandatory properties for the processed
254 * configuration node.
257 * no returned results
259 static void fit_conf_print(const void *fit, int noffset, const char *p)
264 int fdt_index, loadables_index;
267 /* Mandatory properties */
268 ret = fit_get_desc(fit, noffset, &desc);
269 printf("%s Description: ", p);
271 printf("unavailable\n");
273 printf("%s\n", desc);
275 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
276 printf("%s Kernel: ", p);
278 printf("unavailable\n");
280 printf("%s\n", uname);
282 /* Optional properties */
283 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
285 printf("%s Init Ramdisk: %s\n", p, uname);
287 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
289 printf("%s Firmware: %s\n", p, uname);
292 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
293 fdt_index, NULL), uname;
296 printf("%s FDT: ", p);
299 printf("%s\n", uname);
302 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
304 printf("%s FPGA: %s\n", p, uname);
306 /* Print out all of the specified loadables */
307 for (loadables_index = 0;
308 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
309 loadables_index, NULL), uname;
311 if (loadables_index == 0) {
312 printf("%s Loadables: ", p);
316 printf("%s\n", uname);
319 /* Process all hash subnodes of the component configuration node */
320 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
321 (noffset >= 0) && (ndepth > 0);
322 noffset = fdt_next_node(fit, noffset, &ndepth)) {
324 /* Direct child node of the component configuration node */
325 fit_image_print_verification_data(fit, noffset, p);
331 * fit_print_contents - prints out the contents of the FIT format image
332 * @fit: pointer to the FIT format image header
333 * @p: pointer to prefix string
335 * fit_print_contents() formats a multi line FIT image contents description.
336 * The routine prints out FIT image properties (root node level) followed by
337 * the details of each component image.
340 * no returned results
342 void fit_print_contents(const void *fit)
355 /* Indent string is defined in header image.h */
356 p = IMAGE_INDENT_STRING;
358 /* Root node properties */
359 ret = fit_get_desc(fit, 0, &desc);
360 printf("%sFIT description: ", p);
362 printf("unavailable\n");
364 printf("%s\n", desc);
366 if (IMAGE_ENABLE_TIMESTAMP) {
367 ret = fit_get_timestamp(fit, 0, ×tamp);
368 printf("%sCreated: ", p);
370 printf("unavailable\n");
372 genimg_print_time(timestamp);
375 /* Find images parent node offset */
376 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
377 if (images_noffset < 0) {
378 printf("Can't find images parent node '%s' (%s)\n",
379 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
383 /* Process its subnodes, print out component images details */
384 for (ndepth = 0, count = 0,
385 noffset = fdt_next_node(fit, images_noffset, &ndepth);
386 (noffset >= 0) && (ndepth > 0);
387 noffset = fdt_next_node(fit, noffset, &ndepth)) {
390 * Direct child node of the images parent node,
391 * i.e. component image node.
393 printf("%s Image %u (%s)\n", p, count++,
394 fit_get_name(fit, noffset, NULL));
396 fit_image_print(fit, noffset, p);
400 /* Find configurations parent node offset */
401 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
402 if (confs_noffset < 0) {
403 debug("Can't get configurations parent node '%s' (%s)\n",
404 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
408 /* get default configuration unit name from default property */
409 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
411 printf("%s Default Configuration: '%s'\n", p, uname);
413 /* Process its subnodes, print out configurations details */
414 for (ndepth = 0, count = 0,
415 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
416 (noffset >= 0) && (ndepth > 0);
417 noffset = fdt_next_node(fit, noffset, &ndepth)) {
420 * Direct child node of the configurations parent node,
421 * i.e. configuration node.
423 printf("%s Configuration %u (%s)\n", p, count++,
424 fit_get_name(fit, noffset, NULL));
426 fit_conf_print(fit, noffset, p);
432 * fit_image_print - prints out the FIT component image details
433 * @fit: pointer to the FIT format image header
434 * @image_noffset: offset of the component image node
435 * @p: pointer to prefix string
437 * fit_image_print() lists all mandatory properties for the processed component
438 * image. If present, hash nodes are printed out as well. Load
439 * address for images of type firmware is also printed out. Since the load
440 * address is not mandatory for firmware images, it will be output as
441 * "unavailable" when not present.
444 * no returned results
446 void fit_image_print(const void *fit, int image_noffset, const char *p)
449 uint8_t type, arch, os, comp;
457 /* Mandatory properties */
458 ret = fit_get_desc(fit, image_noffset, &desc);
459 printf("%s Description: ", p);
461 printf("unavailable\n");
463 printf("%s\n", desc);
465 if (IMAGE_ENABLE_TIMESTAMP) {
468 ret = fit_get_timestamp(fit, 0, ×tamp);
469 printf("%s Created: ", p);
471 printf("unavailable\n");
473 genimg_print_time(timestamp);
476 fit_image_get_type(fit, image_noffset, &type);
477 printf("%s Type: %s\n", p, genimg_get_type_name(type));
479 fit_image_get_comp(fit, image_noffset, &comp);
480 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
482 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
485 printf("%s Data Start: ", p);
487 printf("unavailable\n");
489 void *vdata = (void *)data;
491 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
495 printf("%s Data Size: ", p);
497 printf("unavailable\n");
499 genimg_print_size(size);
501 /* Remaining, type dependent properties */
502 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
503 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
504 (type == IH_TYPE_FLATDT)) {
505 fit_image_get_arch(fit, image_noffset, &arch);
506 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
509 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
510 (type == IH_TYPE_FIRMWARE)) {
511 fit_image_get_os(fit, image_noffset, &os);
512 printf("%s OS: %s\n", p, genimg_get_os_name(os));
515 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
516 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
517 (type == IH_TYPE_FPGA)) {
518 ret = fit_image_get_load(fit, image_noffset, &load);
519 printf("%s Load Address: ", p);
521 printf("unavailable\n");
523 printf("0x%08lx\n", load);
526 /* optional load address for FDT */
527 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
528 printf("%s Load Address: 0x%08lx\n", p, load);
530 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
531 (type == IH_TYPE_RAMDISK)) {
532 ret = fit_image_get_entry(fit, image_noffset, &entry);
533 printf("%s Entry Point: ", p);
535 printf("unavailable\n");
537 printf("0x%08lx\n", entry);
540 /* Process all hash subnodes of the component image node */
541 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
542 (noffset >= 0) && (ndepth > 0);
543 noffset = fdt_next_node(fit, noffset, &ndepth)) {
545 /* Direct child node of the component image node */
546 fit_image_print_verification_data(fit, noffset, p);
551 void fit_print_contents(const void *fit) { }
552 void fit_image_print(const void *fit, int image_noffset, const char *p) { }
553 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
556 * fit_get_desc - get node description property
557 * @fit: pointer to the FIT format image header
558 * @noffset: node offset
559 * @desc: double pointer to the char, will hold pointer to the description
561 * fit_get_desc() reads description property from a given node, if
562 * description is found pointer to it is returned in third call argument.
568 int fit_get_desc(const void *fit, int noffset, char **desc)
572 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
574 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
582 * fit_get_timestamp - get node timestamp property
583 * @fit: pointer to the FIT format image header
584 * @noffset: node offset
585 * @timestamp: pointer to the time_t, will hold read timestamp
587 * fit_get_timestamp() reads timestamp property from given node, if timestamp
588 * is found and has a correct size its value is returned in third call
593 * -1, on property read failure
594 * -2, on wrong timestamp size
596 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
601 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
603 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
606 if (len != sizeof(uint32_t)) {
607 debug("FIT timestamp with incorrect size of (%u)\n", len);
611 *timestamp = uimage_to_cpu(*((uint32_t *)data));
616 * fit_image_get_node - get node offset for component image of a given unit name
617 * @fit: pointer to the FIT format image header
618 * @image_uname: component image node unit name
620 * fit_image_get_node() finds a component image (within the '/images'
621 * node) of a provided unit name. If image is found its node offset is
622 * returned to the caller.
625 * image node offset when found (>=0)
626 * negative number on failure (FDT_ERR_* code)
628 int fit_image_get_node(const void *fit, const char *image_uname)
630 int noffset, images_noffset;
632 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
633 if (images_noffset < 0) {
634 debug("Can't find images parent node '%s' (%s)\n",
635 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
636 return images_noffset;
639 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
641 debug("Can't get node offset for image unit name: '%s' (%s)\n",
642 image_uname, fdt_strerror(noffset));
649 * fit_image_get_os - get os id for a given component image node
650 * @fit: pointer to the FIT format image header
651 * @noffset: component image node offset
652 * @os: pointer to the uint8_t, will hold os numeric id
654 * fit_image_get_os() finds os property in a given component image node.
655 * If the property is found, its (string) value is translated to the numeric
656 * id which is returned to the caller.
662 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
667 /* Get OS name from property data */
668 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
670 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
675 /* Translate OS name to id */
676 *os = genimg_get_os_id(data);
681 * fit_image_get_arch - get arch id for a given component image node
682 * @fit: pointer to the FIT format image header
683 * @noffset: component image node offset
684 * @arch: pointer to the uint8_t, will hold arch numeric id
686 * fit_image_get_arch() finds arch property in a given component image node.
687 * If the property is found, its (string) value is translated to the numeric
688 * id which is returned to the caller.
694 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
699 /* Get architecture name from property data */
700 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
702 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
707 /* Translate architecture name to id */
708 *arch = genimg_get_arch_id(data);
713 * fit_image_get_type - get type id for a given component image node
714 * @fit: pointer to the FIT format image header
715 * @noffset: component image node offset
716 * @type: pointer to the uint8_t, will hold type numeric id
718 * fit_image_get_type() finds type property in a given component image node.
719 * If the property is found, its (string) value is translated to the numeric
720 * id which is returned to the caller.
726 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
731 /* Get image type name from property data */
732 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
734 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
739 /* Translate image type name to id */
740 *type = genimg_get_type_id(data);
745 * fit_image_get_comp - get comp id for a given component image node
746 * @fit: pointer to the FIT format image header
747 * @noffset: component image node offset
748 * @comp: pointer to the uint8_t, will hold comp numeric id
750 * fit_image_get_comp() finds comp property in a given component image node.
751 * If the property is found, its (string) value is translated to the numeric
752 * id which is returned to the caller.
758 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
763 /* Get compression name from property data */
764 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
766 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
771 /* Translate compression name to id */
772 *comp = genimg_get_comp_id(data);
776 static int fit_image_get_address(const void *fit, int noffset, char *name,
783 cell = fdt_getprop(fit, noffset, name, &len);
785 fit_get_debug(fit, noffset, name, len);
789 if (len > sizeof(ulong)) {
790 printf("Unsupported %s address size\n", name);
795 /* Use load64 to avoid compiling warning for 32-bit target */
797 load64 = (load64 << 32) | uimage_to_cpu(*cell);
800 *load = (ulong)load64;
805 * fit_image_get_load() - get load addr property for given component image node
806 * @fit: pointer to the FIT format image header
807 * @noffset: component image node offset
808 * @load: pointer to the uint32_t, will hold load address
810 * fit_image_get_load() finds load address property in a given component
811 * image node. If the property is found, its value is returned to the caller.
817 int fit_image_get_load(const void *fit, int noffset, ulong *load)
819 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
823 * fit_image_get_entry() - get entry point address property
824 * @fit: pointer to the FIT format image header
825 * @noffset: component image node offset
826 * @entry: pointer to the uint32_t, will hold entry point address
828 * This gets the entry point address property for a given component image
831 * fit_image_get_entry() finds entry point address property in a given
832 * component image node. If the property is found, its value is returned
839 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
841 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
845 * fit_image_get_data - get data property and its size for a given component image node
846 * @fit: pointer to the FIT format image header
847 * @noffset: component image node offset
848 * @data: double pointer to void, will hold data property's data address
849 * @size: pointer to size_t, will hold data property's data size
851 * fit_image_get_data() finds data property in a given component image node.
852 * If the property is found its data start address and size are returned to
859 int fit_image_get_data(const void *fit, int noffset,
860 const void **data, size_t *size)
864 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
866 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
876 * Get 'data-offset' property from a given image node.
878 * @fit: pointer to the FIT image header
879 * @noffset: component image node offset
880 * @data_offset: holds the data-offset property
884 * -ENOENT if the property could not be found
886 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
890 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
894 *data_offset = fdt32_to_cpu(*val);
900 * Get 'data-position' property from a given image node.
902 * @fit: pointer to the FIT image header
903 * @noffset: component image node offset
904 * @data_position: holds the data-position property
908 * -ENOENT if the property could not be found
910 int fit_image_get_data_position(const void *fit, int noffset,
915 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
919 *data_position = fdt32_to_cpu(*val);
925 * Get 'data-size' property from a given image node.
927 * @fit: pointer to the FIT image header
928 * @noffset: component image node offset
929 * @data_size: holds the data-size property
933 * -ENOENT if the property could not be found
935 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
939 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
943 *data_size = fdt32_to_cpu(*val);
949 * fit_image_get_data_and_size - get data and its size including
950 * both embedded and external data
951 * @fit: pointer to the FIT format image header
952 * @noffset: component image node offset
953 * @data: double pointer to void, will hold data property's data address
954 * @size: pointer to size_t, will hold data property's data size
956 * fit_image_get_data_and_size() finds data and its size including
957 * both embedded and external data. If the property is found
958 * its data start address and size are returned to the caller.
962 * otherwise, on failure
964 int fit_image_get_data_and_size(const void *fit, int noffset,
965 const void **data, size_t *size)
967 bool external_data = false;
972 if (!fit_image_get_data_position(fit, noffset, &offset)) {
973 external_data = true;
974 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
975 external_data = true;
977 * For FIT with external data, figure out where
978 * the external images start. This is the base
979 * for the data-offset properties in each image.
981 offset += ((fdt_totalsize(fit) + 3) & ~3);
985 debug("External Data\n");
986 ret = fit_image_get_data_size(fit, noffset, &len);
987 *data = fit + offset;
990 ret = fit_image_get_data(fit, noffset, data, size);
997 * fit_image_hash_get_algo - get hash algorithm name
998 * @fit: pointer to the FIT format image header
999 * @noffset: hash node offset
1000 * @algo: double pointer to char, will hold pointer to the algorithm name
1002 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1003 * If the property is found its data start address is returned to the caller.
1009 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1013 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1014 if (*algo == NULL) {
1015 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1023 * fit_image_hash_get_value - get hash value and length
1024 * @fit: pointer to the FIT format image header
1025 * @noffset: hash node offset
1026 * @value: double pointer to uint8_t, will hold address of a hash value data
1027 * @value_len: pointer to an int, will hold hash data length
1029 * fit_image_hash_get_value() finds hash value property in a given hash node.
1030 * If the property is found its data start address and size are returned to
1037 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1042 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1043 if (*value == NULL) {
1044 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1054 * fit_image_hash_get_ignore - get hash ignore flag
1055 * @fit: pointer to the FIT format image header
1056 * @noffset: hash node offset
1057 * @ignore: pointer to an int, will hold hash ignore flag
1059 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1060 * If the property is found and non-zero, the hash algorithm is not verified by
1061 * u-boot automatically.
1064 * 0, on ignore not found
1065 * value, on ignore found
1067 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1072 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1073 if (value == NULL || len != sizeof(int))
1081 ulong fit_get_end(const void *fit)
1083 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1087 * fit_set_timestamp - set node timestamp property
1088 * @fit: pointer to the FIT format image header
1089 * @noffset: node offset
1090 * @timestamp: timestamp value to be set
1092 * fit_set_timestamp() attempts to set timestamp property in the requested
1093 * node and returns operation status to the caller.
1097 * -ENOSPC if no space in device tree, -1 for other error
1099 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1104 t = cpu_to_uimage(timestamp);
1105 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1108 debug("Can't set '%s' property for '%s' node (%s)\n",
1109 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1111 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1118 * calculate_hash - calculate and return hash for provided input data
1119 * @data: pointer to the input data
1120 * @data_len: data length
1121 * @algo: requested hash algorithm
1122 * @value: pointer to the char, will hold hash value data (caller must
1123 * allocate enough free space)
1124 * value_len: length of the calculated hash
1126 * calculate_hash() computes input data hash according to the requested
1128 * Resulting hash value is placed in caller provided 'value' buffer, length
1129 * of the calculated hash is returned via value_len pointer argument.
1133 * -1, when algo is unsupported
1135 int calculate_hash(const void *data, int data_len, const char *algo,
1136 uint8_t *value, int *value_len)
1138 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1139 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1141 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1143 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1144 sha1_csum_wd((unsigned char *)data, data_len,
1145 (unsigned char *)value, CHUNKSZ_SHA1);
1147 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1148 sha256_csum_wd((unsigned char *)data, data_len,
1149 (unsigned char *)value, CHUNKSZ_SHA256);
1150 *value_len = SHA256_SUM_LEN;
1151 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1152 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1155 debug("Unsupported hash alogrithm\n");
1161 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1162 size_t size, char **err_msgp)
1164 uint8_t value[FIT_MAX_HASH_LEN];
1173 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1174 *err_msgp = "Can't get hash algo property";
1179 if (IMAGE_ENABLE_IGNORE) {
1180 fit_image_hash_get_ignore(fit, noffset, &ignore);
1182 printf("-skipped ");
1187 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1189 *err_msgp = "Can't get hash value property";
1193 if (calculate_hash(data, size, algo, value, &value_len)) {
1194 *err_msgp = "Unsupported hash algorithm";
1198 if (value_len != fit_value_len) {
1199 *err_msgp = "Bad hash value len";
1201 } else if (memcmp(value, fit_value, value_len) != 0) {
1202 *err_msgp = "Bad hash value";
1209 int fit_image_verify_with_data(const void *fit, int image_noffset,
1210 const void *data, size_t size)
1217 /* Verify all required signatures */
1218 if (IMAGE_ENABLE_VERIFY &&
1219 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1220 gd_fdt_blob(), &verify_all)) {
1221 err_msg = "Unable to verify required signature";
1225 /* Process all hash subnodes of the component image node */
1226 fdt_for_each_subnode(noffset, fit, image_noffset) {
1227 const char *name = fit_get_name(fit, noffset, NULL);
1230 * Check subnode name, must be equal to "hash".
1231 * Multiple hash nodes require unique unit node
1232 * names, e.g. hash-1, hash-2, etc.
1234 if (!strncmp(name, FIT_HASH_NODENAME,
1235 strlen(FIT_HASH_NODENAME))) {
1236 if (fit_image_check_hash(fit, noffset, data, size,
1240 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1241 !strncmp(name, FIT_SIG_NODENAME,
1242 strlen(FIT_SIG_NODENAME))) {
1243 ret = fit_image_check_sig(fit, noffset, data,
1244 size, -1, &err_msg);
1247 * Show an indication on failure, but do not return
1248 * an error. Only keys marked 'required' can cause
1249 * an image validation failure. See the call to
1250 * fit_image_verify_required_sigs() above.
1259 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1260 err_msg = "Corrupted or truncated tree";
1267 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1268 err_msg, fit_get_name(fit, noffset, NULL),
1269 fit_get_name(fit, image_noffset, NULL));
1274 * fit_image_verify - verify data integrity
1275 * @fit: pointer to the FIT format image header
1276 * @image_noffset: component image node offset
1278 * fit_image_verify() goes over component image hash nodes,
1279 * re-calculates each data hash and compares with the value stored in hash
1283 * 1, if all hashes are valid
1284 * 0, otherwise (or on error)
1286 int fit_image_verify(const void *fit, int image_noffset)
1293 /* Get image data and data length */
1294 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1295 err_msg = "Can't get image data/size";
1296 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1297 err_msg, fit_get_name(fit, noffset, NULL),
1298 fit_get_name(fit, image_noffset, NULL));
1302 return fit_image_verify_with_data(fit, image_noffset, data, size);
1306 * fit_all_image_verify - verify data integrity for all images
1307 * @fit: pointer to the FIT format image header
1309 * fit_all_image_verify() goes over all images in the FIT and
1310 * for every images checks if all it's hashes are valid.
1313 * 1, if all hashes of all images are valid
1314 * 0, otherwise (or on error)
1316 int fit_all_image_verify(const void *fit)
1323 /* Find images parent node offset */
1324 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1325 if (images_noffset < 0) {
1326 printf("Can't find images parent node '%s' (%s)\n",
1327 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1331 /* Process all image subnodes, check hashes for each */
1332 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1334 for (ndepth = 0, count = 0,
1335 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1336 (noffset >= 0) && (ndepth > 0);
1337 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1340 * Direct child node of the images parent node,
1341 * i.e. component image node.
1343 printf(" Hash(es) for Image %u (%s): ", count,
1344 fit_get_name(fit, noffset, NULL));
1347 if (!fit_image_verify(fit, noffset))
1356 * fit_image_check_os - check whether image node is of a given os type
1357 * @fit: pointer to the FIT format image header
1358 * @noffset: component image node offset
1359 * @os: requested image os
1361 * fit_image_check_os() reads image os property and compares its numeric
1362 * id with the requested os. Comparison result is returned to the caller.
1365 * 1 if image is of given os type
1366 * 0 otherwise (or on error)
1368 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1372 if (fit_image_get_os(fit, noffset, &image_os))
1374 return (os == image_os);
1378 * fit_image_check_arch - check whether image node is of a given arch
1379 * @fit: pointer to the FIT format image header
1380 * @noffset: component image node offset
1381 * @arch: requested imagearch
1383 * fit_image_check_arch() reads image arch property and compares its numeric
1384 * id with the requested arch. Comparison result is returned to the caller.
1387 * 1 if image is of given arch
1388 * 0 otherwise (or on error)
1390 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1393 int aarch32_support = 0;
1395 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1396 aarch32_support = 1;
1399 if (fit_image_get_arch(fit, noffset, &image_arch))
1401 return (arch == image_arch) ||
1402 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1403 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1408 * fit_image_check_type - check whether image node is of a given type
1409 * @fit: pointer to the FIT format image header
1410 * @noffset: component image node offset
1411 * @type: requested image type
1413 * fit_image_check_type() reads image type property and compares its numeric
1414 * id with the requested type. Comparison result is returned to the caller.
1417 * 1 if image is of given type
1418 * 0 otherwise (or on error)
1420 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1424 if (fit_image_get_type(fit, noffset, &image_type))
1426 return (type == image_type);
1430 * fit_image_check_comp - check whether image node uses given compression
1431 * @fit: pointer to the FIT format image header
1432 * @noffset: component image node offset
1433 * @comp: requested image compression type
1435 * fit_image_check_comp() reads image compression property and compares its
1436 * numeric id with the requested compression type. Comparison result is
1437 * returned to the caller.
1440 * 1 if image uses requested compression
1441 * 0 otherwise (or on error)
1443 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1447 if (fit_image_get_comp(fit, noffset, &image_comp))
1449 return (comp == image_comp);
1453 * fit_check_format - sanity check FIT image format
1454 * @fit: pointer to the FIT format image header
1456 * fit_check_format() runs a basic sanity FIT image verification.
1457 * Routine checks for mandatory properties, nodes, etc.
1463 int fit_check_format(const void *fit)
1465 /* mandatory / node 'description' property */
1466 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1467 debug("Wrong FIT format: no description\n");
1471 if (IMAGE_ENABLE_TIMESTAMP) {
1472 /* mandatory / node 'timestamp' property */
1473 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1474 debug("Wrong FIT format: no timestamp\n");
1479 /* mandatory subimages parent '/images' node */
1480 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1481 debug("Wrong FIT format: no images parent node\n");
1490 * fit_conf_find_compat
1491 * @fit: pointer to the FIT format image header
1492 * @fdt: pointer to the device tree to compare against
1494 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1495 * most compatible with the passed in device tree.
1504 * |-o configurations
1512 * |-compatible = "foo,bar", "bim,bam"
1515 * |-compatible = "foo,bar",
1518 * |-compatible = "bim,bam", "baz,biz"
1520 * Configuration 1 would be picked because the first string in U-Boot's
1521 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1522 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1525 * offset to the configuration to use if one was found
1528 int fit_conf_find_compat(const void *fit, const void *fdt)
1531 int noffset, confs_noffset, images_noffset;
1532 const void *fdt_compat;
1534 int best_match_offset = 0;
1535 int best_match_pos = 0;
1537 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1538 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1539 if (confs_noffset < 0 || images_noffset < 0) {
1540 debug("Can't find configurations or images nodes.\n");
1544 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1546 debug("Fdt for comparison has no \"compatible\" property.\n");
1551 * Loop over the configurations in the FIT image.
1553 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1554 (noffset >= 0) && (ndepth > 0);
1555 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1557 const char *kfdt_name;
1559 const char *cur_fdt_compat;
1567 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1569 debug("No fdt property found.\n");
1572 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1574 if (kfdt_noffset < 0) {
1575 debug("No image node named \"%s\" found.\n",
1580 * Get a pointer to this configuration's fdt.
1582 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1583 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1587 len = fdt_compat_len;
1588 cur_fdt_compat = fdt_compat;
1590 * Look for a match for each U-Boot compatibility string in
1591 * turn in this configuration's fdt.
1593 for (i = 0; len > 0 &&
1594 (!best_match_offset || best_match_pos > i); i++) {
1595 int cur_len = strlen(cur_fdt_compat) + 1;
1597 if (!fdt_node_check_compatible(kfdt, 0,
1599 best_match_offset = noffset;
1604 cur_fdt_compat += cur_len;
1607 if (!best_match_offset) {
1608 debug("No match found.\n");
1612 return best_match_offset;
1616 * fit_conf_get_node - get node offset for configuration of a given unit name
1617 * @fit: pointer to the FIT format image header
1618 * @conf_uname: configuration node unit name
1620 * fit_conf_get_node() finds a configuration (within the '/configurations'
1621 * parent node) of a provided unit name. If configuration is found its node
1622 * offset is returned to the caller.
1624 * When NULL is provided in second argument fit_conf_get_node() will search
1625 * for a default configuration node instead. Default configuration node unit
1626 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1630 * configuration node offset when found (>=0)
1631 * negative number on failure (FDT_ERR_* code)
1633 int fit_conf_get_node(const void *fit, const char *conf_uname)
1635 int noffset, confs_noffset;
1638 char *conf_uname_copy = NULL;
1640 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1641 if (confs_noffset < 0) {
1642 debug("Can't find configurations parent node '%s' (%s)\n",
1643 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1644 return confs_noffset;
1647 if (conf_uname == NULL) {
1648 /* get configuration unit name from the default property */
1649 debug("No configuration specified, trying default...\n");
1650 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1651 FIT_DEFAULT_PROP, &len);
1652 if (conf_uname == NULL) {
1653 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1657 debug("Found default configuration: '%s'\n", conf_uname);
1660 s = strchr(conf_uname, '#');
1662 len = s - conf_uname;
1663 conf_uname_copy = malloc(len + 1);
1664 if (!conf_uname_copy) {
1665 debug("Can't allocate uname copy: '%s'\n",
1669 memcpy(conf_uname_copy, conf_uname, len);
1670 conf_uname_copy[len] = '\0';
1671 conf_uname = conf_uname_copy;
1674 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1676 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1677 conf_uname, fdt_strerror(noffset));
1680 if (conf_uname_copy)
1681 free(conf_uname_copy);
1686 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1687 const char *prop_name)
1689 return fdt_stringlist_count(fit, noffset, prop_name);
1692 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1693 const char *prop_name, int index)
1698 /* get kernel image unit name from configuration kernel property */
1699 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1703 return fit_image_get_node(fit, uname);
1706 int fit_conf_get_prop_node(const void *fit, int noffset,
1707 const char *prop_name)
1709 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1712 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1714 fit_image_print(fit, rd_noffset, " ");
1717 puts(" Verifying Hash Integrity ... ");
1718 if (!fit_image_verify(fit, rd_noffset)) {
1719 puts("Bad Data Hash\n");
1728 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1735 debug("* %s: using config '%s' from image at 0x%08lx\n",
1736 prop_name, images->fit_uname_cfg, addr);
1738 /* Check whether configuration has this property defined */
1739 fit_hdr = map_sysmem(addr, 0);
1740 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1741 if (cfg_noffset < 0) {
1742 debug("* %s: no such config\n", prop_name);
1746 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1748 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1756 * fit_get_image_type_property() - get property name for IH_TYPE_...
1758 * @return the properly name where we expect to find the image in the
1761 static const char *fit_get_image_type_property(int type)
1764 * This is sort-of available in the uimage_type[] table in image.c
1765 * but we don't have access to the short name, and "fdt" is different
1766 * anyway. So let's just keep it here.
1769 case IH_TYPE_FLATDT:
1770 return FIT_FDT_PROP;
1771 case IH_TYPE_KERNEL:
1772 return FIT_KERNEL_PROP;
1773 case IH_TYPE_RAMDISK:
1774 return FIT_RAMDISK_PROP;
1775 case IH_TYPE_X86_SETUP:
1776 return FIT_SETUP_PROP;
1777 case IH_TYPE_LOADABLE:
1778 return FIT_LOADABLE_PROP;
1780 return FIT_FPGA_PROP;
1781 case IH_TYPE_STANDALONE:
1782 return FIT_STANDALONE_PROP;
1788 int fit_image_load(bootm_headers_t *images, ulong addr,
1789 const char **fit_unamep, const char **fit_uname_configp,
1790 int arch, int image_type, int bootstage_id,
1791 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1793 int cfg_noffset, noffset;
1794 const char *fit_uname;
1795 const char *fit_uname_config;
1796 const char *fit_base_uname_config;
1801 ulong load, data, len;
1806 const char *prop_name;
1809 fit = map_sysmem(addr, 0);
1810 fit_uname = fit_unamep ? *fit_unamep : NULL;
1811 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1812 fit_base_uname_config = NULL;
1813 prop_name = fit_get_image_type_property(image_type);
1814 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1816 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1817 if (!fit_check_format(fit)) {
1818 printf("Bad FIT %s image format!\n", prop_name);
1819 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1822 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1824 /* get FIT component image node offset */
1825 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1826 noffset = fit_image_get_node(fit, fit_uname);
1829 * no image node unit name, try to get config
1830 * node first. If config unit node name is NULL
1831 * fit_conf_get_node() will try to find default config node
1833 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1834 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1835 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1837 cfg_noffset = fit_conf_get_node(fit,
1840 if (cfg_noffset < 0) {
1841 puts("Could not find configuration node\n");
1842 bootstage_error(bootstage_id +
1843 BOOTSTAGE_SUB_NO_UNIT_NAME);
1847 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1848 printf(" Using '%s' configuration\n", fit_base_uname_config);
1849 /* Remember this config */
1850 if (image_type == IH_TYPE_KERNEL)
1851 images->fit_uname_cfg = fit_base_uname_config;
1853 if (IMAGE_ENABLE_VERIFY && images->verify) {
1854 puts(" Verifying Hash Integrity ... ");
1855 if (fit_config_verify(fit, cfg_noffset)) {
1856 puts("Bad Data Hash\n");
1857 bootstage_error(bootstage_id +
1858 BOOTSTAGE_SUB_HASH);
1864 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1866 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1868 fit_uname = fit_get_name(fit, noffset, NULL);
1871 puts("Could not find subimage node\n");
1872 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1876 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1878 ret = fit_image_select(fit, noffset, images->verify);
1880 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1884 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1885 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1886 if (!fit_image_check_target_arch(fit, noffset)) {
1887 puts("Unsupported Architecture\n");
1888 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1894 fit_image_get_arch(fit, noffset, &os_arch);
1895 images->os.arch = os_arch;
1898 if (image_type == IH_TYPE_FLATDT &&
1899 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1900 puts("FDT image is compressed");
1901 return -EPROTONOSUPPORT;
1904 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1905 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1906 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1907 (image_type == IH_TYPE_KERNEL &&
1908 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1910 os_ok = image_type == IH_TYPE_FLATDT ||
1911 image_type == IH_TYPE_FPGA ||
1912 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1913 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1914 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1917 * If either of the checks fail, we should report an error, but
1918 * if the image type is coming from the "loadables" field, we
1919 * don't care what it is
1921 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
1922 fit_image_get_os(fit, noffset, &os);
1923 printf("No %s %s %s Image\n",
1924 genimg_get_os_name(os),
1925 genimg_get_arch_name(arch),
1926 genimg_get_type_name(image_type));
1927 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1931 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1933 /* get image data address and length */
1934 if (fit_image_get_data_and_size(fit, noffset, &buf, &size)) {
1935 printf("Could not find %s subimage data!\n", prop_name);
1936 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1940 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1941 /* perform any post-processing on the image data */
1942 board_fit_image_post_process((void **)&buf, &size);
1947 /* verify that image data is a proper FDT blob */
1948 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1949 puts("Subimage data is not a FDT");
1953 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1956 * Work-around for eldk-4.2 which gives this warning if we try to
1957 * cast in the unmap_sysmem() call:
1958 * warning: initialization discards qualifiers from pointer target type
1961 void *vbuf = (void *)buf;
1963 data = map_to_sysmem(vbuf);
1966 if (load_op == FIT_LOAD_IGNORED) {
1968 } else if (fit_image_get_load(fit, noffset, &load)) {
1969 if (load_op == FIT_LOAD_REQUIRED) {
1970 printf("Can't get %s subimage load address!\n",
1972 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1975 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1976 ulong image_start, image_end;
1981 * move image data to the load address,
1982 * make sure we don't overwrite initial image
1985 image_end = addr + fit_get_size(fit);
1987 load_end = load + len;
1988 if (image_type != IH_TYPE_KERNEL &&
1989 load < image_end && load_end > image_start) {
1990 printf("Error: %s overwritten\n", prop_name);
1994 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1995 prop_name, data, load);
1997 dst = map_sysmem(load, len);
1998 memmove(dst, buf, len);
2001 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2006 *fit_unamep = (char *)fit_uname;
2007 if (fit_uname_configp)
2008 *fit_uname_configp = (char *)(fit_uname_config ? :
2009 fit_base_uname_config);
2014 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2015 ulong *setup_start, ulong *setup_len)
2022 addr = map_to_sysmem(images->fit_hdr_os);
2023 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2027 ret = fit_image_load(images, addr, NULL, NULL, arch,
2028 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2029 FIT_LOAD_REQUIRED, setup_start, &len);
2035 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2036 const char **fit_unamep, const char **fit_uname_configp,
2037 int arch, ulong *datap, ulong *lenp)
2039 int fdt_noffset, cfg_noffset, count;
2041 const char *fit_uname = NULL;
2042 const char *fit_uname_config = NULL;
2043 char *fit_uname_config_copy = NULL;
2044 char *next_config = NULL;
2046 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2047 ulong image_start, image_end;
2048 ulong ovload, ovlen;
2049 const char *uconfig;
2052 int i, err, noffset, ov_noffset;
2055 fit_uname = fit_unamep ? *fit_unamep : NULL;
2057 if (fit_uname_configp && *fit_uname_configp) {
2058 fit_uname_config_copy = strdup(*fit_uname_configp);
2059 if (!fit_uname_config_copy)
2062 next_config = strchr(fit_uname_config_copy, '#');
2064 *next_config++ = '\0';
2065 if (next_config - 1 > fit_uname_config_copy)
2066 fit_uname_config = fit_uname_config_copy;
2069 fdt_noffset = fit_image_load(images,
2070 addr, &fit_uname, &fit_uname_config,
2071 arch, IH_TYPE_FLATDT,
2072 BOOTSTAGE_ID_FIT_FDT_START,
2073 FIT_LOAD_OPTIONAL, &load, &len);
2075 if (fdt_noffset < 0)
2078 debug("fit_uname=%s, fit_uname_config=%s\n",
2079 fit_uname ? fit_uname : "<NULL>",
2080 fit_uname_config ? fit_uname_config : "<NULL>");
2082 fit = map_sysmem(addr, 0);
2084 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2086 /* single blob, or error just return as well */
2087 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2088 if (count <= 1 && !next_config)
2091 /* we need to apply overlays */
2093 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2095 image_end = addr + fit_get_size(fit);
2096 /* verify that relocation took place by load address not being in fit */
2097 if (load >= image_start && load < image_end) {
2098 /* check is simplified; fit load checks for overlaps */
2099 printf("Overlayed FDT requires relocation\n");
2100 fdt_noffset = -EBADF;
2104 base = map_sysmem(load, len);
2106 /* apply extra configs in FIT first, followed by args */
2107 for (i = 1; ; i++) {
2109 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2111 uname = fit_get_name(fit, noffset, NULL);
2116 uconfig = next_config;
2117 next_config = strchr(next_config, '#');
2119 *next_config++ = '\0';
2123 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2125 ov_noffset = fit_image_load(images,
2126 addr, &uname, &uconfig,
2127 arch, IH_TYPE_FLATDT,
2128 BOOTSTAGE_ID_FIT_FDT_START,
2129 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2130 if (ov_noffset < 0) {
2131 printf("load of %s failed\n", uname);
2134 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2135 uname, ovload, ovlen);
2136 ov = map_sysmem(ovload, ovlen);
2138 base = map_sysmem(load, len + ovlen);
2139 err = fdt_open_into(base, base, len + ovlen);
2141 printf("failed on fdt_open_into\n");
2145 /* the verbose method prints out messages on error */
2146 err = fdt_overlay_apply_verbose(base, ov);
2152 len = fdt_totalsize(base);
2155 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2156 fdt_noffset = -EBADF;
2165 *fit_unamep = fit_uname;
2166 if (fit_uname_configp)
2167 *fit_uname_configp = fit_uname_config;
2169 if (fit_uname_config_copy)
2170 free(fit_uname_config_copy);