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"
20 #include <u-boot/crc.h>
22 static image_header_t header
;
24 static int fit_verify_header (unsigned char *ptr
, int image_size
,
25 struct image_tool_params
*params
)
27 return fdt_check_header(ptr
);
30 static int fit_check_image_types (uint8_t type
)
32 if (type
== IH_TYPE_FLATDT
)
38 int mmap_fdt(struct image_tool_params
*params
, const char *fname
, void **blobp
,
44 /* Load FIT blob into memory (we need to write hashes/signatures) */
45 fd
= open(fname
, O_RDWR
| O_BINARY
);
48 fprintf(stderr
, "%s: Can't open %s: %s\n",
49 params
->cmdname
, fname
, strerror(errno
));
54 if (fstat(fd
, sbuf
) < 0) {
55 fprintf(stderr
, "%s: Can't stat %s: %s\n",
56 params
->cmdname
, fname
, strerror(errno
));
61 ptr
= mmap(0, sbuf
->st_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
62 if (ptr
== MAP_FAILED
) {
63 fprintf(stderr
, "%s: Can't read %s: %s\n",
64 params
->cmdname
, fname
, strerror(errno
));
69 /* check if ptr has a valid blob */
70 if (fdt_check_header(ptr
)) {
71 fprintf(stderr
, "%s: Invalid FIT blob\n", params
->cmdname
);
81 * fit_handle_file - main FIT file processing function
83 * fit_handle_file() runs dtc to convert .its to .itb, includes
84 * binary data, updates timestamp property and calculates hashes.
86 * datafile - .its file
87 * imagefile - .itb file
90 * only on success, otherwise calls exit (EXIT_FAILURE);
92 static int fit_handle_file(struct image_tool_params
*params
)
94 char tmpfile
[MKIMAGE_MAX_TMPFILE_LEN
];
95 char cmd
[MKIMAGE_MAX_DTC_CMDLINE_LEN
];
97 void *dest_blob
= NULL
;
100 off_t destfd_size
= 0;
102 /* Flattened Image Tree (FIT) format handling */
103 debug ("FIT format handling\n");
105 /* call dtc to include binary properties into the tmp file */
106 if (strlen (params
->imagefile
) +
107 strlen (MKIMAGE_TMPFILE_SUFFIX
) + 1 > sizeof (tmpfile
)) {
108 fprintf (stderr
, "%s: Image file name (%s) too long, "
109 "can't create tmpfile",
110 params
->imagefile
, params
->cmdname
);
111 return (EXIT_FAILURE
);
113 sprintf (tmpfile
, "%s%s", params
->imagefile
, MKIMAGE_TMPFILE_SUFFIX
);
115 /* We either compile the source file, or use the existing FIT image */
116 if (params
->datafile
) {
117 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
118 snprintf(cmd
, sizeof(cmd
), "%s %s %s > %s",
119 MKIMAGE_DTC
, params
->dtc
, params
->datafile
, tmpfile
);
120 debug("Trying to execute \"%s\"\n", cmd
);
122 snprintf(cmd
, sizeof(cmd
), "cp %s %s",
123 params
->imagefile
, tmpfile
);
125 if (system (cmd
) == -1) {
126 fprintf (stderr
, "%s: system(%s) failed: %s\n",
127 params
->cmdname
, cmd
, strerror(errno
));
131 if (params
->keydest
) {
132 destfd
= mmap_fdt(params
, params
->keydest
, &dest_blob
, &sbuf
);
135 destfd_size
= sbuf
.st_size
;
138 tfd
= mmap_fdt(params
, tmpfile
, &ptr
, &sbuf
);
142 /* set hashes for images in the blob */
143 if (fit_add_verification_data(params
->keydir
,
144 dest_blob
, ptr
, params
->comment
,
145 params
->require_keys
)) {
146 fprintf(stderr
, "%s Can't add hashes to FIT blob\n",
151 /* for first image creation, add a timestamp at offset 0 i.e., root */
152 if (params
->datafile
&& fit_set_timestamp(ptr
, 0, sbuf
.st_mtime
)) {
153 fprintf (stderr
, "%s: Can't add image timestamp\n",
155 goto err_add_timestamp
;
157 debug ("Added timestamp successfully\n");
159 munmap ((void *)ptr
, sbuf
.st_size
);
162 munmap(dest_blob
, destfd_size
);
166 if (rename (tmpfile
, params
->imagefile
) == -1) {
167 fprintf (stderr
, "%s: Can't rename %s to %s: %s\n",
168 params
->cmdname
, tmpfile
, params
->imagefile
,
171 unlink (params
->imagefile
);
172 return (EXIT_FAILURE
);
174 return (EXIT_SUCCESS
);
178 munmap(ptr
, sbuf
.st_size
);
181 munmap(dest_blob
, destfd_size
);
188 static int fit_check_params(struct image_tool_params
*params
)
190 return ((params
->dflag
&& (params
->fflag
|| params
->lflag
)) ||
191 (params
->fflag
&& (params
->dflag
|| params
->lflag
)) ||
192 (params
->lflag
&& (params
->dflag
|| params
->fflag
)));
195 static struct image_type_params fitimage_params
= {
196 .name
= "FIT Image support",
197 .header_size
= sizeof(image_header_t
),
198 .hdr
= (void*)&header
,
199 .verify_header
= fit_verify_header
,
200 .print_header
= fit_print_contents
,
201 .check_image_type
= fit_check_image_types
,
202 .fflag_handle
= fit_handle_file
,
203 .set_header
= NULL
, /* FIT images use DTB header */
204 .check_params
= fit_check_params
,
207 void init_fit_image_type (void)
209 register_image_type(&fitimage_params
);