blob: 2f3310175489a2d55894b0d5a2c2252b233262d7 [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 = "",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053042};
wdenk5b1d7132002-11-03 00:07:02 +000043
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053044/*
45 * mkimage_register -
46 *
47 * It is used to register respective image generation/list support to the
48 * mkimage core
49 *
50 * the input struct image_type_params is checked and appended to the link
51 * list, if the input structure is already registered, error
52 */
53void mkimage_register (struct image_type_params *tparams)
54{
55 struct image_type_params **tp;
56
57 if (!tparams) {
58 fprintf (stderr, "%s: %s: Null input\n",
59 params.cmdname, __FUNCTION__);
60 exit (EXIT_FAILURE);
61 }
62
63 /* scan the linked list, check for registry and point the last one */
64 for (tp = &mkimage_tparams; *tp != NULL; tp = &(*tp)->next) {
65 if (!strcmp((*tp)->name, tparams->name)) {
66 fprintf (stderr, "%s: %s already registered\n",
67 params.cmdname, tparams->name);
68 return;
69 }
70 }
71
72 /* add input struct entry at the end of link list */
73 *tp = tparams;
74 /* mark input entry as last entry in the link list */
75 tparams->next = NULL;
76
77 debug ("Registered %s\n", tparams->name);
78}
79
80/*
81 * mkimage_get_type -
82 *
83 * It scans all registers image type supports
84 * checks the input type_id for each supported image type
85 *
86 * if successful,
87 * returns respective image_type_params pointer if success
88 * if input type_id is not supported by any of image_type_support
89 * returns NULL
90 */
91struct image_type_params *mkimage_get_type(int type)
92{
93 struct image_type_params *curr;
94
95 for (curr = mkimage_tparams; curr != NULL; curr = curr->next) {
96 if (curr->check_image_type) {
97 if (!curr->check_image_type (type))
98 return curr;
99 }
100 }
101 return NULL;
102}
103
104/*
105 * mkimage_verify_print_header -
106 *
107 * It scans mkimage_tparams link list,
108 * verifies image_header for each supported image type
109 * if verification is successful, prints respective header
110 *
111 * returns negative if input image format does not match with any of
112 * supported image types
113 */
114int mkimage_verify_print_header (void *ptr, struct stat *sbuf)
115{
116 int retval = -1;
117 struct image_type_params *curr;
118
119 for (curr = mkimage_tparams; curr != NULL; curr = curr->next ) {
120 if (curr->verify_header) {
121 retval = curr->verify_header (
122 (unsigned char *)ptr, sbuf->st_size,
123 &params);
124
125 if (retval == 0) {
126 /*
127 * Print the image information
128 * if verify is successful
129 */
130 if (curr->print_header)
131 curr->print_header (ptr);
132 else {
133 fprintf (stderr,
134 "%s: print_header undefined for %s\n",
135 params.cmdname, curr->name);
136 }
137 break;
138 }
139 }
140 }
141 return retval;
142}
wdenk5b1d7132002-11-03 00:07:02 +0000143
144int
145main (int argc, char **argv)
146{
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100147 int ifd = -1;
wdenk5b1d7132002-11-03 00:07:02 +0000148 struct stat sbuf;
Peter Tysera2513e22010-04-04 22:36:03 -0500149 char *ptr;
Prafulla Wadaskar449609f2009-08-16 05:28:19 +0530150 int retval = 0;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530151 struct image_type_params *tparams = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000152
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530153 /* Init Kirkwood Boot image generation/list support */
154 init_kwb_image_type ();
Stefano Babic8edcde52010-01-20 18:19:10 +0100155 /* Init Freescale imx Boot image generation/list support */
156 init_imx_image_type ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530157 /* Init FIT image generation/list support */
158 init_fit_image_type ();
John Rigby3decb142011-07-21 09:10:30 -0400159 /* Init TI OMAP Boot image generation/list support */
160 init_omap_image_type();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530161 /* Init Default image generation/list support */
162 init_default_image_type ();
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000163 /* Init Davinci UBL support */
164 init_ubl_image_type();
wdenk5b1d7132002-11-03 00:07:02 +0000165
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530166 params.cmdname = *argv;
167 params.addr = params.ep = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000168
169 while (--argc > 0 && **++argv == '-') {
170 while (*++*argv) {
171 switch (**argv) {
172 case 'l':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530173 params.lflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000174 break;
175 case 'A':
176 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530177 (params.arch =
178 genimg_get_arch_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000179 usage ();
180 goto NXTARG;
181 case 'C':
182 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530183 (params.comp =
184 genimg_get_comp_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000185 usage ();
186 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100187 case 'D':
188 if (--argc <= 0)
189 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530190 params.dtc = *++argv;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100191 goto NXTARG;
192
wdenk5b1d7132002-11-03 00:07:02 +0000193 case 'O':
194 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530195 (params.os =
196 genimg_get_os_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000197 usage ();
198 goto NXTARG;
199 case 'T':
200 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530201 (params.type =
202 genimg_get_type_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000203 usage ();
204 goto NXTARG;
205
206 case 'a':
207 if (--argc <= 0)
208 usage ();
Peter Tysera2513e22010-04-04 22:36:03 -0500209 params.addr = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000210 if (*ptr) {
211 fprintf (stderr,
212 "%s: invalid load address %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530213 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000214 exit (EXIT_FAILURE);
215 }
216 goto NXTARG;
217 case 'd':
218 if (--argc <= 0)
219 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530220 params.datafile = *++argv;
221 params.dflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000222 goto NXTARG;
223 case 'e':
224 if (--argc <= 0)
225 usage ();
Peter Tysera2513e22010-04-04 22:36:03 -0500226 params.ep = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000227 if (*ptr) {
228 fprintf (stderr,
229 "%s: invalid entry point %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530230 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000231 exit (EXIT_FAILURE);
232 }
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530233 params.eflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000234 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100235 case 'f':
236 if (--argc <= 0)
237 usage ();
Peter Tyser1a99de22009-11-24 16:42:08 -0600238 /*
239 * The flattened image tree (FIT) format
240 * requires a flattened device tree image type
241 */
242 params.type = IH_TYPE_FLATDT;
Peter Tyserfbc1c8f2009-12-06 01:33:24 -0600243 params.datafile = *++argv;
244 params.fflag = 1;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100245 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000246 case 'n':
247 if (--argc <= 0)
248 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530249 params.imagename = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000250 goto NXTARG;
251 case 'v':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530252 params.vflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000253 break;
Wolfgang Denk976b38c2011-02-12 10:37:11 +0100254 case 'V':
255 printf("mkimage version %s\n", PLAIN_VERSION);
256 exit(EXIT_SUCCESS);
wdenk5b1d7132002-11-03 00:07:02 +0000257 case 'x':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530258 params.xflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000259 break;
260 default:
261 usage ();
262 }
263 }
264NXTARG: ;
265 }
266
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530267 if (argc != 1)
268 usage ();
wdenk5b1d7132002-11-03 00:07:02 +0000269
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530270 /* set tparams as per input type_id */
271 tparams = mkimage_get_type(params.type);
272 if (tparams == NULL) {
273 fprintf (stderr, "%s: unsupported type %s\n",
274 params.cmdname, genimg_get_type_name(params.type));
275 exit (EXIT_FAILURE);
276 }
277
278 /*
279 * check the passed arguments parameters meets the requirements
280 * as per image type to be generated/listed
281 */
282 if (tparams->check_params)
283 if (tparams->check_params (&params))
284 usage ();
285
286 if (!params.eflag) {
287 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000288 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530289 if (params.xflag)
290 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000291 }
292
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530293 params.imagefile = *argv;
wdenk5b1d7132002-11-03 00:07:02 +0000294
Peter Tyserc81296c2009-11-24 16:42:10 -0600295 if (params.fflag){
296 if (tparams->fflag_handle)
297 /*
298 * in some cases, some additional processing needs
299 * to be done if fflag is defined
300 *
301 * For ex. fit_handle_file for Fit file support
302 */
303 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000304
Peter Tyserc81296c2009-11-24 16:42:10 -0600305 if (retval != EXIT_SUCCESS)
306 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000307 }
308
Peter Tyserc81296c2009-11-24 16:42:10 -0600309 if (params.lflag || params.fflag) {
310 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
311 } else {
312 ifd = open (params.imagefile,
313 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
314 }
315
316 if (ifd < 0) {
317 fprintf (stderr, "%s: Can't open %s: %s\n",
318 params.cmdname, params.imagefile,
319 strerror(errno));
320 exit (EXIT_FAILURE);
321 }
322
323 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000324 /*
325 * list header information of existing image
326 */
327 if (fstat(ifd, &sbuf) < 0) {
328 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530329 params.cmdname, params.imagefile,
330 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000331 exit (EXIT_FAILURE);
332 }
333
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530334 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000335 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530336 "%s: Bad size: \"%s\" is not valid image\n",
337 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000338 exit (EXIT_FAILURE);
339 }
340
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400341 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
342 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000343 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530344 params.cmdname, params.imagefile,
345 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000346 exit (EXIT_FAILURE);
347 }
348
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530349 /*
350 * scan through mkimage registry for all supported image types
351 * and verify the input image file header for match
352 * Print the image information for matched image type
353 * Returns the error code if not matched
354 */
355 retval = mkimage_verify_print_header (ptr, &sbuf);
wdenk5b1d7132002-11-03 00:07:02 +0000356
wdenk5b1d7132002-11-03 00:07:02 +0000357 (void) munmap((void *)ptr, sbuf.st_size);
358 (void) close (ifd);
359
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530360 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000361 }
362
363 /*
364 * Must be -w then:
365 *
366 * write dummy header, to be fixed later
367 */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530368 memset (tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000369
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530370 if (write(ifd, tparams->hdr, tparams->header_size)
371 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000372 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530373 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000374 exit (EXIT_FAILURE);
375 }
376
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530377 if (params.type == IH_TYPE_MULTI || params.type == IH_TYPE_SCRIPT) {
378 char *file = params.datafile;
Wolfgang Denk3bb66802006-01-11 13:03:54 +0100379 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000380
381 for (;;) {
382 char *sep = NULL;
383
384 if (file) {
385 if ((sep = strchr(file, ':')) != NULL) {
386 *sep = '\0';
387 }
388
389 if (stat (file, &sbuf) < 0) {
390 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530391 params.cmdname, file, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000392 exit (EXIT_FAILURE);
393 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100394 size = cpu_to_uimage (sbuf.st_size);
wdenk5b1d7132002-11-03 00:07:02 +0000395 } else {
396 size = 0;
397 }
398
399 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
400 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530401 params.cmdname, params.imagefile,
402 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000403 exit (EXIT_FAILURE);
404 }
405
406 if (!file) {
407 break;
408 }
409
410 if (sep) {
411 *sep = ':';
412 file = sep + 1;
413 } else {
414 file = NULL;
415 }
416 }
417
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530418 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000419
420 for (;;) {
421 char *sep = strchr(file, ':');
422 if (sep) {
423 *sep = '\0';
424 copy_file (ifd, file, 1);
425 *sep++ = ':';
426 file = sep;
427 } else {
428 copy_file (ifd, file, 0);
429 break;
430 }
431 }
432 } else {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530433 copy_file (ifd, params.datafile, 0);
wdenk5b1d7132002-11-03 00:07:02 +0000434 }
435
436 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530437#if defined(_POSIX_SYNCHRONIZED_IO) && \
438 !defined(__sun__) && \
439 !defined(__FreeBSD__) && \
440 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000441 (void) fdatasync (ifd);
442#else
443 (void) fsync (ifd);
444#endif
445
446 if (fstat(ifd, &sbuf) < 0) {
447 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530448 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000449 exit (EXIT_FAILURE);
450 }
451
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400452 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
453 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000454 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530455 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000456 exit (EXIT_FAILURE);
457 }
458
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530459 /* Setup the image header as per input image type*/
460 if (tparams->set_header)
461 tparams->set_header (ptr, &sbuf, ifd, &params);
462 else {
463 fprintf (stderr, "%s: Can't set header for %s: %s\n",
464 params.cmdname, tparams->name, strerror(errno));
465 exit (EXIT_FAILURE);
466 }
wdenk5b1d7132002-11-03 00:07:02 +0000467
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530468 /* Print the image information by processing image header */
469 if (tparams->print_header)
470 tparams->print_header (ptr);
471 else {
472 fprintf (stderr, "%s: Can't print header for %s: %s\n",
473 params.cmdname, tparams->name, strerror(errno));
474 exit (EXIT_FAILURE);
475 }
wdenk5b1d7132002-11-03 00:07:02 +0000476
477 (void) munmap((void *)ptr, sbuf.st_size);
478
479 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530480#if defined(_POSIX_SYNCHRONIZED_IO) && \
481 !defined(__sun__) && \
482 !defined(__FreeBSD__) && \
483 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000484 (void) fdatasync (ifd);
485#else
486 (void) fsync (ifd);
487#endif
488
489 if (close(ifd)) {
490 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530491 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000492 exit (EXIT_FAILURE);
493 }
494
495 exit (EXIT_SUCCESS);
496}
497
498static void
499copy_file (int ifd, const char *datafile, int pad)
500{
501 int dfd;
502 struct stat sbuf;
503 unsigned char *ptr;
504 int tail;
505 int zero = 0;
506 int offset = 0;
507 int size;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530508 struct image_type_params *tparams = mkimage_get_type (params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000509
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530510 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000511 fprintf (stderr, "Adding Image %s\n", datafile);
512 }
513
wdenkef1464c2003-10-08 22:14:02 +0000514 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000515 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530516 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000517 exit (EXIT_FAILURE);
518 }
519
520 if (fstat(dfd, &sbuf) < 0) {
521 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530522 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000523 exit (EXIT_FAILURE);
524 }
525
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400526 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
527 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000528 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530529 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000530 exit (EXIT_FAILURE);
531 }
532
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530533 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000534 unsigned char *p = NULL;
535 /*
536 * XIP: do not append the image_header_t at the
537 * beginning of the file, but consume the space
538 * reserved for it.
539 */
540
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530541 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000542 fprintf (stderr,
543 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530544 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000545 exit (EXIT_FAILURE);
546 }
547
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530548 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000549 if ( *p != 0xff ) {
550 fprintf (stderr,
551 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530552 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000553 exit (EXIT_FAILURE);
554 }
555 }
556
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530557 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000558 }
559
560 size = sbuf.st_size - offset;
561 if (write(ifd, ptr + offset, size) != size) {
562 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530563 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000564 exit (EXIT_FAILURE);
565 }
566
567 if (pad && ((tail = size % 4) != 0)) {
568
569 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
570 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530571 params.cmdname, params.imagefile,
572 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000573 exit (EXIT_FAILURE);
574 }
575 }
576
577 (void) munmap((void *)ptr, sbuf.st_size);
578 (void) close (dfd);
579}
580
581void
582usage ()
583{
584 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100585 " -l ==> list image header information\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530586 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100587 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
588 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
589 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000590 " -O ==> set operating system to 'os'\n"
591 " -T ==> set image type to 'type'\n"
592 " -C ==> set compression type 'comp'\n"
593 " -a ==> set load address to 'addr' (hex)\n"
594 " -e ==> set entry point to 'ep' (hex)\n"
595 " -n ==> set image name to 'name'\n"
596 " -d ==> use image data from 'datafile'\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100597 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530598 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100599 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530600 params.cmdname);
Wolfgang Denk976b38c2011-02-12 10:37:11 +0100601 fprintf (stderr, " %s -V ==> print version information and exit\n",
602 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100603
wdenk5b1d7132002-11-03 00:07:02 +0000604 exit (EXIT_FAILURE);
605}