xml2pib.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. * xml2pib.c -
  44. *
  45. * Contributor(s):
  46. * Charles Maier
  47. *
  48. *--------------------------------------------------------------------*/
  49. /*====================================================================*
  50. * system header files;
  51. *--------------------------------------------------------------------*/
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <unistd.h>
  55. #include <fcntl.h>
  56. #include <errno.h>
  57. /*====================================================================*
  58. * custom header files;
  59. *--------------------------------------------------------------------*/
  60. #include "../tools/getoptv.h"
  61. #include "../tools/flags.h"
  62. #include "../tools/error.h"
  63. #include "../tools/files.h"
  64. #include "../nodes/node.h"
  65. #include "../key/HPAVKey.h"
  66. #include "../nvm/nvm.h"
  67. #include "../pib/pib.h"
  68. /*====================================================================*
  69. * custom source files;
  70. *--------------------------------------------------------------------*/
  71. #ifndef MAKEFILE
  72. #include "../tools/getoptv.c"
  73. #include "../tools/putoptv.c"
  74. #include "../tools/version.c"
  75. #include "../tools/checksum32.c"
  76. #include "../tools/fdchecksum32.c"
  77. #include "../tools/checksum32.c"
  78. #include "../tools/hexstring.c"
  79. #include "../tools/hexdecode.c"
  80. #include "../tools/strfbits.c"
  81. #include "../tools/todigit.c"
  82. #include "../tools/output.c"
  83. #include "../tools/emalloc.c"
  84. #include "../tools/error.c"
  85. #endif
  86. #ifndef MAKEFILE
  87. #include "../key/SHA256Reset.c"
  88. #include "../key/SHA256Block.c"
  89. #include "../key/SHA256Write.c"
  90. #include "../key/SHA256Fetch.c"
  91. #include "../key/HPAVKeyNID.c"
  92. #include "../key/keys.c"
  93. #endif
  94. #ifndef MAKEFILE
  95. #include "../nodes/xmlopen.c"
  96. #include "../nodes/xmlnode.c"
  97. #include "../nodes/xmlscan.c"
  98. #include "../nodes/xmledit.c"
  99. #include "../nodes/xmltree.c"
  100. #include "../nodes/xmlfree.c"
  101. #endif
  102. /*====================================================================*
  103. *
  104. * signed pibedit1 (struct node const * node, void * memory, size_t extent);
  105. *
  106. * Contributor(s):
  107. * Charles Maier
  108. *
  109. *--------------------------------------------------------------------*/
  110. static signed pibedit1 (struct node const * node, void * memory, size_t extent)
  111. {
  112. struct simple_pib * simple_pib = (struct simple_pib *)(memory);
  113. xmledit (node, memory, extent);
  114. checksum32 (memory, extent, simple_pib->CHECKSUM);
  115. return (0);
  116. }
  117. /*====================================================================*
  118. *
  119. * signed pibedit2 (char const * filename, struct node const * node, char * memory, size_t extent);
  120. *
  121. * search a panther/lynx image chain looking for PIB images and
  122. * verify each one; return 0 on success or -1 on error; errors
  123. * occur due to an invalid image chain or a bad parameter block;
  124. *
  125. * this implementation reads the parameter block from file into
  126. * into memory and checks it there;
  127. *
  128. * Contributor(s):
  129. * Charles Maier
  130. *
  131. *--------------------------------------------------------------------*/
  132. static signed pibedit2 (char const * filename, struct node const * node, char * memory, size_t extent)
  133. {
  134. struct nvm_header2 * nvm_header;
  135. uint32_t origin = ~0;
  136. uint32_t offset = 0;
  137. unsigned length = 0;
  138. unsigned module = 0;
  139. do
  140. {
  141. nvm_header = (struct nvm_header2 *)(memory + offset);
  142. if (LE16TOH (nvm_header->MajorVersion) != 1)
  143. {
  144. error (1, 0, NVM_HDR_VERSION, filename, module);
  145. }
  146. if (LE16TOH (nvm_header->MinorVersion) != 1)
  147. {
  148. error (1, 0, NVM_HDR_VERSION, filename, module);
  149. }
  150. if (LE32TOH (nvm_header->PrevHeader) != origin)
  151. {
  152. error (1, 0, NVM_HDR_LINK, filename, module);
  153. }
  154. if (checksum32 (nvm_header, sizeof (* nvm_header), 0))
  155. {
  156. error (1, 0, NVM_HDR_CHECKSUM, filename, module);
  157. }
  158. origin = offset;
  159. offset += sizeof (* nvm_header);
  160. extent -= sizeof (* nvm_header);
  161. length = LE32TOH (nvm_header->ImageLength);
  162. if (checksum32 (memory + offset, length, nvm_header->ImageChecksum))
  163. {
  164. error (1, 0, NVM_IMG_CHECKSUM, filename, module);
  165. }
  166. if (LE32TOH (nvm_header->ImageType) == NVM_IMAGE_PIB)
  167. {
  168. xmledit (node, memory + offset, length);
  169. nvm_header->ImageChecksum = checksum32 (memory + offset, length, 0);
  170. nvm_header->HeaderChecksum = checksum32 (nvm_header, sizeof (* nvm_header), nvm_header->HeaderChecksum);
  171. return (0);
  172. }
  173. offset += length;
  174. extent -= length;
  175. module++;
  176. }
  177. while (~nvm_header->NextHeader);
  178. return (-1);
  179. }
  180. /*====================================================================*
  181. *
  182. * signed function (char const * filename);
  183. *
  184. * determine the type of file and call appropriate functions;
  185. *
  186. * Contributor(s):
  187. * Charles Maier
  188. *
  189. *--------------------------------------------------------------------*/
  190. static signed function (char const * filename, struct node const * node)
  191. {
  192. char * memory = 0;
  193. signed extent = 0;
  194. signed fd;
  195. if ((fd = open (filename, O_BINARY | O_RDWR)) == -1)
  196. {
  197. error (1, errno, FILE_CANTOPEN, filename);
  198. }
  199. if ((extent = lseek (fd, 0, SEEK_END)) == -1)
  200. {
  201. error (1, errno, FILE_CANTSIZE, filename);
  202. }
  203. if (!(memory = (char *)(malloc (extent))))
  204. {
  205. error (1, errno, FILE_CANTLOAD, filename);
  206. }
  207. if (lseek (fd, 0, SEEK_SET))
  208. {
  209. error (1, errno, FILE_CANTHOME, filename);
  210. }
  211. if (read (fd, memory, extent) != extent)
  212. {
  213. error (1, errno, FILE_CANTREAD, filename);
  214. }
  215. if (LE32TOH (* (uint32_t *)(memory)) == 0x60000000)
  216. {
  217. error (1, 0, FILE_WONTREAD, filename);
  218. }
  219. if (LE32TOH (* (uint32_t *)(memory)) == 0x00010001)
  220. {
  221. pibedit2 (filename, node, memory, extent);
  222. }
  223. else
  224. {
  225. pibedit1 (node, memory, extent);
  226. }
  227. if (lseek (fd, 0, SEEK_SET))
  228. {
  229. error (1, errno, FILE_CANTHOME, filename);
  230. }
  231. if (write (fd, memory, extent) != extent)
  232. {
  233. error (1, errno, FILE_CANTSAVE, filename);
  234. }
  235. close (fd);
  236. return (0);
  237. }
  238. /*====================================================================*
  239. *
  240. * int main (int argc, char const * argv []);
  241. *
  242. *
  243. * Contributor(s):
  244. * Charles Maier
  245. *
  246. *--------------------------------------------------------------------*/
  247. int main (int argc, char const * argv [])
  248. {
  249. static char const * optv [] =
  250. {
  251. "f:qv",
  252. "pib-file [pib-file] [...]",
  253. "Qualcomm Atheros PLC Parameter File Editor",
  254. "f f\txmlfile is (f)",
  255. "q\tquiet",
  256. "v\tverbose",
  257. (char const *) (0)
  258. };
  259. struct node * node = (struct node *)(0);
  260. flag_t flags = (flag_t)(0);
  261. signed c;
  262. optind = 1;
  263. while ((c = getoptv (argc, argv, optv)) != -1)
  264. {
  265. switch (c)
  266. {
  267. case 'f':
  268. node = xmlopen (optarg);
  269. break;
  270. case 'm':
  271. _setbits (flags, PIB_MANIFEST);
  272. break;
  273. case 'q':
  274. _setbits (flags, PIB_SILENCE);
  275. break;
  276. case 'v':
  277. _setbits (flags, PIB_VERBOSE);
  278. break;
  279. default:
  280. break;
  281. }
  282. }
  283. argc -= optind;
  284. argv += optind;
  285. while ((argc) && (* argv))
  286. {
  287. function (* argv, node);
  288. argc--;
  289. argv++;
  290. }
  291. if (_anyset (flags, PIB_VERBOSE))
  292. {
  293. xmltree (node);
  294. }
  295. xmlfree (node);
  296. return (0);
  297. }