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>
25 #include <u-boot/crc.h>
26 #include <u-boot/md5.h>
28 /*****************************************************************************/
29 /* New uImage format routines */
30 /*****************************************************************************/
32 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
33 ulong *addr, const char **name)
40 sep = strchr(spec, sepc);
43 *addr = simple_strtoul(spec, NULL, 16);
53 * fit_parse_conf - parse FIT configuration spec
54 * @spec: input string, containing configuration spec
55 * @add_curr: current image address (to be used as a possible default)
56 * @addr: pointer to a ulong variable, will hold FIT image address of a given
58 * @conf_name double pointer to a char, will hold pointer to a configuration
61 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
62 * where <addr> is a FIT image address that contains configuration
63 * with a <conf> unit name.
65 * Address part is optional, and if omitted default add_curr will
69 * 1 if spec is a valid configuration string,
70 * addr and conf_name are set accordingly
73 int fit_parse_conf(const char *spec, ulong addr_curr,
74 ulong *addr, const char **conf_name)
76 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
80 * fit_parse_subimage - parse FIT subimage spec
81 * @spec: input string, containing subimage spec
82 * @add_curr: current image address (to be used as a possible default)
83 * @addr: pointer to a ulong variable, will hold FIT image address of a given
85 * @image_name: double pointer to a char, will hold pointer to a subimage name
87 * fit_parse_subimage() expects subimage spec in the form of
88 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
89 * subimage with a <subimg> unit name.
91 * Address part is optional, and if omitted default add_curr will
95 * 1 if spec is a valid subimage string,
96 * addr and image_name are set accordingly
99 int fit_parse_subimage(const char *spec, ulong addr_curr,
100 ulong *addr, const char **image_name)
102 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
104 #endif /* !USE_HOSTCC */
106 static void fit_get_debug(const void *fit, int noffset,
107 char *prop_name, int err)
109 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
110 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
114 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
116 * fit_print_contents - prints out the contents of the FIT format image
117 * @fit: pointer to the FIT format image header
118 * @p: pointer to prefix string
120 * fit_print_contents() formats a multi line FIT image contents description.
121 * The routine prints out FIT image properties (root node level) follwed by
122 * the details of each component image.
125 * no returned results
127 void fit_print_contents(const void *fit)
140 /* Indent string is defined in header image.h */
141 p = IMAGE_INDENT_STRING;
143 /* Root node properties */
144 ret = fit_get_desc(fit, 0, &desc);
145 printf("%sFIT description: ", p);
147 printf("unavailable\n");
149 printf("%s\n", desc);
151 if (IMAGE_ENABLE_TIMESTAMP) {
152 ret = fit_get_timestamp(fit, 0, ×tamp);
153 printf("%sCreated: ", p);
155 printf("unavailable\n");
157 genimg_print_time(timestamp);
160 /* Find images parent node offset */
161 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
162 if (images_noffset < 0) {
163 printf("Can't find images parent node '%s' (%s)\n",
164 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
168 /* Process its subnodes, print out component images details */
169 for (ndepth = 0, count = 0,
170 noffset = fdt_next_node(fit, images_noffset, &ndepth);
171 (noffset >= 0) && (ndepth > 0);
172 noffset = fdt_next_node(fit, noffset, &ndepth)) {
175 * Direct child node of the images parent node,
176 * i.e. component image node.
178 printf("%s Image %u (%s)\n", p, count++,
179 fit_get_name(fit, noffset, NULL));
181 fit_image_print(fit, noffset, p);
185 /* Find configurations parent node offset */
186 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
187 if (confs_noffset < 0) {
188 debug("Can't get configurations parent node '%s' (%s)\n",
189 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
193 /* get default configuration unit name from default property */
194 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
196 printf("%s Default Configuration: '%s'\n", p, uname);
198 /* Process its subnodes, print out configurations details */
199 for (ndepth = 0, count = 0,
200 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
201 (noffset >= 0) && (ndepth > 0);
202 noffset = fdt_next_node(fit, noffset, &ndepth)) {
205 * Direct child node of the configurations parent node,
206 * i.e. configuration node.
208 printf("%s Configuration %u (%s)\n", p, count++,
209 fit_get_name(fit, noffset, NULL));
211 fit_conf_print(fit, noffset, p);
217 * fit_image_print_data() - prints out the hash node details
218 * @fit: pointer to the FIT format image header
219 * @noffset: offset of the hash node
220 * @p: pointer to prefix string
221 * @type: Type of information to print ("hash" or "sign")
223 * fit_image_print_data() lists properies for the processed hash node
225 * This function avoid using puts() since it prints a newline on the host
226 * but does not in U-Boot.
229 * no returned results
231 static void fit_image_print_data(const void *fit, int noffset, const char *p,
241 debug("%s %s node: '%s'\n", p, type,
242 fit_get_name(fit, noffset, NULL));
243 printf("%s %s algo: ", p, type);
244 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
245 printf("invalid/unsupported\n");
249 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
250 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
252 printf(":%s", keyname);
254 printf(" (required)");
257 ret = fit_image_hash_get_value(fit, noffset, &value,
259 printf("%s %s value: ", p, type);
261 printf("unavailable\n");
263 for (i = 0; i < value_len; i++)
264 printf("%02x", value[i]);
268 debug("%s %s len: %d\n", p, type, value_len);
270 /* Signatures have a time stamp */
271 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
274 printf("%s Timestamp: ", p);
275 if (fit_get_timestamp(fit, noffset, ×tamp))
276 printf("unavailable\n");
278 genimg_print_time(timestamp);
283 * fit_image_print_verification_data() - prints out the hash/signature details
284 * @fit: pointer to the FIT format image header
285 * @noffset: offset of the hash or signature node
286 * @p: pointer to prefix string
288 * This lists properies for the processed hash node
291 * no returned results
293 static void fit_image_print_verification_data(const void *fit, int noffset,
299 * Check subnode name, must be equal to "hash" or "signature".
300 * Multiple hash/signature nodes require unique unit node
301 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
303 name = fit_get_name(fit, noffset, NULL);
304 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
305 fit_image_print_data(fit, noffset, p, "Hash");
306 } else if (!strncmp(name, FIT_SIG_NODENAME,
307 strlen(FIT_SIG_NODENAME))) {
308 fit_image_print_data(fit, noffset, p, "Sign");
313 * fit_image_print - prints out the FIT component image details
314 * @fit: pointer to the FIT format image header
315 * @image_noffset: offset of the component image node
316 * @p: pointer to prefix string
318 * fit_image_print() lists all mandatory properies for the processed component
319 * image. If present, hash nodes are printed out as well. Load
320 * address for images of type firmware is also printed out. Since the load
321 * address is not mandatory for firmware images, it will be output as
322 * "unavailable" when not present.
325 * no returned results
327 void fit_image_print(const void *fit, int image_noffset, const char *p)
330 uint8_t type, arch, os, comp;
338 /* Mandatory properties */
339 ret = fit_get_desc(fit, image_noffset, &desc);
340 printf("%s Description: ", p);
342 printf("unavailable\n");
344 printf("%s\n", desc);
346 if (IMAGE_ENABLE_TIMESTAMP) {
349 ret = fit_get_timestamp(fit, 0, ×tamp);
350 printf("%s Created: ", p);
352 printf("unavailable\n");
354 genimg_print_time(timestamp);
357 fit_image_get_type(fit, image_noffset, &type);
358 printf("%s Type: %s\n", p, genimg_get_type_name(type));
360 fit_image_get_comp(fit, image_noffset, &comp);
361 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
363 ret = fit_image_get_data(fit, image_noffset, &data, &size);
366 printf("%s Data Start: ", p);
368 printf("unavailable\n");
370 void *vdata = (void *)data;
372 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
376 printf("%s Data Size: ", p);
378 printf("unavailable\n");
380 genimg_print_size(size);
382 /* Remaining, type dependent properties */
383 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
384 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
385 (type == IH_TYPE_FLATDT)) {
386 fit_image_get_arch(fit, image_noffset, &arch);
387 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
390 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
391 fit_image_get_os(fit, image_noffset, &os);
392 printf("%s OS: %s\n", p, genimg_get_os_name(os));
395 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
396 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
397 ret = fit_image_get_load(fit, image_noffset, &load);
398 printf("%s Load Address: ", p);
400 printf("unavailable\n");
402 printf("0x%08lx\n", load);
405 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
406 (type == IH_TYPE_RAMDISK)) {
407 fit_image_get_entry(fit, image_noffset, &entry);
408 printf("%s Entry Point: ", p);
410 printf("unavailable\n");
412 printf("0x%08lx\n", entry);
415 /* Process all hash subnodes of the component image node */
416 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
417 (noffset >= 0) && (ndepth > 0);
418 noffset = fdt_next_node(fit, noffset, &ndepth)) {
420 /* Direct child node of the component image node */
421 fit_image_print_verification_data(fit, noffset, p);
428 * fit_get_desc - get node description property
429 * @fit: pointer to the FIT format image header
430 * @noffset: node offset
431 * @desc: double pointer to the char, will hold pointer to the descrption
433 * fit_get_desc() reads description property from a given node, if
434 * description is found pointer to it is returened in third call argument.
440 int fit_get_desc(const void *fit, int noffset, char **desc)
444 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
446 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
454 * fit_get_timestamp - get node timestamp property
455 * @fit: pointer to the FIT format image header
456 * @noffset: node offset
457 * @timestamp: pointer to the time_t, will hold read timestamp
459 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
460 * is found and has a correct size its value is retured in third call
465 * -1, on property read failure
466 * -2, on wrong timestamp size
468 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
473 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
475 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
478 if (len != sizeof(uint32_t)) {
479 debug("FIT timestamp with incorrect size of (%u)\n", len);
483 *timestamp = uimage_to_cpu(*((uint32_t *)data));
488 * fit_image_get_node - get node offset for component image of a given unit name
489 * @fit: pointer to the FIT format image header
490 * @image_uname: component image node unit name
492 * fit_image_get_node() finds a component image (withing the '/images'
493 * node) of a provided unit name. If image is found its node offset is
494 * returned to the caller.
497 * image node offset when found (>=0)
498 * negative number on failure (FDT_ERR_* code)
500 int fit_image_get_node(const void *fit, const char *image_uname)
502 int noffset, images_noffset;
504 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
505 if (images_noffset < 0) {
506 debug("Can't find images parent node '%s' (%s)\n",
507 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
508 return images_noffset;
511 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
513 debug("Can't get node offset for image unit name: '%s' (%s)\n",
514 image_uname, fdt_strerror(noffset));
521 * fit_image_get_os - get os id for a given component image node
522 * @fit: pointer to the FIT format image header
523 * @noffset: component image node offset
524 * @os: pointer to the uint8_t, will hold os numeric id
526 * fit_image_get_os() finds os property in a given component image node.
527 * If the property is found, its (string) value is translated to the numeric
528 * id which is returned to the caller.
534 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
539 /* Get OS name from property data */
540 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
542 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
547 /* Translate OS name to id */
548 *os = genimg_get_os_id(data);
553 * fit_image_get_arch - get arch id for a given component image node
554 * @fit: pointer to the FIT format image header
555 * @noffset: component image node offset
556 * @arch: pointer to the uint8_t, will hold arch numeric id
558 * fit_image_get_arch() finds arch property in a given component image node.
559 * If the property is found, its (string) value is translated to the numeric
560 * id which is returned to the caller.
566 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
571 /* Get architecture name from property data */
572 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
574 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
579 /* Translate architecture name to id */
580 *arch = genimg_get_arch_id(data);
585 * fit_image_get_type - get type id for a given component image node
586 * @fit: pointer to the FIT format image header
587 * @noffset: component image node offset
588 * @type: pointer to the uint8_t, will hold type numeric id
590 * fit_image_get_type() finds type property in a given component image node.
591 * If the property is found, its (string) value is translated to the numeric
592 * id which is returned to the caller.
598 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
603 /* Get image type name from property data */
604 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
606 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
611 /* Translate image type name to id */
612 *type = genimg_get_type_id(data);
617 * fit_image_get_comp - get comp id for a given component image node
618 * @fit: pointer to the FIT format image header
619 * @noffset: component image node offset
620 * @comp: pointer to the uint8_t, will hold comp numeric id
622 * fit_image_get_comp() finds comp property in a given component image node.
623 * If the property is found, its (string) value is translated to the numeric
624 * id which is returned to the caller.
630 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
635 /* Get compression name from property data */
636 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
638 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
643 /* Translate compression name to id */
644 *comp = genimg_get_comp_id(data);
649 * fit_image_get_load() - get load addr property for given component image node
650 * @fit: pointer to the FIT format image header
651 * @noffset: component image node offset
652 * @load: pointer to the uint32_t, will hold load address
654 * fit_image_get_load() finds load address property in a given component
655 * image node. If the property is found, its value is returned to the caller.
661 int fit_image_get_load(const void *fit, int noffset, ulong *load)
664 const uint32_t *data;
666 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
668 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
672 *load = uimage_to_cpu(*data);
677 * fit_image_get_entry() - get entry point address property
678 * @fit: pointer to the FIT format image header
679 * @noffset: component image node offset
680 * @entry: pointer to the uint32_t, will hold entry point address
682 * This gets the entry point address property for a given component image
685 * fit_image_get_entry() finds entry point address property in a given
686 * component image node. If the property is found, its value is returned
693 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
696 const uint32_t *data;
698 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
700 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
704 *entry = uimage_to_cpu(*data);
709 * fit_image_get_data - get data property and its size for a given component image node
710 * @fit: pointer to the FIT format image header
711 * @noffset: component image node offset
712 * @data: double pointer to void, will hold data property's data address
713 * @size: pointer to size_t, will hold data property's data size
715 * fit_image_get_data() finds data property in a given component image node.
716 * If the property is found its data start address and size are returned to
723 int fit_image_get_data(const void *fit, int noffset,
724 const void **data, size_t *size)
728 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
730 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
740 * fit_image_hash_get_algo - get hash algorithm name
741 * @fit: pointer to the FIT format image header
742 * @noffset: hash node offset
743 * @algo: double pointer to char, will hold pointer to the algorithm name
745 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
746 * If the property is found its data start address is returned to the caller.
752 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
756 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
758 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
766 * fit_image_hash_get_value - get hash value and length
767 * @fit: pointer to the FIT format image header
768 * @noffset: hash node offset
769 * @value: double pointer to uint8_t, will hold address of a hash value data
770 * @value_len: pointer to an int, will hold hash data length
772 * fit_image_hash_get_value() finds hash value property in a given hash node.
773 * If the property is found its data start address and size are returned to
780 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
785 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
786 if (*value == NULL) {
787 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
797 * fit_image_hash_get_ignore - get hash ignore flag
798 * @fit: pointer to the FIT format image header
799 * @noffset: hash node offset
800 * @ignore: pointer to an int, will hold hash ignore flag
802 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
803 * If the property is found and non-zero, the hash algorithm is not verified by
804 * u-boot automatically.
807 * 0, on ignore not found
808 * value, on ignore found
810 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
815 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
816 if (value == NULL || len != sizeof(int))
825 * fit_set_timestamp - set node timestamp property
826 * @fit: pointer to the FIT format image header
827 * @noffset: node offset
828 * @timestamp: timestamp value to be set
830 * fit_set_timestamp() attempts to set timestamp property in the requested
831 * node and returns operation status to the caller.
835 * -1, on property read failure
837 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
842 t = cpu_to_uimage(timestamp);
843 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
846 printf("Can't set '%s' property for '%s' node (%s)\n",
847 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
856 * calculate_hash - calculate and return hash for provided input data
857 * @data: pointer to the input data
858 * @data_len: data length
859 * @algo: requested hash algorithm
860 * @value: pointer to the char, will hold hash value data (caller must
861 * allocate enough free space)
862 * value_len: length of the calculated hash
864 * calculate_hash() computes input data hash according to the requested
866 * Resulting hash value is placed in caller provided 'value' buffer, length
867 * of the calculated hash is returned via value_len pointer argument.
871 * -1, when algo is unsupported
873 int calculate_hash(const void *data, int data_len, const char *algo,
874 uint8_t *value, int *value_len)
876 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
877 *((uint32_t *)value) = crc32_wd(0, data, data_len,
879 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
881 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
882 sha1_csum_wd((unsigned char *)data, data_len,
883 (unsigned char *)value, CHUNKSZ_SHA1);
885 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
886 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
889 debug("Unsupported hash alogrithm\n");
895 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
896 size_t size, char **err_msgp)
898 uint8_t value[FIT_MAX_HASH_LEN];
907 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
908 *err_msgp = "Can't get hash algo property";
913 if (IMAGE_ENABLE_IGNORE) {
914 fit_image_hash_get_ignore(fit, noffset, &ignore);
921 if (fit_image_hash_get_value(fit, noffset, &fit_value,
923 *err_msgp = "Can't get hash value property";
927 if (calculate_hash(data, size, algo, value, &value_len)) {
928 *err_msgp = "Unsupported hash algorithm";
932 if (value_len != fit_value_len) {
933 *err_msgp = "Bad hash value len";
935 } else if (memcmp(value, fit_value, value_len) != 0) {
936 *err_msgp = "Bad hash value";
944 * fit_image_verify - verify data intergity
945 * @fit: pointer to the FIT format image header
946 * @image_noffset: component image node offset
948 * fit_image_verify() goes over component image hash nodes,
949 * re-calculates each data hash and compares with the value stored in hash
953 * 1, if all hashes are valid
954 * 0, otherwise (or on error)
956 int fit_image_verify(const void *fit, int image_noffset)
965 /* Get image data and data length */
966 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
967 err_msg = "Can't get image data/size";
971 /* Verify all required signatures */
972 if (IMAGE_ENABLE_VERIFY &&
973 fit_image_verify_required_sigs(fit, image_noffset, data, size,
974 gd_fdt_blob(), &verify_all)) {
975 err_msg = "Unable to verify required signature";
979 /* Process all hash subnodes of the component image node */
980 for (noffset = fdt_first_subnode(fit, image_noffset);
982 noffset = fdt_next_subnode(fit, noffset)) {
983 const char *name = fit_get_name(fit, noffset, NULL);
986 * Check subnode name, must be equal to "hash".
987 * Multiple hash nodes require unique unit node
988 * names, e.g. hash@1, hash@2, etc.
990 if (!strncmp(name, FIT_HASH_NODENAME,
991 strlen(FIT_HASH_NODENAME))) {
992 if (fit_image_check_hash(fit, noffset, data, size,
996 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
997 !strncmp(name, FIT_SIG_NODENAME,
998 strlen(FIT_SIG_NODENAME))) {
999 ret = fit_image_check_sig(fit, noffset, data,
1000 size, -1, &err_msg);
1008 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1009 err_msg = "Corrupted or truncated tree";
1016 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1017 err_msg, fit_get_name(fit, noffset, NULL),
1018 fit_get_name(fit, image_noffset, NULL));
1023 * fit_all_image_verify - verify data intergity for all images
1024 * @fit: pointer to the FIT format image header
1026 * fit_all_image_verify() goes over all images in the FIT and
1027 * for every images checks if all it's hashes are valid.
1030 * 1, if all hashes of all images are valid
1031 * 0, otherwise (or on error)
1033 int fit_all_image_verify(const void *fit)
1040 /* Find images parent node offset */
1041 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1042 if (images_noffset < 0) {
1043 printf("Can't find images parent node '%s' (%s)\n",
1044 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1048 /* Process all image subnodes, check hashes for each */
1049 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1051 for (ndepth = 0, count = 0,
1052 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1053 (noffset >= 0) && (ndepth > 0);
1054 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1057 * Direct child node of the images parent node,
1058 * i.e. component image node.
1060 printf(" Hash(es) for Image %u (%s): ", count++,
1061 fit_get_name(fit, noffset, NULL));
1063 if (!fit_image_verify(fit, noffset))
1072 * fit_image_check_os - check whether image node is of a given os type
1073 * @fit: pointer to the FIT format image header
1074 * @noffset: component image node offset
1075 * @os: requested image os
1077 * fit_image_check_os() reads image os property and compares its numeric
1078 * id with the requested os. Comparison result is returned to the caller.
1081 * 1 if image is of given os type
1082 * 0 otherwise (or on error)
1084 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1088 if (fit_image_get_os(fit, noffset, &image_os))
1090 return (os == image_os);
1094 * fit_image_check_arch - check whether image node is of a given arch
1095 * @fit: pointer to the FIT format image header
1096 * @noffset: component image node offset
1097 * @arch: requested imagearch
1099 * fit_image_check_arch() reads image arch property and compares its numeric
1100 * id with the requested arch. Comparison result is returned to the caller.
1103 * 1 if image is of given arch
1104 * 0 otherwise (or on error)
1106 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1110 if (fit_image_get_arch(fit, noffset, &image_arch))
1112 return (arch == image_arch);
1116 * fit_image_check_type - check whether image node is of a given type
1117 * @fit: pointer to the FIT format image header
1118 * @noffset: component image node offset
1119 * @type: requested image type
1121 * fit_image_check_type() reads image type property and compares its numeric
1122 * id with the requested type. Comparison result is returned to the caller.
1125 * 1 if image is of given type
1126 * 0 otherwise (or on error)
1128 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1132 if (fit_image_get_type(fit, noffset, &image_type))
1134 return (type == image_type);
1138 * fit_image_check_comp - check whether image node uses given compression
1139 * @fit: pointer to the FIT format image header
1140 * @noffset: component image node offset
1141 * @comp: requested image compression type
1143 * fit_image_check_comp() reads image compression property and compares its
1144 * numeric id with the requested compression type. Comparison result is
1145 * returned to the caller.
1148 * 1 if image uses requested compression
1149 * 0 otherwise (or on error)
1151 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1155 if (fit_image_get_comp(fit, noffset, &image_comp))
1157 return (comp == image_comp);
1161 * fit_check_format - sanity check FIT image format
1162 * @fit: pointer to the FIT format image header
1164 * fit_check_format() runs a basic sanity FIT image verification.
1165 * Routine checks for mandatory properties, nodes, etc.
1171 int fit_check_format(const void *fit)
1173 /* mandatory / node 'description' property */
1174 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1175 debug("Wrong FIT format: no description\n");
1179 if (IMAGE_ENABLE_TIMESTAMP) {
1180 /* mandatory / node 'timestamp' property */
1181 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1182 debug("Wrong FIT format: no timestamp\n");
1187 /* mandatory subimages parent '/images' node */
1188 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1189 debug("Wrong FIT format: no images parent node\n");
1198 * fit_conf_find_compat
1199 * @fit: pointer to the FIT format image header
1200 * @fdt: pointer to the device tree to compare against
1202 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1203 * most compatible with the passed in device tree.
1212 * |-o configurations
1220 * |-compatible = "foo,bar", "bim,bam"
1223 * |-compatible = "foo,bar",
1226 * |-compatible = "bim,bam", "baz,biz"
1228 * Configuration 1 would be picked because the first string in U-Boot's
1229 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1230 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1233 * offset to the configuration to use if one was found
1236 int fit_conf_find_compat(const void *fit, const void *fdt)
1239 int noffset, confs_noffset, images_noffset;
1240 const void *fdt_compat;
1242 int best_match_offset = 0;
1243 int best_match_pos = 0;
1245 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1246 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1247 if (confs_noffset < 0 || images_noffset < 0) {
1248 debug("Can't find configurations or images nodes.\n");
1252 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1254 debug("Fdt for comparison has no \"compatible\" property.\n");
1259 * Loop over the configurations in the FIT image.
1261 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1262 (noffset >= 0) && (ndepth > 0);
1263 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1265 const char *kfdt_name;
1267 const char *cur_fdt_compat;
1275 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1277 debug("No fdt property found.\n");
1280 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1282 if (kfdt_noffset < 0) {
1283 debug("No image node named \"%s\" found.\n",
1288 * Get a pointer to this configuration's fdt.
1290 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1291 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1295 len = fdt_compat_len;
1296 cur_fdt_compat = fdt_compat;
1298 * Look for a match for each U-Boot compatibility string in
1299 * turn in this configuration's fdt.
1301 for (i = 0; len > 0 &&
1302 (!best_match_offset || best_match_pos > i); i++) {
1303 int cur_len = strlen(cur_fdt_compat) + 1;
1305 if (!fdt_node_check_compatible(kfdt, 0,
1307 best_match_offset = noffset;
1312 cur_fdt_compat += cur_len;
1315 if (!best_match_offset) {
1316 debug("No match found.\n");
1320 return best_match_offset;
1324 * fit_conf_get_node - get node offset for configuration of a given unit name
1325 * @fit: pointer to the FIT format image header
1326 * @conf_uname: configuration node unit name
1328 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1329 * parant node) of a provided unit name. If configuration is found its node
1330 * offset is returned to the caller.
1332 * When NULL is provided in second argument fit_conf_get_node() will search
1333 * for a default configuration node instead. Default configuration node unit
1334 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1338 * configuration node offset when found (>=0)
1339 * negative number on failure (FDT_ERR_* code)
1341 int fit_conf_get_node(const void *fit, const char *conf_uname)
1343 int noffset, confs_noffset;
1346 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1347 if (confs_noffset < 0) {
1348 debug("Can't find configurations parent node '%s' (%s)\n",
1349 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1350 return confs_noffset;
1353 if (conf_uname == NULL) {
1354 /* get configuration unit name from the default property */
1355 debug("No configuration specified, trying default...\n");
1356 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1357 FIT_DEFAULT_PROP, &len);
1358 if (conf_uname == NULL) {
1359 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1363 debug("Found default configuration: '%s'\n", conf_uname);
1366 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1368 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1369 conf_uname, fdt_strerror(noffset));
1375 int fit_conf_get_prop_node(const void *fit, int noffset,
1376 const char *prop_name)
1381 /* get kernel image unit name from configuration kernel property */
1382 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1386 return fit_image_get_node(fit, uname);
1390 * fit_conf_print - prints out the FIT configuration details
1391 * @fit: pointer to the FIT format image header
1392 * @noffset: offset of the configuration node
1393 * @p: pointer to prefix string
1395 * fit_conf_print() lists all mandatory properies for the processed
1396 * configuration node.
1399 * no returned results
1401 void fit_conf_print(const void *fit, int noffset, const char *p)
1407 /* Mandatory properties */
1408 ret = fit_get_desc(fit, noffset, &desc);
1409 printf("%s Description: ", p);
1411 printf("unavailable\n");
1413 printf("%s\n", desc);
1415 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1416 printf("%s Kernel: ", p);
1418 printf("unavailable\n");
1420 printf("%s\n", uname);
1422 /* Optional properties */
1423 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1425 printf("%s Init Ramdisk: %s\n", p, uname);
1427 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1429 printf("%s FDT: %s\n", p, uname);
1432 int fit_image_select(const void *fit, int rd_noffset, int verify)
1434 fit_image_print(fit, rd_noffset, " ");
1437 puts(" Verifying Hash Integrity ... ");
1438 if (!fit_image_verify(fit, rd_noffset)) {
1439 puts("Bad Data Hash\n");
1448 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1455 debug("* %s: using config '%s' from image at 0x%08lx\n",
1456 prop_name, images->fit_uname_cfg, addr);
1458 /* Check whether configuration has this property defined */
1459 fit_hdr = map_sysmem(addr, 0);
1460 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1461 if (cfg_noffset < 0) {
1462 debug("* %s: no such config\n", prop_name);
1466 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1468 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1475 int fit_image_load(bootm_headers_t *images, const char *prop_name, ulong addr,
1476 const char **fit_unamep, const char **fit_uname_configp,
1477 int arch, int image_type, int bootstage_id,
1478 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1480 int cfg_noffset, noffset;
1481 const char *fit_uname;
1482 const char *fit_uname_config;
1487 ulong load, data, len;
1490 fit = map_sysmem(addr, 0);
1491 fit_uname = fit_unamep ? *fit_unamep : NULL;
1492 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1493 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1495 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1496 if (!fit_check_format(fit)) {
1497 printf("Bad FIT %s image format!\n", prop_name);
1498 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1501 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1503 /* get FIT component image node offset */
1504 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1505 noffset = fit_image_get_node(fit, fit_uname);
1508 * no image node unit name, try to get config
1509 * node first. If config unit node name is NULL
1510 * fit_conf_get_node() will try to find default config node
1512 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1513 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1514 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1516 cfg_noffset = fit_conf_get_node(fit,
1519 if (cfg_noffset < 0) {
1520 puts("Could not find configuration node\n");
1521 bootstage_error(bootstage_id +
1522 BOOTSTAGE_SUB_NO_UNIT_NAME);
1525 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1526 printf(" Using '%s' configuration\n", fit_uname_config);
1527 if (image_type == IH_TYPE_KERNEL) {
1528 /* Remember (and possibly verify) this config */
1529 images->fit_uname_cfg = fit_uname_config;
1530 if (IMAGE_ENABLE_VERIFY && images->verify) {
1531 puts(" Verifying Hash Integrity ... ");
1532 if (!fit_config_verify(fit, cfg_noffset)) {
1533 puts("Bad Data Hash\n");
1534 bootstage_error(bootstage_id +
1535 BOOTSTAGE_SUB_HASH);
1540 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1543 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1545 fit_uname = fit_get_name(fit, noffset, NULL);
1548 puts("Could not find subimage node\n");
1549 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1553 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1555 ret = fit_image_select(fit, noffset, images->verify);
1557 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1561 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1562 if (!fit_image_check_target_arch(fit, noffset)) {
1563 puts("Unsupported Architecture\n");
1564 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1568 if (image_type == IH_TYPE_FLATDT &&
1569 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1570 puts("FDT image is compressed");
1571 return -EPROTONOSUPPORT;
1574 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1575 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1576 (image_type == IH_TYPE_KERNEL &&
1577 fit_image_check_type(fit, noffset,
1578 IH_TYPE_KERNEL_NOLOAD));
1579 os_ok = image_type == IH_TYPE_FLATDT ||
1580 fit_image_check_os(fit, noffset, IH_OS_LINUX);
1581 if (!type_ok || !os_ok) {
1582 printf("No Linux %s %s Image\n", genimg_get_arch_name(arch),
1583 genimg_get_type_name(image_type));
1584 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1588 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1590 /* get image data address and length */
1591 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1592 printf("Could not find %s subimage data!\n", prop_name);
1593 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1598 /* verify that image data is a proper FDT blob */
1599 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
1600 puts("Subimage data is not a FDT");
1604 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1607 * Work-around for eldk-4.2 which gives this warning if we try to
1608 * case in the unmap_sysmem() call:
1609 * warning: initialization discards qualifiers from pointer target type
1612 void *vbuf = (void *)buf;
1614 data = map_to_sysmem(vbuf);
1617 if (load_op == FIT_LOAD_IGNORED) {
1619 } else if (fit_image_get_load(fit, noffset, &load)) {
1620 if (load_op == FIT_LOAD_REQUIRED) {
1621 printf("Can't get %s subimage load address!\n",
1623 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1627 ulong image_start, image_end;
1632 * move image data to the load address,
1633 * make sure we don't overwrite initial image
1636 image_end = addr + fit_get_size(fit);
1638 load_end = load + len;
1639 if (image_type != IH_TYPE_KERNEL &&
1640 load < image_end && load_end > image_start) {
1641 printf("Error: %s overwritten\n", prop_name);
1645 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1646 prop_name, data, load);
1648 dst = map_sysmem(load, len);
1649 memmove(dst, buf, len);
1652 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1657 *fit_unamep = (char *)fit_uname;
1658 if (fit_uname_configp)
1659 *fit_uname_configp = (char *)fit_uname_config;