chknvm2.c.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. chknvm2.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='chipset.c.html' title=' chipset.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='chknvm.c.html' title=' chknvm.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. * chknvm2.c -
  64. *
  65. *
  66. * Contributor(s):
  67. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  68. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  69. *
  70. *--------------------------------------------------------------------*/
  71. /*====================================================================*
  72. * system header files;
  73. *--------------------------------------------------------------------*/
  74. #include &lt;stdio.h&gt;
  75. #include &lt;stdlib.h&gt;
  76. #include &lt;unistd.h&gt;
  77. #include &lt;memory.h&gt;
  78. #include &lt;fcntl.h&gt;
  79. #include &lt;errno.h&gt;
  80. /*====================================================================*
  81. * custom header files;
  82. *--------------------------------------------------------------------*/
  83. #include &quot;../tools/getoptv.h&quot;
  84. #include &quot;../tools/memory.h&quot;
  85. #include &quot;../tools/flags.h&quot;
  86. #include &quot;../tools/error.h&quot;
  87. #include &quot;../tools/files.h&quot;
  88. #include &quot;../ram/sdram.h&quot;
  89. #include &quot;../nvm/nvm.h&quot;
  90. /*====================================================================*
  91. * custom source files;
  92. *--------------------------------------------------------------------*/
  93. #ifndef MAKEFILE
  94. #include &quot;../tools/getoptv.c&quot;
  95. #include &quot;../tools/putoptv.c&quot;
  96. #include &quot;../tools/version.c&quot;
  97. #include &quot;../tools/checksum32.c&quot;
  98. #include &quot;../tools/strfbits.c&quot;
  99. #include &quot;../tools/error.c&quot;
  100. #endif
  101. #ifndef MAKEFILE
  102. #include &quot;../nvm/nvm.c&quot;
  103. #include &quot;../nvm/nvmpeek.c&quot;
  104. #include &quot;../nvm/nvmpeek1.c&quot;
  105. #include &quot;../nvm/nvmpeek2.c&quot;
  106. #include &quot;../nvm/manifest.c&quot;
  107. #endif
  108. /*====================================================================*
  109. * program constants;
  110. *--------------------------------------------------------------------*/
  111. #define HARDWARE 0
  112. #define SOFTWARE 1
  113. #define VER 2
  114. #define REV 3
  115. #define BUILD 6
  116. #define DATE 7
  117. /*====================================================================*
  118. *
  119. * unsigned string2vector (char ** vector, length, char * string, char c);
  120. *
  121. * convert string to a vector and return vector count; split string
  122. * on characer (c);
  123. *
  124. *
  125. * Contributor(s):
  126. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  127. *
  128. *--------------------------------------------------------------------*/
  129. static unsigned string2vector (char ** vector, char * string, char c)
  130. {
  131. char ** origin = vector;
  132. for (*vector++ = string; *string; string++)
  133. {
  134. if (*string == c)
  135. {
  136. *string++ = (char)(0);
  137. *vector++ = string--;
  138. }
  139. }
  140. *vector = (char *)(0);
  141. return ((unsigned)(vector - origin));
  142. }
  143. /*====================================================================*
  144. *
  145. * void firmware (char const * filename, unsigned module, char const * memory, unsigned offset, flag_t flags);
  146. *
  147. * locate and display information stored in a firmware image; this
  148. * is magic that may change in future firmware releases;
  149. *
  150. *
  151. * Contributor(s):
  152. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  153. *
  154. *--------------------------------------------------------------------*/
  155. static void firmware (char const * filename, unsigned module, char const * memory, unsigned offset, flag_t flags)
  156. {
  157. if (_anyset (flags, NVM_FIRMWARE))
  158. {
  159. if ((* memory &gt; 0x20) &amp;&amp; (* memory &lt; 0x7f))
  160. {
  161. printf (&quot;%s (%d) %s\n&quot;, filename, module, memory);
  162. }
  163. else
  164. {
  165. printf (&quot;%s (%d) %s\n&quot;, filename, module, memory + offset);
  166. }
  167. return;
  168. }
  169. if (_anyset (flags, NVM_IDENTITY))
  170. {
  171. char * vector [16];
  172. char buffer [256];
  173. if ((* memory &gt; 0x20) &amp;&amp; (* memory &lt; 0x7f))
  174. {
  175. strncpy (buffer, memory, sizeof (buffer));
  176. }
  177. else
  178. {
  179. strncpy (buffer, memory + offset, sizeof (buffer));
  180. }
  181. string2vector (vector, buffer, '-');
  182. printf (&quot;%s &quot;, vector [HARDWARE]);
  183. printf (&quot;%04d &quot;, atoi (vector [BUILD]));
  184. printf (&quot;%s &quot;, vector [DATE]);
  185. printf (&quot;%s.&quot;, vector [VER]);
  186. printf (&quot;%s &quot;, vector [REV]);
  187. printf (&quot;%s (%d)\n&quot;, filename, module);
  188. return;
  189. }
  190. return;
  191. }
  192. /*====================================================================*
  193. *
  194. * signed nvmimage2 (void const * memory, size_t extent, char const * filename, flag_t flags)
  195. *
  196. * verify a firmware image chain stored in memory; return 0 on
  197. * success or -1 on error;
  198. *
  199. *
  200. * Contributor(s):
  201. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  202. *
  203. *--------------------------------------------------------------------*/
  204. static signed nvmimage2 (void const * memory, size_t extent, char const * filename, flag_t flags)
  205. {
  206. struct nvm_header1 * nvm_header;
  207. unsigned module = 0;
  208. uint32_t offset = 0;
  209. do
  210. {
  211. nvm_header = (struct nvm_header1 *)((char *)(memory) + offset);
  212. if (LE32TOH (nvm_header-&gt;HEADERVERSION) != 0x60000000)
  213. {
  214. if (_allclr (flags, NVM_SILENCE))
  215. {
  216. error (0, errno, NVM_HDR_VERSION, filename, module);
  217. }
  218. return (-1);
  219. }
  220. if (checksum32 (nvm_header, sizeof (* nvm_header), 0))
  221. {
  222. if (_allclr (flags, NVM_SILENCE))
  223. {
  224. error (0, errno, NVM_HDR_CHECKSUM, filename, module);
  225. }
  226. return (-1);
  227. }
  228. offset += sizeof (* nvm_header);
  229. extent -= sizeof (* nvm_header);
  230. if (_anyset (flags, NVM_VERBOSE))
  231. {
  232. printf (&quot;------- %s (%d) -------\n&quot;, filename, module);
  233. nvmpeek1 (nvm_header);
  234. }
  235. if (LE32TOH (nvm_header-&gt;IMAGETYPE) == NVM_IMAGE_FIRMWARE)
  236. {
  237. firmware (filename, module, (char *)(memory) + offset, 0x70, flags);
  238. }
  239. if (checksum32 ((char *)(memory) + offset, LE32TOH (nvm_header-&gt;IMAGELENGTH), nvm_header-&gt;IMAGECHECKSUM))
  240. {
  241. if (_allclr (flags, NVM_SILENCE))
  242. {
  243. error (0, errno, NVM_IMG_CHECKSUM, filename, module);
  244. }
  245. return (-1);
  246. }
  247. offset += LE32TOH (nvm_header-&gt;IMAGELENGTH);
  248. extent -= LE32TOH (nvm_header-&gt;IMAGELENGTH);
  249. module++;
  250. }
  251. while (nvm_header-&gt;NEXTHEADER);
  252. if (extent)
  253. {
  254. if (_allclr (flags, NVM_SILENCE))
  255. {
  256. error (0, errno, NVM_HDR_LINK, filename, module);
  257. }
  258. }
  259. return ((signed)(extent));
  260. }
  261. /*====================================================================*
  262. *
  263. * signed nvmchain2 (void const * memory, size_t extent, char const * filename, flag_t flags)
  264. *
  265. * verify a firmware image chain stored in memory; return 0 on
  266. * success or -1 on error;
  267. *
  268. *
  269. * Contributor(s):
  270. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  271. *
  272. *--------------------------------------------------------------------*/
  273. static signed nvmchain2 (void const * memory, size_t extent, char const * filename, flag_t flags)
  274. {
  275. struct nvm_header2 * nvm_header;
  276. unsigned module = 0;
  277. uint32_t origin = ~0;
  278. uint32_t offset = 0;
  279. uint32_t length = 0;
  280. do
  281. {
  282. nvm_header = (struct nvm_header2 *)((char *)(memory) + offset);
  283. if (LE16TOH (nvm_header-&gt;MajorVersion) != 1)
  284. {
  285. if (_allclr (flags, NVM_SILENCE))
  286. {
  287. error (0, errno, NVM_HDR_VERSION, filename, module);
  288. }
  289. return (-1);
  290. }
  291. if (LE16TOH (nvm_header-&gt;MinorVersion) != 1)
  292. {
  293. if (_allclr (flags, NVM_SILENCE))
  294. {
  295. error (0, errno, NVM_HDR_VERSION, filename, module);
  296. }
  297. return (-1);
  298. }
  299. if (LE32TOH (nvm_header-&gt;PrevHeader) != origin)
  300. {
  301. if (_allclr (flags, NVM_SILENCE))
  302. {
  303. error (0, errno, NVM_HDR_LINK, filename, module);
  304. }
  305. return (-1);
  306. }
  307. if (checksum32 (nvm_header, sizeof (* nvm_header), 0))
  308. {
  309. if (_allclr (flags, NVM_SILENCE))
  310. {
  311. error (0, 0, NVM_HDR_CHECKSUM, filename, module);
  312. }
  313. return (-1);
  314. }
  315. origin = offset;
  316. offset += sizeof (* nvm_header);
  317. extent -= sizeof (* nvm_header);
  318. length = LE32TOH (nvm_header-&gt;ImageLength);
  319. if (_anyset (flags, NVM_VERBOSE))
  320. {
  321. printf (&quot;------- %s (%d) -------\n&quot;, filename, module);
  322. nvmpeek2 (nvm_header);
  323. }
  324. if (LE32TOH (nvm_header-&gt;ImageType) == NVM_IMAGE_MANIFEST)
  325. {
  326. if (_anyset (flags, NVM_MANIFEST))
  327. {
  328. printf (&quot;------- %s (%d) -------\n&quot;, filename, module);
  329. manifest ((char *)(memory) + offset, length);
  330. return (0);
  331. }
  332. }
  333. if (checksum32 ((char *)(memory) + offset, length, nvm_header-&gt;ImageChecksum))
  334. {
  335. if (_allclr (flags, NVM_SILENCE))
  336. {
  337. error (0, errno, NVM_IMG_CHECKSUM, filename, module);
  338. }
  339. return (-1);
  340. }
  341. offset += length;
  342. extent -= length;
  343. module++;
  344. }
  345. while (~nvm_header-&gt;NextHeader);
  346. if (extent)
  347. {
  348. if (_allclr (flags, NVM_SILENCE))
  349. {
  350. error (0, errno, NVM_HDR_LINK, filename, module);
  351. }
  352. }
  353. return (0);
  354. }
  355. /*====================================================================*
  356. *
  357. * signed chknvm (char const * filename, flag_t flags);
  358. *
  359. * read .nvm file into memory and validate it;
  360. *
  361. *
  362. * Contributor(s):
  363. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  364. *
  365. *--------------------------------------------------------------------*/
  366. static signed chknvm (char const * filename, flag_t flags)
  367. {
  368. void * memory = (void *)(0);
  369. signed extent = 0;
  370. signed status = 0;
  371. signed fd;
  372. if ((fd = open (filename, O_BINARY|O_RDONLY)) == -1)
  373. {
  374. if (_allclr (flags, NVM_SILENCE))
  375. {
  376. error (0, errno, FILE_CANTOPEN, filename);
  377. }
  378. return (-1);
  379. }
  380. if ((extent = lseek (fd, 0, SEEK_END)) == -1)
  381. {
  382. if (_allclr (flags, NVM_SILENCE))
  383. {
  384. error (0, errno, FILE_CANTSIZE, filename);
  385. }
  386. return (-1);
  387. }
  388. if (!(memory = malloc (extent)))
  389. {
  390. if (_allclr (flags, NVM_SILENCE))
  391. {
  392. error (0, errno, FILE_CANTLOAD, filename);
  393. }
  394. return (-1);
  395. }
  396. if (lseek (fd, 0, SEEK_SET))
  397. {
  398. if (_allclr (flags, NVM_SILENCE))
  399. {
  400. error (0, errno, FILE_CANTLOAD, filename);
  401. }
  402. return (-1);
  403. }
  404. if (read (fd, memory, extent) != extent)
  405. {
  406. if (_allclr (flags, NVM_SILENCE))
  407. {
  408. error (0, errno, FILE_CANTREAD, filename);
  409. }
  410. return (-1);
  411. }
  412. close (fd);
  413. if (LE32TOH (* (uint32_t *)(memory)) == 0x60000000)
  414. {
  415. status = nvmimage2 (memory, extent, filename, flags);
  416. }
  417. else
  418. {
  419. status = nvmchain2 (memory, extent, filename, flags);
  420. }
  421. free (memory);
  422. return (status);
  423. }
  424. /*====================================================================*
  425. *
  426. * int main (int argc, char const * argv []);
  427. *
  428. *
  429. *
  430. * Contributor(s):
  431. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  432. *
  433. *--------------------------------------------------------------------*/
  434. int main (int argc, char const * argv [])
  435. {
  436. static char const * optv [] =
  437. {
  438. &quot;imqrv&quot;,
  439. &quot;file [file] [...]&quot;,
  440. &quot;Qualcomm Atheros PLC Image File Validator&quot;,
  441. &quot;i\tprint firmware identity string&quot;,
  442. &quot;m\tdisplay manifest&quot;,
  443. &quot;q\tsuppress messages&quot;,
  444. &quot;r\tprint firmware revision string&quot;,
  445. &quot;v\tverbose messages&quot;,
  446. (char const *) (0)
  447. };
  448. flag_t flags = (flag_t)(0);
  449. signed state = 0;
  450. signed c;
  451. optind = 1;
  452. while ((c = getoptv (argc, argv, optv)) != -1)
  453. {
  454. switch ((char) (c))
  455. {
  456. case 'i':
  457. _setbits (flags, NVM_IDENTITY);
  458. break;
  459. case 'm':
  460. _setbits (flags, NVM_MANIFEST);
  461. break;
  462. case 'r':
  463. _setbits (flags, NVM_FIRMWARE);
  464. break;
  465. case 'q':
  466. _setbits (flags, NVM_SILENCE);
  467. break;
  468. case 'v':
  469. _setbits (flags, NVM_VERBOSE);
  470. break;
  471. default:
  472. break;
  473. }
  474. }
  475. argc -= optind;
  476. argv += optind;
  477. while ((argc) &amp;&amp; (* argv))
  478. {
  479. if (chknvm (* argv, flags))
  480. {
  481. state = 1;
  482. }
  483. else if (_allclr (flags, (NVM_VERBOSE|NVM_SILENCE|NVM_MANIFEST|NVM_FIRMWARE|NVM_IDENTITY)))
  484. {
  485. printf (&quot;%s looks good\n&quot;, * argv);
  486. }
  487. argc--;
  488. argv++;
  489. }
  490. return (state);
  491. }
  492. </pre>
  493. <div class='footerlink'>
  494. [<a href='chipset.c.html' title=' chipset.c '>PREV</a>]
  495. [<a href='toolkit.html' title=' Index '>HOME</a>]
  496. [<a href='chknvm.c.html' title=' chknvm.c '>NEXT</a>]
  497. </div>
  498. </body>
  499. </html>