mkimage.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
  119. { -1, "", "", },
  120. };
  121. table_entry_t comp_name[] = {
  122. { IH_COMP_NONE, "none", "uncompressed", },
  123. { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
  124. { IH_COMP_GZIP, "gzip", "gzip compressed", },
  125. { -1, "", "", },
  126. };
  127. static void copy_file (int, const char *, int);
  128. static void usage (void);
  129. static void print_header (image_header_t *);
  130. static void print_type (image_header_t *);
  131. static char *put_table_entry (table_entry_t *, char *, int);
  132. static char *put_arch (int);
  133. static char *put_type (int);
  134. static char *put_os (int);
  135. static char *put_comp (int);
  136. static int get_table_entry (table_entry_t *, char *, char *);
  137. static int get_arch(char *);
  138. static int get_comp(char *);
  139. static int get_os (char *);
  140. static int get_type(char *);
  141. char *datafile;
  142. char *imagefile;
  143. int dflag = 0;
  144. int eflag = 0;
  145. int lflag = 0;
  146. int vflag = 0;
  147. int xflag = 0;
  148. int opt_os = IH_OS_LINUX;
  149. int opt_arch = IH_CPU_PPC;
  150. int opt_type = IH_TYPE_KERNEL;
  151. int opt_comp = IH_COMP_GZIP;
  152. image_header_t header;
  153. image_header_t *hdr = &header;
  154. int
  155. main (int argc, char **argv)
  156. {
  157. int ifd;
  158. uint32_t checksum;
  159. uint32_t addr;
  160. uint32_t ep;
  161. struct stat sbuf;
  162. unsigned char *ptr;
  163. char *name = "";
  164. cmdname = *argv;
  165. addr = ep = 0;
  166. while (--argc > 0 && **++argv == '-') {
  167. while (*++*argv) {
  168. switch (**argv) {
  169. case 'l':
  170. lflag = 1;
  171. break;
  172. case 'A':
  173. if ((--argc <= 0) ||
  174. (opt_arch = get_arch(*++argv)) < 0)
  175. usage ();
  176. goto NXTARG;
  177. case 'C':
  178. if ((--argc <= 0) ||
  179. (opt_comp = get_comp(*++argv)) < 0)
  180. usage ();
  181. goto NXTARG;
  182. case 'O':
  183. if ((--argc <= 0) ||
  184. (opt_os = get_os(*++argv)) < 0)
  185. usage ();
  186. goto NXTARG;
  187. case 'T':
  188. if ((--argc <= 0) ||
  189. (opt_type = get_type(*++argv)) < 0)
  190. usage ();
  191. goto NXTARG;
  192. case 'a':
  193. if (--argc <= 0)
  194. usage ();
  195. addr = strtoul (*++argv, (char **)&ptr, 16);
  196. if (*ptr) {
  197. fprintf (stderr,
  198. "%s: invalid load address %s\n",
  199. cmdname, *argv);
  200. exit (EXIT_FAILURE);
  201. }
  202. goto NXTARG;
  203. case 'd':
  204. if (--argc <= 0)
  205. usage ();
  206. datafile = *++argv;
  207. dflag = 1;
  208. goto NXTARG;
  209. case 'e':
  210. if (--argc <= 0)
  211. usage ();
  212. ep = strtoul (*++argv, (char **)&ptr, 16);
  213. if (*ptr) {
  214. fprintf (stderr,
  215. "%s: invalid entry point %s\n",
  216. cmdname, *argv);
  217. exit (EXIT_FAILURE);
  218. }
  219. eflag = 1;
  220. goto NXTARG;
  221. case 'n':
  222. if (--argc <= 0)
  223. usage ();
  224. name = *++argv;
  225. goto NXTARG;
  226. case 'v':
  227. vflag++;
  228. break;
  229. case 'x':
  230. xflag++;
  231. break;
  232. default:
  233. usage ();
  234. }
  235. }
  236. NXTARG: ;
  237. }
  238. if ((argc != 1) || ((lflag ^ dflag) == 0))
  239. usage();
  240. if (!eflag) {
  241. ep = addr;
  242. /* If XIP, entry point must be after the U-Boot header */
  243. if (xflag)
  244. ep += sizeof(image_header_t);
  245. }
  246. /*
  247. * If XIP, ensure the entry point is equal to the load address plus
  248. * the size of the U-Boot header.
  249. */
  250. if (xflag) {
  251. if (ep != addr + sizeof(image_header_t)) {
  252. fprintf (stderr,
  253. "%s: For XIP, the entry point must be the load addr + %lu\n",
  254. cmdname,
  255. (unsigned long)sizeof(image_header_t));
  256. exit (EXIT_FAILURE);
  257. }
  258. }
  259. imagefile = *argv;
  260. if (lflag) {
  261. ifd = open(imagefile, O_RDONLY|O_BINARY);
  262. } else {
  263. ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
  264. }
  265. if (ifd < 0) {
  266. fprintf (stderr, "%s: Can't open %s: %s\n",
  267. cmdname, imagefile, strerror(errno));
  268. exit (EXIT_FAILURE);
  269. }
  270. if (lflag) {
  271. int len;
  272. char *data;
  273. /*
  274. * list header information of existing image
  275. */
  276. if (fstat(ifd, &sbuf) < 0) {
  277. fprintf (stderr, "%s: Can't stat %s: %s\n",
  278. cmdname, imagefile, strerror(errno));
  279. exit (EXIT_FAILURE);
  280. }
  281. if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
  282. fprintf (stderr,
  283. "%s: Bad size: \"%s\" is no valid image\n",
  284. cmdname, imagefile);
  285. exit (EXIT_FAILURE);
  286. }
  287. ptr = (unsigned char *)mmap(0, sbuf.st_size,
  288. PROT_READ, MAP_SHARED, ifd, 0);
  289. if ((caddr_t)ptr == (caddr_t)-1) {
  290. fprintf (stderr, "%s: Can't read %s: %s\n",
  291. cmdname, imagefile, strerror(errno));
  292. exit (EXIT_FAILURE);
  293. }
  294. /*
  295. * create copy of header so that we can blank out the
  296. * checksum field for checking - this can't be done
  297. * on the PROT_READ mapped data.
  298. */
  299. memcpy (hdr, ptr, sizeof(image_header_t));
  300. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  301. fprintf (stderr,
  302. "%s: Bad Magic Number: \"%s\" is no valid image\n",
  303. cmdname, imagefile);
  304. exit (EXIT_FAILURE);
  305. }
  306. data = (char *)hdr;
  307. len = sizeof(image_header_t);
  308. checksum = ntohl(hdr->ih_hcrc);
  309. hdr->ih_hcrc = htonl(0); /* clear for re-calculation */
  310. if (crc32 (0, data, len) != checksum) {
  311. fprintf (stderr,
  312. "%s: ERROR: \"%s\" has bad header checksum!\n",
  313. cmdname, imagefile);
  314. exit (EXIT_FAILURE);
  315. }
  316. data = (char *)(ptr + sizeof(image_header_t));
  317. len = sbuf.st_size - sizeof(image_header_t) ;
  318. if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) {
  319. fprintf (stderr,
  320. "%s: ERROR: \"%s\" has corrupted data!\n",
  321. cmdname, imagefile);
  322. exit (EXIT_FAILURE);
  323. }
  324. /* for multi-file images we need the data part, too */
  325. print_header ((image_header_t *)ptr);
  326. (void) munmap((void *)ptr, sbuf.st_size);
  327. (void) close (ifd);
  328. exit (EXIT_SUCCESS);
  329. }
  330. /*
  331. * Must be -w then:
  332. *
  333. * write dummy header, to be fixed later
  334. */
  335. memset (hdr, 0, sizeof(image_header_t));
  336. if (write(ifd, hdr, sizeof(image_header_t)) != sizeof(image_header_t)) {
  337. fprintf (stderr, "%s: Write error on %s: %s\n",
  338. cmdname, imagefile, strerror(errno));
  339. exit (EXIT_FAILURE);
  340. }
  341. if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
  342. char *file = datafile;
  343. uint32_t size;
  344. for (;;) {
  345. char *sep = NULL;
  346. if (file) {
  347. if ((sep = strchr(file, ':')) != NULL) {
  348. *sep = '\0';
  349. }
  350. if (stat (file, &sbuf) < 0) {
  351. fprintf (stderr, "%s: Can't stat %s: %s\n",
  352. cmdname, file, strerror(errno));
  353. exit (EXIT_FAILURE);
  354. }
  355. size = htonl(sbuf.st_size);
  356. } else {
  357. size = 0;
  358. }
  359. if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
  360. fprintf (stderr, "%s: Write error on %s: %s\n",
  361. cmdname, imagefile, strerror(errno));
  362. exit (EXIT_FAILURE);
  363. }
  364. if (!file) {
  365. break;
  366. }
  367. if (sep) {
  368. *sep = ':';
  369. file = sep + 1;
  370. } else {
  371. file = NULL;
  372. }
  373. }
  374. file = datafile;
  375. for (;;) {
  376. char *sep = strchr(file, ':');
  377. if (sep) {
  378. *sep = '\0';
  379. copy_file (ifd, file, 1);
  380. *sep++ = ':';
  381. file = sep;
  382. } else {
  383. copy_file (ifd, file, 0);
  384. break;
  385. }
  386. }
  387. } else {
  388. copy_file (ifd, datafile, 0);
  389. }
  390. /* We're a bit of paranoid */
  391. #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
  392. (void) fdatasync (ifd);
  393. #else
  394. (void) fsync (ifd);
  395. #endif
  396. if (fstat(ifd, &sbuf) < 0) {
  397. fprintf (stderr, "%s: Can't stat %s: %s\n",
  398. cmdname, imagefile, strerror(errno));
  399. exit (EXIT_FAILURE);
  400. }
  401. ptr = (unsigned char *)mmap(0, sbuf.st_size,
  402. PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
  403. if (ptr == (unsigned char *)MAP_FAILED) {
  404. fprintf (stderr, "%s: Can't map %s: %s\n",
  405. cmdname, imagefile, strerror(errno));
  406. exit (EXIT_FAILURE);
  407. }
  408. hdr = (image_header_t *)ptr;
  409. checksum = crc32 (0,
  410. (const char *)(ptr + sizeof(image_header_t)),
  411. sbuf.st_size - sizeof(image_header_t)
  412. );
  413. /* Build new header */
  414. hdr->ih_magic = htonl(IH_MAGIC);
  415. hdr->ih_time = htonl(sbuf.st_mtime);
  416. hdr->ih_size = htonl(sbuf.st_size - sizeof(image_header_t));
  417. hdr->ih_load = htonl(addr);
  418. hdr->ih_ep = htonl(ep);
  419. hdr->ih_dcrc = htonl(checksum);
  420. hdr->ih_os = opt_os;
  421. hdr->ih_arch = opt_arch;
  422. hdr->ih_type = opt_type;
  423. hdr->ih_comp = opt_comp;
  424. strncpy((char *)hdr->ih_name, name, IH_NMLEN);
  425. checksum = crc32(0,(const char *)hdr,sizeof(image_header_t));
  426. hdr->ih_hcrc = htonl(checksum);
  427. print_header (hdr);
  428. (void) munmap((void *)ptr, sbuf.st_size);
  429. /* We're a bit of paranoid */
  430. #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
  431. (void) fdatasync (ifd);
  432. #else
  433. (void) fsync (ifd);
  434. #endif
  435. if (close(ifd)) {
  436. fprintf (stderr, "%s: Write error on %s: %s\n",
  437. cmdname, imagefile, strerror(errno));
  438. exit (EXIT_FAILURE);
  439. }
  440. exit (EXIT_SUCCESS);
  441. }
  442. static void
  443. copy_file (int ifd, const char *datafile, int pad)
  444. {
  445. int dfd;
  446. struct stat sbuf;
  447. unsigned char *ptr;
  448. int tail;
  449. int zero = 0;
  450. int offset = 0;
  451. int size;
  452. if (vflag) {
  453. fprintf (stderr, "Adding Image %s\n", datafile);
  454. }
  455. if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
  456. fprintf (stderr, "%s: Can't open %s: %s\n",
  457. cmdname, datafile, strerror(errno));
  458. exit (EXIT_FAILURE);
  459. }
  460. if (fstat(dfd, &sbuf) < 0) {
  461. fprintf (stderr, "%s: Can't stat %s: %s\n",
  462. cmdname, datafile, strerror(errno));
  463. exit (EXIT_FAILURE);
  464. }
  465. ptr = (unsigned char *)mmap(0, sbuf.st_size,
  466. PROT_READ, MAP_SHARED, dfd, 0);
  467. if (ptr == (unsigned char *)MAP_FAILED) {
  468. fprintf (stderr, "%s: Can't read %s: %s\n",
  469. cmdname, datafile, strerror(errno));
  470. exit (EXIT_FAILURE);
  471. }
  472. if (xflag) {
  473. unsigned char *p = NULL;
  474. /*
  475. * XIP: do not append the image_header_t at the
  476. * beginning of the file, but consume the space
  477. * reserved for it.
  478. */
  479. if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
  480. fprintf (stderr,
  481. "%s: Bad size: \"%s\" is too small for XIP\n",
  482. cmdname, datafile);
  483. exit (EXIT_FAILURE);
  484. }
  485. for (p=ptr; p < ptr+sizeof(image_header_t); p++) {
  486. if ( *p != 0xff ) {
  487. fprintf (stderr,
  488. "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
  489. cmdname, datafile);
  490. exit (EXIT_FAILURE);
  491. }
  492. }
  493. offset = sizeof(image_header_t);
  494. }
  495. size = sbuf.st_size - offset;
  496. if (write(ifd, ptr + offset, size) != size) {
  497. fprintf (stderr, "%s: Write error on %s: %s\n",
  498. cmdname, imagefile, strerror(errno));
  499. exit (EXIT_FAILURE);
  500. }
  501. if (pad && ((tail = size % 4) != 0)) {
  502. if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  503. fprintf (stderr, "%s: Write error on %s: %s\n",
  504. cmdname, imagefile, strerror(errno));
  505. exit (EXIT_FAILURE);
  506. }
  507. }
  508. (void) munmap((void *)ptr, sbuf.st_size);
  509. (void) close (dfd);
  510. }
  511. void
  512. usage ()
  513. {
  514. fprintf (stderr, "Usage: %s -l image\n"
  515. " -l ==> list image header information\n"
  516. " %s [-x] -A arch -O os -T type -C comp "
  517. "-a addr -e ep -n name -d data_file[:data_file...] image\n",
  518. cmdname, cmdname);
  519. fprintf (stderr, " -A ==> set architecture to 'arch'\n"
  520. " -O ==> set operating system to 'os'\n"
  521. " -T ==> set image type to 'type'\n"
  522. " -C ==> set compression type 'comp'\n"
  523. " -a ==> set load address to 'addr' (hex)\n"
  524. " -e ==> set entry point to 'ep' (hex)\n"
  525. " -n ==> set image name to 'name'\n"
  526. " -d ==> use image data from 'datafile'\n"
  527. " -x ==> set XIP (execute in place)\n"
  528. );
  529. exit (EXIT_FAILURE);
  530. }
  531. static void
  532. print_header (image_header_t *hdr)
  533. {
  534. time_t timestamp;
  535. uint32_t size;
  536. timestamp = (time_t)ntohl(hdr->ih_time);
  537. size = ntohl(hdr->ih_size);
  538. printf ("Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
  539. printf ("Created: %s", ctime(&timestamp));
  540. printf ("Image Type: "); print_type(hdr);
  541. printf ("Data Size: %d Bytes = %.2f kB = %.2f MB\n",
  542. size, (double)size / 1.024e3, (double)size / 1.048576e6 );
  543. printf ("Load Address: 0x%08X\n", ntohl(hdr->ih_load));
  544. printf ("Entry Point: 0x%08X\n", ntohl(hdr->ih_ep));
  545. if (hdr->ih_type == IH_TYPE_MULTI || hdr->ih_type == IH_TYPE_SCRIPT) {
  546. int i, ptrs;
  547. uint32_t pos;
  548. unsigned long *len_ptr = (unsigned long *) (
  549. (unsigned long)hdr + sizeof(image_header_t)
  550. );
  551. /* determine number of images first (to calculate image offsets) */
  552. for (i=0; len_ptr[i]; ++i) /* null pointer terminates list */
  553. ;
  554. ptrs = i; /* null pointer terminates list */
  555. pos = sizeof(image_header_t) + ptrs * sizeof(long);
  556. printf ("Contents:\n");
  557. for (i=0; len_ptr[i]; ++i) {
  558. size = ntohl(len_ptr[i]);
  559. printf (" Image %d: %8d Bytes = %4d kB = %d MB\n",
  560. i, size, size>>10, size>>20);
  561. if (hdr->ih_type == IH_TYPE_SCRIPT && i > 0) {
  562. /*
  563. * the user may need to know offsets
  564. * if planning to do something with
  565. * multiple files
  566. */
  567. printf (" Offset = %08X\n", pos);
  568. }
  569. /* copy_file() will pad the first files to even word align */
  570. size += 3;
  571. size &= ~3;
  572. pos += size;
  573. }
  574. }
  575. }
  576. static void
  577. print_type (image_header_t *hdr)
  578. {
  579. printf ("%s %s %s (%s)\n",
  580. put_arch (hdr->ih_arch),
  581. put_os (hdr->ih_os ),
  582. put_type (hdr->ih_type),
  583. put_comp (hdr->ih_comp)
  584. );
  585. }
  586. static char *put_arch (int arch)
  587. {
  588. return (put_table_entry(arch_name, "Unknown Architecture", arch));
  589. }
  590. static char *put_os (int os)
  591. {
  592. return (put_table_entry(os_name, "Unknown OS", os));
  593. }
  594. static char *put_type (int type)
  595. {
  596. return (put_table_entry(type_name, "Unknown Image", type));
  597. }
  598. static char *put_comp (int comp)
  599. {
  600. return (put_table_entry(comp_name, "Unknown Compression", comp));
  601. }
  602. static char *put_table_entry (table_entry_t *table, char *msg, int type)
  603. {
  604. for (; table->val>=0; ++table) {
  605. if (table->val == type)
  606. return (table->lname);
  607. }
  608. return (msg);
  609. }
  610. static int get_arch(char *name)
  611. {
  612. return (get_table_entry(arch_name, "CPU", name));
  613. }
  614. static int get_comp(char *name)
  615. {
  616. return (get_table_entry(comp_name, "Compression", name));
  617. }
  618. static int get_os (char *name)
  619. {
  620. return (get_table_entry(os_name, "OS", name));
  621. }
  622. static int get_type(char *name)
  623. {
  624. return (get_table_entry(type_name, "Image", name));
  625. }
  626. static int get_table_entry (table_entry_t *table, char *msg, char *name)
  627. {
  628. table_entry_t *t;
  629. int first = 1;
  630. for (t=table; t->val>=0; ++t) {
  631. if (t->sname && strcasecmp(t->sname, name)==0)
  632. return (t->val);
  633. }
  634. fprintf (stderr, "\nInvalid %s Type - valid names are", msg);
  635. for (t=table; t->val>=0; ++t) {
  636. if (t->sname == NULL)
  637. continue;
  638. fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
  639. first = 0;
  640. }
  641. fprintf (stderr, "\n");
  642. return (-1);
  643. }