pibdump.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. * pibdump.c - Qualcomm Atheros Parameter Information Block Dump Utility
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. *
  49. *--------------------------------------------------------------------*/
  50. /*====================================================================*
  51. * system header files;
  52. *--------------------------------------------------------------------*/
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <unistd.h>
  57. #include <ctype.h>
  58. #include <errno.h>
  59. /*====================================================================*
  60. * custom header files;
  61. *--------------------------------------------------------------------*/
  62. #include "../tools/getoptv.h"
  63. #include "../tools/putoptv.h"
  64. #include "../tools/version.h"
  65. #include "../tools/memory.h"
  66. #include "../tools/number.h"
  67. #include "../tools/error.h"
  68. #include "../tools/chars.h"
  69. #include "../tools/flags.h"
  70. #include "../tools/sizes.h"
  71. #include "../tools/files.h"
  72. #include "../pib/pib.h"
  73. #include "../nvm/nvm.h"
  74. /*====================================================================*
  75. * custom source files;
  76. *--------------------------------------------------------------------*/
  77. #ifndef MAKEFILE
  78. #include "../tools/getoptv.c"
  79. #include "../tools/putoptv.c"
  80. #include "../tools/version.c"
  81. #include "../tools/hexview.c"
  82. #include "../tools/hexoffset.c"
  83. #include "../tools/error.c"
  84. #include "../tools/checksum32.c"
  85. #include "../tools/fdchecksum32.c"
  86. #endif
  87. #ifndef MAKEFILE
  88. #include "../nvm/nvmseek2.c"
  89. #include "../nvm/nvm.c"
  90. #endif
  91. /*====================================================================*
  92. *
  93. * void pibdump (char const * filename, flag_t flags);
  94. *
  95. * open the named file and determine if it is a Qualcomm Atheros
  96. * parameter information block based on file header information;
  97. * read object definitions from stdin and print an object driven
  98. * dump on stdout;
  99. *
  100. *
  101. *--------------------------------------------------------------------*/
  102. static void pibdump (char const * filename, flag_t flags)
  103. {
  104. uint32_t version;
  105. unsigned object = 0;
  106. unsigned lineno = 0;
  107. off_t origin = 0;
  108. off_t offset = 0;
  109. off_t extent = 0;
  110. unsigned length = 0;
  111. char memory [_ADDRSIZE + 1];
  112. char symbol [_NAMESIZE];
  113. char string [_LINESIZE];
  114. char * sp;
  115. signed fd;
  116. signed c;
  117. if ((fd = open (filename, O_BINARY|O_RDONLY)) == -1)
  118. {
  119. error (1, errno, "%s", filename);
  120. }
  121. if (read (fd, &version, sizeof (version)) != sizeof (version))
  122. {
  123. error (1, errno, FILE_CANTREAD, filename);
  124. }
  125. if (lseek (fd, 0, SEEK_SET))
  126. {
  127. error (1, errno, FILE_CANTHOME, filename);
  128. }
  129. if (LE32TOH (version) == 0x60000000)
  130. {
  131. error (1, 0, "%s is not a PIB file", filename);
  132. }
  133. else if (LE32TOH (version) == 0x00010001)
  134. {
  135. struct nvm_header2 nvm_header;
  136. if (nvmseek2 (fd, filename, &nvm_header, NVM_IMAGE_PIB))
  137. {
  138. error (1, 0, "%s is not a PIB file", filename);
  139. }
  140. extent = LE32TOH (nvm_header.ImageLength);
  141. }
  142. else
  143. {
  144. struct simple_pib pib_header;
  145. if (read (fd, &pib_header, sizeof (pib_header)) != sizeof (pib_header))
  146. {
  147. error (1, errno, FILE_CANTREAD, filename);
  148. }
  149. if (lseek (fd, 0, SEEK_SET))
  150. {
  151. error (1, errno, FILE_CANTHOME, filename);
  152. }
  153. extent = LE16TOH (pib_header.PIBLENGTH);
  154. }
  155. while ((c = getc (stdin)) != EOF)
  156. {
  157. if ((c == '#') || (c == ';'))
  158. {
  159. do
  160. {
  161. c = getc (stdin);
  162. }
  163. while (nobreak (c));
  164. lineno++;
  165. continue;
  166. }
  167. if (isspace (c))
  168. {
  169. if (c == '\n')
  170. {
  171. lineno++;
  172. }
  173. continue;
  174. }
  175. if (c == '+')
  176. {
  177. do { c = getc (stdin); } 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' without ']' 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)
  242. {
  243. #if defined (WIN32)
  244. char * buffer = (char *)(emalloc (length));
  245. #else
  246. byte buffer [length];
  247. #endif
  248. if (read (fd, buffer, length) == (signed)(length))
  249. {
  250. if (!object++)
  251. {
  252. for (c = 0; c < _ADDRSIZE + 65; c++)
  253. {
  254. putc ('-', stdout);
  255. }
  256. putc ('\n', stdout);
  257. }
  258. printf ("%s %u %s\n", hexoffset (memory, sizeof (memory), offset), length, symbol);
  259. hexview (buffer, offset, length, stdout);
  260. for (c = 0; c < _ADDRSIZE + 65; c++)
  261. {
  262. putc ('-', stdout);
  263. }
  264. putc ('\n', stdout);
  265. }
  266. #if defined (WIN32)
  267. free (buffer);
  268. #endif
  269. }
  270. offset += length;
  271. lineno++;
  272. }
  273. offset += origin;
  274. if (_allclr (flags, PIB_SILENCE))
  275. {
  276. if (offset < extent)
  277. {
  278. error (0, 0, "file %s exceeds definition by " OFF_T_SPEC " bytes", filename, extent - offset);
  279. }
  280. if (offset > extent)
  281. {
  282. error (0, 0, "definition exceeds file %s by " OFF_T_SPEC " bytes", filename, offset - extent);
  283. }
  284. if (offset != extent)
  285. {
  286. error (0, 0, "file %s is " OFF_T_SPEC " bytes not " OFF_T_SPEC " bytes", filename, extent, offset);
  287. }
  288. }
  289. close (fd);
  290. return;
  291. }
  292. /*====================================================================*
  293. *
  294. * int main (int argc, char const * argv []);
  295. *
  296. *
  297. *--------------------------------------------------------------------*/
  298. int main (int argc, char const * argv [])
  299. {
  300. static char const * optv [] =
  301. {
  302. "f:qv",
  303. "file",
  304. "Qualcomm Atheros Parameter Information Block Dump Utility",
  305. "f f\tobject definition file",
  306. "q\tquiet mode",
  307. "v\tverbose mode",
  308. (char const *)(0)
  309. };
  310. flag_t flags = (flag_t)(0);
  311. signed c;
  312. optind = 1;
  313. while ((c = getoptv (argc, argv, optv)) != -1)
  314. {
  315. switch (c)
  316. {
  317. case 'f':
  318. if (!freopen (optarg, "rb", stdin))
  319. {
  320. error (1, errno, "%s", optarg);
  321. }
  322. break;
  323. case 'q':
  324. _setbits (flags, PIB_SILENCE);
  325. break;
  326. case 'v':
  327. _setbits (flags, PIB_VERBOSE);
  328. break;
  329. default:
  330. break;
  331. }
  332. }
  333. argc -= optind;
  334. argv += optind;
  335. if (argc > 1)
  336. {
  337. error (1, ENOTSUP, ERROR_TOOMANY);
  338. }
  339. if ((argc) && (* argv))
  340. {
  341. pibdump (*argv, flags);
  342. }
  343. return (0);
  344. }