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 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <u-boot/crc.h>
34 static image_header_t header;
36 static int fit_verify_header (unsigned char *ptr, int image_size,
37 struct mkimage_params *params)
39 return fdt_check_header ((void *)ptr);
42 static int fit_check_image_types (uint8_t type)
44 if (type == IH_TYPE_FLATDT)
50 int mmap_fdt(struct mkimage_params *params, const char *fname, void **blobp,
56 /* Load FIT blob into memory (we need to write hashes/signatures) */
57 fd = open(fname, O_RDWR | O_BINARY);
60 fprintf(stderr, "%s: Can't open %s: %s\n",
61 params->cmdname, fname, strerror(errno));
66 if (fstat(fd, sbuf) < 0) {
67 fprintf(stderr, "%s: Can't stat %s: %s\n",
68 params->cmdname, fname, strerror(errno));
73 ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
74 if (ptr == MAP_FAILED) {
75 fprintf(stderr, "%s: Can't read %s: %s\n",
76 params->cmdname, fname, strerror(errno));
81 /* check if ptr has a valid blob */
82 if (fdt_check_header(ptr)) {
83 fprintf(stderr, "%s: Invalid FIT blob\n", params->cmdname);
93 * fit_handle_file - main FIT file processing function
95 * fit_handle_file() runs dtc to convert .its to .itb, includes
96 * binary data, updates timestamp property and calculates hashes.
98 * datafile - .its file
99 * imagefile - .itb file
102 * only on success, otherwise calls exit (EXIT_FAILURE);
104 static int fit_handle_file (struct mkimage_params *params)
106 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
107 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
109 void *dest_blob = NULL;
112 off_t destfd_size = 0;
114 /* Flattened Image Tree (FIT) format handling */
115 debug ("FIT format handling\n");
117 /* call dtc to include binary properties into the tmp file */
118 if (strlen (params->imagefile) +
119 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
120 fprintf (stderr, "%s: Image file name (%s) too long, "
121 "can't create tmpfile",
122 params->imagefile, params->cmdname);
123 return (EXIT_FAILURE);
125 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
127 /* We either compile the source file, or use the existing FIT image */
128 if (params->datafile) {
129 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
130 snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
131 MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
132 debug("Trying to execute \"%s\"\n", cmd);
134 snprintf(cmd, sizeof(cmd), "cp %s %s",
135 params->imagefile, tmpfile);
137 if (system (cmd) == -1) {
138 fprintf (stderr, "%s: system(%s) failed: %s\n",
139 params->cmdname, cmd, strerror(errno));
143 if (params->keydest) {
144 destfd = mmap_fdt(params, params->keydest, &dest_blob, &sbuf);
147 destfd_size = sbuf.st_size;
150 tfd = mmap_fdt(params, tmpfile, &ptr, &sbuf);
154 /* set hashes for images in the blob */
155 if (fit_add_verification_data(params->keydir, dest_blob, ptr,
157 fprintf (stderr, "%s Can't add hashes to FIT blob",
162 /* for first image creation, add a timestamp at offset 0 i.e., root */
163 if (params->datafile && fit_set_timestamp(ptr, 0, sbuf.st_mtime)) {
164 fprintf (stderr, "%s: Can't add image timestamp\n",
166 goto err_add_timestamp;
168 debug ("Added timestamp successfully\n");
170 munmap ((void *)ptr, sbuf.st_size);
173 munmap(dest_blob, destfd_size);
177 if (rename (tmpfile, params->imagefile) == -1) {
178 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
179 params->cmdname, tmpfile, params->imagefile,
182 unlink (params->imagefile);
183 return (EXIT_FAILURE);
185 return (EXIT_SUCCESS);
189 munmap(ptr, sbuf.st_size);
192 munmap(dest_blob, destfd_size);
199 static int fit_check_params (struct mkimage_params *params)
201 return ((params->dflag && (params->fflag || params->lflag)) ||
202 (params->fflag && (params->dflag || params->lflag)) ||
203 (params->lflag && (params->dflag || params->fflag)));
206 static struct image_type_params fitimage_params = {
207 .name = "FIT Image support",
208 .header_size = sizeof(image_header_t),
209 .hdr = (void*)&header,
210 .verify_header = fit_verify_header,
211 .print_header = fit_print_contents,
212 .check_image_type = fit_check_image_types,
213 .fflag_handle = fit_handle_file,
214 .set_header = NULL, /* FIT images use DTB header */
215 .check_params = fit_check_params,
218 void init_fit_image_type (void)
220 mkimage_register (&fitimage_params);