blob: a465fa38524ab4ef40e9b19581f05f5c647d2244 [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
Simon Glass7106bd42016-02-22 22:55:33 -070070static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +000071{
Peter Tyser632d9ca2010-04-04 22:36:03 -050072 char *ptr;
wdenk5b1d7132002-11-03 00:07:02 +000073
74 while (--argc > 0 && **++argv == '-') {
75 while (*++*argv) {
76 switch (**argv) {
77 case 'l':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053078 params.lflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +000079 break;
80 case 'A':
81 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053082 (params.arch =
83 genimg_get_arch_id (*++argv)) < 0)
Simon Glass7106bd42016-02-22 22:55:33 -070084 usage();
wdenk5b1d7132002-11-03 00:07:02 +000085 goto NXTARG;
Simon Glassbf27d3d2013-06-13 15:10:06 -070086 case 'c':
87 if (--argc <= 0)
88 usage();
89 params.comment = *++argv;
90 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +000091 case 'C':
92 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053093 (params.comp =
94 genimg_get_comp_id (*++argv)) < 0)
Simon Glass7106bd42016-02-22 22:55:33 -070095 usage();
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +010096 goto NXTARG;
97 case 'D':
98 if (--argc <= 0)
Simon Glass7106bd42016-02-22 22:55:33 -070099 usage();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530100 params.dtc = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000101 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100102
wdenk5b1d7132002-11-03 00:07:02 +0000103 case 'O':
104 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530105 (params.os =
106 genimg_get_os_id (*++argv)) < 0)
Simon Glass7106bd42016-02-22 22:55:33 -0700107 usage();
wdenk5b1d7132002-11-03 00:07:02 +0000108 goto NXTARG;
109 case 'T':
Simon Glass434a31b2015-06-23 15:38:25 -0600110 params.type = -1;
111 if (--argc >= 0 && argv[1]) {
112 params.type =
113 genimg_get_type_id(*++argv);
114 }
115 if (params.type < 0) {
116 show_image_types();
117 usage();
118 }
wdenk5b1d7132002-11-03 00:07:02 +0000119 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000120 case 'a':
121 if (--argc <= 0)
Simon Glass7106bd42016-02-22 22:55:33 -0700122 usage();
William Cohena4e80c52016-02-16 08:57:46 -0500123 params.addr = strtoull(*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000124 if (*ptr) {
125 fprintf (stderr,
126 "%s: invalid load address %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530127 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000128 exit (EXIT_FAILURE);
129 }
130 goto NXTARG;
131 case 'd':
132 if (--argc <= 0)
Simon Glass7106bd42016-02-22 22:55:33 -0700133 usage();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530134 params.datafile = *++argv;
135 params.dflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000136 goto NXTARG;
137 case 'e':
138 if (--argc <= 0)
Simon Glass7106bd42016-02-22 22:55:33 -0700139 usage();
William Cohena4e80c52016-02-16 08:57:46 -0500140 params.ep = strtoull(*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000141 if (*ptr) {
142 fprintf (stderr,
143 "%s: invalid entry point %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530144 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000145 exit (EXIT_FAILURE);
146 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530147 params.eflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000148 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100149 case 'f':
150 if (--argc <= 0)
Simon Glass7106bd42016-02-22 22:55:33 -0700151 usage();
Simon Glassce8c3ca2013-06-13 15:10:05 -0700152 params.datafile = *++argv;
153 /* no break */
154 case 'F':
Peter Tyser94734e62009-11-24 16:42:08 -0600155 /*
156 * The flattened image tree (FIT) format
157 * requires a flattened device tree image type
158 */
159 params.type = IH_TYPE_FLATDT;
Peter Tyser54cd5392009-12-06 01:33:24 -0600160 params.fflag = 1;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100161 goto NXTARG;
Simon Glassb1489742013-06-13 15:10:03 -0700162 case 'k':
163 if (--argc <= 0)
164 usage();
165 params.keydir = *++argv;
166 goto NXTARG;
Simon Glassb4d8b092013-06-13 15:10:04 -0700167 case 'K':
168 if (--argc <= 0)
169 usage();
170 params.keydest = *++argv;
171 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000172 case 'n':
173 if (--argc <= 0)
Simon Glass7106bd42016-02-22 22:55:33 -0700174 usage();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530175 params.imagename = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000176 goto NXTARG;
Simon Glass817278a2013-06-13 15:10:07 -0700177 case 'r':
178 params.require_keys = 1;
179 break;
Shaohui Xieea65fd82012-08-10 02:49:35 +0000180 case 'R':
181 if (--argc <= 0)
182 usage();
183 /*
184 * This entry is for the second configuration
185 * file, if only one is not enough.
186 */
187 params.imagename2 = *++argv;
188 goto NXTARG;
Stefano Babic4a963302011-09-15 23:50:16 +0000189 case 's':
190 params.skipcpy = 1;
191 break;
wdenk5b1d7132002-11-03 00:07:02 +0000192 case 'v':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530193 params.vflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000194 break;
Wolfgang Denke97ab722011-02-12 10:37:11 +0100195 case 'V':
196 printf("mkimage version %s\n", PLAIN_VERSION);
197 exit(EXIT_SUCCESS);
wdenk5b1d7132002-11-03 00:07:02 +0000198 case 'x':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530199 params.xflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000200 break;
201 default:
Simon Glass7106bd42016-02-22 22:55:33 -0700202 usage();
wdenk5b1d7132002-11-03 00:07:02 +0000203 }
204 }
205NXTARG: ;
206 }
207
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530208 if (argc != 1)
Simon Glass7106bd42016-02-22 22:55:33 -0700209 usage();
210 params.imagefile = *argv;
211}
212
213
214int main(int argc, char **argv)
215{
216 int ifd = -1;
217 struct stat sbuf;
218 char *ptr;
219 int retval = 0;
220 struct image_type_params *tparams = NULL;
221 int pad_len = 0;
222 int dfd;
223
224 params.cmdname = *argv;
225 params.addr = 0;
226 params.ep = 0;
227
228 process_args(argc, argv);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530229
230 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200231 tparams = imagetool_get_type(params.type);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530232 if (tparams == NULL) {
233 fprintf (stderr, "%s: unsupported type %s\n",
234 params.cmdname, genimg_get_type_name(params.type));
235 exit (EXIT_FAILURE);
236 }
237
238 /*
239 * check the passed arguments parameters meets the requirements
240 * as per image type to be generated/listed
241 */
242 if (tparams->check_params)
243 if (tparams->check_params (&params))
Simon Glass7106bd42016-02-22 22:55:33 -0700244 usage();
wdenk5b1d7132002-11-03 00:07:02 +0000245
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530246 if (!params.eflag) {
247 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000248 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530249 if (params.xflag)
250 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000251 }
252
Peter Tyserb2f8b942009-11-24 16:42:10 -0600253 if (params.fflag){
254 if (tparams->fflag_handle)
255 /*
256 * in some cases, some additional processing needs
257 * to be done if fflag is defined
258 *
259 * For ex. fit_handle_file for Fit file support
260 */
261 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000262
Peter Tyserb2f8b942009-11-24 16:42:10 -0600263 if (retval != EXIT_SUCCESS)
264 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000265 }
266
Peter Tyserb2f8b942009-11-24 16:42:10 -0600267 if (params.lflag || params.fflag) {
268 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
269 } else {
270 ifd = open (params.imagefile,
271 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
272 }
273
274 if (ifd < 0) {
275 fprintf (stderr, "%s: Can't open %s: %s\n",
276 params.cmdname, params.imagefile,
277 strerror(errno));
278 exit (EXIT_FAILURE);
279 }
280
281 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000282 /*
283 * list header information of existing image
284 */
285 if (fstat(ifd, &sbuf) < 0) {
286 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530287 params.cmdname, params.imagefile,
288 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000289 exit (EXIT_FAILURE);
290 }
291
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530292 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000293 fprintf (stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530294 "%s: Bad size: \"%s\" is not valid image\n",
295 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000296 exit (EXIT_FAILURE);
297 }
298
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400299 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
300 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000301 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530302 params.cmdname, params.imagefile,
303 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000304 exit (EXIT_FAILURE);
305 }
306
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530307 /*
308 * scan through mkimage registry for all supported image types
309 * and verify the input image file header for match
310 * Print the image information for matched image type
311 * Returns the error code if not matched
312 */
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -0200313 retval = imagetool_verify_print_header(ptr, &sbuf,
314 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000315
wdenk5b1d7132002-11-03 00:07:02 +0000316 (void) munmap((void *)ptr, sbuf.st_size);
317 (void) close (ifd);
318
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530319 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000320 }
321
Marek Vasut4cc86272015-12-07 18:01:54 +0100322 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert3edd0412015-12-04 00:11:23 +0200323 dfd = open(params.datafile, O_RDONLY | O_BINARY);
324 if (dfd < 0) {
325 fprintf(stderr, "%s: Can't open %s: %s\n",
326 params.cmdname, params.datafile,
327 strerror(errno));
328 exit(EXIT_FAILURE);
329 }
Simon Glass139651f2015-06-23 15:39:12 -0600330
Philippe De Swert3edd0412015-12-04 00:11:23 +0200331 if (fstat(dfd, &sbuf) < 0) {
332 fprintf(stderr, "%s: Can't stat %s: %s\n",
333 params.cmdname, params.datafile,
334 strerror(errno));
335 exit(EXIT_FAILURE);
336 }
Simon Glass139651f2015-06-23 15:39:12 -0600337
Philippe De Swert3edd0412015-12-04 00:11:23 +0200338 params.file_size = sbuf.st_size + tparams->header_size;
339 close(dfd);
340 }
Simon Glass139651f2015-06-23 15:39:12 -0600341
wdenk5b1d7132002-11-03 00:07:02 +0000342 /*
Stefano Babic4a963302011-09-15 23:50:16 +0000343 * In case there an header with a variable
344 * length will be added, the corresponding
345 * function is called. This is responsible to
346 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000347 */
Stefano Babic4a963302011-09-15 23:50:16 +0000348 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200349 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000350 else
351 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000352
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530353 if (write(ifd, tparams->hdr, tparams->header_size)
354 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000355 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530356 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000357 exit (EXIT_FAILURE);
358 }
359
Christian Rieschc14f2812011-12-09 09:47:38 +0000360 if (!params.skipcpy) {
361 if (params.type == IH_TYPE_MULTI ||
362 params.type == IH_TYPE_SCRIPT) {
363 char *file = params.datafile;
364 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000365
Christian Rieschc14f2812011-12-09 09:47:38 +0000366 for (;;) {
367 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000368
Christian Rieschc14f2812011-12-09 09:47:38 +0000369 if (file) {
370 if ((sep = strchr(file, ':')) != NULL) {
371 *sep = '\0';
372 }
373
374 if (stat (file, &sbuf) < 0) {
375 fprintf (stderr, "%s: Can't stat %s: %s\n",
376 params.cmdname, file, strerror(errno));
377 exit (EXIT_FAILURE);
378 }
379 size = cpu_to_uimage (sbuf.st_size);
380 } else {
381 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000382 }
383
Christian Rieschc14f2812011-12-09 09:47:38 +0000384 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
385 fprintf (stderr, "%s: Write error on %s: %s\n",
386 params.cmdname, params.imagefile,
387 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000388 exit (EXIT_FAILURE);
389 }
wdenk5b1d7132002-11-03 00:07:02 +0000390
Christian Rieschc14f2812011-12-09 09:47:38 +0000391 if (!file) {
392 break;
393 }
wdenk5b1d7132002-11-03 00:07:02 +0000394
Christian Rieschc14f2812011-12-09 09:47:38 +0000395 if (sep) {
396 *sep = ':';
397 file = sep + 1;
398 } else {
399 file = NULL;
400 }
wdenk5b1d7132002-11-03 00:07:02 +0000401 }
wdenk5b1d7132002-11-03 00:07:02 +0000402
Christian Rieschc14f2812011-12-09 09:47:38 +0000403 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000404
Christian Rieschc14f2812011-12-09 09:47:38 +0000405 for (;;) {
406 char *sep = strchr(file, ':');
407 if (sep) {
408 *sep = '\0';
409 copy_file (ifd, file, 1);
410 *sep++ = ':';
411 file = sep;
412 } else {
413 copy_file (ifd, file, 0);
414 break;
415 }
wdenk5b1d7132002-11-03 00:07:02 +0000416 }
Shaohui Xieea65fd82012-08-10 02:49:35 +0000417 } else if (params.type == IH_TYPE_PBLIMAGE) {
418 /* PBL has special Image format, implements its' own */
419 pbl_load_uboot(ifd, &params);
Christian Rieschc14f2812011-12-09 09:47:38 +0000420 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200421 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000422 }
wdenk5b1d7132002-11-03 00:07:02 +0000423 }
424
425 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530426#if defined(_POSIX_SYNCHRONIZED_IO) && \
427 !defined(__sun__) && \
428 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200429 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530430 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000431 (void) fdatasync (ifd);
432#else
433 (void) fsync (ifd);
434#endif
435
436 if (fstat(ifd, &sbuf) < 0) {
437 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530438 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000439 exit (EXIT_FAILURE);
440 }
Simon Glass139651f2015-06-23 15:39:12 -0600441 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000442
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400443 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
444 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000445 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530446 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000447 exit (EXIT_FAILURE);
448 }
449
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530450 /* Setup the image header as per input image type*/
451 if (tparams->set_header)
452 tparams->set_header (ptr, &sbuf, ifd, &params);
453 else {
454 fprintf (stderr, "%s: Can't set header for %s: %s\n",
455 params.cmdname, tparams->name, strerror(errno));
456 exit (EXIT_FAILURE);
457 }
wdenk5b1d7132002-11-03 00:07:02 +0000458
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530459 /* Print the image information by processing image header */
460 if (tparams->print_header)
461 tparams->print_header (ptr);
462 else {
463 fprintf (stderr, "%s: Can't print header for %s: %s\n",
464 params.cmdname, tparams->name, strerror(errno));
465 exit (EXIT_FAILURE);
466 }
wdenk5b1d7132002-11-03 00:07:02 +0000467
468 (void) munmap((void *)ptr, sbuf.st_size);
469
470 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530471#if defined(_POSIX_SYNCHRONIZED_IO) && \
472 !defined(__sun__) && \
473 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200474 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530475 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000476 (void) fdatasync (ifd);
477#else
478 (void) fsync (ifd);
479#endif
480
481 if (close(ifd)) {
482 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530483 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000484 exit (EXIT_FAILURE);
485 }
486
487 exit (EXIT_SUCCESS);
488}
489
490static void
491copy_file (int ifd, const char *datafile, int pad)
492{
493 int dfd;
494 struct stat sbuf;
495 unsigned char *ptr;
496 int tail;
497 int zero = 0;
Stefano Babic6531bd92013-08-19 19:03:19 +0200498 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000499 int offset = 0;
500 int size;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200501 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000502
Stefano Babic6531bd92013-08-19 19:03:19 +0200503 memset(zeros, 0, sizeof(zeros));
504
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530505 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000506 fprintf (stderr, "Adding Image %s\n", datafile);
507 }
508
wdenk1ee550e2003-10-08 22:14:02 +0000509 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000510 fprintf (stderr, "%s: Can't open %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
515 if (fstat(dfd, &sbuf) < 0) {
516 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530517 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000518 exit (EXIT_FAILURE);
519 }
520
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400521 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
522 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000523 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530524 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000525 exit (EXIT_FAILURE);
526 }
527
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530528 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000529 unsigned char *p = NULL;
530 /*
531 * XIP: do not append the image_header_t at the
532 * beginning of the file, but consume the space
533 * reserved for it.
534 */
535
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530536 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000537 fprintf (stderr,
538 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530539 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000540 exit (EXIT_FAILURE);
541 }
542
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530543 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000544 if ( *p != 0xff ) {
545 fprintf (stderr,
546 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530547 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000548 exit (EXIT_FAILURE);
549 }
550 }
551
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530552 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000553 }
554
555 size = sbuf.st_size - offset;
556 if (write(ifd, ptr + offset, size) != size) {
557 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530558 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000559 exit (EXIT_FAILURE);
560 }
561
Stefano Babic6531bd92013-08-19 19:03:19 +0200562 tail = size % 4;
563 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000564
565 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
566 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530567 params.cmdname, params.imagefile,
568 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000569 exit (EXIT_FAILURE);
570 }
Stefano Babic6531bd92013-08-19 19:03:19 +0200571 } else if (pad > 1) {
Simon Glass46409cc2015-08-30 16:55:22 -0600572 while (pad > 0) {
573 int todo = sizeof(zeros);
574
575 if (todo > pad)
576 todo = pad;
577 if (write(ifd, (char *)&zeros, todo) != todo) {
578 fprintf(stderr, "%s: Write error on %s: %s\n",
579 params.cmdname, params.imagefile,
580 strerror(errno));
581 exit(EXIT_FAILURE);
582 }
583 pad -= todo;
Stefano Babic6531bd92013-08-19 19:03:19 +0200584 }
wdenk5b1d7132002-11-03 00:07:02 +0000585 }
586
587 (void) munmap((void *)ptr, sbuf.st_size);
588 (void) close (dfd);
589}
590
Guilherme Maciel Ferreiraff0fa612013-12-01 12:43:09 -0700591static void usage(void)
wdenk5b1d7132002-11-03 00:07:02 +0000592{
593 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100594 " -l ==> list image header information\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530595 params.cmdname);
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100596 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
597 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
598 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000599 " -O ==> set operating system to 'os'\n"
600 " -T ==> set image type to 'type'\n"
601 " -C ==> set compression type 'comp'\n"
602 " -a ==> set load address to 'addr' (hex)\n"
603 " -e ==> set entry point to 'ep' (hex)\n"
604 " -n ==> set image name to 'name'\n"
605 " -d ==> use image data from 'datafile'\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100606 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530607 params.cmdname);
Simon Glassce8c3ca2013-06-13 15:10:05 -0700608 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530609 params.cmdname);
Tom Rinie140a462015-08-26 15:21:23 -0400610 fprintf(stderr, " -D => set all options for device tree compiler\n"
Simon Glassb1489742013-06-13 15:10:03 -0700611 " -f => input filename for FIT source\n");
612#ifdef CONFIG_FIT_SIGNATURE
Simon Glass817278a2013-06-13 15:10:07 -0700613 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
Simon Glassb4d8b092013-06-13 15:10:04 -0700614 " -k => set directory containing private keys\n"
Simon Glassce8c3ca2013-06-13 15:10:05 -0700615 " -K => write public keys to this .dtb file\n"
Simon Glassbf27d3d2013-06-13 15:10:06 -0700616 " -c => add comment in signature node\n"
Simon Glass817278a2013-06-13 15:10:07 -0700617 " -F => re-sign existing FIT image\n"
618 " -r => mark keys used as 'required' in dtb\n");
Simon Glassb1489742013-06-13 15:10:03 -0700619#else
620 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
621#endif
Wolfgang Denke97ab722011-02-12 10:37:11 +0100622 fprintf (stderr, " %s -V ==> print version information and exit\n",
623 params.cmdname);
Simon Glass434a31b2015-06-23 15:38:25 -0600624 fprintf(stderr, "Use -T to see a list of available image types\n");
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100625
wdenk5b1d7132002-11-03 00:07:02 +0000626 exit (EXIT_FAILURE);
627}