1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
5 * (C) Copyright 2008 Semihalf
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 #include <linux/compiler.h>
16 #include <linux/kconfig.h>
22 DECLARE_GLOBAL_DATA_PTR;
23 #endif /* !USE_HOSTCC*/
26 #include <bootstage.h>
27 #include <u-boot/crc.h>
28 #include <u-boot/md5.h>
29 #include <u-boot/sha1.h>
30 #include <u-boot/sha256.h>
32 /*****************************************************************************/
33 /* New uImage format routines */
34 /*****************************************************************************/
36 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
37 ulong *addr, const char **name)
44 sep = strchr(spec, sepc);
47 *addr = simple_strtoul(spec, NULL, 16);
57 * fit_parse_conf - parse FIT configuration spec
58 * @spec: input string, containing configuration spec
59 * @add_curr: current image address (to be used as a possible default)
60 * @addr: pointer to a ulong variable, will hold FIT image address of a given
62 * @conf_name double pointer to a char, will hold pointer to a configuration
65 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
66 * where <addr> is a FIT image address that contains configuration
67 * with a <conf> unit name.
69 * Address part is optional, and if omitted default add_curr will
73 * 1 if spec is a valid configuration string,
74 * addr and conf_name are set accordingly
77 int fit_parse_conf(const char *spec, ulong addr_curr,
78 ulong *addr, const char **conf_name)
80 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
84 * fit_parse_subimage - parse FIT subimage spec
85 * @spec: input string, containing subimage spec
86 * @add_curr: current image address (to be used as a possible default)
87 * @addr: pointer to a ulong variable, will hold FIT image address of a given
89 * @image_name: double pointer to a char, will hold pointer to a subimage name
91 * fit_parse_subimage() expects subimage spec in the form of
92 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
93 * subimage with a <subimg> unit name.
95 * Address part is optional, and if omitted default add_curr will
99 * 1 if spec is a valid subimage string,
100 * addr and image_name are set accordingly
103 int fit_parse_subimage(const char *spec, ulong addr_curr,
104 ulong *addr, const char **image_name)
106 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
108 #endif /* !USE_HOSTCC */
110 static void fit_get_debug(const void *fit, int noffset,
111 char *prop_name, int err)
113 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
114 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
119 * fit_get_subimage_count - get component (sub-image) count
120 * @fit: pointer to the FIT format image header
121 * @images_noffset: offset of images node
124 * number of image components
126 int fit_get_subimage_count(const void *fit, int images_noffset)
132 /* Process its subnodes, print out component images details */
133 for (ndepth = 0, count = 0,
134 noffset = fdt_next_node(fit, images_noffset, &ndepth);
135 (noffset >= 0) && (ndepth > 0);
136 noffset = fdt_next_node(fit, noffset, &ndepth)) {
145 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
147 * fit_print_contents - prints out the contents of the FIT format image
148 * @fit: pointer to the FIT format image header
149 * @p: pointer to prefix string
151 * fit_print_contents() formats a multi line FIT image contents description.
152 * The routine prints out FIT image properties (root node level) followed by
153 * the details of each component image.
156 * no returned results
158 void fit_print_contents(const void *fit)
171 /* Indent string is defined in header image.h */
172 p = IMAGE_INDENT_STRING;
174 /* Root node properties */
175 ret = fit_get_desc(fit, 0, &desc);
176 printf("%sFIT description: ", p);
178 printf("unavailable\n");
180 printf("%s\n", desc);
182 if (IMAGE_ENABLE_TIMESTAMP) {
183 ret = fit_get_timestamp(fit, 0, ×tamp);
184 printf("%sCreated: ", p);
186 printf("unavailable\n");
188 genimg_print_time(timestamp);
191 /* Find images parent node offset */
192 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
193 if (images_noffset < 0) {
194 printf("Can't find images parent node '%s' (%s)\n",
195 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
199 /* Process its subnodes, print out component images details */
200 for (ndepth = 0, count = 0,
201 noffset = fdt_next_node(fit, images_noffset, &ndepth);
202 (noffset >= 0) && (ndepth > 0);
203 noffset = fdt_next_node(fit, noffset, &ndepth)) {
206 * Direct child node of the images parent node,
207 * i.e. component image node.
209 printf("%s Image %u (%s)\n", p, count++,
210 fit_get_name(fit, noffset, NULL));
212 fit_image_print(fit, noffset, p);
216 /* Find configurations parent node offset */
217 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
218 if (confs_noffset < 0) {
219 debug("Can't get configurations parent node '%s' (%s)\n",
220 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
224 /* get default configuration unit name from default property */
225 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
227 printf("%s Default Configuration: '%s'\n", p, uname);
229 /* Process its subnodes, print out configurations details */
230 for (ndepth = 0, count = 0,
231 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
232 (noffset >= 0) && (ndepth > 0);
233 noffset = fdt_next_node(fit, noffset, &ndepth)) {
236 * Direct child node of the configurations parent node,
237 * i.e. configuration node.
239 printf("%s Configuration %u (%s)\n", p, count++,
240 fit_get_name(fit, noffset, NULL));
242 fit_conf_print(fit, noffset, p);
248 * fit_image_print_data() - prints out the hash node details
249 * @fit: pointer to the FIT format image header
250 * @noffset: offset of the hash node
251 * @p: pointer to prefix string
252 * @type: Type of information to print ("hash" or "sign")
254 * fit_image_print_data() lists properties for the processed hash node
256 * This function avoid using puts() since it prints a newline on the host
257 * but does not in U-Boot.
260 * no returned results
262 static void fit_image_print_data(const void *fit, int noffset, const char *p,
272 debug("%s %s node: '%s'\n", p, type,
273 fit_get_name(fit, noffset, NULL));
274 printf("%s %s algo: ", p, type);
275 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
276 printf("invalid/unsupported\n");
280 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
281 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
283 printf(":%s", keyname);
285 printf(" (required)");
288 ret = fit_image_hash_get_value(fit, noffset, &value,
290 printf("%s %s value: ", p, type);
292 printf("unavailable\n");
294 for (i = 0; i < value_len; i++)
295 printf("%02x", value[i]);
299 debug("%s %s len: %d\n", p, type, value_len);
301 /* Signatures have a time stamp */
302 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
305 printf("%s Timestamp: ", p);
306 if (fit_get_timestamp(fit, noffset, ×tamp))
307 printf("unavailable\n");
309 genimg_print_time(timestamp);
314 * fit_image_print_verification_data() - prints out the hash/signature details
315 * @fit: pointer to the FIT format image header
316 * @noffset: offset of the hash or signature node
317 * @p: pointer to prefix string
319 * This lists properties for the processed hash node
322 * no returned results
324 static void fit_image_print_verification_data(const void *fit, int noffset,
330 * Check subnode name, must be equal to "hash" or "signature".
331 * Multiple hash/signature nodes require unique unit node
332 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
334 name = fit_get_name(fit, noffset, NULL);
335 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
336 fit_image_print_data(fit, noffset, p, "Hash");
337 } else if (!strncmp(name, FIT_SIG_NODENAME,
338 strlen(FIT_SIG_NODENAME))) {
339 fit_image_print_data(fit, noffset, p, "Sign");
344 * fit_image_print - prints out the FIT component image details
345 * @fit: pointer to the FIT format image header
346 * @image_noffset: offset of the component image node
347 * @p: pointer to prefix string
349 * fit_image_print() lists all mandatory properties for the processed component
350 * image. If present, hash nodes are printed out as well. Load
351 * address for images of type firmware is also printed out. Since the load
352 * address is not mandatory for firmware images, it will be output as
353 * "unavailable" when not present.
356 * no returned results
358 void fit_image_print(const void *fit, int image_noffset, const char *p)
361 uint8_t type, arch, os, comp;
369 /* Mandatory properties */
370 ret = fit_get_desc(fit, image_noffset, &desc);
371 printf("%s Description: ", p);
373 printf("unavailable\n");
375 printf("%s\n", desc);
377 if (IMAGE_ENABLE_TIMESTAMP) {
380 ret = fit_get_timestamp(fit, 0, ×tamp);
381 printf("%s Created: ", p);
383 printf("unavailable\n");
385 genimg_print_time(timestamp);
388 fit_image_get_type(fit, image_noffset, &type);
389 printf("%s Type: %s\n", p, genimg_get_type_name(type));
391 fit_image_get_comp(fit, image_noffset, &comp);
392 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
394 ret = fit_image_get_data(fit, image_noffset, &data, &size);
397 printf("%s Data Start: ", p);
399 printf("unavailable\n");
401 void *vdata = (void *)data;
403 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
407 printf("%s Data Size: ", p);
409 printf("unavailable\n");
411 genimg_print_size(size);
413 /* Remaining, type dependent properties */
414 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
415 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
416 (type == IH_TYPE_FLATDT)) {
417 fit_image_get_arch(fit, image_noffset, &arch);
418 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
421 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
422 (type == IH_TYPE_FIRMWARE)) {
423 fit_image_get_os(fit, image_noffset, &os);
424 printf("%s OS: %s\n", p, genimg_get_os_name(os));
427 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
428 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
429 (type == IH_TYPE_FPGA)) {
430 ret = fit_image_get_load(fit, image_noffset, &load);
431 printf("%s Load Address: ", p);
433 printf("unavailable\n");
435 printf("0x%08lx\n", load);
438 /* optional load address for FDT */
439 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
440 printf("%s Load Address: 0x%08lx\n", p, load);
442 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
443 (type == IH_TYPE_RAMDISK)) {
444 ret = fit_image_get_entry(fit, image_noffset, &entry);
445 printf("%s Entry Point: ", p);
447 printf("unavailable\n");
449 printf("0x%08lx\n", entry);
452 /* Process all hash subnodes of the component image node */
453 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
454 (noffset >= 0) && (ndepth > 0);
455 noffset = fdt_next_node(fit, noffset, &ndepth)) {
457 /* Direct child node of the component image node */
458 fit_image_print_verification_data(fit, noffset, p);
463 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
466 * fit_get_desc - get node description property
467 * @fit: pointer to the FIT format image header
468 * @noffset: node offset
469 * @desc: double pointer to the char, will hold pointer to the description
471 * fit_get_desc() reads description property from a given node, if
472 * description is found pointer to it is returned in third call argument.
478 int fit_get_desc(const void *fit, int noffset, char **desc)
482 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
484 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
492 * fit_get_timestamp - get node timestamp property
493 * @fit: pointer to the FIT format image header
494 * @noffset: node offset
495 * @timestamp: pointer to the time_t, will hold read timestamp
497 * fit_get_timestamp() reads timestamp property from given node, if timestamp
498 * is found and has a correct size its value is returned in third call
503 * -1, on property read failure
504 * -2, on wrong timestamp size
506 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
511 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
513 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
516 if (len != sizeof(uint32_t)) {
517 debug("FIT timestamp with incorrect size of (%u)\n", len);
521 *timestamp = uimage_to_cpu(*((uint32_t *)data));
526 * fit_image_get_node - get node offset for component image of a given unit name
527 * @fit: pointer to the FIT format image header
528 * @image_uname: component image node unit name
530 * fit_image_get_node() finds a component image (within the '/images'
531 * node) of a provided unit name. If image is found its node offset is
532 * returned to the caller.
535 * image node offset when found (>=0)
536 * negative number on failure (FDT_ERR_* code)
538 int fit_image_get_node(const void *fit, const char *image_uname)
540 int noffset, images_noffset;
542 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
543 if (images_noffset < 0) {
544 debug("Can't find images parent node '%s' (%s)\n",
545 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
546 return images_noffset;
549 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
551 debug("Can't get node offset for image unit name: '%s' (%s)\n",
552 image_uname, fdt_strerror(noffset));
559 * fit_image_get_os - get os id for a given component image node
560 * @fit: pointer to the FIT format image header
561 * @noffset: component image node offset
562 * @os: pointer to the uint8_t, will hold os numeric id
564 * fit_image_get_os() finds os property in a given component image node.
565 * If the property is found, its (string) value is translated to the numeric
566 * id which is returned to the caller.
572 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
577 /* Get OS name from property data */
578 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
580 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
585 /* Translate OS name to id */
586 *os = genimg_get_os_id(data);
591 * fit_image_get_arch - get arch id for a given component image node
592 * @fit: pointer to the FIT format image header
593 * @noffset: component image node offset
594 * @arch: pointer to the uint8_t, will hold arch numeric id
596 * fit_image_get_arch() finds arch property in a given component image node.
597 * If the property is found, its (string) value is translated to the numeric
598 * id which is returned to the caller.
604 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
609 /* Get architecture name from property data */
610 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
612 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
617 /* Translate architecture name to id */
618 *arch = genimg_get_arch_id(data);
623 * fit_image_get_type - get type id for a given component image node
624 * @fit: pointer to the FIT format image header
625 * @noffset: component image node offset
626 * @type: pointer to the uint8_t, will hold type numeric id
628 * fit_image_get_type() finds type property in a given component image node.
629 * If the property is found, its (string) value is translated to the numeric
630 * id which is returned to the caller.
636 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
641 /* Get image type name from property data */
642 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
644 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
649 /* Translate image type name to id */
650 *type = genimg_get_type_id(data);
655 * fit_image_get_comp - get comp id for a given component image node
656 * @fit: pointer to the FIT format image header
657 * @noffset: component image node offset
658 * @comp: pointer to the uint8_t, will hold comp numeric id
660 * fit_image_get_comp() finds comp property in a given component image node.
661 * If the property is found, its (string) value is translated to the numeric
662 * id which is returned to the caller.
668 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
673 /* Get compression name from property data */
674 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
676 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
681 /* Translate compression name to id */
682 *comp = genimg_get_comp_id(data);
686 static int fit_image_get_address(const void *fit, int noffset, char *name,
693 cell = fdt_getprop(fit, noffset, name, &len);
695 fit_get_debug(fit, noffset, name, len);
699 if (len > sizeof(ulong)) {
700 printf("Unsupported %s address size\n", name);
705 /* Use load64 to avoid compiling warning for 32-bit target */
707 load64 = (load64 << 32) | uimage_to_cpu(*cell);
710 *load = (ulong)load64;
715 * fit_image_get_load() - get load addr property for given component image node
716 * @fit: pointer to the FIT format image header
717 * @noffset: component image node offset
718 * @load: pointer to the uint32_t, will hold load address
720 * fit_image_get_load() finds load address property in a given component
721 * image node. If the property is found, its value is returned to the caller.
727 int fit_image_get_load(const void *fit, int noffset, ulong *load)
729 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
733 * fit_image_get_entry() - get entry point address property
734 * @fit: pointer to the FIT format image header
735 * @noffset: component image node offset
736 * @entry: pointer to the uint32_t, will hold entry point address
738 * This gets the entry point address property for a given component image
741 * fit_image_get_entry() finds entry point address property in a given
742 * component image node. If the property is found, its value is returned
749 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
751 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
755 * fit_image_get_data - get data property and its size for a given component image node
756 * @fit: pointer to the FIT format image header
757 * @noffset: component image node offset
758 * @data: double pointer to void, will hold data property's data address
759 * @size: pointer to size_t, will hold data property's data size
761 * fit_image_get_data() finds data property in a given component image node.
762 * If the property is found its data start address and size are returned to
769 int fit_image_get_data(const void *fit, int noffset,
770 const void **data, size_t *size)
774 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
776 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
786 * Get 'data-offset' property from a given image node.
788 * @fit: pointer to the FIT image header
789 * @noffset: component image node offset
790 * @data_offset: holds the data-offset property
794 * -ENOENT if the property could not be found
796 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
800 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
804 *data_offset = fdt32_to_cpu(*val);
810 * Get 'data-position' property from a given image node.
812 * @fit: pointer to the FIT image header
813 * @noffset: component image node offset
814 * @data_position: holds the data-position property
818 * -ENOENT if the property could not be found
820 int fit_image_get_data_position(const void *fit, int noffset,
825 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
829 *data_position = fdt32_to_cpu(*val);
835 * Get 'data-size' property from a given image node.
837 * @fit: pointer to the FIT image header
838 * @noffset: component image node offset
839 * @data_size: holds the data-size property
843 * -ENOENT if the property could not be found
845 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
849 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
853 *data_size = fdt32_to_cpu(*val);
859 * fit_image_hash_get_algo - get hash algorithm name
860 * @fit: pointer to the FIT format image header
861 * @noffset: hash node offset
862 * @algo: double pointer to char, will hold pointer to the algorithm name
864 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
865 * If the property is found its data start address is returned to the caller.
871 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
875 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
877 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
885 * fit_image_hash_get_value - get hash value and length
886 * @fit: pointer to the FIT format image header
887 * @noffset: hash node offset
888 * @value: double pointer to uint8_t, will hold address of a hash value data
889 * @value_len: pointer to an int, will hold hash data length
891 * fit_image_hash_get_value() finds hash value property in a given hash node.
892 * If the property is found its data start address and size are returned to
899 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
904 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
905 if (*value == NULL) {
906 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
916 * fit_image_hash_get_ignore - get hash ignore flag
917 * @fit: pointer to the FIT format image header
918 * @noffset: hash node offset
919 * @ignore: pointer to an int, will hold hash ignore flag
921 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
922 * If the property is found and non-zero, the hash algorithm is not verified by
923 * u-boot automatically.
926 * 0, on ignore not found
927 * value, on ignore found
929 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
934 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
935 if (value == NULL || len != sizeof(int))
943 ulong fit_get_end(const void *fit)
945 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
949 * fit_set_timestamp - set node timestamp property
950 * @fit: pointer to the FIT format image header
951 * @noffset: node offset
952 * @timestamp: timestamp value to be set
954 * fit_set_timestamp() attempts to set timestamp property in the requested
955 * node and returns operation status to the caller.
959 * -ENOSPC if no space in device tree, -1 for other error
961 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
966 t = cpu_to_uimage(timestamp);
967 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
970 debug("Can't set '%s' property for '%s' node (%s)\n",
971 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
973 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
980 * calculate_hash - calculate and return hash for provided input data
981 * @data: pointer to the input data
982 * @data_len: data length
983 * @algo: requested hash algorithm
984 * @value: pointer to the char, will hold hash value data (caller must
985 * allocate enough free space)
986 * value_len: length of the calculated hash
988 * calculate_hash() computes input data hash according to the requested
990 * Resulting hash value is placed in caller provided 'value' buffer, length
991 * of the calculated hash is returned via value_len pointer argument.
995 * -1, when algo is unsupported
997 int calculate_hash(const void *data, int data_len, const char *algo,
998 uint8_t *value, int *value_len)
1000 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1001 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1003 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1005 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1006 sha1_csum_wd((unsigned char *)data, data_len,
1007 (unsigned char *)value, CHUNKSZ_SHA1);
1009 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1010 sha256_csum_wd((unsigned char *)data, data_len,
1011 (unsigned char *)value, CHUNKSZ_SHA256);
1012 *value_len = SHA256_SUM_LEN;
1013 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1014 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1017 debug("Unsupported hash alogrithm\n");
1023 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1024 size_t size, char **err_msgp)
1026 uint8_t value[FIT_MAX_HASH_LEN];
1035 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1036 *err_msgp = "Can't get hash algo property";
1041 if (IMAGE_ENABLE_IGNORE) {
1042 fit_image_hash_get_ignore(fit, noffset, &ignore);
1044 printf("-skipped ");
1049 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1051 *err_msgp = "Can't get hash value property";
1055 if (calculate_hash(data, size, algo, value, &value_len)) {
1056 *err_msgp = "Unsupported hash algorithm";
1060 if (value_len != fit_value_len) {
1061 *err_msgp = "Bad hash value len";
1063 } else if (memcmp(value, fit_value, value_len) != 0) {
1064 *err_msgp = "Bad hash value";
1071 int fit_image_verify_with_data(const void *fit, int image_noffset,
1072 const void *data, size_t size)
1079 /* Verify all required signatures */
1080 if (IMAGE_ENABLE_VERIFY &&
1081 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1082 gd_fdt_blob(), &verify_all)) {
1083 err_msg = "Unable to verify required signature";
1087 /* Process all hash subnodes of the component image node */
1088 fdt_for_each_subnode(noffset, fit, image_noffset) {
1089 const char *name = fit_get_name(fit, noffset, NULL);
1092 * Check subnode name, must be equal to "hash".
1093 * Multiple hash nodes require unique unit node
1094 * names, e.g. hash-1, hash-2, etc.
1096 if (!strncmp(name, FIT_HASH_NODENAME,
1097 strlen(FIT_HASH_NODENAME))) {
1098 if (fit_image_check_hash(fit, noffset, data, size,
1102 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1103 !strncmp(name, FIT_SIG_NODENAME,
1104 strlen(FIT_SIG_NODENAME))) {
1105 ret = fit_image_check_sig(fit, noffset, data,
1106 size, -1, &err_msg);
1109 * Show an indication on failure, but do not return
1110 * an error. Only keys marked 'required' can cause
1111 * an image validation failure. See the call to
1112 * fit_image_verify_required_sigs() above.
1121 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1122 err_msg = "Corrupted or truncated tree";
1129 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1130 err_msg, fit_get_name(fit, noffset, NULL),
1131 fit_get_name(fit, image_noffset, NULL));
1136 * fit_image_verify - verify data integrity
1137 * @fit: pointer to the FIT format image header
1138 * @image_noffset: component image node offset
1140 * fit_image_verify() goes over component image hash nodes,
1141 * re-calculates each data hash and compares with the value stored in hash
1145 * 1, if all hashes are valid
1146 * 0, otherwise (or on error)
1148 int fit_image_verify(const void *fit, int image_noffset)
1155 /* Get image data and data length */
1156 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1157 err_msg = "Can't get image data/size";
1158 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1159 err_msg, fit_get_name(fit, noffset, NULL),
1160 fit_get_name(fit, image_noffset, NULL));
1164 return fit_image_verify_with_data(fit, image_noffset, data, size);
1168 * fit_all_image_verify - verify data integrity for all images
1169 * @fit: pointer to the FIT format image header
1171 * fit_all_image_verify() goes over all images in the FIT and
1172 * for every images checks if all it's hashes are valid.
1175 * 1, if all hashes of all images are valid
1176 * 0, otherwise (or on error)
1178 int fit_all_image_verify(const void *fit)
1185 /* Find images parent node offset */
1186 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1187 if (images_noffset < 0) {
1188 printf("Can't find images parent node '%s' (%s)\n",
1189 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1193 /* Process all image subnodes, check hashes for each */
1194 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1196 for (ndepth = 0, count = 0,
1197 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1198 (noffset >= 0) && (ndepth > 0);
1199 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1202 * Direct child node of the images parent node,
1203 * i.e. component image node.
1205 printf(" Hash(es) for Image %u (%s): ", count,
1206 fit_get_name(fit, noffset, NULL));
1209 if (!fit_image_verify(fit, noffset))
1218 * fit_image_check_os - check whether image node is of a given os type
1219 * @fit: pointer to the FIT format image header
1220 * @noffset: component image node offset
1221 * @os: requested image os
1223 * fit_image_check_os() reads image os property and compares its numeric
1224 * id with the requested os. Comparison result is returned to the caller.
1227 * 1 if image is of given os type
1228 * 0 otherwise (or on error)
1230 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1234 if (fit_image_get_os(fit, noffset, &image_os))
1236 return (os == image_os);
1240 * fit_image_check_arch - check whether image node is of a given arch
1241 * @fit: pointer to the FIT format image header
1242 * @noffset: component image node offset
1243 * @arch: requested imagearch
1245 * fit_image_check_arch() reads image arch property and compares its numeric
1246 * id with the requested arch. Comparison result is returned to the caller.
1249 * 1 if image is of given arch
1250 * 0 otherwise (or on error)
1252 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1255 int aarch32_support = 0;
1257 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1258 aarch32_support = 1;
1261 if (fit_image_get_arch(fit, noffset, &image_arch))
1263 return (arch == image_arch) ||
1264 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1265 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1270 * fit_image_check_type - check whether image node is of a given type
1271 * @fit: pointer to the FIT format image header
1272 * @noffset: component image node offset
1273 * @type: requested image type
1275 * fit_image_check_type() reads image type property and compares its numeric
1276 * id with the requested type. Comparison result is returned to the caller.
1279 * 1 if image is of given type
1280 * 0 otherwise (or on error)
1282 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1286 if (fit_image_get_type(fit, noffset, &image_type))
1288 return (type == image_type);
1292 * fit_image_check_comp - check whether image node uses given compression
1293 * @fit: pointer to the FIT format image header
1294 * @noffset: component image node offset
1295 * @comp: requested image compression type
1297 * fit_image_check_comp() reads image compression property and compares its
1298 * numeric id with the requested compression type. Comparison result is
1299 * returned to the caller.
1302 * 1 if image uses requested compression
1303 * 0 otherwise (or on error)
1305 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1309 if (fit_image_get_comp(fit, noffset, &image_comp))
1311 return (comp == image_comp);
1315 * fit_check_format - sanity check FIT image format
1316 * @fit: pointer to the FIT format image header
1318 * fit_check_format() runs a basic sanity FIT image verification.
1319 * Routine checks for mandatory properties, nodes, etc.
1325 int fit_check_format(const void *fit)
1327 /* mandatory / node 'description' property */
1328 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1329 debug("Wrong FIT format: no description\n");
1333 if (IMAGE_ENABLE_TIMESTAMP) {
1334 /* mandatory / node 'timestamp' property */
1335 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1336 debug("Wrong FIT format: no timestamp\n");
1341 /* mandatory subimages parent '/images' node */
1342 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1343 debug("Wrong FIT format: no images parent node\n");
1352 * fit_conf_find_compat
1353 * @fit: pointer to the FIT format image header
1354 * @fdt: pointer to the device tree to compare against
1356 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1357 * most compatible with the passed in device tree.
1366 * |-o configurations
1374 * |-compatible = "foo,bar", "bim,bam"
1377 * |-compatible = "foo,bar",
1380 * |-compatible = "bim,bam", "baz,biz"
1382 * Configuration 1 would be picked because the first string in U-Boot's
1383 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1384 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1387 * offset to the configuration to use if one was found
1390 int fit_conf_find_compat(const void *fit, const void *fdt)
1393 int noffset, confs_noffset, images_noffset;
1394 const void *fdt_compat;
1396 int best_match_offset = 0;
1397 int best_match_pos = 0;
1399 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1400 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1401 if (confs_noffset < 0 || images_noffset < 0) {
1402 debug("Can't find configurations or images nodes.\n");
1406 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1408 debug("Fdt for comparison has no \"compatible\" property.\n");
1413 * Loop over the configurations in the FIT image.
1415 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1416 (noffset >= 0) && (ndepth > 0);
1417 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1419 const char *kfdt_name;
1421 const char *cur_fdt_compat;
1429 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1431 debug("No fdt property found.\n");
1434 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1436 if (kfdt_noffset < 0) {
1437 debug("No image node named \"%s\" found.\n",
1442 * Get a pointer to this configuration's fdt.
1444 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1445 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1449 len = fdt_compat_len;
1450 cur_fdt_compat = fdt_compat;
1452 * Look for a match for each U-Boot compatibility string in
1453 * turn in this configuration's fdt.
1455 for (i = 0; len > 0 &&
1456 (!best_match_offset || best_match_pos > i); i++) {
1457 int cur_len = strlen(cur_fdt_compat) + 1;
1459 if (!fdt_node_check_compatible(kfdt, 0,
1461 best_match_offset = noffset;
1466 cur_fdt_compat += cur_len;
1469 if (!best_match_offset) {
1470 debug("No match found.\n");
1474 return best_match_offset;
1478 * fit_conf_get_node - get node offset for configuration of a given unit name
1479 * @fit: pointer to the FIT format image header
1480 * @conf_uname: configuration node unit name
1482 * fit_conf_get_node() finds a configuration (within the '/configurations'
1483 * parent node) of a provided unit name. If configuration is found its node
1484 * offset is returned to the caller.
1486 * When NULL is provided in second argument fit_conf_get_node() will search
1487 * for a default configuration node instead. Default configuration node unit
1488 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1492 * configuration node offset when found (>=0)
1493 * negative number on failure (FDT_ERR_* code)
1495 int fit_conf_get_node(const void *fit, const char *conf_uname)
1497 int noffset, confs_noffset;
1500 char *conf_uname_copy = NULL;
1502 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1503 if (confs_noffset < 0) {
1504 debug("Can't find configurations parent node '%s' (%s)\n",
1505 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1506 return confs_noffset;
1509 if (conf_uname == NULL) {
1510 /* get configuration unit name from the default property */
1511 debug("No configuration specified, trying default...\n");
1512 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1513 FIT_DEFAULT_PROP, &len);
1514 if (conf_uname == NULL) {
1515 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1519 debug("Found default configuration: '%s'\n", conf_uname);
1522 s = strchr(conf_uname, '#');
1524 len = s - conf_uname;
1525 conf_uname_copy = malloc(len + 1);
1526 if (!conf_uname_copy) {
1527 debug("Can't allocate uname copy: '%s'\n",
1531 memcpy(conf_uname_copy, conf_uname, len);
1532 conf_uname_copy[len] = '\0';
1533 conf_uname = conf_uname_copy;
1536 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1538 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1539 conf_uname, fdt_strerror(noffset));
1542 if (conf_uname_copy)
1543 free(conf_uname_copy);
1548 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1549 const char *prop_name)
1551 return fdt_stringlist_count(fit, noffset, prop_name);
1554 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1555 const char *prop_name, int index)
1560 /* get kernel image unit name from configuration kernel property */
1561 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1565 return fit_image_get_node(fit, uname);
1568 int fit_conf_get_prop_node(const void *fit, int noffset,
1569 const char *prop_name)
1571 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1574 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
1576 * fit_conf_print - prints out the FIT configuration details
1577 * @fit: pointer to the FIT format image header
1578 * @noffset: offset of the configuration node
1579 * @p: pointer to prefix string
1581 * fit_conf_print() lists all mandatory properties for the processed
1582 * configuration node.
1585 * no returned results
1587 void fit_conf_print(const void *fit, int noffset, const char *p)
1592 int fdt_index, loadables_index;
1595 /* Mandatory properties */
1596 ret = fit_get_desc(fit, noffset, &desc);
1597 printf("%s Description: ", p);
1599 printf("unavailable\n");
1601 printf("%s\n", desc);
1603 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1604 printf("%s Kernel: ", p);
1606 printf("unavailable\n");
1608 printf("%s\n", uname);
1610 /* Optional properties */
1611 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1613 printf("%s Init Ramdisk: %s\n", p, uname);
1615 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
1617 printf("%s Firmware: %s\n", p, uname);
1620 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
1621 fdt_index, NULL), uname;
1625 printf("%s FDT: ", p);
1628 printf("%s\n", uname);
1631 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
1633 printf("%s FPGA: %s\n", p, uname);
1635 /* Print out all of the specified loadables */
1636 for (loadables_index = 0;
1637 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
1638 loadables_index, NULL), uname;
1639 loadables_index++) {
1640 if (loadables_index == 0) {
1641 printf("%s Loadables: ", p);
1645 printf("%s\n", uname);
1648 /* Process all hash subnodes of the component configuration node */
1649 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
1650 (noffset >= 0) && (ndepth > 0);
1651 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1653 /* Direct child node of the component configuration node */
1654 fit_image_print_verification_data(fit, noffset, p);
1658 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
1660 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1662 fit_image_print(fit, rd_noffset, " ");
1665 puts(" Verifying Hash Integrity ... ");
1666 if (!fit_image_verify(fit, rd_noffset)) {
1667 puts("Bad Data Hash\n");
1676 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1683 debug("* %s: using config '%s' from image at 0x%08lx\n",
1684 prop_name, images->fit_uname_cfg, addr);
1686 /* Check whether configuration has this property defined */
1687 fit_hdr = map_sysmem(addr, 0);
1688 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1689 if (cfg_noffset < 0) {
1690 debug("* %s: no such config\n", prop_name);
1694 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1696 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1704 * fit_get_image_type_property() - get property name for IH_TYPE_...
1706 * @return the properly name where we expect to find the image in the
1709 static const char *fit_get_image_type_property(int type)
1712 * This is sort-of available in the uimage_type[] table in image.c
1713 * but we don't have access to the short name, and "fdt" is different
1714 * anyway. So let's just keep it here.
1717 case IH_TYPE_FLATDT:
1718 return FIT_FDT_PROP;
1719 case IH_TYPE_KERNEL:
1720 return FIT_KERNEL_PROP;
1721 case IH_TYPE_RAMDISK:
1722 return FIT_RAMDISK_PROP;
1723 case IH_TYPE_X86_SETUP:
1724 return FIT_SETUP_PROP;
1725 case IH_TYPE_LOADABLE:
1726 return FIT_LOADABLE_PROP;
1728 return FIT_FPGA_PROP;
1734 int fit_image_load(bootm_headers_t *images, ulong addr,
1735 const char **fit_unamep, const char **fit_uname_configp,
1736 int arch, int image_type, int bootstage_id,
1737 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1739 int cfg_noffset, noffset;
1740 const char *fit_uname;
1741 const char *fit_uname_config;
1742 const char *fit_base_uname_config;
1747 ulong load, data, len;
1752 const char *prop_name;
1755 fit = map_sysmem(addr, 0);
1756 fit_uname = fit_unamep ? *fit_unamep : NULL;
1757 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1758 fit_base_uname_config = NULL;
1759 prop_name = fit_get_image_type_property(image_type);
1760 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1762 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1763 if (!fit_check_format(fit)) {
1764 printf("Bad FIT %s image format!\n", prop_name);
1765 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1768 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1770 /* get FIT component image node offset */
1771 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1772 noffset = fit_image_get_node(fit, fit_uname);
1775 * no image node unit name, try to get config
1776 * node first. If config unit node name is NULL
1777 * fit_conf_get_node() will try to find default config node
1779 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1780 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1781 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1783 cfg_noffset = fit_conf_get_node(fit,
1786 if (cfg_noffset < 0) {
1787 puts("Could not find configuration node\n");
1788 bootstage_error(bootstage_id +
1789 BOOTSTAGE_SUB_NO_UNIT_NAME);
1792 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1793 printf(" Using '%s' configuration\n", fit_base_uname_config);
1794 if (image_type == IH_TYPE_KERNEL) {
1795 /* Remember (and possibly verify) this config */
1796 images->fit_uname_cfg = fit_base_uname_config;
1797 if (IMAGE_ENABLE_VERIFY && images->verify) {
1798 puts(" Verifying Hash Integrity ... ");
1799 if (fit_config_verify(fit, cfg_noffset)) {
1800 puts("Bad Data Hash\n");
1801 bootstage_error(bootstage_id +
1802 BOOTSTAGE_SUB_HASH);
1807 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1810 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1812 fit_uname = fit_get_name(fit, noffset, NULL);
1815 puts("Could not find subimage node\n");
1816 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1820 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1822 ret = fit_image_select(fit, noffset, images->verify);
1824 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1828 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1829 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1830 if (!fit_image_check_target_arch(fit, noffset)) {
1831 puts("Unsupported Architecture\n");
1832 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1838 fit_image_get_arch(fit, noffset, &os_arch);
1839 images->os.arch = os_arch;
1842 if (image_type == IH_TYPE_FLATDT &&
1843 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1844 puts("FDT image is compressed");
1845 return -EPROTONOSUPPORT;
1848 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1849 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1850 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1851 (image_type == IH_TYPE_KERNEL &&
1852 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1854 os_ok = image_type == IH_TYPE_FLATDT ||
1855 image_type == IH_TYPE_FPGA ||
1856 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1857 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1858 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1861 * If either of the checks fail, we should report an error, but
1862 * if the image type is coming from the "loadables" field, we
1863 * don't care what it is
1865 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
1866 fit_image_get_os(fit, noffset, &os);
1867 printf("No %s %s %s Image\n",
1868 genimg_get_os_name(os),
1869 genimg_get_arch_name(arch),
1870 genimg_get_type_name(image_type));
1871 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1875 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1877 /* get image data address and length */
1878 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1879 printf("Could not find %s subimage data!\n", prop_name);
1880 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1884 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1885 /* perform any post-processing on the image data */
1886 board_fit_image_post_process((void **)&buf, &size);
1891 /* verify that image data is a proper FDT blob */
1892 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1893 puts("Subimage data is not a FDT");
1897 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1900 * Work-around for eldk-4.2 which gives this warning if we try to
1901 * cast in the unmap_sysmem() call:
1902 * warning: initialization discards qualifiers from pointer target type
1905 void *vbuf = (void *)buf;
1907 data = map_to_sysmem(vbuf);
1910 if (load_op == FIT_LOAD_IGNORED) {
1912 } else if (fit_image_get_load(fit, noffset, &load)) {
1913 if (load_op == FIT_LOAD_REQUIRED) {
1914 printf("Can't get %s subimage load address!\n",
1916 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1919 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1920 ulong image_start, image_end;
1925 * move image data to the load address,
1926 * make sure we don't overwrite initial image
1929 image_end = addr + fit_get_size(fit);
1931 load_end = load + len;
1932 if (image_type != IH_TYPE_KERNEL &&
1933 load < image_end && load_end > image_start) {
1934 printf("Error: %s overwritten\n", prop_name);
1938 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1939 prop_name, data, load);
1941 dst = map_sysmem(load, len);
1942 memmove(dst, buf, len);
1945 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1950 *fit_unamep = (char *)fit_uname;
1951 if (fit_uname_configp)
1952 *fit_uname_configp = (char *)(fit_uname_config ? :
1953 fit_base_uname_config);
1958 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1959 ulong *setup_start, ulong *setup_len)
1966 addr = map_to_sysmem(images->fit_hdr_os);
1967 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1971 ret = fit_image_load(images, addr, NULL, NULL, arch,
1972 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1973 FIT_LOAD_REQUIRED, setup_start, &len);
1979 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
1980 const char **fit_unamep, const char **fit_uname_configp,
1981 int arch, ulong *datap, ulong *lenp)
1983 int fdt_noffset, cfg_noffset, count;
1985 const char *fit_uname = NULL;
1986 const char *fit_uname_config = NULL;
1987 char *fit_uname_config_copy = NULL;
1988 char *next_config = NULL;
1990 #ifdef CONFIG_OF_LIBFDT_OVERLAY
1991 ulong image_start, image_end;
1992 ulong ovload, ovlen;
1993 const char *uconfig;
1996 int i, err, noffset, ov_noffset;
1999 fit_uname = fit_unamep ? *fit_unamep : NULL;
2001 if (fit_uname_configp && *fit_uname_configp) {
2002 fit_uname_config_copy = strdup(*fit_uname_configp);
2003 if (!fit_uname_config_copy)
2006 next_config = strchr(fit_uname_config_copy, '#');
2008 *next_config++ = '\0';
2009 if (next_config - 1 > fit_uname_config_copy)
2010 fit_uname_config = fit_uname_config_copy;
2013 fdt_noffset = fit_image_load(images,
2014 addr, &fit_uname, &fit_uname_config,
2015 arch, IH_TYPE_FLATDT,
2016 BOOTSTAGE_ID_FIT_FDT_START,
2017 FIT_LOAD_OPTIONAL, &load, &len);
2019 if (fdt_noffset < 0)
2022 debug("fit_uname=%s, fit_uname_config=%s\n",
2023 fit_uname ? fit_uname : "<NULL>",
2024 fit_uname_config ? fit_uname_config : "<NULL>");
2026 fit = map_sysmem(addr, 0);
2028 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2030 /* single blob, or error just return as well */
2031 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2032 if (count <= 1 && !next_config)
2035 /* we need to apply overlays */
2037 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2039 image_end = addr + fit_get_size(fit);
2040 /* verify that relocation took place by load address not being in fit */
2041 if (load >= image_start && load < image_end) {
2042 /* check is simplified; fit load checks for overlaps */
2043 printf("Overlayed FDT requires relocation\n");
2044 fdt_noffset = -EBADF;
2048 base = map_sysmem(load, len);
2050 /* apply extra configs in FIT first, followed by args */
2051 for (i = 1; ; i++) {
2053 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2055 uname = fit_get_name(fit, noffset, NULL);
2060 uconfig = next_config;
2061 next_config = strchr(next_config, '#');
2063 *next_config++ = '\0';
2067 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2069 ov_noffset = fit_image_load(images,
2070 addr, &uname, &uconfig,
2071 arch, IH_TYPE_FLATDT,
2072 BOOTSTAGE_ID_FIT_FDT_START,
2073 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2074 if (ov_noffset < 0) {
2075 printf("load of %s failed\n", uname);
2078 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2079 uname, ovload, ovlen);
2080 ov = map_sysmem(ovload, ovlen);
2082 base = map_sysmem(load, len + ovlen);
2083 err = fdt_open_into(base, base, len + ovlen);
2085 printf("failed on fdt_open_into\n");
2089 /* the verbose method prints out messages on error */
2090 err = fdt_overlay_apply_verbose(base, ov);
2096 len = fdt_totalsize(base);
2099 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2100 fdt_noffset = -EBADF;
2109 *fit_unamep = fit_uname;
2110 if (fit_uname_configp)
2111 *fit_uname_configp = fit_uname_config;
2113 if (fit_uname_config_copy)
2114 free(fit_uname_config_copy);