1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
5 * (C) Copyright 2008 Semihalf
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 #include <u-boot/crc.h>
16 #include <linux/compiler.h>
17 #include <linux/kconfig.h>
23 DECLARE_GLOBAL_DATA_PTR;
24 #endif /* !USE_HOSTCC*/
28 #include <bootstage.h>
29 #include <u-boot/crc.h>
30 #include <u-boot/md5.h>
31 #include <u-boot/sha1.h>
32 #include <u-boot/sha256.h>
34 /*****************************************************************************/
35 /* New uImage format routines */
36 /*****************************************************************************/
38 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
39 ulong *addr, const char **name)
46 sep = strchr(spec, sepc);
49 *addr = simple_strtoul(spec, NULL, 16);
59 * fit_parse_conf - parse FIT configuration spec
60 * @spec: input string, containing configuration spec
61 * @add_curr: current image address (to be used as a possible default)
62 * @addr: pointer to a ulong variable, will hold FIT image address of a given
64 * @conf_name double pointer to a char, will hold pointer to a configuration
67 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
68 * where <addr> is a FIT image address that contains configuration
69 * with a <conf> unit name.
71 * Address part is optional, and if omitted default add_curr will
75 * 1 if spec is a valid configuration string,
76 * addr and conf_name are set accordingly
79 int fit_parse_conf(const char *spec, ulong addr_curr,
80 ulong *addr, const char **conf_name)
82 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
86 * fit_parse_subimage - parse FIT subimage spec
87 * @spec: input string, containing subimage spec
88 * @add_curr: current image address (to be used as a possible default)
89 * @addr: pointer to a ulong variable, will hold FIT image address of a given
91 * @image_name: double pointer to a char, will hold pointer to a subimage name
93 * fit_parse_subimage() expects subimage spec in the form of
94 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
95 * subimage with a <subimg> unit name.
97 * Address part is optional, and if omitted default add_curr will
101 * 1 if spec is a valid subimage string,
102 * addr and image_name are set accordingly
105 int fit_parse_subimage(const char *spec, ulong addr_curr,
106 ulong *addr, const char **image_name)
108 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
110 #endif /* !USE_HOSTCC */
112 static void fit_get_debug(const void *fit, int noffset,
113 char *prop_name, int err)
115 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
116 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
121 * fit_get_subimage_count - get component (sub-image) count
122 * @fit: pointer to the FIT format image header
123 * @images_noffset: offset of images node
126 * number of image components
128 int fit_get_subimage_count(const void *fit, int images_noffset)
134 /* Process its subnodes, print out component images details */
135 for (ndepth = 0, count = 0,
136 noffset = fdt_next_node(fit, images_noffset, &ndepth);
137 (noffset >= 0) && (ndepth > 0);
138 noffset = fdt_next_node(fit, noffset, &ndepth)) {
147 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
149 * fit_image_print_data() - prints out the hash node details
150 * @fit: pointer to the FIT format image header
151 * @noffset: offset of the hash node
152 * @p: pointer to prefix string
153 * @type: Type of information to print ("hash" or "sign")
155 * fit_image_print_data() lists properties for the processed hash node
157 * This function avoid using puts() since it prints a newline on the host
158 * but does not in U-Boot.
161 * no returned results
163 static void fit_image_print_data(const void *fit, int noffset, const char *p,
174 debug("%s %s node: '%s'\n", p, type,
175 fit_get_name(fit, noffset, NULL));
176 printf("%s %s algo: ", p, type);
177 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
178 printf("invalid/unsupported\n");
182 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
183 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
185 printf(":%s", keyname);
187 printf(" (required)");
190 padding = fdt_getprop(fit, noffset, "padding", NULL);
192 printf("%s %s padding: %s\n", p, type, padding);
194 ret = fit_image_hash_get_value(fit, noffset, &value,
196 printf("%s %s value: ", p, type);
198 printf("unavailable\n");
200 for (i = 0; i < value_len; i++)
201 printf("%02x", value[i]);
205 debug("%s %s len: %d\n", p, type, value_len);
207 /* Signatures have a time stamp */
208 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
211 printf("%s Timestamp: ", p);
212 if (fit_get_timestamp(fit, noffset, ×tamp))
213 printf("unavailable\n");
215 genimg_print_time(timestamp);
220 * fit_image_print_verification_data() - prints out the hash/signature details
221 * @fit: pointer to the FIT format image header
222 * @noffset: offset of the hash or signature node
223 * @p: pointer to prefix string
225 * This lists properties for the processed hash node
228 * no returned results
230 static void fit_image_print_verification_data(const void *fit, int noffset,
236 * Check subnode name, must be equal to "hash" or "signature".
237 * Multiple hash/signature nodes require unique unit node
238 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
240 name = fit_get_name(fit, noffset, NULL);
241 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
242 fit_image_print_data(fit, noffset, p, "Hash");
243 } else if (!strncmp(name, FIT_SIG_NODENAME,
244 strlen(FIT_SIG_NODENAME))) {
245 fit_image_print_data(fit, noffset, p, "Sign");
250 * fit_conf_print - prints out the FIT configuration details
251 * @fit: pointer to the FIT format image header
252 * @noffset: offset of the configuration node
253 * @p: pointer to prefix string
255 * fit_conf_print() lists all mandatory properties for the processed
256 * configuration node.
259 * no returned results
261 static void fit_conf_print(const void *fit, int noffset, const char *p)
266 int fdt_index, loadables_index;
269 /* Mandatory properties */
270 ret = fit_get_desc(fit, noffset, &desc);
271 printf("%s Description: ", p);
273 printf("unavailable\n");
275 printf("%s\n", desc);
277 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
278 printf("%s Kernel: ", p);
280 printf("unavailable\n");
282 printf("%s\n", uname);
284 /* Optional properties */
285 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
287 printf("%s Init Ramdisk: %s\n", p, uname);
289 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
291 printf("%s Firmware: %s\n", p, uname);
294 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
295 fdt_index, NULL), uname;
298 printf("%s FDT: ", p);
301 printf("%s\n", uname);
304 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
306 printf("%s FPGA: %s\n", p, uname);
308 /* Print out all of the specified loadables */
309 for (loadables_index = 0;
310 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
311 loadables_index, NULL), uname;
313 if (loadables_index == 0) {
314 printf("%s Loadables: ", p);
318 printf("%s\n", uname);
321 /* Process all hash subnodes of the component configuration node */
322 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
323 (noffset >= 0) && (ndepth > 0);
324 noffset = fdt_next_node(fit, noffset, &ndepth)) {
326 /* Direct child node of the component configuration node */
327 fit_image_print_verification_data(fit, noffset, p);
333 * fit_print_contents - prints out the contents of the FIT format image
334 * @fit: pointer to the FIT format image header
335 * @p: pointer to prefix string
337 * fit_print_contents() formats a multi line FIT image contents description.
338 * The routine prints out FIT image properties (root node level) followed by
339 * the details of each component image.
342 * no returned results
344 void fit_print_contents(const void *fit)
357 /* Indent string is defined in header image.h */
358 p = IMAGE_INDENT_STRING;
360 /* Root node properties */
361 ret = fit_get_desc(fit, 0, &desc);
362 printf("%sFIT description: ", p);
364 printf("unavailable\n");
366 printf("%s\n", desc);
368 if (IMAGE_ENABLE_TIMESTAMP) {
369 ret = fit_get_timestamp(fit, 0, ×tamp);
370 printf("%sCreated: ", p);
372 printf("unavailable\n");
374 genimg_print_time(timestamp);
377 /* Find images parent node offset */
378 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
379 if (images_noffset < 0) {
380 printf("Can't find images parent node '%s' (%s)\n",
381 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
385 /* Process its subnodes, print out component images details */
386 for (ndepth = 0, count = 0,
387 noffset = fdt_next_node(fit, images_noffset, &ndepth);
388 (noffset >= 0) && (ndepth > 0);
389 noffset = fdt_next_node(fit, noffset, &ndepth)) {
392 * Direct child node of the images parent node,
393 * i.e. component image node.
395 printf("%s Image %u (%s)\n", p, count++,
396 fit_get_name(fit, noffset, NULL));
398 fit_image_print(fit, noffset, p);
402 /* Find configurations parent node offset */
403 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
404 if (confs_noffset < 0) {
405 debug("Can't get configurations parent node '%s' (%s)\n",
406 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
410 /* get default configuration unit name from default property */
411 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
413 printf("%s Default Configuration: '%s'\n", p, uname);
415 /* Process its subnodes, print out configurations details */
416 for (ndepth = 0, count = 0,
417 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
418 (noffset >= 0) && (ndepth > 0);
419 noffset = fdt_next_node(fit, noffset, &ndepth)) {
422 * Direct child node of the configurations parent node,
423 * i.e. configuration node.
425 printf("%s Configuration %u (%s)\n", p, count++,
426 fit_get_name(fit, noffset, NULL));
428 fit_conf_print(fit, noffset, p);
434 * fit_image_print - prints out the FIT component image details
435 * @fit: pointer to the FIT format image header
436 * @image_noffset: offset of the component image node
437 * @p: pointer to prefix string
439 * fit_image_print() lists all mandatory properties for the processed component
440 * image. If present, hash nodes are printed out as well. Load
441 * address for images of type firmware is also printed out. Since the load
442 * address is not mandatory for firmware images, it will be output as
443 * "unavailable" when not present.
446 * no returned results
448 void fit_image_print(const void *fit, int image_noffset, const char *p)
451 uint8_t type, arch, os, comp;
459 /* Mandatory properties */
460 ret = fit_get_desc(fit, image_noffset, &desc);
461 printf("%s Description: ", p);
463 printf("unavailable\n");
465 printf("%s\n", desc);
467 if (IMAGE_ENABLE_TIMESTAMP) {
470 ret = fit_get_timestamp(fit, 0, ×tamp);
471 printf("%s Created: ", p);
473 printf("unavailable\n");
475 genimg_print_time(timestamp);
478 fit_image_get_type(fit, image_noffset, &type);
479 printf("%s Type: %s\n", p, genimg_get_type_name(type));
481 fit_image_get_comp(fit, image_noffset, &comp);
482 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
484 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
487 printf("%s Data Start: ", p);
489 printf("unavailable\n");
491 void *vdata = (void *)data;
493 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
497 printf("%s Data Size: ", p);
499 printf("unavailable\n");
501 genimg_print_size(size);
503 /* Remaining, type dependent properties */
504 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
505 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
506 (type == IH_TYPE_FLATDT)) {
507 fit_image_get_arch(fit, image_noffset, &arch);
508 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
511 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
512 (type == IH_TYPE_FIRMWARE)) {
513 fit_image_get_os(fit, image_noffset, &os);
514 printf("%s OS: %s\n", p, genimg_get_os_name(os));
517 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
518 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
519 (type == IH_TYPE_FPGA)) {
520 ret = fit_image_get_load(fit, image_noffset, &load);
521 printf("%s Load Address: ", p);
523 printf("unavailable\n");
525 printf("0x%08lx\n", load);
528 /* optional load address for FDT */
529 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
530 printf("%s Load Address: 0x%08lx\n", p, load);
532 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
533 (type == IH_TYPE_RAMDISK)) {
534 ret = fit_image_get_entry(fit, image_noffset, &entry);
535 printf("%s Entry Point: ", p);
537 printf("unavailable\n");
539 printf("0x%08lx\n", entry);
542 /* Process all hash subnodes of the component image node */
543 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
544 (noffset >= 0) && (ndepth > 0);
545 noffset = fdt_next_node(fit, noffset, &ndepth)) {
547 /* Direct child node of the component image node */
548 fit_image_print_verification_data(fit, noffset, p);
553 void fit_print_contents(const void *fit) { }
554 void fit_image_print(const void *fit, int image_noffset, const char *p) { }
555 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
558 * fit_get_desc - get node description property
559 * @fit: pointer to the FIT format image header
560 * @noffset: node offset
561 * @desc: double pointer to the char, will hold pointer to the description
563 * fit_get_desc() reads description property from a given node, if
564 * description is found pointer to it is returned in third call argument.
570 int fit_get_desc(const void *fit, int noffset, char **desc)
574 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
576 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
584 * fit_get_timestamp - get node timestamp property
585 * @fit: pointer to the FIT format image header
586 * @noffset: node offset
587 * @timestamp: pointer to the time_t, will hold read timestamp
589 * fit_get_timestamp() reads timestamp property from given node, if timestamp
590 * is found and has a correct size its value is returned in third call
595 * -1, on property read failure
596 * -2, on wrong timestamp size
598 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
603 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
605 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
608 if (len != sizeof(uint32_t)) {
609 debug("FIT timestamp with incorrect size of (%u)\n", len);
613 *timestamp = uimage_to_cpu(*((uint32_t *)data));
618 * fit_image_get_node - get node offset for component image of a given unit name
619 * @fit: pointer to the FIT format image header
620 * @image_uname: component image node unit name
622 * fit_image_get_node() finds a component image (within the '/images'
623 * node) of a provided unit name. If image is found its node offset is
624 * returned to the caller.
627 * image node offset when found (>=0)
628 * negative number on failure (FDT_ERR_* code)
630 int fit_image_get_node(const void *fit, const char *image_uname)
632 int noffset, images_noffset;
634 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
635 if (images_noffset < 0) {
636 debug("Can't find images parent node '%s' (%s)\n",
637 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
638 return images_noffset;
641 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
643 debug("Can't get node offset for image unit name: '%s' (%s)\n",
644 image_uname, fdt_strerror(noffset));
651 * fit_image_get_os - get os id for a given component image node
652 * @fit: pointer to the FIT format image header
653 * @noffset: component image node offset
654 * @os: pointer to the uint8_t, will hold os numeric id
656 * fit_image_get_os() finds os property in a given component image node.
657 * If the property is found, its (string) value is translated to the numeric
658 * id which is returned to the caller.
664 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
669 /* Get OS name from property data */
670 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
672 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
677 /* Translate OS name to id */
678 *os = genimg_get_os_id(data);
683 * fit_image_get_arch - get arch id for a given component image node
684 * @fit: pointer to the FIT format image header
685 * @noffset: component image node offset
686 * @arch: pointer to the uint8_t, will hold arch numeric id
688 * fit_image_get_arch() finds arch property in a given component image node.
689 * If the property is found, its (string) value is translated to the numeric
690 * id which is returned to the caller.
696 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
701 /* Get architecture name from property data */
702 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
704 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
709 /* Translate architecture name to id */
710 *arch = genimg_get_arch_id(data);
715 * fit_image_get_type - get type id for a given component image node
716 * @fit: pointer to the FIT format image header
717 * @noffset: component image node offset
718 * @type: pointer to the uint8_t, will hold type numeric id
720 * fit_image_get_type() finds type property in a given component image node.
721 * If the property is found, its (string) value is translated to the numeric
722 * id which is returned to the caller.
728 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
733 /* Get image type name from property data */
734 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
736 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
741 /* Translate image type name to id */
742 *type = genimg_get_type_id(data);
747 * fit_image_get_comp - get comp id for a given component image node
748 * @fit: pointer to the FIT format image header
749 * @noffset: component image node offset
750 * @comp: pointer to the uint8_t, will hold comp numeric id
752 * fit_image_get_comp() finds comp property in a given component image node.
753 * If the property is found, its (string) value is translated to the numeric
754 * id which is returned to the caller.
760 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
765 /* Get compression name from property data */
766 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
768 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
773 /* Translate compression name to id */
774 *comp = genimg_get_comp_id(data);
778 static int fit_image_get_address(const void *fit, int noffset, char *name,
785 cell = fdt_getprop(fit, noffset, name, &len);
787 fit_get_debug(fit, noffset, name, len);
791 if (len > sizeof(ulong)) {
792 printf("Unsupported %s address size\n", name);
797 /* Use load64 to avoid compiling warning for 32-bit target */
799 load64 = (load64 << 32) | uimage_to_cpu(*cell);
802 *load = (ulong)load64;
807 * fit_image_get_load() - get load addr property for given component image node
808 * @fit: pointer to the FIT format image header
809 * @noffset: component image node offset
810 * @load: pointer to the uint32_t, will hold load address
812 * fit_image_get_load() finds load address property in a given component
813 * image node. If the property is found, its value is returned to the caller.
819 int fit_image_get_load(const void *fit, int noffset, ulong *load)
821 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
825 * fit_image_get_entry() - get entry point address property
826 * @fit: pointer to the FIT format image header
827 * @noffset: component image node offset
828 * @entry: pointer to the uint32_t, will hold entry point address
830 * This gets the entry point address property for a given component image
833 * fit_image_get_entry() finds entry point address property in a given
834 * component image node. If the property is found, its value is returned
841 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
843 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
847 * fit_image_get_data - get data property and its size for a given component image node
848 * @fit: pointer to the FIT format image header
849 * @noffset: component image node offset
850 * @data: double pointer to void, will hold data property's data address
851 * @size: pointer to size_t, will hold data property's data size
853 * fit_image_get_data() finds data property in a given component image node.
854 * If the property is found its data start address and size are returned to
861 int fit_image_get_data(const void *fit, int noffset,
862 const void **data, size_t *size)
866 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
868 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
878 * Get 'data-offset' property from a given image node.
880 * @fit: pointer to the FIT image header
881 * @noffset: component image node offset
882 * @data_offset: holds the data-offset property
886 * -ENOENT if the property could not be found
888 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
892 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
896 *data_offset = fdt32_to_cpu(*val);
902 * Get 'data-position' property from a given image node.
904 * @fit: pointer to the FIT image header
905 * @noffset: component image node offset
906 * @data_position: holds the data-position property
910 * -ENOENT if the property could not be found
912 int fit_image_get_data_position(const void *fit, int noffset,
917 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
921 *data_position = fdt32_to_cpu(*val);
927 * Get 'data-size' property from a given image node.
929 * @fit: pointer to the FIT image header
930 * @noffset: component image node offset
931 * @data_size: holds the data-size property
935 * -ENOENT if the property could not be found
937 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
941 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
945 *data_size = fdt32_to_cpu(*val);
951 * fit_image_get_data_and_size - get data and its size including
952 * both embedded and external data
953 * @fit: pointer to the FIT format image header
954 * @noffset: component image node offset
955 * @data: double pointer to void, will hold data property's data address
956 * @size: pointer to size_t, will hold data property's data size
958 * fit_image_get_data_and_size() finds data and its size including
959 * both embedded and external data. If the property is found
960 * its data start address and size are returned to the caller.
964 * otherwise, on failure
966 int fit_image_get_data_and_size(const void *fit, int noffset,
967 const void **data, size_t *size)
969 bool external_data = false;
974 if (!fit_image_get_data_position(fit, noffset, &offset)) {
975 external_data = true;
976 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
977 external_data = true;
979 * For FIT with external data, figure out where
980 * the external images start. This is the base
981 * for the data-offset properties in each image.
983 offset += ((fdt_totalsize(fit) + 3) & ~3);
987 debug("External Data\n");
988 ret = fit_image_get_data_size(fit, noffset, &len);
989 *data = fit + offset;
992 ret = fit_image_get_data(fit, noffset, data, size);
999 * fit_image_hash_get_algo - get hash algorithm name
1000 * @fit: pointer to the FIT format image header
1001 * @noffset: hash node offset
1002 * @algo: double pointer to char, will hold pointer to the algorithm name
1004 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1005 * If the property is found its data start address is returned to the caller.
1011 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1015 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1016 if (*algo == NULL) {
1017 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1025 * fit_image_hash_get_value - get hash value and length
1026 * @fit: pointer to the FIT format image header
1027 * @noffset: hash node offset
1028 * @value: double pointer to uint8_t, will hold address of a hash value data
1029 * @value_len: pointer to an int, will hold hash data length
1031 * fit_image_hash_get_value() finds hash value property in a given hash node.
1032 * If the property is found its data start address and size are returned to
1039 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1044 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1045 if (*value == NULL) {
1046 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1056 * fit_image_hash_get_ignore - get hash ignore flag
1057 * @fit: pointer to the FIT format image header
1058 * @noffset: hash node offset
1059 * @ignore: pointer to an int, will hold hash ignore flag
1061 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1062 * If the property is found and non-zero, the hash algorithm is not verified by
1063 * u-boot automatically.
1066 * 0, on ignore not found
1067 * value, on ignore found
1069 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1074 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1075 if (value == NULL || len != sizeof(int))
1083 ulong fit_get_end(const void *fit)
1085 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1089 * fit_set_timestamp - set node timestamp property
1090 * @fit: pointer to the FIT format image header
1091 * @noffset: node offset
1092 * @timestamp: timestamp value to be set
1094 * fit_set_timestamp() attempts to set timestamp property in the requested
1095 * node and returns operation status to the caller.
1099 * -ENOSPC if no space in device tree, -1 for other error
1101 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1106 t = cpu_to_uimage(timestamp);
1107 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1110 debug("Can't set '%s' property for '%s' node (%s)\n",
1111 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1113 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1120 * calculate_hash - calculate and return hash for provided input data
1121 * @data: pointer to the input data
1122 * @data_len: data length
1123 * @algo: requested hash algorithm
1124 * @value: pointer to the char, will hold hash value data (caller must
1125 * allocate enough free space)
1126 * value_len: length of the calculated hash
1128 * calculate_hash() computes input data hash according to the requested
1130 * Resulting hash value is placed in caller provided 'value' buffer, length
1131 * of the calculated hash is returned via value_len pointer argument.
1135 * -1, when algo is unsupported
1137 int calculate_hash(const void *data, int data_len, const char *algo,
1138 uint8_t *value, int *value_len)
1140 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1141 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1143 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1145 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1146 sha1_csum_wd((unsigned char *)data, data_len,
1147 (unsigned char *)value, CHUNKSZ_SHA1);
1149 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1150 sha256_csum_wd((unsigned char *)data, data_len,
1151 (unsigned char *)value, CHUNKSZ_SHA256);
1152 *value_len = SHA256_SUM_LEN;
1153 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1154 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1157 debug("Unsupported hash alogrithm\n");
1163 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1164 size_t size, char **err_msgp)
1166 uint8_t value[FIT_MAX_HASH_LEN];
1175 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1176 *err_msgp = "Can't get hash algo property";
1181 if (IMAGE_ENABLE_IGNORE) {
1182 fit_image_hash_get_ignore(fit, noffset, &ignore);
1184 printf("-skipped ");
1189 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1191 *err_msgp = "Can't get hash value property";
1195 if (calculate_hash(data, size, algo, value, &value_len)) {
1196 *err_msgp = "Unsupported hash algorithm";
1200 if (value_len != fit_value_len) {
1201 *err_msgp = "Bad hash value len";
1203 } else if (memcmp(value, fit_value, value_len) != 0) {
1204 *err_msgp = "Bad hash value";
1211 int fit_image_verify_with_data(const void *fit, int image_noffset,
1212 const void *data, size_t size)
1219 /* Verify all required signatures */
1220 if (IMAGE_ENABLE_VERIFY &&
1221 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1222 gd_fdt_blob(), &verify_all)) {
1223 err_msg = "Unable to verify required signature";
1227 /* Process all hash subnodes of the component image node */
1228 fdt_for_each_subnode(noffset, fit, image_noffset) {
1229 const char *name = fit_get_name(fit, noffset, NULL);
1232 * Check subnode name, must be equal to "hash".
1233 * Multiple hash nodes require unique unit node
1234 * names, e.g. hash-1, hash-2, etc.
1236 if (!strncmp(name, FIT_HASH_NODENAME,
1237 strlen(FIT_HASH_NODENAME))) {
1238 if (fit_image_check_hash(fit, noffset, data, size,
1242 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1243 !strncmp(name, FIT_SIG_NODENAME,
1244 strlen(FIT_SIG_NODENAME))) {
1245 ret = fit_image_check_sig(fit, noffset, data,
1246 size, -1, &err_msg);
1249 * Show an indication on failure, but do not return
1250 * an error. Only keys marked 'required' can cause
1251 * an image validation failure. See the call to
1252 * fit_image_verify_required_sigs() above.
1261 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1262 err_msg = "Corrupted or truncated tree";
1269 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1270 err_msg, fit_get_name(fit, noffset, NULL),
1271 fit_get_name(fit, image_noffset, NULL));
1276 * fit_image_verify - verify data integrity
1277 * @fit: pointer to the FIT format image header
1278 * @image_noffset: component image node offset
1280 * fit_image_verify() goes over component image hash nodes,
1281 * re-calculates each data hash and compares with the value stored in hash
1285 * 1, if all hashes are valid
1286 * 0, otherwise (or on error)
1288 int fit_image_verify(const void *fit, int image_noffset)
1295 /* Get image data and data length */
1296 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1297 err_msg = "Can't get image data/size";
1298 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1299 err_msg, fit_get_name(fit, noffset, NULL),
1300 fit_get_name(fit, image_noffset, NULL));
1304 return fit_image_verify_with_data(fit, image_noffset, data, size);
1308 * fit_all_image_verify - verify data integrity for all images
1309 * @fit: pointer to the FIT format image header
1311 * fit_all_image_verify() goes over all images in the FIT and
1312 * for every images checks if all it's hashes are valid.
1315 * 1, if all hashes of all images are valid
1316 * 0, otherwise (or on error)
1318 int fit_all_image_verify(const void *fit)
1325 /* Find images parent node offset */
1326 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1327 if (images_noffset < 0) {
1328 printf("Can't find images parent node '%s' (%s)\n",
1329 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1333 /* Process all image subnodes, check hashes for each */
1334 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1336 for (ndepth = 0, count = 0,
1337 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1338 (noffset >= 0) && (ndepth > 0);
1339 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1342 * Direct child node of the images parent node,
1343 * i.e. component image node.
1345 printf(" Hash(es) for Image %u (%s): ", count,
1346 fit_get_name(fit, noffset, NULL));
1349 if (!fit_image_verify(fit, noffset))
1358 * fit_image_check_os - check whether image node is of a given os type
1359 * @fit: pointer to the FIT format image header
1360 * @noffset: component image node offset
1361 * @os: requested image os
1363 * fit_image_check_os() reads image os property and compares its numeric
1364 * id with the requested os. Comparison result is returned to the caller.
1367 * 1 if image is of given os type
1368 * 0 otherwise (or on error)
1370 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1374 if (fit_image_get_os(fit, noffset, &image_os))
1376 return (os == image_os);
1380 * fit_image_check_arch - check whether image node is of a given arch
1381 * @fit: pointer to the FIT format image header
1382 * @noffset: component image node offset
1383 * @arch: requested imagearch
1385 * fit_image_check_arch() reads image arch property and compares its numeric
1386 * id with the requested arch. Comparison result is returned to the caller.
1389 * 1 if image is of given arch
1390 * 0 otherwise (or on error)
1392 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1395 int aarch32_support = 0;
1397 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1398 aarch32_support = 1;
1401 if (fit_image_get_arch(fit, noffset, &image_arch))
1403 return (arch == image_arch) ||
1404 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1405 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1410 * fit_image_check_type - check whether image node is of a given type
1411 * @fit: pointer to the FIT format image header
1412 * @noffset: component image node offset
1413 * @type: requested image type
1415 * fit_image_check_type() reads image type property and compares its numeric
1416 * id with the requested type. Comparison result is returned to the caller.
1419 * 1 if image is of given type
1420 * 0 otherwise (or on error)
1422 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1426 if (fit_image_get_type(fit, noffset, &image_type))
1428 return (type == image_type);
1432 * fit_image_check_comp - check whether image node uses given compression
1433 * @fit: pointer to the FIT format image header
1434 * @noffset: component image node offset
1435 * @comp: requested image compression type
1437 * fit_image_check_comp() reads image compression property and compares its
1438 * numeric id with the requested compression type. Comparison result is
1439 * returned to the caller.
1442 * 1 if image uses requested compression
1443 * 0 otherwise (or on error)
1445 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1449 if (fit_image_get_comp(fit, noffset, &image_comp))
1451 return (comp == image_comp);
1455 * fit_check_format - sanity check FIT image format
1456 * @fit: pointer to the FIT format image header
1458 * fit_check_format() runs a basic sanity FIT image verification.
1459 * Routine checks for mandatory properties, nodes, etc.
1465 int fit_check_format(const void *fit)
1467 /* mandatory / node 'description' property */
1468 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1469 debug("Wrong FIT format: no description\n");
1473 if (IMAGE_ENABLE_TIMESTAMP) {
1474 /* mandatory / node 'timestamp' property */
1475 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1476 debug("Wrong FIT format: no timestamp\n");
1481 /* mandatory subimages parent '/images' node */
1482 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1483 debug("Wrong FIT format: no images parent node\n");
1492 * fit_conf_find_compat
1493 * @fit: pointer to the FIT format image header
1494 * @fdt: pointer to the device tree to compare against
1496 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1497 * most compatible with the passed in device tree.
1506 * |-o configurations
1514 * |-compatible = "foo,bar", "bim,bam"
1517 * |-compatible = "foo,bar",
1520 * |-compatible = "bim,bam", "baz,biz"
1522 * Configuration 1 would be picked because the first string in U-Boot's
1523 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1524 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1526 * As an optimization, the compatible property from the FDT's root node can be
1527 * copied into the configuration node in the FIT image. This is required to
1528 * match configurations with compressed FDTs.
1531 * offset to the configuration to use if one was found
1534 int fit_conf_find_compat(const void *fit, const void *fdt)
1537 int noffset, confs_noffset, images_noffset;
1538 const void *fdt_compat;
1540 int best_match_offset = 0;
1541 int best_match_pos = 0;
1543 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1544 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1545 if (confs_noffset < 0 || images_noffset < 0) {
1546 debug("Can't find configurations or images nodes.\n");
1550 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1552 debug("Fdt for comparison has no \"compatible\" property.\n");
1557 * Loop over the configurations in the FIT image.
1559 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1560 (noffset >= 0) && (ndepth > 0);
1561 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1563 const char *kfdt_name;
1564 int kfdt_noffset, compat_noffset;
1565 const char *cur_fdt_compat;
1573 /* If there's a compat property in the config node, use that. */
1574 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1575 fdt = fit; /* search in FIT image */
1576 compat_noffset = noffset; /* search under config node */
1577 } else { /* Otherwise extract it from the kernel FDT. */
1578 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1580 debug("No fdt property found.\n");
1583 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1585 if (kfdt_noffset < 0) {
1586 debug("No image node named \"%s\" found.\n",
1591 if (!fit_image_check_comp(fit, kfdt_noffset,
1593 debug("Can't extract compat from \"%s\" "
1594 "(compressed)\n", kfdt_name);
1598 /* search in this config's kernel FDT */
1599 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1600 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1604 compat_noffset = 0; /* search kFDT under root node */
1607 len = fdt_compat_len;
1608 cur_fdt_compat = fdt_compat;
1610 * Look for a match for each U-Boot compatibility string in
1611 * turn in the compat string property.
1613 for (i = 0; len > 0 &&
1614 (!best_match_offset || best_match_pos > i); i++) {
1615 int cur_len = strlen(cur_fdt_compat) + 1;
1617 if (!fdt_node_check_compatible(fdt, compat_noffset,
1619 best_match_offset = noffset;
1624 cur_fdt_compat += cur_len;
1627 if (!best_match_offset) {
1628 debug("No match found.\n");
1632 return best_match_offset;
1636 * fit_conf_get_node - get node offset for configuration of a given unit name
1637 * @fit: pointer to the FIT format image header
1638 * @conf_uname: configuration node unit name
1640 * fit_conf_get_node() finds a configuration (within the '/configurations'
1641 * parent node) of a provided unit name. If configuration is found its node
1642 * offset is returned to the caller.
1644 * When NULL is provided in second argument fit_conf_get_node() will search
1645 * for a default configuration node instead. Default configuration node unit
1646 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1650 * configuration node offset when found (>=0)
1651 * negative number on failure (FDT_ERR_* code)
1653 int fit_conf_get_node(const void *fit, const char *conf_uname)
1655 int noffset, confs_noffset;
1658 char *conf_uname_copy = NULL;
1660 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1661 if (confs_noffset < 0) {
1662 debug("Can't find configurations parent node '%s' (%s)\n",
1663 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1664 return confs_noffset;
1667 if (conf_uname == NULL) {
1668 /* get configuration unit name from the default property */
1669 debug("No configuration specified, trying default...\n");
1670 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1671 FIT_DEFAULT_PROP, &len);
1672 if (conf_uname == NULL) {
1673 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1677 debug("Found default configuration: '%s'\n", conf_uname);
1680 s = strchr(conf_uname, '#');
1682 len = s - conf_uname;
1683 conf_uname_copy = malloc(len + 1);
1684 if (!conf_uname_copy) {
1685 debug("Can't allocate uname copy: '%s'\n",
1689 memcpy(conf_uname_copy, conf_uname, len);
1690 conf_uname_copy[len] = '\0';
1691 conf_uname = conf_uname_copy;
1694 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1696 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1697 conf_uname, fdt_strerror(noffset));
1700 if (conf_uname_copy)
1701 free(conf_uname_copy);
1706 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1707 const char *prop_name)
1709 return fdt_stringlist_count(fit, noffset, prop_name);
1712 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1713 const char *prop_name, int index)
1718 /* get kernel image unit name from configuration kernel property */
1719 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1723 return fit_image_get_node(fit, uname);
1726 int fit_conf_get_prop_node(const void *fit, int noffset,
1727 const char *prop_name)
1729 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1732 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1734 fit_image_print(fit, rd_noffset, " ");
1737 puts(" Verifying Hash Integrity ... ");
1738 if (!fit_image_verify(fit, rd_noffset)) {
1739 puts("Bad Data Hash\n");
1748 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1755 debug("* %s: using config '%s' from image at 0x%08lx\n",
1756 prop_name, images->fit_uname_cfg, addr);
1758 /* Check whether configuration has this property defined */
1759 fit_hdr = map_sysmem(addr, 0);
1760 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1761 if (cfg_noffset < 0) {
1762 debug("* %s: no such config\n", prop_name);
1766 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1768 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1776 * fit_get_image_type_property() - get property name for IH_TYPE_...
1778 * @return the properly name where we expect to find the image in the
1781 static const char *fit_get_image_type_property(int type)
1784 * This is sort-of available in the uimage_type[] table in image.c
1785 * but we don't have access to the short name, and "fdt" is different
1786 * anyway. So let's just keep it here.
1789 case IH_TYPE_FLATDT:
1790 return FIT_FDT_PROP;
1791 case IH_TYPE_KERNEL:
1792 return FIT_KERNEL_PROP;
1793 case IH_TYPE_RAMDISK:
1794 return FIT_RAMDISK_PROP;
1795 case IH_TYPE_X86_SETUP:
1796 return FIT_SETUP_PROP;
1797 case IH_TYPE_LOADABLE:
1798 return FIT_LOADABLE_PROP;
1800 return FIT_FPGA_PROP;
1801 case IH_TYPE_STANDALONE:
1802 return FIT_STANDALONE_PROP;
1808 int fit_image_load(bootm_headers_t *images, ulong addr,
1809 const char **fit_unamep, const char **fit_uname_configp,
1810 int arch, int image_type, int bootstage_id,
1811 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1813 int cfg_noffset, noffset;
1814 const char *fit_uname;
1815 const char *fit_uname_config;
1816 const char *fit_base_uname_config;
1822 ulong load, load_end, data, len;
1827 const char *prop_name;
1830 fit = map_sysmem(addr, 0);
1831 fit_uname = fit_unamep ? *fit_unamep : NULL;
1832 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1833 fit_base_uname_config = NULL;
1834 prop_name = fit_get_image_type_property(image_type);
1835 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1837 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1838 if (!fit_check_format(fit)) {
1839 printf("Bad FIT %s image format!\n", prop_name);
1840 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1843 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1845 /* get FIT component image node offset */
1846 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1847 noffset = fit_image_get_node(fit, fit_uname);
1850 * no image node unit name, try to get config
1851 * node first. If config unit node name is NULL
1852 * fit_conf_get_node() will try to find default config node
1854 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1855 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1856 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1858 cfg_noffset = fit_conf_get_node(fit,
1861 if (cfg_noffset < 0) {
1862 puts("Could not find configuration node\n");
1863 bootstage_error(bootstage_id +
1864 BOOTSTAGE_SUB_NO_UNIT_NAME);
1868 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1869 printf(" Using '%s' configuration\n", fit_base_uname_config);
1870 /* Remember this config */
1871 if (image_type == IH_TYPE_KERNEL)
1872 images->fit_uname_cfg = fit_base_uname_config;
1874 if (IMAGE_ENABLE_VERIFY && images->verify) {
1875 puts(" Verifying Hash Integrity ... ");
1876 if (fit_config_verify(fit, cfg_noffset)) {
1877 puts("Bad Data Hash\n");
1878 bootstage_error(bootstage_id +
1879 BOOTSTAGE_SUB_HASH);
1885 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1887 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1889 fit_uname = fit_get_name(fit, noffset, NULL);
1892 puts("Could not find subimage node\n");
1893 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1897 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1899 ret = fit_image_select(fit, noffset, images->verify);
1901 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1905 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1906 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1907 if (!fit_image_check_target_arch(fit, noffset)) {
1908 puts("Unsupported Architecture\n");
1909 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1915 fit_image_get_arch(fit, noffset, &os_arch);
1916 images->os.arch = os_arch;
1919 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1920 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1921 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1922 (image_type == IH_TYPE_KERNEL &&
1923 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1925 os_ok = image_type == IH_TYPE_FLATDT ||
1926 image_type == IH_TYPE_FPGA ||
1927 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1928 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1929 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1932 * If either of the checks fail, we should report an error, but
1933 * if the image type is coming from the "loadables" field, we
1934 * don't care what it is
1936 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
1937 fit_image_get_os(fit, noffset, &os);
1938 printf("No %s %s %s Image\n",
1939 genimg_get_os_name(os),
1940 genimg_get_arch_name(arch),
1941 genimg_get_type_name(image_type));
1942 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1946 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1948 /* get image data address and length */
1949 if (fit_image_get_data_and_size(fit, noffset,
1950 (const void **)&buf, &size)) {
1951 printf("Could not find %s subimage data!\n", prop_name);
1952 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1956 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1957 /* perform any post-processing on the image data */
1958 board_fit_image_post_process(&buf, &size);
1963 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1965 data = map_to_sysmem(buf);
1967 if (load_op == FIT_LOAD_IGNORED) {
1969 } else if (fit_image_get_load(fit, noffset, &load)) {
1970 if (load_op == FIT_LOAD_REQUIRED) {
1971 printf("Can't get %s subimage load address!\n",
1973 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1976 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1977 ulong image_start, image_end;
1980 * move image data to the load address,
1981 * make sure we don't overwrite initial image
1984 image_end = addr + fit_get_size(fit);
1986 load_end = load + len;
1987 if (image_type != IH_TYPE_KERNEL &&
1988 load < image_end && load_end > image_start) {
1989 printf("Error: %s overwritten\n", prop_name);
1993 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1994 prop_name, data, load);
1996 load = data; /* No load address specified */
1999 comp = IH_COMP_NONE;
2001 /* Kernel images get decompressed later in bootm_load_os(). */
2002 if (!fit_image_get_comp(fit, noffset, &comp) &&
2003 comp != IH_COMP_NONE &&
2004 !(image_type == IH_TYPE_KERNEL ||
2005 image_type == IH_TYPE_KERNEL_NOLOAD ||
2006 image_type == IH_TYPE_RAMDISK)) {
2007 ulong max_decomp_len = len * 20;
2009 loadbuf = malloc(max_decomp_len);
2010 load = map_to_sysmem(loadbuf);
2012 loadbuf = map_sysmem(load, max_decomp_len);
2014 if (image_decomp(comp, load, data, image_type,
2015 loadbuf, buf, len, max_decomp_len, &load_end)) {
2016 printf("Error decompressing %s\n", prop_name);
2020 len = load_end - load;
2021 } else if (load != data) {
2022 loadbuf = map_sysmem(load, len);
2023 memcpy(loadbuf, buf, len);
2026 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2027 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2028 " please fix your .its file!\n");
2030 /* verify that image data is a proper FDT blob */
2031 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2032 puts("Subimage data is not a FDT");
2036 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2041 *fit_unamep = (char *)fit_uname;
2042 if (fit_uname_configp)
2043 *fit_uname_configp = (char *)(fit_uname_config ? :
2044 fit_base_uname_config);
2049 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2050 ulong *setup_start, ulong *setup_len)
2057 addr = map_to_sysmem(images->fit_hdr_os);
2058 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2062 ret = fit_image_load(images, addr, NULL, NULL, arch,
2063 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2064 FIT_LOAD_REQUIRED, setup_start, &len);
2070 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2071 const char **fit_unamep, const char **fit_uname_configp,
2072 int arch, ulong *datap, ulong *lenp)
2074 int fdt_noffset, cfg_noffset, count;
2076 const char *fit_uname = NULL;
2077 const char *fit_uname_config = NULL;
2078 char *fit_uname_config_copy = NULL;
2079 char *next_config = NULL;
2081 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2082 ulong image_start, image_end;
2083 ulong ovload, ovlen;
2084 const char *uconfig;
2087 int i, err, noffset, ov_noffset;
2090 fit_uname = fit_unamep ? *fit_unamep : NULL;
2092 if (fit_uname_configp && *fit_uname_configp) {
2093 fit_uname_config_copy = strdup(*fit_uname_configp);
2094 if (!fit_uname_config_copy)
2097 next_config = strchr(fit_uname_config_copy, '#');
2099 *next_config++ = '\0';
2100 if (next_config - 1 > fit_uname_config_copy)
2101 fit_uname_config = fit_uname_config_copy;
2104 fdt_noffset = fit_image_load(images,
2105 addr, &fit_uname, &fit_uname_config,
2106 arch, IH_TYPE_FLATDT,
2107 BOOTSTAGE_ID_FIT_FDT_START,
2108 FIT_LOAD_OPTIONAL, &load, &len);
2110 if (fdt_noffset < 0)
2113 debug("fit_uname=%s, fit_uname_config=%s\n",
2114 fit_uname ? fit_uname : "<NULL>",
2115 fit_uname_config ? fit_uname_config : "<NULL>");
2117 fit = map_sysmem(addr, 0);
2119 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2121 /* single blob, or error just return as well */
2122 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2123 if (count <= 1 && !next_config)
2126 /* we need to apply overlays */
2128 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2130 image_end = addr + fit_get_size(fit);
2131 /* verify that relocation took place by load address not being in fit */
2132 if (load >= image_start && load < image_end) {
2133 /* check is simplified; fit load checks for overlaps */
2134 printf("Overlayed FDT requires relocation\n");
2135 fdt_noffset = -EBADF;
2139 base = map_sysmem(load, len);
2141 /* apply extra configs in FIT first, followed by args */
2142 for (i = 1; ; i++) {
2144 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2146 uname = fit_get_name(fit, noffset, NULL);
2151 uconfig = next_config;
2152 next_config = strchr(next_config, '#');
2154 *next_config++ = '\0';
2158 * fit_image_load() would load the first FDT from the
2159 * extra config only when uconfig is specified.
2160 * Check if the extra config contains multiple FDTs and
2163 cfg_noffset = fit_conf_get_node(fit, uconfig);
2166 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2170 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2172 ov_noffset = fit_image_load(images,
2173 addr, &uname, &uconfig,
2174 arch, IH_TYPE_FLATDT,
2175 BOOTSTAGE_ID_FIT_FDT_START,
2176 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2177 if (ov_noffset < 0) {
2178 printf("load of %s failed\n", uname);
2181 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2182 uname, ovload, ovlen);
2183 ov = map_sysmem(ovload, ovlen);
2185 base = map_sysmem(load, len + ovlen);
2186 err = fdt_open_into(base, base, len + ovlen);
2188 printf("failed on fdt_open_into\n");
2192 /* the verbose method prints out messages on error */
2193 err = fdt_overlay_apply_verbose(base, ov);
2199 len = fdt_totalsize(base);
2202 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2203 fdt_noffset = -EBADF;
2212 *fit_unamep = fit_uname;
2213 if (fit_uname_configp)
2214 *fit_uname_configp = fit_uname_config;
2216 if (fit_uname_config_copy)
2217 free(fit_uname_config_copy);