pibcomp.c 9.3 KB

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