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)
51 * fit_handle_file - main FIT file processing function
53 * fit_handle_file() runs dtc to convert .its to .itb, includes
54 * binary data, updates timestamp property and calculates hashes.
56 * datafile - .its file
57 * imagefile - .itb file
60 * only on success, otherwise calls exit (EXIT_FAILURE);
62 static int fit_handle_file (struct mkimage_params *params)
64 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
65 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
70 /* Flattened Image Tree (FIT) format handling */
71 debug ("FIT format handling\n");
73 /* call dtc to include binary properties into the tmp file */
74 if (strlen (params->imagefile) +
75 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
76 fprintf (stderr, "%s: Image file name (%s) too long, "
77 "can't create tmpfile",
78 params->imagefile, params->cmdname);
79 return (EXIT_FAILURE);
81 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
83 /* dtc -I dts -O -p 200 datafile > tmpfile */
84 sprintf (cmd, "%s %s %s > %s",
85 MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
86 debug ("Trying to execute \"%s\"\n", cmd);
87 if (system (cmd) == -1) {
88 fprintf (stderr, "%s: system(%s) failed: %s\n",
89 params->cmdname, cmd, strerror(errno));
91 return (EXIT_FAILURE);
94 /* load FIT blob into memory */
95 tfd = open (tmpfile, O_RDWR|O_BINARY);
98 fprintf (stderr, "%s: Can't open %s: %s\n",
99 params->cmdname, tmpfile, strerror(errno));
101 return (EXIT_FAILURE);
104 if (fstat (tfd, &sbuf) < 0) {
105 fprintf (stderr, "%s: Can't stat %s: %s\n",
106 params->cmdname, tmpfile, strerror(errno));
109 return (EXIT_FAILURE);
112 ptr = mmap (0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
114 if (ptr == MAP_FAILED) {
115 fprintf (stderr, "%s: Can't read %s: %s\n",
116 params->cmdname, tmpfile, strerror(errno));
119 return (EXIT_FAILURE);
122 /* check if ptr has a valid blob */
123 if (fdt_check_header (ptr)) {
124 fprintf (stderr, "%s: Invalid FIT blob\n", params->cmdname);
127 return (EXIT_FAILURE);
130 /* set hashes for images in the blob */
131 if (fit_set_hashes (ptr)) {
132 fprintf (stderr, "%s Can't add hashes to FIT blob",
136 return (EXIT_FAILURE);
139 /* add a timestamp at offset 0 i.e., root */
140 if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) {
141 fprintf (stderr, "%s: Can't add image timestamp\n",
145 return (EXIT_FAILURE);
147 debug ("Added timestamp successfully\n");
149 munmap ((void *)ptr, sbuf.st_size);
152 if (rename (tmpfile, params->imagefile) == -1) {
153 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
154 params->cmdname, tmpfile, params->imagefile,
157 unlink (params->imagefile);
158 return (EXIT_FAILURE);
160 return (EXIT_SUCCESS);
163 static int fit_check_params (struct mkimage_params *params)
165 return ((params->dflag && (params->fflag || params->lflag)) ||
166 (params->fflag && (params->dflag || params->lflag)) ||
167 (params->lflag && (params->dflag || params->fflag)));
170 static struct image_type_params fitimage_params = {
171 .name = "FIT Image support",
172 .header_size = sizeof(image_header_t),
173 .hdr = (void*)&header,
174 .verify_header = fit_verify_header,
175 .print_header = fit_print_contents,
176 .check_image_type = fit_check_image_types,
177 .fflag_handle = fit_handle_file,
178 .set_header = NULL, /* FIT images use DTB header */
179 .check_params = fit_check_params,
182 void init_fit_image_type (void)
184 mkimage_register (&fitimage_params);