blob: 8ced9709a63b62f6b717b2e0eecd12bcf89f10bf [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
wdenkdb82c8e2004-02-26 23:01:04 +00002 * (C) Copyright 2000-2004
wdenk5b1d7132002-11-03 00:07:02 +00003 * DENX Software Engineering
4 * Wolfgang Denk, wd@denx.de
5 * All rights reserved.
wdenkdb82c8e2004-02-26 23:01:04 +00006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
wdenk5b1d7132002-11-03 00:07:02 +000021 */
22
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010023#include "mkimage.h"
wdenk5b1d7132002-11-03 00:07:02 +000024#include <image.h>
25
26extern int errno;
27
28#ifndef MAP_FAILED
29#define MAP_FAILED (-1)
30#endif
31
32char *cmdname;
33
34extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
35
36typedef struct table_entry {
37 int val; /* as defined in image.h */
38 char *sname; /* short (input) name */
39 char *lname; /* long (output) name */
40} table_entry_t;
41
42table_entry_t arch_name[] = {
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010043 { IH_ARCH_INVALID, NULL, "Invalid CPU", },
44 { IH_ARCH_ALPHA, "alpha", "Alpha", },
45 { IH_ARCH_ARM, "arm", "ARM", },
46 { IH_ARCH_I386, "x86", "Intel x86", },
47 { IH_ARCH_IA64, "ia64", "IA64", },
48 { IH_ARCH_M68K, "m68k", "MC68000", },
49 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
50 { IH_ARCH_MIPS, "mips", "MIPS", },
51 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
52 { IH_ARCH_NIOS, "nios", "NIOS", },
53 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
54 { IH_ARCH_PPC, "ppc", "PowerPC", },
55 { IH_ARCH_S390, "s390", "IBM S390", },
56 { IH_ARCH_SH, "sh", "SuperH", },
57 { IH_ARCH_SPARC, "sparc", "SPARC", },
58 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
59 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
60 { IH_ARCH_AVR32, "avr32", "AVR32", },
wdenk265d2172004-07-10 22:35:59 +000061 { -1, "", "", },
wdenk5b1d7132002-11-03 00:07:02 +000062};
63
64table_entry_t os_name[] = {
65 { IH_OS_INVALID, NULL, "Invalid OS", },
wdenk5b1d7132002-11-03 00:07:02 +000066 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
wdenk265d2172004-07-10 22:35:59 +000067 { IH_OS_ARTOS, "artos", "ARTOS", },
68 { IH_OS_DELL, "dell", "Dell", },
wdenk5b1d7132002-11-03 00:07:02 +000069 { IH_OS_ESIX, "esix", "Esix", },
wdenk265d2172004-07-10 22:35:59 +000070 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
wdenk5b1d7132002-11-03 00:07:02 +000071 { IH_OS_IRIX, "irix", "Irix", },
wdenk265d2172004-07-10 22:35:59 +000072 { IH_OS_LINUX, "linux", "Linux", },
wdenk5b1d7132002-11-03 00:07:02 +000073 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
wdenk265d2172004-07-10 22:35:59 +000074 { IH_OS_NCR, "ncr", "NCR", },
75 { IH_OS_NETBSD, "netbsd", "NetBSD", },
76 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
wdenk5b1d7132002-11-03 00:07:02 +000077 { IH_OS_PSOS, "psos", "pSOS", },
78 { IH_OS_QNX, "qnx", "QNX", },
wdenk92bbe3f2003-04-20 14:04:18 +000079 { IH_OS_RTEMS, "rtems", "RTEMS", },
wdenk265d2172004-07-10 22:35:59 +000080 { IH_OS_SCO, "sco", "SCO", },
81 { IH_OS_SOLARIS, "solaris", "Solaris", },
82 { IH_OS_SVR4, "svr4", "SVR4", },
83 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
84 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
wdenk5b1d7132002-11-03 00:07:02 +000085 { -1, "", "", },
86};
87
88table_entry_t type_name[] = {
89 { IH_TYPE_INVALID, NULL, "Invalid Image", },
wdenk265d2172004-07-10 22:35:59 +000090 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
91 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
wdenk5b1d7132002-11-03 00:07:02 +000092 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
wdenk5b1d7132002-11-03 00:07:02 +000093 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
wdenk265d2172004-07-10 22:35:59 +000094 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
wdenk5b1d7132002-11-03 00:07:02 +000095 { IH_TYPE_SCRIPT, "script", "Script", },
wdenk265d2172004-07-10 22:35:59 +000096 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Matthew McClintock16485a62006-08-16 10:54:09 -050097 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
wdenk5b1d7132002-11-03 00:07:02 +000098 { -1, "", "", },
99};
100
101table_entry_t comp_name[] = {
102 { IH_COMP_NONE, "none", "uncompressed", },
wdenk5b1d7132002-11-03 00:07:02 +0000103 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
wdenk265d2172004-07-10 22:35:59 +0000104 { IH_COMP_GZIP, "gzip", "gzip compressed", },
wdenk5b1d7132002-11-03 00:07:02 +0000105 { -1, "", "", },
106};
107
108static void copy_file (int, const char *, int);
109static void usage (void);
110static void print_header (image_header_t *);
111static void print_type (image_header_t *);
112static char *put_table_entry (table_entry_t *, char *, int);
113static char *put_arch (int);
114static char *put_type (int);
115static char *put_os (int);
116static char *put_comp (int);
117static int get_table_entry (table_entry_t *, char *, char *);
118static int get_arch(char *);
119static int get_comp(char *);
120static int get_os (char *);
121static int get_type(char *);
122
123
124char *datafile;
125char *imagefile;
126
127int dflag = 0;
128int eflag = 0;
129int lflag = 0;
130int vflag = 0;
131int xflag = 0;
132int opt_os = IH_OS_LINUX;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100133int opt_arch = IH_ARCH_PPC;
wdenk5b1d7132002-11-03 00:07:02 +0000134int opt_type = IH_TYPE_KERNEL;
135int opt_comp = IH_COMP_GZIP;
136
137image_header_t header;
138image_header_t *hdr = &header;
139
140int
141main (int argc, char **argv)
142{
143 int ifd;
144 uint32_t checksum;
145 uint32_t addr;
146 uint32_t ep;
147 struct stat sbuf;
148 unsigned char *ptr;
149 char *name = "";
150
151 cmdname = *argv;
152
153 addr = ep = 0;
154
155 while (--argc > 0 && **++argv == '-') {
156 while (*++*argv) {
157 switch (**argv) {
158 case 'l':
159 lflag = 1;
160 break;
161 case 'A':
162 if ((--argc <= 0) ||
163 (opt_arch = get_arch(*++argv)) < 0)
164 usage ();
165 goto NXTARG;
166 case 'C':
167 if ((--argc <= 0) ||
168 (opt_comp = get_comp(*++argv)) < 0)
169 usage ();
170 goto NXTARG;
171 case 'O':
172 if ((--argc <= 0) ||
173 (opt_os = get_os(*++argv)) < 0)
174 usage ();
175 goto NXTARG;
176 case 'T':
177 if ((--argc <= 0) ||
178 (opt_type = get_type(*++argv)) < 0)
179 usage ();
180 goto NXTARG;
181
182 case 'a':
183 if (--argc <= 0)
184 usage ();
185 addr = strtoul (*++argv, (char **)&ptr, 16);
186 if (*ptr) {
187 fprintf (stderr,
188 "%s: invalid load address %s\n",
189 cmdname, *argv);
190 exit (EXIT_FAILURE);
191 }
192 goto NXTARG;
193 case 'd':
194 if (--argc <= 0)
195 usage ();
196 datafile = *++argv;
197 dflag = 1;
198 goto NXTARG;
199 case 'e':
200 if (--argc <= 0)
201 usage ();
202 ep = strtoul (*++argv, (char **)&ptr, 16);
203 if (*ptr) {
204 fprintf (stderr,
205 "%s: invalid entry point %s\n",
206 cmdname, *argv);
207 exit (EXIT_FAILURE);
208 }
209 eflag = 1;
210 goto NXTARG;
211 case 'n':
212 if (--argc <= 0)
213 usage ();
214 name = *++argv;
215 goto NXTARG;
216 case 'v':
217 vflag++;
218 break;
219 case 'x':
220 xflag++;
221 break;
222 default:
223 usage ();
224 }
225 }
226NXTARG: ;
227 }
228
229 if ((argc != 1) || ((lflag ^ dflag) == 0))
230 usage();
231
232 if (!eflag) {
233 ep = addr;
234 /* If XIP, entry point must be after the U-Boot header */
235 if (xflag)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100236 ep += image_get_header_size ();
wdenk5b1d7132002-11-03 00:07:02 +0000237 }
238
239 /*
240 * If XIP, ensure the entry point is equal to the load address plus
241 * the size of the U-Boot header.
242 */
243 if (xflag) {
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100244 if (ep != addr + image_get_header_size ()) {
Wolfgang Denkdce59c52006-04-28 21:24:32 +0200245 fprintf (stderr,
246 "%s: For XIP, the entry point must be the load addr + %lu\n",
wdenk9dd2b882002-12-03 21:28:10 +0000247 cmdname,
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100248 (unsigned long)image_get_header_size ());
wdenk5b1d7132002-11-03 00:07:02 +0000249 exit (EXIT_FAILURE);
250 }
251 }
252
253 imagefile = *argv;
254
255 if (lflag) {
wdenk1ee550e2003-10-08 22:14:02 +0000256 ifd = open(imagefile, O_RDONLY|O_BINARY);
wdenk5b1d7132002-11-03 00:07:02 +0000257 } else {
wdenk5b1d7132002-11-03 00:07:02 +0000258 ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
wdenk5b1d7132002-11-03 00:07:02 +0000259 }
260
261 if (ifd < 0) {
262 fprintf (stderr, "%s: Can't open %s: %s\n",
263 cmdname, imagefile, strerror(errno));
264 exit (EXIT_FAILURE);
265 }
266
267 if (lflag) {
268 int len;
269 char *data;
270 /*
271 * list header information of existing image
272 */
273 if (fstat(ifd, &sbuf) < 0) {
274 fprintf (stderr, "%s: Can't stat %s: %s\n",
275 cmdname, imagefile, strerror(errno));
276 exit (EXIT_FAILURE);
277 }
278
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100279 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
wdenk5b1d7132002-11-03 00:07:02 +0000280 fprintf (stderr,
281 "%s: Bad size: \"%s\" is no valid image\n",
282 cmdname, imagefile);
283 exit (EXIT_FAILURE);
284 }
285
286 ptr = (unsigned char *)mmap(0, sbuf.st_size,
287 PROT_READ, MAP_SHARED, ifd, 0);
288 if ((caddr_t)ptr == (caddr_t)-1) {
289 fprintf (stderr, "%s: Can't read %s: %s\n",
290 cmdname, imagefile, strerror(errno));
291 exit (EXIT_FAILURE);
292 }
293
294 /*
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100295 * image_check_hcrc() creates copy of header so that
296 * we can blank out the checksum field for checking -
297 * this can't be done on the PROT_READ mapped data.
wdenk5b1d7132002-11-03 00:07:02 +0000298 */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100299 hdr = (image_header_t *)ptr;
wdenk5b1d7132002-11-03 00:07:02 +0000300
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100301 if (!image_check_magic (hdr)) {
wdenk5b1d7132002-11-03 00:07:02 +0000302 fprintf (stderr,
303 "%s: Bad Magic Number: \"%s\" is no valid image\n",
304 cmdname, imagefile);
305 exit (EXIT_FAILURE);
306 }
307
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100308 if (!image_check_hcrc (hdr)) {
wdenk5b1d7132002-11-03 00:07:02 +0000309 fprintf (stderr,
Wolfgang Denkdce59c52006-04-28 21:24:32 +0200310 "%s: ERROR: \"%s\" has bad header checksum!\n",
311 cmdname, imagefile);
312 exit (EXIT_FAILURE);
wdenk5b1d7132002-11-03 00:07:02 +0000313 }
314
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100315 data = (char *)image_get_data (hdr);
316 len = sbuf.st_size - image_get_header_size ();
wdenk5b1d7132002-11-03 00:07:02 +0000317
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100318 if (crc32(0, data, len) != image_get_dcrc (hdr)) {
wdenk5b1d7132002-11-03 00:07:02 +0000319 fprintf (stderr,
Wolfgang Denkdce59c52006-04-28 21:24:32 +0200320 "%s: ERROR: \"%s\" has corrupted data!\n",
321 cmdname, imagefile);
322 exit (EXIT_FAILURE);
wdenk5b1d7132002-11-03 00:07:02 +0000323 }
324
325 /* for multi-file images we need the data part, too */
326 print_header ((image_header_t *)ptr);
327
328 (void) munmap((void *)ptr, sbuf.st_size);
329 (void) close (ifd);
330
331 exit (EXIT_SUCCESS);
332 }
333
334 /*
335 * Must be -w then:
336 *
337 * write dummy header, to be fixed later
338 */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100339 memset (hdr, 0, image_get_header_size ());
wdenk5b1d7132002-11-03 00:07:02 +0000340
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100341 if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) {
wdenk5b1d7132002-11-03 00:07:02 +0000342 fprintf (stderr, "%s: Write error on %s: %s\n",
343 cmdname, imagefile, strerror(errno));
344 exit (EXIT_FAILURE);
345 }
346
347 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
348 char *file = datafile;
Wolfgang Denk31f14c22006-01-11 13:03:54 +0100349 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000350
351 for (;;) {
352 char *sep = NULL;
353
354 if (file) {
355 if ((sep = strchr(file, ':')) != NULL) {
356 *sep = '\0';
357 }
358
359 if (stat (file, &sbuf) < 0) {
360 fprintf (stderr, "%s: Can't stat %s: %s\n",
361 cmdname, file, strerror(errno));
362 exit (EXIT_FAILURE);
363 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100364 size = cpu_to_image (sbuf.st_size);
wdenk5b1d7132002-11-03 00:07:02 +0000365 } else {
366 size = 0;
367 }
368
369 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
370 fprintf (stderr, "%s: Write error on %s: %s\n",
371 cmdname, imagefile, strerror(errno));
372 exit (EXIT_FAILURE);
373 }
374
375 if (!file) {
376 break;
377 }
378
379 if (sep) {
380 *sep = ':';
381 file = sep + 1;
382 } else {
383 file = NULL;
384 }
385 }
386
387 file = datafile;
388
389 for (;;) {
390 char *sep = strchr(file, ':');
391 if (sep) {
392 *sep = '\0';
393 copy_file (ifd, file, 1);
394 *sep++ = ':';
395 file = sep;
396 } else {
397 copy_file (ifd, file, 0);
398 break;
399 }
400 }
401 } else {
402 copy_file (ifd, datafile, 0);
403 }
404
405 /* We're a bit of paranoid */
Aubrey.Lifecb2a02007-05-14 11:47:35 +0800406#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000407 (void) fdatasync (ifd);
408#else
409 (void) fsync (ifd);
410#endif
411
412 if (fstat(ifd, &sbuf) < 0) {
413 fprintf (stderr, "%s: Can't stat %s: %s\n",
414 cmdname, imagefile, strerror(errno));
415 exit (EXIT_FAILURE);
416 }
417
418 ptr = (unsigned char *)mmap(0, sbuf.st_size,
419 PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
420 if (ptr == (unsigned char *)MAP_FAILED) {
421 fprintf (stderr, "%s: Can't map %s: %s\n",
422 cmdname, imagefile, strerror(errno));
423 exit (EXIT_FAILURE);
424 }
425
426 hdr = (image_header_t *)ptr;
427
428 checksum = crc32 (0,
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100429 (const char *)(ptr + image_get_header_size ()),
430 sbuf.st_size - image_get_header_size ()
wdenk5b1d7132002-11-03 00:07:02 +0000431 );
432
433 /* Build new header */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100434 image_set_magic (hdr, IH_MAGIC);
435 image_set_time (hdr, sbuf.st_mtime);
436 image_set_size (hdr, sbuf.st_size - image_get_header_size ());
437 image_set_load (hdr, addr);
438 image_set_ep (hdr, ep);
439 image_set_dcrc (hdr, checksum);
440 image_set_os (hdr, opt_os);
441 image_set_arch (hdr, opt_arch);
442 image_set_type (hdr, opt_type);
443 image_set_comp (hdr, opt_comp);
wdenk5b1d7132002-11-03 00:07:02 +0000444
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100445 image_set_name (hdr, name);
wdenk5b1d7132002-11-03 00:07:02 +0000446
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100447 checksum = crc32 (0, (const char *)hdr, image_get_header_size ());
wdenk5b1d7132002-11-03 00:07:02 +0000448
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100449 image_set_hcrc (hdr, checksum);
wdenk5b1d7132002-11-03 00:07:02 +0000450
451 print_header (hdr);
452
453 (void) munmap((void *)ptr, sbuf.st_size);
454
455 /* We're a bit of paranoid */
Aubrey.Lifecb2a02007-05-14 11:47:35 +0800456#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000457 (void) fdatasync (ifd);
458#else
459 (void) fsync (ifd);
460#endif
461
462 if (close(ifd)) {
463 fprintf (stderr, "%s: Write error on %s: %s\n",
464 cmdname, imagefile, strerror(errno));
465 exit (EXIT_FAILURE);
466 }
467
468 exit (EXIT_SUCCESS);
469}
470
471static void
472copy_file (int ifd, const char *datafile, int pad)
473{
474 int dfd;
475 struct stat sbuf;
476 unsigned char *ptr;
477 int tail;
478 int zero = 0;
479 int offset = 0;
480 int size;
481
482 if (vflag) {
483 fprintf (stderr, "Adding Image %s\n", datafile);
484 }
485
wdenk1ee550e2003-10-08 22:14:02 +0000486 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000487 fprintf (stderr, "%s: Can't open %s: %s\n",
488 cmdname, datafile, strerror(errno));
489 exit (EXIT_FAILURE);
490 }
491
492 if (fstat(dfd, &sbuf) < 0) {
493 fprintf (stderr, "%s: Can't stat %s: %s\n",
494 cmdname, datafile, strerror(errno));
495 exit (EXIT_FAILURE);
496 }
497
498 ptr = (unsigned char *)mmap(0, sbuf.st_size,
499 PROT_READ, MAP_SHARED, dfd, 0);
500 if (ptr == (unsigned char *)MAP_FAILED) {
501 fprintf (stderr, "%s: Can't read %s: %s\n",
502 cmdname, datafile, strerror(errno));
503 exit (EXIT_FAILURE);
504 }
505
506 if (xflag) {
507 unsigned char *p = NULL;
508 /*
509 * XIP: do not append the image_header_t at the
510 * beginning of the file, but consume the space
511 * reserved for it.
512 */
513
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100514 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
wdenk5b1d7132002-11-03 00:07:02 +0000515 fprintf (stderr,
516 "%s: Bad size: \"%s\" is too small for XIP\n",
517 cmdname, datafile);
518 exit (EXIT_FAILURE);
519 }
520
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100521 for (p = ptr; p < ptr + image_get_header_size (); p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000522 if ( *p != 0xff ) {
523 fprintf (stderr,
524 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
525 cmdname, datafile);
526 exit (EXIT_FAILURE);
527 }
528 }
529
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100530 offset = image_get_header_size ();
wdenk5b1d7132002-11-03 00:07:02 +0000531 }
532
533 size = sbuf.st_size - offset;
534 if (write(ifd, ptr + offset, size) != size) {
535 fprintf (stderr, "%s: Write error on %s: %s\n",
536 cmdname, imagefile, strerror(errno));
537 exit (EXIT_FAILURE);
538 }
539
540 if (pad && ((tail = size % 4) != 0)) {
541
542 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
543 fprintf (stderr, "%s: Write error on %s: %s\n",
544 cmdname, imagefile, strerror(errno));
545 exit (EXIT_FAILURE);
546 }
547 }
548
549 (void) munmap((void *)ptr, sbuf.st_size);
550 (void) close (dfd);
551}
552
553void
554usage ()
555{
556 fprintf (stderr, "Usage: %s -l image\n"
557 " -l ==> list image header information\n"
wdenk20dd2fa2004-11-21 00:06:33 +0000558 " %s [-x] -A arch -O os -T type -C comp "
wdenk5b1d7132002-11-03 00:07:02 +0000559 "-a addr -e ep -n name -d data_file[:data_file...] image\n",
560 cmdname, cmdname);
561 fprintf (stderr, " -A ==> set architecture to 'arch'\n"
562 " -O ==> set operating system to 'os'\n"
563 " -T ==> set image type to 'type'\n"
564 " -C ==> set compression type 'comp'\n"
565 " -a ==> set load address to 'addr' (hex)\n"
566 " -e ==> set entry point to 'ep' (hex)\n"
567 " -n ==> set image name to 'name'\n"
568 " -d ==> use image data from 'datafile'\n"
569 " -x ==> set XIP (execute in place)\n"
570 );
571 exit (EXIT_FAILURE);
572}
573
574static void
575print_header (image_header_t *hdr)
576{
577 time_t timestamp;
578 uint32_t size;
579
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100580 timestamp = (time_t)image_get_time (hdr);
581 size = image_get_data_size (hdr);
wdenk5b1d7132002-11-03 00:07:02 +0000582
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100583 printf ("Image Name: %.*s\n", IH_NMLEN, image_get_name (hdr));
wdenk5b1d7132002-11-03 00:07:02 +0000584 printf ("Created: %s", ctime(&timestamp));
585 printf ("Image Type: "); print_type(hdr);
586 printf ("Data Size: %d Bytes = %.2f kB = %.2f MB\n",
587 size, (double)size / 1.024e3, (double)size / 1.048576e6 );
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100588 printf ("Load Address: 0x%08X\n", image_get_load (hdr));
589 printf ("Entry Point: 0x%08X\n", image_get_ep (hdr));
wdenk5b1d7132002-11-03 00:07:02 +0000590
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100591 if (image_check_type (hdr, IH_TYPE_MULTI) ||
592 image_check_type (hdr, IH_TYPE_SCRIPT)) {
wdenk5b1d7132002-11-03 00:07:02 +0000593 int i, ptrs;
594 uint32_t pos;
Wolfgang Denk283a81e2006-09-06 23:29:15 +0200595 uint32_t *len_ptr = (uint32_t *) (
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100596 (unsigned long)hdr + image_get_header_size ()
wdenk5b1d7132002-11-03 00:07:02 +0000597 );
598
599 /* determine number of images first (to calculate image offsets) */
600 for (i=0; len_ptr[i]; ++i) /* null pointer terminates list */
601 ;
602 ptrs = i; /* null pointer terminates list */
603
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100604 pos = image_get_header_size () + ptrs * sizeof(long);
wdenk5b1d7132002-11-03 00:07:02 +0000605 printf ("Contents:\n");
606 for (i=0; len_ptr[i]; ++i) {
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100607 size = image_to_cpu (len_ptr[i]);
wdenk5b1d7132002-11-03 00:07:02 +0000608
609 printf (" Image %d: %8d Bytes = %4d kB = %d MB\n",
610 i, size, size>>10, size>>20);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100611 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000612 /*
613 * the user may need to know offsets
614 * if planning to do something with
615 * multiple files
616 */
wdenk0a658552003-08-05 17:43:17 +0000617 printf (" Offset = %08X\n", pos);
wdenk5b1d7132002-11-03 00:07:02 +0000618 }
619 /* copy_file() will pad the first files to even word align */
620 size += 3;
621 size &= ~3;
622 pos += size;
623 }
624 }
625}
626
627
628static void
629print_type (image_header_t *hdr)
630{
631 printf ("%s %s %s (%s)\n",
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100632 put_arch (image_get_arch (hdr)),
633 put_os (image_get_os (hdr)),
634 put_type (image_get_type (hdr)),
635 put_comp (image_get_comp (hdr))
wdenk5b1d7132002-11-03 00:07:02 +0000636 );
637}
638
639static char *put_arch (int arch)
640{
641 return (put_table_entry(arch_name, "Unknown Architecture", arch));
642}
643
644static char *put_os (int os)
645{
646 return (put_table_entry(os_name, "Unknown OS", os));
647}
648
649static char *put_type (int type)
650{
651 return (put_table_entry(type_name, "Unknown Image", type));
652}
653
654static char *put_comp (int comp)
655{
656 return (put_table_entry(comp_name, "Unknown Compression", comp));
657}
658
659static char *put_table_entry (table_entry_t *table, char *msg, int type)
660{
661 for (; table->val>=0; ++table) {
662 if (table->val == type)
663 return (table->lname);
664 }
665 return (msg);
666}
667
668static int get_arch(char *name)
669{
670 return (get_table_entry(arch_name, "CPU", name));
671}
672
673
674static int get_comp(char *name)
675{
676 return (get_table_entry(comp_name, "Compression", name));
677}
678
679
680static int get_os (char *name)
681{
682 return (get_table_entry(os_name, "OS", name));
683}
684
685
686static int get_type(char *name)
687{
688 return (get_table_entry(type_name, "Image", name));
689}
690
691static int get_table_entry (table_entry_t *table, char *msg, char *name)
692{
693 table_entry_t *t;
694 int first = 1;
695
696 for (t=table; t->val>=0; ++t) {
697 if (t->sname && strcasecmp(t->sname, name)==0)
698 return (t->val);
699 }
700 fprintf (stderr, "\nInvalid %s Type - valid names are", msg);
701 for (t=table; t->val>=0; ++t) {
702 if (t->sname == NULL)
703 continue;
704 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
705 first = 0;
706 }
707 fprintf (stderr, "\n");
708 return (-1);
709}