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 for 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 for 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 fit_image_get_type(fit, image_noffset, &type);
347 printf("%s Type: %s\n", p, genimg_get_type_name(type));
349 fit_image_get_comp(fit, image_noffset, &comp);
350 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
352 ret = fit_image_get_data(fit, image_noffset, &data, &size);
355 printf("%s Data Start: ", p);
357 printf("unavailable\n");
359 void *vdata = (void *)data;
361 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
365 printf("%s Data Size: ", p);
367 printf("unavailable\n");
369 genimg_print_size(size);
371 /* Remaining, type dependent properties */
372 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
373 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
374 (type == IH_TYPE_FLATDT)) {
375 fit_image_get_arch(fit, image_noffset, &arch);
376 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
379 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
380 fit_image_get_os(fit, image_noffset, &os);
381 printf("%s OS: %s\n", p, genimg_get_os_name(os));
384 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
385 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
386 ret = fit_image_get_load(fit, image_noffset, &load);
387 printf("%s Load Address: ", p);
389 printf("unavailable\n");
391 printf("0x%08lx\n", load);
394 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
395 (type == IH_TYPE_RAMDISK)) {
396 fit_image_get_entry(fit, image_noffset, &entry);
397 printf("%s Entry Point: ", p);
399 printf("unavailable\n");
401 printf("0x%08lx\n", entry);
404 /* Process all hash subnodes of the component image node */
405 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
406 (noffset >= 0) && (ndepth > 0);
407 noffset = fdt_next_node(fit, noffset, &ndepth)) {
409 /* Direct child node of the component image node */
410 fit_image_print_verification_data(fit, noffset, p);
417 * fit_get_desc - get node description property
418 * @fit: pointer to the FIT format image header
419 * @noffset: node offset
420 * @desc: double pointer to the char, will hold pointer to the descrption
422 * fit_get_desc() reads description property from a given node, if
423 * description is found pointer to it is returened in third call argument.
429 int fit_get_desc(const void *fit, int noffset, char **desc)
433 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
435 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
443 * fit_get_timestamp - get node timestamp property
444 * @fit: pointer to the FIT format image header
445 * @noffset: node offset
446 * @timestamp: pointer to the time_t, will hold read timestamp
448 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
449 * is found and has a correct size its value is retured in third call
454 * -1, on property read failure
455 * -2, on wrong timestamp size
457 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
462 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
464 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
467 if (len != sizeof(uint32_t)) {
468 debug("FIT timestamp with incorrect size of (%u)\n", len);
472 *timestamp = uimage_to_cpu(*((uint32_t *)data));
477 * fit_image_get_node - get node offset for component image of a given unit name
478 * @fit: pointer to the FIT format image header
479 * @image_uname: component image node unit name
481 * fit_image_get_node() finds a component image (withing the '/images'
482 * node) of a provided unit name. If image is found its node offset is
483 * returned to the caller.
486 * image node offset when found (>=0)
487 * negative number on failure (FDT_ERR_* code)
489 int fit_image_get_node(const void *fit, const char *image_uname)
491 int noffset, images_noffset;
493 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
494 if (images_noffset < 0) {
495 debug("Can't find images parent node '%s' (%s)\n",
496 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
497 return images_noffset;
500 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
502 debug("Can't get node offset for image unit name: '%s' (%s)\n",
503 image_uname, fdt_strerror(noffset));
510 * fit_image_get_os - get os id for a given component image node
511 * @fit: pointer to the FIT format image header
512 * @noffset: component image node offset
513 * @os: pointer to the uint8_t, will hold os numeric id
515 * fit_image_get_os() finds os property in a given component image node.
516 * If the property is found, its (string) value is translated to the numeric
517 * id which is returned to the caller.
523 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
528 /* Get OS name from property data */
529 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
531 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
536 /* Translate OS name to id */
537 *os = genimg_get_os_id(data);
542 * fit_image_get_arch - get arch id for a given component image node
543 * @fit: pointer to the FIT format image header
544 * @noffset: component image node offset
545 * @arch: pointer to the uint8_t, will hold arch numeric id
547 * fit_image_get_arch() finds arch property in a given component image node.
548 * If the property is found, its (string) value is translated to the numeric
549 * id which is returned to the caller.
555 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
560 /* Get architecture name from property data */
561 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
563 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
568 /* Translate architecture name to id */
569 *arch = genimg_get_arch_id(data);
574 * fit_image_get_type - get type id for a given component image node
575 * @fit: pointer to the FIT format image header
576 * @noffset: component image node offset
577 * @type: pointer to the uint8_t, will hold type numeric id
579 * fit_image_get_type() finds type property in a given component image node.
580 * If the property is found, its (string) value is translated to the numeric
581 * id which is returned to the caller.
587 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
592 /* Get image type name from property data */
593 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
595 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
600 /* Translate image type name to id */
601 *type = genimg_get_type_id(data);
606 * fit_image_get_comp - get comp id for a given component image node
607 * @fit: pointer to the FIT format image header
608 * @noffset: component image node offset
609 * @comp: pointer to the uint8_t, will hold comp numeric id
611 * fit_image_get_comp() finds comp property in a given component image node.
612 * If the property is found, its (string) value is translated to the numeric
613 * id which is returned to the caller.
619 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
624 /* Get compression name from property data */
625 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
627 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
632 /* Translate compression name to id */
633 *comp = genimg_get_comp_id(data);
638 * fit_image_get_load() - get load addr property for given component image node
639 * @fit: pointer to the FIT format image header
640 * @noffset: component image node offset
641 * @load: pointer to the uint32_t, will hold load address
643 * fit_image_get_load() finds load address property in a given component
644 * image node. If the property is found, its value is returned to the caller.
650 int fit_image_get_load(const void *fit, int noffset, ulong *load)
653 const uint32_t *data;
655 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
657 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
661 *load = uimage_to_cpu(*data);
666 * fit_image_get_entry() - get entry point address property
667 * @fit: pointer to the FIT format image header
668 * @noffset: component image node offset
669 * @entry: pointer to the uint32_t, will hold entry point address
671 * This gets the entry point address property for a given component image
674 * fit_image_get_entry() finds entry point address property in a given
675 * component image node. If the property is found, its value is returned
682 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
685 const uint32_t *data;
687 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
689 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
693 *entry = uimage_to_cpu(*data);
698 * fit_image_get_data - get data property and its size for a given component image node
699 * @fit: pointer to the FIT format image header
700 * @noffset: component image node offset
701 * @data: double pointer to void, will hold data property's data address
702 * @size: pointer to size_t, will hold data property's data size
704 * fit_image_get_data() finds data property in a given component image node.
705 * If the property is found its data start address and size are returned to
712 int fit_image_get_data(const void *fit, int noffset,
713 const void **data, size_t *size)
717 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
719 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
729 * fit_image_hash_get_algo - get hash algorithm name
730 * @fit: pointer to the FIT format image header
731 * @noffset: hash node offset
732 * @algo: double pointer to char, will hold pointer to the algorithm name
734 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
735 * If the property is found its data start address is returned to the caller.
741 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
745 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
747 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
755 * fit_image_hash_get_value - get hash value and length
756 * @fit: pointer to the FIT format image header
757 * @noffset: hash node offset
758 * @value: double pointer to uint8_t, will hold address of a hash value data
759 * @value_len: pointer to an int, will hold hash data length
761 * fit_image_hash_get_value() finds hash value property in a given hash node.
762 * If the property is found its data start address and size are returned to
769 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
774 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
775 if (*value == NULL) {
776 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
786 * fit_image_hash_get_ignore - get hash ignore flag
787 * @fit: pointer to the FIT format image header
788 * @noffset: hash node offset
789 * @ignore: pointer to an int, will hold hash ignore flag
791 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
792 * If the property is found and non-zero, the hash algorithm is not verified by
793 * u-boot automatically.
796 * 0, on ignore not found
797 * value, on ignore found
799 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
804 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
805 if (value == NULL || len != sizeof(int))
814 * fit_set_timestamp - set node timestamp property
815 * @fit: pointer to the FIT format image header
816 * @noffset: node offset
817 * @timestamp: timestamp value to be set
819 * fit_set_timestamp() attempts to set timestamp property in the requested
820 * node and returns operation status to the caller.
824 * -1, on property read failure
826 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
831 t = cpu_to_uimage(timestamp);
832 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
835 printf("Can't set '%s' property for '%s' node (%s)\n",
836 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
845 * calculate_hash - calculate and return hash for provided input data
846 * @data: pointer to the input data
847 * @data_len: data length
848 * @algo: requested hash algorithm
849 * @value: pointer to the char, will hold hash value data (caller must
850 * allocate enough free space)
851 * value_len: length of the calculated hash
853 * calculate_hash() computes input data hash according to the requested
855 * Resulting hash value is placed in caller provided 'value' buffer, length
856 * of the calculated hash is returned via value_len pointer argument.
860 * -1, when algo is unsupported
862 int calculate_hash(const void *data, int data_len, const char *algo,
863 uint8_t *value, int *value_len)
865 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
866 *((uint32_t *)value) = crc32_wd(0, data, data_len,
868 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
870 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
871 sha1_csum_wd((unsigned char *)data, data_len,
872 (unsigned char *)value, CHUNKSZ_SHA1);
874 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
875 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
878 debug("Unsupported hash alogrithm\n");
884 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
885 size_t size, char **err_msgp)
887 uint8_t value[FIT_MAX_HASH_LEN];
896 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
897 *err_msgp = "Can't get hash algo property";
902 if (IMAGE_ENABLE_IGNORE) {
903 fit_image_hash_get_ignore(fit, noffset, &ignore);
910 if (fit_image_hash_get_value(fit, noffset, &fit_value,
912 *err_msgp = "Can't get hash value property";
916 if (calculate_hash(data, size, algo, value, &value_len)) {
917 *err_msgp = "Unsupported hash algorithm";
921 if (value_len != fit_value_len) {
922 *err_msgp = "Bad hash value len";
924 } else if (memcmp(value, fit_value, value_len) != 0) {
925 *err_msgp = "Bad hash value";
933 * fit_image_verify - verify data intergity
934 * @fit: pointer to the FIT format image header
935 * @image_noffset: component image node offset
937 * fit_image_verify() goes over component image hash nodes,
938 * re-calculates each data hash and compares with the value stored in hash
942 * 1, if all hashes are valid
943 * 0, otherwise (or on error)
945 int fit_image_verify(const void *fit, int image_noffset)
954 /* Get image data and data length */
955 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
956 err_msg = "Can't get image data/size";
960 /* Verify all required signatures */
961 if (IMAGE_ENABLE_VERIFY &&
962 fit_image_verify_required_sigs(fit, image_noffset, data, size,
963 gd_fdt_blob(), &verify_all)) {
964 err_msg = "Unable to verify required signature";
968 /* Process all hash subnodes of the component image node */
969 for (noffset = fdt_first_subnode(fit, image_noffset);
971 noffset = fdt_next_subnode(fit, noffset)) {
972 const char *name = fit_get_name(fit, noffset, NULL);
975 * Check subnode name, must be equal to "hash".
976 * Multiple hash nodes require unique unit node
977 * names, e.g. hash@1, hash@2, etc.
979 if (!strncmp(name, FIT_HASH_NODENAME,
980 strlen(FIT_HASH_NODENAME))) {
981 if (fit_image_check_hash(fit, noffset, data, size,
985 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
986 !strncmp(name, FIT_SIG_NODENAME,
987 strlen(FIT_SIG_NODENAME))) {
988 ret = fit_image_check_sig(fit, noffset, data,
997 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
998 err_msg = "Corrupted or truncated tree";
1005 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1006 err_msg, fit_get_name(fit, noffset, NULL),
1007 fit_get_name(fit, image_noffset, NULL));
1012 * fit_all_image_verify - verify data intergity for all images
1013 * @fit: pointer to the FIT format image header
1015 * fit_all_image_verify() goes over all images in the FIT and
1016 * for every images checks if all it's hashes are valid.
1019 * 1, if all hashes of all images are valid
1020 * 0, otherwise (or on error)
1022 int fit_all_image_verify(const void *fit)
1029 /* Find images parent node offset */
1030 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1031 if (images_noffset < 0) {
1032 printf("Can't find images parent node '%s' (%s)\n",
1033 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1037 /* Process all image subnodes, check hashes for each */
1038 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1040 for (ndepth = 0, count = 0,
1041 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1042 (noffset >= 0) && (ndepth > 0);
1043 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1046 * Direct child node of the images parent node,
1047 * i.e. component image node.
1049 printf(" Hash(es) for Image %u (%s): ", count++,
1050 fit_get_name(fit, noffset, NULL));
1052 if (!fit_image_verify(fit, noffset))
1061 * fit_image_check_os - check whether image node is of a given os type
1062 * @fit: pointer to the FIT format image header
1063 * @noffset: component image node offset
1064 * @os: requested image os
1066 * fit_image_check_os() reads image os property and compares its numeric
1067 * id with the requested os. Comparison result is returned to the caller.
1070 * 1 if image is of given os type
1071 * 0 otherwise (or on error)
1073 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1077 if (fit_image_get_os(fit, noffset, &image_os))
1079 return (os == image_os);
1083 * fit_image_check_arch - check whether image node is of a given arch
1084 * @fit: pointer to the FIT format image header
1085 * @noffset: component image node offset
1086 * @arch: requested imagearch
1088 * fit_image_check_arch() reads image arch property and compares its numeric
1089 * id with the requested arch. Comparison result is returned to the caller.
1092 * 1 if image is of given arch
1093 * 0 otherwise (or on error)
1095 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1099 if (fit_image_get_arch(fit, noffset, &image_arch))
1101 return (arch == image_arch);
1105 * fit_image_check_type - check whether image node is of a given type
1106 * @fit: pointer to the FIT format image header
1107 * @noffset: component image node offset
1108 * @type: requested image type
1110 * fit_image_check_type() reads image type property and compares its numeric
1111 * id with the requested type. Comparison result is returned to the caller.
1114 * 1 if image is of given type
1115 * 0 otherwise (or on error)
1117 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1121 if (fit_image_get_type(fit, noffset, &image_type))
1123 return (type == image_type);
1127 * fit_image_check_comp - check whether image node uses given compression
1128 * @fit: pointer to the FIT format image header
1129 * @noffset: component image node offset
1130 * @comp: requested image compression type
1132 * fit_image_check_comp() reads image compression property and compares its
1133 * numeric id with the requested compression type. Comparison result is
1134 * returned to the caller.
1137 * 1 if image uses requested compression
1138 * 0 otherwise (or on error)
1140 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1144 if (fit_image_get_comp(fit, noffset, &image_comp))
1146 return (comp == image_comp);
1150 * fit_check_format - sanity check FIT image format
1151 * @fit: pointer to the FIT format image header
1153 * fit_check_format() runs a basic sanity FIT image verification.
1154 * Routine checks for mandatory properties, nodes, etc.
1160 int fit_check_format(const void *fit)
1162 /* mandatory / node 'description' property */
1163 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1164 debug("Wrong FIT format: no description\n");
1168 if (IMAGE_ENABLE_TIMESTAMP) {
1169 /* mandatory / node 'timestamp' property */
1170 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1171 debug("Wrong FIT format: no timestamp\n");
1176 /* mandatory subimages parent '/images' node */
1177 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1178 debug("Wrong FIT format: no images parent node\n");
1187 * fit_conf_find_compat
1188 * @fit: pointer to the FIT format image header
1189 * @fdt: pointer to the device tree to compare against
1191 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1192 * most compatible with the passed in device tree.
1201 * |-o configurations
1209 * |-compatible = "foo,bar", "bim,bam"
1212 * |-compatible = "foo,bar",
1215 * |-compatible = "bim,bam", "baz,biz"
1217 * Configuration 1 would be picked because the first string in U-Boot's
1218 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1219 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1222 * offset to the configuration to use if one was found
1225 int fit_conf_find_compat(const void *fit, const void *fdt)
1228 int noffset, confs_noffset, images_noffset;
1229 const void *fdt_compat;
1231 int best_match_offset = 0;
1232 int best_match_pos = 0;
1234 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1235 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1236 if (confs_noffset < 0 || images_noffset < 0) {
1237 debug("Can't find configurations or images nodes.\n");
1241 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1243 debug("Fdt for comparison has no \"compatible\" property.\n");
1248 * Loop over the configurations in the FIT image.
1250 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1251 (noffset >= 0) && (ndepth > 0);
1252 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1254 const char *kfdt_name;
1256 const char *cur_fdt_compat;
1264 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1266 debug("No fdt property found.\n");
1269 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1271 if (kfdt_noffset < 0) {
1272 debug("No image node named \"%s\" found.\n",
1277 * Get a pointer to this configuration's fdt.
1279 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1280 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1284 len = fdt_compat_len;
1285 cur_fdt_compat = fdt_compat;
1287 * Look for a match for each U-Boot compatibility string in
1288 * turn in this configuration's fdt.
1290 for (i = 0; len > 0 &&
1291 (!best_match_offset || best_match_pos > i); i++) {
1292 int cur_len = strlen(cur_fdt_compat) + 1;
1294 if (!fdt_node_check_compatible(kfdt, 0,
1296 best_match_offset = noffset;
1301 cur_fdt_compat += cur_len;
1304 if (!best_match_offset) {
1305 debug("No match found.\n");
1309 return best_match_offset;
1313 * fit_conf_get_node - get node offset for configuration of a given unit name
1314 * @fit: pointer to the FIT format image header
1315 * @conf_uname: configuration node unit name
1317 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1318 * parant node) of a provided unit name. If configuration is found its node
1319 * offset is returned to the caller.
1321 * When NULL is provided in second argument fit_conf_get_node() will search
1322 * for a default configuration node instead. Default configuration node unit
1323 * name is retrived from FIT_DEFAULT_PROP property of the '/configurations'
1327 * configuration node offset when found (>=0)
1328 * negative number on failure (FDT_ERR_* code)
1330 int fit_conf_get_node(const void *fit, const char *conf_uname)
1332 int noffset, confs_noffset;
1335 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1336 if (confs_noffset < 0) {
1337 debug("Can't find configurations parent node '%s' (%s)\n",
1338 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1339 return confs_noffset;
1342 if (conf_uname == NULL) {
1343 /* get configuration unit name from the default property */
1344 debug("No configuration specified, trying default...\n");
1345 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1346 FIT_DEFAULT_PROP, &len);
1347 if (conf_uname == NULL) {
1348 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1352 debug("Found default configuration: '%s'\n", conf_uname);
1355 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1357 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1358 conf_uname, fdt_strerror(noffset));
1364 int fit_conf_get_prop_node(const void *fit, int noffset,
1365 const char *prop_name)
1370 /* get kernel image unit name from configuration kernel property */
1371 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1375 return fit_image_get_node(fit, uname);
1379 * fit_conf_print - prints out the FIT configuration details
1380 * @fit: pointer to the FIT format image header
1381 * @noffset: offset of the configuration node
1382 * @p: pointer to prefix string
1384 * fit_conf_print() lists all mandatory properies for the processed
1385 * configuration node.
1388 * no returned results
1390 void fit_conf_print(const void *fit, int noffset, const char *p)
1396 /* Mandatory properties */
1397 ret = fit_get_desc(fit, noffset, &desc);
1398 printf("%s Description: ", p);
1400 printf("unavailable\n");
1402 printf("%s\n", desc);
1404 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1405 printf("%s Kernel: ", p);
1407 printf("unavailable\n");
1409 printf("%s\n", uname);
1411 /* Optional properties */
1412 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1414 printf("%s Init Ramdisk: %s\n", p, uname);
1416 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1418 printf("%s FDT: %s\n", p, uname);
1421 int fit_image_select(const void *fit, int rd_noffset, int verify)
1423 fit_image_print(fit, rd_noffset, " ");
1426 puts(" Verifying Hash Integrity ... ");
1427 if (!fit_image_verify(fit, rd_noffset)) {
1428 puts("Bad Data Hash\n");
1437 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1444 debug("* %s: using config '%s' from image at 0x%08lx\n",
1445 prop_name, images->fit_uname_cfg, addr);
1447 /* Check whether configuration has this property defined */
1448 fit_hdr = map_sysmem(addr, 0);
1449 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1450 if (cfg_noffset < 0) {
1451 debug("* %s: no such config\n", prop_name);
1455 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1457 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1464 int fit_image_load(bootm_headers_t *images, const char *prop_name, ulong addr,
1465 const char **fit_unamep, const char **fit_uname_configp,
1466 int arch, int image_type, int bootstage_id,
1467 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1469 int cfg_noffset, noffset;
1470 const char *fit_uname;
1471 const char *fit_uname_config;
1476 ulong load, data, len;
1479 fit = map_sysmem(addr, 0);
1480 fit_uname = fit_unamep ? *fit_unamep : NULL;
1481 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1482 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1484 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1485 if (!fit_check_format(fit)) {
1486 printf("Bad FIT %s image format!\n", prop_name);
1487 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1490 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1492 /* get ramdisk component image node offset */
1493 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1494 noffset = fit_image_get_node(fit, fit_uname);
1497 * no image node unit name, try to get config
1498 * node first. If config unit node name is NULL
1499 * fit_conf_get_node() will try to find default config node
1501 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1502 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1503 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1505 cfg_noffset = fit_conf_get_node(fit,
1508 if (cfg_noffset < 0) {
1509 puts("Could not find configuration node\n");
1510 bootstage_error(bootstage_id +
1511 BOOTSTAGE_SUB_NO_UNIT_NAME);
1514 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1515 printf(" Using '%s' configuration\n", fit_uname_config);
1516 if (image_type == IH_TYPE_KERNEL) {
1517 /* Remember (and possibly verify) this config */
1518 images->fit_uname_cfg = fit_uname_config;
1519 if (IMAGE_ENABLE_VERIFY && images->verify) {
1520 puts(" Verifying Hash Integrity ... ");
1521 if (!fit_config_verify(fit, cfg_noffset)) {
1522 puts("Bad Data Hash\n");
1523 bootstage_error(bootstage_id +
1524 BOOTSTAGE_SUB_HASH);
1529 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1532 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1534 fit_uname = fit_get_name(fit, noffset, NULL);
1537 puts("Could not find subimage node\n");
1538 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1542 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1544 ret = fit_image_select(fit, noffset, images->verify);
1546 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1550 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1551 if (!fit_image_check_target_arch(fit, noffset)) {
1552 puts("Unsupported Architecture\n");
1553 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1557 if (image_type == IH_TYPE_FLATDT &&
1558 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1559 puts("FDT image is compressed");
1560 return -EPROTONOSUPPORT;
1563 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1564 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1565 (image_type == IH_TYPE_KERNEL &&
1566 fit_image_check_type(fit, noffset,
1567 IH_TYPE_KERNEL_NOLOAD));
1568 os_ok = image_type == IH_TYPE_FLATDT ||
1569 fit_image_check_os(fit, noffset, IH_OS_LINUX);
1570 if (!type_ok || !os_ok) {
1571 printf("No Linux %s %s Image\n", genimg_get_arch_name(arch),
1572 genimg_get_type_name(image_type));
1573 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1577 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1579 /* get image data address and length */
1580 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1581 printf("Could not find %s subimage data!\n", prop_name);
1582 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1587 /* verify that image data is a proper FDT blob */
1588 if (image_type == IH_TYPE_FLATDT && fdt_check_header((char *)buf)) {
1589 puts("Subimage data is not a FDT");
1593 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1596 * Work-around for eldk-4.2 which gives this warning if we try to
1597 * case in the unmap_sysmem() call:
1598 * warning: initialization discards qualifiers from pointer target type
1601 void *vbuf = (void *)buf;
1603 data = map_to_sysmem(vbuf);
1606 if (load_op == FIT_LOAD_IGNORED) {
1608 } else if (fit_image_get_load(fit, noffset, &load)) {
1609 if (load_op == FIT_LOAD_REQUIRED) {
1610 printf("Can't get %s subimage load address!\n",
1612 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1616 ulong image_start, image_end;
1621 * move image data to the load address,
1622 * make sure we don't overwrite initial image
1625 image_end = addr + fit_get_size(fit);
1627 load_end = load + len;
1628 if (image_type != IH_TYPE_KERNEL &&
1629 load < image_end && load_end > image_start) {
1630 printf("Error: %s overwritten\n", prop_name);
1634 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1635 prop_name, data, load);
1637 dst = map_sysmem(load, len);
1638 memmove(dst, buf, len);
1641 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1646 *fit_unamep = (char *)fit_uname;
1647 if (fit_uname_configp)
1648 *fit_uname_configp = (char *)fit_uname_config;