blob: 89d3c97ff73516c3f3f98c9512d08214d367fb1c [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
Bartlomiej Sieka9d254382008-03-11 12:34:47 +01002 * (C) Copyright 2008 Semihalf
3 *
Prafulla Wadaskar89a4d6b2009-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
wdenk0c852a22004-02-26 23:01:04 +00007 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk5b1d7132002-11-03 00:07:02 +00009 */
10
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010011#include "mkimage.h"
wdenk5b1d7132002-11-03 00:07:02 +000012#include <image.h>
Wolfgang Denk976b38c2011-02-12 10:37:11 +010013#include <version.h>
wdenk5b1d7132002-11-03 00:07:02 +000014
Prafulla Wadaskar89a4d6b2009-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 Wadaskar89a4d6b2009-08-19 17:36:46 +053018/* parameters initialized by core will be used by the image type code */
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -070019struct image_tool_params params = {
Prafulla Wadaskar89a4d6b2009-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 Denk04387d22010-03-27 23:37:46 +010025 .imagename = "",
Shaohui Xie5d898a02012-08-10 02:49:35 +000026 .imagename2 = "",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053027};
wdenk5b1d7132002-11-03 00:07:02 +000028
Simon Glass5b9d44d2015-06-23 15:38:25 -060029static int h_compare_image_name(const void *vtype1, const void *vtype2)
30{
31 const int *type1 = vtype1;
32 const int *type2 = vtype2;
33 const char *name1 = genimg_get_type_short_name(*type1);
34 const char *name2 = genimg_get_type_short_name(*type2);
35
36 return strcmp(name1, name2);
37}
38
39/* Show all image types supported by mkimage */
40static void show_image_types(void)
41{
42 struct image_type_params *tparams;
43 int order[IH_TYPE_COUNT];
44 int count;
45 int type;
46 int i;
47
48 /* Sort the names in order of short name for easier reading */
49 memset(order, '\0', sizeof(order));
50 for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) {
51 tparams = imagetool_get_type(type);
52 if (tparams)
53 order[count++] = type;
54 }
55 qsort(order, count, sizeof(int), h_compare_image_name);
56
57 fprintf(stderr, "\nInvalid image type. Supported image types:\n");
58 for (i = 0; i < count; i++) {
59 type = order[i];
60 tparams = imagetool_get_type(type);
61 if (tparams) {
62 fprintf(stderr, "\t%-15s %s\n",
63 genimg_get_type_short_name(type),
64 genimg_get_type_name(type));
65 }
66 }
67 fprintf(stderr, "\n");
68}
69
Simon Glass0b443de2016-02-22 22:55:33 -070070static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +000071{
Peter Tysera2513e22010-04-04 22:36:03 -050072 char *ptr;
Simon Glassa02221f2016-02-22 22:55:34 -070073 int opt;
wdenk5b1d7132002-11-03 00:07:02 +000074
Simon Glassa02221f2016-02-22 22:55:34 -070075 while ((opt = getopt(argc, argv,
76 "a:A:cC:d:D:e:f:Fk:K:ln:O:rR:sT:vVx")) != -1) {
77 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -070078 case 'a':
79 params.addr = strtoull(optarg, &ptr, 16);
80 if (*ptr) {
81 fprintf(stderr, "%s: invalid load address %s\n",
82 params.cmdname, optarg);
83 exit(EXIT_FAILURE);
84 }
Simon Glassa02221f2016-02-22 22:55:34 -070085 break;
86 case 'A':
87 params.arch = genimg_get_arch_id(optarg);
88 if (params.arch < 0)
89 usage();
90 break;
91 case 'c':
92 params.comment = optarg;
93 break;
94 case 'C':
95 params.comp = genimg_get_comp_id(optarg);
96 if (params.comp < 0)
97 usage();
98 break;
Simon Glassa02221f2016-02-22 22:55:34 -070099 case 'd':
100 params.datafile = optarg;
101 params.dflag = 1;
102 break;
Simon Glass07450082016-02-22 22:55:35 -0700103 case 'D':
104 params.dtc = optarg;
105 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700106 case 'e':
107 params.ep = strtoull(optarg, &ptr, 16);
108 if (*ptr) {
109 fprintf(stderr, "%s: invalid entry point %s\n",
110 params.cmdname, optarg);
111 exit(EXIT_FAILURE);
112 }
113 params.eflag = 1;
114 break;
115 case 'f':
116 params.datafile = optarg;
117 /* no break */
118 case 'F':
119 /*
120 * The flattened image tree (FIT) format
121 * requires a flattened device tree image type
122 */
123 params.type = IH_TYPE_FLATDT;
124 params.fflag = 1;
125 break;
126 case 'k':
127 params.keydir = optarg;
128 break;
129 case 'K':
130 params.keydest = optarg;
131 break;
Simon Glass07450082016-02-22 22:55:35 -0700132 case 'l':
133 params.lflag = 1;
134 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700135 case 'n':
136 params.imagename = optarg;
137 break;
Simon Glass07450082016-02-22 22:55:35 -0700138 case 'O':
139 params.os = genimg_get_os_id(optarg);
140 if (params.os < 0)
141 usage();
142 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700143 case 'r':
144 params.require_keys = 1;
145 break;
146 case 'R':
147 /*
148 * This entry is for the second configuration
149 * file, if only one is not enough.
150 */
151 params.imagename2 = optarg;
152 break;
153 case 's':
154 params.skipcpy = 1;
155 break;
Simon Glass07450082016-02-22 22:55:35 -0700156 case 'T':
157 params.type = genimg_get_type_id(optarg);
158 if (params.type < 0) {
159 show_image_types();
160 usage();
161 }
162 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700163 case 'v':
164 params.vflag++;
165 break;
166 case 'V':
167 printf("mkimage version %s\n", PLAIN_VERSION);
168 exit(EXIT_SUCCESS);
169 case 'x':
170 params.xflag++;
171 break;
172 default:
173 usage();
wdenk5b1d7132002-11-03 00:07:02 +0000174 }
wdenk5b1d7132002-11-03 00:07:02 +0000175 }
176
Simon Glassa02221f2016-02-22 22:55:34 -0700177 if (optind >= argc)
Simon Glass0b443de2016-02-22 22:55:33 -0700178 usage();
Simon Glassa02221f2016-02-22 22:55:34 -0700179 params.imagefile = argv[optind];
Simon Glass0b443de2016-02-22 22:55:33 -0700180}
181
182
183int main(int argc, char **argv)
184{
185 int ifd = -1;
186 struct stat sbuf;
187 char *ptr;
188 int retval = 0;
189 struct image_type_params *tparams = NULL;
190 int pad_len = 0;
191 int dfd;
192
193 params.cmdname = *argv;
194 params.addr = 0;
195 params.ep = 0;
196
197 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000198
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530199 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200200 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530201 if (tparams == NULL) {
202 fprintf (stderr, "%s: unsupported type %s\n",
203 params.cmdname, genimg_get_type_name(params.type));
204 exit (EXIT_FAILURE);
205 }
206
207 /*
208 * check the passed arguments parameters meets the requirements
209 * as per image type to be generated/listed
210 */
211 if (tparams->check_params)
212 if (tparams->check_params (&params))
Simon Glass0b443de2016-02-22 22:55:33 -0700213 usage();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530214
215 if (!params.eflag) {
216 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000217 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530218 if (params.xflag)
219 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000220 }
221
Peter Tyserc81296c2009-11-24 16:42:10 -0600222 if (params.fflag){
223 if (tparams->fflag_handle)
224 /*
225 * in some cases, some additional processing needs
226 * to be done if fflag is defined
227 *
228 * For ex. fit_handle_file for Fit file support
229 */
230 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000231
Peter Tyserc81296c2009-11-24 16:42:10 -0600232 if (retval != EXIT_SUCCESS)
233 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000234 }
235
Peter Tyserc81296c2009-11-24 16:42:10 -0600236 if (params.lflag || params.fflag) {
237 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
238 } else {
239 ifd = open (params.imagefile,
240 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
241 }
242
243 if (ifd < 0) {
244 fprintf (stderr, "%s: Can't open %s: %s\n",
245 params.cmdname, params.imagefile,
246 strerror(errno));
247 exit (EXIT_FAILURE);
248 }
249
250 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000251 /*
252 * list header information of existing image
253 */
254 if (fstat(ifd, &sbuf) < 0) {
255 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530256 params.cmdname, params.imagefile,
257 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000258 exit (EXIT_FAILURE);
259 }
260
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530261 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000262 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530263 "%s: Bad size: \"%s\" is not valid image\n",
264 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000265 exit (EXIT_FAILURE);
266 }
267
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400268 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
269 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000270 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530271 params.cmdname, params.imagefile,
272 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000273 exit (EXIT_FAILURE);
274 }
275
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530276 /*
277 * scan through mkimage registry for all supported image types
278 * and verify the input image file header for match
279 * Print the image information for matched image type
280 * Returns the error code if not matched
281 */
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -0200282 retval = imagetool_verify_print_header(ptr, &sbuf,
283 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000284
wdenk5b1d7132002-11-03 00:07:02 +0000285 (void) munmap((void *)ptr, sbuf.st_size);
286 (void) close (ifd);
287
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530288 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000289 }
290
Marek Vasut34633142015-12-07 18:01:54 +0100291 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200292 dfd = open(params.datafile, O_RDONLY | O_BINARY);
293 if (dfd < 0) {
294 fprintf(stderr, "%s: Can't open %s: %s\n",
295 params.cmdname, params.datafile,
296 strerror(errno));
297 exit(EXIT_FAILURE);
298 }
Simon Glass92a655c2015-06-23 15:39:12 -0600299
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200300 if (fstat(dfd, &sbuf) < 0) {
301 fprintf(stderr, "%s: Can't stat %s: %s\n",
302 params.cmdname, params.datafile,
303 strerror(errno));
304 exit(EXIT_FAILURE);
305 }
Simon Glass92a655c2015-06-23 15:39:12 -0600306
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200307 params.file_size = sbuf.st_size + tparams->header_size;
308 close(dfd);
309 }
Simon Glass92a655c2015-06-23 15:39:12 -0600310
wdenk5b1d7132002-11-03 00:07:02 +0000311 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000312 * In case there an header with a variable
313 * length will be added, the corresponding
314 * function is called. This is responsible to
315 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000316 */
Stefano Babicf0662102011-09-15 23:50:16 +0000317 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200318 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000319 else
320 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000321
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530322 if (write(ifd, tparams->hdr, tparams->header_size)
323 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000324 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530325 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000326 exit (EXIT_FAILURE);
327 }
328
Christian Rieschd1be8f92011-12-09 09:47:38 +0000329 if (!params.skipcpy) {
330 if (params.type == IH_TYPE_MULTI ||
331 params.type == IH_TYPE_SCRIPT) {
332 char *file = params.datafile;
333 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000334
Christian Rieschd1be8f92011-12-09 09:47:38 +0000335 for (;;) {
336 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000337
Christian Rieschd1be8f92011-12-09 09:47:38 +0000338 if (file) {
339 if ((sep = strchr(file, ':')) != NULL) {
340 *sep = '\0';
341 }
342
343 if (stat (file, &sbuf) < 0) {
344 fprintf (stderr, "%s: Can't stat %s: %s\n",
345 params.cmdname, file, strerror(errno));
346 exit (EXIT_FAILURE);
347 }
348 size = cpu_to_uimage (sbuf.st_size);
349 } else {
350 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000351 }
352
Christian Rieschd1be8f92011-12-09 09:47:38 +0000353 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
354 fprintf (stderr, "%s: Write error on %s: %s\n",
355 params.cmdname, params.imagefile,
356 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000357 exit (EXIT_FAILURE);
358 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000359
360 if (!file) {
361 break;
362 }
363
364 if (sep) {
365 *sep = ':';
366 file = sep + 1;
367 } else {
368 file = NULL;
369 }
wdenk5b1d7132002-11-03 00:07:02 +0000370 }
371
Christian Rieschd1be8f92011-12-09 09:47:38 +0000372 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000373
Christian Rieschd1be8f92011-12-09 09:47:38 +0000374 for (;;) {
375 char *sep = strchr(file, ':');
376 if (sep) {
377 *sep = '\0';
378 copy_file (ifd, file, 1);
379 *sep++ = ':';
380 file = sep;
381 } else {
382 copy_file (ifd, file, 0);
383 break;
384 }
wdenk5b1d7132002-11-03 00:07:02 +0000385 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000386 } else if (params.type == IH_TYPE_PBLIMAGE) {
387 /* PBL has special Image format, implements its' own */
388 pbl_load_uboot(ifd, &params);
Christian Rieschd1be8f92011-12-09 09:47:38 +0000389 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200390 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000391 }
wdenk5b1d7132002-11-03 00:07:02 +0000392 }
393
394 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530395#if defined(_POSIX_SYNCHRONIZED_IO) && \
396 !defined(__sun__) && \
397 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200398 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530399 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000400 (void) fdatasync (ifd);
401#else
402 (void) fsync (ifd);
403#endif
404
405 if (fstat(ifd, &sbuf) < 0) {
406 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530407 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000408 exit (EXIT_FAILURE);
409 }
Simon Glass92a655c2015-06-23 15:39:12 -0600410 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000411
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400412 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
413 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000414 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530415 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000416 exit (EXIT_FAILURE);
417 }
418
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530419 /* Setup the image header as per input image type*/
420 if (tparams->set_header)
421 tparams->set_header (ptr, &sbuf, ifd, &params);
422 else {
423 fprintf (stderr, "%s: Can't set header for %s: %s\n",
424 params.cmdname, tparams->name, strerror(errno));
425 exit (EXIT_FAILURE);
426 }
wdenk5b1d7132002-11-03 00:07:02 +0000427
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530428 /* Print the image information by processing image header */
429 if (tparams->print_header)
430 tparams->print_header (ptr);
431 else {
432 fprintf (stderr, "%s: Can't print header for %s: %s\n",
433 params.cmdname, tparams->name, strerror(errno));
434 exit (EXIT_FAILURE);
435 }
wdenk5b1d7132002-11-03 00:07:02 +0000436
437 (void) munmap((void *)ptr, sbuf.st_size);
438
439 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530440#if defined(_POSIX_SYNCHRONIZED_IO) && \
441 !defined(__sun__) && \
442 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200443 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530444 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000445 (void) fdatasync (ifd);
446#else
447 (void) fsync (ifd);
448#endif
449
450 if (close(ifd)) {
451 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530452 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000453 exit (EXIT_FAILURE);
454 }
455
456 exit (EXIT_SUCCESS);
457}
458
459static void
460copy_file (int ifd, const char *datafile, int pad)
461{
462 int dfd;
463 struct stat sbuf;
464 unsigned char *ptr;
465 int tail;
466 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200467 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000468 int offset = 0;
469 int size;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200470 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000471
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200472 memset(zeros, 0, sizeof(zeros));
473
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530474 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000475 fprintf (stderr, "Adding Image %s\n", datafile);
476 }
477
wdenkef1464c2003-10-08 22:14:02 +0000478 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000479 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530480 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000481 exit (EXIT_FAILURE);
482 }
483
484 if (fstat(dfd, &sbuf) < 0) {
485 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530486 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000487 exit (EXIT_FAILURE);
488 }
489
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400490 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
491 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000492 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530493 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000494 exit (EXIT_FAILURE);
495 }
496
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530497 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000498 unsigned char *p = NULL;
499 /*
500 * XIP: do not append the image_header_t at the
501 * beginning of the file, but consume the space
502 * reserved for it.
503 */
504
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530505 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000506 fprintf (stderr,
507 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530508 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000509 exit (EXIT_FAILURE);
510 }
511
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530512 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000513 if ( *p != 0xff ) {
514 fprintf (stderr,
515 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530516 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000517 exit (EXIT_FAILURE);
518 }
519 }
520
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530521 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000522 }
523
524 size = sbuf.st_size - offset;
525 if (write(ifd, ptr + offset, size) != size) {
526 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530527 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000528 exit (EXIT_FAILURE);
529 }
530
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200531 tail = size % 4;
532 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000533
534 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
535 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530536 params.cmdname, params.imagefile,
537 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000538 exit (EXIT_FAILURE);
539 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200540 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600541 while (pad > 0) {
542 int todo = sizeof(zeros);
543
544 if (todo > pad)
545 todo = pad;
546 if (write(ifd, (char *)&zeros, todo) != todo) {
547 fprintf(stderr, "%s: Write error on %s: %s\n",
548 params.cmdname, params.imagefile,
549 strerror(errno));
550 exit(EXIT_FAILURE);
551 }
552 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200553 }
wdenk5b1d7132002-11-03 00:07:02 +0000554 }
555
556 (void) munmap((void *)ptr, sbuf.st_size);
557 (void) close (dfd);
558}
559
Guilherme Maciel Ferreiraf1cc4582013-12-01 12:43:09 -0700560static void usage(void)
wdenk5b1d7132002-11-03 00:07:02 +0000561{
562 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100563 " -l ==> list image header information\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530564 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100565 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
566 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
567 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000568 " -O ==> set operating system to 'os'\n"
569 " -T ==> set image type to 'type'\n"
570 " -C ==> set compression type 'comp'\n"
571 " -a ==> set load address to 'addr' (hex)\n"
572 " -e ==> set entry point to 'ep' (hex)\n"
573 " -n ==> set image name to 'name'\n"
574 " -d ==> use image data from 'datafile'\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100575 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530576 params.cmdname);
Simon Glass95d77b42013-06-13 15:10:05 -0700577 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530578 params.cmdname);
Tom Rini35497302015-08-26 15:21:23 -0400579 fprintf(stderr, " -D => set all options for device tree compiler\n"
Simon Glass80e4df82013-06-13 15:10:03 -0700580 " -f => input filename for FIT source\n");
581#ifdef CONFIG_FIT_SIGNATURE
Simon Glass399c7442013-06-13 15:10:07 -0700582 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
Simon Glasse29495d2013-06-13 15:10:04 -0700583 " -k => set directory containing private keys\n"
Simon Glass95d77b42013-06-13 15:10:05 -0700584 " -K => write public keys to this .dtb file\n"
Simon Glass4f610422013-06-13 15:10:06 -0700585 " -c => add comment in signature node\n"
Simon Glass399c7442013-06-13 15:10:07 -0700586 " -F => re-sign existing FIT image\n"
587 " -r => mark keys used as 'required' in dtb\n");
Simon Glass80e4df82013-06-13 15:10:03 -0700588#else
589 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
590#endif
Wolfgang Denk976b38c2011-02-12 10:37:11 +0100591 fprintf (stderr, " %s -V ==> print version information and exit\n",
592 params.cmdname);
Simon Glass5b9d44d2015-06-23 15:38:25 -0600593 fprintf(stderr, "Use -T to see a list of available image types\n");
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100594
wdenk5b1d7132002-11-03 00:07:02 +0000595 exit (EXIT_FAILURE);
596}