mkimage.c 17 KB

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