pibcomp.c 8.0 KB

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