mkimage.c 18 KB

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