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,
36 DECLARE_GLOBAL_DATA_PTR;
37 #endif /* !USE_HOSTCC*/
39 #include <bootstage.h>
41 #include <u-boot/crc.h>
42 #include <u-boot/md5.h>
44 /*****************************************************************************/
45 /* New uImage format routines */
46 /*****************************************************************************/
48 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
49 ulong *addr, const char **name)
56 sep = strchr(spec, sepc);
59 *addr = simple_strtoul(spec, NULL, 16);
69 * fit_parse_conf - parse FIT configuration spec
70 * @spec: input string, containing configuration spec
71 * @add_curr: current image address (to be used as a possible default)
72 * @addr: pointer to a ulong variable, will hold FIT image address of a given
74 * @conf_name double pointer to a char, will hold pointer to a configuration
77 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
78 * where <addr> is a FIT image address that contains configuration
79 * with a <conf> unit name.
81 * Address part is optional, and if omitted default add_curr will
85 * 1 if spec is a valid configuration string,
86 * addr and conf_name are set accordingly
89 int fit_parse_conf(const char *spec, ulong addr_curr,
90 ulong *addr, const char **conf_name)
92 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
96 * fit_parse_subimage - parse FIT subimage spec
97 * @spec: input string, containing subimage spec
98 * @add_curr: current image address (to be used as a possible default)
99 * @addr: pointer to a ulong variable, will hold FIT image address of a given
101 * @image_name: double pointer to a char, will hold pointer to a subimage name
103 * fit_parse_subimage() expects subimage spec in the for of
104 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
105 * subimage with a <subimg> unit name.
107 * Address part is optional, and if omitted default add_curr will
111 * 1 if spec is a valid subimage string,
112 * addr and image_name are set accordingly
115 int fit_parse_subimage(const char *spec, ulong addr_curr,
116 ulong *addr, const char **image_name)
118 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
120 #endif /* !USE_HOSTCC */
122 static void fit_get_debug(const void *fit, int noffset,
123 char *prop_name, int err)
125 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
126 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
130 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
132 * fit_print_contents - prints out the contents of the FIT format image
133 * @fit: pointer to the FIT format image header
134 * @p: pointer to prefix string
136 * fit_print_contents() formats a multi line FIT image contents description.
137 * The routine prints out FIT image properties (root node level) follwed by
138 * the details of each component image.
141 * no returned results
143 void fit_print_contents(const void *fit)
156 /* Indent string is defined in header image.h */
157 p = IMAGE_INDENT_STRING;
159 /* Root node properties */
160 ret = fit_get_desc(fit, 0, &desc);
161 printf("%sFIT description: ", p);
163 printf("unavailable\n");
165 printf("%s\n", desc);
167 if (IMAGE_ENABLE_TIMESTAMP) {
168 ret = fit_get_timestamp(fit, 0, ×tamp);
169 printf("%sCreated: ", p);
171 printf("unavailable\n");
173 genimg_print_time(timestamp);
176 /* Find images parent node offset */
177 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
178 if (images_noffset < 0) {
179 printf("Can't find images parent node '%s' (%s)\n",
180 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
184 /* Process its subnodes, print out component images details */
185 for (ndepth = 0, count = 0,
186 noffset = fdt_next_node(fit, images_noffset, &ndepth);
187 (noffset >= 0) && (ndepth > 0);
188 noffset = fdt_next_node(fit, noffset, &ndepth)) {
191 * Direct child node of the images parent node,
192 * i.e. component image node.
194 printf("%s Image %u (%s)\n", p, count++,
195 fit_get_name(fit, noffset, NULL));
197 fit_image_print(fit, noffset, p);
201 /* Find configurations parent node offset */
202 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
203 if (confs_noffset < 0) {
204 debug("Can't get configurations parent node '%s' (%s)\n",
205 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
209 /* get default configuration unit name from default property */
210 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
212 printf("%s Default Configuration: '%s'\n", p, uname);
214 /* Process its subnodes, print out configurations details */
215 for (ndepth = 0, count = 0,
216 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
217 (noffset >= 0) && (ndepth > 0);
218 noffset = fdt_next_node(fit, noffset, &ndepth)) {
221 * Direct child node of the configurations parent node,
222 * i.e. configuration node.
224 printf("%s Configuration %u (%s)\n", p, count++,
225 fit_get_name(fit, noffset, NULL));
227 fit_conf_print(fit, noffset, p);
233 * fit_image_print_data() - prints out the hash node details
234 * @fit: pointer to the FIT format image header
235 * @noffset: offset of the hash node
236 * @p: pointer to prefix string
238 * fit_image_print_data() lists properies for the processed hash node
241 * no returned results
243 static void fit_image_print_data(const void *fit, int noffset, const char *p)
251 * Check subnode name, must be equal to "hash".
252 * Multiple hash nodes require unique unit node
253 * names, e.g. hash@1, hash@2, etc.
255 if (strncmp(fit_get_name(fit, noffset, NULL),
257 strlen(FIT_HASH_NODENAME)) != 0)
260 debug("%s Hash node: '%s'\n", p,
261 fit_get_name(fit, noffset, NULL));
263 printf("%s Hash algo: ", p);
264 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
265 printf("invalid/unsupported\n");
268 printf("%s\n", algo);
270 ret = fit_image_hash_get_value(fit, noffset, &value,
272 printf("%s Hash value: ", p);
274 printf("unavailable\n");
276 for (i = 0; i < value_len; i++)
277 printf("%02x", value[i]);
281 debug("%s Hash len: %d\n", p, value_len);
285 * fit_image_print_verification_data() - prints out the hash/signature details
286 * @fit: pointer to the FIT format image header
287 * @noffset: offset of the hash or signature node
288 * @p: pointer to prefix string
290 * This lists properies for the processed hash node
293 * no returned results
295 static void fit_image_print_verification_data(const void *fit, int noffset,
301 * Check subnode name, must be equal to "hash" or "signature".
302 * Multiple hash/signature nodes require unique unit node
303 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
305 name = fit_get_name(fit, noffset, NULL);
306 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME)))
307 fit_image_print_data(fit, noffset, p);
311 * fit_image_print - prints out the FIT component image details
312 * @fit: pointer to the FIT format image header
313 * @image_noffset: offset of the component image node
314 * @p: pointer to prefix string
316 * fit_image_print() lists all mandatory properies for the processed component
317 * image. If present, hash nodes are printed out as well. Load
318 * address for images of type firmware is also printed out. Since the load
319 * address is not mandatory for firmware images, it will be output as
320 * "unavailable" when not present.
323 * no returned results
325 void fit_image_print(const void *fit, int image_noffset, const char *p)
328 uint8_t type, arch, os, comp;
336 /* Mandatory properties */
337 ret = fit_get_desc(fit, image_noffset, &desc);
338 printf("%s Description: ", p);
340 printf("unavailable\n");
342 printf("%s\n", desc);
344 fit_image_get_type(fit, image_noffset, &type);
345 printf("%s Type: %s\n", p, genimg_get_type_name(type));
347 fit_image_get_comp(fit, image_noffset, &comp);
348 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
350 ret = fit_image_get_data(fit, image_noffset, &data, &size);
353 printf("%s Data Start: ", p);
355 printf("unavailable\n");
357 printf("0x%08lx\n", (ulong)data);
360 printf("%s Data Size: ", p);
362 printf("unavailable\n");
364 genimg_print_size(size);
366 /* Remaining, type dependent properties */
367 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
368 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
369 (type == IH_TYPE_FLATDT)) {
370 fit_image_get_arch(fit, image_noffset, &arch);
371 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
374 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
375 fit_image_get_os(fit, image_noffset, &os);
376 printf("%s OS: %s\n", p, genimg_get_os_name(os));
379 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
380 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
381 ret = fit_image_get_load(fit, image_noffset, &load);
382 printf("%s Load Address: ", p);
384 printf("unavailable\n");
386 printf("0x%08lx\n", load);
389 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
390 (type == IH_TYPE_RAMDISK)) {
391 fit_image_get_entry(fit, image_noffset, &entry);
392 printf("%s Entry Point: ", p);
394 printf("unavailable\n");
396 printf("0x%08lx\n", entry);
399 /* Process all hash subnodes of the component image node */
400 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
401 (noffset >= 0) && (ndepth > 0);
402 noffset = fdt_next_node(fit, noffset, &ndepth)) {
404 /* Direct child node of the component image node */
405 fit_image_print_verification_data(fit, noffset, p);
412 * fit_get_desc - get node description property
413 * @fit: pointer to the FIT format image header
414 * @noffset: node offset
415 * @desc: double pointer to the char, will hold pointer to the descrption
417 * fit_get_desc() reads description property from a given node, if
418 * description is found pointer to it is returened in third call argument.
424 int fit_get_desc(const void *fit, int noffset, char **desc)
428 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
430 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
438 * fit_get_timestamp - get node timestamp property
439 * @fit: pointer to the FIT format image header
440 * @noffset: node offset
441 * @timestamp: pointer to the time_t, will hold read timestamp
443 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
444 * is found and has a correct size its value is retured in third call
449 * -1, on property read failure
450 * -2, on wrong timestamp size
452 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
457 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
459 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
462 if (len != sizeof(uint32_t)) {
463 debug("FIT timestamp with incorrect size of (%u)\n", len);
467 *timestamp = uimage_to_cpu(*((uint32_t *)data));
472 * fit_image_get_node - get node offset for component image of a given unit name
473 * @fit: pointer to the FIT format image header
474 * @image_uname: component image node unit name
476 * fit_image_get_node() finds a component image (withing the '/images'
477 * node) of a provided unit name. If image is found its node offset is
478 * returned to the caller.
481 * image node offset when found (>=0)
482 * negative number on failure (FDT_ERR_* code)
484 int fit_image_get_node(const void *fit, const char *image_uname)
486 int noffset, images_noffset;
488 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
489 if (images_noffset < 0) {
490 debug("Can't find images parent node '%s' (%s)\n",
491 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
492 return images_noffset;
495 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
497 debug("Can't get node offset for image unit name: '%s' (%s)\n",
498 image_uname, fdt_strerror(noffset));
505 * fit_image_get_os - get os id for a given component image node
506 * @fit: pointer to the FIT format image header
507 * @noffset: component image node offset
508 * @os: pointer to the uint8_t, will hold os numeric id
510 * fit_image_get_os() finds os property in a given component image node.
511 * If the property is found, its (string) value is translated to the numeric
512 * id which is returned to the caller.
518 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
523 /* Get OS name from property data */
524 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
526 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
531 /* Translate OS name to id */
532 *os = genimg_get_os_id(data);
537 * fit_image_get_arch - get arch id for a given component image node
538 * @fit: pointer to the FIT format image header
539 * @noffset: component image node offset
540 * @arch: pointer to the uint8_t, will hold arch numeric id
542 * fit_image_get_arch() finds arch property in a given component image node.
543 * If the property is found, its (string) value is translated to the numeric
544 * id which is returned to the caller.
550 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
555 /* Get architecture name from property data */
556 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
558 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
563 /* Translate architecture name to id */
564 *arch = genimg_get_arch_id(data);
569 * fit_image_get_type - get type id for a given component image node
570 * @fit: pointer to the FIT format image header
571 * @noffset: component image node offset
572 * @type: pointer to the uint8_t, will hold type numeric id
574 * fit_image_get_type() finds type property in a given component image node.
575 * If the property is found, its (string) value is translated to the numeric
576 * id which is returned to the caller.
582 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
587 /* Get image type name from property data */
588 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
590 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
595 /* Translate image type name to id */
596 *type = genimg_get_type_id(data);
601 * fit_image_get_comp - get comp id for a given component image node
602 * @fit: pointer to the FIT format image header
603 * @noffset: component image node offset
604 * @comp: pointer to the uint8_t, will hold comp numeric id
606 * fit_image_get_comp() finds comp property in a given component image node.
607 * If the property is found, its (string) value is translated to the numeric
608 * id which is returned to the caller.
614 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
619 /* Get compression name from property data */
620 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
622 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
627 /* Translate compression name to id */
628 *comp = genimg_get_comp_id(data);
633 * fit_image_get_load() - get load addr property for given component image node
634 * @fit: pointer to the FIT format image header
635 * @noffset: component image node offset
636 * @load: pointer to the uint32_t, will hold load address
638 * fit_image_get_load() finds load address property in a given component
639 * image node. If the property is found, its value is returned to the caller.
645 int fit_image_get_load(const void *fit, int noffset, ulong *load)
648 const uint32_t *data;
650 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
652 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
656 *load = uimage_to_cpu(*data);
661 * fit_image_get_entry() - get entry point address property
662 * @fit: pointer to the FIT format image header
663 * @noffset: component image node offset
664 * @entry: pointer to the uint32_t, will hold entry point address
666 * This gets the entry point address property for a given component image
669 * fit_image_get_entry() finds entry point address property in a given
670 * component image node. If the property is found, its value is returned
677 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
680 const uint32_t *data;
682 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
684 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
688 *entry = uimage_to_cpu(*data);
693 * fit_image_get_data - get data property and its size for a given component image node
694 * @fit: pointer to the FIT format image header
695 * @noffset: component image node offset
696 * @data: double pointer to void, will hold data property's data address
697 * @size: pointer to size_t, will hold data property's data size
699 * fit_image_get_data() finds data property in a given component image node.
700 * If the property is found its data start address and size are returned to
707 int fit_image_get_data(const void *fit, int noffset,
708 const void **data, size_t *size)
712 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
714 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
724 * fit_image_hash_get_algo - get hash algorithm name
725 * @fit: pointer to the FIT format image header
726 * @noffset: hash node offset
727 * @algo: double pointer to char, will hold pointer to the algorithm name
729 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
730 * If the property is found its data start address is returned to the caller.
736 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
740 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
742 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
750 * fit_image_hash_get_value - get hash value and length
751 * @fit: pointer to the FIT format image header
752 * @noffset: hash node offset
753 * @value: double pointer to uint8_t, will hold address of a hash value data
754 * @value_len: pointer to an int, will hold hash data length
756 * fit_image_hash_get_value() finds hash value property in a given hash node.
757 * If the property is found its data start address and size are returned to
764 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
769 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
770 if (*value == NULL) {
771 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
781 * fit_image_hash_get_ignore - get hash ignore flag
782 * @fit: pointer to the FIT format image header
783 * @noffset: hash node offset
784 * @ignore: pointer to an int, will hold hash ignore flag
786 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
787 * If the property is found and non-zero, the hash algorithm is not verified by
788 * u-boot automatically.
791 * 0, on ignore not found
792 * value, on ignore found
794 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
799 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
800 if (value == NULL || len != sizeof(int))
809 * fit_set_timestamp - set node timestamp property
810 * @fit: pointer to the FIT format image header
811 * @noffset: node offset
812 * @timestamp: timestamp value to be set
814 * fit_set_timestamp() attempts to set timestamp property in the requested
815 * node and returns operation status to the caller.
819 * -1, on property read failure
821 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
826 t = cpu_to_uimage(timestamp);
827 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
830 printf("Can't set '%s' property for '%s' node (%s)\n",
831 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
840 * calculate_hash - calculate and return hash for provided input data
841 * @data: pointer to the input data
842 * @data_len: data length
843 * @algo: requested hash algorithm
844 * @value: pointer to the char, will hold hash value data (caller must
845 * allocate enough free space)
846 * value_len: length of the calculated hash
848 * calculate_hash() computes input data hash according to the requested
850 * Resulting hash value is placed in caller provided 'value' buffer, length
851 * of the calculated hash is returned via value_len pointer argument.
855 * -1, when algo is unsupported
857 int calculate_hash(const void *data, int data_len, const char *algo,
858 uint8_t *value, int *value_len)
860 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
861 *((uint32_t *)value) = crc32_wd(0, data, data_len,
863 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
865 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
866 sha1_csum_wd((unsigned char *)data, data_len,
867 (unsigned char *)value, CHUNKSZ_SHA1);
869 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
870 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
873 debug("Unsupported hash alogrithm\n");
879 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
880 size_t size, char **err_msgp)
882 uint8_t value[FIT_MAX_HASH_LEN];
891 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
892 *err_msgp = "Can't get hash algo property";
897 if (IMAGE_ENABLE_IGNORE) {
898 fit_image_hash_get_ignore(fit, noffset, &ignore);
905 if (fit_image_hash_get_value(fit, noffset, &fit_value,
907 *err_msgp = "Can't get hash value property";
911 if (calculate_hash(data, size, algo, value, &value_len)) {
912 *err_msgp = "Unsupported hash algorithm";
916 if (value_len != fit_value_len) {
917 *err_msgp = "Bad hash value len";
919 } else if (memcmp(value, fit_value, value_len) != 0) {
920 *err_msgp = "Bad hash value";
928 * fit_image_verify - verify data intergity
929 * @fit: pointer to the FIT format image header
930 * @image_noffset: component image node offset
932 * fit_image_verify() goes over component image hash nodes,
933 * re-calculates each data hash and compares with the value stored in hash
937 * 1, if all hashes are valid
938 * 0, otherwise (or on error)
940 int fit_image_verify(const void *fit, int image_noffset)
947 /* Get image data and data length */
948 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
949 err_msg = "Can't get image data/size";
953 /* Process all hash subnodes of the component image node */
954 for (noffset = fdt_first_subnode(fit, image_noffset);
956 noffset = fdt_next_subnode(fit, noffset)) {
957 const char *name = fit_get_name(fit, noffset, NULL);
960 * Check subnode name, must be equal to "hash".
961 * Multiple hash nodes require unique unit node
962 * names, e.g. hash@1, hash@2, etc.
964 if (!strncmp(name, FIT_HASH_NODENAME,
965 strlen(FIT_HASH_NODENAME))) {
966 if (fit_image_check_hash(fit, noffset, data, size,
973 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
974 err_msg = "Corrupted or truncated tree";
981 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
982 err_msg, fit_get_name(fit, noffset, NULL),
983 fit_get_name(fit, image_noffset, NULL));
988 * fit_all_image_verify - verify data intergity for all images
989 * @fit: pointer to the FIT format image header
991 * fit_all_image_verify() goes over all images in the FIT and
992 * for every images checks if all it's hashes are valid.
995 * 1, if all hashes of all images are valid
996 * 0, otherwise (or on error)
998 int fit_all_image_verify(const void *fit)
1005 /* Find images parent node offset */
1006 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1007 if (images_noffset < 0) {
1008 printf("Can't find images parent node '%s' (%s)\n",
1009 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1013 /* Process all image subnodes, check hashes for each */
1014 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1016 for (ndepth = 0, count = 0,
1017 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1018 (noffset >= 0) && (ndepth > 0);
1019 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1022 * Direct child node of the images parent node,
1023 * i.e. component image node.
1025 printf(" Hash(es) for Image %u (%s): ", count++,
1026 fit_get_name(fit, noffset, NULL));
1028 if (!fit_image_verify(fit, noffset))
1037 * fit_image_check_os - check whether image node is of a given os type
1038 * @fit: pointer to the FIT format image header
1039 * @noffset: component image node offset
1040 * @os: requested image os
1042 * fit_image_check_os() reads image os property and compares its numeric
1043 * id with the requested os. Comparison result is returned to the caller.
1046 * 1 if image is of given os type
1047 * 0 otherwise (or on error)
1049 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1053 if (fit_image_get_os(fit, noffset, &image_os))
1055 return (os == image_os);
1059 * fit_image_check_arch - check whether image node is of a given arch
1060 * @fit: pointer to the FIT format image header
1061 * @noffset: component image node offset
1062 * @arch: requested imagearch
1064 * fit_image_check_arch() reads image arch property and compares its numeric
1065 * id with the requested arch. Comparison result is returned to the caller.
1068 * 1 if image is of given arch
1069 * 0 otherwise (or on error)
1071 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1075 if (fit_image_get_arch(fit, noffset, &image_arch))
1077 return (arch == image_arch);
1081 * fit_image_check_type - check whether image node is of a given type
1082 * @fit: pointer to the FIT format image header
1083 * @noffset: component image node offset
1084 * @type: requested image type
1086 * fit_image_check_type() reads image type property and compares its numeric
1087 * id with the requested type. Comparison result is returned to the caller.
1090 * 1 if image is of given type
1091 * 0 otherwise (or on error)
1093 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1097 if (fit_image_get_type(fit, noffset, &image_type))
1099 return (type == image_type);
1103 * fit_image_check_comp - check whether image node uses given compression
1104 * @fit: pointer to the FIT format image header
1105 * @noffset: component image node offset
1106 * @comp: requested image compression type
1108 * fit_image_check_comp() reads image compression property and compares its
1109 * numeric id with the requested compression type. Comparison result is
1110 * returned to the caller.
1113 * 1 if image uses requested compression
1114 * 0 otherwise (or on error)
1116 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1120 if (fit_image_get_comp(fit, noffset, &image_comp))
1122 return (comp == image_comp);
1126 * fit_check_format - sanity check FIT image format
1127 * @fit: pointer to the FIT format image header
1129 * fit_check_format() runs a basic sanity FIT image verification.
1130 * Routine checks for mandatory properties, nodes, etc.
1136 int fit_check_format(const void *fit)
1138 /* mandatory / node 'description' property */
1139 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1140 debug("Wrong FIT format: no description\n");
1144 if (IMAGE_ENABLE_TIMESTAMP) {
1145 /* mandatory / node 'timestamp' property */
1146 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1147 debug("Wrong FIT format: no timestamp\n");
1152 /* mandatory subimages parent '/images' node */
1153 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1154 debug("Wrong FIT format: no images parent node\n");
1163 * fit_conf_find_compat
1164 * @fit: pointer to the FIT format image header
1165 * @fdt: pointer to the device tree to compare against
1167 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1168 * most compatible with the passed in device tree.
1177 * |-o configurations
1185 * |-compatible = "foo,bar", "bim,bam"
1188 * |-compatible = "foo,bar",
1191 * |-compatible = "bim,bam", "baz,biz"
1193 * Configuration 1 would be picked because the first string in U-Boot's
1194 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1195 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1198 * offset to the configuration to use if one was found
1201 int fit_conf_find_compat(const void *fit, const void *fdt)
1204 int noffset, confs_noffset, images_noffset;
1205 const void *fdt_compat;
1207 int best_match_offset = 0;
1208 int best_match_pos = 0;
1210 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1211 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1212 if (confs_noffset < 0 || images_noffset < 0) {
1213 debug("Can't find configurations or images nodes.\n");
1217 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1219 debug("Fdt for comparison has no \"compatible\" property.\n");
1224 * Loop over the configurations in the FIT image.
1226 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1227 (noffset >= 0) && (ndepth > 0);
1228 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1230 const char *kfdt_name;
1232 const char *cur_fdt_compat;
1240 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1242 debug("No fdt property found.\n");
1245 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1247 if (kfdt_noffset < 0) {
1248 debug("No image node named \"%s\" found.\n",
1253 * Get a pointer to this configuration's fdt.
1255 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1256 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1260 len = fdt_compat_len;
1261 cur_fdt_compat = fdt_compat;
1263 * Look for a match for each U-Boot compatibility string in
1264 * turn in this configuration's fdt.
1266 for (i = 0; len > 0 &&
1267 (!best_match_offset || best_match_pos > i); i++) {
1268 int cur_len = strlen(cur_fdt_compat) + 1;
1270 if (!fdt_node_check_compatible(kfdt, 0,
1272 best_match_offset = noffset;
1277 cur_fdt_compat += cur_len;
1280 if (!best_match_offset) {
1281 debug("No match found.\n");
1285 return best_match_offset;
1289 * fit_conf_get_node - get node offset for configuration of a given unit name
1290 * @fit: pointer to the FIT format image header
1291 * @conf_uname: configuration node unit name
1293 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1294 * parant node) of a provided unit name. If configuration is found its node
1295 * offset is returned to the caller.
1297 * When NULL is provided in second argument fit_conf_get_node() will search
1298 * for a default configuration node instead. Default configuration node unit
1299 * name is retrived from FIT_DEFAULT_PROP property of the '/configurations'
1303 * configuration node offset when found (>=0)
1304 * negative number on failure (FDT_ERR_* code)
1306 int fit_conf_get_node(const void *fit, const char *conf_uname)
1308 int noffset, confs_noffset;
1311 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1312 if (confs_noffset < 0) {
1313 debug("Can't find configurations parent node '%s' (%s)\n",
1314 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1315 return confs_noffset;
1318 if (conf_uname == NULL) {
1319 /* get configuration unit name from the default property */
1320 debug("No configuration specified, trying default...\n");
1321 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1322 FIT_DEFAULT_PROP, &len);
1323 if (conf_uname == NULL) {
1324 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1328 debug("Found default configuration: '%s'\n", conf_uname);
1331 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1333 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1334 conf_uname, fdt_strerror(noffset));
1340 int fit_conf_get_prop_node(const void *fit, int noffset,
1341 const char *prop_name)
1346 /* get kernel image unit name from configuration kernel property */
1347 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1351 return fit_image_get_node(fit, uname);
1355 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
1356 * a given configuration
1357 * @fit: pointer to the FIT format image header
1358 * @noffset: configuration node offset
1360 * fit_conf_get_kernel_node() retrives kernel image node unit name from
1361 * configuration FIT_KERNEL_PROP property and translates it to the node
1365 * image node offset when found (>=0)
1366 * negative number on failure (FDT_ERR_* code)
1368 int fit_conf_get_kernel_node(const void *fit, int noffset)
1370 return fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
1374 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
1375 * a given configuration
1376 * @fit: pointer to the FIT format image header
1377 * @noffset: configuration node offset
1379 * fit_conf_get_fdt_node() retrives fdt image node unit name from
1380 * configuration FIT_KERNEL_PROP property and translates it to the node
1384 * image node offset when found (>=0)
1385 * negative number on failure (FDT_ERR_* code)
1387 int fit_conf_get_fdt_node(const void *fit, int noffset)
1389 return fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
1393 * fit_conf_print - prints out the FIT configuration details
1394 * @fit: pointer to the FIT format image header
1395 * @noffset: offset of the configuration node
1396 * @p: pointer to prefix string
1398 * fit_conf_print() lists all mandatory properies for the processed
1399 * configuration node.
1402 * no returned results
1404 void fit_conf_print(const void *fit, int noffset, const char *p)
1410 /* Mandatory properties */
1411 ret = fit_get_desc(fit, noffset, &desc);
1412 printf("%s Description: ", p);
1414 printf("unavailable\n");
1416 printf("%s\n", desc);
1418 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1419 printf("%s Kernel: ", p);
1421 printf("unavailable\n");
1423 printf("%s\n", uname);
1425 /* Optional properties */
1426 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1428 printf("%s Init Ramdisk: %s\n", p, uname);
1430 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1432 printf("%s FDT: %s\n", p, uname);
1435 int fit_image_select(const void *fit, int rd_noffset, int verify)
1437 fit_image_print(fit, rd_noffset, " ");
1440 puts(" Verifying Hash Integrity ... ");
1441 if (!fit_image_verify(fit, rd_noffset)) {
1442 puts("Bad Data Hash\n");
1451 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1458 debug("* %s: using config '%s' from image at 0x%08lx\n",
1459 prop_name, images->fit_uname_cfg, addr);
1461 /* Check whether configuration has this property defined */
1462 fit_hdr = map_sysmem(addr, 0);
1463 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1464 if (cfg_noffset < 0) {
1465 debug("* %s: no such config\n", prop_name);
1469 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1471 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1478 int fit_image_load(bootm_headers_t *images, const char *prop_name, ulong addr,
1479 const char **fit_unamep, const char *fit_uname_config,
1480 int arch, int image_type, int bootstage_id,
1481 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1483 int cfg_noffset, noffset;
1484 const char *fit_uname;
1489 ulong load, data, len;
1492 fit = map_sysmem(addr, 0);
1493 fit_uname = fit_unamep ? *fit_unamep : NULL;
1494 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1496 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1497 if (!fit_check_format(fit)) {
1498 printf("Bad FIT %s image format!\n", prop_name);
1499 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1502 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1504 /* get ramdisk component image node offset */
1505 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1506 noffset = fit_image_get_node(fit, fit_uname);
1509 * no image node unit name, try to get config
1510 * node first. If config unit node name is NULL
1511 * fit_conf_get_node() will try to find default config node
1513 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1514 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1515 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1517 cfg_noffset = fit_conf_get_node(fit,
1520 if (cfg_noffset < 0) {
1521 puts("Could not find configuration node\n");
1522 bootstage_error(bootstage_id +
1523 BOOTSTAGE_SUB_NO_UNIT_NAME);
1526 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1527 printf(" Using '%s' configuration\n", fit_uname_config);
1528 if (image_type == IH_TYPE_KERNEL) {
1529 /* Remember (and possibly verify) this config */
1530 images->fit_uname_cfg = fit_uname_config;
1531 if (IMAGE_ENABLE_VERIFY && images->verify) {
1532 puts(" Verifying Hash Integrity ... ");
1533 if (!fit_config_verify(fit, cfg_noffset)) {
1534 puts("Bad Data Hash\n");
1535 bootstage_error(bootstage_id +
1536 BOOTSTAGE_SUB_HASH);
1541 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1544 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1546 fit_uname = fit_get_name(fit, noffset, NULL);
1549 puts("Could not find subimage node\n");
1550 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1554 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1556 ret = fit_image_select(fit, noffset, images->verify);
1558 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1562 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1563 if (!fit_image_check_target_arch(fit, noffset)) {
1564 puts("Unsupported Architecture\n");
1565 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1569 if (image_type == IH_TYPE_FLATDT &&
1570 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1571 puts("FDT image is compressed");
1572 return -EPROTONOSUPPORT;
1575 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1576 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1577 (image_type == IH_TYPE_KERNEL &&
1578 fit_image_check_type(fit, noffset,
1579 IH_TYPE_KERNEL_NOLOAD));
1580 os_ok = image_type == IH_TYPE_FLATDT ||
1581 fit_image_check_os(fit, noffset, IH_OS_LINUX);
1582 if (!type_ok || !os_ok) {
1583 printf("No Linux %s %s Image\n", genimg_get_arch_name(arch),
1584 genimg_get_type_name(image_type));
1585 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1589 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1591 /* get image data address and length */
1592 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1593 printf("Could not find %s subimage data!\n", prop_name);
1594 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1599 /* verify that image data is a proper FDT blob */
1600 if (image_type == IH_TYPE_FLATDT && fdt_check_header((char *)buf)) {
1601 puts("Subimage data is not a FDT");
1605 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1608 * Work-around for eldk-4.2 which gives this warning if we try to
1609 * case in the unmap_sysmem() call:
1610 * warning: initialization discards qualifiers from pointer target type
1613 void *vbuf = (void *)buf;
1615 data = map_to_sysmem(vbuf);
1618 if (load_op == FIT_LOAD_IGNORED) {
1620 } else if (fit_image_get_load(fit, noffset, &load)) {
1621 if (load_op == FIT_LOAD_REQUIRED) {
1622 printf("Can't get %s subimage load address!\n",
1624 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1628 ulong image_start, image_end;
1633 * move image data to the load address,
1634 * make sure we don't overwrite initial image
1637 image_end = addr + fit_get_size(fit);
1639 load_end = load + len;
1640 if (image_type != IH_TYPE_KERNEL &&
1641 load < image_end && load_end > image_start) {
1642 printf("Error: %s overwritten\n", prop_name);
1646 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1647 prop_name, data, load);
1649 dst = map_sysmem(load, len);
1650 memmove(dst, buf, len);
1653 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1658 *fit_unamep = (char *)fit_uname;