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);
1121 * fit_image_check_type - check whether image node is of a given type
1122 * @fit: pointer to the FIT format image header
1123 * @noffset: component image node offset
1124 * @type: requested image type
1126 * fit_image_check_type() reads image type property and compares its numeric
1127 * id with the requested type. Comparison result is returned to the caller.
1130 * 1 if image is of given type
1131 * 0 otherwise (or on error)
1133 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1137 if (fit_image_get_type(fit, noffset, &image_type))
1139 return (type == image_type);
1143 * fit_image_check_comp - check whether image node uses given compression
1144 * @fit: pointer to the FIT format image header
1145 * @noffset: component image node offset
1146 * @comp: requested image compression type
1148 * fit_image_check_comp() reads image compression property and compares its
1149 * numeric id with the requested compression type. Comparison result is
1150 * returned to the caller.
1153 * 1 if image uses requested compression
1154 * 0 otherwise (or on error)
1156 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1160 if (fit_image_get_comp(fit, noffset, &image_comp))
1162 return (comp == image_comp);
1166 * fit_check_format - sanity check FIT image format
1167 * @fit: pointer to the FIT format image header
1169 * fit_check_format() runs a basic sanity FIT image verification.
1170 * Routine checks for mandatory properties, nodes, etc.
1176 int fit_check_format(const void *fit)
1178 /* mandatory / node 'description' property */
1179 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1180 debug("Wrong FIT format: no description\n");
1184 if (IMAGE_ENABLE_TIMESTAMP) {
1185 /* mandatory / node 'timestamp' property */
1186 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1187 debug("Wrong FIT format: no timestamp\n");
1192 /* mandatory subimages parent '/images' node */
1193 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1194 debug("Wrong FIT format: no images parent node\n");
1203 * fit_conf_find_compat
1204 * @fit: pointer to the FIT format image header
1205 * @fdt: pointer to the device tree to compare against
1207 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1208 * most compatible with the passed in device tree.
1217 * |-o configurations
1225 * |-compatible = "foo,bar", "bim,bam"
1228 * |-compatible = "foo,bar",
1231 * |-compatible = "bim,bam", "baz,biz"
1233 * Configuration 1 would be picked because the first string in U-Boot's
1234 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1235 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1238 * offset to the configuration to use if one was found
1241 int fit_conf_find_compat(const void *fit, const void *fdt)
1244 int noffset, confs_noffset, images_noffset;
1245 const void *fdt_compat;
1247 int best_match_offset = 0;
1248 int best_match_pos = 0;
1250 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1251 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1252 if (confs_noffset < 0 || images_noffset < 0) {
1253 debug("Can't find configurations or images nodes.\n");
1257 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1259 debug("Fdt for comparison has no \"compatible\" property.\n");
1264 * Loop over the configurations in the FIT image.
1266 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1267 (noffset >= 0) && (ndepth > 0);
1268 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1270 const char *kfdt_name;
1272 const char *cur_fdt_compat;
1280 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1282 debug("No fdt property found.\n");
1285 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1287 if (kfdt_noffset < 0) {
1288 debug("No image node named \"%s\" found.\n",
1293 * Get a pointer to this configuration's fdt.
1295 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1296 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1300 len = fdt_compat_len;
1301 cur_fdt_compat = fdt_compat;
1303 * Look for a match for each U-Boot compatibility string in
1304 * turn in this configuration's fdt.
1306 for (i = 0; len > 0 &&
1307 (!best_match_offset || best_match_pos > i); i++) {
1308 int cur_len = strlen(cur_fdt_compat) + 1;
1310 if (!fdt_node_check_compatible(kfdt, 0,
1312 best_match_offset = noffset;
1317 cur_fdt_compat += cur_len;
1320 if (!best_match_offset) {
1321 debug("No match found.\n");
1325 return best_match_offset;
1329 * fit_conf_get_node - get node offset for configuration of a given unit name
1330 * @fit: pointer to the FIT format image header
1331 * @conf_uname: configuration node unit name
1333 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1334 * parant node) of a provided unit name. If configuration is found its node
1335 * offset is returned to the caller.
1337 * When NULL is provided in second argument fit_conf_get_node() will search
1338 * for a default configuration node instead. Default configuration node unit
1339 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1343 * configuration node offset when found (>=0)
1344 * negative number on failure (FDT_ERR_* code)
1346 int fit_conf_get_node(const void *fit, const char *conf_uname)
1348 int noffset, confs_noffset;
1351 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1352 if (confs_noffset < 0) {
1353 debug("Can't find configurations parent node '%s' (%s)\n",
1354 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1355 return confs_noffset;
1358 if (conf_uname == NULL) {
1359 /* get configuration unit name from the default property */
1360 debug("No configuration specified, trying default...\n");
1361 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1362 FIT_DEFAULT_PROP, &len);
1363 if (conf_uname == NULL) {
1364 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1368 debug("Found default configuration: '%s'\n", conf_uname);
1371 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1373 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1374 conf_uname, fdt_strerror(noffset));
1380 int fit_conf_get_prop_node(const void *fit, int noffset,
1381 const char *prop_name)
1386 /* get kernel image unit name from configuration kernel property */
1387 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1391 return fit_image_get_node(fit, uname);
1395 * fit_conf_print - prints out the FIT configuration details
1396 * @fit: pointer to the FIT format image header
1397 * @noffset: offset of the configuration node
1398 * @p: pointer to prefix string
1400 * fit_conf_print() lists all mandatory properies for the processed
1401 * configuration node.
1404 * no returned results
1406 void fit_conf_print(const void *fit, int noffset, const char *p)
1412 /* Mandatory properties */
1413 ret = fit_get_desc(fit, noffset, &desc);
1414 printf("%s Description: ", p);
1416 printf("unavailable\n");
1418 printf("%s\n", desc);
1420 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1421 printf("%s Kernel: ", p);
1423 printf("unavailable\n");
1425 printf("%s\n", uname);
1427 /* Optional properties */
1428 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1430 printf("%s Init Ramdisk: %s\n", p, uname);
1432 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1434 printf("%s FDT: %s\n", p, uname);
1437 int fit_image_select(const void *fit, int rd_noffset, int verify)
1439 fit_image_print(fit, rd_noffset, " ");
1442 puts(" Verifying Hash Integrity ... ");
1443 if (!fit_image_verify(fit, rd_noffset)) {
1444 puts("Bad Data Hash\n");
1453 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1460 debug("* %s: using config '%s' from image at 0x%08lx\n",
1461 prop_name, images->fit_uname_cfg, addr);
1463 /* Check whether configuration has this property defined */
1464 fit_hdr = map_sysmem(addr, 0);
1465 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1466 if (cfg_noffset < 0) {
1467 debug("* %s: no such config\n", prop_name);
1471 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1473 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1481 * fit_get_image_type_property() - get property name for IH_TYPE_...
1483 * @return the properly name where we expect to find the image in the
1486 static const char *fit_get_image_type_property(int type)
1489 * This is sort-of available in the uimage_type[] table in image.c
1490 * but we don't have access to the sohrt name, and "fdt" is different
1491 * anyway. So let's just keep it here.
1494 case IH_TYPE_FLATDT:
1495 return FIT_FDT_PROP;
1496 case IH_TYPE_KERNEL:
1497 return FIT_KERNEL_PROP;
1498 case IH_TYPE_RAMDISK:
1499 return FIT_RAMDISK_PROP;
1505 int fit_image_load(bootm_headers_t *images, ulong addr,
1506 const char **fit_unamep, const char **fit_uname_configp,
1507 int arch, int image_type, int bootstage_id,
1508 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1510 int cfg_noffset, noffset;
1511 const char *fit_uname;
1512 const char *fit_uname_config;
1517 ulong load, data, len;
1518 const char *prop_name;
1521 fit = map_sysmem(addr, 0);
1522 fit_uname = fit_unamep ? *fit_unamep : NULL;
1523 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1524 prop_name = fit_get_image_type_property(image_type);
1525 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1527 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1528 if (!fit_check_format(fit)) {
1529 printf("Bad FIT %s image format!\n", prop_name);
1530 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1533 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1535 /* get FIT component image node offset */
1536 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1537 noffset = fit_image_get_node(fit, fit_uname);
1540 * no image node unit name, try to get config
1541 * node first. If config unit node name is NULL
1542 * fit_conf_get_node() will try to find default config node
1544 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1545 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1546 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1548 cfg_noffset = fit_conf_get_node(fit,
1551 if (cfg_noffset < 0) {
1552 puts("Could not find configuration node\n");
1553 bootstage_error(bootstage_id +
1554 BOOTSTAGE_SUB_NO_UNIT_NAME);
1557 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1558 printf(" Using '%s' configuration\n", fit_uname_config);
1559 if (image_type == IH_TYPE_KERNEL) {
1560 /* Remember (and possibly verify) this config */
1561 images->fit_uname_cfg = fit_uname_config;
1562 if (IMAGE_ENABLE_VERIFY && images->verify) {
1563 puts(" Verifying Hash Integrity ... ");
1564 if (fit_config_verify(fit, cfg_noffset)) {
1565 puts("Bad Data Hash\n");
1566 bootstage_error(bootstage_id +
1567 BOOTSTAGE_SUB_HASH);
1572 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1575 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1577 fit_uname = fit_get_name(fit, noffset, NULL);
1580 puts("Could not find subimage node\n");
1581 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1585 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1587 ret = fit_image_select(fit, noffset, images->verify);
1589 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1593 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1595 if (!fit_image_check_target_arch(fit, noffset)) {
1596 puts("Unsupported Architecture\n");
1597 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1601 if (image_type == IH_TYPE_FLATDT &&
1602 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1603 puts("FDT image is compressed");
1604 return -EPROTONOSUPPORT;
1607 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1608 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1609 (image_type == IH_TYPE_KERNEL &&
1610 fit_image_check_type(fit, noffset,
1611 IH_TYPE_KERNEL_NOLOAD));
1612 os_ok = image_type == IH_TYPE_FLATDT ||
1613 fit_image_check_os(fit, noffset, IH_OS_LINUX);
1614 if (!type_ok || !os_ok) {
1615 printf("No Linux %s %s Image\n", genimg_get_arch_name(arch),
1616 genimg_get_type_name(image_type));
1617 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1621 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1623 /* get image data address and length */
1624 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1625 printf("Could not find %s subimage data!\n", prop_name);
1626 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1631 /* verify that image data is a proper FDT blob */
1632 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1633 puts("Subimage data is not a FDT");
1637 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1640 * Work-around for eldk-4.2 which gives this warning if we try to
1641 * cast in the unmap_sysmem() call:
1642 * warning: initialization discards qualifiers from pointer target type
1645 void *vbuf = (void *)buf;
1647 data = map_to_sysmem(vbuf);
1650 if (load_op == FIT_LOAD_IGNORED) {
1652 } else if (fit_image_get_load(fit, noffset, &load)) {
1653 if (load_op == FIT_LOAD_REQUIRED) {
1654 printf("Can't get %s subimage load address!\n",
1656 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1660 ulong image_start, image_end;
1665 * move image data to the load address,
1666 * make sure we don't overwrite initial image
1669 image_end = addr + fit_get_size(fit);
1671 load_end = load + len;
1672 if (image_type != IH_TYPE_KERNEL &&
1673 load < image_end && load_end > image_start) {
1674 printf("Error: %s overwritten\n", prop_name);
1678 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1679 prop_name, data, load);
1681 dst = map_sysmem(load, len);
1682 memmove(dst, buf, len);
1685 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1690 *fit_unamep = (char *)fit_uname;
1691 if (fit_uname_configp)
1692 *fit_uname_configp = (char *)fit_uname_config;