mkimage.c 17 KB

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