chkpib.c 12 KB

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