blob: b3b45a47a3f14e79c9f5213ad57e9d800ec19965 [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 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
wdenk5b1d7132002-11-03 00:07:02 +000022 */
23
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010024#include "mkimage.h"
wdenk5b1d7132002-11-03 00:07:02 +000025#include <image.h>
Wolfgang Denk976b38c2011-02-12 10:37:11 +010026#include <version.h>
wdenk5b1d7132002-11-03 00:07:02 +000027
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053028static void copy_file(int, const char *, int);
29static void usage(void);
wdenk5b1d7132002-11-03 00:07:02 +000030
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053031/* image_type_params link list to maintain registered image type supports */
32struct image_type_params *mkimage_tparams = NULL;
wdenk5b1d7132002-11-03 00:07:02 +000033
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053034/* parameters initialized by core will be used by the image type code */
35struct mkimage_params params = {
36 .os = IH_OS_LINUX,
37 .arch = IH_ARCH_PPC,
38 .type = IH_TYPE_KERNEL,
39 .comp = IH_COMP_GZIP,
40 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
Wolfgang Denk04387d22010-03-27 23:37:46 +010041 .imagename = "",
Shaohui Xie5d898a02012-08-10 02:49:35 +000042 .imagename2 = "",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053043};
wdenk5b1d7132002-11-03 00:07:02 +000044
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053045/*
46 * mkimage_register -
47 *
48 * It is used to register respective image generation/list support to the
49 * mkimage core
50 *
51 * the input struct image_type_params is checked and appended to the link
52 * list, if the input structure is already registered, error
53 */
54void mkimage_register (struct image_type_params *tparams)
55{
56 struct image_type_params **tp;
57
58 if (!tparams) {
59 fprintf (stderr, "%s: %s: Null input\n",
60 params.cmdname, __FUNCTION__);
61 exit (EXIT_FAILURE);
62 }
63
64 /* scan the linked list, check for registry and point the last one */
65 for (tp = &mkimage_tparams; *tp != NULL; tp = &(*tp)->next) {
66 if (!strcmp((*tp)->name, tparams->name)) {
67 fprintf (stderr, "%s: %s already registered\n",
68 params.cmdname, tparams->name);
69 return;
70 }
71 }
72
73 /* add input struct entry at the end of link list */
74 *tp = tparams;
75 /* mark input entry as last entry in the link list */
76 tparams->next = NULL;
77
78 debug ("Registered %s\n", tparams->name);
79}
80
81/*
82 * mkimage_get_type -
83 *
84 * It scans all registers image type supports
85 * checks the input type_id for each supported image type
86 *
87 * if successful,
88 * returns respective image_type_params pointer if success
89 * if input type_id is not supported by any of image_type_support
90 * returns NULL
91 */
92struct image_type_params *mkimage_get_type(int type)
93{
94 struct image_type_params *curr;
95
96 for (curr = mkimage_tparams; curr != NULL; curr = curr->next) {
97 if (curr->check_image_type) {
98 if (!curr->check_image_type (type))
99 return curr;
100 }
101 }
102 return NULL;
103}
104
105/*
106 * mkimage_verify_print_header -
107 *
108 * It scans mkimage_tparams link list,
109 * verifies image_header for each supported image type
110 * if verification is successful, prints respective header
111 *
112 * returns negative if input image format does not match with any of
113 * supported image types
114 */
115int mkimage_verify_print_header (void *ptr, struct stat *sbuf)
116{
117 int retval = -1;
118 struct image_type_params *curr;
119
120 for (curr = mkimage_tparams; curr != NULL; curr = curr->next ) {
121 if (curr->verify_header) {
122 retval = curr->verify_header (
123 (unsigned char *)ptr, sbuf->st_size,
124 &params);
125
126 if (retval == 0) {
127 /*
128 * Print the image information
129 * if verify is successful
130 */
131 if (curr->print_header)
132 curr->print_header (ptr);
133 else {
134 fprintf (stderr,
135 "%s: print_header undefined for %s\n",
136 params.cmdname, curr->name);
137 }
138 break;
139 }
140 }
141 }
142 return retval;
143}
wdenk5b1d7132002-11-03 00:07:02 +0000144
145int
146main (int argc, char **argv)
147{
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100148 int ifd = -1;
wdenk5b1d7132002-11-03 00:07:02 +0000149 struct stat sbuf;
Peter Tysera2513e22010-04-04 22:36:03 -0500150 char *ptr;
Prafulla Wadaskar449609f2009-08-16 05:28:19 +0530151 int retval = 0;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530152 struct image_type_params *tparams = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000153
Shaohui Xie5d898a02012-08-10 02:49:35 +0000154 /* Init Freescale PBL Boot image generation/list support */
155 init_pbl_image_type();
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530156 /* Init Kirkwood Boot image generation/list support */
157 init_kwb_image_type ();
Stefano Babic8edcde52010-01-20 18:19:10 +0100158 /* Init Freescale imx Boot image generation/list support */
159 init_imx_image_type ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530160 /* Init FIT image generation/list support */
161 init_fit_image_type ();
John Rigby3decb142011-07-21 09:10:30 -0400162 /* Init TI OMAP Boot image generation/list support */
163 init_omap_image_type();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530164 /* Init Default image generation/list support */
165 init_default_image_type ();
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000166 /* Init Davinci UBL support */
167 init_ubl_image_type();
Stefano Babic4962e382011-10-17 00:07:43 +0000168 /* Init Davinci AIS support */
169 init_ais_image_type();
wdenk5b1d7132002-11-03 00:07:02 +0000170
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530171 params.cmdname = *argv;
172 params.addr = params.ep = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000173
174 while (--argc > 0 && **++argv == '-') {
175 while (*++*argv) {
176 switch (**argv) {
177 case 'l':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530178 params.lflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000179 break;
180 case 'A':
181 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530182 (params.arch =
183 genimg_get_arch_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000184 usage ();
185 goto NXTARG;
Simon Glass4f610422013-06-13 15:10:06 -0700186 case 'c':
187 if (--argc <= 0)
188 usage();
189 params.comment = *++argv;
190 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000191 case 'C':
192 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530193 (params.comp =
194 genimg_get_comp_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000195 usage ();
196 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100197 case 'D':
198 if (--argc <= 0)
199 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530200 params.dtc = *++argv;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100201 goto NXTARG;
202
wdenk5b1d7132002-11-03 00:07:02 +0000203 case 'O':
204 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530205 (params.os =
206 genimg_get_os_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000207 usage ();
208 goto NXTARG;
209 case 'T':
210 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530211 (params.type =
212 genimg_get_type_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000213 usage ();
214 goto NXTARG;
215
216 case 'a':
217 if (--argc <= 0)
218 usage ();
Peter Tysera2513e22010-04-04 22:36:03 -0500219 params.addr = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000220 if (*ptr) {
221 fprintf (stderr,
222 "%s: invalid load address %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530223 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000224 exit (EXIT_FAILURE);
225 }
226 goto NXTARG;
227 case 'd':
228 if (--argc <= 0)
229 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530230 params.datafile = *++argv;
231 params.dflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000232 goto NXTARG;
233 case 'e':
234 if (--argc <= 0)
235 usage ();
Peter Tysera2513e22010-04-04 22:36:03 -0500236 params.ep = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000237 if (*ptr) {
238 fprintf (stderr,
239 "%s: invalid entry point %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530240 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000241 exit (EXIT_FAILURE);
242 }
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530243 params.eflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000244 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100245 case 'f':
246 if (--argc <= 0)
247 usage ();
Simon Glass95d77b42013-06-13 15:10:05 -0700248 params.datafile = *++argv;
249 /* no break */
250 case 'F':
Peter Tyser1a99de22009-11-24 16:42:08 -0600251 /*
252 * The flattened image tree (FIT) format
253 * requires a flattened device tree image type
254 */
255 params.type = IH_TYPE_FLATDT;
Peter Tyserfbc1c8f2009-12-06 01:33:24 -0600256 params.fflag = 1;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100257 goto NXTARG;
Simon Glass80e4df82013-06-13 15:10:03 -0700258 case 'k':
259 if (--argc <= 0)
260 usage();
261 params.keydir = *++argv;
262 goto NXTARG;
Simon Glasse29495d2013-06-13 15:10:04 -0700263 case 'K':
264 if (--argc <= 0)
265 usage();
266 params.keydest = *++argv;
267 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000268 case 'n':
269 if (--argc <= 0)
270 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530271 params.imagename = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000272 goto NXTARG;
Shaohui Xie5d898a02012-08-10 02:49:35 +0000273 case 'R':
274 if (--argc <= 0)
275 usage();
276 /*
277 * This entry is for the second configuration
278 * file, if only one is not enough.
279 */
280 params.imagename2 = *++argv;
281 goto NXTARG;
Stefano Babicf0662102011-09-15 23:50:16 +0000282 case 's':
283 params.skipcpy = 1;
284 break;
wdenk5b1d7132002-11-03 00:07:02 +0000285 case 'v':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530286 params.vflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000287 break;
Wolfgang Denk976b38c2011-02-12 10:37:11 +0100288 case 'V':
289 printf("mkimage version %s\n", PLAIN_VERSION);
290 exit(EXIT_SUCCESS);
wdenk5b1d7132002-11-03 00:07:02 +0000291 case 'x':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530292 params.xflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000293 break;
294 default:
295 usage ();
296 }
297 }
298NXTARG: ;
299 }
300
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530301 if (argc != 1)
302 usage ();
wdenk5b1d7132002-11-03 00:07:02 +0000303
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530304 /* set tparams as per input type_id */
305 tparams = mkimage_get_type(params.type);
306 if (tparams == NULL) {
307 fprintf (stderr, "%s: unsupported type %s\n",
308 params.cmdname, genimg_get_type_name(params.type));
309 exit (EXIT_FAILURE);
310 }
311
312 /*
313 * check the passed arguments parameters meets the requirements
314 * as per image type to be generated/listed
315 */
316 if (tparams->check_params)
317 if (tparams->check_params (&params))
318 usage ();
319
320 if (!params.eflag) {
321 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000322 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530323 if (params.xflag)
324 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000325 }
326
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530327 params.imagefile = *argv;
wdenk5b1d7132002-11-03 00:07:02 +0000328
Peter Tyserc81296c2009-11-24 16:42:10 -0600329 if (params.fflag){
330 if (tparams->fflag_handle)
331 /*
332 * in some cases, some additional processing needs
333 * to be done if fflag is defined
334 *
335 * For ex. fit_handle_file for Fit file support
336 */
337 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000338
Peter Tyserc81296c2009-11-24 16:42:10 -0600339 if (retval != EXIT_SUCCESS)
340 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000341 }
342
Peter Tyserc81296c2009-11-24 16:42:10 -0600343 if (params.lflag || params.fflag) {
344 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
345 } else {
346 ifd = open (params.imagefile,
347 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
348 }
349
350 if (ifd < 0) {
351 fprintf (stderr, "%s: Can't open %s: %s\n",
352 params.cmdname, params.imagefile,
353 strerror(errno));
354 exit (EXIT_FAILURE);
355 }
356
357 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000358 /*
359 * list header information of existing image
360 */
361 if (fstat(ifd, &sbuf) < 0) {
362 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530363 params.cmdname, params.imagefile,
364 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000365 exit (EXIT_FAILURE);
366 }
367
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530368 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000369 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530370 "%s: Bad size: \"%s\" is not valid image\n",
371 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000372 exit (EXIT_FAILURE);
373 }
374
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400375 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
376 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000377 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530378 params.cmdname, params.imagefile,
379 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000380 exit (EXIT_FAILURE);
381 }
382
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530383 /*
384 * scan through mkimage registry for all supported image types
385 * and verify the input image file header for match
386 * Print the image information for matched image type
387 * Returns the error code if not matched
388 */
389 retval = mkimage_verify_print_header (ptr, &sbuf);
wdenk5b1d7132002-11-03 00:07:02 +0000390
wdenk5b1d7132002-11-03 00:07:02 +0000391 (void) munmap((void *)ptr, sbuf.st_size);
392 (void) close (ifd);
393
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530394 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000395 }
396
397 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000398 * In case there an header with a variable
399 * length will be added, the corresponding
400 * function is called. This is responsible to
401 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000402 */
Stefano Babicf0662102011-09-15 23:50:16 +0000403 if (tparams->vrec_header)
404 tparams->vrec_header(&params, tparams);
405 else
406 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000407
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530408 if (write(ifd, tparams->hdr, tparams->header_size)
409 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000410 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530411 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000412 exit (EXIT_FAILURE);
413 }
414
Christian Rieschd1be8f92011-12-09 09:47:38 +0000415 if (!params.skipcpy) {
416 if (params.type == IH_TYPE_MULTI ||
417 params.type == IH_TYPE_SCRIPT) {
418 char *file = params.datafile;
419 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000420
Christian Rieschd1be8f92011-12-09 09:47:38 +0000421 for (;;) {
422 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000423
Christian Rieschd1be8f92011-12-09 09:47:38 +0000424 if (file) {
425 if ((sep = strchr(file, ':')) != NULL) {
426 *sep = '\0';
427 }
428
429 if (stat (file, &sbuf) < 0) {
430 fprintf (stderr, "%s: Can't stat %s: %s\n",
431 params.cmdname, file, strerror(errno));
432 exit (EXIT_FAILURE);
433 }
434 size = cpu_to_uimage (sbuf.st_size);
435 } else {
436 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000437 }
438
Christian Rieschd1be8f92011-12-09 09:47:38 +0000439 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
440 fprintf (stderr, "%s: Write error on %s: %s\n",
441 params.cmdname, params.imagefile,
442 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000443 exit (EXIT_FAILURE);
444 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000445
446 if (!file) {
447 break;
448 }
449
450 if (sep) {
451 *sep = ':';
452 file = sep + 1;
453 } else {
454 file = NULL;
455 }
wdenk5b1d7132002-11-03 00:07:02 +0000456 }
457
Christian Rieschd1be8f92011-12-09 09:47:38 +0000458 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000459
Christian Rieschd1be8f92011-12-09 09:47:38 +0000460 for (;;) {
461 char *sep = strchr(file, ':');
462 if (sep) {
463 *sep = '\0';
464 copy_file (ifd, file, 1);
465 *sep++ = ':';
466 file = sep;
467 } else {
468 copy_file (ifd, file, 0);
469 break;
470 }
wdenk5b1d7132002-11-03 00:07:02 +0000471 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000472 } else if (params.type == IH_TYPE_PBLIMAGE) {
473 /* PBL has special Image format, implements its' own */
474 pbl_load_uboot(ifd, &params);
Christian Rieschd1be8f92011-12-09 09:47:38 +0000475 } else {
476 copy_file (ifd, params.datafile, 0);
wdenk5b1d7132002-11-03 00:07:02 +0000477 }
wdenk5b1d7132002-11-03 00:07:02 +0000478 }
479
480 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530481#if defined(_POSIX_SYNCHRONIZED_IO) && \
482 !defined(__sun__) && \
483 !defined(__FreeBSD__) && \
484 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000485 (void) fdatasync (ifd);
486#else
487 (void) fsync (ifd);
488#endif
489
490 if (fstat(ifd, &sbuf) < 0) {
491 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530492 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000493 exit (EXIT_FAILURE);
494 }
495
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400496 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
497 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000498 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530499 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000500 exit (EXIT_FAILURE);
501 }
502
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530503 /* Setup the image header as per input image type*/
504 if (tparams->set_header)
505 tparams->set_header (ptr, &sbuf, ifd, &params);
506 else {
507 fprintf (stderr, "%s: Can't set header for %s: %s\n",
508 params.cmdname, tparams->name, strerror(errno));
509 exit (EXIT_FAILURE);
510 }
wdenk5b1d7132002-11-03 00:07:02 +0000511
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530512 /* Print the image information by processing image header */
513 if (tparams->print_header)
514 tparams->print_header (ptr);
515 else {
516 fprintf (stderr, "%s: Can't print header for %s: %s\n",
517 params.cmdname, tparams->name, strerror(errno));
518 exit (EXIT_FAILURE);
519 }
wdenk5b1d7132002-11-03 00:07:02 +0000520
521 (void) munmap((void *)ptr, sbuf.st_size);
522
523 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530524#if defined(_POSIX_SYNCHRONIZED_IO) && \
525 !defined(__sun__) && \
526 !defined(__FreeBSD__) && \
527 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000528 (void) fdatasync (ifd);
529#else
530 (void) fsync (ifd);
531#endif
532
533 if (close(ifd)) {
534 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530535 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000536 exit (EXIT_FAILURE);
537 }
538
539 exit (EXIT_SUCCESS);
540}
541
542static void
543copy_file (int ifd, const char *datafile, int pad)
544{
545 int dfd;
546 struct stat sbuf;
547 unsigned char *ptr;
548 int tail;
549 int zero = 0;
550 int offset = 0;
551 int size;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530552 struct image_type_params *tparams = mkimage_get_type (params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000553
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530554 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000555 fprintf (stderr, "Adding Image %s\n", datafile);
556 }
557
wdenkef1464c2003-10-08 22:14:02 +0000558 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000559 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530560 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000561 exit (EXIT_FAILURE);
562 }
563
564 if (fstat(dfd, &sbuf) < 0) {
565 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530566 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000567 exit (EXIT_FAILURE);
568 }
569
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400570 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
571 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000572 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530573 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000574 exit (EXIT_FAILURE);
575 }
576
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530577 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000578 unsigned char *p = NULL;
579 /*
580 * XIP: do not append the image_header_t at the
581 * beginning of the file, but consume the space
582 * reserved for it.
583 */
584
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530585 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000586 fprintf (stderr,
587 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530588 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000589 exit (EXIT_FAILURE);
590 }
591
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530592 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000593 if ( *p != 0xff ) {
594 fprintf (stderr,
595 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530596 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000597 exit (EXIT_FAILURE);
598 }
599 }
600
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530601 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000602 }
603
604 size = sbuf.st_size - offset;
605 if (write(ifd, ptr + offset, size) != size) {
606 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530607 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000608 exit (EXIT_FAILURE);
609 }
610
611 if (pad && ((tail = size % 4) != 0)) {
612
613 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
614 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530615 params.cmdname, params.imagefile,
616 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000617 exit (EXIT_FAILURE);
618 }
619 }
620
621 (void) munmap((void *)ptr, sbuf.st_size);
622 (void) close (dfd);
623}
624
625void
626usage ()
627{
628 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100629 " -l ==> list image header information\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530630 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100631 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
632 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
633 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000634 " -O ==> set operating system to 'os'\n"
635 " -T ==> set image type to 'type'\n"
636 " -C ==> set compression type 'comp'\n"
637 " -a ==> set load address to 'addr' (hex)\n"
638 " -e ==> set entry point to 'ep' (hex)\n"
639 " -n ==> set image name to 'name'\n"
640 " -d ==> use image data from 'datafile'\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100641 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530642 params.cmdname);
Simon Glass95d77b42013-06-13 15:10:05 -0700643 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530644 params.cmdname);
Simon Glass80e4df82013-06-13 15:10:03 -0700645 fprintf(stderr, " -D => set options for device tree compiler\n"
646 " -f => input filename for FIT source\n");
647#ifdef CONFIG_FIT_SIGNATURE
Simon Glass4f610422013-06-13 15:10:06 -0700648 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>]\n"
Simon Glasse29495d2013-06-13 15:10:04 -0700649 " -k => set directory containing private keys\n"
Simon Glass95d77b42013-06-13 15:10:05 -0700650 " -K => write public keys to this .dtb file\n"
Simon Glass4f610422013-06-13 15:10:06 -0700651 " -c => add comment in signature node\n"
Simon Glass95d77b42013-06-13 15:10:05 -0700652 " -F => re-sign existing FIT image\n");
Simon Glass80e4df82013-06-13 15:10:03 -0700653#else
654 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
655#endif
Wolfgang Denk976b38c2011-02-12 10:37:11 +0100656 fprintf (stderr, " %s -V ==> print version information and exit\n",
657 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100658
wdenk5b1d7132002-11-03 00:07:02 +0000659 exit (EXIT_FAILURE);
660}