2 * Copyright (c) 2013, Google Inc.
4 * (C) Copyright 2008 Semihalf
6 * (C) Copyright 2000-2006
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * SPDX-License-Identifier: GPL-2.0+
16 #include <linux/compiler.h>
17 #include <linux/kconfig.h>
23 DECLARE_GLOBAL_DATA_PTR;
24 #endif /* !USE_HOSTCC*/
27 #include <bootstage.h>
28 #include <u-boot/crc.h>
29 #include <u-boot/md5.h>
30 #include <u-boot/sha1.h>
31 #include <u-boot/sha256.h>
33 /*****************************************************************************/
34 /* New uImage format routines */
35 /*****************************************************************************/
37 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
38 ulong *addr, const char **name)
45 sep = strchr(spec, sepc);
48 *addr = simple_strtoul(spec, NULL, 16);
58 * fit_parse_conf - parse FIT configuration spec
59 * @spec: input string, containing configuration spec
60 * @add_curr: current image address (to be used as a possible default)
61 * @addr: pointer to a ulong variable, will hold FIT image address of a given
63 * @conf_name double pointer to a char, will hold pointer to a configuration
66 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
67 * where <addr> is a FIT image address that contains configuration
68 * with a <conf> unit name.
70 * Address part is optional, and if omitted default add_curr will
74 * 1 if spec is a valid configuration string,
75 * addr and conf_name are set accordingly
78 int fit_parse_conf(const char *spec, ulong addr_curr,
79 ulong *addr, const char **conf_name)
81 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
85 * fit_parse_subimage - parse FIT subimage spec
86 * @spec: input string, containing subimage spec
87 * @add_curr: current image address (to be used as a possible default)
88 * @addr: pointer to a ulong variable, will hold FIT image address of a given
90 * @image_name: double pointer to a char, will hold pointer to a subimage name
92 * fit_parse_subimage() expects subimage spec in the form of
93 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
94 * subimage with a <subimg> unit name.
96 * Address part is optional, and if omitted default add_curr will
100 * 1 if spec is a valid subimage string,
101 * addr and image_name are set accordingly
104 int fit_parse_subimage(const char *spec, ulong addr_curr,
105 ulong *addr, const char **image_name)
107 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
109 #endif /* !USE_HOSTCC */
111 static void fit_get_debug(const void *fit, int noffset,
112 char *prop_name, int err)
114 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
115 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
120 * fit_get_subimage_count - get component (sub-image) count
121 * @fit: pointer to the FIT format image header
122 * @images_noffset: offset of images node
125 * number of image components
127 int fit_get_subimage_count(const void *fit, int images_noffset)
133 /* Process its subnodes, print out component images details */
134 for (ndepth = 0, count = 0,
135 noffset = fdt_next_node(fit, images_noffset, &ndepth);
136 (noffset >= 0) && (ndepth > 0);
137 noffset = fdt_next_node(fit, noffset, &ndepth)) {
146 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
148 * fit_print_contents - prints out the contents of the FIT format image
149 * @fit: pointer to the FIT format image header
150 * @p: pointer to prefix string
152 * fit_print_contents() formats a multi line FIT image contents description.
153 * The routine prints out FIT image properties (root node level) followed by
154 * the details of each component image.
157 * no returned results
159 void fit_print_contents(const void *fit)
172 /* Indent string is defined in header image.h */
173 p = IMAGE_INDENT_STRING;
175 /* Root node properties */
176 ret = fit_get_desc(fit, 0, &desc);
177 printf("%sFIT description: ", p);
179 printf("unavailable\n");
181 printf("%s\n", desc);
183 if (IMAGE_ENABLE_TIMESTAMP) {
184 ret = fit_get_timestamp(fit, 0, ×tamp);
185 printf("%sCreated: ", p);
187 printf("unavailable\n");
189 genimg_print_time(timestamp);
192 /* Find images parent node offset */
193 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
194 if (images_noffset < 0) {
195 printf("Can't find images parent node '%s' (%s)\n",
196 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
200 /* Process its subnodes, print out component images details */
201 for (ndepth = 0, count = 0,
202 noffset = fdt_next_node(fit, images_noffset, &ndepth);
203 (noffset >= 0) && (ndepth > 0);
204 noffset = fdt_next_node(fit, noffset, &ndepth)) {
207 * Direct child node of the images parent node,
208 * i.e. component image node.
210 printf("%s Image %u (%s)\n", p, count++,
211 fit_get_name(fit, noffset, NULL));
213 fit_image_print(fit, noffset, p);
217 /* Find configurations parent node offset */
218 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
219 if (confs_noffset < 0) {
220 debug("Can't get configurations parent node '%s' (%s)\n",
221 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
225 /* get default configuration unit name from default property */
226 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
228 printf("%s Default Configuration: '%s'\n", p, uname);
230 /* Process its subnodes, print out configurations details */
231 for (ndepth = 0, count = 0,
232 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
233 (noffset >= 0) && (ndepth > 0);
234 noffset = fdt_next_node(fit, noffset, &ndepth)) {
237 * Direct child node of the configurations parent node,
238 * i.e. configuration node.
240 printf("%s Configuration %u (%s)\n", p, count++,
241 fit_get_name(fit, noffset, NULL));
243 fit_conf_print(fit, noffset, p);
249 * fit_image_print_data() - prints out the hash node details
250 * @fit: pointer to the FIT format image header
251 * @noffset: offset of the hash node
252 * @p: pointer to prefix string
253 * @type: Type of information to print ("hash" or "sign")
255 * fit_image_print_data() lists properties for the processed hash node
257 * This function avoid using puts() since it prints a newline on the host
258 * but does not in U-Boot.
261 * no returned results
263 static void fit_image_print_data(const void *fit, int noffset, const char *p,
273 debug("%s %s node: '%s'\n", p, type,
274 fit_get_name(fit, noffset, NULL));
275 printf("%s %s algo: ", p, type);
276 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
277 printf("invalid/unsupported\n");
281 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
282 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
284 printf(":%s", keyname);
286 printf(" (required)");
289 ret = fit_image_hash_get_value(fit, noffset, &value,
291 printf("%s %s value: ", p, type);
293 printf("unavailable\n");
295 for (i = 0; i < value_len; i++)
296 printf("%02x", value[i]);
300 debug("%s %s len: %d\n", p, type, value_len);
302 /* Signatures have a time stamp */
303 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
306 printf("%s Timestamp: ", p);
307 if (fit_get_timestamp(fit, noffset, ×tamp))
308 printf("unavailable\n");
310 genimg_print_time(timestamp);
315 * fit_image_print_verification_data() - prints out the hash/signature details
316 * @fit: pointer to the FIT format image header
317 * @noffset: offset of the hash or signature node
318 * @p: pointer to prefix string
320 * This lists properties for the processed hash node
323 * no returned results
325 static void fit_image_print_verification_data(const void *fit, int noffset,
331 * Check subnode name, must be equal to "hash" or "signature".
332 * Multiple hash/signature nodes require unique unit node
333 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
335 name = fit_get_name(fit, noffset, NULL);
336 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
337 fit_image_print_data(fit, noffset, p, "Hash");
338 } else if (!strncmp(name, FIT_SIG_NODENAME,
339 strlen(FIT_SIG_NODENAME))) {
340 fit_image_print_data(fit, noffset, p, "Sign");
345 * fit_image_print - prints out the FIT component image details
346 * @fit: pointer to the FIT format image header
347 * @image_noffset: offset of the component image node
348 * @p: pointer to prefix string
350 * fit_image_print() lists all mandatory properties for the processed component
351 * image. If present, hash nodes are printed out as well. Load
352 * address for images of type firmware is also printed out. Since the load
353 * address is not mandatory for firmware images, it will be output as
354 * "unavailable" when not present.
357 * no returned results
359 void fit_image_print(const void *fit, int image_noffset, const char *p)
362 uint8_t type, arch, os, comp;
370 /* Mandatory properties */
371 ret = fit_get_desc(fit, image_noffset, &desc);
372 printf("%s Description: ", p);
374 printf("unavailable\n");
376 printf("%s\n", desc);
378 if (IMAGE_ENABLE_TIMESTAMP) {
381 ret = fit_get_timestamp(fit, 0, ×tamp);
382 printf("%s Created: ", p);
384 printf("unavailable\n");
386 genimg_print_time(timestamp);
389 fit_image_get_type(fit, image_noffset, &type);
390 printf("%s Type: %s\n", p, genimg_get_type_name(type));
392 fit_image_get_comp(fit, image_noffset, &comp);
393 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
395 ret = fit_image_get_data(fit, image_noffset, &data, &size);
398 printf("%s Data Start: ", p);
400 printf("unavailable\n");
402 void *vdata = (void *)data;
404 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
408 printf("%s Data Size: ", p);
410 printf("unavailable\n");
412 genimg_print_size(size);
414 /* Remaining, type dependent properties */
415 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
416 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
417 (type == IH_TYPE_FLATDT)) {
418 fit_image_get_arch(fit, image_noffset, &arch);
419 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
422 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
423 (type == IH_TYPE_FIRMWARE)) {
424 fit_image_get_os(fit, image_noffset, &os);
425 printf("%s OS: %s\n", p, genimg_get_os_name(os));
428 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
429 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
430 (type == IH_TYPE_FPGA)) {
431 ret = fit_image_get_load(fit, image_noffset, &load);
432 printf("%s Load Address: ", p);
434 printf("unavailable\n");
436 printf("0x%08lx\n", load);
439 /* optional load address for FDT */
440 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
441 printf("%s Load Address: 0x%08lx\n", p, load);
443 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
444 (type == IH_TYPE_RAMDISK)) {
445 ret = fit_image_get_entry(fit, image_noffset, &entry);
446 printf("%s Entry Point: ", p);
448 printf("unavailable\n");
450 printf("0x%08lx\n", entry);
453 /* Process all hash subnodes of the component image node */
454 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
455 (noffset >= 0) && (ndepth > 0);
456 noffset = fdt_next_node(fit, noffset, &ndepth)) {
458 /* Direct child node of the component image node */
459 fit_image_print_verification_data(fit, noffset, p);
464 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
467 * fit_get_desc - get node description property
468 * @fit: pointer to the FIT format image header
469 * @noffset: node offset
470 * @desc: double pointer to the char, will hold pointer to the description
472 * fit_get_desc() reads description property from a given node, if
473 * description is found pointer to it is returned in third call argument.
479 int fit_get_desc(const void *fit, int noffset, char **desc)
483 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
485 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
493 * fit_get_timestamp - get node timestamp property
494 * @fit: pointer to the FIT format image header
495 * @noffset: node offset
496 * @timestamp: pointer to the time_t, will hold read timestamp
498 * fit_get_timestamp() reads timestamp property from given node, if timestamp
499 * is found and has a correct size its value is returned in third call
504 * -1, on property read failure
505 * -2, on wrong timestamp size
507 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
512 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
514 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
517 if (len != sizeof(uint32_t)) {
518 debug("FIT timestamp with incorrect size of (%u)\n", len);
522 *timestamp = uimage_to_cpu(*((uint32_t *)data));
527 * fit_image_get_node - get node offset for component image of a given unit name
528 * @fit: pointer to the FIT format image header
529 * @image_uname: component image node unit name
531 * fit_image_get_node() finds a component image (within the '/images'
532 * node) of a provided unit name. If image is found its node offset is
533 * returned to the caller.
536 * image node offset when found (>=0)
537 * negative number on failure (FDT_ERR_* code)
539 int fit_image_get_node(const void *fit, const char *image_uname)
541 int noffset, images_noffset;
543 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
544 if (images_noffset < 0) {
545 debug("Can't find images parent node '%s' (%s)\n",
546 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
547 return images_noffset;
550 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
552 debug("Can't get node offset for image unit name: '%s' (%s)\n",
553 image_uname, fdt_strerror(noffset));
560 * fit_image_get_os - get os id for a given component image node
561 * @fit: pointer to the FIT format image header
562 * @noffset: component image node offset
563 * @os: pointer to the uint8_t, will hold os numeric id
565 * fit_image_get_os() finds os property in a given component image node.
566 * If the property is found, its (string) value is translated to the numeric
567 * id which is returned to the caller.
573 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
578 /* Get OS name from property data */
579 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
581 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
586 /* Translate OS name to id */
587 *os = genimg_get_os_id(data);
592 * fit_image_get_arch - get arch id for a given component image node
593 * @fit: pointer to the FIT format image header
594 * @noffset: component image node offset
595 * @arch: pointer to the uint8_t, will hold arch numeric id
597 * fit_image_get_arch() finds arch property in a given component image node.
598 * If the property is found, its (string) value is translated to the numeric
599 * id which is returned to the caller.
605 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
610 /* Get architecture name from property data */
611 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
613 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
618 /* Translate architecture name to id */
619 *arch = genimg_get_arch_id(data);
624 * fit_image_get_type - get type id for a given component image node
625 * @fit: pointer to the FIT format image header
626 * @noffset: component image node offset
627 * @type: pointer to the uint8_t, will hold type numeric id
629 * fit_image_get_type() finds type property in a given component image node.
630 * If the property is found, its (string) value is translated to the numeric
631 * id which is returned to the caller.
637 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
642 /* Get image type name from property data */
643 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
645 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
650 /* Translate image type name to id */
651 *type = genimg_get_type_id(data);
656 * fit_image_get_comp - get comp id for a given component image node
657 * @fit: pointer to the FIT format image header
658 * @noffset: component image node offset
659 * @comp: pointer to the uint8_t, will hold comp numeric id
661 * fit_image_get_comp() finds comp property in a given component image node.
662 * If the property is found, its (string) value is translated to the numeric
663 * id which is returned to the caller.
669 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
674 /* Get compression name from property data */
675 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
677 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
682 /* Translate compression name to id */
683 *comp = genimg_get_comp_id(data);
687 static int fit_image_get_address(const void *fit, int noffset, char *name,
694 cell = fdt_getprop(fit, noffset, name, &len);
696 fit_get_debug(fit, noffset, name, len);
700 if (len > sizeof(ulong)) {
701 printf("Unsupported %s address size\n", name);
706 /* Use load64 to avoid compiling warning for 32-bit target */
708 load64 = (load64 << 32) | uimage_to_cpu(*cell);
711 *load = (ulong)load64;
716 * fit_image_get_load() - get load addr property for given component image node
717 * @fit: pointer to the FIT format image header
718 * @noffset: component image node offset
719 * @load: pointer to the uint32_t, will hold load address
721 * fit_image_get_load() finds load address property in a given component
722 * image node. If the property is found, its value is returned to the caller.
728 int fit_image_get_load(const void *fit, int noffset, ulong *load)
730 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
734 * fit_image_get_entry() - get entry point address property
735 * @fit: pointer to the FIT format image header
736 * @noffset: component image node offset
737 * @entry: pointer to the uint32_t, will hold entry point address
739 * This gets the entry point address property for a given component image
742 * fit_image_get_entry() finds entry point address property in a given
743 * component image node. If the property is found, its value is returned
750 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
752 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
756 * fit_image_get_data - get data property and its size for a given component image node
757 * @fit: pointer to the FIT format image header
758 * @noffset: component image node offset
759 * @data: double pointer to void, will hold data property's data address
760 * @size: pointer to size_t, will hold data property's data size
762 * fit_image_get_data() finds data property in a given component image node.
763 * If the property is found its data start address and size are returned to
770 int fit_image_get_data(const void *fit, int noffset,
771 const void **data, size_t *size)
775 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
777 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
787 * Get 'data-offset' property from a given image node.
789 * @fit: pointer to the FIT image header
790 * @noffset: component image node offset
791 * @data_offset: holds the data-offset property
795 * -ENOENT if the property could not be found
797 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
801 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
805 *data_offset = fdt32_to_cpu(*val);
811 * Get 'data-position' property from a given image node.
813 * @fit: pointer to the FIT image header
814 * @noffset: component image node offset
815 * @data_position: holds the data-position property
819 * -ENOENT if the property could not be found
821 int fit_image_get_data_position(const void *fit, int noffset,
826 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
830 *data_position = fdt32_to_cpu(*val);
836 * Get 'data-size' property from a given image node.
838 * @fit: pointer to the FIT image header
839 * @noffset: component image node offset
840 * @data_size: holds the data-size property
844 * -ENOENT if the property could not be found
846 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
850 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
854 *data_size = fdt32_to_cpu(*val);
860 * fit_image_hash_get_algo - get hash algorithm name
861 * @fit: pointer to the FIT format image header
862 * @noffset: hash node offset
863 * @algo: double pointer to char, will hold pointer to the algorithm name
865 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
866 * If the property is found its data start address is returned to the caller.
872 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
876 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
878 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
886 * fit_image_hash_get_value - get hash value and length
887 * @fit: pointer to the FIT format image header
888 * @noffset: hash node offset
889 * @value: double pointer to uint8_t, will hold address of a hash value data
890 * @value_len: pointer to an int, will hold hash data length
892 * fit_image_hash_get_value() finds hash value property in a given hash node.
893 * If the property is found its data start address and size are returned to
900 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
905 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
906 if (*value == NULL) {
907 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
917 * fit_image_hash_get_ignore - get hash ignore flag
918 * @fit: pointer to the FIT format image header
919 * @noffset: hash node offset
920 * @ignore: pointer to an int, will hold hash ignore flag
922 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
923 * If the property is found and non-zero, the hash algorithm is not verified by
924 * u-boot automatically.
927 * 0, on ignore not found
928 * value, on ignore found
930 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
935 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
936 if (value == NULL || len != sizeof(int))
944 ulong fit_get_end(const void *fit)
946 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
950 * fit_set_timestamp - set node timestamp property
951 * @fit: pointer to the FIT format image header
952 * @noffset: node offset
953 * @timestamp: timestamp value to be set
955 * fit_set_timestamp() attempts to set timestamp property in the requested
956 * node and returns operation status to the caller.
960 * -ENOSPC if no space in device tree, -1 for other error
962 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
967 t = cpu_to_uimage(timestamp);
968 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
971 debug("Can't set '%s' property for '%s' node (%s)\n",
972 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
974 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
981 * calculate_hash - calculate and return hash for provided input data
982 * @data: pointer to the input data
983 * @data_len: data length
984 * @algo: requested hash algorithm
985 * @value: pointer to the char, will hold hash value data (caller must
986 * allocate enough free space)
987 * value_len: length of the calculated hash
989 * calculate_hash() computes input data hash according to the requested
991 * Resulting hash value is placed in caller provided 'value' buffer, length
992 * of the calculated hash is returned via value_len pointer argument.
996 * -1, when algo is unsupported
998 int calculate_hash(const void *data, int data_len, const char *algo,
999 uint8_t *value, int *value_len)
1001 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1002 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1004 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1006 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1007 sha1_csum_wd((unsigned char *)data, data_len,
1008 (unsigned char *)value, CHUNKSZ_SHA1);
1010 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1011 sha256_csum_wd((unsigned char *)data, data_len,
1012 (unsigned char *)value, CHUNKSZ_SHA256);
1013 *value_len = SHA256_SUM_LEN;
1014 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1015 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1018 debug("Unsupported hash alogrithm\n");
1024 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1025 size_t size, char **err_msgp)
1027 uint8_t value[FIT_MAX_HASH_LEN];
1036 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1037 *err_msgp = "Can't get hash algo property";
1042 if (IMAGE_ENABLE_IGNORE) {
1043 fit_image_hash_get_ignore(fit, noffset, &ignore);
1045 printf("-skipped ");
1050 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1052 *err_msgp = "Can't get hash value property";
1056 if (calculate_hash(data, size, algo, value, &value_len)) {
1057 *err_msgp = "Unsupported hash algorithm";
1061 if (value_len != fit_value_len) {
1062 *err_msgp = "Bad hash value len";
1064 } else if (memcmp(value, fit_value, value_len) != 0) {
1065 *err_msgp = "Bad hash value";
1072 int fit_image_verify_with_data(const void *fit, int image_noffset,
1073 const void *data, size_t size)
1080 /* Verify all required signatures */
1081 if (IMAGE_ENABLE_VERIFY &&
1082 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1083 gd_fdt_blob(), &verify_all)) {
1084 err_msg = "Unable to verify required signature";
1088 /* Process all hash subnodes of the component image node */
1089 fdt_for_each_subnode(noffset, fit, image_noffset) {
1090 const char *name = fit_get_name(fit, noffset, NULL);
1093 * Check subnode name, must be equal to "hash".
1094 * Multiple hash nodes require unique unit node
1095 * names, e.g. hash-1, hash-2, etc.
1097 if (!strncmp(name, FIT_HASH_NODENAME,
1098 strlen(FIT_HASH_NODENAME))) {
1099 if (fit_image_check_hash(fit, noffset, data, size,
1103 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1104 !strncmp(name, FIT_SIG_NODENAME,
1105 strlen(FIT_SIG_NODENAME))) {
1106 ret = fit_image_check_sig(fit, noffset, data,
1107 size, -1, &err_msg);
1110 * Show an indication on failure, but do not return
1111 * an error. Only keys marked 'required' can cause
1112 * an image validation failure. See the call to
1113 * fit_image_verify_required_sigs() above.
1122 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1123 err_msg = "Corrupted or truncated tree";
1130 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1131 err_msg, fit_get_name(fit, noffset, NULL),
1132 fit_get_name(fit, image_noffset, NULL));
1137 * fit_image_verify - verify data integrity
1138 * @fit: pointer to the FIT format image header
1139 * @image_noffset: component image node offset
1141 * fit_image_verify() goes over component image hash nodes,
1142 * re-calculates each data hash and compares with the value stored in hash
1146 * 1, if all hashes are valid
1147 * 0, otherwise (or on error)
1149 int fit_image_verify(const void *fit, int image_noffset)
1156 /* Get image data and data length */
1157 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1158 err_msg = "Can't get image data/size";
1159 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1160 err_msg, fit_get_name(fit, noffset, NULL),
1161 fit_get_name(fit, image_noffset, NULL));
1165 return fit_image_verify_with_data(fit, image_noffset, data, size);
1169 * fit_all_image_verify - verify data integrity for all images
1170 * @fit: pointer to the FIT format image header
1172 * fit_all_image_verify() goes over all images in the FIT and
1173 * for every images checks if all it's hashes are valid.
1176 * 1, if all hashes of all images are valid
1177 * 0, otherwise (or on error)
1179 int fit_all_image_verify(const void *fit)
1186 /* Find images parent node offset */
1187 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1188 if (images_noffset < 0) {
1189 printf("Can't find images parent node '%s' (%s)\n",
1190 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1194 /* Process all image subnodes, check hashes for each */
1195 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1197 for (ndepth = 0, count = 0,
1198 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1199 (noffset >= 0) && (ndepth > 0);
1200 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1203 * Direct child node of the images parent node,
1204 * i.e. component image node.
1206 printf(" Hash(es) for Image %u (%s): ", count,
1207 fit_get_name(fit, noffset, NULL));
1210 if (!fit_image_verify(fit, noffset))
1219 * fit_image_check_os - check whether image node is of a given os type
1220 * @fit: pointer to the FIT format image header
1221 * @noffset: component image node offset
1222 * @os: requested image os
1224 * fit_image_check_os() reads image os property and compares its numeric
1225 * id with the requested os. Comparison result is returned to the caller.
1228 * 1 if image is of given os type
1229 * 0 otherwise (or on error)
1231 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1235 if (fit_image_get_os(fit, noffset, &image_os))
1237 return (os == image_os);
1241 * fit_image_check_arch - check whether image node is of a given arch
1242 * @fit: pointer to the FIT format image header
1243 * @noffset: component image node offset
1244 * @arch: requested imagearch
1246 * fit_image_check_arch() reads image arch property and compares its numeric
1247 * id with the requested arch. Comparison result is returned to the caller.
1250 * 1 if image is of given arch
1251 * 0 otherwise (or on error)
1253 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1256 int aarch32_support = 0;
1258 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1259 aarch32_support = 1;
1262 if (fit_image_get_arch(fit, noffset, &image_arch))
1264 return (arch == image_arch) ||
1265 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1266 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1271 * fit_image_check_type - check whether image node is of a given type
1272 * @fit: pointer to the FIT format image header
1273 * @noffset: component image node offset
1274 * @type: requested image type
1276 * fit_image_check_type() reads image type property and compares its numeric
1277 * id with the requested type. Comparison result is returned to the caller.
1280 * 1 if image is of given type
1281 * 0 otherwise (or on error)
1283 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1287 if (fit_image_get_type(fit, noffset, &image_type))
1289 return (type == image_type);
1293 * fit_image_check_comp - check whether image node uses given compression
1294 * @fit: pointer to the FIT format image header
1295 * @noffset: component image node offset
1296 * @comp: requested image compression type
1298 * fit_image_check_comp() reads image compression property and compares its
1299 * numeric id with the requested compression type. Comparison result is
1300 * returned to the caller.
1303 * 1 if image uses requested compression
1304 * 0 otherwise (or on error)
1306 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1310 if (fit_image_get_comp(fit, noffset, &image_comp))
1312 return (comp == image_comp);
1316 * fit_check_format - sanity check FIT image format
1317 * @fit: pointer to the FIT format image header
1319 * fit_check_format() runs a basic sanity FIT image verification.
1320 * Routine checks for mandatory properties, nodes, etc.
1326 int fit_check_format(const void *fit)
1328 /* mandatory / node 'description' property */
1329 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1330 debug("Wrong FIT format: no description\n");
1334 if (IMAGE_ENABLE_TIMESTAMP) {
1335 /* mandatory / node 'timestamp' property */
1336 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1337 debug("Wrong FIT format: no timestamp\n");
1342 /* mandatory subimages parent '/images' node */
1343 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1344 debug("Wrong FIT format: no images parent node\n");
1353 * fit_conf_find_compat
1354 * @fit: pointer to the FIT format image header
1355 * @fdt: pointer to the device tree to compare against
1357 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1358 * most compatible with the passed in device tree.
1367 * |-o configurations
1375 * |-compatible = "foo,bar", "bim,bam"
1378 * |-compatible = "foo,bar",
1381 * |-compatible = "bim,bam", "baz,biz"
1383 * Configuration 1 would be picked because the first string in U-Boot's
1384 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1385 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1388 * offset to the configuration to use if one was found
1391 int fit_conf_find_compat(const void *fit, const void *fdt)
1394 int noffset, confs_noffset, images_noffset;
1395 const void *fdt_compat;
1397 int best_match_offset = 0;
1398 int best_match_pos = 0;
1400 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1401 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1402 if (confs_noffset < 0 || images_noffset < 0) {
1403 debug("Can't find configurations or images nodes.\n");
1407 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1409 debug("Fdt for comparison has no \"compatible\" property.\n");
1414 * Loop over the configurations in the FIT image.
1416 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1417 (noffset >= 0) && (ndepth > 0);
1418 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1420 const char *kfdt_name;
1422 const char *cur_fdt_compat;
1430 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1432 debug("No fdt property found.\n");
1435 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1437 if (kfdt_noffset < 0) {
1438 debug("No image node named \"%s\" found.\n",
1443 * Get a pointer to this configuration's fdt.
1445 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1446 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1450 len = fdt_compat_len;
1451 cur_fdt_compat = fdt_compat;
1453 * Look for a match for each U-Boot compatibility string in
1454 * turn in this configuration's fdt.
1456 for (i = 0; len > 0 &&
1457 (!best_match_offset || best_match_pos > i); i++) {
1458 int cur_len = strlen(cur_fdt_compat) + 1;
1460 if (!fdt_node_check_compatible(kfdt, 0,
1462 best_match_offset = noffset;
1467 cur_fdt_compat += cur_len;
1470 if (!best_match_offset) {
1471 debug("No match found.\n");
1475 return best_match_offset;
1479 * fit_conf_get_node - get node offset for configuration of a given unit name
1480 * @fit: pointer to the FIT format image header
1481 * @conf_uname: configuration node unit name
1483 * fit_conf_get_node() finds a configuration (within the '/configurations'
1484 * parent node) of a provided unit name. If configuration is found its node
1485 * offset is returned to the caller.
1487 * When NULL is provided in second argument fit_conf_get_node() will search
1488 * for a default configuration node instead. Default configuration node unit
1489 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1493 * configuration node offset when found (>=0)
1494 * negative number on failure (FDT_ERR_* code)
1496 int fit_conf_get_node(const void *fit, const char *conf_uname)
1498 int noffset, confs_noffset;
1501 char *conf_uname_copy = NULL;
1503 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1504 if (confs_noffset < 0) {
1505 debug("Can't find configurations parent node '%s' (%s)\n",
1506 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1507 return confs_noffset;
1510 if (conf_uname == NULL) {
1511 /* get configuration unit name from the default property */
1512 debug("No configuration specified, trying default...\n");
1513 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1514 FIT_DEFAULT_PROP, &len);
1515 if (conf_uname == NULL) {
1516 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1520 debug("Found default configuration: '%s'\n", conf_uname);
1523 s = strchr(conf_uname, '#');
1525 len = s - conf_uname;
1526 conf_uname_copy = malloc(len + 1);
1527 if (!conf_uname_copy) {
1528 debug("Can't allocate uname copy: '%s'\n",
1532 memcpy(conf_uname_copy, conf_uname, len);
1533 conf_uname_copy[len] = '\0';
1534 conf_uname = conf_uname_copy;
1537 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1539 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1540 conf_uname, fdt_strerror(noffset));
1543 if (conf_uname_copy)
1544 free(conf_uname_copy);
1549 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1550 const char *prop_name)
1552 return fdt_stringlist_count(fit, noffset, prop_name);
1555 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1556 const char *prop_name, int index)
1561 /* get kernel image unit name from configuration kernel property */
1562 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1566 return fit_image_get_node(fit, uname);
1569 int fit_conf_get_prop_node(const void *fit, int noffset,
1570 const char *prop_name)
1572 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
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;
1594 /* Mandatory properties */
1595 ret = fit_get_desc(fit, noffset, &desc);
1596 printf("%s Description: ", p);
1598 printf("unavailable\n");
1600 printf("%s\n", desc);
1602 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1603 printf("%s Kernel: ", p);
1605 printf("unavailable\n");
1607 printf("%s\n", uname);
1609 /* Optional properties */
1610 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1612 printf("%s Init Ramdisk: %s\n", p, uname);
1614 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
1616 printf("%s Firmware: %s\n", p, uname);
1619 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
1620 fdt_index, NULL), uname;
1624 printf("%s FDT: ", p);
1627 printf("%s\n", uname);
1630 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
1632 printf("%s FPGA: %s\n", p, uname);
1634 /* Print out all of the specified loadables */
1635 for (loadables_index = 0;
1636 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
1637 loadables_index, NULL), uname;
1638 loadables_index++) {
1639 if (loadables_index == 0) {
1640 printf("%s Loadables: ", p);
1644 printf("%s\n", uname);
1648 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1650 fit_image_print(fit, rd_noffset, " ");
1653 puts(" Verifying Hash Integrity ... ");
1654 if (!fit_image_verify(fit, rd_noffset)) {
1655 puts("Bad Data Hash\n");
1664 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1671 debug("* %s: using config '%s' from image at 0x%08lx\n",
1672 prop_name, images->fit_uname_cfg, addr);
1674 /* Check whether configuration has this property defined */
1675 fit_hdr = map_sysmem(addr, 0);
1676 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1677 if (cfg_noffset < 0) {
1678 debug("* %s: no such config\n", prop_name);
1682 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1684 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1692 * fit_get_image_type_property() - get property name for IH_TYPE_...
1694 * @return the properly name where we expect to find the image in the
1697 static const char *fit_get_image_type_property(int type)
1700 * This is sort-of available in the uimage_type[] table in image.c
1701 * but we don't have access to the short name, and "fdt" is different
1702 * anyway. So let's just keep it here.
1705 case IH_TYPE_FLATDT:
1706 return FIT_FDT_PROP;
1707 case IH_TYPE_KERNEL:
1708 return FIT_KERNEL_PROP;
1709 case IH_TYPE_RAMDISK:
1710 return FIT_RAMDISK_PROP;
1711 case IH_TYPE_X86_SETUP:
1712 return FIT_SETUP_PROP;
1713 case IH_TYPE_LOADABLE:
1714 return FIT_LOADABLE_PROP;
1716 return FIT_FPGA_PROP;
1722 int fit_image_load(bootm_headers_t *images, ulong addr,
1723 const char **fit_unamep, const char **fit_uname_configp,
1724 int arch, int image_type, int bootstage_id,
1725 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1727 int cfg_noffset, noffset;
1728 const char *fit_uname;
1729 const char *fit_uname_config;
1730 const char *fit_base_uname_config;
1735 ulong load, data, len;
1740 const char *prop_name;
1743 fit = map_sysmem(addr, 0);
1744 fit_uname = fit_unamep ? *fit_unamep : NULL;
1745 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1746 fit_base_uname_config = NULL;
1747 prop_name = fit_get_image_type_property(image_type);
1748 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1750 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1751 if (!fit_check_format(fit)) {
1752 printf("Bad FIT %s image format!\n", prop_name);
1753 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1756 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1758 /* get FIT component image node offset */
1759 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1760 noffset = fit_image_get_node(fit, fit_uname);
1763 * no image node unit name, try to get config
1764 * node first. If config unit node name is NULL
1765 * fit_conf_get_node() will try to find default config node
1767 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1768 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1769 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1771 cfg_noffset = fit_conf_get_node(fit,
1774 if (cfg_noffset < 0) {
1775 puts("Could not find configuration node\n");
1776 bootstage_error(bootstage_id +
1777 BOOTSTAGE_SUB_NO_UNIT_NAME);
1780 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1781 printf(" Using '%s' configuration\n", fit_base_uname_config);
1782 if (image_type == IH_TYPE_KERNEL) {
1783 /* Remember (and possibly verify) this config */
1784 images->fit_uname_cfg = fit_base_uname_config;
1785 if (IMAGE_ENABLE_VERIFY && images->verify) {
1786 puts(" Verifying Hash Integrity ... ");
1787 if (fit_config_verify(fit, cfg_noffset)) {
1788 puts("Bad Data Hash\n");
1789 bootstage_error(bootstage_id +
1790 BOOTSTAGE_SUB_HASH);
1795 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1798 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1800 fit_uname = fit_get_name(fit, noffset, NULL);
1803 puts("Could not find subimage node\n");
1804 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1808 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1810 ret = fit_image_select(fit, noffset, images->verify);
1812 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1816 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1817 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1818 if (!fit_image_check_target_arch(fit, noffset)) {
1819 puts("Unsupported Architecture\n");
1820 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1826 fit_image_get_arch(fit, noffset, &os_arch);
1827 images->os.arch = os_arch;
1830 if (image_type == IH_TYPE_FLATDT &&
1831 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1832 puts("FDT image is compressed");
1833 return -EPROTONOSUPPORT;
1836 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1837 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1838 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1839 (image_type == IH_TYPE_KERNEL &&
1840 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1842 os_ok = image_type == IH_TYPE_FLATDT ||
1843 image_type == IH_TYPE_FPGA ||
1844 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1845 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1846 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1849 * If either of the checks fail, we should report an error, but
1850 * if the image type is coming from the "loadables" field, we
1851 * don't care what it is
1853 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
1854 fit_image_get_os(fit, noffset, &os);
1855 printf("No %s %s %s Image\n",
1856 genimg_get_os_name(os),
1857 genimg_get_arch_name(arch),
1858 genimg_get_type_name(image_type));
1859 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1863 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1865 /* get image data address and length */
1866 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1867 printf("Could not find %s subimage data!\n", prop_name);
1868 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1872 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1873 /* perform any post-processing on the image data */
1874 board_fit_image_post_process((void **)&buf, &size);
1879 /* verify that image data is a proper FDT blob */
1880 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1881 puts("Subimage data is not a FDT");
1885 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1888 * Work-around for eldk-4.2 which gives this warning if we try to
1889 * cast in the unmap_sysmem() call:
1890 * warning: initialization discards qualifiers from pointer target type
1893 void *vbuf = (void *)buf;
1895 data = map_to_sysmem(vbuf);
1898 if (load_op == FIT_LOAD_IGNORED) {
1900 } else if (fit_image_get_load(fit, noffset, &load)) {
1901 if (load_op == FIT_LOAD_REQUIRED) {
1902 printf("Can't get %s subimage load address!\n",
1904 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1907 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1908 ulong image_start, image_end;
1913 * move image data to the load address,
1914 * make sure we don't overwrite initial image
1917 image_end = addr + fit_get_size(fit);
1919 load_end = load + len;
1920 if (image_type != IH_TYPE_KERNEL &&
1921 load < image_end && load_end > image_start) {
1922 printf("Error: %s overwritten\n", prop_name);
1926 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1927 prop_name, data, load);
1929 dst = map_sysmem(load, len);
1930 memmove(dst, buf, len);
1933 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1938 *fit_unamep = (char *)fit_uname;
1939 if (fit_uname_configp)
1940 *fit_uname_configp = (char *)(fit_uname_config ? :
1941 fit_base_uname_config);
1946 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1947 ulong *setup_start, ulong *setup_len)
1954 addr = map_to_sysmem(images->fit_hdr_os);
1955 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1959 ret = fit_image_load(images, addr, NULL, NULL, arch,
1960 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1961 FIT_LOAD_REQUIRED, setup_start, &len);
1967 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
1968 const char **fit_unamep, const char **fit_uname_configp,
1969 int arch, ulong *datap, ulong *lenp)
1971 int fdt_noffset, cfg_noffset, count;
1973 const char *fit_uname = NULL;
1974 const char *fit_uname_config = NULL;
1975 char *fit_uname_config_copy = NULL;
1976 char *next_config = NULL;
1978 #ifdef CONFIG_OF_LIBFDT_OVERLAY
1979 ulong image_start, image_end;
1980 ulong ovload, ovlen;
1981 const char *uconfig;
1984 int i, err, noffset, ov_noffset;
1987 fit_uname = fit_unamep ? *fit_unamep : NULL;
1989 if (fit_uname_configp && *fit_uname_configp) {
1990 fit_uname_config_copy = strdup(*fit_uname_configp);
1991 if (!fit_uname_config_copy)
1994 next_config = strchr(fit_uname_config_copy, '#');
1996 *next_config++ = '\0';
1997 if (next_config - 1 > fit_uname_config_copy)
1998 fit_uname_config = fit_uname_config_copy;
2001 fdt_noffset = fit_image_load(images,
2002 addr, &fit_uname, &fit_uname_config,
2003 arch, IH_TYPE_FLATDT,
2004 BOOTSTAGE_ID_FIT_FDT_START,
2005 FIT_LOAD_OPTIONAL, &load, &len);
2007 if (fdt_noffset < 0)
2010 debug("fit_uname=%s, fit_uname_config=%s\n",
2011 fit_uname ? fit_uname : "<NULL>",
2012 fit_uname_config ? fit_uname_config : "<NULL>");
2014 fit = map_sysmem(addr, 0);
2016 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2018 /* single blob, or error just return as well */
2019 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2020 if (count <= 1 && !next_config)
2023 /* we need to apply overlays */
2025 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2027 image_end = addr + fit_get_size(fit);
2028 /* verify that relocation took place by load address not being in fit */
2029 if (load >= image_start && load < image_end) {
2030 /* check is simplified; fit load checks for overlaps */
2031 printf("Overlayed FDT requires relocation\n");
2032 fdt_noffset = -EBADF;
2036 base = map_sysmem(load, len);
2038 /* apply extra configs in FIT first, followed by args */
2039 for (i = 1; ; i++) {
2041 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2043 uname = fit_get_name(fit, noffset, NULL);
2048 uconfig = next_config;
2049 next_config = strchr(next_config, '#');
2051 *next_config++ = '\0';
2055 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2057 ov_noffset = fit_image_load(images,
2058 addr, &uname, &uconfig,
2059 arch, IH_TYPE_FLATDT,
2060 BOOTSTAGE_ID_FIT_FDT_START,
2061 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2062 if (ov_noffset < 0) {
2063 printf("load of %s failed\n", uname);
2066 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2067 uname, ovload, ovlen);
2068 ov = map_sysmem(ovload, ovlen);
2070 base = map_sysmem(load, len + ovlen);
2071 err = fdt_open_into(base, base, len + ovlen);
2073 printf("failed on fdt_open_into\n");
2077 /* the verbose method prints out messages on error */
2078 err = fdt_overlay_apply_verbose(base, ov);
2084 len = fdt_totalsize(base);
2087 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2088 fdt_noffset = -EBADF;
2097 *fit_unamep = fit_uname;
2098 if (fit_uname_configp)
2099 *fit_uname_configp = fit_uname_config;
2101 if (fit_uname_config_copy)
2102 free(fit_uname_config_copy);