pibdump.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * pibdump.c - Qualcomm Atheros Parameter Information Block Dump Utility
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. /*====================================================================*
  17. * system header files;
  18. *--------------------------------------------------------------------*/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <ctype.h>
  24. #include <errno.h>
  25. /*====================================================================*
  26. * custom header files;
  27. *--------------------------------------------------------------------*/
  28. #include "../tools/getoptv.h"
  29. #include "../tools/putoptv.h"
  30. #include "../tools/version.h"
  31. #include "../tools/memory.h"
  32. #include "../tools/number.h"
  33. #include "../tools/error.h"
  34. #include "../tools/chars.h"
  35. #include "../tools/flags.h"
  36. #include "../tools/sizes.h"
  37. #include "../tools/files.h"
  38. #include "../pib/pib.h"
  39. #include "../nvm/nvm.h"
  40. /*====================================================================*
  41. * custom source files;
  42. *--------------------------------------------------------------------*/
  43. #ifndef MAKEFILE
  44. #include "../tools/getoptv.c"
  45. #include "../tools/putoptv.c"
  46. #include "../tools/version.c"
  47. #include "../tools/hexview.c"
  48. #include "../tools/hexoffset.c"
  49. #include "../tools/error.c"
  50. #include "../tools/checksum32.c"
  51. #include "../tools/fdchecksum32.c"
  52. #endif
  53. #ifndef MAKEFILE
  54. #include "../nvm/panther_nvm_seek.c"
  55. #include "../nvm/nvm.c"
  56. #endif
  57. /*====================================================================*
  58. *
  59. * void pibdump (char const * filename, flag_t flags);
  60. *
  61. * open the named file and determine if it is a Qualcomm Atheros
  62. * parameter information block based on file header information;
  63. * read object definitions from stdin and print an object driven
  64. * dump on stdout;
  65. *
  66. *--------------------------------------------------------------------*/
  67. static void pibdump (char const * filename, flag_t flags)
  68. {
  69. uint32_t version;
  70. unsigned object = 0;
  71. unsigned lineno = 0;
  72. off_t origin = 0;
  73. off_t offset = 0;
  74. off_t extent = 0;
  75. signed length = 0;
  76. char memory [_ADDRSIZE + 1];
  77. char symbol [_NAMESIZE];
  78. char string [_LINESIZE];
  79. char * sp;
  80. signed fd;
  81. signed c;
  82. if ((fd = open (filename, O_BINARY | O_RDONLY)) == - 1)
  83. {
  84. error (1, errno, "%s", filename);
  85. }
  86. if (read (fd, & version, sizeof (version)) != sizeof (version))
  87. {
  88. error (1, errno, FILE_CANTREAD, filename);
  89. }
  90. if (lseek (fd, 0, SEEK_SET))
  91. {
  92. error (1, errno, FILE_CANTHOME, filename);
  93. }
  94. if (LE32TOH (version) == 0x60000000)
  95. {
  96. error (1, 0, "%s is not a PIB file", filename);
  97. }
  98. else if (LE32TOH (version) == 0x00010001)
  99. {
  100. struct panther_nvm_header header;
  101. if (panther_nvm_seek (fd, filename, & header, NVM_IMAGE_PIB))
  102. {
  103. error (1, 0, "%s is not a PIB file", filename);
  104. }
  105. extent = LE32TOH (header.ImageLength);
  106. }
  107. else
  108. {
  109. struct simple_pib pib_header;
  110. if (read (fd, & pib_header, sizeof (pib_header)) != sizeof (pib_header))
  111. {
  112. error (1, errno, FILE_CANTREAD, filename);
  113. }
  114. if (lseek (fd, 0, SEEK_SET))
  115. {
  116. error (1, errno, FILE_CANTHOME, filename);
  117. }
  118. extent = LE16TOH (pib_header.PIBLENGTH);
  119. }
  120. while ((c = getc (stdin)) != EOF)
  121. {
  122. if ((c == '#') || (c == ';'))
  123. {
  124. do
  125. {
  126. c = getc (stdin);
  127. }
  128. while (nobreak (c));
  129. lineno++;
  130. continue;
  131. }
  132. if (isspace (c))
  133. {
  134. if (c == '\n')
  135. {
  136. lineno++;
  137. }
  138. continue;
  139. }
  140. if (c == '+')
  141. {
  142. do
  143. {
  144. c = getc (stdin);
  145. }
  146. while (isblank (c));
  147. }
  148. length = 0;
  149. while (isdigit (c))
  150. {
  151. length *= 10;
  152. length += c - '0';
  153. c = getc (stdin);
  154. }
  155. while (isblank (c))
  156. {
  157. c = getc (stdin);
  158. }
  159. sp = symbol;
  160. if (isalpha (c) || (c == '_'))
  161. {
  162. do
  163. {
  164. * sp++ = (char) (c);
  165. c = getc (stdin);
  166. }
  167. while (isident (c));
  168. }
  169. while (isblank (c))
  170. {
  171. c = getc (stdin);
  172. }
  173. if (c == '[')
  174. {
  175. * sp++ = (char) (c);
  176. c = getc (stdin);
  177. while (isblank (c))
  178. {
  179. c = getc (stdin);
  180. }
  181. while (isdigit (c))
  182. {
  183. * sp++ = (char) (c);
  184. c = getc (stdin);
  185. }
  186. while (isblank (c))
  187. {
  188. c = getc (stdin);
  189. }
  190. * sp = (char) (0);
  191. if (c != ']')
  192. {
  193. error (1, EINVAL, "Have '%s' without ']' on line %d", symbol, lineno);
  194. }
  195. * sp++ = (char) (c);
  196. c = getc (stdin);
  197. }
  198. * sp = (char) (0);
  199. while (isblank (c))
  200. {
  201. c = getc (stdin);
  202. }
  203. sp = string;
  204. while (nobreak (c))
  205. {
  206. * sp++ = (char) (c);
  207. c = getc (stdin);
  208. }
  209. * sp = (char) (0);
  210. if (length)
  211. {
  212. #if defined (WIN32)
  213. char * buffer = (char *) (emalloc (length));
  214. #else
  215. byte buffer [length];
  216. #endif
  217. if (read (fd, buffer, length) == length)
  218. {
  219. if (! object++)
  220. {
  221. for (c = 0; c < _ADDRSIZE + 65; c++)
  222. {
  223. putc ('-', stdout);
  224. }
  225. putc ('\n', stdout);
  226. }
  227. printf ("%s %u %s\n", hexoffset (memory, sizeof (memory), offset), length, symbol);
  228. hexview (buffer, offset, length, stdout);
  229. for (c = 0; c < _ADDRSIZE + 65; c++)
  230. {
  231. putc ('-', stdout);
  232. }
  233. putc ('\n', stdout);
  234. }
  235. #if defined (WIN32)
  236. free (buffer);
  237. #endif
  238. }
  239. offset += length;
  240. lineno++;
  241. }
  242. offset += origin;
  243. if (_allclr (flags, PIB_SILENCE))
  244. {
  245. if (offset < extent)
  246. {
  247. error (0, 0, "file %s exceeds definition by " OFF_T_SPEC " bytes", filename, extent - offset);
  248. }
  249. if (offset > extent)
  250. {
  251. error (0, 0, "definition exceeds file %s by " OFF_T_SPEC " bytes", filename, offset - extent);
  252. }
  253. if (offset != extent)
  254. {
  255. error (0, 0, "file %s is " OFF_T_SPEC " bytes not " OFF_T_SPEC " bytes", filename, extent, offset);
  256. }
  257. }
  258. close (fd);
  259. return;
  260. }
  261. /*====================================================================*
  262. *
  263. * int main (int argc, char const * argv []);
  264. *
  265. *--------------------------------------------------------------------*/
  266. int main (int argc, char const * argv [])
  267. {
  268. static char const * optv [] =
  269. {
  270. "f:qv",
  271. "file",
  272. "Qualcomm Atheros Parameter Information Block Dump Utility",
  273. "f f\tobject definition file",
  274. "q\tquiet mode",
  275. "v\tverbose mode",
  276. (char const *) (0)
  277. };
  278. flag_t flags = (flag_t) (0);
  279. signed c;
  280. optind = 1;
  281. while (~ (c = getoptv (argc, argv, optv)))
  282. {
  283. switch (c)
  284. {
  285. case 'f':
  286. if (! freopen (optarg, "rb", stdin))
  287. {
  288. error (1, errno, "%s", optarg);
  289. }
  290. break;
  291. case 'q':
  292. _setbits (flags, PIB_SILENCE);
  293. break;
  294. case 'v':
  295. _setbits (flags, PIB_VERBOSE);
  296. break;
  297. default:
  298. break;
  299. }
  300. }
  301. argc -= optind;
  302. argv += optind;
  303. if (argc > 1)
  304. {
  305. error (1, ENOTSUP, ERROR_TOOMANY);
  306. }
  307. if ((argc) && (* argv))
  308. {
  309. pibdump (* argv, flags);
  310. }
  311. return (0);
  312. }