pskey.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * pskey.c - print prescaler key
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. *
  49. *--------------------------------------------------------------------*/
  50. /*====================================================================*
  51. * system header files;
  52. *--------------------------------------------------------------------*/
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include <unistd.h>
  56. #include <errno.h>
  57. #include <sys/stat.h>
  58. /*====================================================================*
  59. * custom header files;
  60. *--------------------------------------------------------------------*/
  61. #include "../tools/getoptv.h"
  62. #include "../tools/number.h"
  63. #include "../tools/types.h"
  64. #include "../tools/error.h"
  65. #include "../tools/files.h"
  66. #include "../tools/flags.h"
  67. #include "../key/HPAVKey.h"
  68. #include "../key/SHA256.h"
  69. #include "../pib/pib.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/fdchecksum32.c"
  78. #include "../tools/hexdecode.c"
  79. #include "../tools/hexstring.c"
  80. #include "../tools/hexout.c"
  81. #include "../tools/error.c"
  82. #endif
  83. #ifndef MAKEFILE
  84. #include "../pib/pibfile1.c"
  85. #endif
  86. #ifndef MAKEFILE
  87. #include "../key/SHA256Reset.c"
  88. #include "../key/SHA256Write.c"
  89. #include "../key/SHA256Block.c"
  90. #include "../key/SHA256Fetch.c"
  91. #include "../key/keys.c"
  92. #endif
  93. /*====================================================================*
  94. * program constants;
  95. *--------------------------------------------------------------------*/
  96. #define PSKEY_VERBOSE (1 << 0)
  97. #define PSKEY_SILENCE (1 << 1)
  98. /*====================================================================*
  99. *
  100. * static signed pskey (struct _file_ * pib);
  101. *
  102. * compute the SHA256 digest of the PIB file prescalers and print
  103. * the key with optional filename on stdout; the digest acts like
  104. * a fingerprint;
  105. *
  106. * assume that offset and extent are for ar7400 and change to 6400
  107. * offset and extent when the PIB requires it;
  108. *
  109. *
  110. *--------------------------------------------------------------------*/
  111. static signed pskey (struct _file_ * pib, off_t offset, void * memory, ssize_t extent, flag_t flags)
  112. {
  113. struct sha256 sha256;
  114. byte digest [SHA256_DIGEST_LENGTH];
  115. struct pib_header pib_header;
  116. if (read (pib->file, &pib_header, sizeof (pib_header)) != sizeof (pib_header))
  117. {
  118. return (-1);
  119. }
  120. if (pib_header.FWVERSION < 0x05)
  121. {
  122. offset = INT_PRESCALER_OFFSET;
  123. extent = INT_PRESCALER_LENGTH;
  124. }
  125. if (lseek (pib->file, offset, SEEK_SET) != offset)
  126. {
  127. error (0, errno, FILE_CANTSEEK, pib->name);
  128. return (-1);
  129. }
  130. if (read (pib->file, memory, extent) != extent)
  131. {
  132. error (0, errno, FILE_CANTREAD, pib->name);
  133. return (-1);
  134. }
  135. SHA256Reset (&sha256);
  136. SHA256Write (&sha256, memory, extent);
  137. SHA256Fetch (&sha256, digest);
  138. hexout (digest, sizeof (digest), '\0', 0, stdout);
  139. if (_allclr (flags, PSKEY_SILENCE))
  140. {
  141. printf (" %s", pib->name);
  142. }
  143. printf ("\n");
  144. return (0);
  145. }
  146. /*====================================================================*
  147. *
  148. * int main (int argc, char const * argv [])
  149. *
  150. *
  151. *--------------------------------------------------------------------*/
  152. int main (int argc, char const * argv [])
  153. {
  154. static char const * optv [] =
  155. {
  156. "lqrsv",
  157. "file [file] [...]",
  158. "print prescaler finger-print on stdout",
  159. "q\tquiet mode",
  160. "v\tverbose mode",
  161. (char const *) (0)
  162. };
  163. struct _file_ pib;
  164. uint8_t buffer [INT_PRESCALER_LENGTH];
  165. signed state = 0;
  166. flag_t flags = (flag_t) (0);
  167. signed c;
  168. optind = 1;
  169. while ((c = getoptv (argc, argv, optv)) != -1)
  170. {
  171. switch ((char) (c))
  172. {
  173. case 'q':
  174. _setbits (flags, PSKEY_SILENCE);
  175. break;
  176. case 'v':
  177. _setbits (flags, PSKEY_VERBOSE);
  178. break;
  179. default:
  180. break;
  181. }
  182. }
  183. argc -= optind;
  184. argv += optind;
  185. while ((argc) && (* argv))
  186. {
  187. pib.name = * argv;
  188. if ((pib.file = open (pib.name, O_BINARY|O_RDONLY)) == -1)
  189. {
  190. error (0, errno, "Can't open: %s", pib.name);
  191. state = 1;
  192. errno = 0;
  193. }
  194. else if (pibfile1 (&pib))
  195. {
  196. error (0, errno, "Bad PIB: %s", pib.name);
  197. state = 1;
  198. }
  199. else if (pskey (&pib, INT_PRESCALER_OFFSET, buffer, sizeof (buffer), flags))
  200. {
  201. state = 1;
  202. }
  203. close (pib.file);
  204. argc--;
  205. argv++;
  206. }
  207. return (state);
  208. }