blob: ab6ea32ad05ca282b8ac6ad4dc4af802753d45e3 [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>
26
Prafulla Wadaskar89a4d6b2009-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 Wadaskar89a4d6b2009-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 Wadaskar89a4d6b2009-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 Wadaskar89a4d6b2009-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 Sieka9d254382008-03-11 12:34:47 +0100145 int ifd = -1;
wdenk5b1d7132002-11-03 00:07:02 +0000146 struct stat sbuf;
147 unsigned char *ptr;
Prafulla Wadaskar449609f2009-08-16 05:28:19 +0530148 int retval = 0;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530149 struct image_type_params *tparams = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000150
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530151 /* Init Kirkwood Boot image generation/list support */
152 init_kwb_image_type ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530153 /* Init FIT image generation/list support */
154 init_fit_image_type ();
155 /* Init Default image generation/list support */
156 init_default_image_type ();
wdenk5b1d7132002-11-03 00:07:02 +0000157
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530158 params.cmdname = *argv;
159 params.addr = params.ep = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000160
161 while (--argc > 0 && **++argv == '-') {
162 while (*++*argv) {
163 switch (**argv) {
164 case 'l':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530165 params.lflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000166 break;
167 case 'A':
168 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530169 (params.arch =
170 genimg_get_arch_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000171 usage ();
172 goto NXTARG;
173 case 'C':
174 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530175 (params.comp =
176 genimg_get_comp_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000177 usage ();
178 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100179 case 'D':
180 if (--argc <= 0)
181 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530182 params.dtc = *++argv;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100183 goto NXTARG;
184
wdenk5b1d7132002-11-03 00:07:02 +0000185 case 'O':
186 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530187 (params.os =
188 genimg_get_os_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000189 usage ();
190 goto NXTARG;
191 case 'T':
192 if ((--argc <= 0) ||
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530193 (params.type =
194 genimg_get_type_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000195 usage ();
196 goto NXTARG;
197
198 case 'a':
199 if (--argc <= 0)
200 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530201 params.addr = strtoul (*++argv,
202 (char **)&ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000203 if (*ptr) {
204 fprintf (stderr,
205 "%s: invalid load address %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530206 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000207 exit (EXIT_FAILURE);
208 }
209 goto NXTARG;
210 case 'd':
211 if (--argc <= 0)
212 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530213 params.datafile = *++argv;
214 params.dflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000215 goto NXTARG;
216 case 'e':
217 if (--argc <= 0)
218 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530219 params.ep = strtoul (*++argv,
220 (char **)&ptr, 16);
wdenk5b1d7132002-11-03 00:07:02 +0000221 if (*ptr) {
222 fprintf (stderr,
223 "%s: invalid entry point %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530224 params.cmdname, *argv);
wdenk5b1d7132002-11-03 00:07:02 +0000225 exit (EXIT_FAILURE);
226 }
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530227 params.eflag = 1;
wdenk5b1d7132002-11-03 00:07:02 +0000228 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100229 case 'f':
230 if (--argc <= 0)
231 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530232 params.datafile = *++argv;
233 params.fflag = 1;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100234 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000235 case 'n':
236 if (--argc <= 0)
237 usage ();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530238 params.imagename = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +0000239 goto NXTARG;
240 case 'v':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530241 params.vflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000242 break;
243 case 'x':
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530244 params.xflag++;
wdenk5b1d7132002-11-03 00:07:02 +0000245 break;
246 default:
247 usage ();
248 }
249 }
250NXTARG: ;
251 }
252
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530253 if (argc != 1)
254 usage ();
wdenk5b1d7132002-11-03 00:07:02 +0000255
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530256 /* set tparams as per input type_id */
257 tparams = mkimage_get_type(params.type);
258 if (tparams == NULL) {
259 fprintf (stderr, "%s: unsupported type %s\n",
260 params.cmdname, genimg_get_type_name(params.type));
261 exit (EXIT_FAILURE);
262 }
263
264 /*
265 * check the passed arguments parameters meets the requirements
266 * as per image type to be generated/listed
267 */
268 if (tparams->check_params)
269 if (tparams->check_params (&params))
270 usage ();
271
272 if (!params.eflag) {
273 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000274 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530275 if (params.xflag)
276 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000277 }
278
279 /*
280 * If XIP, ensure the entry point is equal to the load address plus
281 * the size of the U-Boot header.
282 */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530283 if (params.xflag) {
284 if (params.ep != params.addr + tparams->header_size) {
Wolfgang Denk3577d3a2006-04-28 21:24:32 +0200285 fprintf (stderr,
286 "%s: For XIP, the entry point must be the load addr + %lu\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530287 params.cmdname,
288 (unsigned long)tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000289 exit (EXIT_FAILURE);
290 }
291 }
292
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530293 params.imagefile = *argv;
wdenk5b1d7132002-11-03 00:07:02 +0000294
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530295 if (!params.fflag){
296 if (params.lflag) {
297 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100298 } else {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530299 ifd = open (params.imagefile,
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100300 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
301 }
wdenk5b1d7132002-11-03 00:07:02 +0000302
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100303 if (ifd < 0) {
304 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530305 params.cmdname, params.imagefile,
306 strerror(errno));
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100307 exit (EXIT_FAILURE);
308 }
wdenk5b1d7132002-11-03 00:07:02 +0000309 }
310
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530311 if (params.lflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000312 /*
313 * list header information of existing image
314 */
315 if (fstat(ifd, &sbuf) < 0) {
316 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530317 params.cmdname, params.imagefile,
318 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000319 exit (EXIT_FAILURE);
320 }
321
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530322 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000323 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530324 "%s: Bad size: \"%s\" is not valid image\n",
325 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000326 exit (EXIT_FAILURE);
327 }
328
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400329 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
330 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000331 fprintf (stderr, "%s: Can't read %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 /*
338 * scan through mkimage registry for all supported image types
339 * and verify the input image file header for match
340 * Print the image information for matched image type
341 * Returns the error code if not matched
342 */
343 retval = mkimage_verify_print_header (ptr, &sbuf);
wdenk5b1d7132002-11-03 00:07:02 +0000344
wdenk5b1d7132002-11-03 00:07:02 +0000345 (void) munmap((void *)ptr, sbuf.st_size);
346 (void) close (ifd);
347
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530348 exit (retval);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530349 } else if (params.fflag) {
350 if (tparams->fflag_handle)
351 /*
352 * in some cases, some additional processing needs
353 * to be done if fflag is defined
354 *
355 * For ex. fit_handle_file for Fit file support
356 */
357 retval = tparams->fflag_handle(&params);
358
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530359 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000360 }
361
362 /*
363 * Must be -w then:
364 *
365 * write dummy header, to be fixed later
366 */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530367 memset (tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000368
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530369 if (write(ifd, tparams->hdr, tparams->header_size)
370 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000371 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530372 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000373 exit (EXIT_FAILURE);
374 }
375
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530376 if (params.type == IH_TYPE_MULTI || params.type == IH_TYPE_SCRIPT) {
377 char *file = params.datafile;
Wolfgang Denk3bb66802006-01-11 13:03:54 +0100378 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000379
380 for (;;) {
381 char *sep = NULL;
382
383 if (file) {
384 if ((sep = strchr(file, ':')) != NULL) {
385 *sep = '\0';
386 }
387
388 if (stat (file, &sbuf) < 0) {
389 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530390 params.cmdname, file, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000391 exit (EXIT_FAILURE);
392 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100393 size = cpu_to_uimage (sbuf.st_size);
wdenk5b1d7132002-11-03 00:07:02 +0000394 } else {
395 size = 0;
396 }
397
398 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
399 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530400 params.cmdname, params.imagefile,
401 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000402 exit (EXIT_FAILURE);
403 }
404
405 if (!file) {
406 break;
407 }
408
409 if (sep) {
410 *sep = ':';
411 file = sep + 1;
412 } else {
413 file = NULL;
414 }
415 }
416
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530417 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000418
419 for (;;) {
420 char *sep = strchr(file, ':');
421 if (sep) {
422 *sep = '\0';
423 copy_file (ifd, file, 1);
424 *sep++ = ':';
425 file = sep;
426 } else {
427 copy_file (ifd, file, 0);
428 break;
429 }
430 }
431 } else {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530432 copy_file (ifd, params.datafile, 0);
wdenk5b1d7132002-11-03 00:07:02 +0000433 }
434
435 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530436#if defined(_POSIX_SYNCHRONIZED_IO) && \
437 !defined(__sun__) && \
438 !defined(__FreeBSD__) && \
439 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000440 (void) fdatasync (ifd);
441#else
442 (void) fsync (ifd);
443#endif
444
445 if (fstat(ifd, &sbuf) < 0) {
446 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530447 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000448 exit (EXIT_FAILURE);
449 }
450
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400451 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
452 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000453 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530454 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000455 exit (EXIT_FAILURE);
456 }
457
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530458 /* Setup the image header as per input image type*/
459 if (tparams->set_header)
460 tparams->set_header (ptr, &sbuf, ifd, &params);
461 else {
462 fprintf (stderr, "%s: Can't set header for %s: %s\n",
463 params.cmdname, tparams->name, strerror(errno));
464 exit (EXIT_FAILURE);
465 }
wdenk5b1d7132002-11-03 00:07:02 +0000466
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530467 /* Print the image information by processing image header */
468 if (tparams->print_header)
469 tparams->print_header (ptr);
470 else {
471 fprintf (stderr, "%s: Can't print header for %s: %s\n",
472 params.cmdname, tparams->name, strerror(errno));
473 exit (EXIT_FAILURE);
474 }
wdenk5b1d7132002-11-03 00:07:02 +0000475
476 (void) munmap((void *)ptr, sbuf.st_size);
477
478 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530479#if defined(_POSIX_SYNCHRONIZED_IO) && \
480 !defined(__sun__) && \
481 !defined(__FreeBSD__) && \
482 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000483 (void) fdatasync (ifd);
484#else
485 (void) fsync (ifd);
486#endif
487
488 if (close(ifd)) {
489 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530490 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000491 exit (EXIT_FAILURE);
492 }
493
494 exit (EXIT_SUCCESS);
495}
496
497static void
498copy_file (int ifd, const char *datafile, int pad)
499{
500 int dfd;
501 struct stat sbuf;
502 unsigned char *ptr;
503 int tail;
504 int zero = 0;
505 int offset = 0;
506 int size;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530507 struct image_type_params *tparams = mkimage_get_type (params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000508
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530509 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000510 fprintf (stderr, "Adding Image %s\n", datafile);
511 }
512
wdenkef1464c2003-10-08 22:14:02 +0000513 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000514 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530515 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000516 exit (EXIT_FAILURE);
517 }
518
519 if (fstat(dfd, &sbuf) < 0) {
520 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530521 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000522 exit (EXIT_FAILURE);
523 }
524
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400525 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
526 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000527 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530528 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000529 exit (EXIT_FAILURE);
530 }
531
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530532 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000533 unsigned char *p = NULL;
534 /*
535 * XIP: do not append the image_header_t at the
536 * beginning of the file, but consume the space
537 * reserved for it.
538 */
539
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530540 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000541 fprintf (stderr,
542 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530543 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000544 exit (EXIT_FAILURE);
545 }
546
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530547 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000548 if ( *p != 0xff ) {
549 fprintf (stderr,
550 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530551 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000552 exit (EXIT_FAILURE);
553 }
554 }
555
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530556 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000557 }
558
559 size = sbuf.st_size - offset;
560 if (write(ifd, ptr + offset, size) != size) {
561 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530562 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000563 exit (EXIT_FAILURE);
564 }
565
566 if (pad && ((tail = size % 4) != 0)) {
567
568 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
569 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530570 params.cmdname, params.imagefile,
571 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000572 exit (EXIT_FAILURE);
573 }
574 }
575
576 (void) munmap((void *)ptr, sbuf.st_size);
577 (void) close (dfd);
578}
579
580void
581usage ()
582{
583 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100584 " -l ==> list image header information\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530585 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100586 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
587 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
588 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000589 " -O ==> set operating system to 'os'\n"
590 " -T ==> set image type to 'type'\n"
591 " -C ==> set compression type 'comp'\n"
592 " -a ==> set load address to 'addr' (hex)\n"
593 " -e ==> set entry point to 'ep' (hex)\n"
594 " -n ==> set image name to 'name'\n"
595 " -d ==> use image data from 'datafile'\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100596 " -x ==> set XIP (execute in place)\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530597 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100598 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530599 params.cmdname);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100600
wdenk5b1d7132002-11-03 00:07:02 +0000601 exit (EXIT_FAILURE);
602}