mkimage.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * (C) Copyright 2000-2004
  3. * DENX Software Engineering
  4. * Wolfgang Denk, wd@denx.de
  5. * All rights reserved.
  6. *
  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
  21. */
  22. #ifdef __APPLE__
  23. #define __FreeBSD__ 10
  24. #endif
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #ifndef __WIN32__
  31. #include <netinet/in.h> /* for host / network byte order conversions */
  32. #endif
  33. #include <sys/mman.h>
  34. #include <sys/stat.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37. #if defined(__BEOS__) || defined(__NetBSD__) || defined(__APPLE__)
  38. #include <inttypes.h>
  39. #include <sys/types.h>
  40. #endif
  41. #ifdef __WIN32__
  42. typedef unsigned int __u32;
  43. #define SWAP_LONG(x) \
  44. ((__u32)( \
  45. (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
  46. (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
  47. (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
  48. (((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
  49. typedef unsigned char uint8_t;
  50. typedef unsigned short uint16_t;
  51. typedef unsigned int uint32_t;
  52. #define ntohl(a) SWAP_LONG(a)
  53. #define htonl(a) SWAP_LONG(a)
  54. #endif /* __WIN32__ */
  55. #ifndef O_BINARY /* should be define'd on __WIN32__ */
  56. #define O_BINARY 0
  57. #endif
  58. #include "image.h"
  59. extern int errno;
  60. #ifndef MAP_FAILED
  61. #define MAP_FAILED (-1)
  62. #endif
  63. char *cmdname;
  64. extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
  65. typedef struct table_entry {
  66. int val; /* as defined in image.h */
  67. char *sname; /* short (input) name */
  68. char *lname; /* long (output) name */
  69. } table_entry_t;
  70. table_entry_t arch_name[] = {
  71. { IH_CPU_INVALID, NULL, "Invalid CPU", },
  72. { IH_CPU_ALPHA, "alpha", "Alpha", },
  73. { IH_CPU_ARM, "arm", "ARM", },
  74. { IH_CPU_I386, "x86", "Intel x86", },
  75. { IH_CPU_IA64, "ia64", "IA64", },
  76. { IH_CPU_M68K, "m68k", "MC68000", },
  77. { IH_CPU_MICROBLAZE, "microblaze", "MicroBlaze", },
  78. { IH_CPU_MIPS, "mips", "MIPS", },
  79. { IH_CPU_MIPS64, "mips64", "MIPS 64 Bit", },
  80. { IH_CPU_NIOS, "nios", "NIOS", },
  81. { IH_CPU_NIOS2, "nios2", "NIOS II", },
  82. { IH_CPU_PPC, "ppc", "PowerPC", },
  83. { IH_CPU_S390, "s390", "IBM S390", },
  84. { IH_CPU_SH, "sh", "SuperH", },
  85. { IH_CPU_SPARC, "sparc", "SPARC", },
  86. { IH_CPU_SPARC64, "sparc64", "SPARC 64 Bit", },
  87. { IH_CPU_BLACKFIN, "blackfin", "Blackfin", },
  88. { IH_CPU_AVR32, "avr32", "AVR32", },
  89. { IH_CPU_XTENSA, "xtensa", "XTENSA", },
  90. { -1, "", "", },
  91. };
  92. table_entry_t os_name[] = {
  93. { IH_OS_INVALID, NULL, "Invalid OS", },
  94. { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
  95. { IH_OS_ARTOS, "artos", "ARTOS", },
  96. { IH_OS_DELL, "dell", "Dell", },
  97. { IH_OS_ESIX, "esix", "Esix", },
  98. { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
  99. { IH_OS_IRIX, "irix", "Irix", },
  100. { IH_OS_LINUX, "linux", "Linux", },
  101. { IH_OS_LYNXOS, "lynxos", "LynxOS", },
  102. { IH_OS_NCR, "ncr", "NCR", },
  103. { IH_OS_NETBSD, "netbsd", "NetBSD", },
  104. { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
  105. { IH_OS_PSOS, "psos", "pSOS", },
  106. { IH_OS_QNX, "qnx", "QNX", },
  107. { IH_OS_RTEMS, "rtems", "RTEMS", },
  108. { IH_OS_SCO, "sco", "SCO", },
  109. { IH_OS_SOLARIS, "solaris", "Solaris", },
  110. { IH_OS_SVR4, "svr4", "SVR4", },
  111. { IH_OS_U_BOOT, "u-boot", "U-Boot", },
  112. { IH_OS_VXWORKS, "vxworks", "VxWorks", },
  113. { -1, "", "", },
  114. };
  115. table_entry_t type_name[] = {
  116. { IH_TYPE_INVALID, NULL, "Invalid Image", },
  117. { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
  118. { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
  119. { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
  120. { IH_TYPE_MULTI, "multi", "Multi-File Image", },
  121. { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
  122. { IH_TYPE_SCRIPT, "script", "Script", },
  123. { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
  124. { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
  125. { -1, "", "", },
  126. };
  127. table_entry_t comp_name[] = {
  128. { IH_COMP_NONE, "none", "uncompressed", },
  129. { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
  130. { IH_COMP_GZIP, "gzip", "gzip compressed", },
  131. { IH_COMP_LZMA, "lzma", "lzma compressed", },
  132. { -1, "", "", },
  133. };
  134. static void copy_file (int, const char *, int);
  135. static void usage (void);
  136. static void print_header (image_header_t *);
  137. static void print_type (image_header_t *);
  138. static char *put_table_entry (table_entry_t *, char *, int);
  139. static char *put_arch (int);
  140. static char *put_type (int);
  141. static char *put_os (int);
  142. static char *put_comp (int);
  143. static int get_table_entry (table_entry_t *, char *, char *);
  144. static int get_arch(char *);
  145. static int get_comp(char *);
  146. static int get_os (char *);
  147. static int get_type(char *);
  148. char *datafile;
  149. char *imagefile;
  150. int dflag = 0;
  151. int eflag = 0;
  152. int lflag = 0;
  153. int vflag = 0;
  154. int xflag = 0;
  155. int opt_os = IH_OS_LINUX;
  156. int opt_arch = IH_CPU_PPC;
  157. int opt_type = IH_TYPE_KERNEL;
  158. int opt_comp = IH_COMP_GZIP;
  159. image_header_t header;
  160. image_header_t *hdr = &header;
  161. int
  162. main (int argc, char **argv)
  163. {
  164. int ifd;
  165. uint32_t checksum;
  166. uint32_t addr;
  167. uint32_t ep;
  168. struct stat sbuf;
  169. unsigned char *ptr;
  170. char *name = "";
  171. cmdname = *argv;
  172. addr = ep = 0;
  173. while (--argc > 0 && **++argv == '-') {
  174. while (*++*argv) {
  175. switch (**argv) {
  176. case 'l':
  177. lflag = 1;
  178. break;
  179. case 'A':
  180. if ((--argc <= 0) ||
  181. (opt_arch = get_arch(*++argv)) < 0)
  182. usage ();
  183. goto NXTARG;
  184. case 'C':
  185. if ((--argc <= 0) ||
  186. (opt_comp = get_comp(*++argv)) < 0)
  187. usage ();
  188. goto NXTARG;
  189. case 'O':
  190. if ((--argc <= 0) ||
  191. (opt_os = get_os(*++argv)) < 0)
  192. usage ();
  193. goto NXTARG;
  194. case 'T':
  195. if ((--argc <= 0) ||
  196. (opt_type = get_type(*++argv)) < 0)
  197. usage ();
  198. goto NXTARG;
  199. case 'a':
  200. if (--argc <= 0)
  201. usage ();
  202. addr = strtoul (*++argv, (char **)&ptr, 16);
  203. if (*ptr) {
  204. fprintf (stderr,
  205. "%s: invalid load address %s\n",
  206. cmdname, *argv);
  207. exit (EXIT_FAILURE);
  208. }
  209. goto NXTARG;
  210. case 'd':
  211. if (--argc <= 0)
  212. usage ();
  213. datafile = *++argv;
  214. dflag = 1;
  215. goto NXTARG;
  216. case 'e':
  217. if (--argc <= 0)
  218. usage ();
  219. ep = strtoul (*++argv, (char **)&ptr, 16);
  220. if (*ptr) {
  221. fprintf (stderr,
  222. "%s: invalid entry point %s\n",
  223. cmdname, *argv);
  224. exit (EXIT_FAILURE);
  225. }
  226. eflag = 1;
  227. goto NXTARG;
  228. case 'n':
  229. if (--argc <= 0)
  230. usage ();
  231. name = *++argv;
  232. goto NXTARG;
  233. case 'v':
  234. vflag++;
  235. break;
  236. case 'x':
  237. xflag++;
  238. break;
  239. default:
  240. usage ();
  241. }
  242. }
  243. NXTARG: ;
  244. }
  245. if ((argc != 1) || ((lflag ^ dflag) == 0))
  246. usage();
  247. if (!eflag) {
  248. ep = addr;
  249. /* If XIP, entry point must be after the U-Boot header */
  250. if (xflag)
  251. ep += sizeof(image_header_t);
  252. }
  253. /*
  254. * If XIP, ensure the entry point is equal to the load address plus
  255. * the size of the U-Boot header.
  256. */
  257. if (xflag) {
  258. if (ep != addr + sizeof(image_header_t)) {
  259. fprintf (stderr,
  260. "%s: For XIP, the entry point must be the load addr + %lu\n",
  261. cmdname,
  262. (unsigned long)sizeof(image_header_t));
  263. exit (EXIT_FAILURE);
  264. }
  265. }
  266. imagefile = *argv;
  267. if (lflag) {
  268. ifd = open(imagefile, O_RDONLY|O_BINARY);
  269. } else {
  270. ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
  271. }
  272. if (ifd < 0) {
  273. fprintf (stderr, "%s: Can't open %s: %s\n",
  274. cmdname, imagefile, strerror(errno));
  275. exit (EXIT_FAILURE);
  276. }
  277. if (lflag) {
  278. int len;
  279. char *data;
  280. /*
  281. * list header information of existing image
  282. */
  283. if (fstat(ifd, &sbuf) < 0) {
  284. fprintf (stderr, "%s: Can't stat %s: %s\n",
  285. cmdname, imagefile, strerror(errno));
  286. exit (EXIT_FAILURE);
  287. }
  288. if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
  289. fprintf (stderr,
  290. "%s: Bad size: \"%s\" is no valid image\n",
  291. cmdname, imagefile);
  292. exit (EXIT_FAILURE);
  293. }
  294. ptr = (unsigned char *)mmap(0, sbuf.st_size,
  295. PROT_READ, MAP_SHARED, ifd, 0);
  296. if ((caddr_t)ptr == (caddr_t)-1) {
  297. fprintf (stderr, "%s: Can't read %s: %s\n",
  298. cmdname, imagefile, strerror(errno));
  299. exit (EXIT_FAILURE);
  300. }
  301. /*
  302. * create copy of header so that we can blank out the
  303. * checksum field for checking - this can't be done
  304. * on the PROT_READ mapped data.
  305. */
  306. memcpy (hdr, ptr, sizeof(image_header_t));
  307. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  308. fprintf (stderr,
  309. "%s: Bad Magic Number: \"%s\" is no valid image\n",
  310. cmdname, imagefile);
  311. exit (EXIT_FAILURE);
  312. }
  313. data = (char *)hdr;
  314. len = sizeof(image_header_t);
  315. checksum = ntohl(hdr->ih_hcrc);
  316. hdr->ih_hcrc = htonl(0); /* clear for re-calculation */
  317. if (crc32 (0, data, len) != checksum) {
  318. fprintf (stderr,
  319. "%s: ERROR: \"%s\" has bad header checksum!\n",
  320. cmdname, imagefile);
  321. exit (EXIT_FAILURE);
  322. }
  323. data = (char *)(ptr + sizeof(image_header_t));
  324. len = sbuf.st_size - sizeof(image_header_t) ;
  325. if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) {
  326. fprintf (stderr,
  327. "%s: ERROR: \"%s\" has corrupted data!\n",
  328. cmdname, imagefile);
  329. exit (EXIT_FAILURE);
  330. }
  331. /* for multi-file images we need the data part, too */
  332. print_header ((image_header_t *)ptr);
  333. (void) munmap((void *)ptr, sbuf.st_size);
  334. (void) close (ifd);
  335. exit (EXIT_SUCCESS);
  336. }
  337. /*
  338. * Must be -w then:
  339. *
  340. * write dummy header, to be fixed later
  341. */
  342. memset (hdr, 0, sizeof(image_header_t));
  343. if (write(ifd, hdr, sizeof(image_header_t)) != sizeof(image_header_t)) {
  344. fprintf (stderr, "%s: Write error on %s: %s\n",
  345. cmdname, imagefile, strerror(errno));
  346. exit (EXIT_FAILURE);
  347. }
  348. if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
  349. char *file = datafile;
  350. uint32_t size;
  351. for (;;) {
  352. char *sep = NULL;
  353. if (file) {
  354. if ((sep = strchr(file, ':')) != NULL) {
  355. *sep = '\0';
  356. }
  357. if (stat (file, &sbuf) < 0) {
  358. fprintf (stderr, "%s: Can't stat %s: %s\n",
  359. cmdname, file, strerror(errno));
  360. exit (EXIT_FAILURE);
  361. }
  362. size = htonl(sbuf.st_size);
  363. } else {
  364. size = 0;
  365. }
  366. if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
  367. fprintf (stderr, "%s: Write error on %s: %s\n",
  368. cmdname, imagefile, strerror(errno));
  369. exit (EXIT_FAILURE);
  370. }
  371. if (!file) {
  372. break;
  373. }
  374. if (sep) {
  375. *sep = ':';
  376. file = sep + 1;
  377. } else {
  378. file = NULL;
  379. }
  380. }
  381. file = datafile;
  382. for (;;) {
  383. char *sep = strchr(file, ':');
  384. if (sep) {
  385. *sep = '\0';
  386. copy_file (ifd, file, 1);
  387. *sep++ = ':';
  388. file = sep;
  389. } else {
  390. copy_file (ifd, file, 0);
  391. break;
  392. }
  393. }
  394. } else {
  395. copy_file (ifd, datafile, 0);
  396. }
  397. /* We're a bit of paranoid */
  398. #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
  399. (void) fdatasync (ifd);
  400. #else
  401. (void) fsync (ifd);
  402. #endif
  403. if (fstat(ifd, &sbuf) < 0) {
  404. fprintf (stderr, "%s: Can't stat %s: %s\n",
  405. cmdname, imagefile, strerror(errno));
  406. exit (EXIT_FAILURE);
  407. }
  408. ptr = (unsigned char *)mmap(0, sbuf.st_size,
  409. PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
  410. if (ptr == (unsigned char *)MAP_FAILED) {
  411. fprintf (stderr, "%s: Can't map %s: %s\n",
  412. cmdname, imagefile, strerror(errno));
  413. exit (EXIT_FAILURE);
  414. }
  415. hdr = (image_header_t *)ptr;
  416. checksum = crc32 (0,
  417. (const char *)(ptr + sizeof(image_header_t)),
  418. sbuf.st_size - sizeof(image_header_t)
  419. );
  420. /* Build new header */
  421. hdr->ih_magic = htonl(IH_MAGIC);
  422. hdr->ih_time = htonl(sbuf.st_mtime);
  423. hdr->ih_size = htonl(sbuf.st_size - sizeof(image_header_t));
  424. hdr->ih_load = htonl(addr);
  425. hdr->ih_ep = htonl(ep);
  426. hdr->ih_dcrc = htonl(checksum);
  427. hdr->ih_os = opt_os;
  428. hdr->ih_arch = opt_arch;
  429. hdr->ih_type = opt_type;
  430. hdr->ih_comp = opt_comp;
  431. strncpy((char *)hdr->ih_name, name, IH_NMLEN);
  432. checksum = crc32(0,(const char *)hdr,sizeof(image_header_t));
  433. hdr->ih_hcrc = htonl(checksum);
  434. print_header (hdr);
  435. (void) munmap((void *)ptr, sbuf.st_size);
  436. /* We're a bit of paranoid */
  437. #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
  438. (void) fdatasync (ifd);
  439. #else
  440. (void) fsync (ifd);
  441. #endif
  442. if (close(ifd)) {
  443. fprintf (stderr, "%s: Write error on %s: %s\n",
  444. cmdname, imagefile, strerror(errno));
  445. exit (EXIT_FAILURE);
  446. }
  447. exit (EXIT_SUCCESS);
  448. }
  449. static void
  450. copy_file (int ifd, const char *datafile, int pad)
  451. {
  452. int dfd;
  453. struct stat sbuf;
  454. unsigned char *ptr;
  455. int tail;
  456. int zero = 0;
  457. int offset = 0;
  458. int size;
  459. if (vflag) {
  460. fprintf (stderr, "Adding Image %s\n", datafile);
  461. }
  462. if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
  463. fprintf (stderr, "%s: Can't open %s: %s\n",
  464. cmdname, datafile, strerror(errno));
  465. exit (EXIT_FAILURE);
  466. }
  467. if (fstat(dfd, &sbuf) < 0) {
  468. fprintf (stderr, "%s: Can't stat %s: %s\n",
  469. cmdname, datafile, strerror(errno));
  470. exit (EXIT_FAILURE);
  471. }
  472. ptr = (unsigned char *)mmap(0, sbuf.st_size,
  473. PROT_READ, MAP_SHARED, dfd, 0);
  474. if (ptr == (unsigned char *)MAP_FAILED) {
  475. fprintf (stderr, "%s: Can't read %s: %s\n",
  476. cmdname, datafile, strerror(errno));
  477. exit (EXIT_FAILURE);
  478. }
  479. if (xflag) {
  480. unsigned char *p = NULL;
  481. /*
  482. * XIP: do not append the image_header_t at the
  483. * beginning of the file, but consume the space
  484. * reserved for it.
  485. */
  486. if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
  487. fprintf (stderr,
  488. "%s: Bad size: \"%s\" is too small for XIP\n",
  489. cmdname, datafile);
  490. exit (EXIT_FAILURE);
  491. }
  492. for (p=ptr; p < ptr+sizeof(image_header_t); p++) {
  493. if ( *p != 0xff ) {
  494. fprintf (stderr,
  495. "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
  496. cmdname, datafile);
  497. exit (EXIT_FAILURE);
  498. }
  499. }
  500. offset = sizeof(image_header_t);
  501. }
  502. size = sbuf.st_size - offset;
  503. if (write(ifd, ptr + offset, size) != size) {
  504. fprintf (stderr, "%s: Write error on %s: %s\n",
  505. cmdname, imagefile, strerror(errno));
  506. exit (EXIT_FAILURE);
  507. }
  508. if (pad && ((tail = size % 4) != 0)) {
  509. if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  510. fprintf (stderr, "%s: Write error on %s: %s\n",
  511. cmdname, imagefile, strerror(errno));
  512. exit (EXIT_FAILURE);
  513. }
  514. }
  515. (void) munmap((void *)ptr, sbuf.st_size);
  516. (void) close (dfd);
  517. }
  518. void
  519. usage ()
  520. {
  521. fprintf (stderr, "Usage: %s -l image\n"
  522. " -l ==> list image header information\n"
  523. " %s [-x] -A arch -O os -T type -C comp "
  524. "-a addr -e ep -n name -d data_file[:data_file...] image\n",
  525. cmdname, cmdname);
  526. fprintf (stderr, " -A ==> set architecture to 'arch'\n"
  527. " -O ==> set operating system to 'os'\n"
  528. " -T ==> set image type to 'type'\n"
  529. " -C ==> set compression type 'comp'\n"
  530. " -a ==> set load address to 'addr' (hex)\n"
  531. " -e ==> set entry point to 'ep' (hex)\n"
  532. " -n ==> set image name to 'name'\n"
  533. " -d ==> use image data from 'datafile'\n"
  534. " -x ==> set XIP (execute in place)\n"
  535. );
  536. exit (EXIT_FAILURE);
  537. }
  538. static void
  539. print_header (image_header_t *hdr)
  540. {
  541. time_t timestamp;
  542. uint32_t size;
  543. timestamp = (time_t)ntohl(hdr->ih_time);
  544. size = ntohl(hdr->ih_size);
  545. printf ("Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
  546. printf ("Created: %s", ctime(&timestamp));
  547. printf ("Image Type: "); print_type(hdr);
  548. printf ("Data Size: %d Bytes = %.2f kB = %.2f MB\n",
  549. size, (double)size / 1.024e3, (double)size / 1.048576e6 );
  550. printf ("Load Address: 0x%08X\n", ntohl(hdr->ih_load));
  551. printf ("Entry Point: 0x%08X\n", ntohl(hdr->ih_ep));
  552. if (hdr->ih_type == IH_TYPE_MULTI || hdr->ih_type == IH_TYPE_SCRIPT) {
  553. int i, ptrs;
  554. uint32_t pos;
  555. uint32_t *len_ptr = (uint32_t *) (
  556. (unsigned long)hdr + sizeof(image_header_t)
  557. );
  558. /* determine number of images first (to calculate image offsets) */
  559. for (i=0; len_ptr[i]; ++i) /* null pointer terminates list */
  560. ;
  561. ptrs = i; /* null pointer terminates list */
  562. pos = sizeof(image_header_t) + ptrs * sizeof(long);
  563. printf ("Contents:\n");
  564. for (i=0; len_ptr[i]; ++i) {
  565. size = ntohl(len_ptr[i]);
  566. printf (" Image %d: %8d Bytes = %4d kB = %d MB\n",
  567. i, size, size>>10, size>>20);
  568. if (hdr->ih_type == IH_TYPE_SCRIPT && i > 0) {
  569. /*
  570. * the user may need to know offsets
  571. * if planning to do something with
  572. * multiple files
  573. */
  574. printf (" Offset = %08X\n", pos);
  575. }
  576. /* copy_file() will pad the first files to even word align */
  577. size += 3;
  578. size &= ~3;
  579. pos += size;
  580. }
  581. }
  582. }
  583. static void
  584. print_type (image_header_t *hdr)
  585. {
  586. printf ("%s %s %s (%s)\n",
  587. put_arch (hdr->ih_arch),
  588. put_os (hdr->ih_os ),
  589. put_type (hdr->ih_type),
  590. put_comp (hdr->ih_comp)
  591. );
  592. }
  593. static char *put_arch (int arch)
  594. {
  595. return (put_table_entry(arch_name, "Unknown Architecture", arch));
  596. }
  597. static char *put_os (int os)
  598. {
  599. return (put_table_entry(os_name, "Unknown OS", os));
  600. }
  601. static char *put_type (int type)
  602. {
  603. return (put_table_entry(type_name, "Unknown Image", type));
  604. }
  605. static char *put_comp (int comp)
  606. {
  607. return (put_table_entry(comp_name, "Unknown Compression", comp));
  608. }
  609. static char *put_table_entry (table_entry_t *table, char *msg, int type)
  610. {
  611. for (; table->val>=0; ++table) {
  612. if (table->val == type)
  613. return (table->lname);
  614. }
  615. return (msg);
  616. }
  617. static int get_arch(char *name)
  618. {
  619. return (get_table_entry(arch_name, "CPU", name));
  620. }
  621. static int get_comp(char *name)
  622. {
  623. return (get_table_entry(comp_name, "Compression", name));
  624. }
  625. static int get_os (char *name)
  626. {
  627. return (get_table_entry(os_name, "OS", name));
  628. }
  629. static int get_type(char *name)
  630. {
  631. return (get_table_entry(type_name, "Image", name));
  632. }
  633. static int get_table_entry (table_entry_t *table, char *msg, char *name)
  634. {
  635. table_entry_t *t;
  636. int first = 1;
  637. for (t=table; t->val>=0; ++t) {
  638. if (t->sname && strcasecmp(t->sname, name)==0)
  639. return (t->val);
  640. }
  641. fprintf (stderr, "\nInvalid %s Type - valid names are", msg);
  642. for (t=table; t->val>=0; ++t) {
  643. if (t->sname == NULL)
  644. continue;
  645. fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
  646. first = 0;
  647. }
  648. fprintf (stderr, "\n");
  649. return (-1);
  650. }