mkimage.c 18 KB

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