setpib.c.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. setpib.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='SetNMK.c.html' title=' SetNMK.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='SetProperty.c.html' title=' SetProperty.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. * setpib.c -
  64. *
  65. *
  66. * Contributor(s):
  67. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  68. *
  69. *--------------------------------------------------------------------*/
  70. /*====================================================================*&quot;
  71. * system header files;
  72. *--------------------------------------------------------------------*/
  73. #include &lt;unistd.h&gt;
  74. #include &lt;stdlib.h&gt;
  75. #include &lt;limits.h&gt;
  76. #include &lt;string.h&gt;
  77. #include &lt;ctype.h&gt;
  78. /*====================================================================*
  79. * custom header files;
  80. *--------------------------------------------------------------------*/
  81. #include &quot;../tools/getoptv.h&quot;
  82. #include &quot;../tools/putoptv.h&quot;
  83. #include &quot;../tools/memory.h&quot;
  84. #include &quot;../tools/number.h&quot;
  85. #include &quot;../tools/error.h&quot;
  86. #include &quot;../tools/types.h&quot;
  87. #include &quot;../tools/flags.h&quot;
  88. #include &quot;../tools/files.h&quot;
  89. #include &quot;../pib/pib.h&quot;
  90. #include &quot;../nvm/nvm.h&quot;
  91. /*====================================================================*
  92. * custom source files;
  93. *--------------------------------------------------------------------*/
  94. #ifndef MAKEFILE
  95. #include &quot;../tools/getoptv.c&quot;
  96. #include &quot;../tools/putoptv.c&quot;
  97. #include &quot;../tools/version.c&quot;
  98. #include &quot;../tools/uintspec.c&quot;
  99. #include &quot;../tools/basespec.c&quot;
  100. #include &quot;../tools/dataspec.c&quot;
  101. #include &quot;../tools/bytespec.c&quot;
  102. #include &quot;../tools/todigit.c&quot;
  103. #include &quot;../tools/hexdump.c&quot;
  104. #include &quot;../tools/hexpeek.c&quot;
  105. #include &quot;../tools/fdchecksum32.c&quot;
  106. #include &quot;../tools/checksum32.c&quot;
  107. #include &quot;../tools/memencode.c&quot;
  108. #include &quot;../tools/error.c&quot;
  109. #endif
  110. #ifndef MAKEFILE
  111. #include &quot;../nvm/nvmseek2.c&quot;
  112. #endif
  113. /*====================================================================*
  114. * constants;
  115. *--------------------------------------------------------------------*/
  116. #define SETPIB_VERBOSE (1 &lt;&lt; 0)
  117. #define SETPIB_SILENCE (1 &lt;&lt; 1)
  118. #define SETPIB_HEADERS (1 &lt;&lt; 2)
  119. #define SETPIB_CHANGED (1 &lt;&lt; 3)
  120. #define SETPIB_WINDOW 32
  121. /*====================================================================*
  122. * variables;
  123. *--------------------------------------------------------------------*/
  124. static flag_t flags = (flag_t)(0);
  125. /*====================================================================*
  126. *
  127. * signed modify (void * memory, size_t extent, int argc, char const * argv [], unsigned window)
  128. *
  129. * apply a series of edits to a memory region; edits are specified
  130. * in string vector argv [] which must follow rules understood by
  131. * function memencode(); this function merely walks the vector and
  132. * deals with error and display options;
  133. *
  134. *
  135. * Contributor(s):
  136. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  137. *
  138. *--------------------------------------------------------------------*/
  139. static signed modify (void * memory, size_t extent, int argc, char const * argv [], unsigned window)
  140. {
  141. uint32_t origin;
  142. uint32_t offset;
  143. if (!argc)
  144. {
  145. error (1, ENOTSUP, &quot;Need an offset&quot;);
  146. }
  147. origin = offset = (size_t)(basespec (* argv, 16, sizeof (offset)));
  148. if (offset &gt;= extent)
  149. {
  150. error (1, ECANCELED, &quot;Offset %X exceeds file length of &quot; SIZE_T_SPEC, offset, extent);
  151. }
  152. argc--;
  153. argv++;
  154. if (!argc)
  155. {
  156. _setbits (flags, SETPIB_VERBOSE);
  157. }
  158. while ((argc &gt; 1) &amp;&amp; (* argv))
  159. {
  160. _setbits (flags, SETPIB_CHANGED);
  161. offset += (unsigned)(memencode ((byte *)(memory) + offset, extent - offset, argv [0], argv [1]));
  162. argc -= 2;
  163. argv += 2;
  164. }
  165. if (argc)
  166. {
  167. error (1, ECANCELED, &quot;object %s needs a value&quot;, *argv);
  168. }
  169. if (_anyset (flags, SETPIB_VERBOSE))
  170. {
  171. hexpeek (memory, origin, offset, extent, window, stdout);
  172. }
  173. return (0);
  174. }
  175. /*====================================================================*
  176. *
  177. * signed pibimage1 (signed fd, char const * filename, int argc, char const * argv [], unsigned window);
  178. *
  179. * read an entire flat parameter file into memory, edit it, save
  180. * it and display it;
  181. *
  182. *
  183. * Contributor(s):
  184. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  185. *
  186. *--------------------------------------------------------------------*/
  187. static signed pibimage1 (signed fd, char const * filename, int argc, char const * argv [], unsigned window)
  188. {
  189. off_t extent;
  190. void * memory;
  191. if ((extent = lseek (fd, 0, SEEK_END)) == -1)
  192. {
  193. error (1, errno, FILE_CANTSIZE, filename);
  194. }
  195. if (!(memory = malloc (extent)))
  196. {
  197. error (1, errno, FILE_CANTLOAD, filename);
  198. }
  199. if (lseek (fd, 0, SEEK_SET))
  200. {
  201. error (1, errno, FILE_CANTHOME, filename);
  202. }
  203. if (read (fd, memory, extent) != extent)
  204. {
  205. error (1, errno, FILE_CANTREAD, filename);
  206. }
  207. if (lseek (fd, 0, SEEK_SET))
  208. {
  209. error (1, errno, FILE_CANTHOME, filename);
  210. }
  211. if (modify (memory, extent, argc, argv, window))
  212. {
  213. error (1, errno, FILE_CANTEDIT, filename);
  214. }
  215. if (_anyset (flags, SETPIB_CHANGED))
  216. {
  217. struct pib_header * pib_header = (struct pib_header *)(memory);
  218. pib_header-&gt;CHECKSUM = checksum32 (memory, extent, pib_header-&gt;CHECKSUM);
  219. if (write (fd, memory, extent) != extent)
  220. {
  221. error (1, errno, FILE_CANTSAVE, filename);
  222. }
  223. if (lseek (fd, (off_t)(0) - extent, SEEK_CUR) == -1)
  224. {
  225. error (1, errno, FILE_CANTHOME, filename);
  226. }
  227. }
  228. free (memory);
  229. close (fd);
  230. return (0);
  231. }
  232. /*====================================================================*
  233. *
  234. * signed pibimage2 (signed fd, char const * filename, int argc, char const * argv [], unsigned window);
  235. *
  236. * read an entire flat parameter file into memory, edit it, save
  237. * it and display it;
  238. *
  239. *
  240. * Contributor(s):
  241. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  242. *
  243. *--------------------------------------------------------------------*/
  244. static signed pibimage2 (signed fd, char const * filename, struct nvm_header2 * nvm_header, int argc, char const * argv [], unsigned window)
  245. {
  246. void * memory;
  247. off_t extent = LE32TOH (nvm_header-&gt;ImageLength);
  248. if (!(memory = malloc (extent)))
  249. {
  250. error (1, errno, FILE_CANTLOAD, filename);
  251. }
  252. if (read (fd, memory, extent) != extent)
  253. {
  254. error (1, errno, FILE_CANTREAD, filename);
  255. }
  256. if (lseek (fd, (off_t)(0) - extent, SEEK_CUR) == -1)
  257. {
  258. error (1, errno, FILE_CANTHOME, filename);
  259. }
  260. if (modify (memory, extent, argc, argv, window))
  261. {
  262. error (1, errno, FILE_CANTEDIT, filename);
  263. }
  264. if (_anyset (flags, SETPIB_CHANGED))
  265. {
  266. nvm_header-&gt;ImageChecksum = checksum32 (memory, extent, 0);
  267. if (write (fd, memory, extent) != extent)
  268. {
  269. error (1, errno, FILE_CANTSAVE, filename);
  270. }
  271. if (lseek (fd, (off_t)(0) - extent, SEEK_CUR) == -1)
  272. {
  273. error (1, errno, FILE_CANTHOME, filename);
  274. }
  275. nvm_header-&gt;HeaderChecksum = checksum32 (nvm_header, sizeof (* nvm_header), nvm_header-&gt;HeaderChecksum);
  276. if (lseek (fd, (off_t)(0) - sizeof (* nvm_header), SEEK_CUR) == -1)
  277. {
  278. error (1, errno, FILE_CANTHOME, filename);
  279. }
  280. if (write (fd, nvm_header, sizeof (* nvm_header)) != sizeof (* nvm_header))
  281. {
  282. error (1, errno, FILE_CANTSAVE, filename);
  283. }
  284. if (lseek (fd, (off_t)(0) - sizeof (* nvm_header), SEEK_CUR) == -1)
  285. {
  286. error (1, errno, FILE_CANTHOME, filename);
  287. }
  288. }
  289. free (memory);
  290. return (0);
  291. }
  292. /*====================================================================*
  293. *
  294. * signed function (int argc, char const * argv [], unsigned window);
  295. *
  296. * call an appropriate parameter edit function based on the file
  297. * header;
  298. *
  299. * older parameter files are flat with their own header; newer ones
  300. * are image chains where one of image contains the parameter block;
  301. *
  302. *
  303. * Contributor(s):
  304. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  305. *
  306. *--------------------------------------------------------------------*/
  307. static signed function (int argc, char const * argv [], unsigned window)
  308. {
  309. uint32_t version;
  310. char const * filename = * argv;
  311. signed status = -1;
  312. signed fd;
  313. if ((fd = open (filename, O_BINARY|O_RDWR)) == -1)
  314. {
  315. error (1, errno, FILE_CANTOPEN, filename);
  316. }
  317. if (read (fd, &amp;version, sizeof (version)) != sizeof (version))
  318. {
  319. error (1, errno, FILE_CANTREAD, filename);
  320. }
  321. if (lseek (fd, 0, SEEK_SET))
  322. {
  323. error (1, errno, FILE_CANTHOME, filename);
  324. }
  325. argc--;
  326. argv++;
  327. if (LE32TOH (version) == 0x00010001)
  328. {
  329. struct nvm_header2 nvm_header;
  330. if (!nvmseek2 (fd, filename, &amp;nvm_header, NVM_IMAGE_PIB))
  331. {
  332. status = pibimage2 (fd, filename, &amp;nvm_header, argc, argv, window);
  333. }
  334. }
  335. else
  336. {
  337. status = pibimage1 (fd, filename, argc, argv, window);
  338. }
  339. close (fd);
  340. return (status);
  341. }
  342. /*====================================================================*
  343. *
  344. * int main (int argc, char const * argv []);
  345. *
  346. *
  347. *--------------------------------------------------------------------*/
  348. int main (int argc, char const * argv [])
  349. {
  350. static char const * optv [] =
  351. {
  352. &quot;qvw:x&quot;,
  353. &quot;file base [type data] [type data] [...]\n\n\tstandard-length types are 'byte'|'word'|'long'|'huge'|'hfid'|'mac'|'key'\n\tvariable-length types are 'data'|'text'|'fill'|'skip'&quot;,
  354. &quot;Qualcomm Atheros PIB File Editor&quot;,
  355. &quot;q\tquiet mode&quot;,
  356. &quot;v[v]\tverbose mode&quot;,
  357. &quot;w n\twindow size is (n) [&quot; LITERAL (SETPIB_WINDOW) &quot;]&quot;,
  358. &quot;x\trepair checksum&quot;,
  359. (char const *) (0)
  360. };
  361. unsigned window = SETPIB_WINDOW;
  362. signed c;
  363. optind = 1;
  364. opterr = 1;
  365. while ((c = getoptv (argc, argv, optv)) != -1)
  366. {
  367. switch (c)
  368. {
  369. case 'q':
  370. _setbits (flags, SETPIB_SILENCE);
  371. break;
  372. case 'v':
  373. if (_anyset (flags, SETPIB_VERBOSE))
  374. {
  375. _setbits (flags, SETPIB_HEADERS);
  376. }
  377. _setbits (flags, SETPIB_VERBOSE);
  378. break;
  379. case 'w':
  380. window = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  381. _setbits (flags, SETPIB_VERBOSE);
  382. break;
  383. case 'x':
  384. _setbits (flags, SETPIB_CHANGED);
  385. break;
  386. default:
  387. break;
  388. }
  389. }
  390. argc -= optind;
  391. argv += optind;
  392. if ((argc) &amp;&amp; (* argv))
  393. {
  394. function (argc, argv, window);
  395. }
  396. return (0);
  397. }
  398. </pre>
  399. <div class='footerlink'>
  400. [<a href='SetNMK.c.html' title=' SetNMK.c '>PREV</a>]
  401. [<a href='toolkit.html' title=' Index '>HOME</a>]
  402. [<a href='SetProperty.c.html' title=' SetProperty.c '>NEXT</a>]
  403. </div>
  404. </body>
  405. </html>