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),
127 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
129 * fit_print_contents - prints out the contents of the FIT format image
130 * @fit: pointer to the FIT format image header
131 * @p: pointer to prefix string
133 * fit_print_contents() formats a multi line FIT image contents description.
134 * The routine prints out FIT image properties (root node level) follwed by
135 * the details of each component image.
138 * no returned results
140 void fit_print_contents(const void *fit)
153 /* Indent string is defined in header image.h */
154 p = IMAGE_INDENT_STRING;
156 /* Root node properties */
157 ret = fit_get_desc(fit, 0, &desc);
158 printf("%sFIT description: ", p);
160 printf("unavailable\n");
162 printf("%s\n", desc);
164 if (IMAGE_ENABLE_TIMESTAMP) {
165 ret = fit_get_timestamp(fit, 0, ×tamp);
166 printf("%sCreated: ", p);
168 printf("unavailable\n");
170 genimg_print_time(timestamp);
173 /* Find images parent node offset */
174 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
175 if (images_noffset < 0) {
176 printf("Can't find images parent node '%s' (%s)\n",
177 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
181 /* Process its subnodes, print out component images details */
182 for (ndepth = 0, count = 0,
183 noffset = fdt_next_node(fit, images_noffset, &ndepth);
184 (noffset >= 0) && (ndepth > 0);
185 noffset = fdt_next_node(fit, noffset, &ndepth)) {
188 * Direct child node of the images parent node,
189 * i.e. component image node.
191 printf("%s Image %u (%s)\n", p, count++,
192 fit_get_name(fit, noffset, NULL));
194 fit_image_print(fit, noffset, p);
198 /* Find configurations parent node offset */
199 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
200 if (confs_noffset < 0) {
201 debug("Can't get configurations parent node '%s' (%s)\n",
202 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
206 /* get default configuration unit name from default property */
207 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
209 printf("%s Default Configuration: '%s'\n", p, uname);
211 /* Process its subnodes, print out configurations details */
212 for (ndepth = 0, count = 0,
213 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
214 (noffset >= 0) && (ndepth > 0);
215 noffset = fdt_next_node(fit, noffset, &ndepth)) {
218 * Direct child node of the configurations parent node,
219 * i.e. configuration node.
221 printf("%s Configuration %u (%s)\n", p, count++,
222 fit_get_name(fit, noffset, NULL));
224 fit_conf_print(fit, noffset, p);
230 * fit_image_print_data() - prints out the hash node details
231 * @fit: pointer to the FIT format image header
232 * @noffset: offset of the hash node
233 * @p: pointer to prefix string
235 * fit_image_print_data() lists properies for the processed hash node
238 * no returned results
240 static void fit_image_print_data(const void *fit, int noffset, const char *p)
248 * Check subnode name, must be equal to "hash".
249 * Multiple hash nodes require unique unit node
250 * names, e.g. hash@1, hash@2, etc.
252 if (strncmp(fit_get_name(fit, noffset, NULL),
254 strlen(FIT_HASH_NODENAME)) != 0)
257 debug("%s Hash node: '%s'\n", p,
258 fit_get_name(fit, noffset, NULL));
260 printf("%s Hash algo: ", p);
261 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
262 printf("invalid/unsupported\n");
265 printf("%s\n", algo);
267 ret = fit_image_hash_get_value(fit, noffset, &value,
269 printf("%s Hash value: ", p);
271 printf("unavailable\n");
273 for (i = 0; i < value_len; i++)
274 printf("%02x", value[i]);
278 debug("%s Hash len: %d\n", p, value_len);
282 * fit_image_print_verification_data() - prints out the hash/signature details
283 * @fit: pointer to the FIT format image header
284 * @noffset: offset of the hash or signature node
285 * @p: pointer to prefix string
287 * This lists properies for the processed hash node
290 * no returned results
292 static void fit_image_print_verification_data(const void *fit, int noffset,
298 * Check subnode name, must be equal to "hash" or "signature".
299 * Multiple hash/signature nodes require unique unit node
300 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
302 name = fit_get_name(fit, noffset, NULL);
303 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME)))
304 fit_image_print_data(fit, noffset, p);
308 * fit_image_print - prints out the FIT component image details
309 * @fit: pointer to the FIT format image header
310 * @image_noffset: offset of the component image node
311 * @p: pointer to prefix string
313 * fit_image_print() lists all mandatory properies for the processed component
314 * image. If present, hash nodes are printed out as well. Load
315 * address for images of type firmware is also printed out. Since the load
316 * address is not mandatory for firmware images, it will be output as
317 * "unavailable" when not present.
320 * no returned results
322 void fit_image_print(const void *fit, int image_noffset, const char *p)
325 uint8_t type, arch, os, comp;
333 /* Mandatory properties */
334 ret = fit_get_desc(fit, image_noffset, &desc);
335 printf("%s Description: ", p);
337 printf("unavailable\n");
339 printf("%s\n", desc);
341 fit_image_get_type(fit, image_noffset, &type);
342 printf("%s Type: %s\n", p, genimg_get_type_name(type));
344 fit_image_get_comp(fit, image_noffset, &comp);
345 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
347 ret = fit_image_get_data(fit, image_noffset, &data, &size);
350 printf("%s Data Start: ", p);
352 printf("unavailable\n");
354 printf("0x%08lx\n", (ulong)data);
357 printf("%s Data Size: ", p);
359 printf("unavailable\n");
361 genimg_print_size(size);
363 /* Remaining, type dependent properties */
364 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
365 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
366 (type == IH_TYPE_FLATDT)) {
367 fit_image_get_arch(fit, image_noffset, &arch);
368 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
371 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
372 fit_image_get_os(fit, image_noffset, &os);
373 printf("%s OS: %s\n", p, genimg_get_os_name(os));
376 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
377 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
378 ret = fit_image_get_load(fit, image_noffset, &load);
379 printf("%s Load Address: ", p);
381 printf("unavailable\n");
383 printf("0x%08lx\n", load);
386 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
387 (type == IH_TYPE_RAMDISK)) {
388 fit_image_get_entry(fit, image_noffset, &entry);
389 printf("%s Entry Point: ", p);
391 printf("unavailable\n");
393 printf("0x%08lx\n", entry);
396 /* Process all hash subnodes of the component image node */
397 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
398 (noffset >= 0) && (ndepth > 0);
399 noffset = fdt_next_node(fit, noffset, &ndepth)) {
401 /* Direct child node of the component image node */
402 fit_image_print_verification_data(fit, noffset, p);
409 * fit_get_desc - get node description property
410 * @fit: pointer to the FIT format image header
411 * @noffset: node offset
412 * @desc: double pointer to the char, will hold pointer to the descrption
414 * fit_get_desc() reads description property from a given node, if
415 * description is found pointer to it is returened in third call argument.
421 int fit_get_desc(const void *fit, int noffset, char **desc)
425 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
427 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
435 * fit_get_timestamp - get node timestamp property
436 * @fit: pointer to the FIT format image header
437 * @noffset: node offset
438 * @timestamp: pointer to the time_t, will hold read timestamp
440 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
441 * is found and has a correct size its value is retured in third call
446 * -1, on property read failure
447 * -2, on wrong timestamp size
449 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
454 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
456 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
459 if (len != sizeof(uint32_t)) {
460 debug("FIT timestamp with incorrect size of (%u)\n", len);
464 *timestamp = uimage_to_cpu(*((uint32_t *)data));
469 * fit_image_get_node - get node offset for component image of a given unit name
470 * @fit: pointer to the FIT format image header
471 * @image_uname: component image node unit name
473 * fit_image_get_node() finds a component image (withing the '/images'
474 * node) of a provided unit name. If image is found its node offset is
475 * returned to the caller.
478 * image node offset when found (>=0)
479 * negative number on failure (FDT_ERR_* code)
481 int fit_image_get_node(const void *fit, const char *image_uname)
483 int noffset, images_noffset;
485 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
486 if (images_noffset < 0) {
487 debug("Can't find images parent node '%s' (%s)\n",
488 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
489 return images_noffset;
492 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
494 debug("Can't get node offset for image unit name: '%s' (%s)\n",
495 image_uname, fdt_strerror(noffset));
502 * fit_image_get_os - get os id for a given component image node
503 * @fit: pointer to the FIT format image header
504 * @noffset: component image node offset
505 * @os: pointer to the uint8_t, will hold os numeric id
507 * fit_image_get_os() finds os property in a given component image node.
508 * If the property is found, its (string) value is translated to the numeric
509 * id which is returned to the caller.
515 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
520 /* Get OS name from property data */
521 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
523 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
528 /* Translate OS name to id */
529 *os = genimg_get_os_id(data);
534 * fit_image_get_arch - get arch id for a given component image node
535 * @fit: pointer to the FIT format image header
536 * @noffset: component image node offset
537 * @arch: pointer to the uint8_t, will hold arch numeric id
539 * fit_image_get_arch() finds arch property in a given component image node.
540 * If the property is found, its (string) value is translated to the numeric
541 * id which is returned to the caller.
547 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
552 /* Get architecture name from property data */
553 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
555 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
560 /* Translate architecture name to id */
561 *arch = genimg_get_arch_id(data);
566 * fit_image_get_type - get type id for a given component image node
567 * @fit: pointer to the FIT format image header
568 * @noffset: component image node offset
569 * @type: pointer to the uint8_t, will hold type numeric id
571 * fit_image_get_type() finds type property in a given component image node.
572 * If the property is found, its (string) value is translated to the numeric
573 * id which is returned to the caller.
579 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
584 /* Get image type name from property data */
585 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
587 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
592 /* Translate image type name to id */
593 *type = genimg_get_type_id(data);
598 * fit_image_get_comp - get comp id for a given component image node
599 * @fit: pointer to the FIT format image header
600 * @noffset: component image node offset
601 * @comp: pointer to the uint8_t, will hold comp numeric id
603 * fit_image_get_comp() finds comp property in a given component image node.
604 * If the property is found, its (string) value is translated to the numeric
605 * id which is returned to the caller.
611 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
616 /* Get compression name from property data */
617 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
619 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
624 /* Translate compression name to id */
625 *comp = genimg_get_comp_id(data);
630 * fit_image_get_load() - get load addr property for given component image node
631 * @fit: pointer to the FIT format image header
632 * @noffset: component image node offset
633 * @load: pointer to the uint32_t, will hold load address
635 * fit_image_get_load() finds load address property in a given component
636 * image node. If the property is found, its value is returned to the caller.
642 int fit_image_get_load(const void *fit, int noffset, ulong *load)
645 const uint32_t *data;
647 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
649 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
653 *load = uimage_to_cpu(*data);
658 * fit_image_get_entry() - get entry point address property
659 * @fit: pointer to the FIT format image header
660 * @noffset: component image node offset
661 * @entry: pointer to the uint32_t, will hold entry point address
663 * This gets the entry point address property for a given component image
666 * fit_image_get_entry() finds entry point address property in a given
667 * component image node. If the property is found, its value is returned
674 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
677 const uint32_t *data;
679 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
681 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
685 *entry = uimage_to_cpu(*data);
690 * fit_image_get_data - get data property and its size for a given component image node
691 * @fit: pointer to the FIT format image header
692 * @noffset: component image node offset
693 * @data: double pointer to void, will hold data property's data address
694 * @size: pointer to size_t, will hold data property's data size
696 * fit_image_get_data() finds data property in a given component image node.
697 * If the property is found its data start address and size are returned to
704 int fit_image_get_data(const void *fit, int noffset,
705 const void **data, size_t *size)
709 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
711 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
721 * fit_image_hash_get_algo - get hash algorithm name
722 * @fit: pointer to the FIT format image header
723 * @noffset: hash node offset
724 * @algo: double pointer to char, will hold pointer to the algorithm name
726 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
727 * If the property is found its data start address is returned to the caller.
733 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
737 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
739 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
747 * fit_image_hash_get_value - get hash value and length
748 * @fit: pointer to the FIT format image header
749 * @noffset: hash node offset
750 * @value: double pointer to uint8_t, will hold address of a hash value data
751 * @value_len: pointer to an int, will hold hash data length
753 * fit_image_hash_get_value() finds hash value property in a given hash node.
754 * If the property is found its data start address and size are returned to
761 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
766 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
767 if (*value == NULL) {
768 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
778 * fit_image_hash_get_ignore - get hash ignore flag
779 * @fit: pointer to the FIT format image header
780 * @noffset: hash node offset
781 * @ignore: pointer to an int, will hold hash ignore flag
783 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
784 * If the property is found and non-zero, the hash algorithm is not verified by
785 * u-boot automatically.
788 * 0, on ignore not found
789 * value, on ignore found
791 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
796 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
797 if (value == NULL || len != sizeof(int))
806 * fit_set_timestamp - set node timestamp property
807 * @fit: pointer to the FIT format image header
808 * @noffset: node offset
809 * @timestamp: timestamp value to be set
811 * fit_set_timestamp() attempts to set timestamp property in the requested
812 * node and returns operation status to the caller.
816 * -1, on property read failure
818 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
823 t = cpu_to_uimage(timestamp);
824 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
827 printf("Can't set '%s' property for '%s' node (%s)\n",
828 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
837 * calculate_hash - calculate and return hash for provided input data
838 * @data: pointer to the input data
839 * @data_len: data length
840 * @algo: requested hash algorithm
841 * @value: pointer to the char, will hold hash value data (caller must
842 * allocate enough free space)
843 * value_len: length of the calculated hash
845 * calculate_hash() computes input data hash according to the requested
847 * Resulting hash value is placed in caller provided 'value' buffer, length
848 * of the calculated hash is returned via value_len pointer argument.
852 * -1, when algo is unsupported
854 int calculate_hash(const void *data, int data_len, const char *algo,
855 uint8_t *value, int *value_len)
857 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
858 *((uint32_t *)value) = crc32_wd(0, data, data_len,
860 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
862 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
863 sha1_csum_wd((unsigned char *)data, data_len,
864 (unsigned char *)value, CHUNKSZ_SHA1);
866 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
867 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
870 debug("Unsupported hash alogrithm\n");
876 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
877 size_t size, char **err_msgp)
879 uint8_t value[FIT_MAX_HASH_LEN];
888 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
889 *err_msgp = "Can't get hash algo property";
894 if (IMAGE_ENABLE_IGNORE) {
895 fit_image_hash_get_ignore(fit, noffset, &ignore);
902 if (fit_image_hash_get_value(fit, noffset, &fit_value,
904 *err_msgp = "Can't get hash value property";
908 if (calculate_hash(data, size, algo, value, &value_len)) {
909 *err_msgp = "Unsupported hash algorithm";
913 if (value_len != fit_value_len) {
914 *err_msgp = "Bad hash value len";
916 } else if (memcmp(value, fit_value, value_len) != 0) {
917 *err_msgp = "Bad hash value";
925 * fit_image_verify - verify data intergity
926 * @fit: pointer to the FIT format image header
927 * @image_noffset: component image node offset
929 * fit_image_verify() goes over component image hash nodes,
930 * re-calculates each data hash and compares with the value stored in hash
934 * 1, if all hashes are valid
935 * 0, otherwise (or on error)
937 int fit_image_verify(const void *fit, int image_noffset)
944 /* Get image data and data length */
945 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
946 err_msg = "Can't get image data/size";
950 /* Process all hash subnodes of the component image node */
951 for (noffset = fdt_first_subnode(fit, image_noffset);
953 noffset = fdt_next_subnode(fit, noffset)) {
954 const char *name = fit_get_name(fit, noffset, NULL);
957 * Check subnode name, must be equal to "hash".
958 * Multiple hash nodes require unique unit node
959 * names, e.g. hash@1, hash@2, etc.
961 if (!strncmp(name, FIT_HASH_NODENAME,
962 strlen(FIT_HASH_NODENAME))) {
963 if (fit_image_check_hash(fit, noffset, data, size,
970 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
971 err_msg = "Corrupted or truncated tree";
978 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
979 err_msg, fit_get_name(fit, noffset, NULL),
980 fit_get_name(fit, image_noffset, NULL));
985 * fit_all_image_verify - verify data intergity for all images
986 * @fit: pointer to the FIT format image header
988 * fit_all_image_verify() goes over all images in the FIT and
989 * for every images checks if all it's hashes are valid.
992 * 1, if all hashes of all images are valid
993 * 0, otherwise (or on error)
995 int fit_all_image_verify(const void *fit)
1002 /* Find images parent node offset */
1003 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1004 if (images_noffset < 0) {
1005 printf("Can't find images parent node '%s' (%s)\n",
1006 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1010 /* Process all image subnodes, check hashes for each */
1011 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1013 for (ndepth = 0, count = 0,
1014 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1015 (noffset >= 0) && (ndepth > 0);
1016 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1019 * Direct child node of the images parent node,
1020 * i.e. component image node.
1022 printf(" Hash(es) for Image %u (%s): ", count++,
1023 fit_get_name(fit, noffset, NULL));
1025 if (!fit_image_verify(fit, noffset))
1034 * fit_image_check_os - check whether image node is of a given os type
1035 * @fit: pointer to the FIT format image header
1036 * @noffset: component image node offset
1037 * @os: requested image os
1039 * fit_image_check_os() reads image os property and compares its numeric
1040 * id with the requested os. Comparison result is returned to the caller.
1043 * 1 if image is of given os type
1044 * 0 otherwise (or on error)
1046 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1050 if (fit_image_get_os(fit, noffset, &image_os))
1052 return (os == image_os);
1056 * fit_image_check_arch - check whether image node is of a given arch
1057 * @fit: pointer to the FIT format image header
1058 * @noffset: component image node offset
1059 * @arch: requested imagearch
1061 * fit_image_check_arch() reads image arch property and compares its numeric
1062 * id with the requested arch. Comparison result is returned to the caller.
1065 * 1 if image is of given arch
1066 * 0 otherwise (or on error)
1068 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1072 if (fit_image_get_arch(fit, noffset, &image_arch))
1074 return (arch == image_arch);
1078 * fit_image_check_type - check whether image node is of a given type
1079 * @fit: pointer to the FIT format image header
1080 * @noffset: component image node offset
1081 * @type: requested image type
1083 * fit_image_check_type() reads image type property and compares its numeric
1084 * id with the requested type. Comparison result is returned to the caller.
1087 * 1 if image is of given type
1088 * 0 otherwise (or on error)
1090 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1094 if (fit_image_get_type(fit, noffset, &image_type))
1096 return (type == image_type);
1100 * fit_image_check_comp - check whether image node uses given compression
1101 * @fit: pointer to the FIT format image header
1102 * @noffset: component image node offset
1103 * @comp: requested image compression type
1105 * fit_image_check_comp() reads image compression property and compares its
1106 * numeric id with the requested compression type. Comparison result is
1107 * returned to the caller.
1110 * 1 if image uses requested compression
1111 * 0 otherwise (or on error)
1113 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1117 if (fit_image_get_comp(fit, noffset, &image_comp))
1119 return (comp == image_comp);
1123 * fit_check_format - sanity check FIT image format
1124 * @fit: pointer to the FIT format image header
1126 * fit_check_format() runs a basic sanity FIT image verification.
1127 * Routine checks for mandatory properties, nodes, etc.
1133 int fit_check_format(const void *fit)
1135 /* mandatory / node 'description' property */
1136 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1137 debug("Wrong FIT format: no description\n");
1141 if (IMAGE_ENABLE_TIMESTAMP) {
1142 /* mandatory / node 'timestamp' property */
1143 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1144 debug("Wrong FIT format: no timestamp\n");
1149 /* mandatory subimages parent '/images' node */
1150 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1151 debug("Wrong FIT format: no images parent node\n");
1160 * fit_conf_find_compat
1161 * @fit: pointer to the FIT format image header
1162 * @fdt: pointer to the device tree to compare against
1164 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1165 * most compatible with the passed in device tree.
1174 * |-o configurations
1182 * |-compatible = "foo,bar", "bim,bam"
1185 * |-compatible = "foo,bar",
1188 * |-compatible = "bim,bam", "baz,biz"
1190 * Configuration 1 would be picked because the first string in U-Boot's
1191 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1192 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1195 * offset to the configuration to use if one was found
1198 int fit_conf_find_compat(const void *fit, const void *fdt)
1201 int noffset, confs_noffset, images_noffset;
1202 const void *fdt_compat;
1204 int best_match_offset = 0;
1205 int best_match_pos = 0;
1207 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1208 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1209 if (confs_noffset < 0 || images_noffset < 0) {
1210 debug("Can't find configurations or images nodes.\n");
1214 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1216 debug("Fdt for comparison has no \"compatible\" property.\n");
1221 * Loop over the configurations in the FIT image.
1223 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1224 (noffset >= 0) && (ndepth > 0);
1225 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1227 const char *kfdt_name;
1229 const char *cur_fdt_compat;
1237 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1239 debug("No fdt property found.\n");
1242 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1244 if (kfdt_noffset < 0) {
1245 debug("No image node named \"%s\" found.\n",
1250 * Get a pointer to this configuration's fdt.
1252 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1253 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1257 len = fdt_compat_len;
1258 cur_fdt_compat = fdt_compat;
1260 * Look for a match for each U-Boot compatibility string in
1261 * turn in this configuration's fdt.
1263 for (i = 0; len > 0 &&
1264 (!best_match_offset || best_match_pos > i); i++) {
1265 int cur_len = strlen(cur_fdt_compat) + 1;
1267 if (!fdt_node_check_compatible(kfdt, 0,
1269 best_match_offset = noffset;
1274 cur_fdt_compat += cur_len;
1277 if (!best_match_offset) {
1278 debug("No match found.\n");
1282 return best_match_offset;
1286 * fit_conf_get_node - get node offset for configuration of a given unit name
1287 * @fit: pointer to the FIT format image header
1288 * @conf_uname: configuration node unit name
1290 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1291 * parant node) of a provided unit name. If configuration is found its node
1292 * offset is returned to the caller.
1294 * When NULL is provided in second argument fit_conf_get_node() will search
1295 * for a default configuration node instead. Default configuration node unit
1296 * name is retrived from FIT_DEFAULT_PROP property of the '/configurations'
1300 * configuration node offset when found (>=0)
1301 * negative number on failure (FDT_ERR_* code)
1303 int fit_conf_get_node(const void *fit, const char *conf_uname)
1305 int noffset, confs_noffset;
1308 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1309 if (confs_noffset < 0) {
1310 debug("Can't find configurations parent node '%s' (%s)\n",
1311 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1312 return confs_noffset;
1315 if (conf_uname == NULL) {
1316 /* get configuration unit name from the default property */
1317 debug("No configuration specified, trying default...\n");
1318 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1319 FIT_DEFAULT_PROP, &len);
1320 if (conf_uname == NULL) {
1321 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1325 debug("Found default configuration: '%s'\n", conf_uname);
1328 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1330 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1331 conf_uname, fdt_strerror(noffset));
1337 int fit_conf_get_prop_node(const void *fit, int noffset,
1338 const char *prop_name)
1343 /* get kernel image unit name from configuration kernel property */
1344 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1348 return fit_image_get_node(fit, uname);
1352 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
1353 * a given configuration
1354 * @fit: pointer to the FIT format image header
1355 * @noffset: configuration node offset
1357 * fit_conf_get_kernel_node() retrives kernel image node unit name from
1358 * configuration FIT_KERNEL_PROP property and translates it to the node
1362 * image node offset when found (>=0)
1363 * negative number on failure (FDT_ERR_* code)
1365 int fit_conf_get_kernel_node(const void *fit, int noffset)
1367 return fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
1371 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
1372 * a given configuration
1373 * @fit: pointer to the FIT format image header
1374 * @noffset: configuration node offset
1376 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
1377 * configuration FIT_KERNEL_PROP property and translates it to the node
1381 * image node offset when found (>=0)
1382 * negative number on failure (FDT_ERR_* code)
1384 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
1386 return fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
1390 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
1391 * a given configuration
1392 * @fit: pointer to the FIT format image header
1393 * @noffset: configuration node offset
1395 * fit_conf_get_fdt_node() retrives fdt image node unit name from
1396 * configuration FIT_KERNEL_PROP property and translates it to the node
1400 * image node offset when found (>=0)
1401 * negative number on failure (FDT_ERR_* code)
1403 int fit_conf_get_fdt_node(const void *fit, int noffset)
1405 return fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
1409 * fit_conf_print - prints out the FIT configuration details
1410 * @fit: pointer to the FIT format image header
1411 * @noffset: offset of the configuration node
1412 * @p: pointer to prefix string
1414 * fit_conf_print() lists all mandatory properies for the processed
1415 * configuration node.
1418 * no returned results
1420 void fit_conf_print(const void *fit, int noffset, const char *p)
1426 /* Mandatory properties */
1427 ret = fit_get_desc(fit, noffset, &desc);
1428 printf("%s Description: ", p);
1430 printf("unavailable\n");
1432 printf("%s\n", desc);
1434 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1435 printf("%s Kernel: ", p);
1437 printf("unavailable\n");
1439 printf("%s\n", uname);
1441 /* Optional properties */
1442 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1444 printf("%s Init Ramdisk: %s\n", p, uname);
1446 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1448 printf("%s FDT: %s\n", p, uname);
1452 * fit_check_ramdisk - verify FIT format ramdisk subimage
1453 * @fit_hdr: pointer to the FIT ramdisk header
1454 * @rd_noffset: ramdisk subimage node offset within FIT image
1455 * @arch: requested ramdisk image architecture type
1456 * @verify: data CRC verification flag
1458 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
1459 * specified FIT image.
1465 int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
1468 fit_image_print(fit, rd_noffset, " ");
1471 puts(" Verifying Hash Integrity ... ");
1472 if (!fit_image_verify(fit, rd_noffset)) {
1473 puts("Bad Data Hash\n");
1474 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
1480 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
1481 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
1482 !fit_image_check_arch(fit, rd_noffset, arch) ||
1483 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
1484 printf("No Linux %s Ramdisk Image\n",
1485 genimg_get_arch_name(arch));
1486 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
1490 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);