blob: cf4b7546de354ee1c29ab0ca3e8e84c5b3a90438 [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +01002 * (C) Copyright 2008 Semihalf
3 *
Prafulla Wadaskarfabf3cf2009-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
wdenkdb82c8e2004-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 Balakowicz41d71ed2008-01-08 18:14:09 +010024#include "mkimage.h"
wdenk5b1d7132002-11-03 00:07:02 +000025#include <image.h>
26
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053027static void copy_file(int, const char *, int);
28static void usage(void);
wdenk5b1d7132002-11-03 00:07:02 +000029
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053030/* image_type_params link list to maintain registered image type supports */
31struct image_type_params *mkimage_tparams = NULL;
wdenk5b1d7132002-11-03 00:07:02 +000032
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053033/* parameters initialized by core will be used by the image type code */
34struct mkimage_params params = {
35 .os = IH_OS_LINUX,
36 .arch = IH_ARCH_PPC,
37 .type = IH_TYPE_KERNEL,
38 .comp = IH_COMP_GZIP,
39 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
40};
wdenk5b1d7132002-11-03 00:07:02 +000041
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053042/*
43 * mkimage_register -
44 *
45 * It is used to register respective image generation/list support to the
46 * mkimage core
47 *
48 * the input struct image_type_params is checked and appended to the link
49 * list, if the input structure is already registered, error
50 */
51void mkimage_register (struct image_type_params *tparams)
52{
53 struct image_type_params **tp;
54
55 if (!tparams) {
56 fprintf (stderr, "%s: %s: Null input\n",
57 params.cmdname, __FUNCTION__);
58 exit (EXIT_FAILURE);
59 }
60
61 /* scan the linked list, check for registry and point the last one */
62 for (tp = &mkimage_tparams; *tp != NULL; tp = &(*tp)->next) {
63 if (!strcmp((*tp)->name, tparams->name)) {
64 fprintf (stderr, "%s: %s already registered\n",
65 params.cmdname, tparams->name);
66 return;
67 }
68 }
69
70 /* add input struct entry at the end of link list */
71 *tp = tparams;
72 /* mark input entry as last entry in the link list */
73 tparams->next = NULL;
74
75 debug ("Registered %s\n", tparams->name);
76}
77
78/*
79 * mkimage_get_type -
80 *
81 * It scans all registers image type supports
82 * checks the input type_id for each supported image type
83 *
84 * if successful,
85 * returns respective image_type_params pointer if success
86 * if input type_id is not supported by any of image_type_support
87 * returns NULL
88 */
89struct image_type_params *mkimage_get_type(int type)
90{
91 struct image_type_params *curr;
92
93 for (curr = mkimage_tparams; curr != NULL; curr = curr->next) {
94 if (curr->check_image_type) {
95 if (!curr->check_image_type (type))
96 return curr;
97 }
98 }
99 return NULL;
100}
101
102/*
103 * mkimage_verify_print_header -
104 *
105 * It scans mkimage_tparams link list,
106 * verifies image_header for each supported image type
107 * if verification is successful, prints respective header
108 *
109 * returns negative if input image format does not match with any of
110 * supported image types
111 */
112int mkimage_verify_print_header (void *ptr, struct stat *sbuf)
113{
114 int retval = -1;
115 struct image_type_params *curr;
116
117 for (curr = mkimage_tparams; curr != NULL; curr = curr->next ) {
118 if (curr->verify_header) {
119 retval = curr->verify_header (
120 (unsigned char *)ptr, sbuf->st_size,
121 &params);
122
123 if (retval == 0) {
124 /*
125 * Print the image information
126 * if verify is successful
127 */
128 if (curr->print_header)
129 curr->print_header (ptr);
130 else {
131 fprintf (stderr,
132 "%s: print_header undefined for %s\n",
133 params.cmdname, curr->name);
134 }
135 break;
136 }
137 }
138 }
139 return retval;
140}
wdenk5b1d7132002-11-03 00:07:02 +0000141
142int
143main (int argc, char **argv)
144{
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100145 int ifd = -1;
wdenk5b1d7132002-11-03 00:07:02 +0000146 struct stat sbuf;
147 unsigned char *ptr;
Prafulla Wadaskar4d4d67d2009-08-16 05:28:19 +0530148 int retval = 0;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530149 struct image_type_params *tparams = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000150
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530151 /* Init Kirkwood Boot image generation/list support */
152 init_kwb_image_type ();
Stefano Babic7b07f092010-01-20 18:19:10 +0100153 /* Init Freescale imx Boot image generation/list support */
154 init_imx_image_type ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530155 /* Init FIT image generation/list support */
156 init_fit_image_type ();
157 /* Init Default image generation/list support */
158 init_default_image_type ();
wdenk5b1d7132002-11-03 00:07:02 +0000159
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530160 params.cmdname = *argv;
161 params.addr = params.ep = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000162
163 while (--argc > 0 && **++argv == '-') {
164 while (*++*argv) {
165 switch (**argv) {
166 case 'l':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530167 params.lflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000168 break;
169 case 'A':
170 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530171 (params.arch =
172 genimg_get_arch_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000173 usage ();
174 goto NXTARG;
175 case 'C':
176 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530177 (params.comp =
178 genimg_get_comp_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000179 usage ();
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100180 goto NXTARG;
181 case 'D':
182 if (--argc <= 0)
183 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530184 params.dtc = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000185 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100186
wdenk5b1d7132002-11-03 00:07:02 +0000187 case 'O':
188 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530189 (params.os =
190 genimg_get_os_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000191 usage ();
192 goto NXTARG;
193 case 'T':
194 if ((--argc <= 0) ||
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530195 (params.type =
196 genimg_get_type_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000197 usage ();
198 goto NXTARG;
199
200 case 'a':
201 if (--argc <= 0)
202 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530203 params.addr = strtoul (*++argv,
204 (char **)&ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000205 if (*ptr) {
206 fprintf (stderr,
207 "%s: invalid load address %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530208 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000209 exit (EXIT_FAILURE);
210 }
211 goto NXTARG;
212 case 'd':
213 if (--argc <= 0)
214 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530215 params.datafile = *++argv;
216 params.dflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000217 goto NXTARG;
218 case 'e':
219 if (--argc <= 0)
220 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530221 params.ep = strtoul (*++argv,
222 (char **)&ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000223 if (*ptr) {
224 fprintf (stderr,
225 "%s: invalid entry point %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530226 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000227 exit (EXIT_FAILURE);
228 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530229 params.eflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000230 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100231 case 'f':
232 if (--argc <= 0)
233 usage ();
Peter Tyser94734e62009-11-24 16:42:08 -0600234 /*
235 * The flattened image tree (FIT) format
236 * requires a flattened device tree image type
237 */
238 params.type = IH_TYPE_FLATDT;
Peter Tyser54cd5392009-12-06 01:33:24 -0600239 params.datafile = *++argv;
240 params.fflag = 1;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100241 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000242 case 'n':
243 if (--argc <= 0)
244 usage ();
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530245 params.imagename = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000246 goto NXTARG;
247 case 'v':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530248 params.vflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000249 break;
250 case 'x':
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530251 params.xflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000252 break;
253 default:
254 usage ();
255 }
256 }
257NXTARG: ;
258 }
259
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530260 if (argc != 1)
261 usage ();
262
263 /* set tparams as per input type_id */
264 tparams = mkimage_get_type(params.type);
265 if (tparams == NULL) {
266 fprintf (stderr, "%s: unsupported type %s\n",
267 params.cmdname, genimg_get_type_name(params.type));
268 exit (EXIT_FAILURE);
269 }
270
271 /*
272 * check the passed arguments parameters meets the requirements
273 * as per image type to be generated/listed
274 */
275 if (tparams->check_params)
276 if (tparams->check_params (&params))
277 usage ();
wdenk5b1d7132002-11-03 00:07:02 +0000278
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530279 if (!params.eflag) {
280 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000281 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530282 if (params.xflag)
283 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000284 }
285
286 /*
287 * If XIP, ensure the entry point is equal to the load address plus
288 * the size of the U-Boot header.
289 */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530290 if (params.xflag) {
291 if (params.ep != params.addr + tparams->header_size) {
Wolfgang Denkdce59c52006-04-28 21:24:32 +0200292 fprintf (stderr,
293 "%s: For XIP, the entry point must be the load addr + %lu\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530294 params.cmdname,
295 (unsigned long)tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000296 exit (EXIT_FAILURE);
297 }
298 }
299
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530300 params.imagefile = *argv;
wdenk5b1d7132002-11-03 00:07:02 +0000301
Peter Tyserb2f8b942009-11-24 16:42:10 -0600302 if (params.fflag){
303 if (tparams->fflag_handle)
304 /*
305 * in some cases, some additional processing needs
306 * to be done if fflag is defined
307 *
308 * For ex. fit_handle_file for Fit file support
309 */
310 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000311
Peter Tyserb2f8b942009-11-24 16:42:10 -0600312 if (retval != EXIT_SUCCESS)
313 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000314 }
315
Peter Tyserb2f8b942009-11-24 16:42:10 -0600316 if (params.lflag || params.fflag) {
317 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
318 } else {
319 ifd = open (params.imagefile,
320 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
321 }
322
323 if (ifd < 0) {
324 fprintf (stderr, "%s: Can't open %s: %s\n",
325 params.cmdname, params.imagefile,
326 strerror(errno));
327 exit (EXIT_FAILURE);
328 }
329
330 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000331 /*
332 * list header information of existing image
333 */
334 if (fstat(ifd, &sbuf) < 0) {
335 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530336 params.cmdname, params.imagefile,
337 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000338 exit (EXIT_FAILURE);
339 }
340
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530341 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000342 fprintf (stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530343 "%s: Bad size: \"%s\" is not valid image\n",
344 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000345 exit (EXIT_FAILURE);
346 }
347
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400348 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
349 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000350 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-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 Wadaskarfabf3cf2009-08-19 17:36:46 +0530356 /*
357 * scan through mkimage registry for all supported image types
358 * and verify the input image file header for match
359 * Print the image information for matched image type
360 * Returns the error code if not matched
361 */
362 retval = mkimage_verify_print_header (ptr, &sbuf);
wdenk5b1d7132002-11-03 00:07:02 +0000363
wdenk5b1d7132002-11-03 00:07:02 +0000364 (void) munmap((void *)ptr, sbuf.st_size);
365 (void) close (ifd);
366
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530367 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000368 }
369
370 /*
371 * Must be -w then:
372 *
373 * write dummy header, to be fixed later
374 */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530375 memset (tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000376
Prafulla Wadaskarfabf3cf2009-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 Wadaskarfabf3cf2009-08-19 17:36:46 +0530380 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000381 exit (EXIT_FAILURE);
382 }
383
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530384 if (params.type == IH_TYPE_MULTI || params.type == IH_TYPE_SCRIPT) {
385 char *file = params.datafile;
Wolfgang Denk31f14c22006-01-11 13:03:54 +0100386 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000387
388 for (;;) {
389 char *sep = NULL;
390
391 if (file) {
392 if ((sep = strchr(file, ':')) != NULL) {
393 *sep = '\0';
394 }
395
396 if (stat (file, &sbuf) < 0) {
397 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530398 params.cmdname, file, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000399 exit (EXIT_FAILURE);
400 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100401 size = cpu_to_uimage (sbuf.st_size);
wdenk5b1d7132002-11-03 00:07:02 +0000402 } else {
403 size = 0;
404 }
405
406 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
407 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530408 params.cmdname, params.imagefile,
409 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000410 exit (EXIT_FAILURE);
411 }
412
413 if (!file) {
414 break;
415 }
416
417 if (sep) {
418 *sep = ':';
419 file = sep + 1;
420 } else {
421 file = NULL;
422 }
423 }
424
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530425 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000426
427 for (;;) {
428 char *sep = strchr(file, ':');
429 if (sep) {
430 *sep = '\0';
431 copy_file (ifd, file, 1);
432 *sep++ = ':';
433 file = sep;
434 } else {
435 copy_file (ifd, file, 0);
436 break;
437 }
438 }
439 } else {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530440 copy_file (ifd, params.datafile, 0);
wdenk5b1d7132002-11-03 00:07:02 +0000441 }
442
443 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530444#if defined(_POSIX_SYNCHRONIZED_IO) && \
445 !defined(__sun__) && \
446 !defined(__FreeBSD__) && \
447 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000448 (void) fdatasync (ifd);
449#else
450 (void) fsync (ifd);
451#endif
452
453 if (fstat(ifd, &sbuf) < 0) {
454 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530455 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000456 exit (EXIT_FAILURE);
457 }
458
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400459 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
460 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000461 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530462 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000463 exit (EXIT_FAILURE);
464 }
465
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530466 /* Setup the image header as per input image type*/
467 if (tparams->set_header)
468 tparams->set_header (ptr, &sbuf, ifd, &params);
469 else {
470 fprintf (stderr, "%s: Can't set header for %s: %s\n",
471 params.cmdname, tparams->name, strerror(errno));
472 exit (EXIT_FAILURE);
473 }
wdenk5b1d7132002-11-03 00:07:02 +0000474
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530475 /* Print the image information by processing image header */
476 if (tparams->print_header)
477 tparams->print_header (ptr);
478 else {
479 fprintf (stderr, "%s: Can't print header for %s: %s\n",
480 params.cmdname, tparams->name, strerror(errno));
481 exit (EXIT_FAILURE);
482 }
wdenk5b1d7132002-11-03 00:07:02 +0000483
484 (void) munmap((void *)ptr, sbuf.st_size);
485
486 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530487#if defined(_POSIX_SYNCHRONIZED_IO) && \
488 !defined(__sun__) && \
489 !defined(__FreeBSD__) && \
490 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000491 (void) fdatasync (ifd);
492#else
493 (void) fsync (ifd);
494#endif
495
496 if (close(ifd)) {
497 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530498 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000499 exit (EXIT_FAILURE);
500 }
501
502 exit (EXIT_SUCCESS);
503}
504
505static void
506copy_file (int ifd, const char *datafile, int pad)
507{
508 int dfd;
509 struct stat sbuf;
510 unsigned char *ptr;
511 int tail;
512 int zero = 0;
513 int offset = 0;
514 int size;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530515 struct image_type_params *tparams = mkimage_get_type (params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000516
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530517 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000518 fprintf (stderr, "Adding Image %s\n", datafile);
519 }
520
wdenk1ee550e2003-10-08 22:14:02 +0000521 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000522 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530523 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000524 exit (EXIT_FAILURE);
525 }
526
527 if (fstat(dfd, &sbuf) < 0) {
528 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530529 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000530 exit (EXIT_FAILURE);
531 }
532
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400533 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
534 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000535 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530536 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000537 exit (EXIT_FAILURE);
538 }
539
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530540 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000541 unsigned char *p = NULL;
542 /*
543 * XIP: do not append the image_header_t at the
544 * beginning of the file, but consume the space
545 * reserved for it.
546 */
547
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530548 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000549 fprintf (stderr,
550 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530551 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000552 exit (EXIT_FAILURE);
553 }
554
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530555 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000556 if ( *p != 0xff ) {
557 fprintf (stderr,
558 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530559 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000560 exit (EXIT_FAILURE);
561 }
562 }
563
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530564 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000565 }
566
567 size = sbuf.st_size - offset;
568 if (write(ifd, ptr + offset, size) != size) {
569 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530570 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000571 exit (EXIT_FAILURE);
572 }
573
574 if (pad && ((tail = size % 4) != 0)) {
575
576 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
577 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530578 params.cmdname, params.imagefile,
579 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000580 exit (EXIT_FAILURE);
581 }
582 }
583
584 (void) munmap((void *)ptr, sbuf.st_size);
585 (void) close (dfd);
586}
587
588void
589usage ()
590{
591 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100592 " -l ==> list image header information\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530593 params.cmdname);
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100594 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
595 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
596 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000597 " -O ==> set operating system to 'os'\n"
598 " -T ==> set image type to 'type'\n"
599 " -C ==> set compression type 'comp'\n"
600 " -a ==> set load address to 'addr' (hex)\n"
601 " -e ==> set entry point to 'ep' (hex)\n"
602 " -n ==> set image name to 'name'\n"
603 " -d ==> use image data from 'datafile'\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100604 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530605 params.cmdname);
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100606 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530607 params.cmdname);
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100608
wdenk5b1d7132002-11-03 00:07:02 +0000609 exit (EXIT_FAILURE);
610}