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)
152 /* Indent string is defined in header image.h */
153 p = IMAGE_INDENT_STRING;
155 /* Root node properties */
156 ret = fit_get_desc(fit, 0, &desc);
157 printf("%sFIT description: ", p);
159 printf("unavailable\n");
161 printf("%s\n", desc);
163 if (IMAGE_ENABLE_TIMESTAMP) {
164 ret = fit_get_timestamp(fit, 0, ×tamp);
165 printf("%sCreated: ", p);
167 printf("unavailable\n");
169 genimg_print_time(timestamp);
172 /* Find images parent node offset */
173 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
174 if (images_noffset < 0) {
175 printf("Can't find images parent node '%s' (%s)\n",
176 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
180 /* Process its subnodes, print out component images details */
181 for (ndepth = 0, count = 0,
182 noffset = fdt_next_node(fit, images_noffset, &ndepth);
183 (noffset >= 0) && (ndepth > 0);
184 noffset = fdt_next_node(fit, noffset, &ndepth)) {
187 * Direct child node of the images parent node,
188 * i.e. component image node.
190 printf("%s Image %u (%s)\n", p, count++,
191 fit_get_name(fit, noffset, NULL));
193 fit_image_print(fit, noffset, p);
197 /* Find configurations parent node offset */
198 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
199 if (confs_noffset < 0) {
200 debug("Can't get configurations parent node '%s' (%s)\n",
201 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
205 /* get default configuration unit name from default property */
206 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
208 printf("%s Default Configuration: '%s'\n", p, uname);
210 /* Process its subnodes, print out configurations details */
211 for (ndepth = 0, count = 0,
212 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
213 (noffset >= 0) && (ndepth > 0);
214 noffset = fdt_next_node(fit, noffset, &ndepth)) {
217 * Direct child node of the configurations parent node,
218 * i.e. configuration node.
220 printf("%s Configuration %u (%s)\n", p, count++,
221 fit_get_name(fit, noffset, NULL));
223 fit_conf_print(fit, noffset, p);
229 * fit_image_print_data() - prints out the hash node details
230 * @fit: pointer to the FIT format image header
231 * @noffset: offset of the hash node
232 * @p: pointer to prefix string
234 * fit_image_print_data() lists properies for the processed hash node
237 * no returned results
239 static void fit_image_print_data(const void *fit, int noffset, const char *p)
247 * Check subnode name, must be equal to "hash".
248 * Multiple hash nodes require unique unit node
249 * names, e.g. hash@1, hash@2, etc.
251 if (strncmp(fit_get_name(fit, noffset, NULL),
253 strlen(FIT_HASH_NODENAME)) != 0)
256 debug("%s Hash node: '%s'\n", p,
257 fit_get_name(fit, noffset, NULL));
259 printf("%s Hash algo: ", p);
260 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
261 printf("invalid/unsupported\n");
264 printf("%s\n", algo);
266 ret = fit_image_hash_get_value(fit, noffset, &value,
268 printf("%s Hash value: ", p);
270 printf("unavailable\n");
272 for (i = 0; i < value_len; i++)
273 printf("%02x", value[i]);
277 debug("%s Hash len: %d\n", p, value_len);
281 * fit_image_print_verification_data() - prints out the hash/signature details
282 * @fit: pointer to the FIT format image header
283 * @noffset: offset of the hash or signature node
284 * @p: pointer to prefix string
286 * This lists properies for the processed hash node
289 * no returned results
291 static void fit_image_print_verification_data(const void *fit, int noffset,
297 * Check subnode name, must be equal to "hash" or "signature".
298 * Multiple hash/signature nodes require unique unit node
299 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
301 name = fit_get_name(fit, noffset, NULL);
302 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME)))
303 fit_image_print_data(fit, noffset, p);
307 * fit_image_print - prints out the FIT component image details
308 * @fit: pointer to the FIT format image header
309 * @image_noffset: offset of the component image node
310 * @p: pointer to prefix string
312 * fit_image_print() lists all mandatory properies for the processed component
313 * image. If present, hash nodes are printed out as well. Load
314 * address for images of type firmware is also printed out. Since the load
315 * address is not mandatory for firmware images, it will be output as
316 * "unavailable" when not present.
319 * no returned results
321 void fit_image_print(const void *fit, int image_noffset, const char *p)
324 uint8_t type, arch, os, comp;
332 /* Mandatory properties */
333 ret = fit_get_desc(fit, image_noffset, &desc);
334 printf("%s Description: ", p);
336 printf("unavailable\n");
338 printf("%s\n", desc);
340 fit_image_get_type(fit, image_noffset, &type);
341 printf("%s Type: %s\n", p, genimg_get_type_name(type));
343 fit_image_get_comp(fit, image_noffset, &comp);
344 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
346 ret = fit_image_get_data(fit, image_noffset, &data, &size);
349 printf("%s Data Start: ", p);
351 printf("unavailable\n");
353 printf("0x%08lx\n", (ulong)data);
356 printf("%s Data Size: ", p);
358 printf("unavailable\n");
360 genimg_print_size(size);
362 /* Remaining, type dependent properties */
363 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
364 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
365 (type == IH_TYPE_FLATDT)) {
366 fit_image_get_arch(fit, image_noffset, &arch);
367 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
370 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
371 fit_image_get_os(fit, image_noffset, &os);
372 printf("%s OS: %s\n", p, genimg_get_os_name(os));
375 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
376 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
377 ret = fit_image_get_load(fit, image_noffset, &load);
378 printf("%s Load Address: ", p);
380 printf("unavailable\n");
382 printf("0x%08lx\n", load);
385 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
386 (type == IH_TYPE_RAMDISK)) {
387 fit_image_get_entry(fit, image_noffset, &entry);
388 printf("%s Entry Point: ", p);
390 printf("unavailable\n");
392 printf("0x%08lx\n", entry);
395 /* Process all hash subnodes of the component image node */
396 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
397 (noffset >= 0) && (ndepth > 0);
398 noffset = fdt_next_node(fit, noffset, &ndepth)) {
400 /* Direct child node of the component image node */
401 fit_image_print_verification_data(fit, noffset, p);
407 * fit_get_desc - get node description property
408 * @fit: pointer to the FIT format image header
409 * @noffset: node offset
410 * @desc: double pointer to the char, will hold pointer to the descrption
412 * fit_get_desc() reads description property from a given node, if
413 * description is found pointer to it is returened in third call argument.
419 int fit_get_desc(const void *fit, int noffset, char **desc)
423 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
425 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
433 * fit_get_timestamp - get node timestamp property
434 * @fit: pointer to the FIT format image header
435 * @noffset: node offset
436 * @timestamp: pointer to the time_t, will hold read timestamp
438 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
439 * is found and has a correct size its value is retured in third call
444 * -1, on property read failure
445 * -2, on wrong timestamp size
447 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
452 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
454 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
457 if (len != sizeof(uint32_t)) {
458 debug("FIT timestamp with incorrect size of (%u)\n", len);
462 *timestamp = uimage_to_cpu(*((uint32_t *)data));
467 * fit_image_get_node - get node offset for component image of a given unit name
468 * @fit: pointer to the FIT format image header
469 * @image_uname: component image node unit name
471 * fit_image_get_node() finds a component image (withing the '/images'
472 * node) of a provided unit name. If image is found its node offset is
473 * returned to the caller.
476 * image node offset when found (>=0)
477 * negative number on failure (FDT_ERR_* code)
479 int fit_image_get_node(const void *fit, const char *image_uname)
481 int noffset, images_noffset;
483 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
484 if (images_noffset < 0) {
485 debug("Can't find images parent node '%s' (%s)\n",
486 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
487 return images_noffset;
490 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
492 debug("Can't get node offset for image unit name: '%s' (%s)\n",
493 image_uname, fdt_strerror(noffset));
500 * fit_image_get_os - get os id for a given component image node
501 * @fit: pointer to the FIT format image header
502 * @noffset: component image node offset
503 * @os: pointer to the uint8_t, will hold os numeric id
505 * fit_image_get_os() finds os property in a given component image node.
506 * If the property is found, its (string) value is translated to the numeric
507 * id which is returned to the caller.
513 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
518 /* Get OS name from property data */
519 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
521 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
526 /* Translate OS name to id */
527 *os = genimg_get_os_id(data);
532 * fit_image_get_arch - get arch id for a given component image node
533 * @fit: pointer to the FIT format image header
534 * @noffset: component image node offset
535 * @arch: pointer to the uint8_t, will hold arch numeric id
537 * fit_image_get_arch() finds arch property in a given component image node.
538 * If the property is found, its (string) value is translated to the numeric
539 * id which is returned to the caller.
545 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
550 /* Get architecture name from property data */
551 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
553 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
558 /* Translate architecture name to id */
559 *arch = genimg_get_arch_id(data);
564 * fit_image_get_type - get type id for a given component image node
565 * @fit: pointer to the FIT format image header
566 * @noffset: component image node offset
567 * @type: pointer to the uint8_t, will hold type numeric id
569 * fit_image_get_type() finds type property in a given component image node.
570 * If the property is found, its (string) value is translated to the numeric
571 * id which is returned to the caller.
577 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
582 /* Get image type name from property data */
583 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
585 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
590 /* Translate image type name to id */
591 *type = genimg_get_type_id(data);
596 * fit_image_get_comp - get comp id for a given component image node
597 * @fit: pointer to the FIT format image header
598 * @noffset: component image node offset
599 * @comp: pointer to the uint8_t, will hold comp numeric id
601 * fit_image_get_comp() finds comp property in a given component image node.
602 * If the property is found, its (string) value is translated to the numeric
603 * id which is returned to the caller.
609 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
614 /* Get compression name from property data */
615 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
617 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
622 /* Translate compression name to id */
623 *comp = genimg_get_comp_id(data);
628 * fit_image_get_load() - get load addr property for given component image node
629 * @fit: pointer to the FIT format image header
630 * @noffset: component image node offset
631 * @load: pointer to the uint32_t, will hold load address
633 * fit_image_get_load() finds load address property in a given component
634 * image node. If the property is found, its value is returned to the caller.
640 int fit_image_get_load(const void *fit, int noffset, ulong *load)
643 const uint32_t *data;
645 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
647 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
651 *load = uimage_to_cpu(*data);
656 * fit_image_get_entry() - get entry point address property
657 * @fit: pointer to the FIT format image header
658 * @noffset: component image node offset
659 * @entry: pointer to the uint32_t, will hold entry point address
661 * This gets the entry point address property for a given component image
664 * fit_image_get_entry() finds entry point address property in a given
665 * component image node. If the property is found, its value is returned
672 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
675 const uint32_t *data;
677 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
679 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
683 *entry = uimage_to_cpu(*data);
688 * fit_image_get_data - get data property and its size for a given component image node
689 * @fit: pointer to the FIT format image header
690 * @noffset: component image node offset
691 * @data: double pointer to void, will hold data property's data address
692 * @size: pointer to size_t, will hold data property's data size
694 * fit_image_get_data() finds data property in a given component image node.
695 * If the property is found its data start address and size are returned to
702 int fit_image_get_data(const void *fit, int noffset,
703 const void **data, size_t *size)
707 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
709 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
719 * fit_image_hash_get_algo - get hash algorithm name
720 * @fit: pointer to the FIT format image header
721 * @noffset: hash node offset
722 * @algo: double pointer to char, will hold pointer to the algorithm name
724 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
725 * If the property is found its data start address is returned to the caller.
731 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
735 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
737 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
745 * fit_image_hash_get_value - get hash value and length
746 * @fit: pointer to the FIT format image header
747 * @noffset: hash node offset
748 * @value: double pointer to uint8_t, will hold address of a hash value data
749 * @value_len: pointer to an int, will hold hash data length
751 * fit_image_hash_get_value() finds hash value property in a given hash node.
752 * If the property is found its data start address and size are returned to
759 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
764 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
765 if (*value == NULL) {
766 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
776 * fit_image_hash_get_ignore - get hash ignore flag
777 * @fit: pointer to the FIT format image header
778 * @noffset: hash node offset
779 * @ignore: pointer to an int, will hold hash ignore flag
781 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
782 * If the property is found and non-zero, the hash algorithm is not verified by
783 * u-boot automatically.
786 * 0, on ignore not found
787 * value, on ignore found
789 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
794 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
795 if (value == NULL || len != sizeof(int))
804 * fit_set_timestamp - set node timestamp property
805 * @fit: pointer to the FIT format image header
806 * @noffset: node offset
807 * @timestamp: timestamp value to be set
809 * fit_set_timestamp() attempts to set timestamp property in the requested
810 * node and returns operation status to the caller.
814 * -1, on property read failure
816 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
821 t = cpu_to_uimage(timestamp);
822 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
825 printf("Can't set '%s' property for '%s' node (%s)\n",
826 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
835 * calculate_hash - calculate and return hash for provided input data
836 * @data: pointer to the input data
837 * @data_len: data length
838 * @algo: requested hash algorithm
839 * @value: pointer to the char, will hold hash value data (caller must
840 * allocate enough free space)
841 * value_len: length of the calculated hash
843 * calculate_hash() computes input data hash according to the requested
845 * Resulting hash value is placed in caller provided 'value' buffer, length
846 * of the calculated hash is returned via value_len pointer argument.
850 * -1, when algo is unsupported
852 int calculate_hash(const void *data, int data_len, const char *algo,
853 uint8_t *value, int *value_len)
855 if (strcmp(algo, "crc32") == 0) {
856 *((uint32_t *)value) = crc32_wd(0, data, data_len,
858 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
860 } else if (strcmp(algo, "sha1") == 0) {
861 sha1_csum_wd((unsigned char *)data, data_len,
862 (unsigned char *)value, CHUNKSZ_SHA1);
864 } else if (strcmp(algo, "md5") == 0) {
865 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
868 debug("Unsupported hash alogrithm\n");
874 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
875 size_t size, char **err_msgp)
877 uint8_t value[FIT_MAX_HASH_LEN];
886 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
887 *err_msgp = "Can't get hash algo property";
892 if (IMAGE_ENABLE_IGNORE) {
893 fit_image_hash_get_ignore(fit, noffset, &ignore);
900 if (fit_image_hash_get_value(fit, noffset, &fit_value,
902 *err_msgp = "Can't get hash value property";
906 if (calculate_hash(data, size, algo, value, &value_len)) {
907 *err_msgp = "Unsupported hash algorithm";
911 if (value_len != fit_value_len) {
912 *err_msgp = "Bad hash value len";
914 } else if (memcmp(value, fit_value, value_len) != 0) {
915 *err_msgp = "Bad hash value";
923 * fit_image_verify - verify data intergity
924 * @fit: pointer to the FIT format image header
925 * @image_noffset: component image node offset
927 * fit_image_verify() goes over component image hash nodes,
928 * re-calculates each data hash and compares with the value stored in hash
932 * 1, if all hashes are valid
933 * 0, otherwise (or on error)
935 int fit_image_verify(const void *fit, int image_noffset)
942 /* Get image data and data length */
943 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
944 err_msg = "Can't get image data/size";
948 /* Process all hash subnodes of the component image node */
949 for (noffset = fdt_first_subnode(fit, image_noffset);
951 noffset = fdt_next_subnode(fit, noffset)) {
952 const char *name = fit_get_name(fit, noffset, NULL);
955 * Check subnode name, must be equal to "hash".
956 * Multiple hash nodes require unique unit node
957 * names, e.g. hash@1, hash@2, etc.
959 if (!strncmp(name, FIT_HASH_NODENAME,
960 strlen(FIT_HASH_NODENAME))) {
961 if (fit_image_check_hash(fit, noffset, data, size,
968 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
969 err_msg = "Corrupted or truncated tree";
976 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
977 err_msg, fit_get_name(fit, noffset, NULL),
978 fit_get_name(fit, image_noffset, NULL));
983 * fit_all_image_verify - verify data intergity for all images
984 * @fit: pointer to the FIT format image header
986 * fit_all_image_verify() goes over all images in the FIT and
987 * for every images checks if all it's hashes are valid.
990 * 1, if all hashes of all images are valid
991 * 0, otherwise (or on error)
993 int fit_all_image_verify(const void *fit)
1000 /* Find images parent node offset */
1001 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1002 if (images_noffset < 0) {
1003 printf("Can't find images parent node '%s' (%s)\n",
1004 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1008 /* Process all image subnodes, check hashes for each */
1009 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1011 for (ndepth = 0, count = 0,
1012 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1013 (noffset >= 0) && (ndepth > 0);
1014 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1017 * Direct child node of the images parent node,
1018 * i.e. component image node.
1020 printf(" Hash(es) for Image %u (%s): ", count++,
1021 fit_get_name(fit, noffset, NULL));
1023 if (!fit_image_verify(fit, noffset))
1032 * fit_image_check_os - check whether image node is of a given os type
1033 * @fit: pointer to the FIT format image header
1034 * @noffset: component image node offset
1035 * @os: requested image os
1037 * fit_image_check_os() reads image os property and compares its numeric
1038 * id with the requested os. Comparison result is returned to the caller.
1041 * 1 if image is of given os type
1042 * 0 otherwise (or on error)
1044 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1048 if (fit_image_get_os(fit, noffset, &image_os))
1050 return (os == image_os);
1054 * fit_image_check_arch - check whether image node is of a given arch
1055 * @fit: pointer to the FIT format image header
1056 * @noffset: component image node offset
1057 * @arch: requested imagearch
1059 * fit_image_check_arch() reads image arch property and compares its numeric
1060 * id with the requested arch. Comparison result is returned to the caller.
1063 * 1 if image is of given arch
1064 * 0 otherwise (or on error)
1066 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1070 if (fit_image_get_arch(fit, noffset, &image_arch))
1072 return (arch == image_arch);
1076 * fit_image_check_type - check whether image node is of a given type
1077 * @fit: pointer to the FIT format image header
1078 * @noffset: component image node offset
1079 * @type: requested image type
1081 * fit_image_check_type() reads image type property and compares its numeric
1082 * id with the requested type. Comparison result is returned to the caller.
1085 * 1 if image is of given type
1086 * 0 otherwise (or on error)
1088 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1092 if (fit_image_get_type(fit, noffset, &image_type))
1094 return (type == image_type);
1098 * fit_image_check_comp - check whether image node uses given compression
1099 * @fit: pointer to the FIT format image header
1100 * @noffset: component image node offset
1101 * @comp: requested image compression type
1103 * fit_image_check_comp() reads image compression property and compares its
1104 * numeric id with the requested compression type. Comparison result is
1105 * returned to the caller.
1108 * 1 if image uses requested compression
1109 * 0 otherwise (or on error)
1111 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1115 if (fit_image_get_comp(fit, noffset, &image_comp))
1117 return (comp == image_comp);
1121 * fit_check_format - sanity check FIT image format
1122 * @fit: pointer to the FIT format image header
1124 * fit_check_format() runs a basic sanity FIT image verification.
1125 * Routine checks for mandatory properties, nodes, etc.
1131 int fit_check_format(const void *fit)
1133 /* mandatory / node 'description' property */
1134 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1135 debug("Wrong FIT format: no description\n");
1139 if (IMAGE_ENABLE_TIMESTAMP) {
1140 /* mandatory / node 'timestamp' property */
1141 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1142 debug("Wrong FIT format: no timestamp\n");
1147 /* mandatory subimages parent '/images' node */
1148 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1149 debug("Wrong FIT format: no images parent node\n");
1158 * fit_conf_find_compat
1159 * @fit: pointer to the FIT format image header
1160 * @fdt: pointer to the device tree to compare against
1162 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1163 * most compatible with the passed in device tree.
1172 * |-o configurations
1180 * |-compatible = "foo,bar", "bim,bam"
1183 * |-compatible = "foo,bar",
1186 * |-compatible = "bim,bam", "baz,biz"
1188 * Configuration 1 would be picked because the first string in U-Boot's
1189 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1190 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1193 * offset to the configuration to use if one was found
1196 int fit_conf_find_compat(const void *fit, const void *fdt)
1199 int noffset, confs_noffset, images_noffset;
1200 const void *fdt_compat;
1202 int best_match_offset = 0;
1203 int best_match_pos = 0;
1205 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1206 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1207 if (confs_noffset < 0 || images_noffset < 0) {
1208 debug("Can't find configurations or images nodes.\n");
1212 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1214 debug("Fdt for comparison has no \"compatible\" property.\n");
1219 * Loop over the configurations in the FIT image.
1221 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1222 (noffset >= 0) && (ndepth > 0);
1223 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1225 const char *kfdt_name;
1227 const char *cur_fdt_compat;
1235 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1237 debug("No fdt property found.\n");
1240 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1242 if (kfdt_noffset < 0) {
1243 debug("No image node named \"%s\" found.\n",
1248 * Get a pointer to this configuration's fdt.
1250 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1251 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1255 len = fdt_compat_len;
1256 cur_fdt_compat = fdt_compat;
1258 * Look for a match for each U-Boot compatibility string in
1259 * turn in this configuration's fdt.
1261 for (i = 0; len > 0 &&
1262 (!best_match_offset || best_match_pos > i); i++) {
1263 int cur_len = strlen(cur_fdt_compat) + 1;
1265 if (!fdt_node_check_compatible(kfdt, 0,
1267 best_match_offset = noffset;
1272 cur_fdt_compat += cur_len;
1275 if (!best_match_offset) {
1276 debug("No match found.\n");
1280 return best_match_offset;
1284 * fit_conf_get_node - get node offset for configuration of a given unit name
1285 * @fit: pointer to the FIT format image header
1286 * @conf_uname: configuration node unit name
1288 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1289 * parant node) of a provided unit name. If configuration is found its node
1290 * offset is returned to the caller.
1292 * When NULL is provided in second argument fit_conf_get_node() will search
1293 * for a default configuration node instead. Default configuration node unit
1294 * name is retrived from FIT_DEFAULT_PROP property of the '/configurations'
1298 * configuration node offset when found (>=0)
1299 * negative number on failure (FDT_ERR_* code)
1301 int fit_conf_get_node(const void *fit, const char *conf_uname)
1303 int noffset, confs_noffset;
1306 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1307 if (confs_noffset < 0) {
1308 debug("Can't find configurations parent node '%s' (%s)\n",
1309 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1310 return confs_noffset;
1313 if (conf_uname == NULL) {
1314 /* get configuration unit name from the default property */
1315 debug("No configuration specified, trying default...\n");
1316 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1317 FIT_DEFAULT_PROP, &len);
1318 if (conf_uname == NULL) {
1319 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1323 debug("Found default configuration: '%s'\n", conf_uname);
1326 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1328 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1329 conf_uname, fdt_strerror(noffset));
1335 int fit_conf_get_prop_node(const void *fit, int noffset,
1336 const char *prop_name)
1341 /* get kernel image unit name from configuration kernel property */
1342 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1346 return fit_image_get_node(fit, uname);
1350 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
1351 * a given configuration
1352 * @fit: pointer to the FIT format image header
1353 * @noffset: configuration node offset
1355 * fit_conf_get_kernel_node() retrives kernel image node unit name from
1356 * configuration FIT_KERNEL_PROP property and translates it to the node
1360 * image node offset when found (>=0)
1361 * negative number on failure (FDT_ERR_* code)
1363 int fit_conf_get_kernel_node(const void *fit, int noffset)
1365 return fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
1369 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
1370 * a given configuration
1371 * @fit: pointer to the FIT format image header
1372 * @noffset: configuration node offset
1374 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
1375 * configuration FIT_KERNEL_PROP property and translates it to the node
1379 * image node offset when found (>=0)
1380 * negative number on failure (FDT_ERR_* code)
1382 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
1384 return fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
1388 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
1389 * a given configuration
1390 * @fit: pointer to the FIT format image header
1391 * @noffset: configuration node offset
1393 * fit_conf_get_fdt_node() retrives fdt image node unit name from
1394 * configuration FIT_KERNEL_PROP property and translates it to the node
1398 * image node offset when found (>=0)
1399 * negative number on failure (FDT_ERR_* code)
1401 int fit_conf_get_fdt_node(const void *fit, int noffset)
1403 return fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
1407 * fit_conf_print - prints out the FIT configuration details
1408 * @fit: pointer to the FIT format image header
1409 * @noffset: offset of the configuration node
1410 * @p: pointer to prefix string
1412 * fit_conf_print() lists all mandatory properies for the processed
1413 * configuration node.
1416 * no returned results
1418 void fit_conf_print(const void *fit, int noffset, const char *p)
1424 /* Mandatory properties */
1425 ret = fit_get_desc(fit, noffset, &desc);
1426 printf("%s Description: ", p);
1428 printf("unavailable\n");
1430 printf("%s\n", desc);
1432 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1433 printf("%s Kernel: ", p);
1435 printf("unavailable\n");
1437 printf("%s\n", uname);
1439 /* Optional properties */
1440 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1442 printf("%s Init Ramdisk: %s\n", p, uname);
1444 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1446 printf("%s FDT: %s\n", p, uname);
1450 * fit_check_ramdisk - verify FIT format ramdisk subimage
1451 * @fit_hdr: pointer to the FIT ramdisk header
1452 * @rd_noffset: ramdisk subimage node offset within FIT image
1453 * @arch: requested ramdisk image architecture type
1454 * @verify: data CRC verification flag
1456 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
1457 * specified FIT image.
1463 int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
1466 fit_image_print(fit, rd_noffset, " ");
1469 puts(" Verifying Hash Integrity ... ");
1470 if (!fit_image_verify(fit, rd_noffset)) {
1471 puts("Bad Data Hash\n");
1472 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
1478 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
1479 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
1480 !fit_image_check_arch(fit, rd_noffset, arch) ||
1481 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
1482 printf("No Linux %s Ramdisk Image\n",
1483 genimg_get_arch_name(arch));
1484 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
1488 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);