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