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+
19 #include <u-boot/crc.h>
21 static image_header_t header;
23 static int fit_verify_header (unsigned char *ptr, int image_size,
24 struct mkimage_params *params)
26 return fdt_check_header ((void *)ptr);
29 static int fit_check_image_types (uint8_t type)
31 if (type == IH_TYPE_FLATDT)
37 int mmap_fdt(struct mkimage_params *params, const char *fname, void **blobp,
43 /* Load FIT blob into memory (we need to write hashes/signatures) */
44 fd = open(fname, O_RDWR | O_BINARY);
47 fprintf(stderr, "%s: Can't open %s: %s\n",
48 params->cmdname, fname, strerror(errno));
53 if (fstat(fd, sbuf) < 0) {
54 fprintf(stderr, "%s: Can't stat %s: %s\n",
55 params->cmdname, fname, strerror(errno));
60 ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
61 if (ptr == MAP_FAILED) {
62 fprintf(stderr, "%s: Can't read %s: %s\n",
63 params->cmdname, fname, strerror(errno));
68 /* check if ptr has a valid blob */
69 if (fdt_check_header(ptr)) {
70 fprintf(stderr, "%s: Invalid FIT blob\n", params->cmdname);
80 * fit_handle_file - main FIT file processing function
82 * fit_handle_file() runs dtc to convert .its to .itb, includes
83 * binary data, updates timestamp property and calculates hashes.
85 * datafile - .its file
86 * imagefile - .itb file
89 * only on success, otherwise calls exit (EXIT_FAILURE);
91 static int fit_handle_file (struct mkimage_params *params)
93 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
94 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
96 void *dest_blob = NULL;
99 off_t destfd_size = 0;
101 /* Flattened Image Tree (FIT) format handling */
102 debug ("FIT format handling\n");
104 /* call dtc to include binary properties into the tmp file */
105 if (strlen (params->imagefile) +
106 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
107 fprintf (stderr, "%s: Image file name (%s) too long, "
108 "can't create tmpfile",
109 params->imagefile, params->cmdname);
110 return (EXIT_FAILURE);
112 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
114 /* We either compile the source file, or use the existing FIT image */
115 if (params->datafile) {
116 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
117 snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
118 MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
119 debug("Trying to execute \"%s\"\n", cmd);
121 snprintf(cmd, sizeof(cmd), "cp %s %s",
122 params->imagefile, tmpfile);
124 if (system (cmd) == -1) {
125 fprintf (stderr, "%s: system(%s) failed: %s\n",
126 params->cmdname, cmd, strerror(errno));
130 if (params->keydest) {
131 destfd = mmap_fdt(params, params->keydest, &dest_blob, &sbuf);
134 destfd_size = sbuf.st_size;
137 tfd = mmap_fdt(params, tmpfile, &ptr, &sbuf);
141 /* set hashes for images in the blob */
142 if (fit_add_verification_data(params->keydir,
143 dest_blob, ptr, params->comment,
144 params->require_keys)) {
145 fprintf(stderr, "%s Can't add hashes to FIT blob\n",
150 /* for first image creation, add a timestamp at offset 0 i.e., root */
151 if (params->datafile && fit_set_timestamp(ptr, 0, sbuf.st_mtime)) {
152 fprintf (stderr, "%s: Can't add image timestamp\n",
154 goto err_add_timestamp;
156 debug ("Added timestamp successfully\n");
158 munmap ((void *)ptr, sbuf.st_size);
161 munmap(dest_blob, destfd_size);
165 if (rename (tmpfile, params->imagefile) == -1) {
166 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
167 params->cmdname, tmpfile, params->imagefile,
170 unlink (params->imagefile);
171 return (EXIT_FAILURE);
173 return (EXIT_SUCCESS);
177 munmap(ptr, sbuf.st_size);
180 munmap(dest_blob, destfd_size);
187 static int fit_check_params (struct mkimage_params *params)
189 return ((params->dflag && (params->fflag || params->lflag)) ||
190 (params->fflag && (params->dflag || params->lflag)) ||
191 (params->lflag && (params->dflag || params->fflag)));
194 static struct image_type_params fitimage_params = {
195 .name = "FIT Image support",
196 .header_size = sizeof(image_header_t),
197 .hdr = (void*)&header,
198 .verify_header = fit_verify_header,
199 .print_header = fit_print_contents,
200 .check_image_type = fit_check_image_types,
201 .fflag_handle = fit_handle_file,
202 .set_header = NULL, /* FIT images use DTB header */
203 .check_params = fit_check_params,
206 void init_fit_image_type (void)
208 mkimage_register (&fitimage_params);