chkpib2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*"
  42. *
  43. * chkpib2.c
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. *
  49. *--------------------------------------------------------------------*/
  50. /*====================================================================*
  51. * system header files;
  52. *--------------------------------------------------------------------*/
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <unistd.h>
  56. #include <fcntl.h>
  57. #include <errno.h>
  58. /*====================================================================*
  59. * custom header files;
  60. *--------------------------------------------------------------------*/
  61. #include "../tools/getoptv.h"
  62. #include "../tools/flags.h"
  63. #include "../tools/error.h"
  64. #include "../tools/files.h"
  65. #include "../key/HPAVKey.h"
  66. #include "../nvm/nvm.h"
  67. #include "../pib/pib.h"
  68. /*====================================================================*
  69. * custom source files;
  70. *--------------------------------------------------------------------*/
  71. #ifndef MAKEFILE
  72. #include "../tools/getoptv.c"
  73. #include "../tools/putoptv.c"
  74. #include "../tools/version.c"
  75. #include "../tools/checksum32.c"
  76. #include "../tools/fdchecksum32.c"
  77. #include "../tools/checksum32.c"
  78. #include "../tools/hexstring.c"
  79. #include "../tools/hexdecode.c"
  80. #include "../tools/strfbits.c"
  81. #include "../tools/error.c"
  82. #endif
  83. #ifndef MAKEFILE
  84. #include "../key/SHA256Reset.c"
  85. #include "../key/SHA256Block.c"
  86. #include "../key/SHA256Write.c"
  87. #include "../key/SHA256Fetch.c"
  88. #include "../key/HPAVKeyNID.c"
  89. #include "../key/keys.c"
  90. #endif
  91. #ifndef MAKEFILE
  92. #include "../pib/pibpeek1.c"
  93. #include "../pib/pibpeek2.c"
  94. #endif
  95. #ifndef MAKEFILE
  96. #include "../nvm/manifest.c"
  97. #endif
  98. /*====================================================================*
  99. *
  100. * signed pibimage1 (void const * memory, size_t extent, char const * filename, flag_t flags);
  101. *
  102. * check memory-resident thunderbolt/lightning PIB image; return 0
  103. * for a good image and -1 for a bad image;
  104. *
  105. * the check performed here is not exhaustive but it is adequate;
  106. *
  107. *
  108. * Contributor(s):
  109. * Charles Maier
  110. *
  111. *--------------------------------------------------------------------*/
  112. static signed pibimage1 (void const * memory, size_t extent, char const * filename, flag_t flags)
  113. {
  114. struct simple_pib * simple_pib = (struct simple_pib *)(memory);
  115. uint8_t NID [HPAVKEY_NID_LEN];
  116. if (_anyset (flags, PIB_VERBOSE))
  117. {
  118. printf ("------- %s -------\n", filename);
  119. if (pibpeek1 (memory))
  120. {
  121. if (_allclr (flags, PIB_SILENCE))
  122. {
  123. error (0, 0, PIB_BADVERSION, filename);
  124. }
  125. return (-1);
  126. }
  127. }
  128. if (extent != LE16TOH (simple_pib->PIBLENGTH))
  129. {
  130. if (_allclr (flags, PIB_SILENCE))
  131. {
  132. error (0, 0, PIB_BADLENGTH, filename);
  133. }
  134. return (-1);
  135. }
  136. if (checksum32 (memory, extent, 0))
  137. {
  138. if (_allclr (flags, PIB_SILENCE))
  139. {
  140. error (0, 0, PIB_BADCHECKSUM, filename);
  141. }
  142. return (-1);
  143. }
  144. HPAVKeyNID (NID, simple_pib->NMK, simple_pib->PreferredNID [HPAVKEY_NID_LEN-1] >> 4);
  145. if (memcmp (NID, simple_pib->PreferredNID, sizeof (NID)))
  146. {
  147. if (_allclr (flags, PIB_SILENCE))
  148. {
  149. error (0, 0, PIB_BADNID, filename);
  150. }
  151. return (-1);
  152. }
  153. return (0);
  154. }
  155. /*====================================================================*
  156. *
  157. * signed pibimage2 (void const * memory, size_t extent, char const * filename, flag_t flags);
  158. *
  159. * check memory-resident panther/lynx PIB image; return 0 for a
  160. * good image and -1 for an bad image;
  161. *
  162. * the check performed here is not exhaustive but it is adequate;
  163. *
  164. *
  165. * Contributor(s):
  166. * Charles Maier
  167. *
  168. *--------------------------------------------------------------------*/
  169. static signed pibimage2 (void const * memory, size_t extent, char const * filename, flag_t flags)
  170. {
  171. struct simple_pib * simple_pib = (struct simple_pib *)(memory);
  172. struct pib_header * pib_header = (struct pib_header *)(memory);
  173. uint8_t NID [HPAVKEY_NID_LEN];
  174. if (_anyset (flags, PIB_VERBOSE))
  175. {
  176. pib_header->PIBLENGTH = HTOLE16(extent);
  177. printf ("------- %s -------\n", filename);
  178. if (pibpeek2 (memory))
  179. {
  180. if (_allclr (flags, PIB_SILENCE))
  181. {
  182. error (0, 0, PIB_BADVERSION, filename);
  183. }
  184. return (-1);
  185. }
  186. memset (pib_header, 0, sizeof (* pib_header));
  187. }
  188. HPAVKeyNID (NID, simple_pib->NMK, simple_pib->PreferredNID [HPAVKEY_NID_LEN-1] >> 4);
  189. if (memcmp (NID, simple_pib->PreferredNID, sizeof (NID)))
  190. {
  191. if (_allclr (flags, PIB_SILENCE))
  192. {
  193. error (0, 0, PIB_BADNID, filename);
  194. }
  195. return (-1);
  196. }
  197. return (0);
  198. }
  199. /*====================================================================*
  200. *
  201. * signed pibchain2 (void const * memory, size_t extent, char const * filename, flag_t flags);
  202. *
  203. * search a panther/lynx image chain looking for PIB images and
  204. * verify each one; return 0 on success or -1 on error; errors
  205. * occur due to an invalid image chain or a bad parameter block;
  206. *
  207. * this implementation reads the parameter block from file into
  208. * into memory and checks it there;
  209. *
  210. *
  211. * Contributor(s):
  212. * Charles Maier
  213. *
  214. *--------------------------------------------------------------------*/
  215. static signed pibchain2 (void const * memory, size_t extent, char const * filename, flag_t flags)
  216. {
  217. struct nvm_header2 * nvm_header;
  218. uint32_t origin = ~0;
  219. uint32_t offset = 0;
  220. unsigned module = 0;
  221. do
  222. {
  223. nvm_header = (struct nvm_header2 *)((char *)(memory) + offset);
  224. if (LE16TOH (nvm_header->MajorVersion) != 1)
  225. {
  226. if (_allclr (flags, NVM_SILENCE))
  227. {
  228. error (0, 0, NVM_HDR_VERSION, filename, module);
  229. }
  230. return (-1);
  231. }
  232. if (LE16TOH (nvm_header->MinorVersion) != 1)
  233. {
  234. if (_allclr (flags, NVM_SILENCE))
  235. {
  236. error (0, 0, NVM_HDR_VERSION, filename, module);
  237. }
  238. return (-1);
  239. }
  240. if (LE32TOH (nvm_header->PrevHeader) != origin)
  241. {
  242. if (_allclr (flags, NVM_SILENCE))
  243. {
  244. error (0, 0, NVM_HDR_LINK, filename, module);
  245. }
  246. return (-1);
  247. }
  248. if (checksum32 (nvm_header, sizeof (* nvm_header), 0))
  249. {
  250. if (_allclr (flags, NVM_SILENCE))
  251. {
  252. error (0, 0, NVM_HDR_CHECKSUM, filename, module);
  253. }
  254. return (-1);
  255. }
  256. origin = offset;
  257. offset += sizeof (* nvm_header);
  258. extent -= sizeof (* nvm_header);
  259. if (checksum32 ((char *)(memory) + offset, LE32TOH (nvm_header->ImageLength), nvm_header->ImageChecksum))
  260. {
  261. if (_allclr (flags, NVM_SILENCE))
  262. {
  263. error (0, 0, NVM_IMG_CHECKSUM, filename, module);
  264. }
  265. return (-1);
  266. }
  267. if (LE32TOH (nvm_header->ImageType) == NVM_IMAGE_MANIFEST)
  268. {
  269. if (_anyset (flags, NVM_MANIFEST))
  270. {
  271. printf ("------- %s (%d) -------\n", filename, module);
  272. manifest ((char *)(memory) + offset, LE32TOH (nvm_header->ImageLength));
  273. return (0);
  274. }
  275. }
  276. else if (LE32TOH (nvm_header->ImageType) == NVM_IMAGE_PIB)
  277. {
  278. return (pibimage2 ((char *)(memory) + offset, LE32TOH (nvm_header->ImageLength), filename, flags));
  279. }
  280. offset += LE32TOH (nvm_header->ImageLength);
  281. extent -= LE32TOH (nvm_header->ImageLength);
  282. module++;
  283. }
  284. while (~nvm_header->NextHeader);
  285. if (extent)
  286. {
  287. if (_allclr (flags, NVM_SILENCE))
  288. {
  289. error (0, errno, NVM_HDR_LINK, filename, module);
  290. }
  291. return (-1);
  292. }
  293. error (0, 0, "%s has no PIB", filename);
  294. return (-1);
  295. }
  296. /*====================================================================*
  297. *
  298. * signed chkpib (char const * filename, flag_t flags);
  299. *
  300. *
  301. * Contributor(s):
  302. * Charles Maier
  303. *
  304. *--------------------------------------------------------------------*/
  305. static signed chkpib (char const * filename, flag_t flags)
  306. {
  307. void * memory = 0;
  308. signed extent = 0;
  309. signed status;
  310. signed fd;
  311. if ((fd = open (filename, O_BINARY|O_RDONLY)) == -1)
  312. {
  313. if (_allclr (flags, NVM_SILENCE))
  314. {
  315. error (0, errno, FILE_CANTOPEN, filename);
  316. }
  317. return (-1);
  318. }
  319. if ((extent = lseek (fd, 0, SEEK_END)) == -1)
  320. {
  321. if (_allclr (flags, NVM_SILENCE))
  322. {
  323. error (0, errno, FILE_CANTSIZE, filename);
  324. }
  325. return (-1);
  326. }
  327. if (!(memory = malloc (extent)))
  328. {
  329. if (_allclr (flags, NVM_SILENCE))
  330. {
  331. error (0, errno, FILE_CANTLOAD, filename);
  332. }
  333. return (-1);
  334. }
  335. if (lseek (fd, 0, SEEK_SET))
  336. {
  337. if (_allclr (flags, NVM_SILENCE))
  338. {
  339. error (0, errno, FILE_CANTHOME, filename);
  340. }
  341. return (-1);
  342. }
  343. if (read (fd, memory, extent) != extent)
  344. {
  345. if (_allclr (flags, NVM_SILENCE))
  346. {
  347. error (0, errno, FILE_CANTREAD, filename);
  348. }
  349. return (-1);
  350. }
  351. close (fd);
  352. if (LE32TOH (* (uint32_t *)(memory)) == 0x60000000)
  353. {
  354. if (_allclr (flags, NVM_SILENCE))
  355. {
  356. error (0, 0, FILE_WONTREAD, filename);
  357. }
  358. status = -1;
  359. }
  360. else if (LE32TOH (* (uint32_t *)(memory)) == 0x00010001)
  361. {
  362. status = pibchain2 (memory, extent, filename, flags);
  363. }
  364. else
  365. {
  366. status = pibimage1 (memory, extent, filename, flags);
  367. }
  368. free (memory);
  369. return (status);
  370. }
  371. /*====================================================================*
  372. *
  373. * int main (int argc, char const * argv []);
  374. *
  375. *
  376. *
  377. * Contributor(s):
  378. * Charles Maier
  379. *
  380. *--------------------------------------------------------------------*/
  381. int main (int argc, char const * argv [])
  382. {
  383. static char const * optv [] =
  384. {
  385. "mqv",
  386. "file [file] [...]",
  387. "Qualcomm Atheros PLC Parameter File Inspector",
  388. "m\tdisplay manifest",
  389. "q\tquiet",
  390. "v\tverbose messages",
  391. (char const *) (0)
  392. };
  393. flag_t flags = (flag_t)(0);
  394. signed state = 0;
  395. signed c;
  396. optind = 1;
  397. while ((c = getoptv (argc, argv, optv)) != -1)
  398. {
  399. switch (c)
  400. {
  401. case 'm':
  402. _setbits (flags, PIB_MANIFEST);
  403. break;
  404. case 'q':
  405. _setbits (flags, PIB_SILENCE);
  406. break;
  407. case 'v':
  408. _setbits (flags, PIB_VERBOSE);
  409. break;
  410. default:
  411. break;
  412. }
  413. }
  414. argc -= optind;
  415. argv += optind;
  416. while ((argc) && (* argv))
  417. {
  418. errno = 0;
  419. if (chkpib (* argv, flags))
  420. {
  421. state = 1;
  422. }
  423. else if (_allclr (flags, (PIB_VERBOSE | PIB_SILENCE | PIB_MANIFEST)))
  424. {
  425. printf ("%s looks good\n", * argv);
  426. }
  427. argc--;
  428. argv++;
  429. }
  430. return (state);
  431. }