blob: e81d4550835c6763bcab21e0b2b8ab2bf46baba3 [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
Simon Glass434a31b2015-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
70int main(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +000071{
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +010072 int ifd = -1;
wdenk5b1d7132002-11-03 00:07:02 +000073 struct stat sbuf;
Peter Tyser632d9ca2010-04-04 22:36:03 -050074 char *ptr;
Prafulla Wadaskar4d4d67d2009-08-16 05:28:19 +053075 int retval = 0;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053076 struct image_type_params *tparams = NULL;
Stefano Babic6531bd92013-08-19 19:03:19 +020077 int pad_len = 0;
Simon Glass139651f2015-06-23 15:39:12 -060078 int dfd;
wdenk5b1d7132002-11-03 00:07:02 +000079
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053080 params.cmdname = *argv;
81 params.addr = params.ep = 0;
wdenk5b1d7132002-11-03 00:07:02 +000082
83 while (--argc > 0 && **++argv == '-') {
84 while (*++*argv) {
85 switch (**argv) {
86 case 'l':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053087 params.lflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +000088 break;
89 case 'A':
90 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053091 (params.arch =
92 genimg_get_arch_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000093 usage ();
94 goto NXTARG;
Simon Glassbf27d3d2013-06-13 15:10:06 -070095 case 'c':
96 if (--argc <= 0)
97 usage();
98 params.comment = *++argv;
99 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000100 case 'C':
101 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530102 (params.comp =
103 genimg_get_comp_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000104 usage ();
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100105 goto NXTARG;
106 case 'D':
107 if (--argc <= 0)
108 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530109 params.dtc = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000110 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100111
wdenk5b1d7132002-11-03 00:07:02 +0000112 case 'O':
113 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530114 (params.os =
115 genimg_get_os_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000116 usage ();
117 goto NXTARG;
118 case 'T':
Simon Glass434a31b2015-06-23 15:38:25 -0600119 params.type = -1;
120 if (--argc >= 0 && argv[1]) {
121 params.type =
122 genimg_get_type_id(*++argv);
123 }
124 if (params.type < 0) {
125 show_image_types();
126 usage();
127 }
wdenk5b1d7132002-11-03 00:07:02 +0000128 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000129 case 'a':
130 if (--argc <= 0)
131 usage ();
Peter Tyser632d9ca2010-04-04 22:36:03 -0500132 params.addr = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000133 if (*ptr) {
134 fprintf (stderr,
135 "%s: invalid load address %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530136 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000137 exit (EXIT_FAILURE);
138 }
139 goto NXTARG;
140 case 'd':
141 if (--argc <= 0)
142 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530143 params.datafile = *++argv;
144 params.dflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000145 goto NXTARG;
146 case 'e':
147 if (--argc <= 0)
148 usage ();
Peter Tyser632d9ca2010-04-04 22:36:03 -0500149 params.ep = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000150 if (*ptr) {
151 fprintf (stderr,
152 "%s: invalid entry point %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530153 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000154 exit (EXIT_FAILURE);
155 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530156 params.eflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000157 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100158 case 'f':
159 if (--argc <= 0)
160 usage ();
Simon Glassce8c3ca2013-06-13 15:10:05 -0700161 params.datafile = *++argv;
162 /* no break */
163 case 'F':
Peter Tyser94734e62009-11-24 16:42:08 -0600164 /*
165 * The flattened image tree (FIT) format
166 * requires a flattened device tree image type
167 */
168 params.type = IH_TYPE_FLATDT;
Peter Tyser54cd5392009-12-06 01:33:24 -0600169 params.fflag = 1;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100170 goto NXTARG;
Simon Glassb1489742013-06-13 15:10:03 -0700171 case 'k':
172 if (--argc <= 0)
173 usage();
174 params.keydir = *++argv;
175 goto NXTARG;
Simon Glassb4d8b092013-06-13 15:10:04 -0700176 case 'K':
177 if (--argc <= 0)
178 usage();
179 params.keydest = *++argv;
180 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000181 case 'n':
182 if (--argc <= 0)
183 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530184 params.imagename = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000185 goto NXTARG;
Simon Glass817278a2013-06-13 15:10:07 -0700186 case 'r':
187 params.require_keys = 1;
188 break;
Shaohui Xieea65fd82012-08-10 02:49:35 +0000189 case 'R':
190 if (--argc <= 0)
191 usage();
192 /*
193 * This entry is for the second configuration
194 * file, if only one is not enough.
195 */
196 params.imagename2 = *++argv;
197 goto NXTARG;
Stefano Babic4a963302011-09-15 23:50:16 +0000198 case 's':
199 params.skipcpy = 1;
200 break;
wdenk5b1d7132002-11-03 00:07:02 +0000201 case 'v':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530202 params.vflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000203 break;
Wolfgang Denke97ab722011-02-12 10:37:11 +0100204 case 'V':
205 printf("mkimage version %s\n", PLAIN_VERSION);
206 exit(EXIT_SUCCESS);
wdenk5b1d7132002-11-03 00:07:02 +0000207 case 'x':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530208 params.xflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000209 break;
210 default:
211 usage ();
212 }
213 }
214NXTARG: ;
215 }
216
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530217 if (argc != 1)
218 usage ();
219
220 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200221 tparams = imagetool_get_type(params.type);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530222 if (tparams == NULL) {
223 fprintf (stderr, "%s: unsupported type %s\n",
224 params.cmdname, genimg_get_type_name(params.type));
225 exit (EXIT_FAILURE);
226 }
227
228 /*
229 * check the passed arguments parameters meets the requirements
230 * as per image type to be generated/listed
231 */
232 if (tparams->check_params)
233 if (tparams->check_params (&params))
234 usage ();
wdenk5b1d7132002-11-03 00:07:02 +0000235
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530236 if (!params.eflag) {
237 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000238 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530239 if (params.xflag)
240 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000241 }
242
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530243 params.imagefile = *argv;
wdenk5b1d7132002-11-03 00:07:02 +0000244
Peter Tyserb2f8b942009-11-24 16:42:10 -0600245 if (params.fflag){
246 if (tparams->fflag_handle)
247 /*
248 * in some cases, some additional processing needs
249 * to be done if fflag is defined
250 *
251 * For ex. fit_handle_file for Fit file support
252 */
253 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000254
Peter Tyserb2f8b942009-11-24 16:42:10 -0600255 if (retval != EXIT_SUCCESS)
256 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000257 }
258
Peter Tyserb2f8b942009-11-24 16:42:10 -0600259 if (params.lflag || params.fflag) {
260 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
261 } else {
262 ifd = open (params.imagefile,
263 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
264 }
265
266 if (ifd < 0) {
267 fprintf (stderr, "%s: Can't open %s: %s\n",
268 params.cmdname, params.imagefile,
269 strerror(errno));
270 exit (EXIT_FAILURE);
271 }
272
273 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000274 /*
275 * list header information of existing image
276 */
277 if (fstat(ifd, &sbuf) < 0) {
278 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530279 params.cmdname, params.imagefile,
280 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000281 exit (EXIT_FAILURE);
282 }
283
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530284 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000285 fprintf (stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530286 "%s: Bad size: \"%s\" is not valid image\n",
287 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000288 exit (EXIT_FAILURE);
289 }
290
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400291 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
292 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000293 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530294 params.cmdname, params.imagefile,
295 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000296 exit (EXIT_FAILURE);
297 }
298
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530299 /*
300 * scan through mkimage registry for all supported image types
301 * and verify the input image file header for match
302 * Print the image information for matched image type
303 * Returns the error code if not matched
304 */
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -0200305 retval = imagetool_verify_print_header(ptr, &sbuf,
306 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000307
wdenk5b1d7132002-11-03 00:07:02 +0000308 (void) munmap((void *)ptr, sbuf.st_size);
309 (void) close (ifd);
310
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530311 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000312 }
313
Simon Glass139651f2015-06-23 15:39:12 -0600314 dfd = open(params.datafile, O_RDONLY | O_BINARY);
315 if (dfd < 0) {
316 fprintf(stderr, "%s: Can't open %s: %s\n",
317 params.cmdname, params.datafile, strerror(errno));
318 exit(EXIT_FAILURE);
319 }
320
321 if (fstat(dfd, &sbuf) < 0) {
322 fprintf(stderr, "%s: Can't stat %s: %s\n",
323 params.cmdname, params.datafile, strerror(errno));
324 exit(EXIT_FAILURE);
325 }
326
327 params.file_size = sbuf.st_size + tparams->header_size;
328 close(dfd);
329
wdenk5b1d7132002-11-03 00:07:02 +0000330 /*
Stefano Babic4a963302011-09-15 23:50:16 +0000331 * In case there an header with a variable
332 * length will be added, the corresponding
333 * function is called. This is responsible to
334 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000335 */
Stefano Babic4a963302011-09-15 23:50:16 +0000336 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200337 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000338 else
339 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000340
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530341 if (write(ifd, tparams->hdr, tparams->header_size)
342 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000343 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530344 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000345 exit (EXIT_FAILURE);
346 }
347
Christian Rieschc14f2812011-12-09 09:47:38 +0000348 if (!params.skipcpy) {
349 if (params.type == IH_TYPE_MULTI ||
350 params.type == IH_TYPE_SCRIPT) {
351 char *file = params.datafile;
352 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000353
Christian Rieschc14f2812011-12-09 09:47:38 +0000354 for (;;) {
355 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000356
Christian Rieschc14f2812011-12-09 09:47:38 +0000357 if (file) {
358 if ((sep = strchr(file, ':')) != NULL) {
359 *sep = '\0';
360 }
361
362 if (stat (file, &sbuf) < 0) {
363 fprintf (stderr, "%s: Can't stat %s: %s\n",
364 params.cmdname, file, strerror(errno));
365 exit (EXIT_FAILURE);
366 }
367 size = cpu_to_uimage (sbuf.st_size);
368 } else {
369 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000370 }
371
Christian Rieschc14f2812011-12-09 09:47:38 +0000372 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
373 fprintf (stderr, "%s: Write error on %s: %s\n",
374 params.cmdname, params.imagefile,
375 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000376 exit (EXIT_FAILURE);
377 }
wdenk5b1d7132002-11-03 00:07:02 +0000378
Christian Rieschc14f2812011-12-09 09:47:38 +0000379 if (!file) {
380 break;
381 }
wdenk5b1d7132002-11-03 00:07:02 +0000382
Christian Rieschc14f2812011-12-09 09:47:38 +0000383 if (sep) {
384 *sep = ':';
385 file = sep + 1;
386 } else {
387 file = NULL;
388 }
wdenk5b1d7132002-11-03 00:07:02 +0000389 }
wdenk5b1d7132002-11-03 00:07:02 +0000390
Christian Rieschc14f2812011-12-09 09:47:38 +0000391 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000392
Christian Rieschc14f2812011-12-09 09:47:38 +0000393 for (;;) {
394 char *sep = strchr(file, ':');
395 if (sep) {
396 *sep = '\0';
397 copy_file (ifd, file, 1);
398 *sep++ = ':';
399 file = sep;
400 } else {
401 copy_file (ifd, file, 0);
402 break;
403 }
wdenk5b1d7132002-11-03 00:07:02 +0000404 }
Shaohui Xieea65fd82012-08-10 02:49:35 +0000405 } else if (params.type == IH_TYPE_PBLIMAGE) {
406 /* PBL has special Image format, implements its' own */
407 pbl_load_uboot(ifd, &params);
Christian Rieschc14f2812011-12-09 09:47:38 +0000408 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200409 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000410 }
wdenk5b1d7132002-11-03 00:07:02 +0000411 }
412
413 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530414#if defined(_POSIX_SYNCHRONIZED_IO) && \
415 !defined(__sun__) && \
416 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200417 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530418 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000419 (void) fdatasync (ifd);
420#else
421 (void) fsync (ifd);
422#endif
423
424 if (fstat(ifd, &sbuf) < 0) {
425 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530426 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000427 exit (EXIT_FAILURE);
428 }
Simon Glass139651f2015-06-23 15:39:12 -0600429 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000430
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400431 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
432 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000433 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530434 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000435 exit (EXIT_FAILURE);
436 }
437
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530438 /* Setup the image header as per input image type*/
439 if (tparams->set_header)
440 tparams->set_header (ptr, &sbuf, ifd, &params);
441 else {
442 fprintf (stderr, "%s: Can't set header for %s: %s\n",
443 params.cmdname, tparams->name, strerror(errno));
444 exit (EXIT_FAILURE);
445 }
wdenk5b1d7132002-11-03 00:07:02 +0000446
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530447 /* Print the image information by processing image header */
448 if (tparams->print_header)
449 tparams->print_header (ptr);
450 else {
451 fprintf (stderr, "%s: Can't print header for %s: %s\n",
452 params.cmdname, tparams->name, strerror(errno));
453 exit (EXIT_FAILURE);
454 }
wdenk5b1d7132002-11-03 00:07:02 +0000455
456 (void) munmap((void *)ptr, sbuf.st_size);
457
458 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530459#if defined(_POSIX_SYNCHRONIZED_IO) && \
460 !defined(__sun__) && \
461 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200462 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530463 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000464 (void) fdatasync (ifd);
465#else
466 (void) fsync (ifd);
467#endif
468
469 if (close(ifd)) {
470 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530471 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000472 exit (EXIT_FAILURE);
473 }
474
475 exit (EXIT_SUCCESS);
476}
477
478static void
479copy_file (int ifd, const char *datafile, int pad)
480{
481 int dfd;
482 struct stat sbuf;
483 unsigned char *ptr;
484 int tail;
485 int zero = 0;
Stefano Babic6531bd92013-08-19 19:03:19 +0200486 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000487 int offset = 0;
488 int size;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200489 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000490
Stefano Babic6531bd92013-08-19 19:03:19 +0200491 if (pad >= sizeof(zeros)) {
492 fprintf(stderr, "%s: Can't pad to %d\n",
493 params.cmdname, pad);
494 exit(EXIT_FAILURE);
495 }
496
497 memset(zeros, 0, sizeof(zeros));
498
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530499 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000500 fprintf (stderr, "Adding Image %s\n", datafile);
501 }
502
wdenk1ee550e2003-10-08 22:14:02 +0000503 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000504 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530505 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000506 exit (EXIT_FAILURE);
507 }
508
509 if (fstat(dfd, &sbuf) < 0) {
510 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530511 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000512 exit (EXIT_FAILURE);
513 }
514
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400515 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
516 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000517 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530518 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000519 exit (EXIT_FAILURE);
520 }
521
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530522 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000523 unsigned char *p = NULL;
524 /*
525 * XIP: do not append the image_header_t at the
526 * beginning of the file, but consume the space
527 * reserved for it.
528 */
529
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530530 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000531 fprintf (stderr,
532 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530533 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000534 exit (EXIT_FAILURE);
535 }
536
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530537 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000538 if ( *p != 0xff ) {
539 fprintf (stderr,
540 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530541 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000542 exit (EXIT_FAILURE);
543 }
544 }
545
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530546 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000547 }
548
549 size = sbuf.st_size - offset;
550 if (write(ifd, ptr + offset, size) != size) {
551 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530552 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000553 exit (EXIT_FAILURE);
554 }
555
Stefano Babic6531bd92013-08-19 19:03:19 +0200556 tail = size % 4;
557 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000558
559 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
560 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530561 params.cmdname, params.imagefile,
562 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000563 exit (EXIT_FAILURE);
564 }
Stefano Babic6531bd92013-08-19 19:03:19 +0200565 } else if (pad > 1) {
566 if (write(ifd, (char *)&zeros, pad) != pad) {
567 fprintf(stderr, "%s: Write error on %s: %s\n",
568 params.cmdname, params.imagefile,
569 strerror(errno));
570 exit(EXIT_FAILURE);
571 }
wdenk5b1d7132002-11-03 00:07:02 +0000572 }
573
574 (void) munmap((void *)ptr, sbuf.st_size);
575 (void) close (dfd);
576}
577
Guilherme Maciel Ferreiraff0fa612013-12-01 12:43:09 -0700578static void usage(void)
wdenk5b1d7132002-11-03 00:07:02 +0000579{
580 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100581 " -l ==> list image header information\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530582 params.cmdname);
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100583 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
584 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
585 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000586 " -O ==> set operating system to 'os'\n"
587 " -T ==> set image type to 'type'\n"
588 " -C ==> set compression type 'comp'\n"
589 " -a ==> set load address to 'addr' (hex)\n"
590 " -e ==> set entry point to 'ep' (hex)\n"
591 " -n ==> set image name to 'name'\n"
592 " -d ==> use image data from 'datafile'\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100593 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530594 params.cmdname);
Simon Glassce8c3ca2013-06-13 15:10:05 -0700595 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530596 params.cmdname);
Simon Glassb1489742013-06-13 15:10:03 -0700597 fprintf(stderr, " -D => set options for device tree compiler\n"
598 " -f => input filename for FIT source\n");
599#ifdef CONFIG_FIT_SIGNATURE
Simon Glass817278a2013-06-13 15:10:07 -0700600 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
Simon Glassb4d8b092013-06-13 15:10:04 -0700601 " -k => set directory containing private keys\n"
Simon Glassce8c3ca2013-06-13 15:10:05 -0700602 " -K => write public keys to this .dtb file\n"
Simon Glassbf27d3d2013-06-13 15:10:06 -0700603 " -c => add comment in signature node\n"
Simon Glass817278a2013-06-13 15:10:07 -0700604 " -F => re-sign existing FIT image\n"
605 " -r => mark keys used as 'required' in dtb\n");
Simon Glassb1489742013-06-13 15:10:03 -0700606#else
607 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
608#endif
Wolfgang Denke97ab722011-02-12 10:37:11 +0100609 fprintf (stderr, " %s -V ==> print version information and exit\n",
610 params.cmdname);
Simon Glass434a31b2015-06-23 15:38:25 -0600611 fprintf(stderr, "Use -T to see a list of available image types\n");
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100612
wdenk5b1d7132002-11-03 00:07:02 +0000613 exit (EXIT_FAILURE);
614}