psin.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * psin.c - load prescalers into int6000 parameter file;
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  15. *
  16. *--------------------------------------------------------------------*/
  17. /*====================================================================*
  18. * system header files;
  19. *--------------------------------------------------------------------*/
  20. #include <stdio.h>
  21. #include <stdint.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. /*====================================================================*
  27. * custom header files;
  28. *--------------------------------------------------------------------*/
  29. #include "../tools/endian.h"
  30. #include "../tools/getoptv.h"
  31. #include "../tools/number.h"
  32. #include "../tools/chars.h"
  33. #include "../tools/types.h"
  34. #include "../tools/error.h"
  35. #include "../tools/files.h"
  36. #include "../key/HPAVKey.h"
  37. #include "../pib/pib.h"
  38. #include "../plc/plc.h"
  39. /*====================================================================*
  40. * custom source files;
  41. *--------------------------------------------------------------------*/
  42. #ifndef MAKEFILE
  43. #include "../tools/getoptv.c"
  44. #include "../tools/putoptv.c"
  45. #include "../tools/version.c"
  46. #include "../tools/fdchecksum32.c"
  47. #include "../tools/hexdecode.c"
  48. #include "../tools/hexstring.c"
  49. #include "../tools/todigit.c"
  50. #include "../tools/error.c"
  51. #endif
  52. #ifndef MAKEFILE
  53. #include "../pib/lightning_pib_file.c"
  54. #include "../pib/lightning_pib_lock.c"
  55. #include "../pib/pibscalers.c"
  56. #endif
  57. #ifndef MAKEFILE
  58. #include "../key/keys.c"
  59. #endif
  60. /*====================================================================*
  61. *
  62. *
  63. * signed ar7x00_psin (struct _file_ * pib, uint32_t value, uint32_t index);
  64. *
  65. * Places a single 10 bit prescaler into the pib file at index;
  66. *
  67. *--------------------------------------------------------------------*/
  68. signed ar7x00_psin (struct _file_ * pib, uint32_t value, uint32_t index)
  69. {
  70. off_t offset = AMP_PRESCALER_OFFSET + (index * 10 / 8);
  71. uint8_t bit_offset = (index * 10) % 8;
  72. uint16_t tmp;
  73. if (lseek (pib->file, offset, SEEK_SET) != offset)
  74. {
  75. return (- 1);
  76. }
  77. if (read (pib->file, & tmp, sizeof (tmp)) != sizeof (tmp))
  78. {
  79. return (- 1);
  80. }
  81. if (lseek (pib->file, offset, SEEK_SET) != offset)
  82. {
  83. return (- 1);
  84. }
  85. value &= 0x03FF;
  86. tmp = LE16TOH (tmp);
  87. tmp &= ~ (0x03FF << bit_offset);
  88. tmp |= value << bit_offset;
  89. tmp = HTOLE16 (tmp);
  90. if (write (pib->file, & tmp, sizeof (tmp)) != sizeof (tmp))
  91. {
  92. return (- 1);
  93. }
  94. return (0);
  95. }
  96. /*====================================================================*
  97. *
  98. * signed psin (struct _file_ * pib);
  99. *
  100. *--------------------------------------------------------------------*/
  101. static signed psin (struct _file_ * pib)
  102. {
  103. unsigned index = 0;
  104. unsigned count = 0;
  105. unsigned limit = pibscalers (pib);
  106. uint32_t value = 0;
  107. signed c;
  108. if ((limit != INT_CARRIERS) && (limit != AMP_CARRIERS))
  109. {
  110. error (1, 0, "Don't understand this PIB's prescaler format");
  111. }
  112. if (limit == INT_CARRIERS)
  113. {
  114. if (lseek (pib->file, INT_PRESCALER_OFFSET, SEEK_SET) != INT_PRESCALER_OFFSET)
  115. {
  116. error (1, errno, FILE_CANTSEEK, pib->name);
  117. }
  118. }
  119. while ((c = getc (stdin)) != EOF)
  120. {
  121. if (isspace (c))
  122. {
  123. continue;
  124. }
  125. if ((c == '#') || (c == ';'))
  126. {
  127. do
  128. {
  129. c = getc (stdin);
  130. }
  131. while (nobreak (c));
  132. continue;
  133. }
  134. index = 0;
  135. while (isdigit (c))
  136. {
  137. index *= 10;
  138. index += c - '0';
  139. c = getc (stdin);
  140. }
  141. if (index != count)
  142. {
  143. error (1, ECANCELED, "Carrier %d out of order", index);
  144. }
  145. if (index >= limit)
  146. {
  147. error (1, EOVERFLOW, "Too many prescalers");
  148. }
  149. while (isblank (c))
  150. {
  151. c = getc (stdin);
  152. }
  153. value = 0;
  154. while (isxdigit (c))
  155. {
  156. value *= 16;
  157. value += todigit (c);
  158. c = getc (stdin);
  159. }
  160. if (limit == INT_CARRIERS)
  161. {
  162. value = HTOLE32 (value);
  163. if (write (pib->file, & value, sizeof (value)) != sizeof (value))
  164. {
  165. error (1, errno, "Can't save %s", pib->name);
  166. }
  167. }
  168. else if (limit == AMP_CARRIERS)
  169. {
  170. if (value & ~ 0x03FF)
  171. {
  172. error (1, errno, "Position %d has invalid prescaler value", index);
  173. }
  174. if (ar7x00_psin (pib, value, index))
  175. {
  176. error (1, errno, "Can't update %s", pib->name);
  177. }
  178. }
  179. while (nobreak (c))
  180. {
  181. c = getc (stdin);
  182. };
  183. count++;
  184. }
  185. return (0);
  186. }
  187. /*====================================================================*
  188. *
  189. * int main (int argc, char const * argv [])
  190. *
  191. *
  192. *--------------------------------------------------------------------*/
  193. int main (int argc, char const * argv [])
  194. {
  195. static char const * optv [] =
  196. {
  197. "",
  198. "pibfile [< scalers]",
  199. "load prescalers into int6000 parameter file",
  200. (char const *) (0)
  201. };
  202. struct _file_ pib;
  203. signed c;
  204. optind = 1;
  205. while (~ (c = getoptv (argc, argv, optv)))
  206. {
  207. switch ((char) (c))
  208. {
  209. default:
  210. break;
  211. }
  212. }
  213. argc -= optind;
  214. argv += optind;
  215. if (argc > 1)
  216. {
  217. error (1, ECANCELED, "Only one target file allowed");
  218. }
  219. if ((argc) && (* argv))
  220. {
  221. pib.name = * argv;
  222. if ((pib.file = open (pib.name, O_BINARY | O_RDWR)) == - 1)
  223. {
  224. error (1, errno, "Can't open %s", pib.name);
  225. }
  226. else if (lightning_pib_file (& pib))
  227. {
  228. error (1, errno, "Bad PIB file: %s", pib.name);
  229. }
  230. else if (psin (& pib))
  231. {
  232. error (1, ECANCELED, "%s", pib.name);
  233. }
  234. else if (lightning_pib_lock (& pib))
  235. {
  236. error (1, ECANCELED, "%s", pib.name);
  237. }
  238. close (pib.file);
  239. }
  240. return (0);
  241. }