pib2xml.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * pib2xml.c - print PIB contents in XML format;
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. /*====================================================================*
  17. * system header files;
  18. *--------------------------------------------------------------------*/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. /*====================================================================*
  25. * custom header files;
  26. *--------------------------------------------------------------------*/
  27. #include "../tools/getoptv.h"
  28. #include "../tools/putoptv.h"
  29. #include "../tools/version.h"
  30. #include "../tools/endian.h"
  31. #include "../tools/format.h"
  32. #include "../tools/files.h"
  33. #include "../tools/chars.h"
  34. #include "../tools/error.h"
  35. #include "../tools/flags.h"
  36. #include "../nodes/node.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/output.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. #ifndef MAKEFILE
  56. #include "../nodes/xmlschema.c"
  57. #endif
  58. /*====================================================================*
  59. *
  60. * void pibdump (signed fd, char const * filename, char const * schema, unsigned extent, flag_t flags);
  61. *
  62. *
  63. *--------------------------------------------------------------------*/
  64. static void pibdump (signed fd, char const * filename, char const * schema, unsigned extent, flag_t flags)
  65. {
  66. unsigned offset = 0;
  67. signed indent = 0;
  68. signed length = 0;
  69. unsigned lineno = 1;
  70. char symbol [0x0100];
  71. char string [0x0400];
  72. char * sp;
  73. signed c;
  74. output (indent++, "<%s xmlns:xsi='%s' xsi:noNamespaceSchemaLocation='%s'>", DATA_OBJECT, XML_NAMESPACE, schema);
  75. while ((c = getc (stdin)) != EOF)
  76. {
  77. if ((c == '#') || (c == ';'))
  78. {
  79. do
  80. {
  81. c = getc (stdin);
  82. }
  83. while (nobreak (c));
  84. }
  85. if (isspace (c))
  86. {
  87. if (c == '\n')
  88. {
  89. lineno++;
  90. }
  91. continue;
  92. }
  93. if ((c == '+') || (c == '-'))
  94. {
  95. do
  96. {
  97. c = getc (stdin);
  98. }
  99. while (isblank (c));
  100. }
  101. length = 0;
  102. while (isdigit (c))
  103. {
  104. length *= 10;
  105. length += c - '0';
  106. c = getc (stdin);
  107. }
  108. while (isblank (c))
  109. {
  110. c = getc (stdin);
  111. }
  112. sp = symbol;
  113. if (isalpha (c) || (c == '_'))
  114. {
  115. do
  116. {
  117. * sp++ = (char) (c);
  118. c = getc (stdin);
  119. }
  120. while (isident (c));
  121. }
  122. * sp = (char) (0);
  123. while (isblank (c))
  124. {
  125. c = getc (stdin);
  126. }
  127. if (c == '[')
  128. {
  129. #if 0
  130. * sp++ = (char) (c);
  131. #endif
  132. c = getc (stdin);
  133. while (isblank (c))
  134. {
  135. c = getc (stdin);
  136. }
  137. while (isdigit (c))
  138. {
  139. #if 0
  140. * sp++ = (char) (c);
  141. #endif
  142. c = getc (stdin);
  143. }
  144. while (isblank (c))
  145. {
  146. c = getc (stdin);
  147. }
  148. if (c != ']')
  149. {
  150. error (1, EINVAL, "Have '%c' but need ']'", c);
  151. }
  152. #if 0
  153. * sp++ = (char) (c);
  154. #endif
  155. c = getc (stdin);
  156. }
  157. * sp = (char) (0);
  158. while (isblank (c))
  159. {
  160. c = getc (stdin);
  161. }
  162. sp = string;
  163. while (nobreak (c))
  164. {
  165. * sp++ = (char) (c);
  166. c = getc (stdin);
  167. }
  168. * sp = (char) (0);
  169. if (length > 0)
  170. {
  171. #if defined (WIN32)
  172. byte * buffer = (byte *) (emalloc (length));
  173. #else
  174. byte buffer [length];
  175. #endif
  176. if (read (fd, buffer, length) == length)
  177. {
  178. output (indent++, "<%s name='%s'>", DATA_MEMBER, symbol);
  179. #if 0
  180. if (* string)
  181. {
  182. output (indent++, "<text>");
  183. output (indent, "%s", string);
  184. output (indent--, "</text>");
  185. }
  186. #endif
  187. output (indent++, "<%s>", DATA_OFFSET);
  188. output (indent, "%04X", offset);
  189. output (indent--, "</%s>", DATA_OFFSET);
  190. output (indent++, "<%s>", DATA_LENGTH);
  191. output (indent, "%d", length);
  192. output (indent--, "</%s>", DATA_LENGTH);
  193. output (indent++, "<%s>", DATA_MEMORY);
  194. for (c = 0; c < indent; c++)
  195. {
  196. printf ("\t");
  197. }
  198. for (c = 0; c < length; c++)
  199. {
  200. printf ("%02X", buffer [c]);
  201. }
  202. printf ("\n");
  203. output (indent--, "</%s>", DATA_MEMORY);
  204. output (indent--, "</%s>", DATA_MEMBER);
  205. }
  206. #if defined (WIN32)
  207. free (buffer);
  208. #endif
  209. }
  210. offset += length;
  211. }
  212. output (indent--, "</%s>", DATA_OBJECT);
  213. if (_allclr (flags, PIB_SILENCE))
  214. {
  215. if (offset != extent)
  216. {
  217. error (0, 0, "file %s is %d not %d bytes", filename, extent, offset);
  218. }
  219. }
  220. return;
  221. }
  222. /*====================================================================*
  223. *
  224. * int main (int argc, char const * argv []);
  225. *
  226. *
  227. *--------------------------------------------------------------------*/
  228. int main (int argc, char const * argv [])
  229. {
  230. static char const * optv [] =
  231. {
  232. "f:x",
  233. "file [file] [...]",
  234. "Qualcomm Atheros Parameter File XML Dump Utility",
  235. "f f\tobject definition file",
  236. "x\tprint XML schema on stdout and exit",
  237. (char const *) (0)
  238. };
  239. char const * schema = DATA_SCHEMA;
  240. flag_t flags = (flag_t) (0);
  241. signed c;
  242. optind = 1;
  243. while (~ (c = getoptv (argc, argv, optv)))
  244. {
  245. switch (c)
  246. {
  247. case 'f':
  248. if (! freopen (optarg, "rb", stdin))
  249. {
  250. error (1, errno, "Can't open %s", optarg);
  251. }
  252. break;
  253. case 'x':
  254. xmlschema ();
  255. return (0);
  256. case 'q':
  257. _setbits (flags, PIB_SILENCE);
  258. break;
  259. case 'v':
  260. _setbits (flags, PIB_VERBOSE);
  261. break;
  262. default:
  263. break;
  264. }
  265. }
  266. argc -= optind;
  267. argv += optind;
  268. if (argc > 1)
  269. {
  270. error (1, ENOTSUP, ERROR_TOOMANY);
  271. }
  272. #if defined (WIN32)
  273. setmode (STDOUT_FILENO, O_BINARY);
  274. #endif
  275. if ((argc) && (* argv))
  276. {
  277. signed fd;
  278. uint32_t version;
  279. if ((fd = open (* argv, O_BINARY | O_RDONLY)) == - 1)
  280. {
  281. error (1, errno, "%s", * argv);
  282. }
  283. else if (read (fd, & version, sizeof (version)) != sizeof (version))
  284. {
  285. error (1, errno, "Can't read %s", * argv);
  286. }
  287. else if (lseek (fd, 0, SEEK_SET))
  288. {
  289. error (1, errno, "Can't home %s", * argv);
  290. }
  291. else if (LE32TOH (version) == 0x60000000)
  292. {
  293. error (1, errno, "Won't read %s", * argv);
  294. }
  295. else if (LE32TOH (version) == 0x00010001)
  296. {
  297. struct panther_nvm_header header;
  298. if (! panther_nvm_seek (fd, * argv, & header, NVM_IMAGE_PIB))
  299. {
  300. pibdump (fd, * argv, schema, LE32TOH (header.ImageLength), flags);
  301. }
  302. }
  303. else
  304. {
  305. struct simple_pib pib_header;
  306. if (read (fd, & pib_header, sizeof (pib_header)) != sizeof (pib_header))
  307. {
  308. error (1, errno, "Can't read %s", * argv);
  309. }
  310. if (lseek (fd, 0, SEEK_SET))
  311. {
  312. error (1, errno, "Can't home %s", * argv);
  313. }
  314. pibdump (fd, * argv, schema, LE16TOH (pib_header.PIBLENGTH), flags);
  315. }
  316. close (fd);
  317. }
  318. return (0);
  319. }