pibcomp.c.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. pibcomp.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='pib2xml.c.html' title=' pib2xml.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='pibdump.c.html' title=' pibdump.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. * pibcomp.c - Qualcomm Atheros Parameter Information Block Compare Utility
  64. *
  65. *
  66. * Contributor(s):
  67. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  68. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  69. *
  70. *--------------------------------------------------------------------*/
  71. /*====================================================================*
  72. * system header files;
  73. *--------------------------------------------------------------------*/
  74. #include &lt;unistd.h&gt;
  75. #include &lt;stdlib.h&gt;
  76. #include &lt;string.h&gt;
  77. #include &lt;ctype.h&gt;
  78. #include &lt;errno.h&gt;
  79. #include &lt;sys/stat.h&gt;
  80. /*====================================================================*
  81. * custom header files;
  82. *--------------------------------------------------------------------*/
  83. #include &quot;../tools/getoptv.h&quot;
  84. #include &quot;../tools/number.h&quot;
  85. #include &quot;../tools/memory.h&quot;
  86. #include &quot;../tools/error.h&quot;
  87. #include &quot;../tools/flags.h&quot;
  88. #include &quot;../tools/chars.h&quot;
  89. #include &quot;../tools/sizes.h&quot;
  90. #include &quot;../tools/files.h&quot;
  91. #include &quot;../nvm/nvm.h&quot;
  92. #include &quot;../pib/pib.h&quot;
  93. /*====================================================================*
  94. * custom source files;
  95. *--------------------------------------------------------------------*/
  96. #ifndef MAKEFILE
  97. #include &quot;../tools/getoptv.c&quot;
  98. #include &quot;../tools/putoptv.c&quot;
  99. #include &quot;../tools/version.c&quot;
  100. #include &quot;../tools/hexview.c&quot;
  101. #include &quot;../tools/hexoffset.c&quot;
  102. #include &quot;../tools/error.c&quot;
  103. #include &quot;../tools/checksum32.c&quot;
  104. #include &quot;../tools/fdchecksum32.c&quot;
  105. #endif
  106. #ifndef MAKEFILE
  107. #include &quot;../nvm/nvmseek2.c&quot;
  108. #endif
  109. /*====================================================================*
  110. *
  111. * void function (char const * filename [], flag_t flags);
  112. *
  113. * read object definitions from stdin and use them to compare two
  114. * files; dump only those objects that differ on stdout;
  115. *
  116. *
  117. *--------------------------------------------------------------------*/
  118. static void function (char const * filename [], flag_t flags)
  119. {
  120. unsigned file;
  121. unsigned object = 0;
  122. unsigned lineno = 1;
  123. signed fd [2];
  124. signed length = 0;
  125. off_t origin [2];
  126. off_t offset [2];
  127. off_t extent [2];
  128. char memory [_ADDRSIZE+1];
  129. char symbol [_NAMESIZE];
  130. char string [_LINESIZE];
  131. char * sp;
  132. signed c;
  133. for (file = 0; file &lt; SIZEOF (fd); file++)
  134. {
  135. uint32_t version;
  136. if ((fd [file] = open (filename [file], O_BINARY|O_RDONLY)) == -1)
  137. {
  138. error (1, errno, &quot;%s&quot;, filename [file]);
  139. }
  140. if (read (fd [file], &amp;version, sizeof (version)) != sizeof (version))
  141. {
  142. error (1, errno, FILE_CANTREAD, filename [file]);
  143. }
  144. if ((extent [file] = lseek (fd [file], 0, SEEK_END)) == (off_t)(-1))
  145. {
  146. error (1, 0, FILE_CANTSIZE, filename [file]);
  147. }
  148. if ((origin [file] = lseek (fd [file], 0, SEEK_SET)))
  149. {
  150. error (1, errno, FILE_CANTHOME, filename [file]);
  151. }
  152. if (LE32TOH (version) == 0x60000000)
  153. {
  154. error (1, ENOTSUP, &quot;%s is not a PIB file&quot;, filename [file]);
  155. }
  156. if (LE32TOH (version) == 0x00010001)
  157. {
  158. struct nvm_header2 nvm_header;
  159. if (nvmseek2 (fd [file], filename [file], &amp;nvm_header, NVM_IMAGE_PIB))
  160. {
  161. error (1, ENOTSUP, &quot;%s is not a PIB file&quot;, filename [file]);
  162. }
  163. origin [file] = lseek (fd [file], 0, SEEK_CUR);
  164. }
  165. }
  166. if (origin [0] != origin [1])
  167. {
  168. error (0, EINVAL, &quot;PIBs have different offsets&quot;);
  169. }
  170. memset (offset, 0, sizeof (offset));
  171. while ((c = getc (stdin)) != EOF)
  172. {
  173. if ((c == '#') || (c == ';'))
  174. {
  175. do
  176. {
  177. c = getc (stdin);
  178. }
  179. while (nobreak (c));
  180. lineno++;
  181. continue;
  182. }
  183. if (isspace (c))
  184. {
  185. if (c == '\n')
  186. {
  187. lineno++;
  188. }
  189. continue;
  190. }
  191. if (c == '+')
  192. {
  193. do
  194. {
  195. c = getc (stdin);
  196. }
  197. 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' but need ']' 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 &gt; 0)
  262. {
  263. #if defined (WIN32)
  264. char * buffer [2];
  265. buffer [0] = (char *)(emalloc (length));
  266. buffer [1] = (char *)(emalloc (length));
  267. #else
  268. byte buffer [2][length];
  269. #endif
  270. if ((read (fd [0], buffer [0], length) == length) &amp;&amp; (read (fd [1], buffer [1], length) == length))
  271. {
  272. if (memcmp (buffer [0], buffer [1], length))
  273. {
  274. if (!object++)
  275. {
  276. for (c = 0; c &lt; _ADDRSIZE + 65; c++)
  277. {
  278. putc ('-', stdout);
  279. }
  280. putc ('\n', stdout);
  281. }
  282. printf (&quot;%s %d %s\n&quot;, hexoffset (memory, sizeof (memory), offset [0]), length, symbol);
  283. for (c = 0; c &lt; _ADDRSIZE; c++)
  284. {
  285. putc ('-', stdout);
  286. }
  287. printf (&quot; %s\n&quot;, filename [0]);
  288. hexview (buffer [0], offset [0], length, stdout);
  289. for (c = 0; c &lt; _ADDRSIZE; c++)
  290. {
  291. putc ('-', stdout);
  292. }
  293. printf (&quot; %s\n&quot;, filename [1]);
  294. hexview (buffer [1], offset [1], length, stdout);
  295. for (c = 0; c &lt; _ADDRSIZE + 65; c++)
  296. {
  297. putc ('-', stdout);
  298. }
  299. putc ('\n', stdout);
  300. }
  301. }
  302. #if defined (WIN32)
  303. free (buffer [0]);
  304. free (buffer [1]);
  305. #endif
  306. }
  307. offset [0] += length;
  308. offset [1] += length;
  309. lineno++;
  310. }
  311. if (_allclr (flags, PIB_SILENCE))
  312. {
  313. offset [0] += origin [0];
  314. offset [1] += origin [1];
  315. for (file = 0; file &lt; SIZEOF (extent); file++)
  316. {
  317. if (offset [file] &lt; extent [file])
  318. {
  319. error (0, 0, &quot;%s exceeds definition by &quot; OFF_T_SPEC &quot; bytes&quot;, filename [file], extent [file] - offset [file]);
  320. }
  321. if (offset [file] &gt; extent [file])
  322. {
  323. error (0, 0, &quot;definition exceeds %s by &quot; OFF_T_SPEC &quot; bytes&quot;, filename [file], offset [file] - extent [file]);
  324. }
  325. }
  326. if (extent [0] &gt; extent [1])
  327. {
  328. error (0, 0, &quot;%s exceeds %s by &quot; OFF_T_SPEC &quot; bytes&quot;, filename [0], filename [1], extent [0] - extent [1]);
  329. }
  330. if (extent [1] &gt; extent [0])
  331. {
  332. error (0, 0, &quot;%s exceeds %s by &quot; OFF_T_SPEC &quot; bytes&quot;, filename [1], filename [0], extent [1] - extent [0]);
  333. }
  334. }
  335. close (fd [0]);
  336. close (fd [1]);
  337. return;
  338. }
  339. /*====================================================================*
  340. *
  341. * int main (int argc, char const * argv []);
  342. *
  343. *
  344. *
  345. *--------------------------------------------------------------------*/
  346. int main (int argc, char const * argv [])
  347. {
  348. static char const * optv [] =
  349. {
  350. &quot;f:qv&quot;,
  351. &quot;file1 file2&quot;,
  352. &quot;Qualcomm Atheros Parameter Information Block Compare Utility&quot;,
  353. &quot;f f\tobject definition file&quot;,
  354. &quot;q\tquiet mode&quot;,
  355. &quot;v\tverbose mode&quot;,
  356. (char const *)(0)
  357. };
  358. flag_t flags = (flag_t)(0);
  359. signed c;
  360. optind = 1;
  361. while ((c = getoptv (argc, argv, optv)) != -1)
  362. {
  363. switch (c)
  364. {
  365. case 'f':
  366. if (!freopen (optarg, &quot;rb&quot;, stdin))
  367. {
  368. error (1, errno, &quot;%s&quot;, optarg);
  369. }
  370. break;
  371. case 'q':
  372. _setbits (flags, PIB_SILENCE);
  373. break;
  374. case 'v':
  375. _setbits (flags, PIB_VERBOSE);
  376. break;
  377. default:
  378. break;
  379. }
  380. }
  381. argc -= optind;
  382. argv += optind;
  383. if (argc != 2)
  384. {
  385. error (1, 0, &quot;Need two files to compare.&quot;);
  386. }
  387. function (argv, flags);
  388. return (0);
  389. }
  390. </pre>
  391. <div class='footerlink'>
  392. [<a href='pib2xml.c.html' title=' pib2xml.c '>PREV</a>]
  393. [<a href='toolkit.html' title=' Index '>HOME</a>]
  394. [<a href='pibdump.c.html' title=' pibdump.c '>NEXT</a>]
  395. </div>
  396. </body>
  397. </html>