blob: 6e1ff2b1e2f12ca0c0da28c272bc56a0b16f78f9 [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +01002 * (C) Copyright 2008 Semihalf
3 *
wdenkdb82c8e2004-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
7 * All rights reserved.
wdenkdb82c8e2004-02-26 23:01:04 +00008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
wdenk5b1d7132002-11-03 00:07:02 +000023 */
24
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010025#include "mkimage.h"
wdenk5b1d7132002-11-03 00:07:02 +000026#include <image.h>
27
28extern int errno;
29
30#ifndef MAP_FAILED
31#define MAP_FAILED (-1)
32#endif
33
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010034extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
35static void copy_file (int, const char *, int);
36static void usage (void);
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +010037static void image_verify_header (char *, int);
38static void fit_handle_file (void);
wdenk5b1d7132002-11-03 00:07:02 +000039
40char *datafile;
41char *imagefile;
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010042char *cmdname;
wdenk5b1d7132002-11-03 00:07:02 +000043
44int dflag = 0;
45int eflag = 0;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +010046int fflag = 0;
wdenk5b1d7132002-11-03 00:07:02 +000047int lflag = 0;
48int vflag = 0;
49int xflag = 0;
50int opt_os = IH_OS_LINUX;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010051int opt_arch = IH_ARCH_PPC;
wdenk5b1d7132002-11-03 00:07:02 +000052int opt_type = IH_TYPE_KERNEL;
53int opt_comp = IH_COMP_GZIP;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +010054char *opt_dtc = MKIMAGE_DEFAULT_DTC_OPTIONS;
wdenk5b1d7132002-11-03 00:07:02 +000055
56image_header_t header;
57image_header_t *hdr = &header;
58
59int
60main (int argc, char **argv)
61{
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +010062 int ifd = -1;
wdenk5b1d7132002-11-03 00:07:02 +000063 uint32_t checksum;
64 uint32_t addr;
65 uint32_t ep;
66 struct stat sbuf;
67 unsigned char *ptr;
68 char *name = "";
69
70 cmdname = *argv;
71
72 addr = ep = 0;
73
74 while (--argc > 0 && **++argv == '-') {
75 while (*++*argv) {
76 switch (**argv) {
77 case 'l':
78 lflag = 1;
79 break;
80 case 'A':
81 if ((--argc <= 0) ||
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010082 (opt_arch = genimg_get_arch_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000083 usage ();
84 goto NXTARG;
85 case 'C':
86 if ((--argc <= 0) ||
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010087 (opt_comp = genimg_get_comp_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000088 usage ();
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +010089 goto NXTARG;
90 case 'D':
91 if (--argc <= 0)
92 usage ();
93 opt_dtc = *++argv;
wdenk5b1d7132002-11-03 00:07:02 +000094 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +010095
wdenk5b1d7132002-11-03 00:07:02 +000096 case 'O':
97 if ((--argc <= 0) ||
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010098 (opt_os = genimg_get_os_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +000099 usage ();
100 goto NXTARG;
101 case 'T':
102 if ((--argc <= 0) ||
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100103 (opt_type = genimg_get_type_id (*++argv)) < 0)
wdenk5b1d7132002-11-03 00:07:02 +0000104 usage ();
105 goto NXTARG;
106
107 case 'a':
108 if (--argc <= 0)
109 usage ();
110 addr = strtoul (*++argv, (char **)&ptr, 16);
111 if (*ptr) {
112 fprintf (stderr,
113 "%s: invalid load address %s\n",
114 cmdname, *argv);
115 exit (EXIT_FAILURE);
116 }
117 goto NXTARG;
118 case 'd':
119 if (--argc <= 0)
120 usage ();
121 datafile = *++argv;
122 dflag = 1;
123 goto NXTARG;
124 case 'e':
125 if (--argc <= 0)
126 usage ();
127 ep = strtoul (*++argv, (char **)&ptr, 16);
128 if (*ptr) {
129 fprintf (stderr,
130 "%s: invalid entry point %s\n",
131 cmdname, *argv);
132 exit (EXIT_FAILURE);
133 }
134 eflag = 1;
135 goto NXTARG;
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100136 case 'f':
137 if (--argc <= 0)
138 usage ();
139 datafile = *++argv;
140 fflag = 1;
141 goto NXTARG;
wdenk5b1d7132002-11-03 00:07:02 +0000142 case 'n':
143 if (--argc <= 0)
144 usage ();
145 name = *++argv;
146 goto NXTARG;
147 case 'v':
148 vflag++;
149 break;
150 case 'x':
151 xflag++;
152 break;
153 default:
154 usage ();
155 }
156 }
157NXTARG: ;
158 }
159
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100160 if ((argc != 1) ||
161 (dflag && (fflag || lflag)) ||
162 (fflag && (dflag || lflag)) ||
163 (lflag && (dflag || fflag)))
wdenk5b1d7132002-11-03 00:07:02 +0000164 usage();
165
166 if (!eflag) {
167 ep = addr;
168 /* If XIP, entry point must be after the U-Boot header */
169 if (xflag)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100170 ep += image_get_header_size ();
wdenk5b1d7132002-11-03 00:07:02 +0000171 }
172
173 /*
174 * If XIP, ensure the entry point is equal to the load address plus
175 * the size of the U-Boot header.
176 */
177 if (xflag) {
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100178 if (ep != addr + image_get_header_size ()) {
Wolfgang Denkdce59c52006-04-28 21:24:32 +0200179 fprintf (stderr,
180 "%s: For XIP, the entry point must be the load addr + %lu\n",
wdenk9dd2b882002-12-03 21:28:10 +0000181 cmdname,
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100182 (unsigned long)image_get_header_size ());
wdenk5b1d7132002-11-03 00:07:02 +0000183 exit (EXIT_FAILURE);
184 }
185 }
186
187 imagefile = *argv;
188
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100189 if (!fflag){
190 if (lflag) {
191 ifd = open (imagefile, O_RDONLY|O_BINARY);
192 } else {
193 ifd = open (imagefile,
194 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
195 }
wdenk5b1d7132002-11-03 00:07:02 +0000196
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100197 if (ifd < 0) {
198 fprintf (stderr, "%s: Can't open %s: %s\n",
199 cmdname, imagefile, strerror(errno));
200 exit (EXIT_FAILURE);
201 }
wdenk5b1d7132002-11-03 00:07:02 +0000202 }
203
204 if (lflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000205 /*
206 * list header information of existing image
207 */
208 if (fstat(ifd, &sbuf) < 0) {
209 fprintf (stderr, "%s: Can't stat %s: %s\n",
210 cmdname, imagefile, strerror(errno));
211 exit (EXIT_FAILURE);
212 }
213
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100214 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
wdenk5b1d7132002-11-03 00:07:02 +0000215 fprintf (stderr,
216 "%s: Bad size: \"%s\" is no valid image\n",
217 cmdname, imagefile);
218 exit (EXIT_FAILURE);
219 }
220
221 ptr = (unsigned char *)mmap(0, sbuf.st_size,
222 PROT_READ, MAP_SHARED, ifd, 0);
223 if ((caddr_t)ptr == (caddr_t)-1) {
224 fprintf (stderr, "%s: Can't read %s: %s\n",
225 cmdname, imagefile, strerror(errno));
226 exit (EXIT_FAILURE);
227 }
228
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100229 if (fdt_check_header (ptr)) {
230 /* old-style image */
231 image_verify_header ((char *)ptr, sbuf.st_size);
232 image_print_contents_noindent ((image_header_t *)ptr);
233 } else {
234 /* FIT image */
235 fit_print_contents_noindent (ptr);
wdenk5b1d7132002-11-03 00:07:02 +0000236 }
237
wdenk5b1d7132002-11-03 00:07:02 +0000238 (void) munmap((void *)ptr, sbuf.st_size);
239 (void) close (ifd);
240
241 exit (EXIT_SUCCESS);
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100242 } else if (fflag) {
243 /* Flattened Image Tree (FIT) format handling */
244 debug ("FIT format handling\n");
245 fit_handle_file ();
246 exit (EXIT_SUCCESS);
wdenk5b1d7132002-11-03 00:07:02 +0000247 }
248
249 /*
250 * Must be -w then:
251 *
252 * write dummy header, to be fixed later
253 */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100254 memset (hdr, 0, image_get_header_size ());
wdenk5b1d7132002-11-03 00:07:02 +0000255
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100256 if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) {
wdenk5b1d7132002-11-03 00:07:02 +0000257 fprintf (stderr, "%s: Write error on %s: %s\n",
258 cmdname, imagefile, strerror(errno));
259 exit (EXIT_FAILURE);
260 }
261
262 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
263 char *file = datafile;
Wolfgang Denk31f14c22006-01-11 13:03:54 +0100264 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000265
266 for (;;) {
267 char *sep = NULL;
268
269 if (file) {
270 if ((sep = strchr(file, ':')) != NULL) {
271 *sep = '\0';
272 }
273
274 if (stat (file, &sbuf) < 0) {
275 fprintf (stderr, "%s: Can't stat %s: %s\n",
276 cmdname, file, strerror(errno));
277 exit (EXIT_FAILURE);
278 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100279 size = cpu_to_uimage (sbuf.st_size);
wdenk5b1d7132002-11-03 00:07:02 +0000280 } else {
281 size = 0;
282 }
283
284 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
285 fprintf (stderr, "%s: Write error on %s: %s\n",
286 cmdname, imagefile, strerror(errno));
287 exit (EXIT_FAILURE);
288 }
289
290 if (!file) {
291 break;
292 }
293
294 if (sep) {
295 *sep = ':';
296 file = sep + 1;
297 } else {
298 file = NULL;
299 }
300 }
301
302 file = datafile;
303
304 for (;;) {
305 char *sep = strchr(file, ':');
306 if (sep) {
307 *sep = '\0';
308 copy_file (ifd, file, 1);
309 *sep++ = ':';
310 file = sep;
311 } else {
312 copy_file (ifd, file, 0);
313 break;
314 }
315 }
316 } else {
317 copy_file (ifd, datafile, 0);
318 }
319
320 /* We're a bit of paranoid */
Aubrey.Lifecb2a02007-05-14 11:47:35 +0800321#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000322 (void) fdatasync (ifd);
323#else
324 (void) fsync (ifd);
325#endif
326
327 if (fstat(ifd, &sbuf) < 0) {
328 fprintf (stderr, "%s: Can't stat %s: %s\n",
329 cmdname, imagefile, strerror(errno));
330 exit (EXIT_FAILURE);
331 }
332
333 ptr = (unsigned char *)mmap(0, sbuf.st_size,
334 PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
335 if (ptr == (unsigned char *)MAP_FAILED) {
336 fprintf (stderr, "%s: Can't map %s: %s\n",
337 cmdname, imagefile, strerror(errno));
338 exit (EXIT_FAILURE);
339 }
340
341 hdr = (image_header_t *)ptr;
342
343 checksum = crc32 (0,
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100344 (const char *)(ptr + image_get_header_size ()),
345 sbuf.st_size - image_get_header_size ()
wdenk5b1d7132002-11-03 00:07:02 +0000346 );
347
348 /* Build new header */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100349 image_set_magic (hdr, IH_MAGIC);
350 image_set_time (hdr, sbuf.st_mtime);
351 image_set_size (hdr, sbuf.st_size - image_get_header_size ());
352 image_set_load (hdr, addr);
353 image_set_ep (hdr, ep);
354 image_set_dcrc (hdr, checksum);
355 image_set_os (hdr, opt_os);
356 image_set_arch (hdr, opt_arch);
357 image_set_type (hdr, opt_type);
358 image_set_comp (hdr, opt_comp);
wdenk5b1d7132002-11-03 00:07:02 +0000359
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100360 image_set_name (hdr, name);
wdenk5b1d7132002-11-03 00:07:02 +0000361
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100362 checksum = crc32 (0, (const char *)hdr, image_get_header_size ());
wdenk5b1d7132002-11-03 00:07:02 +0000363
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100364 image_set_hcrc (hdr, checksum);
wdenk5b1d7132002-11-03 00:07:02 +0000365
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100366 image_print_contents_noindent (hdr);
wdenk5b1d7132002-11-03 00:07:02 +0000367
368 (void) munmap((void *)ptr, sbuf.st_size);
369
370 /* We're a bit of paranoid */
Aubrey.Lifecb2a02007-05-14 11:47:35 +0800371#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000372 (void) fdatasync (ifd);
373#else
374 (void) fsync (ifd);
375#endif
376
377 if (close(ifd)) {
378 fprintf (stderr, "%s: Write error on %s: %s\n",
379 cmdname, imagefile, strerror(errno));
380 exit (EXIT_FAILURE);
381 }
382
383 exit (EXIT_SUCCESS);
384}
385
386static void
387copy_file (int ifd, const char *datafile, int pad)
388{
389 int dfd;
390 struct stat sbuf;
391 unsigned char *ptr;
392 int tail;
393 int zero = 0;
394 int offset = 0;
395 int size;
396
397 if (vflag) {
398 fprintf (stderr, "Adding Image %s\n", datafile);
399 }
400
wdenk1ee550e2003-10-08 22:14:02 +0000401 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000402 fprintf (stderr, "%s: Can't open %s: %s\n",
403 cmdname, datafile, strerror(errno));
404 exit (EXIT_FAILURE);
405 }
406
407 if (fstat(dfd, &sbuf) < 0) {
408 fprintf (stderr, "%s: Can't stat %s: %s\n",
409 cmdname, datafile, strerror(errno));
410 exit (EXIT_FAILURE);
411 }
412
413 ptr = (unsigned char *)mmap(0, sbuf.st_size,
414 PROT_READ, MAP_SHARED, dfd, 0);
415 if (ptr == (unsigned char *)MAP_FAILED) {
416 fprintf (stderr, "%s: Can't read %s: %s\n",
417 cmdname, datafile, strerror(errno));
418 exit (EXIT_FAILURE);
419 }
420
421 if (xflag) {
422 unsigned char *p = NULL;
423 /*
424 * XIP: do not append the image_header_t at the
425 * beginning of the file, but consume the space
426 * reserved for it.
427 */
428
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100429 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
wdenk5b1d7132002-11-03 00:07:02 +0000430 fprintf (stderr,
431 "%s: Bad size: \"%s\" is too small for XIP\n",
432 cmdname, datafile);
433 exit (EXIT_FAILURE);
434 }
435
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100436 for (p = ptr; p < ptr + image_get_header_size (); p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000437 if ( *p != 0xff ) {
438 fprintf (stderr,
439 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
440 cmdname, datafile);
441 exit (EXIT_FAILURE);
442 }
443 }
444
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100445 offset = image_get_header_size ();
wdenk5b1d7132002-11-03 00:07:02 +0000446 }
447
448 size = sbuf.st_size - offset;
449 if (write(ifd, ptr + offset, size) != size) {
450 fprintf (stderr, "%s: Write error on %s: %s\n",
451 cmdname, imagefile, strerror(errno));
452 exit (EXIT_FAILURE);
453 }
454
455 if (pad && ((tail = size % 4) != 0)) {
456
457 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
458 fprintf (stderr, "%s: Write error on %s: %s\n",
459 cmdname, imagefile, strerror(errno));
460 exit (EXIT_FAILURE);
461 }
462 }
463
464 (void) munmap((void *)ptr, sbuf.st_size);
465 (void) close (dfd);
466}
467
468void
469usage ()
470{
471 fprintf (stderr, "Usage: %s -l image\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100472 " -l ==> list image header information\n",
473 cmdname);
474 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
475 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
476 " -A ==> set architecture to 'arch'\n"
wdenk5b1d7132002-11-03 00:07:02 +0000477 " -O ==> set operating system to 'os'\n"
478 " -T ==> set image type to 'type'\n"
479 " -C ==> set compression type 'comp'\n"
480 " -a ==> set load address to 'addr' (hex)\n"
481 " -e ==> set entry point to 'ep' (hex)\n"
482 " -n ==> set image name to 'name'\n"
483 " -d ==> use image data from 'datafile'\n"
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100484 " -x ==> set XIP (execute in place)\n",
485 cmdname);
486 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
487 cmdname);
488
wdenk5b1d7132002-11-03 00:07:02 +0000489 exit (EXIT_FAILURE);
490}
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +0100491
492static void
493image_verify_header (char *ptr, int image_size)
494{
495 int len;
496 char *data;
497 uint32_t checksum;
498 image_header_t header;
499 image_header_t *hdr = &header;
500
501 /*
502 * create copy of header so that we can blank out the
503 * checksum field for checking - this can't be done
504 * on the PROT_READ mapped data.
505 */
506 memcpy (hdr, ptr, sizeof(image_header_t));
507
508 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
509 fprintf (stderr,
510 "%s: Bad Magic Number: \"%s\" is no valid image\n",
511 cmdname, imagefile);
512 exit (EXIT_FAILURE);
513 }
514
515 data = (char *)hdr;
516 len = sizeof(image_header_t);
517
518 checksum = ntohl(hdr->ih_hcrc);
519 hdr->ih_hcrc = htonl(0); /* clear for re-calculation */
520
521 if (crc32 (0, data, len) != checksum) {
522 fprintf (stderr,
523 "%s: ERROR: \"%s\" has bad header checksum!\n",
524 cmdname, imagefile);
525 exit (EXIT_FAILURE);
526 }
527
528 data = ptr + sizeof(image_header_t);
529 len = image_size - sizeof(image_header_t) ;
530
531 if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) {
532 fprintf (stderr,
533 "%s: ERROR: \"%s\" has corrupted data!\n",
534 cmdname, imagefile);
535 exit (EXIT_FAILURE);
536 }
537}
538
539/**
540 * fit_handle_file - main FIT file processing function
541 *
542 * fit_handle_file() runs dtc to convert .its to .itb, includes
543 * binary data, updates timestamp property and calculates hashes.
544 *
545 * datafile - .its file
546 * imagefile - .itb file
547 *
548 * returns:
549 * only on success, otherwise calls exit (EXIT_FAILURE);
550 */
551static void fit_handle_file (void)
552{
553 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
554 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
555 int tfd;
556 struct stat sbuf;
557 unsigned char *ptr;
558
559 /* call dtc to include binary properties into the tmp file */
560 if (strlen (imagefile) + strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 >
561 sizeof (tmpfile)) {
562 fprintf (stderr, "%s: Image file name (%s) too long, "
563 "can't create tmpfile",
564 imagefile, cmdname);
565 exit (EXIT_FAILURE);
566 }
567 sprintf (tmpfile, "%s%s", imagefile, MKIMAGE_TMPFILE_SUFFIX);
568
569 /* dtc -I dts -O -p 200 datafile > tmpfile */
570 sprintf (cmd, "%s %s %s > %s",
571 MKIMAGE_DTC, opt_dtc, datafile, tmpfile);
572 debug ("Trying to execute \"%s\"\n", cmd);
573 if (system (cmd) == -1) {
574 fprintf (stderr, "%s: system(%s) failed: %s\n",
575 cmdname, cmd, strerror(errno));
576 unlink (tmpfile);
577 exit (EXIT_FAILURE);
578 }
579
580 /* load FIT blob into memory */
581 tfd = open (tmpfile, O_RDWR|O_BINARY);
582
583 if (tfd < 0) {
584 fprintf (stderr, "%s: Can't open %s: %s\n",
585 cmdname, tmpfile, strerror(errno));
586 unlink (tmpfile);
587 exit (EXIT_FAILURE);
588 }
589
590 if (fstat (tfd, &sbuf) < 0) {
591 fprintf (stderr, "%s: Can't stat %s: %s\n",
592 cmdname, tmpfile, strerror(errno));
593 unlink (tmpfile);
594 exit (EXIT_FAILURE);
595 }
596
597 ptr = (unsigned char *)mmap (0, sbuf.st_size,
598 PROT_READ|PROT_WRITE, MAP_SHARED, tfd, 0);
599 if ((caddr_t)ptr == (caddr_t)-1) {
600 fprintf (stderr, "%s: Can't read %s: %s\n",
601 cmdname, tmpfile, strerror(errno));
602 unlink (tmpfile);
603 exit (EXIT_FAILURE);
604 }
605
606 /* check if ptr has a valid blob */
607 if (fdt_check_header (ptr)) {
608 fprintf (stderr, "%s: Invalid FIT blob\n", cmdname);
609 unlink (tmpfile);
610 exit (EXIT_FAILURE);
611 }
612
613 /* set hashes for images in the blob */
614 if (fit_set_hashes (ptr)) {
615 fprintf (stderr, "%s Can't add hashes to FIT blob", cmdname);
616 unlink (tmpfile);
617 exit (EXIT_FAILURE);
618 }
619
620 /* add a timestamp at offset 0 i.e., root */
621 if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) {
622 fprintf (stderr, "%s: Can't add image timestamp\n", cmdname);
623 unlink (tmpfile);
624 exit (EXIT_FAILURE);
625 }
626 debug ("Added timestamp successfully\n");
627
628 munmap ((void *)ptr, sbuf.st_size);
629 close (tfd);
630
631 if (rename (tmpfile, imagefile) == -1) {
632 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
633 cmdname, tmpfile, imagefile, strerror (errno));
634 unlink (tmpfile);
635 unlink (imagefile);
636 exit (EXIT_FAILURE);
637 }
638}