mkimage.c 17 KB

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