xml2pib.c.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. xml2pib.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='WritePIB.c.html' title=' WritePIB.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='xmlattribute.c.html' title=' xmlattribute.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. *
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted (subject to the limitations
  29. * in the disclaimer below) provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above
  36. * copyright notice, this list of conditions and the following
  37. * disclaimer in the documentation and/or other materials
  38. * provided with the distribution.
  39. *
  40. * * Neither the name of Qualcomm Atheros nor the names of
  41. * its contributors may be used to endorse or promote products
  42. * derived from this software without specific prior written
  43. * permission.
  44. *
  45. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  46. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  47. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  48. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  51. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  57. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*&quot;
  62. *
  63. * xml2pib.c -
  64. *
  65. * Contributor(s):
  66. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  67. *
  68. *--------------------------------------------------------------------*/
  69. /*====================================================================*
  70. * system header files;
  71. *--------------------------------------------------------------------*/
  72. #include &lt;stdio.h&gt;
  73. #include &lt;stdlib.h&gt;
  74. #include &lt;unistd.h&gt;
  75. #include &lt;fcntl.h&gt;
  76. #include &lt;errno.h&gt;
  77. /*====================================================================*
  78. * custom header files;
  79. *--------------------------------------------------------------------*/
  80. #include &quot;../tools/getoptv.h&quot;
  81. #include &quot;../tools/flags.h&quot;
  82. #include &quot;../tools/error.h&quot;
  83. #include &quot;../tools/files.h&quot;
  84. #include &quot;../nodes/node.h&quot;
  85. #include &quot;../key/HPAVKey.h&quot;
  86. #include &quot;../nvm/nvm.h&quot;
  87. #include &quot;../pib/pib.h&quot;
  88. /*====================================================================*
  89. * custom source files;
  90. *--------------------------------------------------------------------*/
  91. #ifndef MAKEFILE
  92. #include &quot;../tools/getoptv.c&quot;
  93. #include &quot;../tools/putoptv.c&quot;
  94. #include &quot;../tools/version.c&quot;
  95. #include &quot;../tools/checksum32.c&quot;
  96. #include &quot;../tools/fdchecksum32.c&quot;
  97. #include &quot;../tools/checksum32.c&quot;
  98. #include &quot;../tools/hexstring.c&quot;
  99. #include &quot;../tools/hexdecode.c&quot;
  100. #include &quot;../tools/strfbits.c&quot;
  101. #include &quot;../tools/todigit.c&quot;
  102. #include &quot;../tools/output.c&quot;
  103. #include &quot;../tools/emalloc.c&quot;
  104. #include &quot;../tools/error.c&quot;
  105. #endif
  106. #ifndef MAKEFILE
  107. #include &quot;../key/SHA256Reset.c&quot;
  108. #include &quot;../key/SHA256Block.c&quot;
  109. #include &quot;../key/SHA256Write.c&quot;
  110. #include &quot;../key/SHA256Fetch.c&quot;
  111. #include &quot;../key/HPAVKeyNID.c&quot;
  112. #include &quot;../key/keys.c&quot;
  113. #endif
  114. #ifndef MAKEFILE
  115. #include &quot;../nodes/xmlopen.c&quot;
  116. #include &quot;../nodes/xmlnode.c&quot;
  117. #include &quot;../nodes/xmlscan.c&quot;
  118. #include &quot;../nodes/xmledit.c&quot;
  119. #include &quot;../nodes/xmltree.c&quot;
  120. #include &quot;../nodes/xmlfree.c&quot;
  121. #endif
  122. /*====================================================================*
  123. *
  124. * signed pibedit1 (struct node const * node, void * memory, size_t extent);
  125. *
  126. * Contributor(s):
  127. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  128. *
  129. *--------------------------------------------------------------------*/
  130. static signed pibedit1 (struct node const * node, void * memory, size_t extent)
  131. {
  132. struct simple_pib * simple_pib = (struct simple_pib *)(memory);
  133. xmledit (node, memory, extent);
  134. checksum32 (memory, extent, simple_pib-&gt;CHECKSUM);
  135. return (0);
  136. }
  137. /*====================================================================*
  138. *
  139. * signed pibedit2 (char const * filename, struct node const * node, char * memory, size_t extent);
  140. *
  141. * search a panther/lynx image chain looking for PIB images and
  142. * verify each one; return 0 on success or -1 on error; errors
  143. * occur due to an invalid image chain or a bad parameter block;
  144. *
  145. * this implementation reads the parameter block from file into
  146. * into memory and checks it there;
  147. *
  148. * Contributor(s):
  149. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  150. *
  151. *--------------------------------------------------------------------*/
  152. static signed pibedit2 (char const * filename, struct node const * node, char * memory, size_t extent)
  153. {
  154. struct nvm_header2 * nvm_header;
  155. uint32_t origin = ~0;
  156. uint32_t offset = 0;
  157. unsigned length = 0;
  158. unsigned module = 0;
  159. do
  160. {
  161. nvm_header = (struct nvm_header2 *)(memory + offset);
  162. if (LE16TOH (nvm_header-&gt;MajorVersion) != 1)
  163. {
  164. error (1, 0, NVM_HDR_VERSION, filename, module);
  165. }
  166. if (LE16TOH (nvm_header-&gt;MinorVersion) != 1)
  167. {
  168. error (1, 0, NVM_HDR_VERSION, filename, module);
  169. }
  170. if (LE32TOH (nvm_header-&gt;PrevHeader) != origin)
  171. {
  172. error (1, 0, NVM_HDR_LINK, filename, module);
  173. }
  174. if (checksum32 (nvm_header, sizeof (* nvm_header), 0))
  175. {
  176. error (1, 0, NVM_HDR_CHECKSUM, filename, module);
  177. }
  178. origin = offset;
  179. offset += sizeof (* nvm_header);
  180. extent -= sizeof (* nvm_header);
  181. length = LE32TOH (nvm_header-&gt;ImageLength);
  182. if (checksum32 (memory + offset, length, nvm_header-&gt;ImageChecksum))
  183. {
  184. error (1, 0, NVM_IMG_CHECKSUM, filename, module);
  185. }
  186. if (LE32TOH (nvm_header-&gt;ImageType) == NVM_IMAGE_PIB)
  187. {
  188. xmledit (node, memory + offset, length);
  189. nvm_header-&gt;ImageChecksum = checksum32 (memory + offset, length, 0);
  190. nvm_header-&gt;HeaderChecksum = checksum32 (nvm_header, sizeof (* nvm_header), nvm_header-&gt;HeaderChecksum);
  191. return (0);
  192. }
  193. offset += length;
  194. extent -= length;
  195. module++;
  196. }
  197. while (~nvm_header-&gt;NextHeader);
  198. return (-1);
  199. }
  200. /*====================================================================*
  201. *
  202. * signed function (char const * filename);
  203. *
  204. * determine the type of file and call appropriate functions;
  205. *
  206. * Contributor(s):
  207. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  208. *
  209. *--------------------------------------------------------------------*/
  210. static signed function (char const * filename, struct node const * node)
  211. {
  212. char * memory = 0;
  213. signed extent = 0;
  214. signed fd;
  215. if ((fd = open (filename, O_BINARY | O_RDWR)) == -1)
  216. {
  217. error (1, errno, FILE_CANTOPEN, filename);
  218. }
  219. if ((extent = lseek (fd, 0, SEEK_END)) == -1)
  220. {
  221. error (1, errno, FILE_CANTSIZE, filename);
  222. }
  223. if (!(memory = (char *)(malloc (extent))))
  224. {
  225. error (1, errno, FILE_CANTLOAD, filename);
  226. }
  227. if (lseek (fd, 0, SEEK_SET))
  228. {
  229. error (1, errno, FILE_CANTHOME, filename);
  230. }
  231. if (read (fd, memory, extent) != extent)
  232. {
  233. error (1, errno, FILE_CANTREAD, filename);
  234. }
  235. if (LE32TOH (* (uint32_t *)(memory)) == 0x60000000)
  236. {
  237. error (1, 0, FILE_WONTREAD, filename);
  238. }
  239. if (LE32TOH (* (uint32_t *)(memory)) == 0x00010001)
  240. {
  241. pibedit2 (filename, node, memory, extent);
  242. }
  243. else
  244. {
  245. pibedit1 (node, memory, extent);
  246. }
  247. if (lseek (fd, 0, SEEK_SET))
  248. {
  249. error (1, errno, FILE_CANTHOME, filename);
  250. }
  251. if (write (fd, memory, extent) != extent)
  252. {
  253. error (1, errno, FILE_CANTSAVE, filename);
  254. }
  255. close (fd);
  256. return (0);
  257. }
  258. /*====================================================================*
  259. *
  260. * int main (int argc, char const * argv []);
  261. *
  262. *
  263. * Contributor(s):
  264. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  265. *
  266. *--------------------------------------------------------------------*/
  267. int main (int argc, char const * argv [])
  268. {
  269. static char const * optv [] =
  270. {
  271. &quot;f:qv&quot;,
  272. &quot;pib-file [pib-file] [...]&quot;,
  273. &quot;Qualcomm Atheros PLC Parameter File Editor&quot;,
  274. &quot;f f\txmlfile is (f)&quot;,
  275. &quot;q\tquiet&quot;,
  276. &quot;v\tverbose&quot;,
  277. (char const *) (0)
  278. };
  279. struct node * node = (struct node *)(0);
  280. flag_t flags = (flag_t)(0);
  281. signed c;
  282. optind = 1;
  283. while ((c = getoptv (argc, argv, optv)) != -1)
  284. {
  285. switch (c)
  286. {
  287. case 'f':
  288. node = xmlopen (optarg);
  289. break;
  290. case 'm':
  291. _setbits (flags, PIB_MANIFEST);
  292. break;
  293. case 'q':
  294. _setbits (flags, PIB_SILENCE);
  295. break;
  296. case 'v':
  297. _setbits (flags, PIB_VERBOSE);
  298. break;
  299. default:
  300. break;
  301. }
  302. }
  303. argc -= optind;
  304. argv += optind;
  305. while ((argc) &amp;&amp; (* argv))
  306. {
  307. function (* argv, node);
  308. argc--;
  309. argv++;
  310. }
  311. if (_anyset (flags, PIB_VERBOSE))
  312. {
  313. xmltree (node);
  314. }
  315. xmlfree (node);
  316. return (0);
  317. }
  318. </pre>
  319. <div class='footerlink'>
  320. [<a href='WritePIB.c.html' title=' WritePIB.c '>PREV</a>]
  321. [<a href='toolkit.html' title=' Index '>HOME</a>]
  322. [<a href='xmlattribute.c.html' title=' xmlattribute.c '>NEXT</a>]
  323. </div>
  324. </body>
  325. </html>