blob: def7df250cc72f9dc6719c5fb74a4134ef601528 [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;
186 case 'C':
187 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530188 (params.comp =
189 genimg_get_comp_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000190 usage ();
191 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100192 case 'D':
193 if (--argc <= 0)
194 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530195 params.dtc = *++argv;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100196 goto NXTARG;
197
wdenk5b1d7132002-11-03 00:07:02 +0000198 case 'O':
199 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530200 (params.os =
201 genimg_get_os_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000202 usage ();
203 goto NXTARG;
204 case 'T':
205 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530206 (params.type =
207 genimg_get_type_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000208 usage ();
209 goto NXTARG;
210
211 case 'a':
212 if (--argc <= 0)
213 usage ();
Peter Tysera2513e22010-04-04 22:36:03 -0500214 params.addr = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000215 if (*ptr) {
216 fprintf (stderr,
217 "%s: invalid load address %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530218 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000219 exit (EXIT_FAILURE);
220 }
221 goto NXTARG;
222 case 'd':
223 if (--argc <= 0)
224 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530225 params.datafile = *++argv;
226 params.dflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000227 goto NXTARG;
228 case 'e':
229 if (--argc <= 0)
230 usage ();
Peter Tysera2513e22010-04-04 22:36:03 -0500231 params.ep = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000232 if (*ptr) {
233 fprintf (stderr,
234 "%s: invalid entry point %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530235 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000236 exit (EXIT_FAILURE);
237 }
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530238 params.eflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000239 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100240 case 'f':
241 if (--argc <= 0)
242 usage ();
Peter Tyser1a99de22009-11-24 16:42:08 -0600243 /*
244 * The flattened image tree (FIT) format
245 * requires a flattened device tree image type
246 */
247 params.type = IH_TYPE_FLATDT;
Peter Tyserfbc1c8f2009-12-06 01:33:24 -0600248 params.datafile = *++argv;
249 params.fflag = 1;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100250 goto NXTARG;
Simon Glass80e4df82013-06-13 15:10:03 -0700251 case 'k':
252 if (--argc <= 0)
253 usage();
254 params.keydir = *++argv;
255 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000256 case 'n':
257 if (--argc <= 0)
258 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530259 params.imagename = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000260 goto NXTARG;
Shaohui Xie5d898a02012-08-10 02:49:35 +0000261 case 'R':
262 if (--argc <= 0)
263 usage();
264 /*
265 * This entry is for the second configuration
266 * file, if only one is not enough.
267 */
268 params.imagename2 = *++argv;
269 goto NXTARG;
Stefano Babicf0662102011-09-15 23:50:16 +0000270 case 's':
271 params.skipcpy = 1;
272 break;
wdenk5b1d7132002-11-03 00:07:02 +0000273 case 'v':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530274 params.vflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000275 break;
Wolfgang Denk976b38c2011-02-12 10:37:11 +0100276 case 'V':
277 printf("mkimage version %s\n", PLAIN_VERSION);
278 exit(EXIT_SUCCESS);
wdenk5b1d7132002-11-03 00:07:02 +0000279 case 'x':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530280 params.xflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000281 break;
282 default:
283 usage ();
284 }
285 }
286NXTARG: ;
287 }
288
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530289 if (argc != 1)
290 usage ();
wdenk5b1d7132002-11-03 00:07:02 +0000291
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530292 /* set tparams as per input type_id */
293 tparams = mkimage_get_type(params.type);
294 if (tparams == NULL) {
295 fprintf (stderr, "%s: unsupported type %s\n",
296 params.cmdname, genimg_get_type_name(params.type));
297 exit (EXIT_FAILURE);
298 }
299
300 /*
301 * check the passed arguments parameters meets the requirements
302 * as per image type to be generated/listed
303 */
304 if (tparams->check_params)
305 if (tparams->check_params (&params))
306 usage ();
307
308 if (!params.eflag) {
309 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000310 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530311 if (params.xflag)
312 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000313 }
314
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530315 params.imagefile = *argv;
wdenk5b1d7132002-11-03 00:07:02 +0000316
Peter Tyserc81296c2009-11-24 16:42:10 -0600317 if (params.fflag){
318 if (tparams->fflag_handle)
319 /*
320 * in some cases, some additional processing needs
321 * to be done if fflag is defined
322 *
323 * For ex. fit_handle_file for Fit file support
324 */
325 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000326
Peter Tyserc81296c2009-11-24 16:42:10 -0600327 if (retval != EXIT_SUCCESS)
328 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000329 }
330
Peter Tyserc81296c2009-11-24 16:42:10 -0600331 if (params.lflag || params.fflag) {
332 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
333 } else {
334 ifd = open (params.imagefile,
335 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
336 }
337
338 if (ifd < 0) {
339 fprintf (stderr, "%s: Can't open %s: %s\n",
340 params.cmdname, params.imagefile,
341 strerror(errno));
342 exit (EXIT_FAILURE);
343 }
344
345 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000346 /*
347 * list header information of existing image
348 */
349 if (fstat(ifd, &sbuf) < 0) {
350 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530351 params.cmdname, params.imagefile,
352 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000353 exit (EXIT_FAILURE);
354 }
355
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530356 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000357 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530358 "%s: Bad size: \"%s\" is not valid image\n",
359 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000360 exit (EXIT_FAILURE);
361 }
362
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400363 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
364 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000365 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530366 params.cmdname, params.imagefile,
367 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000368 exit (EXIT_FAILURE);
369 }
370
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530371 /*
372 * scan through mkimage registry for all supported image types
373 * and verify the input image file header for match
374 * Print the image information for matched image type
375 * Returns the error code if not matched
376 */
377 retval = mkimage_verify_print_header (ptr, &sbuf);
wdenk5b1d7132002-11-03 00:07:02 +0000378
wdenk5b1d7132002-11-03 00:07:02 +0000379 (void) munmap((void *)ptr, sbuf.st_size);
380 (void) close (ifd);
381
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530382 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000383 }
384
385 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000386 * In case there an header with a variable
387 * length will be added, the corresponding
388 * function is called. This is responsible to
389 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000390 */
Stefano Babicf0662102011-09-15 23:50:16 +0000391 if (tparams->vrec_header)
392 tparams->vrec_header(&params, tparams);
393 else
394 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000395
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530396 if (write(ifd, tparams->hdr, tparams->header_size)
397 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000398 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530399 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000400 exit (EXIT_FAILURE);
401 }
402
Christian Rieschd1be8f92011-12-09 09:47:38 +0000403 if (!params.skipcpy) {
404 if (params.type == IH_TYPE_MULTI ||
405 params.type == IH_TYPE_SCRIPT) {
406 char *file = params.datafile;
407 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000408
Christian Rieschd1be8f92011-12-09 09:47:38 +0000409 for (;;) {
410 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000411
Christian Rieschd1be8f92011-12-09 09:47:38 +0000412 if (file) {
413 if ((sep = strchr(file, ':')) != NULL) {
414 *sep = '\0';
415 }
416
417 if (stat (file, &sbuf) < 0) {
418 fprintf (stderr, "%s: Can't stat %s: %s\n",
419 params.cmdname, file, strerror(errno));
420 exit (EXIT_FAILURE);
421 }
422 size = cpu_to_uimage (sbuf.st_size);
423 } else {
424 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000425 }
426
Christian Rieschd1be8f92011-12-09 09:47:38 +0000427 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
428 fprintf (stderr, "%s: Write error on %s: %s\n",
429 params.cmdname, params.imagefile,
430 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000431 exit (EXIT_FAILURE);
432 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000433
434 if (!file) {
435 break;
436 }
437
438 if (sep) {
439 *sep = ':';
440 file = sep + 1;
441 } else {
442 file = NULL;
443 }
wdenk5b1d7132002-11-03 00:07:02 +0000444 }
445
Christian Rieschd1be8f92011-12-09 09:47:38 +0000446 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000447
Christian Rieschd1be8f92011-12-09 09:47:38 +0000448 for (;;) {
449 char *sep = strchr(file, ':');
450 if (sep) {
451 *sep = '\0';
452 copy_file (ifd, file, 1);
453 *sep++ = ':';
454 file = sep;
455 } else {
456 copy_file (ifd, file, 0);
457 break;
458 }
wdenk5b1d7132002-11-03 00:07:02 +0000459 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000460 } else if (params.type == IH_TYPE_PBLIMAGE) {
461 /* PBL has special Image format, implements its' own */
462 pbl_load_uboot(ifd, &params);
Christian Rieschd1be8f92011-12-09 09:47:38 +0000463 } else {
464 copy_file (ifd, params.datafile, 0);
wdenk5b1d7132002-11-03 00:07:02 +0000465 }
wdenk5b1d7132002-11-03 00:07:02 +0000466 }
467
468 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530469#if defined(_POSIX_SYNCHRONIZED_IO) && \
470 !defined(__sun__) && \
471 !defined(__FreeBSD__) && \
472 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000473 (void) fdatasync (ifd);
474#else
475 (void) fsync (ifd);
476#endif
477
478 if (fstat(ifd, &sbuf) < 0) {
479 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530480 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000481 exit (EXIT_FAILURE);
482 }
483
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400484 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
485 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000486 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530487 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000488 exit (EXIT_FAILURE);
489 }
490
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530491 /* Setup the image header as per input image type*/
492 if (tparams->set_header)
493 tparams->set_header (ptr, &sbuf, ifd, &params);
494 else {
495 fprintf (stderr, "%s: Can't set header for %s: %s\n",
496 params.cmdname, tparams->name, strerror(errno));
497 exit (EXIT_FAILURE);
498 }
wdenk5b1d7132002-11-03 00:07:02 +0000499
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530500 /* Print the image information by processing image header */
501 if (tparams->print_header)
502 tparams->print_header (ptr);
503 else {
504 fprintf (stderr, "%s: Can't print header for %s: %s\n",
505 params.cmdname, tparams->name, strerror(errno));
506 exit (EXIT_FAILURE);
507 }
wdenk5b1d7132002-11-03 00:07:02 +0000508
509 (void) munmap((void *)ptr, sbuf.st_size);
510
511 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530512#if defined(_POSIX_SYNCHRONIZED_IO) && \
513 !defined(__sun__) && \
514 !defined(__FreeBSD__) && \
515 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000516 (void) fdatasync (ifd);
517#else
518 (void) fsync (ifd);
519#endif
520
521 if (close(ifd)) {
522 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530523 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000524 exit (EXIT_FAILURE);
525 }
526
527 exit (EXIT_SUCCESS);
528}
529
530static void
531copy_file (int ifd, const char *datafile, int pad)
532{
533 int dfd;
534 struct stat sbuf;
535 unsigned char *ptr;
536 int tail;
537 int zero = 0;
538 int offset = 0;
539 int size;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530540 struct image_type_params *tparams = mkimage_get_type (params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000541
Prafulla Wadaskar89a4d6b2009-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
wdenkef1464c2003-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 Wadaskar89a4d6b2009-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 Wadaskar89a4d6b2009-08-19 17:36:46 +0530554 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000555 exit (EXIT_FAILURE);
556 }
557
Mike Frysingerfa956fd2008-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 Wadaskar89a4d6b2009-08-19 17:36:46 +0530561 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000562 exit (EXIT_FAILURE);
563 }
564
Prafulla Wadaskar89a4d6b2009-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 Wadaskar89a4d6b2009-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 Wadaskar89a4d6b2009-08-19 17:36:46 +0530576 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000577 exit (EXIT_FAILURE);
578 }
579
Prafulla Wadaskar89a4d6b2009-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 Wadaskar89a4d6b2009-08-19 17:36:46 +0530584 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000585 exit (EXIT_FAILURE);
586 }
587 }
588
Prafulla Wadaskar89a4d6b2009-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 Wadaskar89a4d6b2009-08-19 17:36:46 +0530595 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000596 exit (EXIT_FAILURE);
597 }
598
599 if (pad && ((tail = size % 4) != 0)) {
600
601 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
602 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530603 params.cmdname, params.imagefile,
604 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000605 exit (EXIT_FAILURE);
606 }
607 }
608
609 (void) munmap((void *)ptr, sbuf.st_size);
610 (void) close (dfd);
611}
612
613void
614usage ()
615{
616 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100617 " -l ==> list image header information\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530618 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100619 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
620 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
621 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000622 " -O ==> set operating system to 'os'\n"
623 " -T ==> set image type to 'type'\n"
624 " -C ==> set compression type 'comp'\n"
625 " -a ==> set load address to 'addr' (hex)\n"
626 " -e ==> set entry point to 'ep' (hex)\n"
627 " -n ==> set image name to 'name'\n"
628 " -d ==> use image data from 'datafile'\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100629 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530630 params.cmdname);
Simon Glass80e4df82013-06-13 15:10:03 -0700631 fprintf(stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530632 params.cmdname);
Simon Glass80e4df82013-06-13 15:10:03 -0700633 fprintf(stderr, " -D => set options for device tree compiler\n"
634 " -f => input filename for FIT source\n");
635#ifdef CONFIG_FIT_SIGNATURE
636 fprintf(stderr, "Signing / verified boot options: [-k keydir]\n"
637 " -k => set directory containing private keys\n");
638#else
639 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
640#endif
Wolfgang Denk976b38c2011-02-12 10:37:11 +0100641 fprintf (stderr, " %s -V ==> print version information and exit\n",
642 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100643
wdenk5b1d7132002-11-03 00:07:02 +0000644 exit (EXIT_FAILURE);
645}