pibdump.c.html 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. pibdump.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='pibcomp.c.html' title=' pibcomp.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='pibfile1.c.html' title=' pibfile1.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. *
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted (subject to the limitations
  29. * in the disclaimer below) provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above
  36. * copyright notice, this list of conditions and the following
  37. * disclaimer in the documentation and/or other materials
  38. * provided with the distribution.
  39. *
  40. * * Neither the name of Qualcomm Atheros nor the names of
  41. * its contributors may be used to endorse or promote products
  42. * derived from this software without specific prior written
  43. * permission.
  44. *
  45. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  46. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  47. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  48. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  51. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  57. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*
  62. *
  63. * pibdump.c - Qualcomm Atheros Parameter Information Block Dump Utility
  64. *
  65. *
  66. * Contributor(s):
  67. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  68. *
  69. *--------------------------------------------------------------------*/
  70. /*====================================================================*
  71. * system header files;
  72. *--------------------------------------------------------------------*/
  73. #include &lt;stdio.h&gt;
  74. #include &lt;stdlib.h&gt;
  75. #include &lt;string.h&gt;
  76. #include &lt;unistd.h&gt;
  77. #include &lt;ctype.h&gt;
  78. #include &lt;errno.h&gt;
  79. /*====================================================================*
  80. * custom header files;
  81. *--------------------------------------------------------------------*/
  82. #include &quot;../tools/getoptv.h&quot;
  83. #include &quot;../tools/putoptv.h&quot;
  84. #include &quot;../tools/version.h&quot;
  85. #include &quot;../tools/memory.h&quot;
  86. #include &quot;../tools/number.h&quot;
  87. #include &quot;../tools/error.h&quot;
  88. #include &quot;../tools/chars.h&quot;
  89. #include &quot;../tools/flags.h&quot;
  90. #include &quot;../tools/sizes.h&quot;
  91. #include &quot;../tools/files.h&quot;
  92. #include &quot;../pib/pib.h&quot;
  93. #include &quot;../nvm/nvm.h&quot;
  94. /*====================================================================*
  95. * custom source files;
  96. *--------------------------------------------------------------------*/
  97. #ifndef MAKEFILE
  98. #include &quot;../tools/getoptv.c&quot;
  99. #include &quot;../tools/putoptv.c&quot;
  100. #include &quot;../tools/version.c&quot;
  101. #include &quot;../tools/hexview.c&quot;
  102. #include &quot;../tools/hexoffset.c&quot;
  103. #include &quot;../tools/error.c&quot;
  104. #include &quot;../tools/checksum32.c&quot;
  105. #include &quot;../tools/fdchecksum32.c&quot;
  106. #endif
  107. #ifndef MAKEFILE
  108. #include &quot;../nvm/nvmseek2.c&quot;
  109. #include &quot;../nvm/nvm.c&quot;
  110. #endif
  111. /*====================================================================*
  112. *
  113. * void pibdump (char const * filename, flag_t flags);
  114. *
  115. * open the named file and determine if it is a Qualcomm Atheros
  116. * parameter information block based on file header information;
  117. * read object definitions from stdin and print an object driven
  118. * dump on stdout;
  119. *
  120. *
  121. *--------------------------------------------------------------------*/
  122. static void pibdump (char const * filename, flag_t flags)
  123. {
  124. uint32_t version;
  125. unsigned object = 0;
  126. unsigned lineno = 0;
  127. off_t origin = 0;
  128. off_t offset = 0;
  129. off_t extent = 0;
  130. unsigned length = 0;
  131. char memory [_ADDRSIZE + 1];
  132. char symbol [_NAMESIZE];
  133. char string [_LINESIZE];
  134. char * sp;
  135. signed fd;
  136. signed c;
  137. if ((fd = open (filename, O_BINARY|O_RDONLY)) == -1)
  138. {
  139. error (1, errno, &quot;%s&quot;, filename);
  140. }
  141. if (read (fd, &amp;version, sizeof (version)) != sizeof (version))
  142. {
  143. error (1, errno, FILE_CANTREAD, filename);
  144. }
  145. if (lseek (fd, 0, SEEK_SET))
  146. {
  147. error (1, errno, FILE_CANTHOME, filename);
  148. }
  149. if (LE32TOH (version) == 0x60000000)
  150. {
  151. error (1, 0, &quot;%s is not a PIB file&quot;, filename);
  152. }
  153. else if (LE32TOH (version) == 0x00010001)
  154. {
  155. struct nvm_header2 nvm_header;
  156. if (nvmseek2 (fd, filename, &amp;nvm_header, NVM_IMAGE_PIB))
  157. {
  158. error (1, 0, &quot;%s is not a PIB file&quot;, filename);
  159. }
  160. extent = LE32TOH (nvm_header.ImageLength);
  161. }
  162. else
  163. {
  164. struct simple_pib pib_header;
  165. if (read (fd, &amp;pib_header, sizeof (pib_header)) != sizeof (pib_header))
  166. {
  167. error (1, errno, FILE_CANTREAD, filename);
  168. }
  169. if (lseek (fd, 0, SEEK_SET))
  170. {
  171. error (1, errno, FILE_CANTHOME, filename);
  172. }
  173. extent = LE16TOH (pib_header.PIBLENGTH);
  174. }
  175. while ((c = getc (stdin)) != EOF)
  176. {
  177. if ((c == '#') || (c == ';'))
  178. {
  179. do
  180. {
  181. c = getc (stdin);
  182. }
  183. while (nobreak (c));
  184. lineno++;
  185. continue;
  186. }
  187. if (isspace (c))
  188. {
  189. if (c == '\n')
  190. {
  191. lineno++;
  192. }
  193. continue;
  194. }
  195. if (c == '+')
  196. {
  197. do { c = getc (stdin); } while (isblank (c));
  198. }
  199. length = 0;
  200. while (isdigit (c))
  201. {
  202. length *= 10;
  203. length += c - '0';
  204. c = getc (stdin);
  205. }
  206. while (isblank (c))
  207. {
  208. c = getc (stdin);
  209. }
  210. sp = symbol;
  211. if (isalpha (c) || (c == '_'))
  212. {
  213. do
  214. {
  215. *sp++ = (char)(c);
  216. c = getc (stdin);
  217. }
  218. while (isident (c));
  219. }
  220. while (isblank (c))
  221. {
  222. c = getc (stdin);
  223. }
  224. if (c == '[')
  225. {
  226. *sp++ = (char)(c);
  227. c = getc (stdin);
  228. while (isblank (c))
  229. {
  230. c = getc (stdin);
  231. }
  232. while (isdigit (c))
  233. {
  234. *sp++ = (char)(c);
  235. c = getc (stdin);
  236. }
  237. while (isblank (c))
  238. {
  239. c = getc (stdin);
  240. }
  241. *sp = (char)(0);
  242. if (c != ']')
  243. {
  244. error (1, EINVAL, &quot;Have '%s' without ']' on line %d&quot;, symbol, lineno);
  245. }
  246. *sp++ = (char)(c);
  247. c = getc (stdin);
  248. }
  249. *sp = (char)(0);
  250. while (isblank (c))
  251. {
  252. c = getc (stdin);
  253. }
  254. sp = string;
  255. while (nobreak (c))
  256. {
  257. *sp++ = (char)(c);
  258. c = getc (stdin);
  259. }
  260. *sp = (char)(0);
  261. if (length)
  262. {
  263. #if defined (WIN32)
  264. char * buffer = (char *)(emalloc (length));
  265. #else
  266. byte buffer [length];
  267. #endif
  268. if (read (fd, buffer, length) == (signed)(length))
  269. {
  270. if (!object++)
  271. {
  272. for (c = 0; c &lt; _ADDRSIZE + 65; c++)
  273. {
  274. putc ('-', stdout);
  275. }
  276. putc ('\n', stdout);
  277. }
  278. printf (&quot;%s %u %s\n&quot;, hexoffset (memory, sizeof (memory), offset), length, symbol);
  279. hexview (buffer, offset, length, stdout);
  280. for (c = 0; c &lt; _ADDRSIZE + 65; c++)
  281. {
  282. putc ('-', stdout);
  283. }
  284. putc ('\n', stdout);
  285. }
  286. #if defined (WIN32)
  287. free (buffer);
  288. #endif
  289. }
  290. offset += length;
  291. lineno++;
  292. }
  293. offset += origin;
  294. if (_allclr (flags, PIB_SILENCE))
  295. {
  296. if (offset &lt; extent)
  297. {
  298. error (0, 0, &quot;file %s exceeds definition by &quot; OFF_T_SPEC &quot; bytes&quot;, filename, extent - offset);
  299. }
  300. if (offset &gt; extent)
  301. {
  302. error (0, 0, &quot;definition exceeds file %s by &quot; OFF_T_SPEC &quot; bytes&quot;, filename, offset - extent);
  303. }
  304. if (offset != extent)
  305. {
  306. error (0, 0, &quot;file %s is &quot; OFF_T_SPEC &quot; bytes not &quot; OFF_T_SPEC &quot; bytes&quot;, filename, extent, offset);
  307. }
  308. }
  309. close (fd);
  310. return;
  311. }
  312. /*====================================================================*
  313. *
  314. * int main (int argc, char const * argv []);
  315. *
  316. *
  317. *--------------------------------------------------------------------*/
  318. int main (int argc, char const * argv [])
  319. {
  320. static char const * optv [] =
  321. {
  322. &quot;f:qv&quot;,
  323. &quot;file&quot;,
  324. &quot;Qualcomm Atheros Parameter Information Block Dump Utility&quot;,
  325. &quot;f f\tobject definition file&quot;,
  326. &quot;q\tquiet mode&quot;,
  327. &quot;v\tverbose mode&quot;,
  328. (char const *)(0)
  329. };
  330. flag_t flags = (flag_t)(0);
  331. signed c;
  332. optind = 1;
  333. while ((c = getoptv (argc, argv, optv)) != -1)
  334. {
  335. switch (c)
  336. {
  337. case 'f':
  338. if (!freopen (optarg, &quot;rb&quot;, stdin))
  339. {
  340. error (1, errno, &quot;%s&quot;, optarg);
  341. }
  342. break;
  343. case 'q':
  344. _setbits (flags, PIB_SILENCE);
  345. break;
  346. case 'v':
  347. _setbits (flags, PIB_VERBOSE);
  348. break;
  349. default:
  350. break;
  351. }
  352. }
  353. argc -= optind;
  354. argv += optind;
  355. if (argc &gt; 1)
  356. {
  357. error (1, ENOTSUP, ERROR_TOOMANY);
  358. }
  359. if ((argc) &amp;&amp; (* argv))
  360. {
  361. pibdump (*argv, flags);
  362. }
  363. return (0);
  364. }
  365. </pre>
  366. <div class='footerlink'>
  367. [<a href='pibcomp.c.html' title=' pibcomp.c '>PREV</a>]
  368. [<a href='toolkit.html' title=' Index '>HOME</a>]
  369. [<a href='pibfile1.c.html' title=' pibfile1.c '>NEXT</a>]
  370. </div>
  371. </body>
  372. </html>