blob: dc2d4c5386f130f44b36dd2a4773f60c74b5e767 [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
Bartlomiej Sieka9d254382008-03-11 12:34:47 +01002 * (C) Copyright 2008 Semihalf
3 *
wdenk0c852a22004-02-26 23:01:04 +00004 * (C) Copyright 2000-2004
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
Marian Balakowicz570abb02008-02-29 15:59:59 +010027extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
28static void copy_file (int, const char *, int);
29static void usage (void);
Prafulla Wadaskar40864632009-08-10 18:49:37 +053030static int image_verify_header (char *, int);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +010031static void fit_handle_file (void);
wdenk5b1d7132002-11-03 00:07:02 +000032
33char *datafile;
34char *imagefile;
Marian Balakowicz570abb02008-02-29 15:59:59 +010035char *cmdname;
wdenk5b1d7132002-11-03 00:07:02 +000036
37int dflag = 0;
38int eflag = 0;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +010039int fflag = 0;
wdenk5b1d7132002-11-03 00:07:02 +000040int lflag = 0;
41int vflag = 0;
42int xflag = 0;
43int opt_os = IH_OS_LINUX;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010044int opt_arch = IH_ARCH_PPC;
wdenk5b1d7132002-11-03 00:07:02 +000045int opt_type = IH_TYPE_KERNEL;
46int opt_comp = IH_COMP_GZIP;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +010047char *opt_dtc = MKIMAGE_DEFAULT_DTC_OPTIONS;
wdenk5b1d7132002-11-03 00:07:02 +000048
49image_header_t header;
50image_header_t *hdr = &header;
51
52int
53main (int argc, char **argv)
54{
Bartlomiej Sieka9d254382008-03-11 12:34:47 +010055 int ifd = -1;
wdenk5b1d7132002-11-03 00:07:02 +000056 uint32_t checksum;
57 uint32_t addr;
58 uint32_t ep;
59 struct stat sbuf;
60 unsigned char *ptr;
61 char *name = "";
Prafulla Wadaskar40864632009-08-10 18:49:37 +053062 int retval;
wdenk5b1d7132002-11-03 00:07:02 +000063
64 cmdname = *argv;
65
66 addr = ep = 0;
67
68 while (--argc > 0 && **++argv == '-') {
69 while (*++*argv) {
70 switch (**argv) {
71 case 'l':
72 lflag = 1;
73 break;
74 case 'A':
75 if ((--argc <= 0) ||
Marian Balakowicz570abb02008-02-29 15:59:59 +010076 (opt_arch = genimg_get_arch_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000077 usage ();
78 goto NXTARG;
79 case 'C':
80 if ((--argc <= 0) ||
Marian Balakowicz570abb02008-02-29 15:59:59 +010081 (opt_comp = genimg_get_comp_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000082 usage ();
83 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +010084 case 'D':
85 if (--argc <= 0)
86 usage ();
87 opt_dtc = *++argv;
88 goto NXTARG;
89
wdenk5b1d7132002-11-03 00:07:02 +000090 case 'O':
91 if ((--argc <= 0) ||
Marian Balakowicz570abb02008-02-29 15:59:59 +010092 (opt_os = genimg_get_os_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000093 usage ();
94 goto NXTARG;
95 case 'T':
96 if ((--argc <= 0) ||
Marian Balakowicz570abb02008-02-29 15:59:59 +010097 (opt_type = genimg_get_type_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000098 usage ();
99 goto NXTARG;
100
101 case 'a':
102 if (--argc <= 0)
103 usage ();
104 addr = strtoul (*++argv, (char **)&ptr, 16);
105 if (*ptr) {
106 fprintf (stderr,
107 "%s: invalid load address %s\n",
108 cmdname, *argv);
109 exit (EXIT_FAILURE);
110 }
111 goto NXTARG;
112 case 'd':
113 if (--argc <= 0)
114 usage ();
115 datafile = *++argv;
116 dflag = 1;
117 goto NXTARG;
118 case 'e':
119 if (--argc <= 0)
120 usage ();
121 ep = strtoul (*++argv, (char **)&ptr, 16);
122 if (*ptr) {
123 fprintf (stderr,
124 "%s: invalid entry point %s\n",
125 cmdname, *argv);
126 exit (EXIT_FAILURE);
127 }
128 eflag = 1;
129 goto NXTARG;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100130 case 'f':
131 if (--argc <= 0)
132 usage ();
133 datafile = *++argv;
134 fflag = 1;
135 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000136 case 'n':
137 if (--argc <= 0)
138 usage ();
139 name = *++argv;
140 goto NXTARG;
141 case 'v':
142 vflag++;
143 break;
144 case 'x':
145 xflag++;
146 break;
147 default:
148 usage ();
149 }
150 }
151NXTARG: ;
152 }
153
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100154 if ((argc != 1) ||
155 (dflag && (fflag || lflag)) ||
156 (fflag && (dflag || lflag)) ||
157 (lflag && (dflag || fflag)))
wdenk5b1d7132002-11-03 00:07:02 +0000158 usage();
159
160 if (!eflag) {
161 ep = addr;
162 /* If XIP, entry point must be after the U-Boot header */
163 if (xflag)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100164 ep += image_get_header_size ();
wdenk5b1d7132002-11-03 00:07:02 +0000165 }
166
167 /*
168 * If XIP, ensure the entry point is equal to the load address plus
169 * the size of the U-Boot header.
170 */
171 if (xflag) {
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100172 if (ep != addr + image_get_header_size ()) {
Wolfgang Denk3577d3a2006-04-28 21:24:32 +0200173 fprintf (stderr,
174 "%s: For XIP, the entry point must be the load addr + %lu\n",
wdenka6c7ad22002-12-03 21:28:10 +0000175 cmdname,
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100176 (unsigned long)image_get_header_size ());
wdenk5b1d7132002-11-03 00:07:02 +0000177 exit (EXIT_FAILURE);
178 }
179 }
180
181 imagefile = *argv;
182
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100183 if (!fflag){
184 if (lflag) {
185 ifd = open (imagefile, O_RDONLY|O_BINARY);
186 } else {
187 ifd = open (imagefile,
188 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
189 }
wdenk5b1d7132002-11-03 00:07:02 +0000190
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100191 if (ifd < 0) {
192 fprintf (stderr, "%s: Can't open %s: %s\n",
193 cmdname, imagefile, strerror(errno));
194 exit (EXIT_FAILURE);
195 }
wdenk5b1d7132002-11-03 00:07:02 +0000196 }
197
198 if (lflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000199 /*
200 * list header information of existing image
201 */
202 if (fstat(ifd, &sbuf) < 0) {
203 fprintf (stderr, "%s: Can't stat %s: %s\n",
204 cmdname, imagefile, strerror(errno));
205 exit (EXIT_FAILURE);
206 }
207
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100208 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
wdenk5b1d7132002-11-03 00:07:02 +0000209 fprintf (stderr,
210 "%s: Bad size: \"%s\" is no valid image\n",
211 cmdname, imagefile);
212 exit (EXIT_FAILURE);
213 }
214
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400215 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
216 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000217 fprintf (stderr, "%s: Can't read %s: %s\n",
218 cmdname, imagefile, strerror(errno));
219 exit (EXIT_FAILURE);
220 }
221
Prafulla Wadaskar40864632009-08-10 18:49:37 +0530222 if (!(retval = fdt_check_header (ptr))) {
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100223 /* FIT image */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200224 fit_print_contents (ptr);
Prafulla Wadaskar40864632009-08-10 18:49:37 +0530225 } else if (!(retval = image_verify_header ((char *)ptr,
226 sbuf.st_size))) {
227 /* old-style image */
228 image_print_contents ((image_header_t *)ptr);
wdenk5b1d7132002-11-03 00:07:02 +0000229 }
230
wdenk5b1d7132002-11-03 00:07:02 +0000231 (void) munmap((void *)ptr, sbuf.st_size);
232 (void) close (ifd);
233
Prafulla Wadaskar40864632009-08-10 18:49:37 +0530234 exit (retval);
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100235 } else if (fflag) {
236 /* Flattened Image Tree (FIT) format handling */
237 debug ("FIT format handling\n");
238 fit_handle_file ();
Prafulla Wadaskar40864632009-08-10 18:49:37 +0530239 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000240 }
241
242 /*
243 * Must be -w then:
244 *
245 * write dummy header, to be fixed later
246 */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100247 memset (hdr, 0, image_get_header_size ());
wdenk5b1d7132002-11-03 00:07:02 +0000248
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100249 if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) {
wdenk5b1d7132002-11-03 00:07:02 +0000250 fprintf (stderr, "%s: Write error on %s: %s\n",
251 cmdname, imagefile, strerror(errno));
252 exit (EXIT_FAILURE);
253 }
254
255 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
256 char *file = datafile;
Wolfgang Denk3bb66802006-01-11 13:03:54 +0100257 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000258
259 for (;;) {
260 char *sep = NULL;
261
262 if (file) {
263 if ((sep = strchr(file, ':')) != NULL) {
264 *sep = '\0';
265 }
266
267 if (stat (file, &sbuf) < 0) {
268 fprintf (stderr, "%s: Can't stat %s: %s\n",
269 cmdname, file, strerror(errno));
270 exit (EXIT_FAILURE);
271 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100272 size = cpu_to_uimage (sbuf.st_size);
wdenk5b1d7132002-11-03 00:07:02 +0000273 } else {
274 size = 0;
275 }
276
277 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
278 fprintf (stderr, "%s: Write error on %s: %s\n",
279 cmdname, imagefile, strerror(errno));
280 exit (EXIT_FAILURE);
281 }
282
283 if (!file) {
284 break;
285 }
286
287 if (sep) {
288 *sep = ':';
289 file = sep + 1;
290 } else {
291 file = NULL;
292 }
293 }
294
295 file = datafile;
296
297 for (;;) {
298 char *sep = strchr(file, ':');
299 if (sep) {
300 *sep = '\0';
301 copy_file (ifd, file, 1);
302 *sep++ = ':';
303 file = sep;
304 } else {
305 copy_file (ifd, file, 0);
306 break;
307 }
308 }
309 } else {
310 copy_file (ifd, datafile, 0);
311 }
312
313 /* We're a bit of paranoid */
Aubrey.Li5dfaa502007-05-14 11:47:35 +0800314#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000315 (void) fdatasync (ifd);
316#else
317 (void) fsync (ifd);
318#endif
319
320 if (fstat(ifd, &sbuf) < 0) {
321 fprintf (stderr, "%s: Can't stat %s: %s\n",
322 cmdname, imagefile, strerror(errno));
323 exit (EXIT_FAILURE);
324 }
325
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400326 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
327 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000328 fprintf (stderr, "%s: Can't map %s: %s\n",
329 cmdname, imagefile, strerror(errno));
330 exit (EXIT_FAILURE);
331 }
332
333 hdr = (image_header_t *)ptr;
334
335 checksum = crc32 (0,
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100336 (const char *)(ptr + image_get_header_size ()),
337 sbuf.st_size - image_get_header_size ()
wdenk5b1d7132002-11-03 00:07:02 +0000338 );
339
340 /* Build new header */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100341 image_set_magic (hdr, IH_MAGIC);
342 image_set_time (hdr, sbuf.st_mtime);
343 image_set_size (hdr, sbuf.st_size - image_get_header_size ());
344 image_set_load (hdr, addr);
345 image_set_ep (hdr, ep);
346 image_set_dcrc (hdr, checksum);
347 image_set_os (hdr, opt_os);
348 image_set_arch (hdr, opt_arch);
349 image_set_type (hdr, opt_type);
350 image_set_comp (hdr, opt_comp);
wdenk5b1d7132002-11-03 00:07:02 +0000351
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100352 image_set_name (hdr, name);
wdenk5b1d7132002-11-03 00:07:02 +0000353
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100354 checksum = crc32 (0, (const char *)hdr, image_get_header_size ());
wdenk5b1d7132002-11-03 00:07:02 +0000355
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100356 image_set_hcrc (hdr, checksum);
wdenk5b1d7132002-11-03 00:07:02 +0000357
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200358 image_print_contents (hdr);
wdenk5b1d7132002-11-03 00:07:02 +0000359
360 (void) munmap((void *)ptr, sbuf.st_size);
361
362 /* We're a bit of paranoid */
Aubrey.Li5dfaa502007-05-14 11:47:35 +0800363#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000364 (void) fdatasync (ifd);
365#else
366 (void) fsync (ifd);
367#endif
368
369 if (close(ifd)) {
370 fprintf (stderr, "%s: Write error on %s: %s\n",
371 cmdname, imagefile, strerror(errno));
372 exit (EXIT_FAILURE);
373 }
374
375 exit (EXIT_SUCCESS);
376}
377
378static void
379copy_file (int ifd, const char *datafile, int pad)
380{
381 int dfd;
382 struct stat sbuf;
383 unsigned char *ptr;
384 int tail;
385 int zero = 0;
386 int offset = 0;
387 int size;
388
389 if (vflag) {
390 fprintf (stderr, "Adding Image %s\n", datafile);
391 }
392
wdenkef1464c2003-10-08 22:14:02 +0000393 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000394 fprintf (stderr, "%s: Can't open %s: %s\n",
395 cmdname, datafile, strerror(errno));
396 exit (EXIT_FAILURE);
397 }
398
399 if (fstat(dfd, &sbuf) < 0) {
400 fprintf (stderr, "%s: Can't stat %s: %s\n",
401 cmdname, datafile, strerror(errno));
402 exit (EXIT_FAILURE);
403 }
404
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400405 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
406 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000407 fprintf (stderr, "%s: Can't read %s: %s\n",
408 cmdname, datafile, strerror(errno));
409 exit (EXIT_FAILURE);
410 }
411
412 if (xflag) {
413 unsigned char *p = NULL;
414 /*
415 * XIP: do not append the image_header_t at the
416 * beginning of the file, but consume the space
417 * reserved for it.
418 */
419
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100420 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
wdenk5b1d7132002-11-03 00:07:02 +0000421 fprintf (stderr,
422 "%s: Bad size: \"%s\" is too small for XIP\n",
423 cmdname, datafile);
424 exit (EXIT_FAILURE);
425 }
426
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100427 for (p = ptr; p < ptr + image_get_header_size (); p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000428 if ( *p != 0xff ) {
429 fprintf (stderr,
430 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
431 cmdname, datafile);
432 exit (EXIT_FAILURE);
433 }
434 }
435
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100436 offset = image_get_header_size ();
wdenk5b1d7132002-11-03 00:07:02 +0000437 }
438
439 size = sbuf.st_size - offset;
440 if (write(ifd, ptr + offset, size) != size) {
441 fprintf (stderr, "%s: Write error on %s: %s\n",
442 cmdname, imagefile, strerror(errno));
443 exit (EXIT_FAILURE);
444 }
445
446 if (pad && ((tail = size % 4) != 0)) {
447
448 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
449 fprintf (stderr, "%s: Write error on %s: %s\n",
450 cmdname, imagefile, strerror(errno));
451 exit (EXIT_FAILURE);
452 }
453 }
454
455 (void) munmap((void *)ptr, sbuf.st_size);
456 (void) close (dfd);
457}
458
459void
460usage ()
461{
462 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100463 " -l ==> list image header information\n",
464 cmdname);
465 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
466 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
467 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000468 " -O ==> set operating system to 'os'\n"
469 " -T ==> set image type to 'type'\n"
470 " -C ==> set compression type 'comp'\n"
471 " -a ==> set load address to 'addr' (hex)\n"
472 " -e ==> set entry point to 'ep' (hex)\n"
473 " -n ==> set image name to 'name'\n"
474 " -d ==> use image data from 'datafile'\n"
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100475 " -x ==> set XIP (execute in place)\n",
476 cmdname);
477 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
478 cmdname);
479
wdenk5b1d7132002-11-03 00:07:02 +0000480 exit (EXIT_FAILURE);
481}
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100482
Prafulla Wadaskar40864632009-08-10 18:49:37 +0530483static int
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100484image_verify_header (char *ptr, int image_size)
485{
486 int len;
487 char *data;
488 uint32_t checksum;
489 image_header_t header;
490 image_header_t *hdr = &header;
491
492 /*
493 * create copy of header so that we can blank out the
494 * checksum field for checking - this can't be done
495 * on the PROT_READ mapped data.
496 */
497 memcpy (hdr, ptr, sizeof(image_header_t));
498
Mike Frysinger37566092009-07-02 19:23:25 -0400499 if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100500 fprintf (stderr,
501 "%s: Bad Magic Number: \"%s\" is no valid image\n",
502 cmdname, imagefile);
Prafulla Wadaskar40864632009-08-10 18:49:37 +0530503 return -FDT_ERR_BADMAGIC;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100504 }
505
506 data = (char *)hdr;
507 len = sizeof(image_header_t);
508
Mike Frysinger37566092009-07-02 19:23:25 -0400509 checksum = be32_to_cpu(hdr->ih_hcrc);
510 hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100511
512 if (crc32 (0, data, len) != checksum) {
513 fprintf (stderr,
514 "%s: ERROR: \"%s\" has bad header checksum!\n",
515 cmdname, imagefile);
Prafulla Wadaskar40864632009-08-10 18:49:37 +0530516 return -FDT_ERR_BADSTATE;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100517 }
518
519 data = ptr + sizeof(image_header_t);
520 len = image_size - sizeof(image_header_t) ;
521
Mike Frysinger37566092009-07-02 19:23:25 -0400522 if (crc32 (0, data, len) != be32_to_cpu(hdr->ih_dcrc)) {
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100523 fprintf (stderr,
524 "%s: ERROR: \"%s\" has corrupted data!\n",
525 cmdname, imagefile);
Prafulla Wadaskar40864632009-08-10 18:49:37 +0530526 return -FDT_ERR_BADSTRUCTURE;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100527 }
Prafulla Wadaskar40864632009-08-10 18:49:37 +0530528 return 0;
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100529}
530
531/**
532 * fit_handle_file - main FIT file processing function
533 *
534 * fit_handle_file() runs dtc to convert .its to .itb, includes
535 * binary data, updates timestamp property and calculates hashes.
536 *
537 * datafile - .its file
538 * imagefile - .itb file
539 *
540 * returns:
541 * only on success, otherwise calls exit (EXIT_FAILURE);
542 */
543static void fit_handle_file (void)
544{
545 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
546 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
547 int tfd;
548 struct stat sbuf;
549 unsigned char *ptr;
550
551 /* call dtc to include binary properties into the tmp file */
552 if (strlen (imagefile) + strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 >
553 sizeof (tmpfile)) {
554 fprintf (stderr, "%s: Image file name (%s) too long, "
555 "can't create tmpfile",
556 imagefile, cmdname);
557 exit (EXIT_FAILURE);
558 }
559 sprintf (tmpfile, "%s%s", imagefile, MKIMAGE_TMPFILE_SUFFIX);
560
561 /* dtc -I dts -O -p 200 datafile > tmpfile */
562 sprintf (cmd, "%s %s %s > %s",
563 MKIMAGE_DTC, opt_dtc, datafile, tmpfile);
564 debug ("Trying to execute \"%s\"\n", cmd);
565 if (system (cmd) == -1) {
566 fprintf (stderr, "%s: system(%s) failed: %s\n",
567 cmdname, cmd, strerror(errno));
568 unlink (tmpfile);
569 exit (EXIT_FAILURE);
570 }
571
572 /* load FIT blob into memory */
573 tfd = open (tmpfile, O_RDWR|O_BINARY);
574
575 if (tfd < 0) {
576 fprintf (stderr, "%s: Can't open %s: %s\n",
577 cmdname, tmpfile, strerror(errno));
578 unlink (tmpfile);
579 exit (EXIT_FAILURE);
580 }
581
582 if (fstat (tfd, &sbuf) < 0) {
583 fprintf (stderr, "%s: Can't stat %s: %s\n",
584 cmdname, tmpfile, strerror(errno));
585 unlink (tmpfile);
586 exit (EXIT_FAILURE);
587 }
588
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400589 ptr = mmap (0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, tfd, 0);
590 if (ptr == MAP_FAILED) {
Bartlomiej Sieka9d254382008-03-11 12:34:47 +0100591 fprintf (stderr, "%s: Can't read %s: %s\n",
592 cmdname, tmpfile, strerror(errno));
593 unlink (tmpfile);
594 exit (EXIT_FAILURE);
595 }
596
597 /* check if ptr has a valid blob */
598 if (fdt_check_header (ptr)) {
599 fprintf (stderr, "%s: Invalid FIT blob\n", cmdname);
600 unlink (tmpfile);
601 exit (EXIT_FAILURE);
602 }
603
604 /* set hashes for images in the blob */
605 if (fit_set_hashes (ptr)) {
606 fprintf (stderr, "%s Can't add hashes to FIT blob", cmdname);
607 unlink (tmpfile);
608 exit (EXIT_FAILURE);
609 }
610
611 /* add a timestamp at offset 0 i.e., root */
612 if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) {
613 fprintf (stderr, "%s: Can't add image timestamp\n", cmdname);
614 unlink (tmpfile);
615 exit (EXIT_FAILURE);
616 }
617 debug ("Added timestamp successfully\n");
618
619 munmap ((void *)ptr, sbuf.st_size);
620 close (tfd);
621
622 if (rename (tmpfile, imagefile) == -1) {
623 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
624 cmdname, tmpfile, imagefile, strerror (errno));
625 unlink (tmpfile);
626 unlink (imagefile);
627 exit (EXIT_FAILURE);
628 }
629}