blob: 7215d0f2ea97b6b4a0557c2a7d64672524142e7a [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 ();
159 /* Init Default image generation/list support */
160 init_default_image_type ();
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000161 /* Init Davinci UBL support */
162 init_ubl_image_type();
wdenk5b1d7132002-11-03 00:07:02 +0000163
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530164 params.cmdname = *argv;
165 params.addr = params.ep = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000166
167 while (--argc > 0 && **++argv == '-') {
168 while (*++*argv) {
169 switch (**argv) {
170 case 'l':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530171 params.lflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000172 break;
173 case 'A':
174 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530175 (params.arch =
176 genimg_get_arch_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000177 usage ();
178 goto NXTARG;
179 case 'C':
180 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530181 (params.comp =
182 genimg_get_comp_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000183 usage ();
184 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100185 case 'D':
186 if (--argc <= 0)
187 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530188 params.dtc = *++argv;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100189 goto NXTARG;
190
wdenk5b1d7132002-11-03 00:07:02 +0000191 case 'O':
192 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530193 (params.os =
194 genimg_get_os_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000195 usage ();
196 goto NXTARG;
197 case 'T':
198 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530199 (params.type =
200 genimg_get_type_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000201 usage ();
202 goto NXTARG;
203
204 case 'a':
205 if (--argc <= 0)
206 usage ();
Peter Tysera2513e22010-04-04 22:36:03 -0500207 params.addr = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000208 if (*ptr) {
209 fprintf (stderr,
210 "%s: invalid load address %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530211 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000212 exit (EXIT_FAILURE);
213 }
214 goto NXTARG;
215 case 'd':
216 if (--argc <= 0)
217 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530218 params.datafile = *++argv;
219 params.dflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000220 goto NXTARG;
221 case 'e':
222 if (--argc <= 0)
223 usage ();
Peter Tysera2513e22010-04-04 22:36:03 -0500224 params.ep = strtoul (*++argv, &ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000225 if (*ptr) {
226 fprintf (stderr,
227 "%s: invalid entry point %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530228 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000229 exit (EXIT_FAILURE);
230 }
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530231 params.eflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000232 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100233 case 'f':
234 if (--argc <= 0)
235 usage ();
Peter Tyser1a99de22009-11-24 16:42:08 -0600236 /*
237 * The flattened image tree (FIT) format
238 * requires a flattened device tree image type
239 */
240 params.type = IH_TYPE_FLATDT;
Peter Tyserfbc1c8f2009-12-06 01:33:24 -0600241 params.datafile = *++argv;
242 params.fflag = 1;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100243 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000244 case 'n':
245 if (--argc <= 0)
246 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530247 params.imagename = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000248 goto NXTARG;
249 case 'v':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530250 params.vflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000251 break;
Wolfgang Denk976b38c2011-02-12 10:37:11 +0100252 case 'V':
253 printf("mkimage version %s\n", PLAIN_VERSION);
254 exit(EXIT_SUCCESS);
wdenk5b1d7132002-11-03 00:07:02 +0000255 case 'x':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530256 params.xflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000257 break;
258 default:
259 usage ();
260 }
261 }
262NXTARG: ;
263 }
264
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530265 if (argc != 1)
266 usage ();
wdenk5b1d7132002-11-03 00:07:02 +0000267
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530268 /* set tparams as per input type_id */
269 tparams = mkimage_get_type(params.type);
270 if (tparams == NULL) {
271 fprintf (stderr, "%s: unsupported type %s\n",
272 params.cmdname, genimg_get_type_name(params.type));
273 exit (EXIT_FAILURE);
274 }
275
276 /*
277 * check the passed arguments parameters meets the requirements
278 * as per image type to be generated/listed
279 */
280 if (tparams->check_params)
281 if (tparams->check_params (&params))
282 usage ();
283
284 if (!params.eflag) {
285 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000286 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530287 if (params.xflag)
288 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000289 }
290
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530291 params.imagefile = *argv;
wdenk5b1d7132002-11-03 00:07:02 +0000292
Peter Tyserc81296c2009-11-24 16:42:10 -0600293 if (params.fflag){
294 if (tparams->fflag_handle)
295 /*
296 * in some cases, some additional processing needs
297 * to be done if fflag is defined
298 *
299 * For ex. fit_handle_file for Fit file support
300 */
301 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000302
Peter Tyserc81296c2009-11-24 16:42:10 -0600303 if (retval != EXIT_SUCCESS)
304 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000305 }
306
Peter Tyserc81296c2009-11-24 16:42:10 -0600307 if (params.lflag || params.fflag) {
308 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
309 } else {
310 ifd = open (params.imagefile,
311 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
312 }
313
314 if (ifd < 0) {
315 fprintf (stderr, "%s: Can't open %s: %s\n",
316 params.cmdname, params.imagefile,
317 strerror(errno));
318 exit (EXIT_FAILURE);
319 }
320
321 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000322 /*
323 * list header information of existing image
324 */
325 if (fstat(ifd, &sbuf) < 0) {
326 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530327 params.cmdname, params.imagefile,
328 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000329 exit (EXIT_FAILURE);
330 }
331
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530332 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000333 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530334 "%s: Bad size: \"%s\" is not valid image\n",
335 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000336 exit (EXIT_FAILURE);
337 }
338
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400339 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
340 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000341 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530342 params.cmdname, params.imagefile,
343 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000344 exit (EXIT_FAILURE);
345 }
346
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530347 /*
348 * scan through mkimage registry for all supported image types
349 * and verify the input image file header for match
350 * Print the image information for matched image type
351 * Returns the error code if not matched
352 */
353 retval = mkimage_verify_print_header (ptr, &sbuf);
wdenk5b1d7132002-11-03 00:07:02 +0000354
wdenk5b1d7132002-11-03 00:07:02 +0000355 (void) munmap((void *)ptr, sbuf.st_size);
356 (void) close (ifd);
357
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530358 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000359 }
360
361 /*
362 * Must be -w then:
363 *
364 * write dummy header, to be fixed later
365 */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530366 memset (tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000367
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530368 if (write(ifd, tparams->hdr, tparams->header_size)
369 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000370 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530371 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000372 exit (EXIT_FAILURE);
373 }
374
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530375 if (params.type == IH_TYPE_MULTI || params.type == IH_TYPE_SCRIPT) {
376 char *file = params.datafile;
Wolfgang Denk3bb66802006-01-11 13:03:54 +0100377 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000378
379 for (;;) {
380 char *sep = NULL;
381
382 if (file) {
383 if ((sep = strchr(file, ':')) != NULL) {
384 *sep = '\0';
385 }
386
387 if (stat (file, &sbuf) < 0) {
388 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530389 params.cmdname, file, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000390 exit (EXIT_FAILURE);
391 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100392 size = cpu_to_uimage (sbuf.st_size);
wdenk5b1d7132002-11-03 00:07:02 +0000393 } else {
394 size = 0;
395 }
396
397 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
398 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530399 params.cmdname, params.imagefile,
400 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000401 exit (EXIT_FAILURE);
402 }
403
404 if (!file) {
405 break;
406 }
407
408 if (sep) {
409 *sep = ':';
410 file = sep + 1;
411 } else {
412 file = NULL;
413 }
414 }
415
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530416 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000417
418 for (;;) {
419 char *sep = strchr(file, ':');
420 if (sep) {
421 *sep = '\0';
422 copy_file (ifd, file, 1);
423 *sep++ = ':';
424 file = sep;
425 } else {
426 copy_file (ifd, file, 0);
427 break;
428 }
429 }
430 } else {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530431 copy_file (ifd, params.datafile, 0);
wdenk5b1d7132002-11-03 00:07:02 +0000432 }
433
434 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530435#if defined(_POSIX_SYNCHRONIZED_IO) && \
436 !defined(__sun__) && \
437 !defined(__FreeBSD__) && \
438 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000439 (void) fdatasync (ifd);
440#else
441 (void) fsync (ifd);
442#endif
443
444 if (fstat(ifd, &sbuf) < 0) {
445 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530446 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000447 exit (EXIT_FAILURE);
448 }
449
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400450 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
451 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000452 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530453 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000454 exit (EXIT_FAILURE);
455 }
456
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530457 /* Setup the image header as per input image type*/
458 if (tparams->set_header)
459 tparams->set_header (ptr, &sbuf, ifd, &params);
460 else {
461 fprintf (stderr, "%s: Can't set header for %s: %s\n",
462 params.cmdname, tparams->name, strerror(errno));
463 exit (EXIT_FAILURE);
464 }
wdenk5b1d7132002-11-03 00:07:02 +0000465
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530466 /* Print the image information by processing image header */
467 if (tparams->print_header)
468 tparams->print_header (ptr);
469 else {
470 fprintf (stderr, "%s: Can't print header for %s: %s\n",
471 params.cmdname, tparams->name, strerror(errno));
472 exit (EXIT_FAILURE);
473 }
wdenk5b1d7132002-11-03 00:07:02 +0000474
475 (void) munmap((void *)ptr, sbuf.st_size);
476
477 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530478#if defined(_POSIX_SYNCHRONIZED_IO) && \
479 !defined(__sun__) && \
480 !defined(__FreeBSD__) && \
481 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000482 (void) fdatasync (ifd);
483#else
484 (void) fsync (ifd);
485#endif
486
487 if (close(ifd)) {
488 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530489 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000490 exit (EXIT_FAILURE);
491 }
492
493 exit (EXIT_SUCCESS);
494}
495
496static void
497copy_file (int ifd, const char *datafile, int pad)
498{
499 int dfd;
500 struct stat sbuf;
501 unsigned char *ptr;
502 int tail;
503 int zero = 0;
504 int offset = 0;
505 int size;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530506 struct image_type_params *tparams = mkimage_get_type (params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000507
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530508 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000509 fprintf (stderr, "Adding Image %s\n", datafile);
510 }
511
wdenkef1464c2003-10-08 22:14:02 +0000512 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000513 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530514 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000515 exit (EXIT_FAILURE);
516 }
517
518 if (fstat(dfd, &sbuf) < 0) {
519 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530520 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000521 exit (EXIT_FAILURE);
522 }
523
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400524 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
525 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000526 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530527 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000528 exit (EXIT_FAILURE);
529 }
530
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530531 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000532 unsigned char *p = NULL;
533 /*
534 * XIP: do not append the image_header_t at the
535 * beginning of the file, but consume the space
536 * reserved for it.
537 */
538
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530539 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000540 fprintf (stderr,
541 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530542 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000543 exit (EXIT_FAILURE);
544 }
545
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530546 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000547 if ( *p != 0xff ) {
548 fprintf (stderr,
549 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530550 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000551 exit (EXIT_FAILURE);
552 }
553 }
554
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530555 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000556 }
557
558 size = sbuf.st_size - offset;
559 if (write(ifd, ptr + offset, size) != size) {
560 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530561 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000562 exit (EXIT_FAILURE);
563 }
564
565 if (pad && ((tail = size % 4) != 0)) {
566
567 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
568 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530569 params.cmdname, params.imagefile,
570 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000571 exit (EXIT_FAILURE);
572 }
573 }
574
575 (void) munmap((void *)ptr, sbuf.st_size);
576 (void) close (dfd);
577}
578
579void
580usage ()
581{
582 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100583 " -l ==> list image header information\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530584 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100585 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
586 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
587 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000588 " -O ==> set operating system to 'os'\n"
589 " -T ==> set image type to 'type'\n"
590 " -C ==> set compression type 'comp'\n"
591 " -a ==> set load address to 'addr' (hex)\n"
592 " -e ==> set entry point to 'ep' (hex)\n"
593 " -n ==> set image name to 'name'\n"
594 " -d ==> use image data from 'datafile'\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100595 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530596 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100597 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530598 params.cmdname);
Wolfgang Denk976b38c2011-02-12 10:37:11 +0100599 fprintf (stderr, " %s -V ==> print version information and exit\n",
600 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100601
wdenk5b1d7132002-11-03 00:07:02 +0000602 exit (EXIT_FAILURE);
603}