1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
5 * (C) Copyright 2008 Semihalf
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 #include <linux/libfdt.h>
15 #include <u-boot/crc.h>
17 #include <linux/compiler.h>
18 #include <linux/kconfig.h>
25 DECLARE_GLOBAL_DATA_PTR;
26 #endif /* !USE_HOSTCC*/
30 #include <bootstage.h>
31 #include <u-boot/crc.h>
32 #include <u-boot/md5.h>
33 #include <u-boot/sha1.h>
34 #include <u-boot/sha256.h>
36 /*****************************************************************************/
37 /* New uImage format routines */
38 /*****************************************************************************/
40 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
41 ulong *addr, const char **name)
48 sep = strchr(spec, sepc);
51 *addr = simple_strtoul(spec, NULL, 16);
61 * fit_parse_conf - parse FIT configuration spec
62 * @spec: input string, containing configuration spec
63 * @add_curr: current image address (to be used as a possible default)
64 * @addr: pointer to a ulong variable, will hold FIT image address of a given
66 * @conf_name double pointer to a char, will hold pointer to a configuration
69 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
70 * where <addr> is a FIT image address that contains configuration
71 * with a <conf> unit name.
73 * Address part is optional, and if omitted default add_curr will
77 * 1 if spec is a valid configuration string,
78 * addr and conf_name are set accordingly
81 int fit_parse_conf(const char *spec, ulong addr_curr,
82 ulong *addr, const char **conf_name)
84 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
88 * fit_parse_subimage - parse FIT subimage spec
89 * @spec: input string, containing subimage spec
90 * @add_curr: current image address (to be used as a possible default)
91 * @addr: pointer to a ulong variable, will hold FIT image address of a given
93 * @image_name: double pointer to a char, will hold pointer to a subimage name
95 * fit_parse_subimage() expects subimage spec in the form of
96 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
97 * subimage with a <subimg> unit name.
99 * Address part is optional, and if omitted default add_curr will
103 * 1 if spec is a valid subimage string,
104 * addr and image_name are set accordingly
107 int fit_parse_subimage(const char *spec, ulong addr_curr,
108 ulong *addr, const char **image_name)
110 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
112 #endif /* !USE_HOSTCC */
114 static void fit_get_debug(const void *fit, int noffset,
115 char *prop_name, int err)
117 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
118 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
123 * fit_get_subimage_count - get component (sub-image) count
124 * @fit: pointer to the FIT format image header
125 * @images_noffset: offset of images node
128 * number of image components
130 int fit_get_subimage_count(const void *fit, int images_noffset)
136 /* Process its subnodes, print out component images details */
137 for (ndepth = 0, count = 0,
138 noffset = fdt_next_node(fit, images_noffset, &ndepth);
139 (noffset >= 0) && (ndepth > 0);
140 noffset = fdt_next_node(fit, noffset, &ndepth)) {
149 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
151 * fit_image_print_data() - prints out the hash node details
152 * @fit: pointer to the FIT format image header
153 * @noffset: offset of the hash node
154 * @p: pointer to prefix string
155 * @type: Type of information to print ("hash" or "sign")
157 * fit_image_print_data() lists properties for the processed hash node
159 * This function avoid using puts() since it prints a newline on the host
160 * but does not in U-Boot.
163 * no returned results
165 static void fit_image_print_data(const void *fit, int noffset, const char *p,
176 debug("%s %s node: '%s'\n", p, type,
177 fit_get_name(fit, noffset, NULL));
178 printf("%s %s algo: ", p, type);
179 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
180 printf("invalid/unsupported\n");
184 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
185 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
187 printf(":%s", keyname);
189 printf(" (required)");
192 padding = fdt_getprop(fit, noffset, "padding", NULL);
194 printf("%s %s padding: %s\n", p, type, padding);
196 ret = fit_image_hash_get_value(fit, noffset, &value,
198 printf("%s %s value: ", p, type);
200 printf("unavailable\n");
202 for (i = 0; i < value_len; i++)
203 printf("%02x", value[i]);
207 debug("%s %s len: %d\n", p, type, value_len);
209 /* Signatures have a time stamp */
210 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
213 printf("%s Timestamp: ", p);
214 if (fit_get_timestamp(fit, noffset, ×tamp))
215 printf("unavailable\n");
217 genimg_print_time(timestamp);
222 * fit_image_print_verification_data() - prints out the hash/signature details
223 * @fit: pointer to the FIT format image header
224 * @noffset: offset of the hash or signature node
225 * @p: pointer to prefix string
227 * This lists properties for the processed hash node
230 * no returned results
232 static void fit_image_print_verification_data(const void *fit, int noffset,
238 * Check subnode name, must be equal to "hash" or "signature".
239 * Multiple hash/signature nodes require unique unit node
240 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
242 name = fit_get_name(fit, noffset, NULL);
243 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
244 fit_image_print_data(fit, noffset, p, "Hash");
245 } else if (!strncmp(name, FIT_SIG_NODENAME,
246 strlen(FIT_SIG_NODENAME))) {
247 fit_image_print_data(fit, noffset, p, "Sign");
252 * fit_conf_print - prints out the FIT configuration details
253 * @fit: pointer to the FIT format image header
254 * @noffset: offset of the configuration node
255 * @p: pointer to prefix string
257 * fit_conf_print() lists all mandatory properties for the processed
258 * configuration node.
261 * no returned results
263 static void fit_conf_print(const void *fit, int noffset, const char *p)
268 int fdt_index, loadables_index;
271 /* Mandatory properties */
272 ret = fit_get_desc(fit, noffset, &desc);
273 printf("%s Description: ", p);
275 printf("unavailable\n");
277 printf("%s\n", desc);
279 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
280 printf("%s Kernel: ", p);
282 printf("unavailable\n");
284 printf("%s\n", uname);
286 /* Optional properties */
287 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
289 printf("%s Init Ramdisk: %s\n", p, uname);
291 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
293 printf("%s Firmware: %s\n", p, uname);
296 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
297 fdt_index, NULL), uname;
300 printf("%s FDT: ", p);
303 printf("%s\n", uname);
306 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
308 printf("%s FPGA: %s\n", p, uname);
310 /* Print out all of the specified loadables */
311 for (loadables_index = 0;
312 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
313 loadables_index, NULL), uname;
315 if (loadables_index == 0) {
316 printf("%s Loadables: ", p);
320 printf("%s\n", uname);
323 /* Process all hash subnodes of the component configuration node */
324 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
325 (noffset >= 0) && (ndepth > 0);
326 noffset = fdt_next_node(fit, noffset, &ndepth)) {
328 /* Direct child node of the component configuration node */
329 fit_image_print_verification_data(fit, noffset, p);
335 * fit_print_contents - prints out the contents of the FIT format image
336 * @fit: pointer to the FIT format image header
337 * @p: pointer to prefix string
339 * fit_print_contents() formats a multi line FIT image contents description.
340 * The routine prints out FIT image properties (root node level) followed by
341 * the details of each component image.
344 * no returned results
346 void fit_print_contents(const void *fit)
359 /* Indent string is defined in header image.h */
360 p = IMAGE_INDENT_STRING;
362 /* Root node properties */
363 ret = fit_get_desc(fit, 0, &desc);
364 printf("%sFIT description: ", p);
366 printf("unavailable\n");
368 printf("%s\n", desc);
370 if (IMAGE_ENABLE_TIMESTAMP) {
371 ret = fit_get_timestamp(fit, 0, ×tamp);
372 printf("%sCreated: ", p);
374 printf("unavailable\n");
376 genimg_print_time(timestamp);
379 /* Find images parent node offset */
380 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
381 if (images_noffset < 0) {
382 printf("Can't find images parent node '%s' (%s)\n",
383 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
387 /* Process its subnodes, print out component images details */
388 for (ndepth = 0, count = 0,
389 noffset = fdt_next_node(fit, images_noffset, &ndepth);
390 (noffset >= 0) && (ndepth > 0);
391 noffset = fdt_next_node(fit, noffset, &ndepth)) {
394 * Direct child node of the images parent node,
395 * i.e. component image node.
397 printf("%s Image %u (%s)\n", p, count++,
398 fit_get_name(fit, noffset, NULL));
400 fit_image_print(fit, noffset, p);
404 /* Find configurations parent node offset */
405 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
406 if (confs_noffset < 0) {
407 debug("Can't get configurations parent node '%s' (%s)\n",
408 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
412 /* get default configuration unit name from default property */
413 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
415 printf("%s Default Configuration: '%s'\n", p, uname);
417 /* Process its subnodes, print out configurations details */
418 for (ndepth = 0, count = 0,
419 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
420 (noffset >= 0) && (ndepth > 0);
421 noffset = fdt_next_node(fit, noffset, &ndepth)) {
424 * Direct child node of the configurations parent node,
425 * i.e. configuration node.
427 printf("%s Configuration %u (%s)\n", p, count++,
428 fit_get_name(fit, noffset, NULL));
430 fit_conf_print(fit, noffset, p);
436 * fit_image_print - prints out the FIT component image details
437 * @fit: pointer to the FIT format image header
438 * @image_noffset: offset of the component image node
439 * @p: pointer to prefix string
441 * fit_image_print() lists all mandatory properties for the processed component
442 * image. If present, hash nodes are printed out as well. Load
443 * address for images of type firmware is also printed out. Since the load
444 * address is not mandatory for firmware images, it will be output as
445 * "unavailable" when not present.
448 * no returned results
450 void fit_image_print(const void *fit, int image_noffset, const char *p)
453 uint8_t type, arch, os, comp;
461 /* Mandatory properties */
462 ret = fit_get_desc(fit, image_noffset, &desc);
463 printf("%s Description: ", p);
465 printf("unavailable\n");
467 printf("%s\n", desc);
469 if (IMAGE_ENABLE_TIMESTAMP) {
472 ret = fit_get_timestamp(fit, 0, ×tamp);
473 printf("%s Created: ", p);
475 printf("unavailable\n");
477 genimg_print_time(timestamp);
480 fit_image_get_type(fit, image_noffset, &type);
481 printf("%s Type: %s\n", p, genimg_get_type_name(type));
483 fit_image_get_comp(fit, image_noffset, &comp);
484 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
486 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
489 printf("%s Data Start: ", p);
491 printf("unavailable\n");
493 void *vdata = (void *)data;
495 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
499 printf("%s Data Size: ", p);
501 printf("unavailable\n");
503 genimg_print_size(size);
505 /* Remaining, type dependent properties */
506 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
507 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
508 (type == IH_TYPE_FLATDT)) {
509 fit_image_get_arch(fit, image_noffset, &arch);
510 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
513 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
514 (type == IH_TYPE_FIRMWARE)) {
515 fit_image_get_os(fit, image_noffset, &os);
516 printf("%s OS: %s\n", p, genimg_get_os_name(os));
519 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
520 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
521 (type == IH_TYPE_FPGA)) {
522 ret = fit_image_get_load(fit, image_noffset, &load);
523 printf("%s Load Address: ", p);
525 printf("unavailable\n");
527 printf("0x%08lx\n", load);
530 /* optional load address for FDT */
531 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
532 printf("%s Load Address: 0x%08lx\n", p, load);
534 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
535 (type == IH_TYPE_RAMDISK)) {
536 ret = fit_image_get_entry(fit, image_noffset, &entry);
537 printf("%s Entry Point: ", p);
539 printf("unavailable\n");
541 printf("0x%08lx\n", entry);
544 /* Process all hash subnodes of the component image node */
545 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
546 (noffset >= 0) && (ndepth > 0);
547 noffset = fdt_next_node(fit, noffset, &ndepth)) {
549 /* Direct child node of the component image node */
550 fit_image_print_verification_data(fit, noffset, p);
555 void fit_print_contents(const void *fit) { }
556 void fit_image_print(const void *fit, int image_noffset, const char *p) { }
557 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
560 * fit_get_desc - get node description property
561 * @fit: pointer to the FIT format image header
562 * @noffset: node offset
563 * @desc: double pointer to the char, will hold pointer to the description
565 * fit_get_desc() reads description property from a given node, if
566 * description is found pointer to it is returned in third call argument.
572 int fit_get_desc(const void *fit, int noffset, char **desc)
576 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
578 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
586 * fit_get_timestamp - get node timestamp property
587 * @fit: pointer to the FIT format image header
588 * @noffset: node offset
589 * @timestamp: pointer to the time_t, will hold read timestamp
591 * fit_get_timestamp() reads timestamp property from given node, if timestamp
592 * is found and has a correct size its value is returned in third call
597 * -1, on property read failure
598 * -2, on wrong timestamp size
600 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
605 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
607 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
610 if (len != sizeof(uint32_t)) {
611 debug("FIT timestamp with incorrect size of (%u)\n", len);
615 *timestamp = uimage_to_cpu(*((uint32_t *)data));
620 * fit_image_get_node - get node offset for component image of a given unit name
621 * @fit: pointer to the FIT format image header
622 * @image_uname: component image node unit name
624 * fit_image_get_node() finds a component image (within the '/images'
625 * node) of a provided unit name. If image is found its node offset is
626 * returned to the caller.
629 * image node offset when found (>=0)
630 * negative number on failure (FDT_ERR_* code)
632 int fit_image_get_node(const void *fit, const char *image_uname)
634 int noffset, images_noffset;
636 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
637 if (images_noffset < 0) {
638 debug("Can't find images parent node '%s' (%s)\n",
639 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
640 return images_noffset;
643 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
645 debug("Can't get node offset for image unit name: '%s' (%s)\n",
646 image_uname, fdt_strerror(noffset));
653 * fit_image_get_os - get os id for a given component image node
654 * @fit: pointer to the FIT format image header
655 * @noffset: component image node offset
656 * @os: pointer to the uint8_t, will hold os numeric id
658 * fit_image_get_os() finds os property in a given component image node.
659 * If the property is found, its (string) value is translated to the numeric
660 * id which is returned to the caller.
666 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
671 /* Get OS name from property data */
672 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
674 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
679 /* Translate OS name to id */
680 *os = genimg_get_os_id(data);
685 * fit_image_get_arch - get arch id for a given component image node
686 * @fit: pointer to the FIT format image header
687 * @noffset: component image node offset
688 * @arch: pointer to the uint8_t, will hold arch numeric id
690 * fit_image_get_arch() finds arch property in a given component image node.
691 * If the property is found, its (string) value is translated to the numeric
692 * id which is returned to the caller.
698 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
703 /* Get architecture name from property data */
704 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
706 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
711 /* Translate architecture name to id */
712 *arch = genimg_get_arch_id(data);
717 * fit_image_get_type - get type id for a given component image node
718 * @fit: pointer to the FIT format image header
719 * @noffset: component image node offset
720 * @type: pointer to the uint8_t, will hold type numeric id
722 * fit_image_get_type() finds type property in a given component image node.
723 * If the property is found, its (string) value is translated to the numeric
724 * id which is returned to the caller.
730 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
735 /* Get image type name from property data */
736 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
738 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
743 /* Translate image type name to id */
744 *type = genimg_get_type_id(data);
749 * fit_image_get_comp - get comp id for a given component image node
750 * @fit: pointer to the FIT format image header
751 * @noffset: component image node offset
752 * @comp: pointer to the uint8_t, will hold comp numeric id
754 * fit_image_get_comp() finds comp property in a given component image node.
755 * If the property is found, its (string) value is translated to the numeric
756 * id which is returned to the caller.
762 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
767 /* Get compression name from property data */
768 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
770 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
775 /* Translate compression name to id */
776 *comp = genimg_get_comp_id(data);
780 static int fit_image_get_address(const void *fit, int noffset, char *name,
787 cell = fdt_getprop(fit, noffset, name, &len);
789 fit_get_debug(fit, noffset, name, len);
793 if (len > sizeof(ulong)) {
794 printf("Unsupported %s address size\n", name);
799 /* Use load64 to avoid compiling warning for 32-bit target */
801 load64 = (load64 << 32) | uimage_to_cpu(*cell);
804 *load = (ulong)load64;
809 * fit_image_get_load() - get load addr property for given component image node
810 * @fit: pointer to the FIT format image header
811 * @noffset: component image node offset
812 * @load: pointer to the uint32_t, will hold load address
814 * fit_image_get_load() finds load address property in a given component
815 * image node. If the property is found, its value is returned to the caller.
821 int fit_image_get_load(const void *fit, int noffset, ulong *load)
823 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
827 * fit_image_get_entry() - get entry point address property
828 * @fit: pointer to the FIT format image header
829 * @noffset: component image node offset
830 * @entry: pointer to the uint32_t, will hold entry point address
832 * This gets the entry point address property for a given component image
835 * fit_image_get_entry() finds entry point address property in a given
836 * component image node. If the property is found, its value is returned
843 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
845 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
849 * fit_image_get_data - get data property and its size for a given component image node
850 * @fit: pointer to the FIT format image header
851 * @noffset: component image node offset
852 * @data: double pointer to void, will hold data property's data address
853 * @size: pointer to size_t, will hold data property's data size
855 * fit_image_get_data() finds data property in a given component image node.
856 * If the property is found its data start address and size are returned to
863 int fit_image_get_data(const void *fit, int noffset,
864 const void **data, size_t *size)
868 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
870 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
880 * Get 'data-offset' property from a given image node.
882 * @fit: pointer to the FIT image header
883 * @noffset: component image node offset
884 * @data_offset: holds the data-offset property
888 * -ENOENT if the property could not be found
890 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
894 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
898 *data_offset = fdt32_to_cpu(*val);
904 * Get 'data-position' property from a given image node.
906 * @fit: pointer to the FIT image header
907 * @noffset: component image node offset
908 * @data_position: holds the data-position property
912 * -ENOENT if the property could not be found
914 int fit_image_get_data_position(const void *fit, int noffset,
919 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
923 *data_position = fdt32_to_cpu(*val);
929 * Get 'data-size' property from a given image node.
931 * @fit: pointer to the FIT image header
932 * @noffset: component image node offset
933 * @data_size: holds the data-size property
937 * -ENOENT if the property could not be found
939 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
943 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
947 *data_size = fdt32_to_cpu(*val);
953 * Get 'data-size-unciphered' property from a given image node.
955 * @fit: pointer to the FIT image header
956 * @noffset: component image node offset
957 * @data_size: holds the data-size property
961 * -ENOENT if the property could not be found
963 int fit_image_get_data_size_unciphered(const void *fit, int noffset,
968 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
972 *data_size = (size_t)fdt32_to_cpu(*val);
978 * fit_image_get_data_and_size - get data and its size including
979 * both embedded and external data
980 * @fit: pointer to the FIT format image header
981 * @noffset: component image node offset
982 * @data: double pointer to void, will hold data property's data address
983 * @size: pointer to size_t, will hold data property's data size
985 * fit_image_get_data_and_size() finds data and its size including
986 * both embedded and external data. If the property is found
987 * its data start address and size are returned to the caller.
991 * otherwise, on failure
993 int fit_image_get_data_and_size(const void *fit, int noffset,
994 const void **data, size_t *size)
996 bool external_data = false;
1001 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1002 external_data = true;
1003 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1004 external_data = true;
1006 * For FIT with external data, figure out where
1007 * the external images start. This is the base
1008 * for the data-offset properties in each image.
1010 offset += ((fdt_totalsize(fit) + 3) & ~3);
1013 if (external_data) {
1014 debug("External Data\n");
1015 ret = fit_image_get_data_size(fit, noffset, &len);
1017 *data = fit + offset;
1021 ret = fit_image_get_data(fit, noffset, data, size);
1028 * fit_image_hash_get_algo - get hash algorithm name
1029 * @fit: pointer to the FIT format image header
1030 * @noffset: hash node offset
1031 * @algo: double pointer to char, will hold pointer to the algorithm name
1033 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1034 * If the property is found its data start address is returned to the caller.
1040 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1044 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1045 if (*algo == NULL) {
1046 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1054 * fit_image_hash_get_value - get hash value and length
1055 * @fit: pointer to the FIT format image header
1056 * @noffset: hash node offset
1057 * @value: double pointer to uint8_t, will hold address of a hash value data
1058 * @value_len: pointer to an int, will hold hash data length
1060 * fit_image_hash_get_value() finds hash value property in a given hash node.
1061 * If the property is found its data start address and size are returned to
1068 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1073 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1074 if (*value == NULL) {
1075 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1085 * fit_image_hash_get_ignore - get hash ignore flag
1086 * @fit: pointer to the FIT format image header
1087 * @noffset: hash node offset
1088 * @ignore: pointer to an int, will hold hash ignore flag
1090 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1091 * If the property is found and non-zero, the hash algorithm is not verified by
1092 * u-boot automatically.
1095 * 0, on ignore not found
1096 * value, on ignore found
1098 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1103 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1104 if (value == NULL || len != sizeof(int))
1113 * fit_image_cipher_get_algo - get cipher algorithm name
1114 * @fit: pointer to the FIT format image header
1115 * @noffset: cipher node offset
1116 * @algo: double pointer to char, will hold pointer to the algorithm name
1118 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1119 * cipher node. If the property is found its data start address is returned
1126 int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1130 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1132 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1139 ulong fit_get_end(const void *fit)
1141 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1145 * fit_set_timestamp - set node timestamp property
1146 * @fit: pointer to the FIT format image header
1147 * @noffset: node offset
1148 * @timestamp: timestamp value to be set
1150 * fit_set_timestamp() attempts to set timestamp property in the requested
1151 * node and returns operation status to the caller.
1155 * -ENOSPC if no space in device tree, -1 for other error
1157 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1162 t = cpu_to_uimage(timestamp);
1163 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1166 debug("Can't set '%s' property for '%s' node (%s)\n",
1167 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1169 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1176 * calculate_hash - calculate and return hash for provided input data
1177 * @data: pointer to the input data
1178 * @data_len: data length
1179 * @algo: requested hash algorithm
1180 * @value: pointer to the char, will hold hash value data (caller must
1181 * allocate enough free space)
1182 * value_len: length of the calculated hash
1184 * calculate_hash() computes input data hash according to the requested
1186 * Resulting hash value is placed in caller provided 'value' buffer, length
1187 * of the calculated hash is returned via value_len pointer argument.
1191 * -1, when algo is unsupported
1193 int calculate_hash(const void *data, int data_len, const char *algo,
1194 uint8_t *value, int *value_len)
1196 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1197 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1199 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1201 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1202 sha1_csum_wd((unsigned char *)data, data_len,
1203 (unsigned char *)value, CHUNKSZ_SHA1);
1205 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1206 sha256_csum_wd((unsigned char *)data, data_len,
1207 (unsigned char *)value, CHUNKSZ_SHA256);
1208 *value_len = SHA256_SUM_LEN;
1209 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1210 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1213 debug("Unsupported hash alogrithm\n");
1219 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1220 size_t size, char **err_msgp)
1222 uint8_t value[FIT_MAX_HASH_LEN];
1231 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1232 *err_msgp = "Can't get hash algo property";
1237 if (IMAGE_ENABLE_IGNORE) {
1238 fit_image_hash_get_ignore(fit, noffset, &ignore);
1240 printf("-skipped ");
1245 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1247 *err_msgp = "Can't get hash value property";
1251 if (calculate_hash(data, size, algo, value, &value_len)) {
1252 *err_msgp = "Unsupported hash algorithm";
1256 if (value_len != fit_value_len) {
1257 *err_msgp = "Bad hash value len";
1259 } else if (memcmp(value, fit_value, value_len) != 0) {
1260 *err_msgp = "Bad hash value";
1267 int fit_image_verify_with_data(const void *fit, int image_noffset,
1268 const void *data, size_t size)
1275 /* Verify all required signatures */
1276 if (FIT_IMAGE_ENABLE_VERIFY &&
1277 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1278 gd_fdt_blob(), &verify_all)) {
1279 err_msg = "Unable to verify required signature";
1283 /* Process all hash subnodes of the component image node */
1284 fdt_for_each_subnode(noffset, fit, image_noffset) {
1285 const char *name = fit_get_name(fit, noffset, NULL);
1288 * Check subnode name, must be equal to "hash".
1289 * Multiple hash nodes require unique unit node
1290 * names, e.g. hash-1, hash-2, etc.
1292 if (!strncmp(name, FIT_HASH_NODENAME,
1293 strlen(FIT_HASH_NODENAME))) {
1294 if (fit_image_check_hash(fit, noffset, data, size,
1298 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
1299 !strncmp(name, FIT_SIG_NODENAME,
1300 strlen(FIT_SIG_NODENAME))) {
1301 ret = fit_image_check_sig(fit, noffset, data,
1302 size, -1, &err_msg);
1305 * Show an indication on failure, but do not return
1306 * an error. Only keys marked 'required' can cause
1307 * an image validation failure. See the call to
1308 * fit_image_verify_required_sigs() above.
1317 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1318 err_msg = "Corrupted or truncated tree";
1325 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1326 err_msg, fit_get_name(fit, noffset, NULL),
1327 fit_get_name(fit, image_noffset, NULL));
1332 * fit_image_verify - verify data integrity
1333 * @fit: pointer to the FIT format image header
1334 * @image_noffset: component image node offset
1336 * fit_image_verify() goes over component image hash nodes,
1337 * re-calculates each data hash and compares with the value stored in hash
1341 * 1, if all hashes are valid
1342 * 0, otherwise (or on error)
1344 int fit_image_verify(const void *fit, int image_noffset)
1351 /* Get image data and data length */
1352 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1353 err_msg = "Can't get image data/size";
1354 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1355 err_msg, fit_get_name(fit, noffset, NULL),
1356 fit_get_name(fit, image_noffset, NULL));
1360 return fit_image_verify_with_data(fit, image_noffset, data, size);
1364 * fit_all_image_verify - verify data integrity for all images
1365 * @fit: pointer to the FIT format image header
1367 * fit_all_image_verify() goes over all images in the FIT and
1368 * for every images checks if all it's hashes are valid.
1371 * 1, if all hashes of all images are valid
1372 * 0, otherwise (or on error)
1374 int fit_all_image_verify(const void *fit)
1381 /* Find images parent node offset */
1382 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1383 if (images_noffset < 0) {
1384 printf("Can't find images parent node '%s' (%s)\n",
1385 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1389 /* Process all image subnodes, check hashes for each */
1390 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1392 for (ndepth = 0, count = 0,
1393 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1394 (noffset >= 0) && (ndepth > 0);
1395 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1398 * Direct child node of the images parent node,
1399 * i.e. component image node.
1401 printf(" Hash(es) for Image %u (%s): ", count,
1402 fit_get_name(fit, noffset, NULL));
1405 if (!fit_image_verify(fit, noffset))
1413 #ifdef CONFIG_FIT_CIPHER
1414 static int fit_image_uncipher(const void *fit, int image_noffset,
1415 void **data, size_t *size)
1417 int cipher_noffset, ret;
1421 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1422 FIT_CIPHER_NODENAME);
1423 if (cipher_noffset < 0)
1426 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1427 *data, *size, &dst, &size_dst);
1437 #endif /* CONFIG_FIT_CIPHER */
1440 * fit_image_check_os - check whether image node is of a given os type
1441 * @fit: pointer to the FIT format image header
1442 * @noffset: component image node offset
1443 * @os: requested image os
1445 * fit_image_check_os() reads image os property and compares its numeric
1446 * id with the requested os. Comparison result is returned to the caller.
1449 * 1 if image is of given os type
1450 * 0 otherwise (or on error)
1452 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1456 if (fit_image_get_os(fit, noffset, &image_os))
1458 return (os == image_os);
1462 * fit_image_check_arch - check whether image node is of a given arch
1463 * @fit: pointer to the FIT format image header
1464 * @noffset: component image node offset
1465 * @arch: requested imagearch
1467 * fit_image_check_arch() reads image arch property and compares its numeric
1468 * id with the requested arch. Comparison result is returned to the caller.
1471 * 1 if image is of given arch
1472 * 0 otherwise (or on error)
1474 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1477 int aarch32_support = 0;
1479 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1480 aarch32_support = 1;
1483 if (fit_image_get_arch(fit, noffset, &image_arch))
1485 return (arch == image_arch) ||
1486 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1487 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1492 * fit_image_check_type - check whether image node is of a given type
1493 * @fit: pointer to the FIT format image header
1494 * @noffset: component image node offset
1495 * @type: requested image type
1497 * fit_image_check_type() reads image type property and compares its numeric
1498 * id with the requested type. Comparison result is returned to the caller.
1501 * 1 if image is of given type
1502 * 0 otherwise (or on error)
1504 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1508 if (fit_image_get_type(fit, noffset, &image_type))
1510 return (type == image_type);
1514 * fit_image_check_comp - check whether image node uses given compression
1515 * @fit: pointer to the FIT format image header
1516 * @noffset: component image node offset
1517 * @comp: requested image compression type
1519 * fit_image_check_comp() reads image compression property and compares its
1520 * numeric id with the requested compression type. Comparison result is
1521 * returned to the caller.
1524 * 1 if image uses requested compression
1525 * 0 otherwise (or on error)
1527 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1531 if (fit_image_get_comp(fit, noffset, &image_comp))
1533 return (comp == image_comp);
1537 * fit_check_format - sanity check FIT image format
1538 * @fit: pointer to the FIT format image header
1540 * fit_check_format() runs a basic sanity FIT image verification.
1541 * Routine checks for mandatory properties, nodes, etc.
1547 int fit_check_format(const void *fit)
1549 /* mandatory / node 'description' property */
1550 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1551 debug("Wrong FIT format: no description\n");
1555 if (IMAGE_ENABLE_TIMESTAMP) {
1556 /* mandatory / node 'timestamp' property */
1557 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1558 debug("Wrong FIT format: no timestamp\n");
1563 /* mandatory subimages parent '/images' node */
1564 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1565 debug("Wrong FIT format: no images parent node\n");
1574 * fit_conf_find_compat
1575 * @fit: pointer to the FIT format image header
1576 * @fdt: pointer to the device tree to compare against
1578 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1579 * most compatible with the passed in device tree.
1588 * |-o configurations
1596 * |-compatible = "foo,bar", "bim,bam"
1599 * |-compatible = "foo,bar",
1602 * |-compatible = "bim,bam", "baz,biz"
1604 * Configuration 1 would be picked because the first string in U-Boot's
1605 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1606 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1608 * As an optimization, the compatible property from the FDT's root node can be
1609 * copied into the configuration node in the FIT image. This is required to
1610 * match configurations with compressed FDTs.
1613 * offset to the configuration to use if one was found
1616 int fit_conf_find_compat(const void *fit, const void *fdt)
1619 int noffset, confs_noffset, images_noffset;
1620 const void *fdt_compat;
1622 int best_match_offset = 0;
1623 int best_match_pos = 0;
1625 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1626 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1627 if (confs_noffset < 0 || images_noffset < 0) {
1628 debug("Can't find configurations or images nodes.\n");
1632 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1634 debug("Fdt for comparison has no \"compatible\" property.\n");
1639 * Loop over the configurations in the FIT image.
1641 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1642 (noffset >= 0) && (ndepth > 0);
1643 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1645 const char *kfdt_name;
1646 int kfdt_noffset, compat_noffset;
1647 const char *cur_fdt_compat;
1655 /* If there's a compat property in the config node, use that. */
1656 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1657 fdt = fit; /* search in FIT image */
1658 compat_noffset = noffset; /* search under config node */
1659 } else { /* Otherwise extract it from the kernel FDT. */
1660 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1662 debug("No fdt property found.\n");
1665 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1667 if (kfdt_noffset < 0) {
1668 debug("No image node named \"%s\" found.\n",
1673 if (!fit_image_check_comp(fit, kfdt_noffset,
1675 debug("Can't extract compat from \"%s\" "
1676 "(compressed)\n", kfdt_name);
1680 /* search in this config's kernel FDT */
1681 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1682 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1686 compat_noffset = 0; /* search kFDT under root node */
1689 len = fdt_compat_len;
1690 cur_fdt_compat = fdt_compat;
1692 * Look for a match for each U-Boot compatibility string in
1693 * turn in the compat string property.
1695 for (i = 0; len > 0 &&
1696 (!best_match_offset || best_match_pos > i); i++) {
1697 int cur_len = strlen(cur_fdt_compat) + 1;
1699 if (!fdt_node_check_compatible(fdt, compat_noffset,
1701 best_match_offset = noffset;
1706 cur_fdt_compat += cur_len;
1709 if (!best_match_offset) {
1710 debug("No match found.\n");
1714 return best_match_offset;
1717 int fit_conf_get_node(const void *fit, const char *conf_uname)
1719 int noffset, confs_noffset;
1722 char *conf_uname_copy = NULL;
1724 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1725 if (confs_noffset < 0) {
1726 debug("Can't find configurations parent node '%s' (%s)\n",
1727 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1728 return confs_noffset;
1731 if (conf_uname == NULL) {
1732 /* get configuration unit name from the default property */
1733 debug("No configuration specified, trying default...\n");
1734 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1735 FIT_DEFAULT_PROP, &len);
1736 if (conf_uname == NULL) {
1737 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1741 debug("Found default configuration: '%s'\n", conf_uname);
1744 s = strchr(conf_uname, '#');
1746 len = s - conf_uname;
1747 conf_uname_copy = malloc(len + 1);
1748 if (!conf_uname_copy) {
1749 debug("Can't allocate uname copy: '%s'\n",
1753 memcpy(conf_uname_copy, conf_uname, len);
1754 conf_uname_copy[len] = '\0';
1755 conf_uname = conf_uname_copy;
1758 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1760 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1761 conf_uname, fdt_strerror(noffset));
1764 if (conf_uname_copy)
1765 free(conf_uname_copy);
1770 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1771 const char *prop_name)
1773 return fdt_stringlist_count(fit, noffset, prop_name);
1776 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1777 const char *prop_name, int index)
1782 /* get kernel image unit name from configuration kernel property */
1783 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1787 return fit_image_get_node(fit, uname);
1790 int fit_conf_get_prop_node(const void *fit, int noffset,
1791 const char *prop_name)
1793 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1796 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1798 fit_image_print(fit, rd_noffset, " ");
1801 puts(" Verifying Hash Integrity ... ");
1802 if (!fit_image_verify(fit, rd_noffset)) {
1803 puts("Bad Data Hash\n");
1812 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1819 debug("* %s: using config '%s' from image at 0x%08lx\n",
1820 prop_name, images->fit_uname_cfg, addr);
1822 /* Check whether configuration has this property defined */
1823 fit_hdr = map_sysmem(addr, 0);
1824 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1825 if (cfg_noffset < 0) {
1826 debug("* %s: no such config\n", prop_name);
1830 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1832 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1840 * fit_get_image_type_property() - get property name for IH_TYPE_...
1842 * @return the properly name where we expect to find the image in the
1845 static const char *fit_get_image_type_property(int type)
1848 * This is sort-of available in the uimage_type[] table in image.c
1849 * but we don't have access to the short name, and "fdt" is different
1850 * anyway. So let's just keep it here.
1853 case IH_TYPE_FLATDT:
1854 return FIT_FDT_PROP;
1855 case IH_TYPE_KERNEL:
1856 return FIT_KERNEL_PROP;
1857 case IH_TYPE_RAMDISK:
1858 return FIT_RAMDISK_PROP;
1859 case IH_TYPE_X86_SETUP:
1860 return FIT_SETUP_PROP;
1861 case IH_TYPE_LOADABLE:
1862 return FIT_LOADABLE_PROP;
1864 return FIT_FPGA_PROP;
1865 case IH_TYPE_STANDALONE:
1866 return FIT_STANDALONE_PROP;
1872 int fit_image_load(bootm_headers_t *images, ulong addr,
1873 const char **fit_unamep, const char **fit_uname_configp,
1874 int arch, int image_type, int bootstage_id,
1875 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1877 int cfg_noffset, noffset;
1878 const char *fit_uname;
1879 const char *fit_uname_config;
1880 const char *fit_base_uname_config;
1886 ulong load, load_end, data, len;
1891 const char *prop_name;
1894 fit = map_sysmem(addr, 0);
1895 fit_uname = fit_unamep ? *fit_unamep : NULL;
1896 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1897 fit_base_uname_config = NULL;
1898 prop_name = fit_get_image_type_property(image_type);
1899 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1901 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1902 if (!fit_check_format(fit)) {
1903 printf("Bad FIT %s image format!\n", prop_name);
1904 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1907 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1909 /* get FIT component image node offset */
1910 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1911 noffset = fit_image_get_node(fit, fit_uname);
1914 * no image node unit name, try to get config
1915 * node first. If config unit node name is NULL
1916 * fit_conf_get_node() will try to find default config node
1918 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1919 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1920 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1922 cfg_noffset = fit_conf_get_node(fit,
1925 if (cfg_noffset < 0) {
1926 puts("Could not find configuration node\n");
1927 bootstage_error(bootstage_id +
1928 BOOTSTAGE_SUB_NO_UNIT_NAME);
1932 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1933 printf(" Using '%s' configuration\n", fit_base_uname_config);
1934 /* Remember this config */
1935 if (image_type == IH_TYPE_KERNEL)
1936 images->fit_uname_cfg = fit_base_uname_config;
1938 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
1939 puts(" Verifying Hash Integrity ... ");
1940 if (fit_config_verify(fit, cfg_noffset)) {
1941 puts("Bad Data Hash\n");
1942 bootstage_error(bootstage_id +
1943 BOOTSTAGE_SUB_HASH);
1949 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1951 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1953 fit_uname = fit_get_name(fit, noffset, NULL);
1956 printf("Could not find subimage node type '%s'\n", prop_name);
1957 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1961 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1963 ret = fit_image_select(fit, noffset, images->verify);
1965 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1969 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1970 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1971 if (!fit_image_check_target_arch(fit, noffset)) {
1972 puts("Unsupported Architecture\n");
1973 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1979 fit_image_get_arch(fit, noffset, &os_arch);
1980 images->os.arch = os_arch;
1983 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1984 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1985 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1986 (image_type == IH_TYPE_KERNEL &&
1987 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1989 os_ok = image_type == IH_TYPE_FLATDT ||
1990 image_type == IH_TYPE_FPGA ||
1991 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1992 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1993 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
1994 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
1995 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
1998 * If either of the checks fail, we should report an error, but
1999 * if the image type is coming from the "loadables" field, we
2000 * don't care what it is
2002 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
2003 fit_image_get_os(fit, noffset, &os);
2004 printf("No %s %s %s Image\n",
2005 genimg_get_os_name(os),
2006 genimg_get_arch_name(arch),
2007 genimg_get_type_name(image_type));
2008 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2012 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2014 /* get image data address and length */
2015 if (fit_image_get_data_and_size(fit, noffset,
2016 (const void **)&buf, &size)) {
2017 printf("Could not find %s subimage data!\n", prop_name);
2018 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2022 #ifdef CONFIG_FIT_CIPHER
2023 /* Decrypt data before uncompress/move */
2024 if (IMAGE_ENABLE_DECRYPT) {
2025 puts(" Decrypting Data ... ");
2026 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2034 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
2035 /* perform any post-processing on the image data */
2036 board_fit_image_post_process(&buf, &size);
2041 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2043 data = map_to_sysmem(buf);
2045 if (load_op == FIT_LOAD_IGNORED) {
2047 } else if (fit_image_get_load(fit, noffset, &load)) {
2048 if (load_op == FIT_LOAD_REQUIRED) {
2049 printf("Can't get %s subimage load address!\n",
2051 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2054 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
2055 ulong image_start, image_end;
2058 * move image data to the load address,
2059 * make sure we don't overwrite initial image
2062 image_end = addr + fit_get_size(fit);
2064 load_end = load + len;
2065 if (image_type != IH_TYPE_KERNEL &&
2066 load < image_end && load_end > image_start) {
2067 printf("Error: %s overwritten\n", prop_name);
2071 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2072 prop_name, data, load);
2074 load = data; /* No load address specified */
2077 comp = IH_COMP_NONE;
2079 /* Kernel images get decompressed later in bootm_load_os(). */
2080 if (!fit_image_get_comp(fit, noffset, &comp) &&
2081 comp != IH_COMP_NONE &&
2082 !(image_type == IH_TYPE_KERNEL ||
2083 image_type == IH_TYPE_KERNEL_NOLOAD ||
2084 image_type == IH_TYPE_RAMDISK)) {
2085 ulong max_decomp_len = len * 20;
2087 loadbuf = malloc(max_decomp_len);
2088 load = map_to_sysmem(loadbuf);
2090 loadbuf = map_sysmem(load, max_decomp_len);
2092 if (image_decomp(comp, load, data, image_type,
2093 loadbuf, buf, len, max_decomp_len, &load_end)) {
2094 printf("Error decompressing %s\n", prop_name);
2098 len = load_end - load;
2099 } else if (load != data) {
2100 loadbuf = map_sysmem(load, len);
2101 memcpy(loadbuf, buf, len);
2104 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2105 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2106 " please fix your .its file!\n");
2108 /* verify that image data is a proper FDT blob */
2109 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2110 puts("Subimage data is not a FDT");
2114 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2119 *fit_unamep = (char *)fit_uname;
2120 if (fit_uname_configp)
2121 *fit_uname_configp = (char *)(fit_uname_config ? :
2122 fit_base_uname_config);
2127 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2128 ulong *setup_start, ulong *setup_len)
2135 addr = map_to_sysmem(images->fit_hdr_os);
2136 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2140 ret = fit_image_load(images, addr, NULL, NULL, arch,
2141 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2142 FIT_LOAD_REQUIRED, setup_start, &len);
2148 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2149 const char **fit_unamep, const char **fit_uname_configp,
2150 int arch, ulong *datap, ulong *lenp)
2152 int fdt_noffset, cfg_noffset, count;
2154 const char *fit_uname = NULL;
2155 const char *fit_uname_config = NULL;
2156 char *fit_uname_config_copy = NULL;
2157 char *next_config = NULL;
2159 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2160 ulong image_start, image_end;
2161 ulong ovload, ovlen;
2162 const char *uconfig;
2165 int i, err, noffset, ov_noffset;
2168 fit_uname = fit_unamep ? *fit_unamep : NULL;
2170 if (fit_uname_configp && *fit_uname_configp) {
2171 fit_uname_config_copy = strdup(*fit_uname_configp);
2172 if (!fit_uname_config_copy)
2175 next_config = strchr(fit_uname_config_copy, '#');
2177 *next_config++ = '\0';
2178 if (next_config - 1 > fit_uname_config_copy)
2179 fit_uname_config = fit_uname_config_copy;
2182 fdt_noffset = fit_image_load(images,
2183 addr, &fit_uname, &fit_uname_config,
2184 arch, IH_TYPE_FLATDT,
2185 BOOTSTAGE_ID_FIT_FDT_START,
2186 FIT_LOAD_OPTIONAL, &load, &len);
2188 if (fdt_noffset < 0)
2191 debug("fit_uname=%s, fit_uname_config=%s\n",
2192 fit_uname ? fit_uname : "<NULL>",
2193 fit_uname_config ? fit_uname_config : "<NULL>");
2195 fit = map_sysmem(addr, 0);
2197 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2199 /* single blob, or error just return as well */
2200 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2201 if (count <= 1 && !next_config)
2204 /* we need to apply overlays */
2206 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2208 image_end = addr + fit_get_size(fit);
2209 /* verify that relocation took place by load address not being in fit */
2210 if (load >= image_start && load < image_end) {
2211 /* check is simplified; fit load checks for overlaps */
2212 printf("Overlayed FDT requires relocation\n");
2213 fdt_noffset = -EBADF;
2217 base = map_sysmem(load, len);
2219 /* apply extra configs in FIT first, followed by args */
2220 for (i = 1; ; i++) {
2222 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2224 uname = fit_get_name(fit, noffset, NULL);
2229 uconfig = next_config;
2230 next_config = strchr(next_config, '#');
2232 *next_config++ = '\0';
2236 * fit_image_load() would load the first FDT from the
2237 * extra config only when uconfig is specified.
2238 * Check if the extra config contains multiple FDTs and
2241 cfg_noffset = fit_conf_get_node(fit, uconfig);
2244 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2248 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2250 ov_noffset = fit_image_load(images,
2251 addr, &uname, &uconfig,
2252 arch, IH_TYPE_FLATDT,
2253 BOOTSTAGE_ID_FIT_FDT_START,
2254 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2255 if (ov_noffset < 0) {
2256 printf("load of %s failed\n", uname);
2259 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2260 uname, ovload, ovlen);
2261 ov = map_sysmem(ovload, ovlen);
2263 base = map_sysmem(load, len + ovlen);
2264 err = fdt_open_into(base, base, len + ovlen);
2266 printf("failed on fdt_open_into\n");
2270 /* the verbose method prints out messages on error */
2271 err = fdt_overlay_apply_verbose(base, ov);
2277 len = fdt_totalsize(base);
2280 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2281 fdt_noffset = -EBADF;
2290 *fit_unamep = fit_uname;
2291 if (fit_uname_configp)
2292 *fit_uname_configp = fit_uname_config;
2294 if (fit_uname_config_copy)
2295 free(fit_uname_config_copy);