mkimage.c 17 KB

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