psin.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. * psin.c - load prescalers into int6000 and qca7000 parameter file;
  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 <stdint.h>
  56. #include <ctype.h>
  57. #include <string.h>
  58. #include <unistd.h>
  59. #include <errno.h>
  60. /*====================================================================*
  61. * custom header files;
  62. *--------------------------------------------------------------------*/
  63. #include "../tools/endian.h"
  64. #include "../tools/getoptv.h"
  65. #include "../tools/number.h"
  66. #include "../tools/chars.h"
  67. #include "../tools/types.h"
  68. #include "../tools/error.h"
  69. #include "../tools/files.h"
  70. #include "../key/HPAVKey.h"
  71. #include "../pib/pib.h"
  72. #include "../plc/plc.h"
  73. /*====================================================================*
  74. * custom source files;
  75. *--------------------------------------------------------------------*/
  76. #ifndef MAKEFILE
  77. #include "../tools/getoptv.c"
  78. #include "../tools/putoptv.c"
  79. #include "../tools/version.c"
  80. #include "../tools/fdchecksum32.c"
  81. #include "../tools/hexdecode.c"
  82. #include "../tools/hexstring.c"
  83. #include "../tools/todigit.c"
  84. #include "../tools/error.c"
  85. #endif
  86. #ifndef MAKEFILE
  87. #include "../pib/pibfile.c"
  88. #include "../pib/pibfile1.c"
  89. #include "../pib/pibfile2.c"
  90. #include "../pib/piblock.c"
  91. #include "../pib/pibscalers.c"
  92. #endif
  93. #ifndef MAKEFILE
  94. #include "../key/keys.c"
  95. #endif
  96. /*====================================================================*
  97. *
  98. *
  99. * signed ar7x00_psin (struct _file_ * pib, uint32_t value, uint32_t index);
  100. *
  101. * Places a single 10 bit prescaler into the pib file at index;
  102. *
  103. *--------------------------------------------------------------------*/
  104. signed ar7x00_psin (struct _file_ * pib, uint32_t value, uint32_t index)
  105. {
  106. off_t offset = AMP_PRESCALER_OFFSET + (index * 10 / 8);
  107. uint8_t bit_offset = (index * 10) % 8;
  108. uint16_t tmp;
  109. if (lseek (pib->file, offset, SEEK_SET) != offset)
  110. {
  111. return (-1);
  112. }
  113. if (read (pib->file, &tmp, sizeof (tmp)) != sizeof (tmp))
  114. {
  115. return (-1);
  116. }
  117. if (lseek (pib->file, offset, SEEK_SET) != offset)
  118. {
  119. return (-1);
  120. }
  121. value &= 0x03FF;
  122. tmp = LE16TOH (tmp);
  123. tmp &= ~(0x03FF << bit_offset);
  124. tmp |= value << bit_offset;
  125. tmp = HTOLE16 (tmp);
  126. if (write (pib->file, &tmp, sizeof (tmp)) != sizeof (tmp))
  127. {
  128. return (-1);
  129. }
  130. return (0);
  131. }
  132. /*====================================================================*
  133. *
  134. * signed psin (struct _file_ * pib);
  135. *
  136. *
  137. *--------------------------------------------------------------------*/
  138. static signed psin (struct _file_ * pib)
  139. {
  140. unsigned index = 0;
  141. unsigned count = 0;
  142. unsigned limit = pibscalers (pib);
  143. uint32_t value = 0;
  144. signed c;
  145. if ((limit != INT_CARRIERS) && (limit != AMP_CARRIERS) && (limit != PLC_CARRIERS))
  146. {
  147. error (1, 0, "Don't understand this PIB's prescaler format");
  148. }
  149. if (limit == INT_CARRIERS)
  150. {
  151. if (lseek (pib->file, INT_PRESCALER_OFFSET, SEEK_SET) != INT_PRESCALER_OFFSET)
  152. {
  153. error (1, errno, FILE_CANTSEEK, pib->name);
  154. }
  155. }
  156. else if (limit == PLC_CARRIERS)
  157. {
  158. if (lseek (pib->file, QCA_PRESCALER_OFFSET, SEEK_SET) != QCA_PRESCALER_OFFSET)
  159. {
  160. error (1, errno, FILE_CANTSEEK, pib->name);
  161. }
  162. }
  163. while ((c = getc (stdin)) != EOF)
  164. {
  165. if (isspace (c))
  166. {
  167. continue;
  168. }
  169. if ((c == '#') || (c == ';'))
  170. {
  171. do
  172. {
  173. c = getc (stdin);
  174. }
  175. while (nobreak (c));
  176. continue;
  177. }
  178. index = 0;
  179. while (isdigit (c))
  180. {
  181. index *= 10;
  182. index += c - '0';
  183. c = getc (stdin);
  184. }
  185. if (index != count)
  186. {
  187. error (1, ECANCELED, "Carrier %d out of order", index);
  188. }
  189. if (index >= limit)
  190. {
  191. error (1, EOVERFLOW, "Too many prescalers");
  192. }
  193. while (isblank (c))
  194. {
  195. c = getc (stdin);
  196. }
  197. value = 0;
  198. while (isxdigit (c))
  199. {
  200. value *= 16;
  201. value += todigit (c);
  202. c = getc (stdin);
  203. }
  204. if (limit == INT_CARRIERS)
  205. {
  206. value = HTOLE32 (value);
  207. if (write (pib->file, &value, sizeof (value)) != sizeof (value))
  208. {
  209. error (1, errno, "Can't save %s", pib->name);
  210. }
  211. }
  212. else if (limit == AMP_CARRIERS)
  213. {
  214. if (value & ~0x03FF)
  215. {
  216. error (1, errno, "Position %d has invalid prescaler value", index);
  217. }
  218. if (ar7x00_psin (pib, value, index))
  219. {
  220. error (1, errno, "Can't update %s", pib->name);
  221. }
  222. }
  223. else if (limit == PLC_CARRIERS)
  224. {
  225. uint8_t tmp = value & 0xff;
  226. if (write (pib->file, &tmp, sizeof (tmp)) != sizeof (tmp))
  227. {
  228. error (1, errno, "Can't save %s", pib->name);
  229. }
  230. }
  231. while (nobreak (c))
  232. {
  233. c = getc (stdin);
  234. };
  235. count++;
  236. }
  237. return (0);
  238. }
  239. /*====================================================================*
  240. *
  241. * int main (int argc, char const * argv [])
  242. *
  243. *
  244. *--------------------------------------------------------------------*/
  245. int main (int argc, char const * argv [])
  246. {
  247. static char const * optv [] =
  248. {
  249. "",
  250. "pibfile [< scalers]",
  251. "load prescalers into int6000 and qca7000 parameter file",
  252. (char const *) (0)
  253. };
  254. struct _file_ pib;
  255. signed c;
  256. optind = 1;
  257. while ((c = getoptv (argc, argv, optv)) != -1)
  258. {
  259. switch ((char) (c))
  260. {
  261. default:
  262. break;
  263. }
  264. }
  265. argc -= optind;
  266. argv += optind;
  267. if (argc > 1)
  268. {
  269. error (1, ECANCELED, "Only one target file allowed");
  270. }
  271. if ((argc) && (* argv))
  272. {
  273. pib.name = * argv;
  274. if ((pib.file = open (pib.name, O_BINARY|O_RDWR)) == -1)
  275. {
  276. error (1, errno, "Can't open %s", pib.name);
  277. }
  278. else if (pibfile (&pib))
  279. {
  280. error (1, errno, "Bad PIB file: %s", pib.name);
  281. }
  282. else if (psin (&pib))
  283. {
  284. error (1, ECANCELED, "%s", pib.name);
  285. }
  286. else if (piblock (&pib))
  287. {
  288. error (1, ECANCELED, "%s", pib.name);
  289. }
  290. close (pib.file);
  291. }
  292. return (0);
  293. }