mkimage.c 17 KB

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