mkimage.c 18 KB

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