chkpib.c.html 14 KB

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