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
237 * @type: Type of information to print ("hash" or "sign")
239 * fit_image_print_data() lists properies for the processed hash node
241 * This function avoid using puts() since it prints a newline on the host
242 * but does not in U-Boot.
245 * no returned results
247 static void fit_image_print_data(const void *fit, int noffset, const char *p,
257 debug("%s %s node: '%s'\n", p, type,
258 fit_get_name(fit, noffset, NULL));
259 printf("%s %s algo: ", p, type);
260 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
261 printf("invalid/unsupported\n");
265 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
266 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
268 printf(":%s", keyname);
270 printf(" (required)");
273 ret = fit_image_hash_get_value(fit, noffset, &value,
275 printf("%s %s value: ", p, type);
277 printf("unavailable\n");
279 for (i = 0; i < value_len; i++)
280 printf("%02x", value[i]);
284 debug("%s %s len: %d\n", p, type, value_len);
286 /* Signatures have a time stamp */
287 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
290 printf("%s Timestamp: ", p);
291 if (fit_get_timestamp(fit, noffset, ×tamp))
292 printf("unavailable\n");
294 genimg_print_time(timestamp);
299 * fit_image_print_verification_data() - prints out the hash/signature details
300 * @fit: pointer to the FIT format image header
301 * @noffset: offset of the hash or signature node
302 * @p: pointer to prefix string
304 * This lists properies for the processed hash node
307 * no returned results
309 static void fit_image_print_verification_data(const void *fit, int noffset,
315 * Check subnode name, must be equal to "hash" or "signature".
316 * Multiple hash/signature nodes require unique unit node
317 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
319 name = fit_get_name(fit, noffset, NULL);
320 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
321 fit_image_print_data(fit, noffset, p, "Hash");
322 } else if (!strncmp(name, FIT_SIG_NODENAME,
323 strlen(FIT_SIG_NODENAME))) {
324 fit_image_print_data(fit, noffset, p, "Sign");
329 * fit_image_print - prints out the FIT component image details
330 * @fit: pointer to the FIT format image header
331 * @image_noffset: offset of the component image node
332 * @p: pointer to prefix string
334 * fit_image_print() lists all mandatory properies for the processed component
335 * image. If present, hash nodes are printed out as well. Load
336 * address for images of type firmware is also printed out. Since the load
337 * address is not mandatory for firmware images, it will be output as
338 * "unavailable" when not present.
341 * no returned results
343 void fit_image_print(const void *fit, int image_noffset, const char *p)
346 uint8_t type, arch, os, comp;
354 /* Mandatory properties */
355 ret = fit_get_desc(fit, image_noffset, &desc);
356 printf("%s Description: ", p);
358 printf("unavailable\n");
360 printf("%s\n", desc);
362 fit_image_get_type(fit, image_noffset, &type);
363 printf("%s Type: %s\n", p, genimg_get_type_name(type));
365 fit_image_get_comp(fit, image_noffset, &comp);
366 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
368 ret = fit_image_get_data(fit, image_noffset, &data, &size);
371 printf("%s Data Start: ", p);
373 printf("unavailable\n");
375 void *vdata = (void *)data;
377 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
381 printf("%s Data Size: ", p);
383 printf("unavailable\n");
385 genimg_print_size(size);
387 /* Remaining, type dependent properties */
388 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
389 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
390 (type == IH_TYPE_FLATDT)) {
391 fit_image_get_arch(fit, image_noffset, &arch);
392 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
395 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
396 fit_image_get_os(fit, image_noffset, &os);
397 printf("%s OS: %s\n", p, genimg_get_os_name(os));
400 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
401 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
402 ret = fit_image_get_load(fit, image_noffset, &load);
403 printf("%s Load Address: ", p);
405 printf("unavailable\n");
407 printf("0x%08lx\n", load);
410 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
411 (type == IH_TYPE_RAMDISK)) {
412 fit_image_get_entry(fit, image_noffset, &entry);
413 printf("%s Entry Point: ", p);
415 printf("unavailable\n");
417 printf("0x%08lx\n", entry);
420 /* Process all hash subnodes of the component image node */
421 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
422 (noffset >= 0) && (ndepth > 0);
423 noffset = fdt_next_node(fit, noffset, &ndepth)) {
425 /* Direct child node of the component image node */
426 fit_image_print_verification_data(fit, noffset, p);
433 * fit_get_desc - get node description property
434 * @fit: pointer to the FIT format image header
435 * @noffset: node offset
436 * @desc: double pointer to the char, will hold pointer to the descrption
438 * fit_get_desc() reads description property from a given node, if
439 * description is found pointer to it is returened in third call argument.
445 int fit_get_desc(const void *fit, int noffset, char **desc)
449 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
451 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
459 * fit_get_timestamp - get node timestamp property
460 * @fit: pointer to the FIT format image header
461 * @noffset: node offset
462 * @timestamp: pointer to the time_t, will hold read timestamp
464 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
465 * is found and has a correct size its value is retured in third call
470 * -1, on property read failure
471 * -2, on wrong timestamp size
473 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
478 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
480 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
483 if (len != sizeof(uint32_t)) {
484 debug("FIT timestamp with incorrect size of (%u)\n", len);
488 *timestamp = uimage_to_cpu(*((uint32_t *)data));
493 * fit_image_get_node - get node offset for component image of a given unit name
494 * @fit: pointer to the FIT format image header
495 * @image_uname: component image node unit name
497 * fit_image_get_node() finds a component image (withing the '/images'
498 * node) of a provided unit name. If image is found its node offset is
499 * returned to the caller.
502 * image node offset when found (>=0)
503 * negative number on failure (FDT_ERR_* code)
505 int fit_image_get_node(const void *fit, const char *image_uname)
507 int noffset, images_noffset;
509 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
510 if (images_noffset < 0) {
511 debug("Can't find images parent node '%s' (%s)\n",
512 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
513 return images_noffset;
516 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
518 debug("Can't get node offset for image unit name: '%s' (%s)\n",
519 image_uname, fdt_strerror(noffset));
526 * fit_image_get_os - get os id for a given component image node
527 * @fit: pointer to the FIT format image header
528 * @noffset: component image node offset
529 * @os: pointer to the uint8_t, will hold os numeric id
531 * fit_image_get_os() finds os property in a given component image node.
532 * If the property is found, its (string) value is translated to the numeric
533 * id which is returned to the caller.
539 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
544 /* Get OS name from property data */
545 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
547 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
552 /* Translate OS name to id */
553 *os = genimg_get_os_id(data);
558 * fit_image_get_arch - get arch id for a given component image node
559 * @fit: pointer to the FIT format image header
560 * @noffset: component image node offset
561 * @arch: pointer to the uint8_t, will hold arch numeric id
563 * fit_image_get_arch() finds arch property in a given component image node.
564 * If the property is found, its (string) value is translated to the numeric
565 * id which is returned to the caller.
571 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
576 /* Get architecture name from property data */
577 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
579 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
584 /* Translate architecture name to id */
585 *arch = genimg_get_arch_id(data);
590 * fit_image_get_type - get type id for a given component image node
591 * @fit: pointer to the FIT format image header
592 * @noffset: component image node offset
593 * @type: pointer to the uint8_t, will hold type numeric id
595 * fit_image_get_type() finds type property in a given component image node.
596 * If the property is found, its (string) value is translated to the numeric
597 * id which is returned to the caller.
603 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
608 /* Get image type name from property data */
609 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
611 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
616 /* Translate image type name to id */
617 *type = genimg_get_type_id(data);
622 * fit_image_get_comp - get comp id for a given component image node
623 * @fit: pointer to the FIT format image header
624 * @noffset: component image node offset
625 * @comp: pointer to the uint8_t, will hold comp numeric id
627 * fit_image_get_comp() finds comp property in a given component image node.
628 * If the property is found, its (string) value is translated to the numeric
629 * id which is returned to the caller.
635 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
640 /* Get compression name from property data */
641 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
643 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
648 /* Translate compression name to id */
649 *comp = genimg_get_comp_id(data);
654 * fit_image_get_load() - get load addr property for given component image node
655 * @fit: pointer to the FIT format image header
656 * @noffset: component image node offset
657 * @load: pointer to the uint32_t, will hold load address
659 * fit_image_get_load() finds load address property in a given component
660 * image node. If the property is found, its value is returned to the caller.
666 int fit_image_get_load(const void *fit, int noffset, ulong *load)
669 const uint32_t *data;
671 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
673 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
677 *load = uimage_to_cpu(*data);
682 * fit_image_get_entry() - get entry point address property
683 * @fit: pointer to the FIT format image header
684 * @noffset: component image node offset
685 * @entry: pointer to the uint32_t, will hold entry point address
687 * This gets the entry point address property for a given component image
690 * fit_image_get_entry() finds entry point address property in a given
691 * component image node. If the property is found, its value is returned
698 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
701 const uint32_t *data;
703 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
705 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
709 *entry = uimage_to_cpu(*data);
714 * fit_image_get_data - get data property and its size for a given component image node
715 * @fit: pointer to the FIT format image header
716 * @noffset: component image node offset
717 * @data: double pointer to void, will hold data property's data address
718 * @size: pointer to size_t, will hold data property's data size
720 * fit_image_get_data() finds data property in a given component image node.
721 * If the property is found its data start address and size are returned to
728 int fit_image_get_data(const void *fit, int noffset,
729 const void **data, size_t *size)
733 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
735 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
745 * fit_image_hash_get_algo - get hash algorithm name
746 * @fit: pointer to the FIT format image header
747 * @noffset: hash node offset
748 * @algo: double pointer to char, will hold pointer to the algorithm name
750 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
751 * If the property is found its data start address is returned to the caller.
757 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
761 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
763 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
771 * fit_image_hash_get_value - get hash value and length
772 * @fit: pointer to the FIT format image header
773 * @noffset: hash node offset
774 * @value: double pointer to uint8_t, will hold address of a hash value data
775 * @value_len: pointer to an int, will hold hash data length
777 * fit_image_hash_get_value() finds hash value property in a given hash node.
778 * If the property is found its data start address and size are returned to
785 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
790 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
791 if (*value == NULL) {
792 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
802 * fit_image_hash_get_ignore - get hash ignore flag
803 * @fit: pointer to the FIT format image header
804 * @noffset: hash node offset
805 * @ignore: pointer to an int, will hold hash ignore flag
807 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
808 * If the property is found and non-zero, the hash algorithm is not verified by
809 * u-boot automatically.
812 * 0, on ignore not found
813 * value, on ignore found
815 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
820 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
821 if (value == NULL || len != sizeof(int))
830 * fit_set_timestamp - set node timestamp property
831 * @fit: pointer to the FIT format image header
832 * @noffset: node offset
833 * @timestamp: timestamp value to be set
835 * fit_set_timestamp() attempts to set timestamp property in the requested
836 * node and returns operation status to the caller.
840 * -1, on property read failure
842 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
847 t = cpu_to_uimage(timestamp);
848 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
851 printf("Can't set '%s' property for '%s' node (%s)\n",
852 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
861 * calculate_hash - calculate and return hash for provided input data
862 * @data: pointer to the input data
863 * @data_len: data length
864 * @algo: requested hash algorithm
865 * @value: pointer to the char, will hold hash value data (caller must
866 * allocate enough free space)
867 * value_len: length of the calculated hash
869 * calculate_hash() computes input data hash according to the requested
871 * Resulting hash value is placed in caller provided 'value' buffer, length
872 * of the calculated hash is returned via value_len pointer argument.
876 * -1, when algo is unsupported
878 int calculate_hash(const void *data, int data_len, const char *algo,
879 uint8_t *value, int *value_len)
881 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
882 *((uint32_t *)value) = crc32_wd(0, data, data_len,
884 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
886 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
887 sha1_csum_wd((unsigned char *)data, data_len,
888 (unsigned char *)value, CHUNKSZ_SHA1);
890 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
891 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
894 debug("Unsupported hash alogrithm\n");
900 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
901 size_t size, char **err_msgp)
903 uint8_t value[FIT_MAX_HASH_LEN];
912 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
913 *err_msgp = "Can't get hash algo property";
918 if (IMAGE_ENABLE_IGNORE) {
919 fit_image_hash_get_ignore(fit, noffset, &ignore);
926 if (fit_image_hash_get_value(fit, noffset, &fit_value,
928 *err_msgp = "Can't get hash value property";
932 if (calculate_hash(data, size, algo, value, &value_len)) {
933 *err_msgp = "Unsupported hash algorithm";
937 if (value_len != fit_value_len) {
938 *err_msgp = "Bad hash value len";
940 } else if (memcmp(value, fit_value, value_len) != 0) {
941 *err_msgp = "Bad hash value";
949 * fit_image_verify - verify data intergity
950 * @fit: pointer to the FIT format image header
951 * @image_noffset: component image node offset
953 * fit_image_verify() goes over component image hash nodes,
954 * re-calculates each data hash and compares with the value stored in hash
958 * 1, if all hashes are valid
959 * 0, otherwise (or on error)
961 int fit_image_verify(const void *fit, int image_noffset)
970 /* Get image data and data length */
971 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
972 err_msg = "Can't get image data/size";
976 /* Verify all required signatures */
977 if (IMAGE_ENABLE_VERIFY &&
978 fit_image_verify_required_sigs(fit, image_noffset, data, size,
979 gd_fdt_blob(), &verify_all)) {
980 err_msg = "Unable to verify required signature";
984 /* Process all hash subnodes of the component image node */
985 for (noffset = fdt_first_subnode(fit, image_noffset);
987 noffset = fdt_next_subnode(fit, noffset)) {
988 const char *name = fit_get_name(fit, noffset, NULL);
991 * Check subnode name, must be equal to "hash".
992 * Multiple hash nodes require unique unit node
993 * names, e.g. hash@1, hash@2, etc.
995 if (!strncmp(name, FIT_HASH_NODENAME,
996 strlen(FIT_HASH_NODENAME))) {
997 if (fit_image_check_hash(fit, noffset, data, size,
1001 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1002 !strncmp(name, FIT_SIG_NODENAME,
1003 strlen(FIT_SIG_NODENAME))) {
1004 ret = fit_image_check_sig(fit, noffset, data,
1005 size, -1, &err_msg);
1013 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1014 err_msg = "Corrupted or truncated tree";
1021 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1022 err_msg, fit_get_name(fit, noffset, NULL),
1023 fit_get_name(fit, image_noffset, NULL));
1028 * fit_all_image_verify - verify data intergity for all images
1029 * @fit: pointer to the FIT format image header
1031 * fit_all_image_verify() goes over all images in the FIT and
1032 * for every images checks if all it's hashes are valid.
1035 * 1, if all hashes of all images are valid
1036 * 0, otherwise (or on error)
1038 int fit_all_image_verify(const void *fit)
1045 /* Find images parent node offset */
1046 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1047 if (images_noffset < 0) {
1048 printf("Can't find images parent node '%s' (%s)\n",
1049 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1053 /* Process all image subnodes, check hashes for each */
1054 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1056 for (ndepth = 0, count = 0,
1057 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1058 (noffset >= 0) && (ndepth > 0);
1059 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1062 * Direct child node of the images parent node,
1063 * i.e. component image node.
1065 printf(" Hash(es) for Image %u (%s): ", count++,
1066 fit_get_name(fit, noffset, NULL));
1068 if (!fit_image_verify(fit, noffset))
1077 * fit_image_check_os - check whether image node is of a given os type
1078 * @fit: pointer to the FIT format image header
1079 * @noffset: component image node offset
1080 * @os: requested image os
1082 * fit_image_check_os() reads image os property and compares its numeric
1083 * id with the requested os. Comparison result is returned to the caller.
1086 * 1 if image is of given os type
1087 * 0 otherwise (or on error)
1089 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1093 if (fit_image_get_os(fit, noffset, &image_os))
1095 return (os == image_os);
1099 * fit_image_check_arch - check whether image node is of a given arch
1100 * @fit: pointer to the FIT format image header
1101 * @noffset: component image node offset
1102 * @arch: requested imagearch
1104 * fit_image_check_arch() reads image arch property and compares its numeric
1105 * id with the requested arch. Comparison result is returned to the caller.
1108 * 1 if image is of given arch
1109 * 0 otherwise (or on error)
1111 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1115 if (fit_image_get_arch(fit, noffset, &image_arch))
1117 return (arch == image_arch);
1121 * fit_image_check_type - check whether image node is of a given type
1122 * @fit: pointer to the FIT format image header
1123 * @noffset: component image node offset
1124 * @type: requested image type
1126 * fit_image_check_type() reads image type property and compares its numeric
1127 * id with the requested type. Comparison result is returned to the caller.
1130 * 1 if image is of given type
1131 * 0 otherwise (or on error)
1133 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1137 if (fit_image_get_type(fit, noffset, &image_type))
1139 return (type == image_type);
1143 * fit_image_check_comp - check whether image node uses given compression
1144 * @fit: pointer to the FIT format image header
1145 * @noffset: component image node offset
1146 * @comp: requested image compression type
1148 * fit_image_check_comp() reads image compression property and compares its
1149 * numeric id with the requested compression type. Comparison result is
1150 * returned to the caller.
1153 * 1 if image uses requested compression
1154 * 0 otherwise (or on error)
1156 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1160 if (fit_image_get_comp(fit, noffset, &image_comp))
1162 return (comp == image_comp);
1166 * fit_check_format - sanity check FIT image format
1167 * @fit: pointer to the FIT format image header
1169 * fit_check_format() runs a basic sanity FIT image verification.
1170 * Routine checks for mandatory properties, nodes, etc.
1176 int fit_check_format(const void *fit)
1178 /* mandatory / node 'description' property */
1179 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1180 debug("Wrong FIT format: no description\n");
1184 if (IMAGE_ENABLE_TIMESTAMP) {
1185 /* mandatory / node 'timestamp' property */
1186 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1187 debug("Wrong FIT format: no timestamp\n");
1192 /* mandatory subimages parent '/images' node */
1193 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1194 debug("Wrong FIT format: no images parent node\n");
1203 * fit_conf_find_compat
1204 * @fit: pointer to the FIT format image header
1205 * @fdt: pointer to the device tree to compare against
1207 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1208 * most compatible with the passed in device tree.
1217 * |-o configurations
1225 * |-compatible = "foo,bar", "bim,bam"
1228 * |-compatible = "foo,bar",
1231 * |-compatible = "bim,bam", "baz,biz"
1233 * Configuration 1 would be picked because the first string in U-Boot's
1234 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1235 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1238 * offset to the configuration to use if one was found
1241 int fit_conf_find_compat(const void *fit, const void *fdt)
1244 int noffset, confs_noffset, images_noffset;
1245 const void *fdt_compat;
1247 int best_match_offset = 0;
1248 int best_match_pos = 0;
1250 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1251 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1252 if (confs_noffset < 0 || images_noffset < 0) {
1253 debug("Can't find configurations or images nodes.\n");
1257 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1259 debug("Fdt for comparison has no \"compatible\" property.\n");
1264 * Loop over the configurations in the FIT image.
1266 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1267 (noffset >= 0) && (ndepth > 0);
1268 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1270 const char *kfdt_name;
1272 const char *cur_fdt_compat;
1280 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1282 debug("No fdt property found.\n");
1285 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1287 if (kfdt_noffset < 0) {
1288 debug("No image node named \"%s\" found.\n",
1293 * Get a pointer to this configuration's fdt.
1295 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1296 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1300 len = fdt_compat_len;
1301 cur_fdt_compat = fdt_compat;
1303 * Look for a match for each U-Boot compatibility string in
1304 * turn in this configuration's fdt.
1306 for (i = 0; len > 0 &&
1307 (!best_match_offset || best_match_pos > i); i++) {
1308 int cur_len = strlen(cur_fdt_compat) + 1;
1310 if (!fdt_node_check_compatible(kfdt, 0,
1312 best_match_offset = noffset;
1317 cur_fdt_compat += cur_len;
1320 if (!best_match_offset) {
1321 debug("No match found.\n");
1325 return best_match_offset;
1329 * fit_conf_get_node - get node offset for configuration of a given unit name
1330 * @fit: pointer to the FIT format image header
1331 * @conf_uname: configuration node unit name
1333 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1334 * parant node) of a provided unit name. If configuration is found its node
1335 * offset is returned to the caller.
1337 * When NULL is provided in second argument fit_conf_get_node() will search
1338 * for a default configuration node instead. Default configuration node unit
1339 * name is retrived from FIT_DEFAULT_PROP property of the '/configurations'
1343 * configuration node offset when found (>=0)
1344 * negative number on failure (FDT_ERR_* code)
1346 int fit_conf_get_node(const void *fit, const char *conf_uname)
1348 int noffset, confs_noffset;
1351 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1352 if (confs_noffset < 0) {
1353 debug("Can't find configurations parent node '%s' (%s)\n",
1354 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1355 return confs_noffset;
1358 if (conf_uname == NULL) {
1359 /* get configuration unit name from the default property */
1360 debug("No configuration specified, trying default...\n");
1361 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1362 FIT_DEFAULT_PROP, &len);
1363 if (conf_uname == NULL) {
1364 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1368 debug("Found default configuration: '%s'\n", conf_uname);
1371 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1373 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1374 conf_uname, fdt_strerror(noffset));
1380 int fit_conf_get_prop_node(const void *fit, int noffset,
1381 const char *prop_name)
1386 /* get kernel image unit name from configuration kernel property */
1387 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1391 return fit_image_get_node(fit, uname);
1395 * fit_conf_print - prints out the FIT configuration details
1396 * @fit: pointer to the FIT format image header
1397 * @noffset: offset of the configuration node
1398 * @p: pointer to prefix string
1400 * fit_conf_print() lists all mandatory properies for the processed
1401 * configuration node.
1404 * no returned results
1406 void fit_conf_print(const void *fit, int noffset, const char *p)
1412 /* Mandatory properties */
1413 ret = fit_get_desc(fit, noffset, &desc);
1414 printf("%s Description: ", p);
1416 printf("unavailable\n");
1418 printf("%s\n", desc);
1420 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1421 printf("%s Kernel: ", p);
1423 printf("unavailable\n");
1425 printf("%s\n", uname);
1427 /* Optional properties */
1428 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1430 printf("%s Init Ramdisk: %s\n", p, uname);
1432 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1434 printf("%s FDT: %s\n", p, uname);
1437 int fit_image_select(const void *fit, int rd_noffset, int verify)
1439 fit_image_print(fit, rd_noffset, " ");
1442 puts(" Verifying Hash Integrity ... ");
1443 if (!fit_image_verify(fit, rd_noffset)) {
1444 puts("Bad Data Hash\n");
1453 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1460 debug("* %s: using config '%s' from image at 0x%08lx\n",
1461 prop_name, images->fit_uname_cfg, addr);
1463 /* Check whether configuration has this property defined */
1464 fit_hdr = map_sysmem(addr, 0);
1465 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1466 if (cfg_noffset < 0) {
1467 debug("* %s: no such config\n", prop_name);
1471 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1473 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1480 int fit_image_load(bootm_headers_t *images, const char *prop_name, ulong addr,
1481 const char **fit_unamep, const char *fit_uname_config,
1482 int arch, int image_type, int bootstage_id,
1483 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1485 int cfg_noffset, noffset;
1486 const char *fit_uname;
1491 ulong load, data, len;
1494 fit = map_sysmem(addr, 0);
1495 fit_uname = fit_unamep ? *fit_unamep : NULL;
1496 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1498 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1499 if (!fit_check_format(fit)) {
1500 printf("Bad FIT %s image format!\n", prop_name);
1501 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1504 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1506 /* get ramdisk component image node offset */
1507 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1508 noffset = fit_image_get_node(fit, fit_uname);
1511 * no image node unit name, try to get config
1512 * node first. If config unit node name is NULL
1513 * fit_conf_get_node() will try to find default config node
1515 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1516 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1517 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1519 cfg_noffset = fit_conf_get_node(fit,
1522 if (cfg_noffset < 0) {
1523 puts("Could not find configuration node\n");
1524 bootstage_error(bootstage_id +
1525 BOOTSTAGE_SUB_NO_UNIT_NAME);
1528 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1529 printf(" Using '%s' configuration\n", fit_uname_config);
1530 if (image_type == IH_TYPE_KERNEL) {
1531 /* Remember (and possibly verify) this config */
1532 images->fit_uname_cfg = fit_uname_config;
1533 if (IMAGE_ENABLE_VERIFY && images->verify) {
1534 puts(" Verifying Hash Integrity ... ");
1535 if (!fit_config_verify(fit, cfg_noffset)) {
1536 puts("Bad Data Hash\n");
1537 bootstage_error(bootstage_id +
1538 BOOTSTAGE_SUB_HASH);
1543 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1546 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1548 fit_uname = fit_get_name(fit, noffset, NULL);
1551 puts("Could not find subimage node\n");
1552 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1556 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1558 ret = fit_image_select(fit, noffset, images->verify);
1560 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1564 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1565 if (!fit_image_check_target_arch(fit, noffset)) {
1566 puts("Unsupported Architecture\n");
1567 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1571 if (image_type == IH_TYPE_FLATDT &&
1572 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1573 puts("FDT image is compressed");
1574 return -EPROTONOSUPPORT;
1577 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1578 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1579 (image_type == IH_TYPE_KERNEL &&
1580 fit_image_check_type(fit, noffset,
1581 IH_TYPE_KERNEL_NOLOAD));
1582 os_ok = image_type == IH_TYPE_FLATDT ||
1583 fit_image_check_os(fit, noffset, IH_OS_LINUX);
1584 if (!type_ok || !os_ok) {
1585 printf("No Linux %s %s Image\n", genimg_get_arch_name(arch),
1586 genimg_get_type_name(image_type));
1587 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1591 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1593 /* get image data address and length */
1594 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1595 printf("Could not find %s subimage data!\n", prop_name);
1596 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
1601 /* verify that image data is a proper FDT blob */
1602 if (image_type == IH_TYPE_FLATDT && fdt_check_header((char *)buf)) {
1603 puts("Subimage data is not a FDT");
1607 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1610 * Work-around for eldk-4.2 which gives this warning if we try to
1611 * case in the unmap_sysmem() call:
1612 * warning: initialization discards qualifiers from pointer target type
1615 void *vbuf = (void *)buf;
1617 data = map_to_sysmem(vbuf);
1620 if (load_op == FIT_LOAD_IGNORED) {
1622 } else if (fit_image_get_load(fit, noffset, &load)) {
1623 if (load_op == FIT_LOAD_REQUIRED) {
1624 printf("Can't get %s subimage load address!\n",
1626 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1630 ulong image_start, image_end;
1635 * move image data to the load address,
1636 * make sure we don't overwrite initial image
1639 image_end = addr + fit_get_size(fit);
1641 load_end = load + len;
1642 if (image_type != IH_TYPE_KERNEL &&
1643 load < image_end && load_end > image_start) {
1644 printf("Error: %s overwritten\n", prop_name);
1648 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1649 prop_name, data, load);
1651 dst = map_sysmem(load, len);
1652 memmove(dst, buf, len);
1655 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1660 *fit_unamep = (char *)fit_uname;