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_data() - prints out the hash node details
233 * @fit: pointer to the FIT format image header
234 * @noffset: offset of the hash node
235 * @p: pointer to prefix string
237 * fit_image_print_data() lists properies for the processed hash node
240 * no returned results
242 static void fit_image_print_data(const void *fit, int noffset, const char *p)
250 * Check subnode name, must be equal to "hash".
251 * Multiple hash nodes require unique unit node
252 * names, e.g. hash@1, hash@2, etc.
254 if (strncmp(fit_get_name(fit, noffset, NULL),
256 strlen(FIT_HASH_NODENAME)) != 0)
259 debug("%s Hash node: '%s'\n", p,
260 fit_get_name(fit, noffset, NULL));
262 printf("%s Hash algo: ", p);
263 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
264 printf("invalid/unsupported\n");
267 printf("%s\n", algo);
269 ret = fit_image_hash_get_value(fit, noffset, &value,
271 printf("%s Hash value: ", p);
273 printf("unavailable\n");
275 for (i = 0; i < value_len; i++)
276 printf("%02x", value[i]);
280 debug("%s Hash len: %d\n", p, value_len);
284 * fit_image_print_verification_data() - prints out the hash/signature details
285 * @fit: pointer to the FIT format image header
286 * @noffset: offset of the hash or signature node
287 * @p: pointer to prefix string
289 * This lists properies for the processed hash node
292 * no returned results
294 static void fit_image_print_verification_data(const void *fit, int noffset,
300 * Check subnode name, must be equal to "hash" or "signature".
301 * Multiple hash/signature nodes require unique unit node
302 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
304 name = fit_get_name(fit, noffset, NULL);
305 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME)))
306 fit_image_print_data(fit, noffset, p);
310 * fit_image_print - prints out the FIT component image details
311 * @fit: pointer to the FIT format image header
312 * @image_noffset: offset of the component image node
313 * @p: pointer to prefix string
315 * fit_image_print() lists all mandatory properies for the processed component
316 * image. If present, hash nodes are printed out as well. Load
317 * address for images of type firmware is also printed out. Since the load
318 * address is not mandatory for firmware images, it will be output as
319 * "unavailable" when not present.
322 * no returned results
324 void fit_image_print(const void *fit, int image_noffset, const char *p)
327 uint8_t type, arch, os, comp;
335 /* Mandatory properties */
336 ret = fit_get_desc(fit, image_noffset, &desc);
337 printf("%s Description: ", p);
339 printf("unavailable\n");
341 printf("%s\n", desc);
343 fit_image_get_type(fit, image_noffset, &type);
344 printf("%s Type: %s\n", p, genimg_get_type_name(type));
346 fit_image_get_comp(fit, image_noffset, &comp);
347 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
349 ret = fit_image_get_data(fit, image_noffset, &data, &size);
352 printf("%s Data Start: ", p);
354 printf("unavailable\n");
356 printf("0x%08lx\n", (ulong)data);
359 printf("%s Data Size: ", p);
361 printf("unavailable\n");
363 genimg_print_size(size);
365 /* Remaining, type dependent properties */
366 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
367 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
368 (type == IH_TYPE_FLATDT)) {
369 fit_image_get_arch(fit, image_noffset, &arch);
370 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
373 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
374 fit_image_get_os(fit, image_noffset, &os);
375 printf("%s OS: %s\n", p, genimg_get_os_name(os));
378 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
379 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
380 ret = fit_image_get_load(fit, image_noffset, &load);
381 printf("%s Load Address: ", p);
383 printf("unavailable\n");
385 printf("0x%08lx\n", load);
388 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
389 (type == IH_TYPE_RAMDISK)) {
390 fit_image_get_entry(fit, image_noffset, &entry);
391 printf("%s Entry Point: ", p);
393 printf("unavailable\n");
395 printf("0x%08lx\n", entry);
398 /* Process all hash subnodes of the component image node */
399 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
400 (noffset >= 0) && (ndepth > 0);
401 noffset = fdt_next_node(fit, noffset, &ndepth)) {
403 /* Direct child node of the component image node */
404 fit_image_print_verification_data(fit, noffset, p);
410 * fit_get_desc - get node description property
411 * @fit: pointer to the FIT format image header
412 * @noffset: node offset
413 * @desc: double pointer to the char, will hold pointer to the descrption
415 * fit_get_desc() reads description property from a given node, if
416 * description is found pointer to it is returened in third call argument.
422 int fit_get_desc(const void *fit, int noffset, char **desc)
426 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
428 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
436 * fit_get_timestamp - get node timestamp property
437 * @fit: pointer to the FIT format image header
438 * @noffset: node offset
439 * @timestamp: pointer to the time_t, will hold read timestamp
441 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
442 * is found and has a correct size its value is retured in third call
447 * -1, on property read failure
448 * -2, on wrong timestamp size
450 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
455 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
457 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
460 if (len != sizeof(uint32_t)) {
461 debug("FIT timestamp with incorrect size of (%u)\n", len);
465 *timestamp = uimage_to_cpu(*((uint32_t *)data));
470 * fit_image_get_node - get node offset for component image of a given unit name
471 * @fit: pointer to the FIT format image header
472 * @image_uname: component image node unit name
474 * fit_image_get_node() finds a component image (withing the '/images'
475 * node) of a provided unit name. If image is found its node offset is
476 * returned to the caller.
479 * image node offset when found (>=0)
480 * negative number on failure (FDT_ERR_* code)
482 int fit_image_get_node(const void *fit, const char *image_uname)
484 int noffset, images_noffset;
486 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
487 if (images_noffset < 0) {
488 debug("Can't find images parent node '%s' (%s)\n",
489 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
490 return images_noffset;
493 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
495 debug("Can't get node offset for image unit name: '%s' (%s)\n",
496 image_uname, fdt_strerror(noffset));
503 * fit_image_get_os - get os id for a given component image node
504 * @fit: pointer to the FIT format image header
505 * @noffset: component image node offset
506 * @os: pointer to the uint8_t, will hold os numeric id
508 * fit_image_get_os() finds os property in a given component image node.
509 * If the property is found, its (string) value is translated to the numeric
510 * id which is returned to the caller.
516 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
521 /* Get OS name from property data */
522 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
524 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
529 /* Translate OS name to id */
530 *os = genimg_get_os_id(data);
535 * fit_image_get_arch - get arch id for a given component image node
536 * @fit: pointer to the FIT format image header
537 * @noffset: component image node offset
538 * @arch: pointer to the uint8_t, will hold arch numeric id
540 * fit_image_get_arch() finds arch property in a given component image node.
541 * If the property is found, its (string) value is translated to the numeric
542 * id which is returned to the caller.
548 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
553 /* Get architecture name from property data */
554 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
556 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
561 /* Translate architecture name to id */
562 *arch = genimg_get_arch_id(data);
567 * fit_image_get_type - get type id for a given component image node
568 * @fit: pointer to the FIT format image header
569 * @noffset: component image node offset
570 * @type: pointer to the uint8_t, will hold type numeric id
572 * fit_image_get_type() finds type property in a given component image node.
573 * If the property is found, its (string) value is translated to the numeric
574 * id which is returned to the caller.
580 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
585 /* Get image type name from property data */
586 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
588 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
593 /* Translate image type name to id */
594 *type = genimg_get_type_id(data);
599 * fit_image_get_comp - get comp id for a given component image node
600 * @fit: pointer to the FIT format image header
601 * @noffset: component image node offset
602 * @comp: pointer to the uint8_t, will hold comp numeric id
604 * fit_image_get_comp() finds comp property in a given component image node.
605 * If the property is found, its (string) value is translated to the numeric
606 * id which is returned to the caller.
612 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
617 /* Get compression name from property data */
618 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
620 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
625 /* Translate compression name to id */
626 *comp = genimg_get_comp_id(data);
631 * fit_image_get_load() - get load addr property for given component image node
632 * @fit: pointer to the FIT format image header
633 * @noffset: component image node offset
634 * @load: pointer to the uint32_t, will hold load address
636 * fit_image_get_load() finds load address property in a given component
637 * image node. If the property is found, its value is returned to the caller.
643 int fit_image_get_load(const void *fit, int noffset, ulong *load)
646 const uint32_t *data;
648 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
650 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
654 *load = uimage_to_cpu(*data);
659 * fit_image_get_entry() - get entry point address property
660 * @fit: pointer to the FIT format image header
661 * @noffset: component image node offset
662 * @entry: pointer to the uint32_t, will hold entry point address
664 * This gets the entry point address property for a given component image
667 * fit_image_get_entry() finds entry point address property in a given
668 * component image node. If the property is found, its value is returned
675 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
678 const uint32_t *data;
680 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
682 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
686 *entry = uimage_to_cpu(*data);
691 * fit_image_get_data - get data property and its size for a given component image node
692 * @fit: pointer to the FIT format image header
693 * @noffset: component image node offset
694 * @data: double pointer to void, will hold data property's data address
695 * @size: pointer to size_t, will hold data property's data size
697 * fit_image_get_data() finds data property in a given component image node.
698 * If the property is found its data start address and size are returned to
705 int fit_image_get_data(const void *fit, int noffset,
706 const void **data, size_t *size)
710 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
712 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
722 * fit_image_hash_get_algo - get hash algorithm name
723 * @fit: pointer to the FIT format image header
724 * @noffset: hash node offset
725 * @algo: double pointer to char, will hold pointer to the algorithm name
727 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
728 * If the property is found its data start address is returned to the caller.
734 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
738 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
740 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
748 * fit_image_hash_get_value - get hash value and length
749 * @fit: pointer to the FIT format image header
750 * @noffset: hash node offset
751 * @value: double pointer to uint8_t, will hold address of a hash value data
752 * @value_len: pointer to an int, will hold hash data length
754 * fit_image_hash_get_value() finds hash value property in a given hash node.
755 * If the property is found its data start address and size are returned to
762 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
767 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
768 if (*value == NULL) {
769 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
779 * fit_image_hash_get_ignore - get hash ignore flag
780 * @fit: pointer to the FIT format image header
781 * @noffset: hash node offset
782 * @ignore: pointer to an int, will hold hash ignore flag
784 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
785 * If the property is found and non-zero, the hash algorithm is not verified by
786 * u-boot automatically.
789 * 0, on ignore not found
790 * value, on ignore found
792 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
797 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
798 if (value == NULL || len != sizeof(int))
807 * fit_set_timestamp - set node timestamp property
808 * @fit: pointer to the FIT format image header
809 * @noffset: node offset
810 * @timestamp: timestamp value to be set
812 * fit_set_timestamp() attempts to set timestamp property in the requested
813 * node and returns operation status to the caller.
817 * -1, on property read failure
819 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
824 t = cpu_to_uimage(timestamp);
825 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
828 printf("Can't set '%s' property for '%s' node (%s)\n",
829 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
838 * calculate_hash - calculate and return hash for provided input data
839 * @data: pointer to the input data
840 * @data_len: data length
841 * @algo: requested hash algorithm
842 * @value: pointer to the char, will hold hash value data (caller must
843 * allocate enough free space)
844 * value_len: length of the calculated hash
846 * calculate_hash() computes input data hash according to the requested
848 * Resulting hash value is placed in caller provided 'value' buffer, length
849 * of the calculated hash is returned via value_len pointer argument.
853 * -1, when algo is unsupported
855 int calculate_hash(const void *data, int data_len, const char *algo,
856 uint8_t *value, int *value_len)
858 if (strcmp(algo, "crc32") == 0) {
859 *((uint32_t *)value) = crc32_wd(0, data, data_len,
861 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
863 } else if (strcmp(algo, "sha1") == 0) {
864 sha1_csum_wd((unsigned char *)data, data_len,
865 (unsigned char *)value, CHUNKSZ_SHA1);
867 } else if (strcmp(algo, "md5") == 0) {
868 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
871 debug("Unsupported hash alogrithm\n");
877 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
878 size_t size, char **err_msgp)
880 uint8_t value[FIT_MAX_HASH_LEN];
889 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
890 *err_msgp = "Can't get hash algo property";
895 if (IMAGE_ENABLE_IGNORE) {
896 fit_image_hash_get_ignore(fit, noffset, &ignore);
903 if (fit_image_hash_get_value(fit, noffset, &fit_value,
905 *err_msgp = "Can't get hash value property";
909 if (calculate_hash(data, size, algo, value, &value_len)) {
910 *err_msgp = "Unsupported hash algorithm";
914 if (value_len != fit_value_len) {
915 *err_msgp = "Bad hash value len";
917 } else if (memcmp(value, fit_value, value_len) != 0) {
918 *err_msgp = "Bad hash value";
926 * fit_image_verify - verify data intergity
927 * @fit: pointer to the FIT format image header
928 * @image_noffset: component image node offset
930 * fit_image_verify() goes over component image hash nodes,
931 * re-calculates each data hash and compares with the value stored in hash
935 * 1, if all hashes are valid
936 * 0, otherwise (or on error)
938 int fit_image_verify(const void *fit, int image_noffset)
945 /* Get image data and data length */
946 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
947 err_msg = "Can't get image data/size";
951 /* Process all hash subnodes of the component image node */
952 for (noffset = fdt_first_subnode(fit, image_noffset);
954 noffset = fdt_next_subnode(fit, noffset)) {
955 const char *name = fit_get_name(fit, noffset, NULL);
958 * Check subnode name, must be equal to "hash".
959 * Multiple hash nodes require unique unit node
960 * names, e.g. hash@1, hash@2, etc.
962 if (!strncmp(name, FIT_HASH_NODENAME,
963 strlen(FIT_HASH_NODENAME))) {
964 if (fit_image_check_hash(fit, noffset, data, size,
971 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
972 err_msg = "Corrupted or truncated tree";
979 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
980 err_msg, fit_get_name(fit, noffset, NULL),
981 fit_get_name(fit, image_noffset, NULL));
986 * fit_all_image_verify - verify data intergity for all images
987 * @fit: pointer to the FIT format image header
989 * fit_all_image_verify() goes over all images in the FIT and
990 * for every images checks if all it's hashes are valid.
993 * 1, if all hashes of all images are valid
994 * 0, otherwise (or on error)
996 int fit_all_image_verify(const void *fit)
1003 /* Find images parent node offset */
1004 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1005 if (images_noffset < 0) {
1006 printf("Can't find images parent node '%s' (%s)\n",
1007 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1011 /* Process all image subnodes, check hashes for each */
1012 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1014 for (ndepth = 0, count = 0,
1015 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1016 (noffset >= 0) && (ndepth > 0);
1017 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1020 * Direct child node of the images parent node,
1021 * i.e. component image node.
1023 printf(" Hash(es) for Image %u (%s): ", count++,
1024 fit_get_name(fit, noffset, NULL));
1026 if (!fit_image_verify(fit, noffset))
1035 * fit_image_check_os - check whether image node is of a given os type
1036 * @fit: pointer to the FIT format image header
1037 * @noffset: component image node offset
1038 * @os: requested image os
1040 * fit_image_check_os() reads image os property and compares its numeric
1041 * id with the requested os. Comparison result is returned to the caller.
1044 * 1 if image is of given os type
1045 * 0 otherwise (or on error)
1047 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1051 if (fit_image_get_os(fit, noffset, &image_os))
1053 return (os == image_os);
1057 * fit_image_check_arch - check whether image node is of a given arch
1058 * @fit: pointer to the FIT format image header
1059 * @noffset: component image node offset
1060 * @arch: requested imagearch
1062 * fit_image_check_arch() reads image arch property and compares its numeric
1063 * id with the requested arch. Comparison result is returned to the caller.
1066 * 1 if image is of given arch
1067 * 0 otherwise (or on error)
1069 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1073 if (fit_image_get_arch(fit, noffset, &image_arch))
1075 return (arch == image_arch);
1079 * fit_image_check_type - check whether image node is of a given type
1080 * @fit: pointer to the FIT format image header
1081 * @noffset: component image node offset
1082 * @type: requested image type
1084 * fit_image_check_type() reads image type property and compares its numeric
1085 * id with the requested type. Comparison result is returned to the caller.
1088 * 1 if image is of given type
1089 * 0 otherwise (or on error)
1091 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1095 if (fit_image_get_type(fit, noffset, &image_type))
1097 return (type == image_type);
1101 * fit_image_check_comp - check whether image node uses given compression
1102 * @fit: pointer to the FIT format image header
1103 * @noffset: component image node offset
1104 * @comp: requested image compression type
1106 * fit_image_check_comp() reads image compression property and compares its
1107 * numeric id with the requested compression type. Comparison result is
1108 * returned to the caller.
1111 * 1 if image uses requested compression
1112 * 0 otherwise (or on error)
1114 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1118 if (fit_image_get_comp(fit, noffset, &image_comp))
1120 return (comp == image_comp);
1124 * fit_check_format - sanity check FIT image format
1125 * @fit: pointer to the FIT format image header
1127 * fit_check_format() runs a basic sanity FIT image verification.
1128 * Routine checks for mandatory properties, nodes, etc.
1134 int fit_check_format(const void *fit)
1136 /* mandatory / node 'description' property */
1137 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1138 debug("Wrong FIT format: no description\n");
1142 if (IMAGE_ENABLE_TIMESTAMP) {
1143 /* mandatory / node 'timestamp' property */
1144 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1145 debug("Wrong FIT format: no timestamp\n");
1150 /* mandatory subimages parent '/images' node */
1151 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1152 debug("Wrong FIT format: no images parent node\n");
1161 * fit_conf_find_compat
1162 * @fit: pointer to the FIT format image header
1163 * @fdt: pointer to the device tree to compare against
1165 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1166 * most compatible with the passed in device tree.
1175 * |-o configurations
1183 * |-compatible = "foo,bar", "bim,bam"
1186 * |-compatible = "foo,bar",
1189 * |-compatible = "bim,bam", "baz,biz"
1191 * Configuration 1 would be picked because the first string in U-Boot's
1192 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1193 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1196 * offset to the configuration to use if one was found
1199 int fit_conf_find_compat(const void *fit, const void *fdt)
1202 int noffset, confs_noffset, images_noffset;
1203 const void *fdt_compat;
1205 int best_match_offset = 0;
1206 int best_match_pos = 0;
1208 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1209 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1210 if (confs_noffset < 0 || images_noffset < 0) {
1211 debug("Can't find configurations or images nodes.\n");
1215 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1217 debug("Fdt for comparison has no \"compatible\" property.\n");
1222 * Loop over the configurations in the FIT image.
1224 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1225 (noffset >= 0) && (ndepth > 0);
1226 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1228 const char *kfdt_name;
1230 const char *cur_fdt_compat;
1238 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1240 debug("No fdt property found.\n");
1243 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1245 if (kfdt_noffset < 0) {
1246 debug("No image node named \"%s\" found.\n",
1251 * Get a pointer to this configuration's fdt.
1253 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1254 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1258 len = fdt_compat_len;
1259 cur_fdt_compat = fdt_compat;
1261 * Look for a match for each U-Boot compatibility string in
1262 * turn in this configuration's fdt.
1264 for (i = 0; len > 0 &&
1265 (!best_match_offset || best_match_pos > i); i++) {
1266 int cur_len = strlen(cur_fdt_compat) + 1;
1268 if (!fdt_node_check_compatible(kfdt, 0,
1270 best_match_offset = noffset;
1275 cur_fdt_compat += cur_len;
1278 if (!best_match_offset) {
1279 debug("No match found.\n");
1283 return best_match_offset;
1287 * fit_conf_get_node - get node offset for configuration of a given unit name
1288 * @fit: pointer to the FIT format image header
1289 * @conf_uname: configuration node unit name
1291 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1292 * parant node) of a provided unit name. If configuration is found its node
1293 * offset is returned to the caller.
1295 * When NULL is provided in second argument fit_conf_get_node() will search
1296 * for a default configuration node instead. Default configuration node unit
1297 * name is retrived from FIT_DEFAULT_PROP property of the '/configurations'
1301 * configuration node offset when found (>=0)
1302 * negative number on failure (FDT_ERR_* code)
1304 int fit_conf_get_node(const void *fit, const char *conf_uname)
1306 int noffset, confs_noffset;
1309 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1310 if (confs_noffset < 0) {
1311 debug("Can't find configurations parent node '%s' (%s)\n",
1312 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1313 return confs_noffset;
1316 if (conf_uname == NULL) {
1317 /* get configuration unit name from the default property */
1318 debug("No configuration specified, trying default...\n");
1319 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1320 FIT_DEFAULT_PROP, &len);
1321 if (conf_uname == NULL) {
1322 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1326 debug("Found default configuration: '%s'\n", conf_uname);
1329 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1331 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1332 conf_uname, fdt_strerror(noffset));
1338 int fit_conf_get_prop_node(const void *fit, int noffset,
1339 const char *prop_name)
1344 /* get kernel image unit name from configuration kernel property */
1345 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1349 return fit_image_get_node(fit, uname);
1353 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
1354 * a given configuration
1355 * @fit: pointer to the FIT format image header
1356 * @noffset: configuration node offset
1358 * fit_conf_get_kernel_node() retrives kernel image node unit name from
1359 * configuration FIT_KERNEL_PROP property and translates it to the node
1363 * image node offset when found (>=0)
1364 * negative number on failure (FDT_ERR_* code)
1366 int fit_conf_get_kernel_node(const void *fit, int noffset)
1368 return fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
1372 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
1373 * a given configuration
1374 * @fit: pointer to the FIT format image header
1375 * @noffset: configuration node offset
1377 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
1378 * configuration FIT_KERNEL_PROP property and translates it to the node
1382 * image node offset when found (>=0)
1383 * negative number on failure (FDT_ERR_* code)
1385 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
1387 return fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
1391 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
1392 * a given configuration
1393 * @fit: pointer to the FIT format image header
1394 * @noffset: configuration node offset
1396 * fit_conf_get_fdt_node() retrives fdt image node unit name from
1397 * configuration FIT_KERNEL_PROP property and translates it to the node
1401 * image node offset when found (>=0)
1402 * negative number on failure (FDT_ERR_* code)
1404 int fit_conf_get_fdt_node(const void *fit, int noffset)
1406 return fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
1410 * fit_conf_print - prints out the FIT configuration details
1411 * @fit: pointer to the FIT format image header
1412 * @noffset: offset of the configuration node
1413 * @p: pointer to prefix string
1415 * fit_conf_print() lists all mandatory properies for the processed
1416 * configuration node.
1419 * no returned results
1421 void fit_conf_print(const void *fit, int noffset, const char *p)
1427 /* Mandatory properties */
1428 ret = fit_get_desc(fit, noffset, &desc);
1429 printf("%s Description: ", p);
1431 printf("unavailable\n");
1433 printf("%s\n", desc);
1435 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1436 printf("%s Kernel: ", p);
1438 printf("unavailable\n");
1440 printf("%s\n", uname);
1442 /* Optional properties */
1443 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1445 printf("%s Init Ramdisk: %s\n", p, uname);
1447 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1449 printf("%s FDT: %s\n", p, uname);
1453 * fit_check_ramdisk - verify FIT format ramdisk subimage
1454 * @fit_hdr: pointer to the FIT ramdisk header
1455 * @rd_noffset: ramdisk subimage node offset within FIT image
1456 * @arch: requested ramdisk image architecture type
1457 * @verify: data CRC verification flag
1459 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
1460 * specified FIT image.
1467 int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
1470 fit_image_print(fit, rd_noffset, " ");
1473 puts(" Verifying Hash Integrity ... ");
1474 if (!fit_image_verify(fit, rd_noffset)) {
1475 puts("Bad Data Hash\n");
1476 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
1482 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
1483 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
1484 !fit_image_check_arch(fit, rd_noffset, arch) ||
1485 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
1486 printf("No Linux %s Ramdisk Image\n",
1487 genimg_get_arch_name(arch));
1488 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
1492 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
1495 #endif /* USE_HOSTCC */