2 * Copyright (c) 2013, Google Inc.
4 * (C) Copyright 2008 Semihalf
6 * (C) Copyright 2000-2006
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * SPDX-License-Identifier: GPL-2.0+
20 DECLARE_GLOBAL_DATA_PTR;
21 #endif /* !USE_HOSTCC*/
23 #include <bootstage.h>
24 #include <u-boot/crc.h>
25 #include <u-boot/md5.h>
26 #include <u-boot/sha1.h>
27 #include <u-boot/sha256.h>
29 /*****************************************************************************/
30 /* New uImage format routines */
31 /*****************************************************************************/
33 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
34 ulong *addr, const char **name)
41 sep = strchr(spec, sepc);
44 *addr = simple_strtoul(spec, NULL, 16);
54 * fit_parse_conf - parse FIT configuration spec
55 * @spec: input string, containing configuration spec
56 * @add_curr: current image address (to be used as a possible default)
57 * @addr: pointer to a ulong variable, will hold FIT image address of a given
59 * @conf_name double pointer to a char, will hold pointer to a configuration
62 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
63 * where <addr> is a FIT image address that contains configuration
64 * with a <conf> unit name.
66 * Address part is optional, and if omitted default add_curr will
70 * 1 if spec is a valid configuration string,
71 * addr and conf_name are set accordingly
74 int fit_parse_conf(const char *spec, ulong addr_curr,
75 ulong *addr, const char **conf_name)
77 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
81 * fit_parse_subimage - parse FIT subimage spec
82 * @spec: input string, containing subimage spec
83 * @add_curr: current image address (to be used as a possible default)
84 * @addr: pointer to a ulong variable, will hold FIT image address of a given
86 * @image_name: double pointer to a char, will hold pointer to a subimage name
88 * fit_parse_subimage() expects subimage spec in the form of
89 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
90 * subimage with a <subimg> unit name.
92 * Address part is optional, and if omitted default add_curr will
96 * 1 if spec is a valid subimage string,
97 * addr and image_name are set accordingly
100 int fit_parse_subimage(const char *spec, ulong addr_curr,
101 ulong *addr, const char **image_name)
103 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
105 #endif /* !USE_HOSTCC */
107 static void fit_get_debug(const void *fit, int noffset,
108 char *prop_name, int err)
110 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
111 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
115 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
117 * fit_print_contents - prints out the contents of the FIT format image
118 * @fit: pointer to the FIT format image header
119 * @p: pointer to prefix string
121 * fit_print_contents() formats a multi line FIT image contents description.
122 * The routine prints out FIT image properties (root node level) follwed by
123 * the details of each component image.
126 * no returned results
128 void fit_print_contents(const void *fit)
141 /* Indent string is defined in header image.h */
142 p = IMAGE_INDENT_STRING;
144 /* Root node properties */
145 ret = fit_get_desc(fit, 0, &desc);
146 printf("%sFIT description: ", p);
148 printf("unavailable\n");
150 printf("%s\n", desc);
152 if (IMAGE_ENABLE_TIMESTAMP) {
153 ret = fit_get_timestamp(fit, 0, ×tamp);
154 printf("%sCreated: ", p);
156 printf("unavailable\n");
158 genimg_print_time(timestamp);
161 /* Find images parent node offset */
162 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
163 if (images_noffset < 0) {
164 printf("Can't find images parent node '%s' (%s)\n",
165 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
169 /* Process its subnodes, print out component images details */
170 for (ndepth = 0, count = 0,
171 noffset = fdt_next_node(fit, images_noffset, &ndepth);
172 (noffset >= 0) && (ndepth > 0);
173 noffset = fdt_next_node(fit, noffset, &ndepth)) {
176 * Direct child node of the images parent node,
177 * i.e. component image node.
179 printf("%s Image %u (%s)\n", p, count++,
180 fit_get_name(fit, noffset, NULL));
182 fit_image_print(fit, noffset, p);
186 /* Find configurations parent node offset */
187 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
188 if (confs_noffset < 0) {
189 debug("Can't get configurations parent node '%s' (%s)\n",
190 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
194 /* get default configuration unit name from default property */
195 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
197 printf("%s Default Configuration: '%s'\n", p, uname);
199 /* Process its subnodes, print out configurations details */
200 for (ndepth = 0, count = 0,
201 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
202 (noffset >= 0) && (ndepth > 0);
203 noffset = fdt_next_node(fit, noffset, &ndepth)) {
206 * Direct child node of the configurations parent node,
207 * i.e. configuration node.
209 printf("%s Configuration %u (%s)\n", p, count++,
210 fit_get_name(fit, noffset, NULL));
212 fit_conf_print(fit, noffset, p);
218 * fit_image_print_data() - prints out the hash node details
219 * @fit: pointer to the FIT format image header
220 * @noffset: offset of the hash node
221 * @p: pointer to prefix string
222 * @type: Type of information to print ("hash" or "sign")
224 * fit_image_print_data() lists properies for the processed hash node
226 * This function avoid using puts() since it prints a newline on the host
227 * but does not in U-Boot.
230 * no returned results
232 static void fit_image_print_data(const void *fit, int noffset, const char *p,
242 debug("%s %s node: '%s'\n", p, type,
243 fit_get_name(fit, noffset, NULL));
244 printf("%s %s algo: ", p, type);
245 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
246 printf("invalid/unsupported\n");
250 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
251 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
253 printf(":%s", keyname);
255 printf(" (required)");
258 ret = fit_image_hash_get_value(fit, noffset, &value,
260 printf("%s %s value: ", p, type);
262 printf("unavailable\n");
264 for (i = 0; i < value_len; i++)
265 printf("%02x", value[i]);
269 debug("%s %s len: %d\n", p, type, value_len);
271 /* Signatures have a time stamp */
272 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
275 printf("%s Timestamp: ", p);
276 if (fit_get_timestamp(fit, noffset, ×tamp))
277 printf("unavailable\n");
279 genimg_print_time(timestamp);
284 * fit_image_print_verification_data() - prints out the hash/signature details
285 * @fit: pointer to the FIT format image header
286 * @noffset: offset of the hash or signature node
287 * @p: pointer to prefix string
289 * This lists properies for the processed hash node
292 * no returned results
294 static void fit_image_print_verification_data(const void *fit, int noffset,
300 * Check subnode name, must be equal to "hash" or "signature".
301 * Multiple hash/signature nodes require unique unit node
302 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
304 name = fit_get_name(fit, noffset, NULL);
305 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
306 fit_image_print_data(fit, noffset, p, "Hash");
307 } else if (!strncmp(name, FIT_SIG_NODENAME,
308 strlen(FIT_SIG_NODENAME))) {
309 fit_image_print_data(fit, noffset, p, "Sign");
314 * fit_image_print - prints out the FIT component image details
315 * @fit: pointer to the FIT format image header
316 * @image_noffset: offset of the component image node
317 * @p: pointer to prefix string
319 * fit_image_print() lists all mandatory properies for the processed component
320 * image. If present, hash nodes are printed out as well. Load
321 * address for images of type firmware is also printed out. Since the load
322 * address is not mandatory for firmware images, it will be output as
323 * "unavailable" when not present.
326 * no returned results
328 void fit_image_print(const void *fit, int image_noffset, const char *p)
331 uint8_t type, arch, os, comp;
339 /* Mandatory properties */
340 ret = fit_get_desc(fit, image_noffset, &desc);
341 printf("%s Description: ", p);
343 printf("unavailable\n");
345 printf("%s\n", desc);
347 if (IMAGE_ENABLE_TIMESTAMP) {
350 ret = fit_get_timestamp(fit, 0, ×tamp);
351 printf("%s Created: ", p);
353 printf("unavailable\n");
355 genimg_print_time(timestamp);
358 fit_image_get_type(fit, image_noffset, &type);
359 printf("%s Type: %s\n", p, genimg_get_type_name(type));
361 fit_image_get_comp(fit, image_noffset, &comp);
362 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
364 ret = fit_image_get_data(fit, image_noffset, &data, &size);
367 printf("%s Data Start: ", p);
369 printf("unavailable\n");
371 void *vdata = (void *)data;
373 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
377 printf("%s Data Size: ", p);
379 printf("unavailable\n");
381 genimg_print_size(size);
383 /* Remaining, type dependent properties */
384 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
385 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
386 (type == IH_TYPE_FLATDT)) {
387 fit_image_get_arch(fit, image_noffset, &arch);
388 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
391 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
392 fit_image_get_os(fit, image_noffset, &os);
393 printf("%s OS: %s\n", p, genimg_get_os_name(os));
396 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
397 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
398 ret = fit_image_get_load(fit, image_noffset, &load);
399 printf("%s Load Address: ", p);
401 printf("unavailable\n");
403 printf("0x%08lx\n", load);
406 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
407 (type == IH_TYPE_RAMDISK)) {
408 fit_image_get_entry(fit, image_noffset, &entry);
409 printf("%s Entry Point: ", p);
411 printf("unavailable\n");
413 printf("0x%08lx\n", entry);
416 /* Process all hash subnodes of the component image node */
417 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
418 (noffset >= 0) && (ndepth > 0);
419 noffset = fdt_next_node(fit, noffset, &ndepth)) {
421 /* Direct child node of the component image node */
422 fit_image_print_verification_data(fit, noffset, p);
429 * fit_get_desc - get node description property
430 * @fit: pointer to the FIT format image header
431 * @noffset: node offset
432 * @desc: double pointer to the char, will hold pointer to the descrption
434 * fit_get_desc() reads description property from a given node, if
435 * description is found pointer to it is returened in third call argument.
441 int fit_get_desc(const void *fit, int noffset, char **desc)
445 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
447 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
455 * fit_get_timestamp - get node timestamp property
456 * @fit: pointer to the FIT format image header
457 * @noffset: node offset
458 * @timestamp: pointer to the time_t, will hold read timestamp
460 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
461 * is found and has a correct size its value is retured in third call
466 * -1, on property read failure
467 * -2, on wrong timestamp size
469 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
474 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
476 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
479 if (len != sizeof(uint32_t)) {
480 debug("FIT timestamp with incorrect size of (%u)\n", len);
484 *timestamp = uimage_to_cpu(*((uint32_t *)data));
489 * fit_image_get_node - get node offset for component image of a given unit name
490 * @fit: pointer to the FIT format image header
491 * @image_uname: component image node unit name
493 * fit_image_get_node() finds a component image (withing the '/images'
494 * node) of a provided unit name. If image is found its node offset is
495 * returned to the caller.
498 * image node offset when found (>=0)
499 * negative number on failure (FDT_ERR_* code)
501 int fit_image_get_node(const void *fit, const char *image_uname)
503 int noffset, images_noffset;
505 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
506 if (images_noffset < 0) {
507 debug("Can't find images parent node '%s' (%s)\n",
508 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
509 return images_noffset;
512 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
514 debug("Can't get node offset for image unit name: '%s' (%s)\n",
515 image_uname, fdt_strerror(noffset));
522 * fit_image_get_os - get os id for a given component image node
523 * @fit: pointer to the FIT format image header
524 * @noffset: component image node offset
525 * @os: pointer to the uint8_t, will hold os numeric id
527 * fit_image_get_os() finds os property in a given component image node.
528 * If the property is found, its (string) value is translated to the numeric
529 * id which is returned to the caller.
535 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
540 /* Get OS name from property data */
541 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
543 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
548 /* Translate OS name to id */
549 *os = genimg_get_os_id(data);
554 * fit_image_get_arch - get arch id for a given component image node
555 * @fit: pointer to the FIT format image header
556 * @noffset: component image node offset
557 * @arch: pointer to the uint8_t, will hold arch numeric id
559 * fit_image_get_arch() finds arch property in a given component image node.
560 * If the property is found, its (string) value is translated to the numeric
561 * id which is returned to the caller.
567 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
572 /* Get architecture name from property data */
573 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
575 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
580 /* Translate architecture name to id */
581 *arch = genimg_get_arch_id(data);
586 * fit_image_get_type - get type id for a given component image node
587 * @fit: pointer to the FIT format image header
588 * @noffset: component image node offset
589 * @type: pointer to the uint8_t, will hold type numeric id
591 * fit_image_get_type() finds type property in a given component image node.
592 * If the property is found, its (string) value is translated to the numeric
593 * id which is returned to the caller.
599 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
604 /* Get image type name from property data */
605 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
607 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
612 /* Translate image type name to id */
613 *type = genimg_get_type_id(data);
618 * fit_image_get_comp - get comp id for a given component image node
619 * @fit: pointer to the FIT format image header
620 * @noffset: component image node offset
621 * @comp: pointer to the uint8_t, will hold comp numeric id
623 * fit_image_get_comp() finds comp property in a given component image node.
624 * If the property is found, its (string) value is translated to the numeric
625 * id which is returned to the caller.
631 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
636 /* Get compression name from property data */
637 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
639 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
644 /* Translate compression name to id */
645 *comp = genimg_get_comp_id(data);
650 * fit_image_get_load() - get load addr property for given component image node
651 * @fit: pointer to the FIT format image header
652 * @noffset: component image node offset
653 * @load: pointer to the uint32_t, will hold load address
655 * fit_image_get_load() finds load address property in a given component
656 * image node. If the property is found, its value is returned to the caller.
662 int fit_image_get_load(const void *fit, int noffset, ulong *load)
665 const uint32_t *data;
667 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
669 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
673 *load = uimage_to_cpu(*data);
678 * fit_image_get_entry() - get entry point address property
679 * @fit: pointer to the FIT format image header
680 * @noffset: component image node offset
681 * @entry: pointer to the uint32_t, will hold entry point address
683 * This gets the entry point address property for a given component image
686 * fit_image_get_entry() finds entry point address property in a given
687 * component image node. If the property is found, its value is returned
694 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
697 const uint32_t *data;
699 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
701 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
705 *entry = uimage_to_cpu(*data);
710 * fit_image_get_data - get data property and its size for a given component image node
711 * @fit: pointer to the FIT format image header
712 * @noffset: component image node offset
713 * @data: double pointer to void, will hold data property's data address
714 * @size: pointer to size_t, will hold data property's data size
716 * fit_image_get_data() finds data property in a given component image node.
717 * If the property is found its data start address and size are returned to
724 int fit_image_get_data(const void *fit, int noffset,
725 const void **data, size_t *size)
729 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
731 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
741 * fit_image_hash_get_algo - get hash algorithm name
742 * @fit: pointer to the FIT format image header
743 * @noffset: hash node offset
744 * @algo: double pointer to char, will hold pointer to the algorithm name
746 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
747 * If the property is found its data start address is returned to the caller.
753 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
757 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
759 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
767 * fit_image_hash_get_value - get hash value and length
768 * @fit: pointer to the FIT format image header
769 * @noffset: hash node offset
770 * @value: double pointer to uint8_t, will hold address of a hash value data
771 * @value_len: pointer to an int, will hold hash data length
773 * fit_image_hash_get_value() finds hash value property in a given hash node.
774 * If the property is found its data start address and size are returned to
781 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
786 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
787 if (*value == NULL) {
788 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
798 * fit_image_hash_get_ignore - get hash ignore flag
799 * @fit: pointer to the FIT format image header
800 * @noffset: hash node offset
801 * @ignore: pointer to an int, will hold hash ignore flag
803 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
804 * If the property is found and non-zero, the hash algorithm is not verified by
805 * u-boot automatically.
808 * 0, on ignore not found
809 * value, on ignore found
811 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
816 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
817 if (value == NULL || len != sizeof(int))
826 * fit_set_timestamp - set node timestamp property
827 * @fit: pointer to the FIT format image header
828 * @noffset: node offset
829 * @timestamp: timestamp value to be set
831 * fit_set_timestamp() attempts to set timestamp property in the requested
832 * node and returns operation status to the caller.
836 * -ENOSPC if no space in device tree, -1 for other error
838 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
843 t = cpu_to_uimage(timestamp);
844 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
847 printf("Can't set '%s' property for '%s' node (%s)\n",
848 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
850 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
857 * calculate_hash - calculate and return hash for provided input data
858 * @data: pointer to the input data
859 * @data_len: data length
860 * @algo: requested hash algorithm
861 * @value: pointer to the char, will hold hash value data (caller must
862 * allocate enough free space)
863 * value_len: length of the calculated hash
865 * calculate_hash() computes input data hash according to the requested
867 * Resulting hash value is placed in caller provided 'value' buffer, length
868 * of the calculated hash is returned via value_len pointer argument.
872 * -1, when algo is unsupported
874 int calculate_hash(const void *data, int data_len, const char *algo,
875 uint8_t *value, int *value_len)
877 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
878 *((uint32_t *)value) = crc32_wd(0, data, data_len,
880 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
882 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
883 sha1_csum_wd((unsigned char *)data, data_len,
884 (unsigned char *)value, CHUNKSZ_SHA1);
886 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
887 sha256_csum_wd((unsigned char *)data, data_len,
888 (unsigned char *)value, CHUNKSZ_SHA256);
889 *value_len = SHA256_SUM_LEN;
890 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
891 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
894 debug("Unsupported hash alogrithm\n");
900 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
901 size_t size, char **err_msgp)
903 uint8_t value[FIT_MAX_HASH_LEN];
912 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
913 *err_msgp = "Can't get hash algo property";
918 if (IMAGE_ENABLE_IGNORE) {
919 fit_image_hash_get_ignore(fit, noffset, &ignore);
926 if (fit_image_hash_get_value(fit, noffset, &fit_value,
928 *err_msgp = "Can't get hash value property";
932 if (calculate_hash(data, size, algo, value, &value_len)) {
933 *err_msgp = "Unsupported hash algorithm";
937 if (value_len != fit_value_len) {
938 *err_msgp = "Bad hash value len";
940 } else if (memcmp(value, fit_value, value_len) != 0) {
941 *err_msgp = "Bad hash value";
949 * fit_image_verify - verify data intergity
950 * @fit: pointer to the FIT format image header
951 * @image_noffset: component image node offset
953 * fit_image_verify() goes over component image hash nodes,
954 * re-calculates each data hash and compares with the value stored in hash
958 * 1, if all hashes are valid
959 * 0, otherwise (or on error)
961 int fit_image_verify(const void *fit, int image_noffset)
970 /* Get image data and data length */
971 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
972 err_msg = "Can't get image data/size";
976 /* Verify all required signatures */
977 if (IMAGE_ENABLE_VERIFY &&
978 fit_image_verify_required_sigs(fit, image_noffset, data, size,
979 gd_fdt_blob(), &verify_all)) {
980 err_msg = "Unable to verify required signature";
984 /* Process all hash subnodes of the component image node */
985 for (noffset = fdt_first_subnode(fit, image_noffset);
987 noffset = fdt_next_subnode(fit, noffset)) {
988 const char *name = fit_get_name(fit, noffset, NULL);
991 * Check subnode name, must be equal to "hash".
992 * Multiple hash nodes require unique unit node
993 * names, e.g. hash@1, hash@2, etc.
995 if (!strncmp(name, FIT_HASH_NODENAME,
996 strlen(FIT_HASH_NODENAME))) {
997 if (fit_image_check_hash(fit, noffset, data, size,
1001 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1002 !strncmp(name, FIT_SIG_NODENAME,
1003 strlen(FIT_SIG_NODENAME))) {
1004 ret = fit_image_check_sig(fit, noffset, data,
1005 size, -1, &err_msg);
1013 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1014 err_msg = "Corrupted or truncated tree";
1021 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1022 err_msg, fit_get_name(fit, noffset, NULL),
1023 fit_get_name(fit, image_noffset, NULL));
1028 * fit_all_image_verify - verify data intergity for all images
1029 * @fit: pointer to the FIT format image header
1031 * fit_all_image_verify() goes over all images in the FIT and
1032 * for every images checks if all it's hashes are valid.
1035 * 1, if all hashes of all images are valid
1036 * 0, otherwise (or on error)
1038 int fit_all_image_verify(const void *fit)
1045 /* Find images parent node offset */
1046 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1047 if (images_noffset < 0) {
1048 printf("Can't find images parent node '%s' (%s)\n",
1049 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1053 /* Process all image subnodes, check hashes for each */
1054 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1056 for (ndepth = 0, count = 0,
1057 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1058 (noffset >= 0) && (ndepth > 0);
1059 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1062 * Direct child node of the images parent node,
1063 * i.e. component image node.
1065 printf(" Hash(es) for Image %u (%s): ", count++,
1066 fit_get_name(fit, noffset, NULL));
1068 if (!fit_image_verify(fit, noffset))
1077 * fit_image_check_os - check whether image node is of a given os type
1078 * @fit: pointer to the FIT format image header
1079 * @noffset: component image node offset
1080 * @os: requested image os
1082 * fit_image_check_os() reads image os property and compares its numeric
1083 * id with the requested os. Comparison result is returned to the caller.
1086 * 1 if image is of given os type
1087 * 0 otherwise (or on error)
1089 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1093 if (fit_image_get_os(fit, noffset, &image_os))
1095 return (os == image_os);
1099 * fit_image_check_arch - check whether image node is of a given arch
1100 * @fit: pointer to the FIT format image header
1101 * @noffset: component image node offset
1102 * @arch: requested imagearch
1104 * fit_image_check_arch() reads image arch property and compares its numeric
1105 * id with the requested arch. Comparison result is returned to the caller.
1108 * 1 if image is of given arch
1109 * 0 otherwise (or on error)
1111 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1115 if (fit_image_get_arch(fit, noffset, &image_arch))
1117 return (arch == image_arch) ||
1118 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64);
1122 * fit_image_check_type - check whether image node is of a given type
1123 * @fit: pointer to the FIT format image header
1124 * @noffset: component image node offset
1125 * @type: requested image type
1127 * fit_image_check_type() reads image type property and compares its numeric
1128 * id with the requested type. Comparison result is returned to the caller.
1131 * 1 if image is of given type
1132 * 0 otherwise (or on error)
1134 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1138 if (fit_image_get_type(fit, noffset, &image_type))
1140 return (type == image_type);
1144 * fit_image_check_comp - check whether image node uses given compression
1145 * @fit: pointer to the FIT format image header
1146 * @noffset: component image node offset
1147 * @comp: requested image compression type
1149 * fit_image_check_comp() reads image compression property and compares its
1150 * numeric id with the requested compression type. Comparison result is
1151 * returned to the caller.
1154 * 1 if image uses requested compression
1155 * 0 otherwise (or on error)
1157 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1161 if (fit_image_get_comp(fit, noffset, &image_comp))
1163 return (comp == image_comp);
1167 * fit_check_format - sanity check FIT image format
1168 * @fit: pointer to the FIT format image header
1170 * fit_check_format() runs a basic sanity FIT image verification.
1171 * Routine checks for mandatory properties, nodes, etc.
1177 int fit_check_format(const void *fit)
1179 /* mandatory / node 'description' property */
1180 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1181 debug("Wrong FIT format: no description\n");
1185 if (IMAGE_ENABLE_TIMESTAMP) {
1186 /* mandatory / node 'timestamp' property */
1187 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1188 debug("Wrong FIT format: no timestamp\n");
1193 /* mandatory subimages parent '/images' node */
1194 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1195 debug("Wrong FIT format: no images parent node\n");
1204 * fit_conf_find_compat
1205 * @fit: pointer to the FIT format image header
1206 * @fdt: pointer to the device tree to compare against
1208 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1209 * most compatible with the passed in device tree.
1218 * |-o configurations
1226 * |-compatible = "foo,bar", "bim,bam"
1229 * |-compatible = "foo,bar",
1232 * |-compatible = "bim,bam", "baz,biz"
1234 * Configuration 1 would be picked because the first string in U-Boot's
1235 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1236 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1239 * offset to the configuration to use if one was found
1242 int fit_conf_find_compat(const void *fit, const void *fdt)
1245 int noffset, confs_noffset, images_noffset;
1246 const void *fdt_compat;
1248 int best_match_offset = 0;
1249 int best_match_pos = 0;
1251 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1252 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1253 if (confs_noffset < 0 || images_noffset < 0) {
1254 debug("Can't find configurations or images nodes.\n");
1258 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1260 debug("Fdt for comparison has no \"compatible\" property.\n");
1265 * Loop over the configurations in the FIT image.
1267 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1268 (noffset >= 0) && (ndepth > 0);
1269 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1271 const char *kfdt_name;
1273 const char *cur_fdt_compat;
1281 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1283 debug("No fdt property found.\n");
1286 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1288 if (kfdt_noffset < 0) {
1289 debug("No image node named \"%s\" found.\n",
1294 * Get a pointer to this configuration's fdt.
1296 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1297 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1301 len = fdt_compat_len;
1302 cur_fdt_compat = fdt_compat;
1304 * Look for a match for each U-Boot compatibility string in
1305 * turn in this configuration's fdt.
1307 for (i = 0; len > 0 &&
1308 (!best_match_offset || best_match_pos > i); i++) {
1309 int cur_len = strlen(cur_fdt_compat) + 1;
1311 if (!fdt_node_check_compatible(kfdt, 0,
1313 best_match_offset = noffset;
1318 cur_fdt_compat += cur_len;
1321 if (!best_match_offset) {
1322 debug("No match found.\n");
1326 return best_match_offset;
1330 * fit_conf_get_node - get node offset for configuration of a given unit name
1331 * @fit: pointer to the FIT format image header
1332 * @conf_uname: configuration node unit name
1334 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1335 * parant node) of a provided unit name. If configuration is found its node
1336 * offset is returned to the caller.
1338 * When NULL is provided in second argument fit_conf_get_node() will search
1339 * for a default configuration node instead. Default configuration node unit
1340 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1344 * configuration node offset when found (>=0)
1345 * negative number on failure (FDT_ERR_* code)
1347 int fit_conf_get_node(const void *fit, const char *conf_uname)
1349 int noffset, confs_noffset;
1352 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1353 if (confs_noffset < 0) {
1354 debug("Can't find configurations parent node '%s' (%s)\n",
1355 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1356 return confs_noffset;
1359 if (conf_uname == NULL) {
1360 /* get configuration unit name from the default property */
1361 debug("No configuration specified, trying default...\n");
1362 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1363 FIT_DEFAULT_PROP, &len);
1364 if (conf_uname == NULL) {
1365 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1369 debug("Found default configuration: '%s'\n", conf_uname);
1372 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1374 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1375 conf_uname, fdt_strerror(noffset));
1381 int fit_conf_get_prop_node(const void *fit, int noffset,
1382 const char *prop_name)
1387 /* get kernel image unit name from configuration kernel property */
1388 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1392 return fit_image_get_node(fit, uname);
1396 * fit_conf_print - prints out the FIT configuration details
1397 * @fit: pointer to the FIT format image header
1398 * @noffset: offset of the configuration node
1399 * @p: pointer to prefix string
1401 * fit_conf_print() lists all mandatory properies for the processed
1402 * configuration node.
1405 * no returned results
1407 void fit_conf_print(const void *fit, int noffset, const char *p)
1413 /* Mandatory properties */
1414 ret = fit_get_desc(fit, noffset, &desc);
1415 printf("%s Description: ", p);
1417 printf("unavailable\n");
1419 printf("%s\n", desc);
1421 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1422 printf("%s Kernel: ", p);
1424 printf("unavailable\n");
1426 printf("%s\n", uname);
1428 /* Optional properties */
1429 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1431 printf("%s Init Ramdisk: %s\n", p, uname);
1433 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1435 printf("%s FDT: %s\n", p, uname);
1438 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1440 fit_image_print(fit, rd_noffset, " ");
1443 puts(" Verifying Hash Integrity ... ");
1444 if (!fit_image_verify(fit, rd_noffset)) {
1445 puts("Bad Data Hash\n");
1454 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1461 debug("* %s: using config '%s' from image at 0x%08lx\n",
1462 prop_name, images->fit_uname_cfg, addr);
1464 /* Check whether configuration has this property defined */
1465 fit_hdr = map_sysmem(addr, 0);
1466 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1467 if (cfg_noffset < 0) {
1468 debug("* %s: no such config\n", prop_name);
1472 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1474 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1482 * fit_get_image_type_property() - get property name for IH_TYPE_...
1484 * @return the properly name where we expect to find the image in the
1487 static const char *fit_get_image_type_property(int type)
1490 * This is sort-of available in the uimage_type[] table in image.c
1491 * but we don't have access to the sohrt name, and "fdt" is different
1492 * anyway. So let's just keep it here.
1495 case IH_TYPE_FLATDT:
1496 return FIT_FDT_PROP;
1497 case IH_TYPE_KERNEL:
1498 return FIT_KERNEL_PROP;
1499 case IH_TYPE_RAMDISK:
1500 return FIT_RAMDISK_PROP;
1501 case IH_TYPE_X86_SETUP:
1502 return FIT_SETUP_PROP;
1508 int fit_image_load(bootm_headers_t *images, ulong addr,
1509 const char **fit_unamep, const char **fit_uname_configp,
1510 int arch, int image_type, int bootstage_id,
1511 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1513 int cfg_noffset, noffset;
1514 const char *fit_uname;
1515 const char *fit_uname_config;
1520 ulong load, data, len;
1522 const char *prop_name;
1525 fit = map_sysmem(addr, 0);
1526 fit_uname = fit_unamep ? *fit_unamep : NULL;
1527 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1528 prop_name = fit_get_image_type_property(image_type);
1529 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1531 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1532 if (!fit_check_format(fit)) {
1533 printf("Bad FIT %s image format!\n", prop_name);
1534 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1537 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1539 /* get FIT component image node offset */
1540 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1541 noffset = fit_image_get_node(fit, fit_uname);
1544 * no image node unit name, try to get config
1545 * node first. If config unit node name is NULL
1546 * fit_conf_get_node() will try to find default config node
1548 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1549 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1550 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1552 cfg_noffset = fit_conf_get_node(fit,
1555 if (cfg_noffset < 0) {
1556 puts("Could not find configuration node\n");
1557 bootstage_error(bootstage_id +
1558 BOOTSTAGE_SUB_NO_UNIT_NAME);
1561 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1562 printf(" Using '%s' configuration\n", fit_uname_config);
1563 if (image_type == IH_TYPE_KERNEL) {
1564 /* Remember (and possibly verify) this config */
1565 images->fit_uname_cfg = fit_uname_config;
1566 if (IMAGE_ENABLE_VERIFY && images->verify) {
1567 puts(" Verifying Hash Integrity ... ");
1568 if (fit_config_verify(fit, cfg_noffset)) {
1569 puts("Bad Data Hash\n");
1570 bootstage_error(bootstage_id +
1571 BOOTSTAGE_SUB_HASH);
1576 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1579 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1581 fit_uname = fit_get_name(fit, noffset, NULL);
1584 puts("Could not find subimage node\n");
1585 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1589 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1591 ret = fit_image_select(fit, noffset, images->verify);
1593 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1597 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1598 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
1599 if (!fit_image_check_target_arch(fit, noffset)) {
1600 puts("Unsupported Architecture\n");
1601 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1605 if (image_type == IH_TYPE_FLATDT &&
1606 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1607 puts("FDT image is compressed");
1608 return -EPROTONOSUPPORT;
1611 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1612 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1613 (image_type == IH_TYPE_KERNEL &&
1614 fit_image_check_type(fit, noffset,
1615 IH_TYPE_KERNEL_NOLOAD));
1617 os_ok = image_type == IH_TYPE_FLATDT ||
1618 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1619 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
1620 if (!type_ok || !os_ok) {
1621 fit_image_get_os(fit, noffset, &os);
1622 printf("No %s %s %s Image\n",
1623 genimg_get_os_name(os),
1624 genimg_get_arch_name(arch),
1625 genimg_get_type_name(image_type));
1626 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1630 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1632 /* get image data address and length */
1633 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1634 printf("Could not find %s subimage data!\n", prop_name);
1635 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1640 /* verify that image data is a proper FDT blob */
1641 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1642 puts("Subimage data is not a FDT");
1646 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1649 * Work-around for eldk-4.2 which gives this warning if we try to
1650 * cast in the unmap_sysmem() call:
1651 * warning: initialization discards qualifiers from pointer target type
1654 void *vbuf = (void *)buf;
1656 data = map_to_sysmem(vbuf);
1659 if (load_op == FIT_LOAD_IGNORED) {
1661 } else if (fit_image_get_load(fit, noffset, &load)) {
1662 if (load_op == FIT_LOAD_REQUIRED) {
1663 printf("Can't get %s subimage load address!\n",
1665 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1668 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
1669 ulong image_start, image_end;
1674 * move image data to the load address,
1675 * make sure we don't overwrite initial image
1678 image_end = addr + fit_get_size(fit);
1680 load_end = load + len;
1681 if (image_type != IH_TYPE_KERNEL &&
1682 load < image_end && load_end > image_start) {
1683 printf("Error: %s overwritten\n", prop_name);
1687 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1688 prop_name, data, load);
1690 dst = map_sysmem(load, len);
1691 memmove(dst, buf, len);
1694 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1699 *fit_unamep = (char *)fit_uname;
1700 if (fit_uname_configp)
1701 *fit_uname_configp = (char *)fit_uname_config;
1706 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1707 ulong *setup_start, ulong *setup_len)
1714 addr = map_to_sysmem(images->fit_hdr_os);
1715 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1719 ret = fit_image_load(images, addr, NULL, NULL, arch,
1720 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1721 FIT_LOAD_REQUIRED, setup_start, &len);