getpib.c.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. getpib.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='getoptv.c.html' title=' getoptv.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='GetProperty.c.html' title=' GetProperty.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. /*====================================================================*
  62. *
  63. * getpib.c - PIB Data Extractor
  64. *
  65. * Contributor(s):
  66. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  67. *
  68. *--------------------------------------------------------------------*/
  69. /*====================================================================*
  70. * system header files;
  71. *--------------------------------------------------------------------*/
  72. #include &lt;unistd.h&gt;
  73. #include &lt;stdlib.h&gt;
  74. #include &lt;limits.h&gt;
  75. #include &lt;string.h&gt;
  76. #include &lt;ctype.h&gt;
  77. /*====================================================================*
  78. * custom header files;
  79. *--------------------------------------------------------------------*/
  80. #include &quot;../tools/getoptv.h&quot;
  81. #include &quot;../tools/memory.h&quot;
  82. #include &quot;../tools/number.h&quot;
  83. #include &quot;../tools/error.h&quot;
  84. #include &quot;../tools/types.h&quot;
  85. #include &quot;../tools/flags.h&quot;
  86. #include &quot;../tools/files.h&quot;
  87. #include &quot;../pib/pib.h&quot;
  88. #include &quot;../nvm/nvm.h&quot;
  89. /*====================================================================*
  90. * custom source files;
  91. *--------------------------------------------------------------------*/
  92. #ifndef MAKEFILE
  93. #include &quot;../tools/getoptv.c&quot;
  94. #include &quot;../tools/putoptv.c&quot;
  95. #include &quot;../tools/version.c&quot;
  96. #include &quot;../tools/uintspec.c&quot;
  97. #include &quot;../tools/basespec.c&quot;
  98. #include &quot;../tools/todigit.c&quot;
  99. #include &quot;../tools/hexout.c&quot;
  100. #include &quot;../tools/error.c&quot;
  101. #include &quot;../tools/checksum32.c&quot;
  102. #include &quot;../tools/fdchecksum32.c&quot;
  103. #endif
  104. #ifndef MAKEFILE
  105. #include &quot;../nvm/nvmseek2.c&quot;
  106. #endif
  107. /*====================================================================*
  108. * constants;
  109. *--------------------------------------------------------------------*/
  110. #define GETPIB_COMMA ' '
  111. #define GETPIB_TOOBIG &quot;object '%s' exceeds extent of &quot; SIZE_T_SPEC &quot; bytes&quot;
  112. #define GETPIB_NOSIZE &quot;object '%s' has no length&quot;
  113. #define GETPIB_VERBOSE (1 &lt;&lt; 0)
  114. #define GETPIB_SILENCE (1 &lt;&lt; 1)
  115. #define GETPIB_NEWLINE (1 &lt;&lt; 2)
  116. /*====================================================================*
  117. *
  118. * void getmemory (byte const * memory, size_t extent, char const * object, size_t length);
  119. *
  120. *--------------------------------------------------------------------*/
  121. static void getmemory (byte const * memory, size_t extent, char const * object, size_t length)
  122. {
  123. if (length &gt; extent)
  124. {
  125. error (1, ECANCELED, GETPIB_TOOBIG, object, length);
  126. }
  127. hexout (memory, length, ':', '\0', stdout);
  128. return;
  129. }
  130. /*====================================================================*
  131. *
  132. * void getstring (byte const * memory, size_t extent, char const * object, size_t length);
  133. *
  134. *--------------------------------------------------------------------*/
  135. static void getstring (byte const * memory, size_t extent, char const * object, size_t length)
  136. {
  137. char const * string = (char const *) (memory);
  138. if (length &gt; extent)
  139. {
  140. error (1, ECANCELED, GETPIB_TOOBIG, object, length);
  141. }
  142. while (isprint (* string) &amp;&amp; (length--))
  143. {
  144. putc (* string++, stdout);
  145. }
  146. return;
  147. }
  148. /*====================================================================*
  149. *
  150. * void snatch (int argc, char const * argv [], byte const * memory, size_t extent, char comma);
  151. *
  152. * extract and print the specified data objects from memory; comma
  153. * delimits consecutive objects on output;
  154. *
  155. *--------------------------------------------------------------------*/
  156. static void snatch (int argc, char const * argv [], byte const * memory, size_t extent, char comma)
  157. {
  158. size_t length = 0;
  159. size_t offset = 0;
  160. if (! (argc) || ! (* argv))
  161. {
  162. error (1, ECANCELED, &quot;Need an offset&quot;);
  163. }
  164. offset = (size_t) (basespec (* argv, 16, sizeof (uint32_t)));
  165. if (offset &gt; extent)
  166. {
  167. error (1, ECANCELED, &quot;offset &quot; SIZE_T_SPEC &quot; exceeds extent of &quot; SIZE_T_SPEC &quot; bytes&quot;, offset, extent);
  168. }
  169. memory += offset;
  170. extent -= offset;
  171. argc--;
  172. argv++;
  173. while ((argc) &amp;&amp; (* argv))
  174. {
  175. char const * object = * argv;
  176. argc--;
  177. argv++;
  178. if (! strcmp (object, &quot;byte&quot;))
  179. {
  180. uint8_t * number = (uint8_t *) (memory);
  181. if (sizeof (* number) &gt; extent)
  182. {
  183. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  184. }
  185. printf (&quot;%u&quot;, * number);
  186. memory += sizeof (* number);
  187. extent -= sizeof (* number);
  188. }
  189. else if (! strcmp (object, &quot;word&quot;))
  190. {
  191. uint16_t * number = (uint16_t *) (memory);
  192. if (sizeof (* number) &gt; extent)
  193. {
  194. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  195. }
  196. printf (&quot;%u&quot;, LE16TOH (* number));
  197. memory += sizeof (* number);
  198. extent -= sizeof (* number);
  199. }
  200. else if (! strcmp (object, &quot;long&quot;))
  201. {
  202. uint32_t * number = (uint32_t *) (memory);
  203. if (sizeof (* number) &gt; extent)
  204. {
  205. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  206. }
  207. printf (&quot;%u&quot;, LE32TOH (* number));
  208. memory += sizeof (* number);
  209. extent -= sizeof (* number);
  210. }
  211. else if (! strcmp (object, &quot;huge&quot;))
  212. {
  213. uint64_t * number = (uint64_t *) (memory);
  214. if (sizeof (* number) &gt; extent)
  215. {
  216. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  217. }
  218. printf (&quot;%llu&quot;, LE64TOH (* number));
  219. memory += sizeof (* number);
  220. extent -= sizeof (* number);
  221. }
  222. #if 1
  223. else if (! strcmp (object, &quot;xbyte&quot;))
  224. {
  225. uint8_t * number = (uint8_t *) (memory);
  226. if (sizeof (* number) &gt; extent)
  227. {
  228. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  229. }
  230. printf (&quot;0x%02X&quot;, * number);
  231. memory += sizeof (* number);
  232. extent -= sizeof (* number);
  233. }
  234. else if (! strcmp (object, &quot;xword&quot;))
  235. {
  236. uint16_t * number = (uint16_t *) (memory);
  237. if (sizeof (* number) &gt; extent)
  238. {
  239. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  240. }
  241. printf (&quot;0x%04X&quot;, LE16TOH (* number));
  242. memory += sizeof (* number);
  243. extent -= sizeof (* number);
  244. }
  245. else if (! strcmp (object, &quot;xlong&quot;))
  246. {
  247. uint32_t * number = (uint32_t *) (memory);
  248. if (sizeof (* number) &gt; extent)
  249. {
  250. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  251. }
  252. printf (&quot;0x%08X&quot;, LE32TOH (* number));
  253. memory += sizeof (* number);
  254. extent -= sizeof (* number);
  255. }
  256. else if (! strcmp (object, &quot;xhuge&quot;))
  257. {
  258. uint64_t * number = (uint64_t *) (memory);
  259. if (sizeof (* number) &gt; extent)
  260. {
  261. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  262. }
  263. printf (&quot;0x%016llX&quot;, LE64TOH (* number));
  264. memory += sizeof (* number);
  265. extent -= sizeof (* number);
  266. }
  267. #endif
  268. else if (! strcmp (object, &quot;mac&quot;))
  269. {
  270. length = ETHER_ADDR_LEN;
  271. if (length &gt; extent)
  272. {
  273. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  274. }
  275. getmemory (memory, extent, object, length);
  276. memory += length;
  277. extent -= length;
  278. }
  279. else if (! strcmp (object, &quot;key&quot;))
  280. {
  281. length = PIB_KEY_LEN;
  282. if (length &gt; extent)
  283. {
  284. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  285. }
  286. getmemory (memory, extent, object, length);
  287. memory += length;
  288. extent -= length;
  289. }
  290. else if (! strcmp (object, &quot;hfid&quot;))
  291. {
  292. length = PIB_HFID_LEN;
  293. if (length &gt; extent)
  294. {
  295. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  296. }
  297. getstring (memory, extent, object, length);
  298. memory += length;
  299. extent -= length;
  300. }
  301. #if 1
  302. else if (! strcmp (object, &quot;adminusername&quot;) || ! strcmp (object, &quot;adminpassword&quot;) || ! strcmp (object, &quot;accessusername&quot;))
  303. {
  304. length = PIB_NAME_LEN + 1;
  305. if (length &gt; extent)
  306. {
  307. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  308. }
  309. getstring (memory, extent, object, length);
  310. memory += length;
  311. extent -= length;
  312. }
  313. else if (! strcmp (object, &quot;accesspassword&quot;))
  314. {
  315. length = PIB_HFID_LEN + 1;
  316. if (length &gt; extent)
  317. {
  318. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  319. }
  320. getstring (memory, extent, object, length);
  321. memory += length;
  322. extent -= length;
  323. }
  324. else if (! strcmp (object, &quot;username&quot;) || ! strcmp (object, &quot;password&quot;) || ! strcmp (object, &quot;url&quot;))
  325. {
  326. length = PIB_TEXT_LEN + 1;
  327. getstring (memory, extent, object, length);
  328. memory += length;
  329. extent -= length;
  330. }
  331. #endif
  332. else if (! strcmp (object, &quot;data&quot;))
  333. {
  334. if (! * argv)
  335. {
  336. error (1, EINVAL, GETPIB_NOSIZE, object);
  337. }
  338. length = (unsigned) (uintspec (* argv, 1, extent));
  339. hexout (memory, length, 0, 0, stdout);
  340. memory += length;
  341. extent -= length;
  342. argc--;
  343. argv++;
  344. }
  345. else if (! strcmp (object, &quot;text&quot;))
  346. {
  347. if (! * argv)
  348. {
  349. error (1, EINVAL, GETPIB_NOSIZE, object);
  350. }
  351. length = (unsigned) (uintspec (* argv, 1, extent));
  352. getstring (memory, extent, object, length);
  353. memory += length;
  354. extent -= length;
  355. argc--;
  356. argv++;
  357. }
  358. else if (! strcmp (object, &quot;skip&quot;))
  359. {
  360. if (! * argv)
  361. {
  362. error (1, EINVAL, GETPIB_NOSIZE, object);
  363. }
  364. length = (unsigned) (uintspec (* argv, 1, extent));
  365. memory += length;
  366. extent -= length;
  367. argc--;
  368. argv++;
  369. continue;
  370. }
  371. else
  372. {
  373. error (1, ENOTSUP, &quot;%s&quot;, object);
  374. }
  375. if ((argc) &amp;&amp; (* argv))
  376. {
  377. putc (comma, stdout);
  378. }
  379. }
  380. return;
  381. }
  382. /*====================================================================*
  383. *
  384. * signed pibimage1 (int argc, char const * argv [], char comma);
  385. *
  386. * read an entire flat parameter file into memory, edit it, save
  387. * it and display it;
  388. *
  389. * Contributor(s):
  390. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  391. *
  392. *--------------------------------------------------------------------*/
  393. static signed pibimage1 (int argc, char const * argv [], char comma)
  394. {
  395. signed fd;
  396. off_t extent;
  397. byte * memory;
  398. if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1)
  399. {
  400. error (1, errno, FILE_CANTOPEN, * argv);
  401. }
  402. if ((extent = lseek (fd, 0, SEEK_END)) == - 1)
  403. {
  404. error (1, errno, FILE_CANTSIZE, * argv);
  405. }
  406. if (lseek (fd, 0, SEEK_SET))
  407. {
  408. error (1, errno, FILE_CANTHOME, * argv);
  409. }
  410. if (! (memory = malloc (extent)))
  411. {
  412. error (1, errno, FILE_CANTLOAD, * argv);
  413. }
  414. if (read (fd, memory, extent) != extent)
  415. {
  416. error (1, errno, FILE_CANTREAD, * argv);
  417. }
  418. close (fd);
  419. snatch (argc - 1, argv + 1, memory, extent, comma);
  420. free (memory);
  421. return (0);
  422. }
  423. /*====================================================================*
  424. *
  425. * signed pibimage2 (int argc, char const * argv [], char comma);
  426. *
  427. * read an entire flat parameter file into memory, edit it, save
  428. * it and display it;
  429. *
  430. * Contributor(s):
  431. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  432. *
  433. *--------------------------------------------------------------------*/
  434. static signed pibimage2 (int argc, char const * argv [], char comma)
  435. {
  436. struct nvm_header2 header;
  437. signed fd;
  438. off_t extent;
  439. byte * memory;
  440. if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1)
  441. {
  442. error (1, errno, FILE_CANTOPEN, * argv);
  443. }
  444. if (nvmseek2 (fd, * argv, &amp; header, NVM_IMAGE_PIB))
  445. {
  446. error (1, errno, &quot;Can't find PIB image in %s&quot;, * argv);
  447. }
  448. extent = LE32TOH (header.ImageLength);
  449. if (! (memory = malloc (extent)))
  450. {
  451. error (1, errno, FILE_CANTLOAD, * argv);
  452. }
  453. if (read (fd, memory, extent) != extent)
  454. {
  455. error (1, errno, FILE_CANTREAD, * argv);
  456. }
  457. close (fd);
  458. snatch (argc - 1, argv + 1, memory, extent, comma);
  459. free (memory);
  460. return (0);
  461. }
  462. /*====================================================================*
  463. *
  464. * signed function (int argc, char const * argv [], char comma);
  465. *
  466. * call an appropriate parameter edit function based on the file
  467. * header;
  468. *
  469. * older parameter files are flat with their own header; newer ones
  470. * are image chains where one of image contains the parameter block;
  471. *
  472. *
  473. * Contributor(s):
  474. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  475. *
  476. *--------------------------------------------------------------------*/
  477. static signed function (int argc, char const * argv [], char comma)
  478. {
  479. uint32_t version;
  480. signed status;
  481. signed fd;
  482. if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1)
  483. {
  484. error (1, errno, FILE_CANTOPEN, * argv);
  485. }
  486. if (read (fd, &amp; version, sizeof (version)) != sizeof (version))
  487. {
  488. error (1, errno, FILE_CANTREAD, * argv);
  489. }
  490. close (fd);
  491. if (LE32TOH (version) == 0x00010001)
  492. {
  493. status = pibimage2 (argc, argv, comma);
  494. }
  495. else
  496. {
  497. status = pibimage1 (argc, argv, comma);
  498. }
  499. return (status);
  500. }
  501. /*====================================================================*
  502. *
  503. * int main (int argc, char const * argv []);
  504. *
  505. *
  506. *--------------------------------------------------------------------*/
  507. int main (int argc, char const * argv [])
  508. {
  509. static char const * optv [] =
  510. {
  511. &quot;c:qvn&quot;,
  512. &quot;file offset type [size]\n\n\tstandard-length types are 'byte'|'word'|'long'|'huge'|'hfid'|'mac'|'key'\n\tvariable-length types are 'data'|'text'|'skip' and need a size&quot;,
  513. &quot;PIB Data Extractor&quot;,
  514. &quot;c c\tobject separator is (c) [&quot; LITERAL (GETPIB_COMMA) &quot;]&quot;,
  515. &quot;n\tappend newline&quot;,
  516. &quot;q\tquiet mode&quot;,
  517. &quot;v\tverbose mode&quot;,
  518. (char const *) (0)
  519. };
  520. flag_t flags = (flag_t) (0);
  521. char comma = GETPIB_COMMA;
  522. signed c;
  523. optind = 1;
  524. opterr = 1;
  525. while (~ (c = getoptv (argc, argv, optv)))
  526. {
  527. switch (c)
  528. {
  529. case 'c':
  530. comma = * optarg;
  531. break;
  532. case 'n':
  533. _setbits (flags, GETPIB_NEWLINE);
  534. break;
  535. case 'q':
  536. _setbits (flags, GETPIB_SILENCE);
  537. break;
  538. case 'v':
  539. _setbits (flags, GETPIB_VERBOSE);
  540. break;
  541. default:
  542. break;
  543. }
  544. }
  545. argc -= optind;
  546. argv += optind;
  547. if ((argc) &amp;&amp; (* argv))
  548. {
  549. function (argc, argv, comma);
  550. if (_anyset (flags, GETPIB_NEWLINE))
  551. {
  552. putc ('\n', stdout);
  553. }
  554. }
  555. return (0);
  556. }
  557. </pre>
  558. <div class='footerlink'>
  559. [<a href='getoptv.c.html' title=' getoptv.c '>PREV</a>]
  560. [<a href='toolkit.html' title=' Index '>HOME</a>]
  561. [<a href='GetProperty.c.html' title=' GetProperty.c '>NEXT</a>]
  562. </div>
  563. </body>
  564. </html>