chknvm2.c 12 KB

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