chkpib2.c.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. chkpib2.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='chknvm.c.html' title=' chknvm.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='chkpib.c.html' title=' chkpib.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. * chkpib2.c
  64. *
  65. *
  66. * Contributor(s):
  67. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  68. *
  69. *--------------------------------------------------------------------*/
  70. /*====================================================================*
  71. * system header files;
  72. *--------------------------------------------------------------------*/
  73. #include &lt;stdio.h&gt;
  74. #include &lt;stdlib.h&gt;
  75. #include &lt;unistd.h&gt;
  76. #include &lt;fcntl.h&gt;
  77. #include &lt;errno.h&gt;
  78. /*====================================================================*
  79. * custom header files;
  80. *--------------------------------------------------------------------*/
  81. #include &quot;../tools/getoptv.h&quot;
  82. #include &quot;../tools/flags.h&quot;
  83. #include &quot;../tools/error.h&quot;
  84. #include &quot;../tools/files.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/error.c&quot;
  102. #endif
  103. #ifndef MAKEFILE
  104. #include &quot;../key/SHA256Reset.c&quot;
  105. #include &quot;../key/SHA256Block.c&quot;
  106. #include &quot;../key/SHA256Write.c&quot;
  107. #include &quot;../key/SHA256Fetch.c&quot;
  108. #include &quot;../key/HPAVKeyNID.c&quot;
  109. #include &quot;../key/keys.c&quot;
  110. #endif
  111. #ifndef MAKEFILE
  112. #include &quot;../pib/pibpeek1.c&quot;
  113. #include &quot;../pib/pibpeek2.c&quot;
  114. #endif
  115. #ifndef MAKEFILE
  116. #include &quot;../nvm/manifest.c&quot;
  117. #endif
  118. /*====================================================================*
  119. *
  120. * signed pibimage1 (void const * memory, size_t extent, char const * filename, flag_t flags);
  121. *
  122. * check memory-resident thunderbolt/lightning PIB image; return 0
  123. * for a good image and -1 for a bad image;
  124. *
  125. * the check performed here is not exhaustive but it is adequate;
  126. *
  127. *
  128. * Contributor(s):
  129. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  130. *
  131. *--------------------------------------------------------------------*/
  132. static signed pibimage1 (void const * memory, size_t extent, char const * filename, flag_t flags)
  133. {
  134. struct simple_pib * simple_pib = (struct simple_pib *)(memory);
  135. uint8_t NID [HPAVKEY_NID_LEN];
  136. if (_anyset (flags, PIB_VERBOSE))
  137. {
  138. printf (&quot;------- %s -------\n&quot;, filename);
  139. if (pibpeek1 (memory))
  140. {
  141. if (_allclr (flags, PIB_SILENCE))
  142. {
  143. error (0, 0, PIB_BADVERSION, filename);
  144. }
  145. return (-1);
  146. }
  147. }
  148. if (extent != LE16TOH (simple_pib-&gt;PIBLENGTH))
  149. {
  150. if (_allclr (flags, PIB_SILENCE))
  151. {
  152. error (0, 0, PIB_BADLENGTH, filename);
  153. }
  154. return (-1);
  155. }
  156. if (checksum32 (memory, extent, 0))
  157. {
  158. if (_allclr (flags, PIB_SILENCE))
  159. {
  160. error (0, 0, PIB_BADCHECKSUM, filename);
  161. }
  162. return (-1);
  163. }
  164. HPAVKeyNID (NID, simple_pib-&gt;NMK, simple_pib-&gt;PreferredNID [HPAVKEY_NID_LEN-1] &gt;&gt; 4);
  165. if (memcmp (NID, simple_pib-&gt;PreferredNID, sizeof (NID)))
  166. {
  167. if (_allclr (flags, PIB_SILENCE))
  168. {
  169. error (0, 0, PIB_BADNID, filename);
  170. }
  171. return (-1);
  172. }
  173. return (0);
  174. }
  175. /*====================================================================*
  176. *
  177. * signed pibimage2 (void const * memory, size_t extent, char const * filename, flag_t flags);
  178. *
  179. * check memory-resident panther/lynx PIB image; return 0 for a
  180. * good image and -1 for an bad image;
  181. *
  182. * the check performed here is not exhaustive but it is adequate;
  183. *
  184. *
  185. * Contributor(s):
  186. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  187. *
  188. *--------------------------------------------------------------------*/
  189. static signed pibimage2 (void const * memory, size_t extent, char const * filename, flag_t flags)
  190. {
  191. struct simple_pib * simple_pib = (struct simple_pib *)(memory);
  192. struct pib_header * pib_header = (struct pib_header *)(memory);
  193. uint8_t NID [HPAVKEY_NID_LEN];
  194. if (_anyset (flags, PIB_VERBOSE))
  195. {
  196. pib_header-&gt;PIBLENGTH = HTOLE16(extent);
  197. printf (&quot;------- %s -------\n&quot;, filename);
  198. if (pibpeek2 (memory))
  199. {
  200. if (_allclr (flags, PIB_SILENCE))
  201. {
  202. error (0, 0, PIB_BADVERSION, filename);
  203. }
  204. return (-1);
  205. }
  206. memset (pib_header, 0, sizeof (* pib_header));
  207. }
  208. HPAVKeyNID (NID, simple_pib-&gt;NMK, simple_pib-&gt;PreferredNID [HPAVKEY_NID_LEN-1] &gt;&gt; 4);
  209. if (memcmp (NID, simple_pib-&gt;PreferredNID, sizeof (NID)))
  210. {
  211. if (_allclr (flags, PIB_SILENCE))
  212. {
  213. error (0, 0, PIB_BADNID, filename);
  214. }
  215. return (-1);
  216. }
  217. return (0);
  218. }
  219. /*====================================================================*
  220. *
  221. * signed pibchain2 (void const * memory, size_t extent, char const * filename, flag_t flags);
  222. *
  223. * search a panther/lynx image chain looking for PIB images and
  224. * verify each one; return 0 on success or -1 on error; errors
  225. * occur due to an invalid image chain or a bad parameter block;
  226. *
  227. * this implementation reads the parameter block from file into
  228. * into memory and checks it there;
  229. *
  230. *
  231. * Contributor(s):
  232. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  233. *
  234. *--------------------------------------------------------------------*/
  235. static signed pibchain2 (void const * memory, size_t extent, char const * filename, flag_t flags)
  236. {
  237. struct nvm_header2 * nvm_header;
  238. uint32_t origin = ~0;
  239. uint32_t offset = 0;
  240. unsigned module = 0;
  241. do
  242. {
  243. nvm_header = (struct nvm_header2 *)((char *)(memory) + offset);
  244. if (LE16TOH (nvm_header-&gt;MajorVersion) != 1)
  245. {
  246. if (_allclr (flags, NVM_SILENCE))
  247. {
  248. error (0, 0, NVM_HDR_VERSION, filename, module);
  249. }
  250. return (-1);
  251. }
  252. if (LE16TOH (nvm_header-&gt;MinorVersion) != 1)
  253. {
  254. if (_allclr (flags, NVM_SILENCE))
  255. {
  256. error (0, 0, NVM_HDR_VERSION, filename, module);
  257. }
  258. return (-1);
  259. }
  260. if (LE32TOH (nvm_header-&gt;PrevHeader) != origin)
  261. {
  262. if (_allclr (flags, NVM_SILENCE))
  263. {
  264. error (0, 0, NVM_HDR_LINK, filename, module);
  265. }
  266. return (-1);
  267. }
  268. if (checksum32 (nvm_header, sizeof (* nvm_header), 0))
  269. {
  270. if (_allclr (flags, NVM_SILENCE))
  271. {
  272. error (0, 0, NVM_HDR_CHECKSUM, filename, module);
  273. }
  274. return (-1);
  275. }
  276. origin = offset;
  277. offset += sizeof (* nvm_header);
  278. extent -= sizeof (* nvm_header);
  279. if (checksum32 ((char *)(memory) + offset, LE32TOH (nvm_header-&gt;ImageLength), nvm_header-&gt;ImageChecksum))
  280. {
  281. if (_allclr (flags, NVM_SILENCE))
  282. {
  283. error (0, 0, NVM_IMG_CHECKSUM, filename, module);
  284. }
  285. return (-1);
  286. }
  287. if (LE32TOH (nvm_header-&gt;ImageType) == NVM_IMAGE_MANIFEST)
  288. {
  289. if (_anyset (flags, NVM_MANIFEST))
  290. {
  291. printf (&quot;------- %s (%d) -------\n&quot;, filename, module);
  292. manifest ((char *)(memory) + offset, LE32TOH (nvm_header-&gt;ImageLength));
  293. return (0);
  294. }
  295. }
  296. else if (LE32TOH (nvm_header-&gt;ImageType) == NVM_IMAGE_PIB)
  297. {
  298. return (pibimage2 ((char *)(memory) + offset, LE32TOH (nvm_header-&gt;ImageLength), filename, flags));
  299. }
  300. offset += LE32TOH (nvm_header-&gt;ImageLength);
  301. extent -= LE32TOH (nvm_header-&gt;ImageLength);
  302. module++;
  303. }
  304. while (~nvm_header-&gt;NextHeader);
  305. if (extent)
  306. {
  307. if (_allclr (flags, NVM_SILENCE))
  308. {
  309. error (0, errno, NVM_HDR_LINK, filename, module);
  310. }
  311. return (-1);
  312. }
  313. error (0, 0, &quot;%s has no PIB&quot;, filename);
  314. return (-1);
  315. }
  316. /*====================================================================*
  317. *
  318. * signed chkpib (char const * filename, flag_t flags);
  319. *
  320. *
  321. * Contributor(s):
  322. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  323. *
  324. *--------------------------------------------------------------------*/
  325. static signed chkpib (char const * filename, flag_t flags)
  326. {
  327. void * memory = 0;
  328. signed extent = 0;
  329. signed status;
  330. signed fd;
  331. if ((fd = open (filename, O_BINARY|O_RDONLY)) == -1)
  332. {
  333. if (_allclr (flags, NVM_SILENCE))
  334. {
  335. error (0, errno, FILE_CANTOPEN, filename);
  336. }
  337. return (-1);
  338. }
  339. if ((extent = lseek (fd, 0, SEEK_END)) == -1)
  340. {
  341. if (_allclr (flags, NVM_SILENCE))
  342. {
  343. error (0, errno, FILE_CANTSIZE, filename);
  344. }
  345. return (-1);
  346. }
  347. if (!(memory = malloc (extent)))
  348. {
  349. if (_allclr (flags, NVM_SILENCE))
  350. {
  351. error (0, errno, FILE_CANTLOAD, filename);
  352. }
  353. return (-1);
  354. }
  355. if (lseek (fd, 0, SEEK_SET))
  356. {
  357. if (_allclr (flags, NVM_SILENCE))
  358. {
  359. error (0, errno, FILE_CANTHOME, filename);
  360. }
  361. return (-1);
  362. }
  363. if (read (fd, memory, extent) != extent)
  364. {
  365. if (_allclr (flags, NVM_SILENCE))
  366. {
  367. error (0, errno, FILE_CANTREAD, filename);
  368. }
  369. return (-1);
  370. }
  371. close (fd);
  372. if (LE32TOH (* (uint32_t *)(memory)) == 0x60000000)
  373. {
  374. if (_allclr (flags, NVM_SILENCE))
  375. {
  376. error (0, 0, FILE_WONTREAD, filename);
  377. }
  378. status = -1;
  379. }
  380. else if (LE32TOH (* (uint32_t *)(memory)) == 0x00010001)
  381. {
  382. status = pibchain2 (memory, extent, filename, flags);
  383. }
  384. else
  385. {
  386. status = pibimage1 (memory, extent, filename, flags);
  387. }
  388. free (memory);
  389. return (status);
  390. }
  391. /*====================================================================*
  392. *
  393. * int main (int argc, char const * argv []);
  394. *
  395. *
  396. *
  397. * Contributor(s):
  398. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  399. *
  400. *--------------------------------------------------------------------*/
  401. int main (int argc, char const * argv [])
  402. {
  403. static char const * optv [] =
  404. {
  405. &quot;mqv&quot;,
  406. &quot;file [file] [...]&quot;,
  407. &quot;Qualcomm Atheros PLC Parameter File Inspector&quot;,
  408. &quot;m\tdisplay manifest&quot;,
  409. &quot;q\tquiet&quot;,
  410. &quot;v\tverbose messages&quot;,
  411. (char const *) (0)
  412. };
  413. flag_t flags = (flag_t)(0);
  414. signed state = 0;
  415. signed c;
  416. optind = 1;
  417. while ((c = getoptv (argc, argv, optv)) != -1)
  418. {
  419. switch (c)
  420. {
  421. case 'm':
  422. _setbits (flags, PIB_MANIFEST);
  423. break;
  424. case 'q':
  425. _setbits (flags, PIB_SILENCE);
  426. break;
  427. case 'v':
  428. _setbits (flags, PIB_VERBOSE);
  429. break;
  430. default:
  431. break;
  432. }
  433. }
  434. argc -= optind;
  435. argv += optind;
  436. while ((argc) &amp;&amp; (* argv))
  437. {
  438. errno = 0;
  439. if (chkpib (* argv, flags))
  440. {
  441. state = 1;
  442. }
  443. else if (_allclr (flags, (PIB_VERBOSE | PIB_SILENCE | PIB_MANIFEST)))
  444. {
  445. printf (&quot;%s looks good\n&quot;, * argv);
  446. }
  447. argc--;
  448. argv++;
  449. }
  450. return (state);
  451. }
  452. </pre>
  453. <div class='footerlink'>
  454. [<a href='chknvm.c.html' title=' chknvm.c '>PREV</a>]
  455. [<a href='toolkit.html' title=' Index '>HOME</a>]
  456. [<a href='chkpib.c.html' title=' chkpib.c '>NEXT</a>]
  457. </div>
  458. </body>
  459. </html>