2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2004
5 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
8 * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
9 * FIT image specific code abstracted from mkimage.c
10 * some functions added to address abstraction
12 * All rights reserved.
14 * SPDX-License-Identifier: GPL-2.0+
17 #include "imagetool.h"
18 #include "fit_common.h"
21 #include <u-boot/crc.h>
23 static image_header_t header
;
25 static int fit_add_file_data(struct image_tool_params
*params
, size_t size_inc
,
29 void *dest_blob
= NULL
;
30 off_t destfd_size
= 0;
35 tfd
= mmap_fdt(params
->cmdname
, tmpfile
, size_inc
, &ptr
, &sbuf
, true);
39 if (params
->keydest
) {
40 struct stat dest_sbuf
;
42 destfd
= mmap_fdt(params
->cmdname
, params
->keydest
, size_inc
,
43 &dest_blob
, &dest_sbuf
, false);
48 destfd_size
= dest_sbuf
.st_size
;
51 /* for first image creation, add a timestamp at offset 0 i.e., root */
53 ret
= fit_set_timestamp(ptr
, 0, sbuf
.st_mtime
);
56 ret
= fit_add_verification_data(params
->keydir
, dest_blob
, ptr
,
58 params
->require_keys
);
62 munmap(dest_blob
, destfd_size
);
67 munmap(ptr
, sbuf
.st_size
);
74 * fit_handle_file - main FIT file processing function
76 * fit_handle_file() runs dtc to convert .its to .itb, includes
77 * binary data, updates timestamp property and calculates hashes.
79 * datafile - .its file
80 * imagefile - .itb file
83 * only on success, otherwise calls exit (EXIT_FAILURE);
85 static int fit_handle_file(struct image_tool_params
*params
)
87 char tmpfile
[MKIMAGE_MAX_TMPFILE_LEN
];
88 char cmd
[MKIMAGE_MAX_DTC_CMDLINE_LEN
];
92 /* Flattened Image Tree (FIT) format handling */
93 debug ("FIT format handling\n");
95 /* call dtc to include binary properties into the tmp file */
96 if (strlen (params
->imagefile
) +
97 strlen (MKIMAGE_TMPFILE_SUFFIX
) + 1 > sizeof (tmpfile
)) {
98 fprintf (stderr
, "%s: Image file name (%s) too long, "
99 "can't create tmpfile",
100 params
->imagefile
, params
->cmdname
);
101 return (EXIT_FAILURE
);
103 sprintf (tmpfile
, "%s%s", params
->imagefile
, MKIMAGE_TMPFILE_SUFFIX
);
105 /* We either compile the source file, or use the existing FIT image */
106 if (params
->datafile
) {
107 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
108 snprintf(cmd
, sizeof(cmd
), "%s %s %s > %s",
109 MKIMAGE_DTC
, params
->dtc
, params
->datafile
, tmpfile
);
110 debug("Trying to execute \"%s\"\n", cmd
);
112 snprintf(cmd
, sizeof(cmd
), "cp %s %s",
113 params
->imagefile
, tmpfile
);
115 if (system (cmd
) == -1) {
116 fprintf (stderr
, "%s: system(%s) failed: %s\n",
117 params
->cmdname
, cmd
, strerror(errno
));
122 * Set hashes for images in the blob. Unfortunately we may need more
123 * space in either FDT, so keep trying until we succeed.
125 * Note: this is pretty inefficient for signing, since we must
126 * calculate the signature every time. It would be better to calculate
127 * all the data and then store it in a separate step. However, this
128 * would be considerably more complex to implement. Generally a few
129 * steps of this loop is enough to sign with several keys.
131 for (size_inc
= 0; size_inc
< 64 * 1024; size_inc
+= 1024) {
132 ret
= fit_add_file_data(params
, size_inc
, tmpfile
);
133 if (!ret
|| ret
!= -ENOSPC
)
138 fprintf(stderr
, "%s Can't add hashes to FIT blob\n",
143 if (rename (tmpfile
, params
->imagefile
) == -1) {
144 fprintf (stderr
, "%s: Can't rename %s to %s: %s\n",
145 params
->cmdname
, tmpfile
, params
->imagefile
,
148 unlink (params
->imagefile
);
159 * fit_image_extract - extract a FIT component image
160 * @fit: pointer to the FIT format image header
161 * @image_noffset: offset of the component image node
162 * @file_name: name of the file to store the FIT sub-image
165 * zero in case of success or a negative value if fail.
167 static int fit_image_extract(
170 const char *file_name
)
172 const void *file_data
;
173 size_t file_size
= 0;
175 /* get the "data" property of component at offset "image_noffset" */
176 fit_image_get_data(fit
, image_noffset
, &file_data
, &file_size
);
178 /* save the "file_data" into the file specified by "file_name" */
179 return imagetool_save_subimage(file_name
, (ulong
) file_data
, file_size
);
183 * fit_extract_contents - retrieve a sub-image component from the FIT image
184 * @ptr: pointer to the FIT format image header
185 * @params: command line parameters
188 * zero in case of success or a negative value if fail.
190 static int fit_extract_contents(void *ptr
, struct image_tool_params
*params
)
195 const void *fit
= ptr
;
199 /* Indent string is defined in header image.h */
200 p
= IMAGE_INDENT_STRING
;
202 if (!fit_check_format(fit
)) {
203 printf("Bad FIT image format\n");
207 /* Find images parent node offset */
208 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
209 if (images_noffset
< 0) {
210 printf("Can't find images parent node '%s' (%s)\n",
211 FIT_IMAGES_PATH
, fdt_strerror(images_noffset
));
215 /* Avoid any overrun */
216 count
= fit_get_subimage_count(fit
, images_noffset
);
217 if ((params
->pflag
< 0) || (count
<= params
->pflag
)) {
218 printf("No such component at '%d'\n", params
->pflag
);
222 /* Process its subnodes, extract the desired component from image */
223 for (ndepth
= 0, count
= 0,
224 noffset
= fdt_next_node(fit
, images_noffset
, &ndepth
);
225 (noffset
>= 0) && (ndepth
> 0);
226 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
229 * Direct child node of the images parent node,
230 * i.e. component image node.
232 if (params
->pflag
== count
) {
233 printf("Extracted:\n%s Image %u (%s)\n", p
,
234 count
, fit_get_name(fit
, noffset
, NULL
));
236 fit_image_print(fit
, noffset
, p
);
238 return fit_image_extract(fit
, noffset
,
249 static int fit_check_params(struct image_tool_params
*params
)
251 return ((params
->dflag
&& (params
->fflag
|| params
->lflag
)) ||
252 (params
->fflag
&& (params
->dflag
|| params
->lflag
)) ||
253 (params
->lflag
&& (params
->dflag
|| params
->fflag
)));
259 sizeof(image_header_t
),
265 fit_extract_contents
,
266 fit_check_image_types
,
268 NULL
/* FIT images use DTB header */