blob: 5ccd951048cef587686d264575aae6ea7623e90f [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +01002 * (C) Copyright 2008 Semihalf
3 *
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +05304 * (C) Copyright 2000-2009
wdenk5b1d7132002-11-03 00:07:02 +00005 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
wdenkdb82c8e2004-02-26 23:01:04 +00007 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk5b1d7132002-11-03 00:07:02 +00009 */
10
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010011#include "mkimage.h"
wdenk5b1d7132002-11-03 00:07:02 +000012#include <image.h>
Wolfgang Denke97ab722011-02-12 10:37:11 +010013#include <version.h>
wdenk5b1d7132002-11-03 00:07:02 +000014
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053015static void copy_file(int, const char *, int);
16static void usage(void);
wdenk5b1d7132002-11-03 00:07:02 +000017
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053018/* parameters initialized by core will be used by the image type code */
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070019struct image_tool_params params = {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053020 .os = IH_OS_LINUX,
21 .arch = IH_ARCH_PPC,
22 .type = IH_TYPE_KERNEL,
23 .comp = IH_COMP_GZIP,
24 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
Wolfgang Denk23416b82010-03-27 23:37:46 +010025 .imagename = "",
Shaohui Xieea65fd82012-08-10 02:49:35 +000026 .imagename2 = "",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053027};
wdenk5b1d7132002-11-03 00:07:02 +000028
wdenk5b1d7132002-11-03 00:07:02 +000029int
30main (int argc, char **argv)
31{
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +010032 int ifd = -1;
wdenk5b1d7132002-11-03 00:07:02 +000033 struct stat sbuf;
Peter Tyser632d9ca2010-04-04 22:36:03 -050034 char *ptr;
Prafulla Wadaskar4d4d67d2009-08-16 05:28:19 +053035 int retval = 0;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053036 struct image_type_params *tparams = NULL;
Stefano Babic6531bd92013-08-19 19:03:19 +020037 int pad_len = 0;
wdenk5b1d7132002-11-03 00:07:02 +000038
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053039 params.cmdname = *argv;
40 params.addr = params.ep = 0;
wdenk5b1d7132002-11-03 00:07:02 +000041
42 while (--argc > 0 && **++argv == '-') {
43 while (*++*argv) {
44 switch (**argv) {
45 case 'l':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053046 params.lflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +000047 break;
48 case 'A':
49 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053050 (params.arch =
51 genimg_get_arch_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000052 usage ();
53 goto NXTARG;
Simon Glassbf27d3d2013-06-13 15:10:06 -070054 case 'c':
55 if (--argc <= 0)
56 usage();
57 params.comment = *++argv;
58 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +000059 case 'C':
60 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053061 (params.comp =
62 genimg_get_comp_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000063 usage ();
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +010064 goto NXTARG;
65 case 'D':
66 if (--argc <= 0)
67 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053068 params.dtc = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +000069 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +010070
wdenk5b1d7132002-11-03 00:07:02 +000071 case 'O':
72 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053073 (params.os =
74 genimg_get_os_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000075 usage ();
76 goto NXTARG;
77 case 'T':
78 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053079 (params.type =
80 genimg_get_type_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000081 usage ();
82 goto NXTARG;
83
84 case 'a':
85 if (--argc <= 0)
86 usage ();
Peter Tyser632d9ca2010-04-04 22:36:03 -050087 params.addr = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +000088 if (*ptr) {
89 fprintf (stderr,
90 "%s: invalid load address %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053091 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +000092 exit (EXIT_FAILURE);
93 }
94 goto NXTARG;
95 case 'd':
96 if (--argc <= 0)
97 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053098 params.datafile = *++argv;
99 params.dflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000100 goto NXTARG;
101 case 'e':
102 if (--argc <= 0)
103 usage ();
Peter Tyser632d9ca2010-04-04 22:36:03 -0500104 params.ep = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000105 if (*ptr) {
106 fprintf (stderr,
107 "%s: invalid entry point %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530108 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000109 exit (EXIT_FAILURE);
110 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530111 params.eflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000112 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100113 case 'f':
114 if (--argc <= 0)
115 usage ();
Simon Glassce8c3ca2013-06-13 15:10:05 -0700116 params.datafile = *++argv;
117 /* no break */
118 case 'F':
Peter Tyser94734e62009-11-24 16:42:08 -0600119 /*
120 * The flattened image tree (FIT) format
121 * requires a flattened device tree image type
122 */
123 params.type = IH_TYPE_FLATDT;
Peter Tyser54cd5392009-12-06 01:33:24 -0600124 params.fflag = 1;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100125 goto NXTARG;
Simon Glassb1489742013-06-13 15:10:03 -0700126 case 'k':
127 if (--argc <= 0)
128 usage();
129 params.keydir = *++argv;
130 goto NXTARG;
Simon Glassb4d8b092013-06-13 15:10:04 -0700131 case 'K':
132 if (--argc <= 0)
133 usage();
134 params.keydest = *++argv;
135 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000136 case 'n':
137 if (--argc <= 0)
138 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530139 params.imagename = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000140 goto NXTARG;
Simon Glass817278a2013-06-13 15:10:07 -0700141 case 'r':
142 params.require_keys = 1;
143 break;
Shaohui Xieea65fd82012-08-10 02:49:35 +0000144 case 'R':
145 if (--argc <= 0)
146 usage();
147 /*
148 * This entry is for the second configuration
149 * file, if only one is not enough.
150 */
151 params.imagename2 = *++argv;
152 goto NXTARG;
Stefano Babic4a963302011-09-15 23:50:16 +0000153 case 's':
154 params.skipcpy = 1;
155 break;
wdenk5b1d7132002-11-03 00:07:02 +0000156 case 'v':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530157 params.vflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000158 break;
Wolfgang Denke97ab722011-02-12 10:37:11 +0100159 case 'V':
160 printf("mkimage version %s\n", PLAIN_VERSION);
161 exit(EXIT_SUCCESS);
wdenk5b1d7132002-11-03 00:07:02 +0000162 case 'x':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530163 params.xflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000164 break;
165 default:
166 usage ();
167 }
168 }
169NXTARG: ;
170 }
171
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530172 if (argc != 1)
173 usage ();
174
175 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200176 tparams = imagetool_get_type(params.type);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530177 if (tparams == NULL) {
178 fprintf (stderr, "%s: unsupported type %s\n",
179 params.cmdname, genimg_get_type_name(params.type));
180 exit (EXIT_FAILURE);
181 }
182
183 /*
184 * check the passed arguments parameters meets the requirements
185 * as per image type to be generated/listed
186 */
187 if (tparams->check_params)
188 if (tparams->check_params (&params))
189 usage ();
wdenk5b1d7132002-11-03 00:07:02 +0000190
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530191 if (!params.eflag) {
192 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000193 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530194 if (params.xflag)
195 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000196 }
197
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530198 params.imagefile = *argv;
wdenk5b1d7132002-11-03 00:07:02 +0000199
Peter Tyserb2f8b942009-11-24 16:42:10 -0600200 if (params.fflag){
201 if (tparams->fflag_handle)
202 /*
203 * in some cases, some additional processing needs
204 * to be done if fflag is defined
205 *
206 * For ex. fit_handle_file for Fit file support
207 */
208 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000209
Peter Tyserb2f8b942009-11-24 16:42:10 -0600210 if (retval != EXIT_SUCCESS)
211 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000212 }
213
Peter Tyserb2f8b942009-11-24 16:42:10 -0600214 if (params.lflag || params.fflag) {
215 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
216 } else {
217 ifd = open (params.imagefile,
218 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
219 }
220
221 if (ifd < 0) {
222 fprintf (stderr, "%s: Can't open %s: %s\n",
223 params.cmdname, params.imagefile,
224 strerror(errno));
225 exit (EXIT_FAILURE);
226 }
227
228 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000229 /*
230 * list header information of existing image
231 */
232 if (fstat(ifd, &sbuf) < 0) {
233 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530234 params.cmdname, params.imagefile,
235 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000236 exit (EXIT_FAILURE);
237 }
238
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530239 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000240 fprintf (stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530241 "%s: Bad size: \"%s\" is not valid image\n",
242 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000243 exit (EXIT_FAILURE);
244 }
245
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400246 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
247 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000248 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530249 params.cmdname, params.imagefile,
250 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000251 exit (EXIT_FAILURE);
252 }
253
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530254 /*
255 * scan through mkimage registry for all supported image types
256 * and verify the input image file header for match
257 * Print the image information for matched image type
258 * Returns the error code if not matched
259 */
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -0200260 retval = imagetool_verify_print_header(ptr, &sbuf,
261 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000262
wdenk5b1d7132002-11-03 00:07:02 +0000263 (void) munmap((void *)ptr, sbuf.st_size);
264 (void) close (ifd);
265
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530266 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000267 }
268
269 /*
Stefano Babic4a963302011-09-15 23:50:16 +0000270 * In case there an header with a variable
271 * length will be added, the corresponding
272 * function is called. This is responsible to
273 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000274 */
Stefano Babic4a963302011-09-15 23:50:16 +0000275 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200276 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000277 else
278 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000279
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530280 if (write(ifd, tparams->hdr, tparams->header_size)
281 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000282 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530283 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000284 exit (EXIT_FAILURE);
285 }
286
Christian Rieschc14f2812011-12-09 09:47:38 +0000287 if (!params.skipcpy) {
288 if (params.type == IH_TYPE_MULTI ||
289 params.type == IH_TYPE_SCRIPT) {
290 char *file = params.datafile;
291 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000292
Christian Rieschc14f2812011-12-09 09:47:38 +0000293 for (;;) {
294 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000295
Christian Rieschc14f2812011-12-09 09:47:38 +0000296 if (file) {
297 if ((sep = strchr(file, ':')) != NULL) {
298 *sep = '\0';
299 }
300
301 if (stat (file, &sbuf) < 0) {
302 fprintf (stderr, "%s: Can't stat %s: %s\n",
303 params.cmdname, file, strerror(errno));
304 exit (EXIT_FAILURE);
305 }
306 size = cpu_to_uimage (sbuf.st_size);
307 } else {
308 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000309 }
310
Christian Rieschc14f2812011-12-09 09:47:38 +0000311 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
312 fprintf (stderr, "%s: Write error on %s: %s\n",
313 params.cmdname, params.imagefile,
314 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000315 exit (EXIT_FAILURE);
316 }
wdenk5b1d7132002-11-03 00:07:02 +0000317
Christian Rieschc14f2812011-12-09 09:47:38 +0000318 if (!file) {
319 break;
320 }
wdenk5b1d7132002-11-03 00:07:02 +0000321
Christian Rieschc14f2812011-12-09 09:47:38 +0000322 if (sep) {
323 *sep = ':';
324 file = sep + 1;
325 } else {
326 file = NULL;
327 }
wdenk5b1d7132002-11-03 00:07:02 +0000328 }
wdenk5b1d7132002-11-03 00:07:02 +0000329
Christian Rieschc14f2812011-12-09 09:47:38 +0000330 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000331
Christian Rieschc14f2812011-12-09 09:47:38 +0000332 for (;;) {
333 char *sep = strchr(file, ':');
334 if (sep) {
335 *sep = '\0';
336 copy_file (ifd, file, 1);
337 *sep++ = ':';
338 file = sep;
339 } else {
340 copy_file (ifd, file, 0);
341 break;
342 }
wdenk5b1d7132002-11-03 00:07:02 +0000343 }
Shaohui Xieea65fd82012-08-10 02:49:35 +0000344 } else if (params.type == IH_TYPE_PBLIMAGE) {
345 /* PBL has special Image format, implements its' own */
346 pbl_load_uboot(ifd, &params);
Christian Rieschc14f2812011-12-09 09:47:38 +0000347 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200348 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000349 }
wdenk5b1d7132002-11-03 00:07:02 +0000350 }
351
352 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530353#if defined(_POSIX_SYNCHRONIZED_IO) && \
354 !defined(__sun__) && \
355 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200356 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530357 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000358 (void) fdatasync (ifd);
359#else
360 (void) fsync (ifd);
361#endif
362
363 if (fstat(ifd, &sbuf) < 0) {
364 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530365 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000366 exit (EXIT_FAILURE);
367 }
368
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400369 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
370 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000371 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530372 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000373 exit (EXIT_FAILURE);
374 }
375
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530376 /* Setup the image header as per input image type*/
377 if (tparams->set_header)
378 tparams->set_header (ptr, &sbuf, ifd, &params);
379 else {
380 fprintf (stderr, "%s: Can't set header for %s: %s\n",
381 params.cmdname, tparams->name, strerror(errno));
382 exit (EXIT_FAILURE);
383 }
wdenk5b1d7132002-11-03 00:07:02 +0000384
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530385 /* Print the image information by processing image header */
386 if (tparams->print_header)
387 tparams->print_header (ptr);
388 else {
389 fprintf (stderr, "%s: Can't print header for %s: %s\n",
390 params.cmdname, tparams->name, strerror(errno));
391 exit (EXIT_FAILURE);
392 }
wdenk5b1d7132002-11-03 00:07:02 +0000393
394 (void) munmap((void *)ptr, sbuf.st_size);
395
396 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530397#if defined(_POSIX_SYNCHRONIZED_IO) && \
398 !defined(__sun__) && \
399 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200400 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530401 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000402 (void) fdatasync (ifd);
403#else
404 (void) fsync (ifd);
405#endif
406
407 if (close(ifd)) {
408 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530409 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000410 exit (EXIT_FAILURE);
411 }
412
413 exit (EXIT_SUCCESS);
414}
415
416static void
417copy_file (int ifd, const char *datafile, int pad)
418{
419 int dfd;
420 struct stat sbuf;
421 unsigned char *ptr;
422 int tail;
423 int zero = 0;
Stefano Babic6531bd92013-08-19 19:03:19 +0200424 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000425 int offset = 0;
426 int size;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200427 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000428
Stefano Babic6531bd92013-08-19 19:03:19 +0200429 if (pad >= sizeof(zeros)) {
430 fprintf(stderr, "%s: Can't pad to %d\n",
431 params.cmdname, pad);
432 exit(EXIT_FAILURE);
433 }
434
435 memset(zeros, 0, sizeof(zeros));
436
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530437 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000438 fprintf (stderr, "Adding Image %s\n", datafile);
439 }
440
wdenk1ee550e2003-10-08 22:14:02 +0000441 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000442 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530443 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000444 exit (EXIT_FAILURE);
445 }
446
447 if (fstat(dfd, &sbuf) < 0) {
448 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530449 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000450 exit (EXIT_FAILURE);
451 }
452
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400453 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
454 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000455 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530456 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000457 exit (EXIT_FAILURE);
458 }
459
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530460 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000461 unsigned char *p = NULL;
462 /*
463 * XIP: do not append the image_header_t at the
464 * beginning of the file, but consume the space
465 * reserved for it.
466 */
467
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530468 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000469 fprintf (stderr,
470 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530471 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000472 exit (EXIT_FAILURE);
473 }
474
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530475 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000476 if ( *p != 0xff ) {
477 fprintf (stderr,
478 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530479 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000480 exit (EXIT_FAILURE);
481 }
482 }
483
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530484 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000485 }
486
487 size = sbuf.st_size - offset;
488 if (write(ifd, ptr + offset, size) != size) {
489 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530490 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000491 exit (EXIT_FAILURE);
492 }
493
Stefano Babic6531bd92013-08-19 19:03:19 +0200494 tail = size % 4;
495 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000496
497 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
498 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530499 params.cmdname, params.imagefile,
500 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000501 exit (EXIT_FAILURE);
502 }
Stefano Babic6531bd92013-08-19 19:03:19 +0200503 } else if (pad > 1) {
504 if (write(ifd, (char *)&zeros, pad) != pad) {
505 fprintf(stderr, "%s: Write error on %s: %s\n",
506 params.cmdname, params.imagefile,
507 strerror(errno));
508 exit(EXIT_FAILURE);
509 }
wdenk5b1d7132002-11-03 00:07:02 +0000510 }
511
512 (void) munmap((void *)ptr, sbuf.st_size);
513 (void) close (dfd);
514}
515
Guilherme Maciel Ferreiraff0fa612013-12-01 12:43:09 -0700516static void usage(void)
wdenk5b1d7132002-11-03 00:07:02 +0000517{
518 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100519 " -l ==> list image header information\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530520 params.cmdname);
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100521 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
522 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
523 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000524 " -O ==> set operating system to 'os'\n"
525 " -T ==> set image type to 'type'\n"
526 " -C ==> set compression type 'comp'\n"
527 " -a ==> set load address to 'addr' (hex)\n"
528 " -e ==> set entry point to 'ep' (hex)\n"
529 " -n ==> set image name to 'name'\n"
530 " -d ==> use image data from 'datafile'\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100531 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530532 params.cmdname);
Simon Glassce8c3ca2013-06-13 15:10:05 -0700533 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530534 params.cmdname);
Simon Glassb1489742013-06-13 15:10:03 -0700535 fprintf(stderr, " -D => set options for device tree compiler\n"
536 " -f => input filename for FIT source\n");
537#ifdef CONFIG_FIT_SIGNATURE
Simon Glass817278a2013-06-13 15:10:07 -0700538 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
Simon Glassb4d8b092013-06-13 15:10:04 -0700539 " -k => set directory containing private keys\n"
Simon Glassce8c3ca2013-06-13 15:10:05 -0700540 " -K => write public keys to this .dtb file\n"
Simon Glassbf27d3d2013-06-13 15:10:06 -0700541 " -c => add comment in signature node\n"
Simon Glass817278a2013-06-13 15:10:07 -0700542 " -F => re-sign existing FIT image\n"
543 " -r => mark keys used as 'required' in dtb\n");
Simon Glassb1489742013-06-13 15:10:03 -0700544#else
545 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
546#endif
Wolfgang Denke97ab722011-02-12 10:37:11 +0100547 fprintf (stderr, " %s -V ==> print version information and exit\n",
548 params.cmdname);
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100549
wdenk5b1d7132002-11-03 00:07:02 +0000550 exit (EXIT_FAILURE);
551}