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 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 #endif /* !USE_HOSTCC*/
36 #include <bootstage.h>
38 #include <u-boot/crc.h>
39 #include <u-boot/md5.h>
41 /*****************************************************************************/
42 /* New uImage format routines */
43 /*****************************************************************************/
45 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
46 ulong *addr, const char **name)
53 sep = strchr(spec, sepc);
56 *addr = simple_strtoul(spec, NULL, 16);
66 * fit_parse_conf - parse FIT configuration spec
67 * @spec: input string, containing configuration spec
68 * @add_curr: current image address (to be used as a possible default)
69 * @addr: pointer to a ulong variable, will hold FIT image address of a given
71 * @conf_name double pointer to a char, will hold pointer to a configuration
74 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
75 * where <addr> is a FIT image address that contains configuration
76 * with a <conf> unit name.
78 * Address part is optional, and if omitted default add_curr will
82 * 1 if spec is a valid configuration string,
83 * addr and conf_name are set accordingly
86 int fit_parse_conf(const char *spec, ulong addr_curr,
87 ulong *addr, const char **conf_name)
89 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
93 * fit_parse_subimage - parse FIT subimage spec
94 * @spec: input string, containing subimage spec
95 * @add_curr: current image address (to be used as a possible default)
96 * @addr: pointer to a ulong variable, will hold FIT image address of a given
98 * @image_name: double pointer to a char, will hold pointer to a subimage name
100 * fit_parse_subimage() expects subimage spec in the for of
101 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
102 * subimage with a <subimg> unit name.
104 * Address part is optional, and if omitted default add_curr will
108 * 1 if spec is a valid subimage string,
109 * addr and image_name are set accordingly
112 int fit_parse_subimage(const char *spec, ulong addr_curr,
113 ulong *addr, const char **image_name)
115 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
117 #endif /* !USE_HOSTCC */
119 static void fit_get_debug(const void *fit, int noffset,
120 char *prop_name, int err)
122 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
123 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
128 * fit_print_contents - prints out the contents of the FIT format image
129 * @fit: pointer to the FIT format image header
130 * @p: pointer to prefix string
132 * fit_print_contents() formats a multi line FIT image contents description.
133 * The routine prints out FIT image properties (root node level) follwed by
134 * the details of each component image.
137 * no returned results
139 void fit_print_contents(const void *fit)
158 /* Root node properties */
159 ret = fit_get_desc(fit, 0, &desc);
160 printf("%sFIT description: ", p);
162 printf("unavailable\n");
164 printf("%s\n", desc);
166 if (IMAGE_ENABLE_TIMESTAMP) {
167 ret = fit_get_timestamp(fit, 0, ×tamp);
168 printf("%sCreated: ", p);
170 printf("unavailable\n");
172 genimg_print_time(timestamp);
175 /* Find images parent node offset */
176 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
177 if (images_noffset < 0) {
178 printf("Can't find images parent node '%s' (%s)\n",
179 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
183 /* Process its subnodes, print out component images details */
184 for (ndepth = 0, count = 0,
185 noffset = fdt_next_node(fit, images_noffset, &ndepth);
186 (noffset >= 0) && (ndepth > 0);
187 noffset = fdt_next_node(fit, noffset, &ndepth)) {
190 * Direct child node of the images parent node,
191 * i.e. component image node.
193 printf("%s Image %u (%s)\n", p, count++,
194 fit_get_name(fit, noffset, NULL));
196 fit_image_print(fit, noffset, p);
200 /* Find configurations parent node offset */
201 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
202 if (confs_noffset < 0) {
203 debug("Can't get configurations parent node '%s' (%s)\n",
204 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
208 /* get default configuration unit name from default property */
209 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
211 printf("%s Default Configuration: '%s'\n", p, uname);
213 /* Process its subnodes, print out configurations details */
214 for (ndepth = 0, count = 0,
215 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
216 (noffset >= 0) && (ndepth > 0);
217 noffset = fdt_next_node(fit, noffset, &ndepth)) {
220 * Direct child node of the configurations parent node,
221 * i.e. configuration node.
223 printf("%s Configuration %u (%s)\n", p, count++,
224 fit_get_name(fit, noffset, NULL));
226 fit_conf_print(fit, noffset, p);
232 * fit_image_print - prints out the FIT component image details
233 * @fit: pointer to the FIT format image header
234 * @image_noffset: offset of the component image node
235 * @p: pointer to prefix string
237 * fit_image_print() lists all mandatory properies for the processed component
238 * image. If present, hash nodes are printed out as well. Load
239 * address for images of type firmware is also printed out. Since the load
240 * address is not mandatory for firmware images, it will be output as
241 * "unavailable" when not present.
244 * no returned results
246 void fit_image_print(const void *fit, int image_noffset, const char *p)
249 uint8_t type, arch, os, comp;
257 /* Mandatory properties */
258 ret = fit_get_desc(fit, image_noffset, &desc);
259 printf("%s Description: ", p);
261 printf("unavailable\n");
263 printf("%s\n", desc);
265 fit_image_get_type(fit, image_noffset, &type);
266 printf("%s Type: %s\n", p, genimg_get_type_name(type));
268 fit_image_get_comp(fit, image_noffset, &comp);
269 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
271 ret = fit_image_get_data(fit, image_noffset, &data, &size);
274 printf("%s Data Start: ", p);
276 printf("unavailable\n");
278 printf("0x%08lx\n", (ulong)data);
281 printf("%s Data Size: ", p);
283 printf("unavailable\n");
285 genimg_print_size(size);
287 /* Remaining, type dependent properties */
288 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
289 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
290 (type == IH_TYPE_FLATDT)) {
291 fit_image_get_arch(fit, image_noffset, &arch);
292 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
295 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
296 fit_image_get_os(fit, image_noffset, &os);
297 printf("%s OS: %s\n", p, genimg_get_os_name(os));
300 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
301 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
302 ret = fit_image_get_load(fit, image_noffset, &load);
303 printf("%s Load Address: ", p);
305 printf("unavailable\n");
307 printf("0x%08lx\n", load);
310 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
311 (type == IH_TYPE_RAMDISK)) {
312 fit_image_get_entry(fit, image_noffset, &entry);
313 printf("%s Entry Point: ", p);
315 printf("unavailable\n");
317 printf("0x%08lx\n", entry);
320 /* Process all hash subnodes of the component image node */
321 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
322 (noffset >= 0) && (ndepth > 0);
323 noffset = fdt_next_node(fit, noffset, &ndepth)) {
325 /* Direct child node of the component image node */
326 fit_image_print_hash(fit, noffset, p);
332 * fit_image_print_hash - prints out the hash node details
333 * @fit: pointer to the FIT format image header
334 * @noffset: offset of the hash node
335 * @p: pointer to prefix string
337 * fit_image_print_hash() lists properies for the processed hash node
340 * no returned results
342 void fit_image_print_hash(const void *fit, int noffset, const char *p)
350 * Check subnode name, must be equal to "hash".
351 * Multiple hash nodes require unique unit node
352 * names, e.g. hash@1, hash@2, etc.
354 if (strncmp(fit_get_name(fit, noffset, NULL),
355 FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME)) != 0)
358 debug("%s Hash node: '%s'\n", p,
359 fit_get_name(fit, noffset, NULL));
361 printf("%s Hash algo: ", p);
362 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
363 printf("invalid/unsupported\n");
366 printf("%s\n", algo);
368 ret = fit_image_hash_get_value(fit, noffset, &value,
370 printf("%s Hash value: ", p);
372 printf("unavailable\n");
374 for (i = 0; i < value_len; i++)
375 printf("%02x", value[i]);
379 debug("%s Hash len: %d\n", p, value_len);
383 * fit_get_desc - get node description property
384 * @fit: pointer to the FIT format image header
385 * @noffset: node offset
386 * @desc: double pointer to the char, will hold pointer to the descrption
388 * fit_get_desc() reads description property from a given node, if
389 * description is found pointer to it is returened in third call argument.
395 int fit_get_desc(const void *fit, int noffset, char **desc)
399 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
401 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
409 * fit_get_timestamp - get node timestamp property
410 * @fit: pointer to the FIT format image header
411 * @noffset: node offset
412 * @timestamp: pointer to the time_t, will hold read timestamp
414 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
415 * is found and has a correct size its value is retured in third call
420 * -1, on property read failure
421 * -2, on wrong timestamp size
423 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
428 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
430 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
433 if (len != sizeof(uint32_t)) {
434 debug("FIT timestamp with incorrect size of (%u)\n", len);
438 *timestamp = uimage_to_cpu(*((uint32_t *)data));
443 * fit_image_get_node - get node offset for component image of a given unit name
444 * @fit: pointer to the FIT format image header
445 * @image_uname: component image node unit name
447 * fit_image_get_node() finds a component image (withing the '/images'
448 * node) of a provided unit name. If image is found its node offset is
449 * returned to the caller.
452 * image node offset when found (>=0)
453 * negative number on failure (FDT_ERR_* code)
455 int fit_image_get_node(const void *fit, const char *image_uname)
457 int noffset, images_noffset;
459 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
460 if (images_noffset < 0) {
461 debug("Can't find images parent node '%s' (%s)\n",
462 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
463 return images_noffset;
466 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
468 debug("Can't get node offset for image unit name: '%s' (%s)\n",
469 image_uname, fdt_strerror(noffset));
476 * fit_image_get_os - get os id for a given component image node
477 * @fit: pointer to the FIT format image header
478 * @noffset: component image node offset
479 * @os: pointer to the uint8_t, will hold os numeric id
481 * fit_image_get_os() finds os property in a given component image node.
482 * If the property is found, its (string) value is translated to the numeric
483 * id which is returned to the caller.
489 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
494 /* Get OS name from property data */
495 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
497 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
502 /* Translate OS name to id */
503 *os = genimg_get_os_id(data);
508 * fit_image_get_arch - get arch id for a given component image node
509 * @fit: pointer to the FIT format image header
510 * @noffset: component image node offset
511 * @arch: pointer to the uint8_t, will hold arch numeric id
513 * fit_image_get_arch() finds arch property in a given component image node.
514 * If the property is found, its (string) value is translated to the numeric
515 * id which is returned to the caller.
521 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
526 /* Get architecture name from property data */
527 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
529 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
534 /* Translate architecture name to id */
535 *arch = genimg_get_arch_id(data);
540 * fit_image_get_type - get type id for a given component image node
541 * @fit: pointer to the FIT format image header
542 * @noffset: component image node offset
543 * @type: pointer to the uint8_t, will hold type numeric id
545 * fit_image_get_type() finds type property in a given component image node.
546 * If the property is found, its (string) value is translated to the numeric
547 * id which is returned to the caller.
553 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
558 /* Get image type name from property data */
559 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
561 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
566 /* Translate image type name to id */
567 *type = genimg_get_type_id(data);
572 * fit_image_get_comp - get comp id for a given component image node
573 * @fit: pointer to the FIT format image header
574 * @noffset: component image node offset
575 * @comp: pointer to the uint8_t, will hold comp numeric id
577 * fit_image_get_comp() finds comp property in a given component image node.
578 * If the property is found, its (string) value is translated to the numeric
579 * id which is returned to the caller.
585 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
590 /* Get compression name from property data */
591 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
593 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
598 /* Translate compression name to id */
599 *comp = genimg_get_comp_id(data);
604 * fit_image_get_load() - get load addr property for given component image node
605 * @fit: pointer to the FIT format image header
606 * @noffset: component image node offset
607 * @load: pointer to the uint32_t, will hold load address
609 * fit_image_get_load() finds load address property in a given component
610 * image node. If the property is found, its value is returned to the caller.
616 int fit_image_get_load(const void *fit, int noffset, ulong *load)
619 const uint32_t *data;
621 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
623 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
627 *load = uimage_to_cpu(*data);
632 * fit_image_get_entry() - get entry point address property
633 * @fit: pointer to the FIT format image header
634 * @noffset: component image node offset
635 * @entry: pointer to the uint32_t, will hold entry point address
637 * This gets the entry point address property for a given component image
640 * fit_image_get_entry() finds entry point address property in a given
641 * component image node. If the property is found, its value is returned
648 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
651 const uint32_t *data;
653 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
655 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
659 *entry = uimage_to_cpu(*data);
664 * fit_image_get_data - get data property and its size for a given component image node
665 * @fit: pointer to the FIT format image header
666 * @noffset: component image node offset
667 * @data: double pointer to void, will hold data property's data address
668 * @size: pointer to size_t, will hold data property's data size
670 * fit_image_get_data() finds data property in a given component image node.
671 * If the property is found its data start address and size are returned to
678 int fit_image_get_data(const void *fit, int noffset,
679 const void **data, size_t *size)
683 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
685 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
695 * fit_image_hash_get_algo - get hash algorithm name
696 * @fit: pointer to the FIT format image header
697 * @noffset: hash node offset
698 * @algo: double pointer to char, will hold pointer to the algorithm name
700 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
701 * If the property is found its data start address is returned to the caller.
707 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
711 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
713 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
721 * fit_image_hash_get_value - get hash value and length
722 * @fit: pointer to the FIT format image header
723 * @noffset: hash node offset
724 * @value: double pointer to uint8_t, will hold address of a hash value data
725 * @value_len: pointer to an int, will hold hash data length
727 * fit_image_hash_get_value() finds hash value property in a given hash node.
728 * If the property is found its data start address and size are returned to
735 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
740 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
741 if (*value == NULL) {
742 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
753 * fit_image_hash_get_ignore - get hash ignore flag
754 * @fit: pointer to the FIT format image header
755 * @noffset: hash node offset
756 * @ignore: pointer to an int, will hold hash ignore flag
758 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
759 * If the property is found and non-zero, the hash algorithm is not verified by
760 * u-boot automatically.
763 * 0, on ignore not found
764 * value, on ignore found
766 int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
771 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
772 if (value == NULL || len != sizeof(int))
782 * fit_set_timestamp - set node timestamp property
783 * @fit: pointer to the FIT format image header
784 * @noffset: node offset
785 * @timestamp: timestamp value to be set
787 * fit_set_timestamp() attempts to set timestamp property in the requested
788 * node and returns operation status to the caller.
792 * -1, on property read failure
794 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
799 t = cpu_to_uimage(timestamp);
800 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
803 printf("Can't set '%s' property for '%s' node (%s)\n",
804 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
813 * calculate_hash - calculate and return hash for provided input data
814 * @data: pointer to the input data
815 * @data_len: data length
816 * @algo: requested hash algorithm
817 * @value: pointer to the char, will hold hash value data (caller must
818 * allocate enough free space)
819 * value_len: length of the calculated hash
821 * calculate_hash() computes input data hash according to the requested
823 * Resulting hash value is placed in caller provided 'value' buffer, length
824 * of the calculated hash is returned via value_len pointer argument.
828 * -1, when algo is unsupported
830 static int calculate_hash(const void *data, int data_len, const char *algo,
831 uint8_t *value, int *value_len)
833 if (strcmp(algo, "crc32") == 0) {
834 *((uint32_t *)value) = crc32_wd(0, data, data_len,
836 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
838 } else if (strcmp(algo, "sha1") == 0) {
839 sha1_csum_wd((unsigned char *)data, data_len,
840 (unsigned char *)value, CHUNKSZ_SHA1);
842 } else if (strcmp(algo, "md5") == 0) {
843 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
846 debug("Unsupported hash alogrithm\n");
854 * fit_set_hashes - process FIT component image nodes and calculate hashes
855 * @fit: pointer to the FIT format image header
857 * fit_set_hashes() adds hash values for all component images in the FIT blob.
858 * Hashes are calculated for all component images which have hash subnodes
859 * with algorithm property set to one of the supported hash algorithms.
863 * libfdt error code, on failure
865 int fit_set_hashes(void *fit)
872 /* Find images parent node offset */
873 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
874 if (images_noffset < 0) {
875 printf("Can't find images parent node '%s' (%s)\n",
876 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
877 return images_noffset;
880 /* Process its subnodes, print out component images details */
881 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
882 (noffset >= 0) && (ndepth > 0);
883 noffset = fdt_next_node(fit, noffset, &ndepth)) {
886 * Direct child node of the images parent node,
887 * i.e. component image node.
889 ret = fit_image_set_hashes(fit, noffset);
899 * fit_image_set_hashes - calculate/set hashes for given component image node
900 * @fit: pointer to the FIT format image header
901 * @image_noffset: requested component image node
903 * fit_image_set_hashes() adds hash values for an component image node. All
904 * existing hash subnodes are checked, if algorithm property is set to one of
905 * the supported hash algorithms, hash value is computed and corresponding
906 * hash node property is set, for example:
908 * Input component image node structure:
910 * o image@1 (at image_noffset)
911 * | - data = [binary data]
915 * Output component image node structure:
917 * o image@1 (at image_noffset)
918 * | - data = [binary data]
921 * |- value = sha1(data)
927 int fit_image_set_hashes(void *fit, int image_noffset)
932 uint8_t value[FIT_MAX_HASH_LEN];
937 /* Get image data and data length */
938 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
939 printf("Can't get image data/size\n");
943 /* Process all hash subnodes of the component image node */
944 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
945 (noffset >= 0) && (ndepth > 0);
946 noffset = fdt_next_node(fit, noffset, &ndepth)) {
948 /* Direct child node of the component image node */
951 * Check subnode name, must be equal to "hash".
952 * Multiple hash nodes require unique unit node
953 * names, e.g. hash@1, hash@2, etc.
955 if (strncmp(fit_get_name(fit, noffset, NULL),
957 strlen(FIT_HASH_NODENAME)) != 0) {
958 /* Not a hash subnode, skip it */
962 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
963 printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
964 fit_get_name(fit, noffset, NULL),
965 fit_get_name(fit, image_noffset, NULL));
969 if (calculate_hash(data, size, algo, value,
971 printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
972 algo, fit_get_name(fit, noffset, NULL),
973 fit_get_name(fit, image_noffset, NULL));
977 if (fit_image_hash_set_value(fit, noffset, value,
979 printf("Can't set hash value for '%s' hash node in '%s' image node\n",
980 fit_get_name(fit, noffset, NULL),
981 fit_get_name(fit, image_noffset, NULL));
991 * fit_image_hash_set_value - set hash value in requested has node
992 * @fit: pointer to the FIT format image header
993 * @noffset: hash node offset
994 * @value: hash value to be set
995 * @value_len: hash value length
997 * fit_image_hash_set_value() attempts to set hash value in a node at offset
998 * given and returns operation status to the caller.
1004 int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
1009 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
1011 printf("Can't set hash '%s' property for '%s' node(%s)\n",
1012 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
1019 #endif /* USE_HOSTCC */
1022 * fit_image_check_hashes - verify data intergity
1023 * @fit: pointer to the FIT format image header
1024 * @image_noffset: component image node offset
1026 * fit_image_check_hashes() goes over component image hash nodes,
1027 * re-calculates each data hash and compares with the value stored in hash
1031 * 1, if all hashes are valid
1032 * 0, otherwise (or on error)
1034 int fit_image_check_hashes(const void *fit, int image_noffset)
1044 uint8_t value[FIT_MAX_HASH_LEN];
1050 /* Get image data and data length */
1051 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1052 printf("Can't get image data/size\n");
1056 /* Process all hash subnodes of the component image node */
1057 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
1058 (noffset >= 0) && (ndepth > 0);
1059 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1061 /* Direct child node of the component image node */
1064 * Check subnode name, must be equal to "hash".
1065 * Multiple hash nodes require unique unit node
1066 * names, e.g. hash@1, hash@2, etc.
1068 if (strncmp(fit_get_name(fit, noffset, NULL),
1070 strlen(FIT_HASH_NODENAME)) != 0)
1073 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1074 err_msg = " error!\nCan't get hash algo property";
1080 fit_image_hash_get_ignore(fit, noffset, &ignore);
1082 printf("-skipped ");
1087 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1089 err_msg = " error!\nCan't get hash value "
1094 if (calculate_hash(data, size, algo, value,
1096 err_msg = " error!\n"
1097 "Unsupported hash algorithm";
1101 if (value_len != fit_value_len) {
1102 err_msg = " error !\nBad hash value len";
1104 } else if (memcmp(value, fit_value, value_len) != 0) {
1105 err_msg = " error!\nBad hash value";
1112 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1113 err_msg = " error!\nCorrupted or truncated tree";
1120 printf("%s for '%s' hash node in '%s' image node\n",
1121 err_msg, fit_get_name(fit, noffset, NULL),
1122 fit_get_name(fit, image_noffset, NULL));
1127 * fit_all_image_check_hashes - verify data intergity for all images
1128 * @fit: pointer to the FIT format image header
1130 * fit_all_image_check_hashes() goes over all images in the FIT and
1131 * for every images checks if all it's hashes are valid.
1134 * 1, if all hashes of all images are valid
1135 * 0, otherwise (or on error)
1137 int fit_all_image_check_hashes(const void *fit)
1144 /* Find images parent node offset */
1145 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1146 if (images_noffset < 0) {
1147 printf("Can't find images parent node '%s' (%s)\n",
1148 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1152 /* Process all image subnodes, check hashes for each */
1153 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1155 for (ndepth = 0, count = 0,
1156 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1157 (noffset >= 0) && (ndepth > 0);
1158 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1161 * Direct child node of the images parent node,
1162 * i.e. component image node.
1164 printf(" Hash(es) for Image %u (%s): ", count++,
1165 fit_get_name(fit, noffset, NULL));
1167 if (!fit_image_check_hashes(fit, noffset))
1176 * fit_image_check_os - check whether image node is of a given os type
1177 * @fit: pointer to the FIT format image header
1178 * @noffset: component image node offset
1179 * @os: requested image os
1181 * fit_image_check_os() reads image os property and compares its numeric
1182 * id with the requested os. Comparison result is returned to the caller.
1185 * 1 if image is of given os type
1186 * 0 otherwise (or on error)
1188 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1192 if (fit_image_get_os(fit, noffset, &image_os))
1194 return (os == image_os);
1198 * fit_image_check_arch - check whether image node is of a given arch
1199 * @fit: pointer to the FIT format image header
1200 * @noffset: component image node offset
1201 * @arch: requested imagearch
1203 * fit_image_check_arch() reads image arch property and compares its numeric
1204 * id with the requested arch. Comparison result is returned to the caller.
1207 * 1 if image is of given arch
1208 * 0 otherwise (or on error)
1210 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1214 if (fit_image_get_arch(fit, noffset, &image_arch))
1216 return (arch == image_arch);
1220 * fit_image_check_type - check whether image node is of a given type
1221 * @fit: pointer to the FIT format image header
1222 * @noffset: component image node offset
1223 * @type: requested image type
1225 * fit_image_check_type() reads image type property and compares its numeric
1226 * id with the requested type. Comparison result is returned to the caller.
1229 * 1 if image is of given type
1230 * 0 otherwise (or on error)
1232 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1236 if (fit_image_get_type(fit, noffset, &image_type))
1238 return (type == image_type);
1242 * fit_image_check_comp - check whether image node uses given compression
1243 * @fit: pointer to the FIT format image header
1244 * @noffset: component image node offset
1245 * @comp: requested image compression type
1247 * fit_image_check_comp() reads image compression property and compares its
1248 * numeric id with the requested compression type. Comparison result is
1249 * returned to the caller.
1252 * 1 if image uses requested compression
1253 * 0 otherwise (or on error)
1255 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1259 if (fit_image_get_comp(fit, noffset, &image_comp))
1261 return (comp == image_comp);
1265 * fit_check_format - sanity check FIT image format
1266 * @fit: pointer to the FIT format image header
1268 * fit_check_format() runs a basic sanity FIT image verification.
1269 * Routine checks for mandatory properties, nodes, etc.
1275 int fit_check_format(const void *fit)
1277 /* mandatory / node 'description' property */
1278 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1279 debug("Wrong FIT format: no description\n");
1283 if (IMAGE_ENABLE_TIMESTAMP) {
1284 /* mandatory / node 'timestamp' property */
1285 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1286 debug("Wrong FIT format: no timestamp\n");
1291 /* mandatory subimages parent '/images' node */
1292 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1293 debug("Wrong FIT format: no images parent node\n");
1302 * fit_conf_find_compat
1303 * @fit: pointer to the FIT format image header
1304 * @fdt: pointer to the device tree to compare against
1306 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1307 * most compatible with the passed in device tree.
1316 * |-o configurations
1324 * |-compatible = "foo,bar", "bim,bam"
1327 * |-compatible = "foo,bar",
1330 * |-compatible = "bim,bam", "baz,biz"
1332 * Configuration 1 would be picked because the first string in U-Boot's
1333 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1334 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1337 * offset to the configuration to use if one was found
1340 int fit_conf_find_compat(const void *fit, const void *fdt)
1343 int noffset, confs_noffset, images_noffset;
1344 const void *fdt_compat;
1346 int best_match_offset = 0;
1347 int best_match_pos = 0;
1349 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1350 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1351 if (confs_noffset < 0 || images_noffset < 0) {
1352 debug("Can't find configurations or images nodes.\n");
1356 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1358 debug("Fdt for comparison has no \"compatible\" property.\n");
1363 * Loop over the configurations in the FIT image.
1365 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1366 (noffset >= 0) && (ndepth > 0);
1367 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1369 const char *kfdt_name;
1371 const char *cur_fdt_compat;
1379 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1381 debug("No fdt property found.\n");
1384 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1386 if (kfdt_noffset < 0) {
1387 debug("No image node named \"%s\" found.\n",
1392 * Get a pointer to this configuration's fdt.
1394 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1395 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1399 len = fdt_compat_len;
1400 cur_fdt_compat = fdt_compat;
1402 * Look for a match for each U-Boot compatibility string in
1403 * turn in this configuration's fdt.
1405 for (i = 0; len > 0 &&
1406 (!best_match_offset || best_match_pos > i); i++) {
1407 int cur_len = strlen(cur_fdt_compat) + 1;
1409 if (!fdt_node_check_compatible(kfdt, 0,
1411 best_match_offset = noffset;
1416 cur_fdt_compat += cur_len;
1419 if (!best_match_offset) {
1420 debug("No match found.\n");
1424 return best_match_offset;
1428 * fit_conf_get_node - get node offset for configuration of a given unit name
1429 * @fit: pointer to the FIT format image header
1430 * @conf_uname: configuration node unit name
1432 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1433 * parant node) of a provided unit name. If configuration is found its node
1434 * offset is returned to the caller.
1436 * When NULL is provided in second argument fit_conf_get_node() will search
1437 * for a default configuration node instead. Default configuration node unit
1438 * name is retrived from FIT_DEFAULT_PROP property of the '/configurations'
1442 * configuration node offset when found (>=0)
1443 * negative number on failure (FDT_ERR_* code)
1445 int fit_conf_get_node(const void *fit, const char *conf_uname)
1447 int noffset, confs_noffset;
1450 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1451 if (confs_noffset < 0) {
1452 debug("Can't find configurations parent node '%s' (%s)\n",
1453 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1454 return confs_noffset;
1457 if (conf_uname == NULL) {
1458 /* get configuration unit name from the default property */
1459 debug("No configuration specified, trying default...\n");
1460 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1461 FIT_DEFAULT_PROP, &len);
1462 if (conf_uname == NULL) {
1463 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1467 debug("Found default configuration: '%s'\n", conf_uname);
1470 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1472 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1473 conf_uname, fdt_strerror(noffset));
1479 static int __fit_conf_get_prop_node(const void *fit, int noffset,
1480 const char *prop_name)
1485 /* get kernel image unit name from configuration kernel property */
1486 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1490 return fit_image_get_node(fit, uname);
1494 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
1495 * a given configuration
1496 * @fit: pointer to the FIT format image header
1497 * @noffset: configuration node offset
1499 * fit_conf_get_kernel_node() retrives kernel image node unit name from
1500 * configuration FIT_KERNEL_PROP property and translates it to the node
1504 * image node offset when found (>=0)
1505 * negative number on failure (FDT_ERR_* code)
1507 int fit_conf_get_kernel_node(const void *fit, int noffset)
1509 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
1513 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
1514 * a given configuration
1515 * @fit: pointer to the FIT format image header
1516 * @noffset: configuration node offset
1518 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
1519 * configuration FIT_KERNEL_PROP property and translates it to the node
1523 * image node offset when found (>=0)
1524 * negative number on failure (FDT_ERR_* code)
1526 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
1528 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
1532 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
1533 * a given configuration
1534 * @fit: pointer to the FIT format image header
1535 * @noffset: configuration node offset
1537 * fit_conf_get_fdt_node() retrives fdt image node unit name from
1538 * configuration FIT_KERNEL_PROP property and translates it to the node
1542 * image node offset when found (>=0)
1543 * negative number on failure (FDT_ERR_* code)
1545 int fit_conf_get_fdt_node(const void *fit, int noffset)
1547 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
1551 * fit_conf_print - prints out the FIT configuration details
1552 * @fit: pointer to the FIT format image header
1553 * @noffset: offset of the configuration node
1554 * @p: pointer to prefix string
1556 * fit_conf_print() lists all mandatory properies for the processed
1557 * configuration node.
1560 * no returned results
1562 void fit_conf_print(const void *fit, int noffset, const char *p)
1568 /* Mandatory properties */
1569 ret = fit_get_desc(fit, noffset, &desc);
1570 printf("%s Description: ", p);
1572 printf("unavailable\n");
1574 printf("%s\n", desc);
1576 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1577 printf("%s Kernel: ", p);
1579 printf("unavailable\n");
1581 printf("%s\n", uname);
1583 /* Optional properties */
1584 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1586 printf("%s Init Ramdisk: %s\n", p, uname);
1588 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1590 printf("%s FDT: %s\n", p, uname);
1594 * fit_check_ramdisk - verify FIT format ramdisk subimage
1595 * @fit_hdr: pointer to the FIT ramdisk header
1596 * @rd_noffset: ramdisk subimage node offset within FIT image
1597 * @arch: requested ramdisk image architecture type
1598 * @verify: data CRC verification flag
1600 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
1601 * specified FIT image.
1608 int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
1611 fit_image_print(fit, rd_noffset, " ");
1614 puts(" Verifying Hash Integrity ... ");
1615 if (!fit_image_check_hashes(fit, rd_noffset)) {
1616 puts("Bad Data Hash\n");
1617 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
1623 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
1624 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
1625 !fit_image_check_arch(fit, rd_noffset, arch) ||
1626 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
1627 printf("No Linux %s Ramdisk Image\n",
1628 genimg_get_arch_name(arch));
1629 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
1633 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
1636 #endif /* USE_HOSTCC */