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;
26 * fit_handle_file - main FIT file processing function
28 * fit_handle_file() runs dtc to convert .its to .itb, includes
29 * binary data, updates timestamp property and calculates hashes.
31 * datafile - .its file
32 * imagefile - .itb file
35 * only on success, otherwise calls exit (EXIT_FAILURE);
37 static int fit_handle_file(struct image_tool_params *params)
39 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
40 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
42 void *dest_blob = NULL;
45 off_t destfd_size = 0;
47 /* Flattened Image Tree (FIT) format handling */
48 debug ("FIT format handling\n");
50 /* call dtc to include binary properties into the tmp file */
51 if (strlen (params->imagefile) +
52 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
53 fprintf (stderr, "%s: Image file name (%s) too long, "
54 "can't create tmpfile",
55 params->imagefile, params->cmdname);
56 return (EXIT_FAILURE);
58 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
60 /* We either compile the source file, or use the existing FIT image */
61 if (params->datafile) {
62 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
63 snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
64 MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
65 debug("Trying to execute \"%s\"\n", cmd);
67 snprintf(cmd, sizeof(cmd), "cp %s %s",
68 params->imagefile, tmpfile);
70 if (system (cmd) == -1) {
71 fprintf (stderr, "%s: system(%s) failed: %s\n",
72 params->cmdname, cmd, strerror(errno));
76 if (params->keydest) {
77 destfd = mmap_fdt(params->cmdname, params->keydest,
78 &dest_blob, &sbuf, 1);
81 destfd_size = sbuf.st_size;
84 tfd = mmap_fdt(params->cmdname, tmpfile, &ptr, &sbuf, 1);
88 /* set hashes for images in the blob */
89 if (fit_add_verification_data(params->keydir,
90 dest_blob, ptr, params->comment,
91 params->require_keys)) {
92 fprintf(stderr, "%s Can't add hashes to FIT blob\n",
97 /* for first image creation, add a timestamp at offset 0 i.e., root */
98 if (params->datafile && fit_set_timestamp(ptr, 0, sbuf.st_mtime)) {
99 fprintf (stderr, "%s: Can't add image timestamp\n",
101 goto err_add_timestamp;
103 debug ("Added timestamp successfully\n");
105 munmap ((void *)ptr, sbuf.st_size);
108 munmap(dest_blob, destfd_size);
112 if (rename (tmpfile, params->imagefile) == -1) {
113 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
114 params->cmdname, tmpfile, params->imagefile,
117 unlink (params->imagefile);
118 return (EXIT_FAILURE);
120 return (EXIT_SUCCESS);
124 munmap(ptr, sbuf.st_size);
127 munmap(dest_blob, destfd_size);
134 static int fit_check_params(struct image_tool_params *params)
136 return ((params->dflag && (params->fflag || params->lflag)) ||
137 (params->fflag && (params->dflag || params->lflag)) ||
138 (params->lflag && (params->dflag || params->fflag)));
141 static struct image_type_params fitimage_params = {
142 .name = "FIT Image support",
143 .header_size = sizeof(image_header_t),
144 .hdr = (void*)&header,
145 .verify_header = fit_verify_header,
146 .print_header = fit_print_contents,
147 .check_image_type = fit_check_image_types,
148 .fflag_handle = fit_handle_file,
149 .set_header = NULL, /* FIT images use DTB header */
150 .check_params = fit_check_params,
153 void init_fit_image_type (void)
155 register_image_type(&fitimage_params);