1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
5 * (C) Copyright 2008 Semihalf
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 #include <linux/compiler.h>
16 #include <linux/kconfig.h>
22 DECLARE_GLOBAL_DATA_PTR;
23 #endif /* !USE_HOSTCC*/
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_SPL_FIT_PRINT)
148 * fit_image_print_data() - prints out the hash node details
149 * @fit: pointer to the FIT format image header
150 * @noffset: offset of the hash node
151 * @p: pointer to prefix string
152 * @type: Type of information to print ("hash" or "sign")
154 * fit_image_print_data() lists properties for the processed hash node
156 * This function avoid using puts() since it prints a newline on the host
157 * but does not in U-Boot.
160 * no returned results
162 static void fit_image_print_data(const void *fit, int noffset, const char *p,
173 debug("%s %s node: '%s'\n", p, type,
174 fit_get_name(fit, noffset, NULL));
175 printf("%s %s algo: ", p, type);
176 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
177 printf("invalid/unsupported\n");
181 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
182 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
184 printf(":%s", keyname);
186 printf(" (required)");
189 padding = fdt_getprop(fit, noffset, "padding", NULL);
191 printf("%s %s padding: %s\n", p, type, padding);
193 ret = fit_image_hash_get_value(fit, noffset, &value,
195 printf("%s %s value: ", p, type);
197 printf("unavailable\n");
199 for (i = 0; i < value_len; i++)
200 printf("%02x", value[i]);
204 debug("%s %s len: %d\n", p, type, value_len);
206 /* Signatures have a time stamp */
207 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
210 printf("%s Timestamp: ", p);
211 if (fit_get_timestamp(fit, noffset, ×tamp))
212 printf("unavailable\n");
214 genimg_print_time(timestamp);
219 * fit_image_print_verification_data() - prints out the hash/signature details
220 * @fit: pointer to the FIT format image header
221 * @noffset: offset of the hash or signature node
222 * @p: pointer to prefix string
224 * This lists properties for the processed hash node
227 * no returned results
229 static void fit_image_print_verification_data(const void *fit, int noffset,
235 * Check subnode name, must be equal to "hash" or "signature".
236 * Multiple hash/signature nodes require unique unit node
237 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
239 name = fit_get_name(fit, noffset, NULL);
240 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
241 fit_image_print_data(fit, noffset, p, "Hash");
242 } else if (!strncmp(name, FIT_SIG_NODENAME,
243 strlen(FIT_SIG_NODENAME))) {
244 fit_image_print_data(fit, noffset, p, "Sign");
249 * fit_conf_print - prints out the FIT configuration details
250 * @fit: pointer to the FIT format image header
251 * @noffset: offset of the configuration node
252 * @p: pointer to prefix string
254 * fit_conf_print() lists all mandatory properties for the processed
255 * configuration node.
258 * no returned results
260 static void fit_conf_print(const void *fit, int noffset, const char *p)
265 int fdt_index, loadables_index;
268 /* Mandatory properties */
269 ret = fit_get_desc(fit, noffset, &desc);
270 printf("%s Description: ", p);
272 printf("unavailable\n");
274 printf("%s\n", desc);
276 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
277 printf("%s Kernel: ", p);
279 printf("unavailable\n");
281 printf("%s\n", uname);
283 /* Optional properties */
284 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
286 printf("%s Init Ramdisk: %s\n", p, uname);
288 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
290 printf("%s Firmware: %s\n", p, uname);
293 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
294 fdt_index, NULL), uname;
297 printf("%s FDT: ", p);
300 printf("%s\n", uname);
303 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
305 printf("%s FPGA: %s\n", p, uname);
307 /* Print out all of the specified loadables */
308 for (loadables_index = 0;
309 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
310 loadables_index, NULL), uname;
312 if (loadables_index == 0) {
313 printf("%s Loadables: ", p);
317 printf("%s\n", uname);
320 /* Process all hash subnodes of the component configuration node */
321 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
322 (noffset >= 0) && (ndepth > 0);
323 noffset = fdt_next_node(fit, noffset, &ndepth)) {
325 /* Direct child node of the component configuration node */
326 fit_image_print_verification_data(fit, noffset, p);
332 * fit_print_contents - prints out the contents of the FIT format image
333 * @fit: pointer to the FIT format image header
334 * @p: pointer to prefix string
336 * fit_print_contents() formats a multi line FIT image contents description.
337 * The routine prints out FIT image properties (root node level) followed by
338 * the details of each component image.
341 * no returned results
343 void fit_print_contents(const void *fit)
356 /* Indent string is defined in header image.h */
357 p = IMAGE_INDENT_STRING;
359 /* Root node properties */
360 ret = fit_get_desc(fit, 0, &desc);
361 printf("%sFIT description: ", p);
363 printf("unavailable\n");
365 printf("%s\n", desc);
367 if (IMAGE_ENABLE_TIMESTAMP) {
368 ret = fit_get_timestamp(fit, 0, ×tamp);
369 printf("%sCreated: ", p);
371 printf("unavailable\n");
373 genimg_print_time(timestamp);
376 /* Find images parent node offset */
377 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
378 if (images_noffset < 0) {
379 printf("Can't find images parent node '%s' (%s)\n",
380 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
384 /* Process its subnodes, print out component images details */
385 for (ndepth = 0, count = 0,
386 noffset = fdt_next_node(fit, images_noffset, &ndepth);
387 (noffset >= 0) && (ndepth > 0);
388 noffset = fdt_next_node(fit, noffset, &ndepth)) {
391 * Direct child node of the images parent node,
392 * i.e. component image node.
394 printf("%s Image %u (%s)\n", p, count++,
395 fit_get_name(fit, noffset, NULL));
397 fit_image_print(fit, noffset, p);
401 /* Find configurations parent node offset */
402 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
403 if (confs_noffset < 0) {
404 debug("Can't get configurations parent node '%s' (%s)\n",
405 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
409 /* get default configuration unit name from default property */
410 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
412 printf("%s Default Configuration: '%s'\n", p, uname);
414 /* Process its subnodes, print out configurations details */
415 for (ndepth = 0, count = 0,
416 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
417 (noffset >= 0) && (ndepth > 0);
418 noffset = fdt_next_node(fit, noffset, &ndepth)) {
421 * Direct child node of the configurations parent node,
422 * i.e. configuration node.
424 printf("%s Configuration %u (%s)\n", p, count++,
425 fit_get_name(fit, noffset, NULL));
427 fit_conf_print(fit, noffset, p);
433 * fit_image_print - prints out the FIT component image details
434 * @fit: pointer to the FIT format image header
435 * @image_noffset: offset of the component image node
436 * @p: pointer to prefix string
438 * fit_image_print() lists all mandatory properties for the processed component
439 * image. If present, hash nodes are printed out as well. Load
440 * address for images of type firmware is also printed out. Since the load
441 * address is not mandatory for firmware images, it will be output as
442 * "unavailable" when not present.
445 * no returned results
447 void fit_image_print(const void *fit, int image_noffset, const char *p)
450 uint8_t type, arch, os, comp;
458 /* Mandatory properties */
459 ret = fit_get_desc(fit, image_noffset, &desc);
460 printf("%s Description: ", p);
462 printf("unavailable\n");
464 printf("%s\n", desc);
466 if (IMAGE_ENABLE_TIMESTAMP) {
469 ret = fit_get_timestamp(fit, 0, ×tamp);
470 printf("%s Created: ", p);
472 printf("unavailable\n");
474 genimg_print_time(timestamp);
477 fit_image_get_type(fit, image_noffset, &type);
478 printf("%s Type: %s\n", p, genimg_get_type_name(type));
480 fit_image_get_comp(fit, image_noffset, &comp);
481 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
483 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
486 printf("%s Data Start: ", p);
488 printf("unavailable\n");
490 void *vdata = (void *)data;
492 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
496 printf("%s Data Size: ", p);
498 printf("unavailable\n");
500 genimg_print_size(size);
502 /* Remaining, type dependent properties */
503 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
504 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
505 (type == IH_TYPE_FLATDT)) {
506 fit_image_get_arch(fit, image_noffset, &arch);
507 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
510 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
511 (type == IH_TYPE_FIRMWARE)) {
512 fit_image_get_os(fit, image_noffset, &os);
513 printf("%s OS: %s\n", p, genimg_get_os_name(os));
516 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
517 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
518 (type == IH_TYPE_FPGA)) {
519 ret = fit_image_get_load(fit, image_noffset, &load);
520 printf("%s Load Address: ", p);
522 printf("unavailable\n");
524 printf("0x%08lx\n", load);
527 /* optional load address for FDT */
528 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
529 printf("%s Load Address: 0x%08lx\n", p, load);
531 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
532 (type == IH_TYPE_RAMDISK)) {
533 ret = fit_image_get_entry(fit, image_noffset, &entry);
534 printf("%s Entry Point: ", p);
536 printf("unavailable\n");
538 printf("0x%08lx\n", entry);
541 /* Process all hash subnodes of the component image node */
542 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
543 (noffset >= 0) && (ndepth > 0);
544 noffset = fdt_next_node(fit, noffset, &ndepth)) {
546 /* Direct child node of the component image node */
547 fit_image_print_verification_data(fit, noffset, p);
552 void fit_print_contents(const void *fit) { }
553 void fit_image_print(const void *fit, int image_noffset, const char *p) { }
554 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
557 * fit_get_desc - get node description property
558 * @fit: pointer to the FIT format image header
559 * @noffset: node offset
560 * @desc: double pointer to the char, will hold pointer to the description
562 * fit_get_desc() reads description property from a given node, if
563 * description is found pointer to it is returned in third call argument.
569 int fit_get_desc(const void *fit, int noffset, char **desc)
573 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
575 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
583 * fit_get_timestamp - get node timestamp property
584 * @fit: pointer to the FIT format image header
585 * @noffset: node offset
586 * @timestamp: pointer to the time_t, will hold read timestamp
588 * fit_get_timestamp() reads timestamp property from given node, if timestamp
589 * is found and has a correct size its value is returned in third call
594 * -1, on property read failure
595 * -2, on wrong timestamp size
597 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
602 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
604 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
607 if (len != sizeof(uint32_t)) {
608 debug("FIT timestamp with incorrect size of (%u)\n", len);
612 *timestamp = uimage_to_cpu(*((uint32_t *)data));
617 * fit_image_get_node - get node offset for component image of a given unit name
618 * @fit: pointer to the FIT format image header
619 * @image_uname: component image node unit name
621 * fit_image_get_node() finds a component image (within the '/images'
622 * node) of a provided unit name. If image is found its node offset is
623 * returned to the caller.
626 * image node offset when found (>=0)
627 * negative number on failure (FDT_ERR_* code)
629 int fit_image_get_node(const void *fit, const char *image_uname)
631 int noffset, images_noffset;
633 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
634 if (images_noffset < 0) {
635 debug("Can't find images parent node '%s' (%s)\n",
636 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
637 return images_noffset;
640 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
642 debug("Can't get node offset for image unit name: '%s' (%s)\n",
643 image_uname, fdt_strerror(noffset));
650 * fit_image_get_os - get os id for a given component image node
651 * @fit: pointer to the FIT format image header
652 * @noffset: component image node offset
653 * @os: pointer to the uint8_t, will hold os numeric id
655 * fit_image_get_os() finds os property in a given component image node.
656 * If the property is found, its (string) value is translated to the numeric
657 * id which is returned to the caller.
663 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
668 /* Get OS name from property data */
669 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
671 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
676 /* Translate OS name to id */
677 *os = genimg_get_os_id(data);
682 * fit_image_get_arch - get arch id for a given component image node
683 * @fit: pointer to the FIT format image header
684 * @noffset: component image node offset
685 * @arch: pointer to the uint8_t, will hold arch numeric id
687 * fit_image_get_arch() finds arch property in a given component image node.
688 * If the property is found, its (string) value is translated to the numeric
689 * id which is returned to the caller.
695 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
700 /* Get architecture name from property data */
701 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
703 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
708 /* Translate architecture name to id */
709 *arch = genimg_get_arch_id(data);
714 * fit_image_get_type - get type id for a given component image node
715 * @fit: pointer to the FIT format image header
716 * @noffset: component image node offset
717 * @type: pointer to the uint8_t, will hold type numeric id
719 * fit_image_get_type() finds type property in a given component image node.
720 * If the property is found, its (string) value is translated to the numeric
721 * id which is returned to the caller.
727 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
732 /* Get image type name from property data */
733 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
735 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
740 /* Translate image type name to id */
741 *type = genimg_get_type_id(data);
746 * fit_image_get_comp - get comp id for a given component image node
747 * @fit: pointer to the FIT format image header
748 * @noffset: component image node offset
749 * @comp: pointer to the uint8_t, will hold comp numeric id
751 * fit_image_get_comp() finds comp property in a given component image node.
752 * If the property is found, its (string) value is translated to the numeric
753 * id which is returned to the caller.
759 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
764 /* Get compression name from property data */
765 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
767 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
772 /* Translate compression name to id */
773 *comp = genimg_get_comp_id(data);
777 static int fit_image_get_address(const void *fit, int noffset, char *name,
784 cell = fdt_getprop(fit, noffset, name, &len);
786 fit_get_debug(fit, noffset, name, len);
790 if (len > sizeof(ulong)) {
791 printf("Unsupported %s address size\n", name);
796 /* Use load64 to avoid compiling warning for 32-bit target */
798 load64 = (load64 << 32) | uimage_to_cpu(*cell);
801 *load = (ulong)load64;
806 * fit_image_get_load() - get load addr property for given component image node
807 * @fit: pointer to the FIT format image header
808 * @noffset: component image node offset
809 * @load: pointer to the uint32_t, will hold load address
811 * fit_image_get_load() finds load address property in a given component
812 * image node. If the property is found, its value is returned to the caller.
818 int fit_image_get_load(const void *fit, int noffset, ulong *load)
820 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
824 * fit_image_get_entry() - get entry point address property
825 * @fit: pointer to the FIT format image header
826 * @noffset: component image node offset
827 * @entry: pointer to the uint32_t, will hold entry point address
829 * This gets the entry point address property for a given component image
832 * fit_image_get_entry() finds entry point address property in a given
833 * component image node. If the property is found, its value is returned
840 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
842 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
846 * fit_image_get_data - get data property and its size for a given component image node
847 * @fit: pointer to the FIT format image header
848 * @noffset: component image node offset
849 * @data: double pointer to void, will hold data property's data address
850 * @size: pointer to size_t, will hold data property's data size
852 * fit_image_get_data() finds data property in a given component image node.
853 * If the property is found its data start address and size are returned to
860 int fit_image_get_data(const void *fit, int noffset,
861 const void **data, size_t *size)
865 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
867 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
877 * Get 'data-offset' property from a given image node.
879 * @fit: pointer to the FIT image header
880 * @noffset: component image node offset
881 * @data_offset: holds the data-offset property
885 * -ENOENT if the property could not be found
887 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
891 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
895 *data_offset = fdt32_to_cpu(*val);
901 * Get 'data-position' property from a given image node.
903 * @fit: pointer to the FIT image header
904 * @noffset: component image node offset
905 * @data_position: holds the data-position property
909 * -ENOENT if the property could not be found
911 int fit_image_get_data_position(const void *fit, int noffset,
916 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
920 *data_position = fdt32_to_cpu(*val);
926 * Get 'data-size' property from a given image node.
928 * @fit: pointer to the FIT image header
929 * @noffset: component image node offset
930 * @data_size: holds the data-size property
934 * -ENOENT if the property could not be found
936 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
940 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
944 *data_size = fdt32_to_cpu(*val);
950 * fit_image_get_data_and_size - get data and its size including
951 * both embedded and external data
952 * @fit: pointer to the FIT format image header
953 * @noffset: component image node offset
954 * @data: double pointer to void, will hold data property's data address
955 * @size: pointer to size_t, will hold data property's data size
957 * fit_image_get_data_and_size() finds data and its size including
958 * both embedded and external data. If the property is found
959 * its data start address and size are returned to the caller.
963 * otherwise, on failure
965 int fit_image_get_data_and_size(const void *fit, int noffset,
966 const void **data, size_t *size)
968 bool external_data = false;
973 if (!fit_image_get_data_position(fit, noffset, &offset)) {
974 external_data = true;
975 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
976 external_data = true;
978 * For FIT with external data, figure out where
979 * the external images start. This is the base
980 * for the data-offset properties in each image.
982 offset += ((fdt_totalsize(fit) + 3) & ~3);
986 debug("External Data\n");
987 ret = fit_image_get_data_size(fit, noffset, &len);
988 *data = fit + offset;
991 ret = fit_image_get_data(fit, noffset, data, size);
998 * fit_image_hash_get_algo - get hash algorithm name
999 * @fit: pointer to the FIT format image header
1000 * @noffset: hash node offset
1001 * @algo: double pointer to char, will hold pointer to the algorithm name
1003 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1004 * If the property is found its data start address is returned to the caller.
1010 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1014 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1015 if (*algo == NULL) {
1016 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1024 * fit_image_hash_get_value - get hash value and length
1025 * @fit: pointer to the FIT format image header
1026 * @noffset: hash node offset
1027 * @value: double pointer to uint8_t, will hold address of a hash value data
1028 * @value_len: pointer to an int, will hold hash data length
1030 * fit_image_hash_get_value() finds hash value property in a given hash node.
1031 * If the property is found its data start address and size are returned to
1038 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1043 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1044 if (*value == NULL) {
1045 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1055 * fit_image_hash_get_ignore - get hash ignore flag
1056 * @fit: pointer to the FIT format image header
1057 * @noffset: hash node offset
1058 * @ignore: pointer to an int, will hold hash ignore flag
1060 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1061 * If the property is found and non-zero, the hash algorithm is not verified by
1062 * u-boot automatically.
1065 * 0, on ignore not found
1066 * value, on ignore found
1068 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1073 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1074 if (value == NULL || len != sizeof(int))
1082 ulong fit_get_end(const void *fit)
1084 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1088 * fit_set_timestamp - set node timestamp property
1089 * @fit: pointer to the FIT format image header
1090 * @noffset: node offset
1091 * @timestamp: timestamp value to be set
1093 * fit_set_timestamp() attempts to set timestamp property in the requested
1094 * node and returns operation status to the caller.
1098 * -ENOSPC if no space in device tree, -1 for other error
1100 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1105 t = cpu_to_uimage(timestamp);
1106 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1109 debug("Can't set '%s' property for '%s' node (%s)\n",
1110 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1112 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1119 * calculate_hash - calculate and return hash for provided input data
1120 * @data: pointer to the input data
1121 * @data_len: data length
1122 * @algo: requested hash algorithm
1123 * @value: pointer to the char, will hold hash value data (caller must
1124 * allocate enough free space)
1125 * value_len: length of the calculated hash
1127 * calculate_hash() computes input data hash according to the requested
1129 * Resulting hash value is placed in caller provided 'value' buffer, length
1130 * of the calculated hash is returned via value_len pointer argument.
1134 * -1, when algo is unsupported
1136 int calculate_hash(const void *data, int data_len, const char *algo,
1137 uint8_t *value, int *value_len)
1139 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1140 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1142 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1144 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1145 sha1_csum_wd((unsigned char *)data, data_len,
1146 (unsigned char *)value, CHUNKSZ_SHA1);
1148 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1149 sha256_csum_wd((unsigned char *)data, data_len,
1150 (unsigned char *)value, CHUNKSZ_SHA256);
1151 *value_len = SHA256_SUM_LEN;
1152 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1153 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1156 debug("Unsupported hash alogrithm\n");
1162 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1163 size_t size, char **err_msgp)
1165 uint8_t value[FIT_MAX_HASH_LEN];
1174 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1175 *err_msgp = "Can't get hash algo property";
1180 if (IMAGE_ENABLE_IGNORE) {
1181 fit_image_hash_get_ignore(fit, noffset, &ignore);
1183 printf("-skipped ");
1188 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1190 *err_msgp = "Can't get hash value property";
1194 if (calculate_hash(data, size, algo, value, &value_len)) {
1195 *err_msgp = "Unsupported hash algorithm";
1199 if (value_len != fit_value_len) {
1200 *err_msgp = "Bad hash value len";
1202 } else if (memcmp(value, fit_value, value_len) != 0) {
1203 *err_msgp = "Bad hash value";
1210 int fit_image_verify_with_data(const void *fit, int image_noffset,
1211 const void *data, size_t size)
1218 /* Verify all required signatures */
1219 if (IMAGE_ENABLE_VERIFY &&
1220 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1221 gd_fdt_blob(), &verify_all)) {
1222 err_msg = "Unable to verify required signature";
1226 /* Process all hash subnodes of the component image node */
1227 fdt_for_each_subnode(noffset, fit, image_noffset) {
1228 const char *name = fit_get_name(fit, noffset, NULL);
1231 * Check subnode name, must be equal to "hash".
1232 * Multiple hash nodes require unique unit node
1233 * names, e.g. hash-1, hash-2, etc.
1235 if (!strncmp(name, FIT_HASH_NODENAME,
1236 strlen(FIT_HASH_NODENAME))) {
1237 if (fit_image_check_hash(fit, noffset, data, size,
1241 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1242 !strncmp(name, FIT_SIG_NODENAME,
1243 strlen(FIT_SIG_NODENAME))) {
1244 ret = fit_image_check_sig(fit, noffset, data,
1245 size, -1, &err_msg);
1248 * Show an indication on failure, but do not return
1249 * an error. Only keys marked 'required' can cause
1250 * an image validation failure. See the call to
1251 * fit_image_verify_required_sigs() above.
1260 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1261 err_msg = "Corrupted or truncated tree";
1268 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1269 err_msg, fit_get_name(fit, noffset, NULL),
1270 fit_get_name(fit, image_noffset, NULL));
1275 * fit_image_verify - verify data integrity
1276 * @fit: pointer to the FIT format image header
1277 * @image_noffset: component image node offset
1279 * fit_image_verify() goes over component image hash nodes,
1280 * re-calculates each data hash and compares with the value stored in hash
1284 * 1, if all hashes are valid
1285 * 0, otherwise (or on error)
1287 int fit_image_verify(const void *fit, int image_noffset)
1294 /* Get image data and data length */
1295 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1296 err_msg = "Can't get image data/size";
1297 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1298 err_msg, fit_get_name(fit, noffset, NULL),
1299 fit_get_name(fit, image_noffset, NULL));
1303 return fit_image_verify_with_data(fit, image_noffset, data, size);
1307 * fit_all_image_verify - verify data integrity for all images
1308 * @fit: pointer to the FIT format image header
1310 * fit_all_image_verify() goes over all images in the FIT and
1311 * for every images checks if all it's hashes are valid.
1314 * 1, if all hashes of all images are valid
1315 * 0, otherwise (or on error)
1317 int fit_all_image_verify(const void *fit)
1324 /* Find images parent node offset */
1325 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1326 if (images_noffset < 0) {
1327 printf("Can't find images parent node '%s' (%s)\n",
1328 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1332 /* Process all image subnodes, check hashes for each */
1333 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1335 for (ndepth = 0, count = 0,
1336 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1337 (noffset >= 0) && (ndepth > 0);
1338 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1341 * Direct child node of the images parent node,
1342 * i.e. component image node.
1344 printf(" Hash(es) for Image %u (%s): ", count,
1345 fit_get_name(fit, noffset, NULL));
1348 if (!fit_image_verify(fit, noffset))
1357 * fit_image_check_os - check whether image node is of a given os type
1358 * @fit: pointer to the FIT format image header
1359 * @noffset: component image node offset
1360 * @os: requested image os
1362 * fit_image_check_os() reads image os property and compares its numeric
1363 * id with the requested os. Comparison result is returned to the caller.
1366 * 1 if image is of given os type
1367 * 0 otherwise (or on error)
1369 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1373 if (fit_image_get_os(fit, noffset, &image_os))
1375 return (os == image_os);
1379 * fit_image_check_arch - check whether image node is of a given arch
1380 * @fit: pointer to the FIT format image header
1381 * @noffset: component image node offset
1382 * @arch: requested imagearch
1384 * fit_image_check_arch() reads image arch property and compares its numeric
1385 * id with the requested arch. Comparison result is returned to the caller.
1388 * 1 if image is of given arch
1389 * 0 otherwise (or on error)
1391 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1394 int aarch32_support = 0;
1396 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1397 aarch32_support = 1;
1400 if (fit_image_get_arch(fit, noffset, &image_arch))
1402 return (arch == image_arch) ||
1403 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1404 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1409 * fit_image_check_type - check whether image node is of a given type
1410 * @fit: pointer to the FIT format image header
1411 * @noffset: component image node offset
1412 * @type: requested image type
1414 * fit_image_check_type() reads image type property and compares its numeric
1415 * id with the requested type. Comparison result is returned to the caller.
1418 * 1 if image is of given type
1419 * 0 otherwise (or on error)
1421 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1425 if (fit_image_get_type(fit, noffset, &image_type))
1427 return (type == image_type);
1431 * fit_image_check_comp - check whether image node uses given compression
1432 * @fit: pointer to the FIT format image header
1433 * @noffset: component image node offset
1434 * @comp: requested image compression type
1436 * fit_image_check_comp() reads image compression property and compares its
1437 * numeric id with the requested compression type. Comparison result is
1438 * returned to the caller.
1441 * 1 if image uses requested compression
1442 * 0 otherwise (or on error)
1444 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1448 if (fit_image_get_comp(fit, noffset, &image_comp))
1450 return (comp == image_comp);
1454 * fit_check_format - sanity check FIT image format
1455 * @fit: pointer to the FIT format image header
1457 * fit_check_format() runs a basic sanity FIT image verification.
1458 * Routine checks for mandatory properties, nodes, etc.
1464 int fit_check_format(const void *fit)
1466 /* mandatory / node 'description' property */
1467 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1468 debug("Wrong FIT format: no description\n");
1472 if (IMAGE_ENABLE_TIMESTAMP) {
1473 /* mandatory / node 'timestamp' property */
1474 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1475 debug("Wrong FIT format: no timestamp\n");
1480 /* mandatory subimages parent '/images' node */
1481 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1482 debug("Wrong FIT format: no images parent node\n");
1491 * fit_conf_find_compat
1492 * @fit: pointer to the FIT format image header
1493 * @fdt: pointer to the device tree to compare against
1495 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1496 * most compatible with the passed in device tree.
1505 * |-o configurations
1513 * |-compatible = "foo,bar", "bim,bam"
1516 * |-compatible = "foo,bar",
1519 * |-compatible = "bim,bam", "baz,biz"
1521 * Configuration 1 would be picked because the first string in U-Boot's
1522 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1523 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1525 * As an optimization, the compatible property from the FDT's root node can be
1526 * copied into the configuration node in the FIT image. This is required to
1527 * match configurations with compressed FDTs.
1530 * offset to the configuration to use if one was found
1533 int fit_conf_find_compat(const void *fit, const void *fdt)
1536 int noffset, confs_noffset, images_noffset;
1537 const void *fdt_compat;
1539 int best_match_offset = 0;
1540 int best_match_pos = 0;
1542 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1543 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1544 if (confs_noffset < 0 || images_noffset < 0) {
1545 debug("Can't find configurations or images nodes.\n");
1549 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1551 debug("Fdt for comparison has no \"compatible\" property.\n");
1556 * Loop over the configurations in the FIT image.
1558 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1559 (noffset >= 0) && (ndepth > 0);
1560 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1562 const char *kfdt_name;
1563 int kfdt_noffset, compat_noffset;
1564 const char *cur_fdt_compat;
1572 /* If there's a compat property in the config node, use that. */
1573 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1574 fdt = fit; /* search in FIT image */
1575 compat_noffset = noffset; /* search under config node */
1576 } else { /* Otherwise extract it from the kernel FDT. */
1577 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1579 debug("No fdt property found.\n");
1582 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1584 if (kfdt_noffset < 0) {
1585 debug("No image node named \"%s\" found.\n",
1590 if (!fit_image_check_comp(fit, kfdt_noffset,
1592 debug("Can't extract compat from \"%s\" "
1593 "(compressed)\n", kfdt_name);
1597 /* search in this config's kernel FDT */
1598 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1599 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1603 compat_noffset = 0; /* search kFDT under root node */
1606 len = fdt_compat_len;
1607 cur_fdt_compat = fdt_compat;
1609 * Look for a match for each U-Boot compatibility string in
1610 * turn in the compat string property.
1612 for (i = 0; len > 0 &&
1613 (!best_match_offset || best_match_pos > i); i++) {
1614 int cur_len = strlen(cur_fdt_compat) + 1;
1616 if (!fdt_node_check_compatible(fdt, compat_noffset,
1618 best_match_offset = noffset;
1623 cur_fdt_compat += cur_len;
1626 if (!best_match_offset) {
1627 debug("No match found.\n");
1631 return best_match_offset;
1635 * fit_conf_get_node - get node offset for configuration of a given unit name
1636 * @fit: pointer to the FIT format image header
1637 * @conf_uname: configuration node unit name
1639 * fit_conf_get_node() finds a configuration (within the '/configurations'
1640 * parent node) of a provided unit name. If configuration is found its node
1641 * offset is returned to the caller.
1643 * When NULL is provided in second argument fit_conf_get_node() will search
1644 * for a default configuration node instead. Default configuration node unit
1645 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1649 * configuration node offset when found (>=0)
1650 * negative number on failure (FDT_ERR_* code)
1652 int fit_conf_get_node(const void *fit, const char *conf_uname)
1654 int noffset, confs_noffset;
1657 char *conf_uname_copy = NULL;
1659 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1660 if (confs_noffset < 0) {
1661 debug("Can't find configurations parent node '%s' (%s)\n",
1662 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1663 return confs_noffset;
1666 if (conf_uname == NULL) {
1667 /* get configuration unit name from the default property */
1668 debug("No configuration specified, trying default...\n");
1669 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1670 FIT_DEFAULT_PROP, &len);
1671 if (conf_uname == NULL) {
1672 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1676 debug("Found default configuration: '%s'\n", conf_uname);
1679 s = strchr(conf_uname, '#');
1681 len = s - conf_uname;
1682 conf_uname_copy = malloc(len + 1);
1683 if (!conf_uname_copy) {
1684 debug("Can't allocate uname copy: '%s'\n",
1688 memcpy(conf_uname_copy, conf_uname, len);
1689 conf_uname_copy[len] = '\0';
1690 conf_uname = conf_uname_copy;
1693 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1695 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1696 conf_uname, fdt_strerror(noffset));
1699 if (conf_uname_copy)
1700 free(conf_uname_copy);
1705 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1706 const char *prop_name)
1708 return fdt_stringlist_count(fit, noffset, prop_name);
1711 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1712 const char *prop_name, int index)
1717 /* get kernel image unit name from configuration kernel property */
1718 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1722 return fit_image_get_node(fit, uname);
1725 int fit_conf_get_prop_node(const void *fit, int noffset,
1726 const char *prop_name)
1728 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1731 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1733 fit_image_print(fit, rd_noffset, " ");
1736 puts(" Verifying Hash Integrity ... ");
1737 if (!fit_image_verify(fit, rd_noffset)) {
1738 puts("Bad Data Hash\n");
1747 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1754 debug("* %s: using config '%s' from image at 0x%08lx\n",
1755 prop_name, images->fit_uname_cfg, addr);
1757 /* Check whether configuration has this property defined */
1758 fit_hdr = map_sysmem(addr, 0);
1759 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1760 if (cfg_noffset < 0) {
1761 debug("* %s: no such config\n", prop_name);
1765 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1767 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1775 * fit_get_image_type_property() - get property name for IH_TYPE_...
1777 * @return the properly name where we expect to find the image in the
1780 static const char *fit_get_image_type_property(int type)
1783 * This is sort-of available in the uimage_type[] table in image.c
1784 * but we don't have access to the short name, and "fdt" is different
1785 * anyway. So let's just keep it here.
1788 case IH_TYPE_FLATDT:
1789 return FIT_FDT_PROP;
1790 case IH_TYPE_KERNEL:
1791 return FIT_KERNEL_PROP;
1792 case IH_TYPE_RAMDISK:
1793 return FIT_RAMDISK_PROP;
1794 case IH_TYPE_X86_SETUP:
1795 return FIT_SETUP_PROP;
1796 case IH_TYPE_LOADABLE:
1797 return FIT_LOADABLE_PROP;
1799 return FIT_FPGA_PROP;
1800 case IH_TYPE_STANDALONE:
1801 return FIT_STANDALONE_PROP;
1807 int fit_image_load(bootm_headers_t *images, ulong addr,
1808 const char **fit_unamep, const char **fit_uname_configp,
1809 int arch, int image_type, int bootstage_id,
1810 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1812 int cfg_noffset, noffset;
1813 const char *fit_uname;
1814 const char *fit_uname_config;
1815 const char *fit_base_uname_config;
1821 ulong load, load_end, data, len;
1826 const char *prop_name;
1829 fit = map_sysmem(addr, 0);
1830 fit_uname = fit_unamep ? *fit_unamep : NULL;
1831 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1832 fit_base_uname_config = NULL;
1833 prop_name = fit_get_image_type_property(image_type);
1834 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1836 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1837 if (!fit_check_format(fit)) {
1838 printf("Bad FIT %s image format!\n", prop_name);
1839 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1842 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1844 /* get FIT component image node offset */
1845 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1846 noffset = fit_image_get_node(fit, fit_uname);
1849 * no image node unit name, try to get config
1850 * node first. If config unit node name is NULL
1851 * fit_conf_get_node() will try to find default config node
1853 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1854 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1855 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1857 cfg_noffset = fit_conf_get_node(fit,
1860 if (cfg_noffset < 0) {
1861 puts("Could not find configuration node\n");
1862 bootstage_error(bootstage_id +
1863 BOOTSTAGE_SUB_NO_UNIT_NAME);
1867 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1868 printf(" Using '%s' configuration\n", fit_base_uname_config);
1869 /* Remember this config */
1870 if (image_type == IH_TYPE_KERNEL)
1871 images->fit_uname_cfg = fit_base_uname_config;
1873 if (IMAGE_ENABLE_VERIFY && images->verify) {
1874 puts(" Verifying Hash Integrity ... ");
1875 if (fit_config_verify(fit, cfg_noffset)) {
1876 puts("Bad Data Hash\n");
1877 bootstage_error(bootstage_id +
1878 BOOTSTAGE_SUB_HASH);
1884 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1886 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1888 fit_uname = fit_get_name(fit, noffset, NULL);
1891 puts("Could not find subimage node\n");
1892 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1896 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1898 ret = fit_image_select(fit, noffset, images->verify);
1900 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1904 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1905 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1906 if (!fit_image_check_target_arch(fit, noffset)) {
1907 puts("Unsupported Architecture\n");
1908 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1914 fit_image_get_arch(fit, noffset, &os_arch);
1915 images->os.arch = os_arch;
1918 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1919 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1920 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1921 (image_type == IH_TYPE_KERNEL &&
1922 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
1924 os_ok = image_type == IH_TYPE_FLATDT ||
1925 image_type == IH_TYPE_FPGA ||
1926 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1927 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
1928 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1931 * If either of the checks fail, we should report an error, but
1932 * if the image type is coming from the "loadables" field, we
1933 * don't care what it is
1935 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
1936 fit_image_get_os(fit, noffset, &os);
1937 printf("No %s %s %s Image\n",
1938 genimg_get_os_name(os),
1939 genimg_get_arch_name(arch),
1940 genimg_get_type_name(image_type));
1941 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1945 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1947 /* get image data address and length */
1948 if (fit_image_get_data_and_size(fit, noffset,
1949 (const void **)&buf, &size)) {
1950 printf("Could not find %s subimage data!\n", prop_name);
1951 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1955 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1956 /* perform any post-processing on the image data */
1957 board_fit_image_post_process(&buf, &size);
1962 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1964 data = map_to_sysmem(buf);
1966 if (load_op == FIT_LOAD_IGNORED) {
1968 } else if (fit_image_get_load(fit, noffset, &load)) {
1969 if (load_op == FIT_LOAD_REQUIRED) {
1970 printf("Can't get %s subimage load address!\n",
1972 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1975 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1976 ulong image_start, image_end;
1979 * move image data to the load address,
1980 * make sure we don't overwrite initial image
1983 image_end = addr + fit_get_size(fit);
1985 load_end = load + len;
1986 if (image_type != IH_TYPE_KERNEL &&
1987 load < image_end && load_end > image_start) {
1988 printf("Error: %s overwritten\n", prop_name);
1992 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1993 prop_name, data, load);
1995 load = data; /* No load address specified */
1998 comp = IH_COMP_NONE;
2000 /* Kernel images get decompressed later in bootm_load_os(). */
2001 if (!fit_image_get_comp(fit, noffset, &comp) &&
2002 comp != IH_COMP_NONE &&
2003 !(image_type == IH_TYPE_KERNEL ||
2004 image_type == IH_TYPE_KERNEL_NOLOAD ||
2005 image_type == IH_TYPE_RAMDISK)) {
2006 ulong max_decomp_len = len * 20;
2008 loadbuf = malloc(max_decomp_len);
2009 load = map_to_sysmem(loadbuf);
2011 loadbuf = map_sysmem(load, max_decomp_len);
2013 if (image_decomp(comp, load, data, image_type,
2014 loadbuf, buf, len, max_decomp_len, &load_end)) {
2015 printf("Error decompressing %s\n", prop_name);
2019 len = load_end - load;
2020 } else if (load != data) {
2021 loadbuf = map_sysmem(load, len);
2022 memcpy(loadbuf, buf, len);
2025 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2026 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2027 " please fix your .its file!\n");
2029 /* verify that image data is a proper FDT blob */
2030 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2031 puts("Subimage data is not a FDT");
2035 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2040 *fit_unamep = (char *)fit_uname;
2041 if (fit_uname_configp)
2042 *fit_uname_configp = (char *)(fit_uname_config ? :
2043 fit_base_uname_config);
2048 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2049 ulong *setup_start, ulong *setup_len)
2056 addr = map_to_sysmem(images->fit_hdr_os);
2057 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2061 ret = fit_image_load(images, addr, NULL, NULL, arch,
2062 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2063 FIT_LOAD_REQUIRED, setup_start, &len);
2069 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2070 const char **fit_unamep, const char **fit_uname_configp,
2071 int arch, ulong *datap, ulong *lenp)
2073 int fdt_noffset, cfg_noffset, count;
2075 const char *fit_uname = NULL;
2076 const char *fit_uname_config = NULL;
2077 char *fit_uname_config_copy = NULL;
2078 char *next_config = NULL;
2080 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2081 ulong image_start, image_end;
2082 ulong ovload, ovlen;
2083 const char *uconfig;
2086 int i, err, noffset, ov_noffset;
2089 fit_uname = fit_unamep ? *fit_unamep : NULL;
2091 if (fit_uname_configp && *fit_uname_configp) {
2092 fit_uname_config_copy = strdup(*fit_uname_configp);
2093 if (!fit_uname_config_copy)
2096 next_config = strchr(fit_uname_config_copy, '#');
2098 *next_config++ = '\0';
2099 if (next_config - 1 > fit_uname_config_copy)
2100 fit_uname_config = fit_uname_config_copy;
2103 fdt_noffset = fit_image_load(images,
2104 addr, &fit_uname, &fit_uname_config,
2105 arch, IH_TYPE_FLATDT,
2106 BOOTSTAGE_ID_FIT_FDT_START,
2107 FIT_LOAD_OPTIONAL, &load, &len);
2109 if (fdt_noffset < 0)
2112 debug("fit_uname=%s, fit_uname_config=%s\n",
2113 fit_uname ? fit_uname : "<NULL>",
2114 fit_uname_config ? fit_uname_config : "<NULL>");
2116 fit = map_sysmem(addr, 0);
2118 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2120 /* single blob, or error just return as well */
2121 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2122 if (count <= 1 && !next_config)
2125 /* we need to apply overlays */
2127 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2129 image_end = addr + fit_get_size(fit);
2130 /* verify that relocation took place by load address not being in fit */
2131 if (load >= image_start && load < image_end) {
2132 /* check is simplified; fit load checks for overlaps */
2133 printf("Overlayed FDT requires relocation\n");
2134 fdt_noffset = -EBADF;
2138 base = map_sysmem(load, len);
2140 /* apply extra configs in FIT first, followed by args */
2141 for (i = 1; ; i++) {
2143 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2145 uname = fit_get_name(fit, noffset, NULL);
2150 uconfig = next_config;
2151 next_config = strchr(next_config, '#');
2153 *next_config++ = '\0';
2157 * fit_image_load() would load the first FDT from the
2158 * extra config only when uconfig is specified.
2159 * Check if the extra config contains multiple FDTs and
2162 cfg_noffset = fit_conf_get_node(fit, uconfig);
2165 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2169 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2171 ov_noffset = fit_image_load(images,
2172 addr, &uname, &uconfig,
2173 arch, IH_TYPE_FLATDT,
2174 BOOTSTAGE_ID_FIT_FDT_START,
2175 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2176 if (ov_noffset < 0) {
2177 printf("load of %s failed\n", uname);
2180 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2181 uname, ovload, ovlen);
2182 ov = map_sysmem(ovload, ovlen);
2184 base = map_sysmem(load, len + ovlen);
2185 err = fdt_open_into(base, base, len + ovlen);
2187 printf("failed on fdt_open_into\n");
2191 /* the verbose method prints out messages on error */
2192 err = fdt_overlay_apply_verbose(base, ov);
2198 len = fdt_totalsize(base);
2201 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2202 fdt_noffset = -EBADF;
2211 *fit_unamep = fit_uname;
2212 if (fit_uname_configp)
2213 *fit_uname_configp = fit_uname_config;
2215 if (fit_uname_config_copy)
2216 free(fit_uname_config_copy);