blob: c307a37615fc7e9550cfc59a5b77028086d21ad4 [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;
Stefano Babicf0662102011-09-15 23:50:16 +0000251 case 's':
252 params.skipcpy = 1;
253 break;
wdenk5b1d7132002-11-03 00:07:02 +0000254 case 'v':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530255 params.vflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000256 break;
Wolfgang Denk976b38c2011-02-12 10:37:11 +0100257 case 'V':
258 printf("mkimage version %s\n", PLAIN_VERSION);
259 exit(EXIT_SUCCESS);
wdenk5b1d7132002-11-03 00:07:02 +0000260 case 'x':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530261 params.xflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000262 break;
263 default:
264 usage ();
265 }
266 }
267NXTARG: ;
268 }
269
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530270 if (argc != 1)
271 usage ();
wdenk5b1d7132002-11-03 00:07:02 +0000272
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530273 /* set tparams as per input type_id */
274 tparams = mkimage_get_type(params.type);
275 if (tparams == NULL) {
276 fprintf (stderr, "%s: unsupported type %s\n",
277 params.cmdname, genimg_get_type_name(params.type));
278 exit (EXIT_FAILURE);
279 }
280
281 /*
282 * check the passed arguments parameters meets the requirements
283 * as per image type to be generated/listed
284 */
285 if (tparams->check_params)
286 if (tparams->check_params (&params))
287 usage ();
288
289 if (!params.eflag) {
290 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000291 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530292 if (params.xflag)
293 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000294 }
295
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530296 params.imagefile = *argv;
wdenk5b1d7132002-11-03 00:07:02 +0000297
Peter Tyserc81296c2009-11-24 16:42:10 -0600298 if (params.fflag){
299 if (tparams->fflag_handle)
300 /*
301 * in some cases, some additional processing needs
302 * to be done if fflag is defined
303 *
304 * For ex. fit_handle_file for Fit file support
305 */
306 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000307
Peter Tyserc81296c2009-11-24 16:42:10 -0600308 if (retval != EXIT_SUCCESS)
309 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000310 }
311
Peter Tyserc81296c2009-11-24 16:42:10 -0600312 if (params.lflag || params.fflag) {
313 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
314 } else {
315 ifd = open (params.imagefile,
316 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
317 }
318
319 if (ifd < 0) {
320 fprintf (stderr, "%s: Can't open %s: %s\n",
321 params.cmdname, params.imagefile,
322 strerror(errno));
323 exit (EXIT_FAILURE);
324 }
325
326 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000327 /*
328 * list header information of existing image
329 */
330 if (fstat(ifd, &sbuf) < 0) {
331 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530332 params.cmdname, params.imagefile,
333 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000334 exit (EXIT_FAILURE);
335 }
336
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530337 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000338 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530339 "%s: Bad size: \"%s\" is not valid image\n",
340 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000341 exit (EXIT_FAILURE);
342 }
343
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400344 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
345 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000346 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530347 params.cmdname, params.imagefile,
348 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000349 exit (EXIT_FAILURE);
350 }
351
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530352 /*
353 * scan through mkimage registry for all supported image types
354 * and verify the input image file header for match
355 * Print the image information for matched image type
356 * Returns the error code if not matched
357 */
358 retval = mkimage_verify_print_header (ptr, &sbuf);
wdenk5b1d7132002-11-03 00:07:02 +0000359
wdenk5b1d7132002-11-03 00:07:02 +0000360 (void) munmap((void *)ptr, sbuf.st_size);
361 (void) close (ifd);
362
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530363 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000364 }
365
366 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000367 * In case there an header with a variable
368 * length will be added, the corresponding
369 * function is called. This is responsible to
370 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000371 */
Stefano Babicf0662102011-09-15 23:50:16 +0000372 if (tparams->vrec_header)
373 tparams->vrec_header(&params, tparams);
374 else
375 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000376
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530377 if (write(ifd, tparams->hdr, tparams->header_size)
378 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000379 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530380 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000381 exit (EXIT_FAILURE);
382 }
383
Stefano Babicf0662102011-09-15 23:50:16 +0000384 if (!params.skipcpy &&
385 (params.type == IH_TYPE_MULTI ||
386 params.type == IH_TYPE_SCRIPT)) {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530387 char *file = params.datafile;
Wolfgang Denk3bb66802006-01-11 13:03:54 +0100388 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000389
390 for (;;) {
391 char *sep = NULL;
392
393 if (file) {
394 if ((sep = strchr(file, ':')) != NULL) {
395 *sep = '\0';
396 }
397
398 if (stat (file, &sbuf) < 0) {
399 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530400 params.cmdname, file, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000401 exit (EXIT_FAILURE);
402 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100403 size = cpu_to_uimage (sbuf.st_size);
wdenk5b1d7132002-11-03 00:07:02 +0000404 } else {
405 size = 0;
406 }
407
408 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
409 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530410 params.cmdname, params.imagefile,
411 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000412 exit (EXIT_FAILURE);
413 }
414
415 if (!file) {
416 break;
417 }
418
419 if (sep) {
420 *sep = ':';
421 file = sep + 1;
422 } else {
423 file = NULL;
424 }
425 }
426
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530427 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000428
429 for (;;) {
430 char *sep = strchr(file, ':');
431 if (sep) {
432 *sep = '\0';
433 copy_file (ifd, file, 1);
434 *sep++ = ':';
435 file = sep;
436 } else {
437 copy_file (ifd, file, 0);
438 break;
439 }
440 }
441 } else {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530442 copy_file (ifd, params.datafile, 0);
wdenk5b1d7132002-11-03 00:07:02 +0000443 }
444
445 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530446#if defined(_POSIX_SYNCHRONIZED_IO) && \
447 !defined(__sun__) && \
448 !defined(__FreeBSD__) && \
449 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000450 (void) fdatasync (ifd);
451#else
452 (void) fsync (ifd);
453#endif
454
455 if (fstat(ifd, &sbuf) < 0) {
456 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530457 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000458 exit (EXIT_FAILURE);
459 }
460
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400461 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
462 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000463 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530464 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000465 exit (EXIT_FAILURE);
466 }
467
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530468 /* Setup the image header as per input image type*/
469 if (tparams->set_header)
470 tparams->set_header (ptr, &sbuf, ifd, &params);
471 else {
472 fprintf (stderr, "%s: Can't set header for %s: %s\n",
473 params.cmdname, tparams->name, strerror(errno));
474 exit (EXIT_FAILURE);
475 }
wdenk5b1d7132002-11-03 00:07:02 +0000476
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530477 /* Print the image information by processing image header */
478 if (tparams->print_header)
479 tparams->print_header (ptr);
480 else {
481 fprintf (stderr, "%s: Can't print header for %s: %s\n",
482 params.cmdname, tparams->name, strerror(errno));
483 exit (EXIT_FAILURE);
484 }
wdenk5b1d7132002-11-03 00:07:02 +0000485
486 (void) munmap((void *)ptr, sbuf.st_size);
487
488 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530489#if defined(_POSIX_SYNCHRONIZED_IO) && \
490 !defined(__sun__) && \
491 !defined(__FreeBSD__) && \
492 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000493 (void) fdatasync (ifd);
494#else
495 (void) fsync (ifd);
496#endif
497
498 if (close(ifd)) {
499 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530500 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000501 exit (EXIT_FAILURE);
502 }
503
504 exit (EXIT_SUCCESS);
505}
506
507static void
508copy_file (int ifd, const char *datafile, int pad)
509{
510 int dfd;
511 struct stat sbuf;
512 unsigned char *ptr;
513 int tail;
514 int zero = 0;
515 int offset = 0;
516 int size;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530517 struct image_type_params *tparams = mkimage_get_type (params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000518
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530519 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000520 fprintf (stderr, "Adding Image %s\n", datafile);
521 }
522
wdenkef1464c2003-10-08 22:14:02 +0000523 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000524 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530525 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000526 exit (EXIT_FAILURE);
527 }
528
529 if (fstat(dfd, &sbuf) < 0) {
530 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530531 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000532 exit (EXIT_FAILURE);
533 }
534
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400535 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
536 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000537 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530538 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000539 exit (EXIT_FAILURE);
540 }
541
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530542 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000543 unsigned char *p = NULL;
544 /*
545 * XIP: do not append the image_header_t at the
546 * beginning of the file, but consume the space
547 * reserved for it.
548 */
549
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530550 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000551 fprintf (stderr,
552 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530553 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000554 exit (EXIT_FAILURE);
555 }
556
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530557 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000558 if ( *p != 0xff ) {
559 fprintf (stderr,
560 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530561 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000562 exit (EXIT_FAILURE);
563 }
564 }
565
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530566 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000567 }
568
569 size = sbuf.st_size - offset;
570 if (write(ifd, ptr + offset, size) != size) {
571 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530572 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000573 exit (EXIT_FAILURE);
574 }
575
576 if (pad && ((tail = size % 4) != 0)) {
577
578 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
579 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530580 params.cmdname, params.imagefile,
581 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000582 exit (EXIT_FAILURE);
583 }
584 }
585
586 (void) munmap((void *)ptr, sbuf.st_size);
587 (void) close (dfd);
588}
589
590void
591usage ()
592{
593 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100594 " -l ==> list image header information\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530595 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100596 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
597 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
598 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000599 " -O ==> set operating system to 'os'\n"
600 " -T ==> set image type to 'type'\n"
601 " -C ==> set compression type 'comp'\n"
602 " -a ==> set load address to 'addr' (hex)\n"
603 " -e ==> set entry point to 'ep' (hex)\n"
604 " -n ==> set image name to 'name'\n"
605 " -d ==> use image data from 'datafile'\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100606 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530607 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100608 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530609 params.cmdname);
Wolfgang Denk976b38c2011-02-12 10:37:11 +0100610 fprintf (stderr, " %s -V ==> print version information and exit\n",
611 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100612
wdenk5b1d7132002-11-03 00:07:02 +0000613 exit (EXIT_FAILURE);
614}