blob: c70408c9ba02375ae959e20d438e4e65acfd75b0 [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/* image_type_params link list to maintain registered image type supports */
19struct image_type_params *mkimage_tparams = NULL;
wdenk5b1d7132002-11-03 00:07:02 +000020
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053021/* parameters initialized by core will be used by the image type code */
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070022struct image_tool_params params = {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053023 .os = IH_OS_LINUX,
24 .arch = IH_ARCH_PPC,
25 .type = IH_TYPE_KERNEL,
26 .comp = IH_COMP_GZIP,
27 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
Wolfgang Denk23416b82010-03-27 23:37:46 +010028 .imagename = "",
Shaohui Xieea65fd82012-08-10 02:49:35 +000029 .imagename2 = "",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053030};
wdenk5b1d7132002-11-03 00:07:02 +000031
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053032/*
33 * mkimage_register -
34 *
35 * It is used to register respective image generation/list support to the
36 * mkimage core
37 *
38 * the input struct image_type_params is checked and appended to the link
39 * list, if the input structure is already registered, error
40 */
41void mkimage_register (struct image_type_params *tparams)
42{
43 struct image_type_params **tp;
44
45 if (!tparams) {
46 fprintf (stderr, "%s: %s: Null input\n",
47 params.cmdname, __FUNCTION__);
48 exit (EXIT_FAILURE);
49 }
50
51 /* scan the linked list, check for registry and point the last one */
52 for (tp = &mkimage_tparams; *tp != NULL; tp = &(*tp)->next) {
53 if (!strcmp((*tp)->name, tparams->name)) {
54 fprintf (stderr, "%s: %s already registered\n",
55 params.cmdname, tparams->name);
56 return;
57 }
58 }
59
60 /* add input struct entry at the end of link list */
61 *tp = tparams;
62 /* mark input entry as last entry in the link list */
63 tparams->next = NULL;
64
65 debug ("Registered %s\n", tparams->name);
66}
67
68/*
69 * mkimage_get_type -
70 *
71 * It scans all registers image type supports
72 * checks the input type_id for each supported image type
73 *
74 * if successful,
75 * returns respective image_type_params pointer if success
76 * if input type_id is not supported by any of image_type_support
77 * returns NULL
78 */
79struct image_type_params *mkimage_get_type(int type)
80{
81 struct image_type_params *curr;
82
83 for (curr = mkimage_tparams; curr != NULL; curr = curr->next) {
84 if (curr->check_image_type) {
85 if (!curr->check_image_type (type))
86 return curr;
87 }
88 }
89 return NULL;
90}
91
92/*
93 * mkimage_verify_print_header -
94 *
95 * It scans mkimage_tparams link list,
96 * verifies image_header for each supported image type
97 * if verification is successful, prints respective header
98 *
99 * returns negative if input image format does not match with any of
100 * supported image types
101 */
102int mkimage_verify_print_header (void *ptr, struct stat *sbuf)
103{
104 int retval = -1;
105 struct image_type_params *curr;
106
107 for (curr = mkimage_tparams; curr != NULL; curr = curr->next ) {
108 if (curr->verify_header) {
109 retval = curr->verify_header (
110 (unsigned char *)ptr, sbuf->st_size,
111 &params);
112
113 if (retval == 0) {
114 /*
115 * Print the image information
116 * if verify is successful
117 */
118 if (curr->print_header)
119 curr->print_header (ptr);
120 else {
121 fprintf (stderr,
122 "%s: print_header undefined for %s\n",
123 params.cmdname, curr->name);
124 }
125 break;
126 }
127 }
128 }
129 return retval;
130}
wdenk5b1d7132002-11-03 00:07:02 +0000131
132int
133main (int argc, char **argv)
134{
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100135 int ifd = -1;
wdenk5b1d7132002-11-03 00:07:02 +0000136 struct stat sbuf;
Peter Tyser632d9ca2010-04-04 22:36:03 -0500137 char *ptr;
Prafulla Wadaskar4d4d67d2009-08-16 05:28:19 +0530138 int retval = 0;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530139 struct image_type_params *tparams = NULL;
Stefano Babic6531bd92013-08-19 19:03:19 +0200140 int pad_len = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000141
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700142 /* Init all image generation/list support */
143 register_image_tool(mkimage_register);
wdenk5b1d7132002-11-03 00:07:02 +0000144
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530145 params.cmdname = *argv;
146 params.addr = params.ep = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000147
148 while (--argc > 0 && **++argv == '-') {
149 while (*++*argv) {
150 switch (**argv) {
151 case 'l':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530152 params.lflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000153 break;
154 case 'A':
155 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530156 (params.arch =
157 genimg_get_arch_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000158 usage ();
159 goto NXTARG;
Simon Glassbf27d3d2013-06-13 15:10:06 -0700160 case 'c':
161 if (--argc <= 0)
162 usage();
163 params.comment = *++argv;
164 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000165 case 'C':
166 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530167 (params.comp =
168 genimg_get_comp_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000169 usage ();
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100170 goto NXTARG;
171 case 'D':
172 if (--argc <= 0)
173 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530174 params.dtc = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000175 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100176
wdenk5b1d7132002-11-03 00:07:02 +0000177 case 'O':
178 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530179 (params.os =
180 genimg_get_os_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000181 usage ();
182 goto NXTARG;
183 case 'T':
184 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530185 (params.type =
186 genimg_get_type_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000187 usage ();
188 goto NXTARG;
189
190 case 'a':
191 if (--argc <= 0)
192 usage ();
Peter Tyser632d9ca2010-04-04 22:36:03 -0500193 params.addr = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000194 if (*ptr) {
195 fprintf (stderr,
196 "%s: invalid load address %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530197 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000198 exit (EXIT_FAILURE);
199 }
200 goto NXTARG;
201 case 'd':
202 if (--argc <= 0)
203 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530204 params.datafile = *++argv;
205 params.dflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000206 goto NXTARG;
207 case 'e':
208 if (--argc <= 0)
209 usage ();
Peter Tyser632d9ca2010-04-04 22:36:03 -0500210 params.ep = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000211 if (*ptr) {
212 fprintf (stderr,
213 "%s: invalid entry point %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530214 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000215 exit (EXIT_FAILURE);
216 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530217 params.eflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000218 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100219 case 'f':
220 if (--argc <= 0)
221 usage ();
Simon Glassce8c3ca2013-06-13 15:10:05 -0700222 params.datafile = *++argv;
223 /* no break */
224 case 'F':
Peter Tyser94734e62009-11-24 16:42:08 -0600225 /*
226 * The flattened image tree (FIT) format
227 * requires a flattened device tree image type
228 */
229 params.type = IH_TYPE_FLATDT;
Peter Tyser54cd5392009-12-06 01:33:24 -0600230 params.fflag = 1;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100231 goto NXTARG;
Simon Glassb1489742013-06-13 15:10:03 -0700232 case 'k':
233 if (--argc <= 0)
234 usage();
235 params.keydir = *++argv;
236 goto NXTARG;
Simon Glassb4d8b092013-06-13 15:10:04 -0700237 case 'K':
238 if (--argc <= 0)
239 usage();
240 params.keydest = *++argv;
241 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000242 case 'n':
243 if (--argc <= 0)
244 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530245 params.imagename = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000246 goto NXTARG;
Simon Glass817278a2013-06-13 15:10:07 -0700247 case 'r':
248 params.require_keys = 1;
249 break;
Shaohui Xieea65fd82012-08-10 02:49:35 +0000250 case 'R':
251 if (--argc <= 0)
252 usage();
253 /*
254 * This entry is for the second configuration
255 * file, if only one is not enough.
256 */
257 params.imagename2 = *++argv;
258 goto NXTARG;
Stefano Babic4a963302011-09-15 23:50:16 +0000259 case 's':
260 params.skipcpy = 1;
261 break;
wdenk5b1d7132002-11-03 00:07:02 +0000262 case 'v':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530263 params.vflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000264 break;
Wolfgang Denke97ab722011-02-12 10:37:11 +0100265 case 'V':
266 printf("mkimage version %s\n", PLAIN_VERSION);
267 exit(EXIT_SUCCESS);
wdenk5b1d7132002-11-03 00:07:02 +0000268 case 'x':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530269 params.xflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000270 break;
271 default:
272 usage ();
273 }
274 }
275NXTARG: ;
276 }
277
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530278 if (argc != 1)
279 usage ();
280
281 /* set tparams as per input type_id */
282 tparams = mkimage_get_type(params.type);
283 if (tparams == NULL) {
284 fprintf (stderr, "%s: unsupported type %s\n",
285 params.cmdname, genimg_get_type_name(params.type));
286 exit (EXIT_FAILURE);
287 }
288
289 /*
290 * check the passed arguments parameters meets the requirements
291 * as per image type to be generated/listed
292 */
293 if (tparams->check_params)
294 if (tparams->check_params (&params))
295 usage ();
wdenk5b1d7132002-11-03 00:07:02 +0000296
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530297 if (!params.eflag) {
298 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000299 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530300 if (params.xflag)
301 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000302 }
303
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530304 params.imagefile = *argv;
wdenk5b1d7132002-11-03 00:07:02 +0000305
Peter Tyserb2f8b942009-11-24 16:42:10 -0600306 if (params.fflag){
307 if (tparams->fflag_handle)
308 /*
309 * in some cases, some additional processing needs
310 * to be done if fflag is defined
311 *
312 * For ex. fit_handle_file for Fit file support
313 */
314 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000315
Peter Tyserb2f8b942009-11-24 16:42:10 -0600316 if (retval != EXIT_SUCCESS)
317 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000318 }
319
Peter Tyserb2f8b942009-11-24 16:42:10 -0600320 if (params.lflag || params.fflag) {
321 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
322 } else {
323 ifd = open (params.imagefile,
324 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
325 }
326
327 if (ifd < 0) {
328 fprintf (stderr, "%s: Can't open %s: %s\n",
329 params.cmdname, params.imagefile,
330 strerror(errno));
331 exit (EXIT_FAILURE);
332 }
333
334 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000335 /*
336 * list header information of existing image
337 */
338 if (fstat(ifd, &sbuf) < 0) {
339 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530340 params.cmdname, params.imagefile,
341 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000342 exit (EXIT_FAILURE);
343 }
344
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530345 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000346 fprintf (stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530347 "%s: Bad size: \"%s\" is not valid image\n",
348 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000349 exit (EXIT_FAILURE);
350 }
351
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400352 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
353 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000354 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530355 params.cmdname, params.imagefile,
356 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000357 exit (EXIT_FAILURE);
358 }
359
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530360 /*
361 * scan through mkimage registry for all supported image types
362 * and verify the input image file header for match
363 * Print the image information for matched image type
364 * Returns the error code if not matched
365 */
366 retval = mkimage_verify_print_header (ptr, &sbuf);
wdenk5b1d7132002-11-03 00:07:02 +0000367
wdenk5b1d7132002-11-03 00:07:02 +0000368 (void) munmap((void *)ptr, sbuf.st_size);
369 (void) close (ifd);
370
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530371 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000372 }
373
374 /*
Stefano Babic4a963302011-09-15 23:50:16 +0000375 * In case there an header with a variable
376 * length will be added, the corresponding
377 * function is called. This is responsible to
378 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000379 */
Stefano Babic4a963302011-09-15 23:50:16 +0000380 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200381 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000382 else
383 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000384
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530385 if (write(ifd, tparams->hdr, tparams->header_size)
386 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000387 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530388 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000389 exit (EXIT_FAILURE);
390 }
391
Christian Rieschc14f2812011-12-09 09:47:38 +0000392 if (!params.skipcpy) {
393 if (params.type == IH_TYPE_MULTI ||
394 params.type == IH_TYPE_SCRIPT) {
395 char *file = params.datafile;
396 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000397
Christian Rieschc14f2812011-12-09 09:47:38 +0000398 for (;;) {
399 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000400
Christian Rieschc14f2812011-12-09 09:47:38 +0000401 if (file) {
402 if ((sep = strchr(file, ':')) != NULL) {
403 *sep = '\0';
404 }
405
406 if (stat (file, &sbuf) < 0) {
407 fprintf (stderr, "%s: Can't stat %s: %s\n",
408 params.cmdname, file, strerror(errno));
409 exit (EXIT_FAILURE);
410 }
411 size = cpu_to_uimage (sbuf.st_size);
412 } else {
413 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000414 }
415
Christian Rieschc14f2812011-12-09 09:47:38 +0000416 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
417 fprintf (stderr, "%s: Write error on %s: %s\n",
418 params.cmdname, params.imagefile,
419 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000420 exit (EXIT_FAILURE);
421 }
wdenk5b1d7132002-11-03 00:07:02 +0000422
Christian Rieschc14f2812011-12-09 09:47:38 +0000423 if (!file) {
424 break;
425 }
wdenk5b1d7132002-11-03 00:07:02 +0000426
Christian Rieschc14f2812011-12-09 09:47:38 +0000427 if (sep) {
428 *sep = ':';
429 file = sep + 1;
430 } else {
431 file = NULL;
432 }
wdenk5b1d7132002-11-03 00:07:02 +0000433 }
wdenk5b1d7132002-11-03 00:07:02 +0000434
Christian Rieschc14f2812011-12-09 09:47:38 +0000435 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000436
Christian Rieschc14f2812011-12-09 09:47:38 +0000437 for (;;) {
438 char *sep = strchr(file, ':');
439 if (sep) {
440 *sep = '\0';
441 copy_file (ifd, file, 1);
442 *sep++ = ':';
443 file = sep;
444 } else {
445 copy_file (ifd, file, 0);
446 break;
447 }
wdenk5b1d7132002-11-03 00:07:02 +0000448 }
Shaohui Xieea65fd82012-08-10 02:49:35 +0000449 } else if (params.type == IH_TYPE_PBLIMAGE) {
450 /* PBL has special Image format, implements its' own */
451 pbl_load_uboot(ifd, &params);
Christian Rieschc14f2812011-12-09 09:47:38 +0000452 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200453 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000454 }
wdenk5b1d7132002-11-03 00:07:02 +0000455 }
456
457 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530458#if defined(_POSIX_SYNCHRONIZED_IO) && \
459 !defined(__sun__) && \
460 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200461 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530462 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000463 (void) fdatasync (ifd);
464#else
465 (void) fsync (ifd);
466#endif
467
468 if (fstat(ifd, &sbuf) < 0) {
469 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530470 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000471 exit (EXIT_FAILURE);
472 }
473
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400474 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
475 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000476 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530477 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000478 exit (EXIT_FAILURE);
479 }
480
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530481 /* Setup the image header as per input image type*/
482 if (tparams->set_header)
483 tparams->set_header (ptr, &sbuf, ifd, &params);
484 else {
485 fprintf (stderr, "%s: Can't set header for %s: %s\n",
486 params.cmdname, tparams->name, strerror(errno));
487 exit (EXIT_FAILURE);
488 }
wdenk5b1d7132002-11-03 00:07:02 +0000489
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530490 /* Print the image information by processing image header */
491 if (tparams->print_header)
492 tparams->print_header (ptr);
493 else {
494 fprintf (stderr, "%s: Can't print header for %s: %s\n",
495 params.cmdname, tparams->name, strerror(errno));
496 exit (EXIT_FAILURE);
497 }
wdenk5b1d7132002-11-03 00:07:02 +0000498
499 (void) munmap((void *)ptr, sbuf.st_size);
500
501 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530502#if defined(_POSIX_SYNCHRONIZED_IO) && \
503 !defined(__sun__) && \
504 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200505 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530506 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000507 (void) fdatasync (ifd);
508#else
509 (void) fsync (ifd);
510#endif
511
512 if (close(ifd)) {
513 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530514 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000515 exit (EXIT_FAILURE);
516 }
517
518 exit (EXIT_SUCCESS);
519}
520
521static void
522copy_file (int ifd, const char *datafile, int pad)
523{
524 int dfd;
525 struct stat sbuf;
526 unsigned char *ptr;
527 int tail;
528 int zero = 0;
Stefano Babic6531bd92013-08-19 19:03:19 +0200529 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000530 int offset = 0;
531 int size;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530532 struct image_type_params *tparams = mkimage_get_type (params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000533
Stefano Babic6531bd92013-08-19 19:03:19 +0200534 if (pad >= sizeof(zeros)) {
535 fprintf(stderr, "%s: Can't pad to %d\n",
536 params.cmdname, pad);
537 exit(EXIT_FAILURE);
538 }
539
540 memset(zeros, 0, sizeof(zeros));
541
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530542 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000543 fprintf (stderr, "Adding Image %s\n", datafile);
544 }
545
wdenk1ee550e2003-10-08 22:14:02 +0000546 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000547 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530548 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000549 exit (EXIT_FAILURE);
550 }
551
552 if (fstat(dfd, &sbuf) < 0) {
553 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530554 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000555 exit (EXIT_FAILURE);
556 }
557
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400558 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
559 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000560 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530561 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000562 exit (EXIT_FAILURE);
563 }
564
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530565 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000566 unsigned char *p = NULL;
567 /*
568 * XIP: do not append the image_header_t at the
569 * beginning of the file, but consume the space
570 * reserved for it.
571 */
572
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530573 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000574 fprintf (stderr,
575 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530576 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000577 exit (EXIT_FAILURE);
578 }
579
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530580 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000581 if ( *p != 0xff ) {
582 fprintf (stderr,
583 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530584 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000585 exit (EXIT_FAILURE);
586 }
587 }
588
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530589 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000590 }
591
592 size = sbuf.st_size - offset;
593 if (write(ifd, ptr + offset, size) != size) {
594 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530595 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000596 exit (EXIT_FAILURE);
597 }
598
Stefano Babic6531bd92013-08-19 19:03:19 +0200599 tail = size % 4;
600 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000601
602 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
603 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530604 params.cmdname, params.imagefile,
605 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000606 exit (EXIT_FAILURE);
607 }
Stefano Babic6531bd92013-08-19 19:03:19 +0200608 } else if (pad > 1) {
609 if (write(ifd, (char *)&zeros, pad) != pad) {
610 fprintf(stderr, "%s: Write error on %s: %s\n",
611 params.cmdname, params.imagefile,
612 strerror(errno));
613 exit(EXIT_FAILURE);
614 }
wdenk5b1d7132002-11-03 00:07:02 +0000615 }
616
617 (void) munmap((void *)ptr, sbuf.st_size);
618 (void) close (dfd);
619}
620
Guilherme Maciel Ferreiraff0fa612013-12-01 12:43:09 -0700621static void usage(void)
wdenk5b1d7132002-11-03 00:07:02 +0000622{
623 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100624 " -l ==> list image header information\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530625 params.cmdname);
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100626 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
627 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
628 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000629 " -O ==> set operating system to 'os'\n"
630 " -T ==> set image type to 'type'\n"
631 " -C ==> set compression type 'comp'\n"
632 " -a ==> set load address to 'addr' (hex)\n"
633 " -e ==> set entry point to 'ep' (hex)\n"
634 " -n ==> set image name to 'name'\n"
635 " -d ==> use image data from 'datafile'\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100636 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530637 params.cmdname);
Simon Glassce8c3ca2013-06-13 15:10:05 -0700638 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530639 params.cmdname);
Simon Glassb1489742013-06-13 15:10:03 -0700640 fprintf(stderr, " -D => set options for device tree compiler\n"
641 " -f => input filename for FIT source\n");
642#ifdef CONFIG_FIT_SIGNATURE
Simon Glass817278a2013-06-13 15:10:07 -0700643 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
Simon Glassb4d8b092013-06-13 15:10:04 -0700644 " -k => set directory containing private keys\n"
Simon Glassce8c3ca2013-06-13 15:10:05 -0700645 " -K => write public keys to this .dtb file\n"
Simon Glassbf27d3d2013-06-13 15:10:06 -0700646 " -c => add comment in signature node\n"
Simon Glass817278a2013-06-13 15:10:07 -0700647 " -F => re-sign existing FIT image\n"
648 " -r => mark keys used as 'required' in dtb\n");
Simon Glassb1489742013-06-13 15:10:03 -0700649#else
650 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
651#endif
Wolfgang Denke97ab722011-02-12 10:37:11 +0100652 fprintf (stderr, " %s -V ==> print version information and exit\n",
653 params.cmdname);
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100654
wdenk5b1d7132002-11-03 00:07:02 +0000655 exit (EXIT_FAILURE);
656}