setpib.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /****************************************************************************
  2. Copyright (c) 2013-2021, Qualcomm Technologies, Inc.
  3. All Rights Reserved.
  4. Confidential and Proprietary - Qualcomm Technologies, Inc.
  5. **********************************************************************
  6. 2013 Qualcomm Atheros, Inc.
  7. ****************************************************************************/
  8. /*====================================================================*
  9. * system header files;
  10. *--------------------------------------------------------------------*/
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <limits.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. /*====================================================================*
  17. * custom header files;
  18. *--------------------------------------------------------------------*/
  19. #include "../tools/getoptv.h"
  20. #include "../tools/putoptv.h"
  21. #include "../tools/memory.h"
  22. #include "../tools/number.h"
  23. #include "../tools/error.h"
  24. #include "../tools/types.h"
  25. #include "../tools/flags.h"
  26. #include "../tools/files.h"
  27. #include "../pib/pib.h"
  28. #include "../nvm/nvm.h"
  29. /*====================================================================*
  30. * custom source files;
  31. *--------------------------------------------------------------------*/
  32. #ifndef MAKEFILE
  33. #include "../tools/getoptv.c"
  34. #include "../tools/putoptv.c"
  35. #include "../tools/version.c"
  36. #include "../tools/uintspec.c"
  37. #include "../tools/basespec.c"
  38. #include "../tools/dataspec.c"
  39. #include "../tools/bytespec.c"
  40. #include "../tools/todigit.c"
  41. #include "../tools/hexdump.c"
  42. #include "../tools/hexpeek.c"
  43. #include "../tools/fdchecksum32.c"
  44. #include "../tools/checksum32.c"
  45. #include "../tools/memencode.c"
  46. #include "../tools/error.c"
  47. #endif
  48. #ifndef MAKEFILE
  49. #include "../nvm/panther_nvm_seek.c"
  50. #endif
  51. /*====================================================================*
  52. * constants;
  53. *--------------------------------------------------------------------*/
  54. #define SETPIB_VERBOSE (1 << 0)
  55. #define SETPIB_SILENCE (1 << 1)
  56. #define SETPIB_HEADERS (1 << 2)
  57. #define SETPIB_CHANGED (1 << 3)
  58. #define SETPIB_WINDOW 32
  59. /*====================================================================*
  60. * variables;
  61. *--------------------------------------------------------------------*/
  62. static flag_t flags = (flag_t) (0);
  63. static bool repairchksum = false;
  64. /*====================================================================*
  65. *
  66. * signed getpib (void * memory, size_t extent, int argc, char const * argv [], unsigned window)
  67. *
  68. * apply a series of edits to a memory region; edits are specified
  69. * in string vector argv [] which must follow rules understood by
  70. * function memencode(); this function merely walks the vector and
  71. * deals with error and display options;
  72. *
  73. * Contributor(s):
  74. * Charles Maier <cmaier@qca.qualcomm.com>
  75. *
  76. *--------------------------------------------------------------------*/
  77. static signed getpib (void * memory, off_t extent, int argc, char const * argv [], unsigned window)
  78. {
  79. uint32_t origin;
  80. uint32_t offset;
  81. if (! argc)
  82. {
  83. error (1, ENOTSUP, "Need an offset");
  84. }
  85. origin = offset = (uint32_t) (basespec (* argv, 16, sizeof (offset)));
  86. if ((off_t) (offset) >= extent)
  87. {
  88. error (1, ECANCELED, "Offset %X exceeds file length " OFF_T_SPEC, offset, extent);
  89. }
  90. argc--;
  91. argv++;
  92. if (! argc)
  93. {
  94. _setbits (flags, SETPIB_VERBOSE);
  95. }
  96. while ((argc > 1) && (* argv))
  97. {
  98. _setbits (flags, SETPIB_CHANGED);
  99. offset += (size_t) (memencode ((byte *) (memory) + offset, extent - offset, argv [0], argv [1]));
  100. argc -= 2;
  101. argv += 2;
  102. }
  103. if (argc)
  104. {
  105. error (1, ECANCELED, "object %s needs a value", * argv);
  106. }
  107. if (_anyset (flags, SETPIB_VERBOSE))
  108. {
  109. hexpeek (memory, origin, offset, extent, window, stdout);
  110. }
  111. return (0);
  112. }
  113. /*====================================================================*
  114. *
  115. * signed pibimage1 (int argc, char const * argv [], unsigned window);
  116. *
  117. * read an entire flat parameter file into memory, edit it, save
  118. * it and display it;
  119. *
  120. * Contributor(s):
  121. * Charles Maier <cmaier@qca.qualcomm.com>
  122. *
  123. *--------------------------------------------------------------------*/
  124. static signed pibimage1 (int argc, char const * argv [], unsigned window)
  125. {
  126. void * memory;
  127. off_t extent;
  128. signed fd;
  129. if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1)
  130. {
  131. error (1, errno, FILE_CANTOPEN, * argv);
  132. }
  133. if ((extent = lseek (fd, 0, SEEK_END)) == - 1)
  134. {
  135. error (1, errno, FILE_CANTSIZE, * argv);
  136. }
  137. if (! (memory = malloc (extent)))
  138. {
  139. error (1, errno, FILE_CANTLOAD, * argv);
  140. }
  141. if (lseek (fd, 0, SEEK_SET))
  142. {
  143. error (1, errno, FILE_CANTHOME, * argv);
  144. }
  145. if (read (fd, memory, extent) != extent)
  146. {
  147. error (1, errno, FILE_CANTREAD, * argv);
  148. }
  149. if (lseek (fd, 0, SEEK_SET))
  150. {
  151. error (1, errno, FILE_CANTHOME, * argv);
  152. }
  153. if (getpib (memory, extent, argc - 1, argv + 1, window))
  154. {
  155. error (1, errno, FILE_CANTEDIT, * argv);
  156. }
  157. if (_anyset (flags, SETPIB_CHANGED))
  158. {
  159. struct pib_header * pib_header = (struct pib_header *) (memory);
  160. pib_header->CHECKSUM = checksum32 (memory, extent, pib_header->CHECKSUM);
  161. if (write (fd, memory, extent) != extent)
  162. {
  163. error (1, errno, FILE_CANTSAVE, * argv);
  164. }
  165. if (lseek (fd, (off_t) (0) - extent, SEEK_CUR) == - 1)
  166. {
  167. error (1, errno, FILE_CANTHOME, * argv);
  168. }
  169. }
  170. free (memory);
  171. close (fd);
  172. return (0);
  173. }
  174. /*====================================================================*
  175. *
  176. * signed pibimage2 (signed fd, char const * filename, int argc, char const * argv [], unsigned window);
  177. *
  178. * read an entire flat parameter file into memory, edit it, save
  179. * it and display it;
  180. *
  181. * Contributor(s):
  182. * Charles Maier <cmaier@qca.qualcomm.com>
  183. *
  184. *--------------------------------------------------------------------*/
  185. static signed pibimage2 (int argc, char const * argv [], unsigned window)
  186. {
  187. struct panther_nvm_header header;
  188. signed fd;
  189. if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1)
  190. {
  191. error (1, errno, FILE_CANTOPEN, * argv);
  192. }
  193. if (! panther_nvm_seek2 (fd, * argv, & header, NVM_IMAGE_PIB, repairchksum))
  194. {
  195. byte * memory;
  196. off_t extent = LE32TOH (header.ImageLength);
  197. if (! (memory = malloc (extent)))
  198. {
  199. error (1, errno, FILE_CANTLOAD, * argv);
  200. }
  201. if (read (fd, memory, extent) != extent)
  202. {
  203. error (1, errno, FILE_CANTREAD, * argv);
  204. }
  205. if (lseek (fd, (off_t) (0) - extent, SEEK_CUR) == - 1)
  206. {
  207. error (1, errno, FILE_CANTHOME, * argv);
  208. }
  209. if (getpib (memory, extent, argc - 1, argv + 1, window))
  210. {
  211. error (1, errno, FILE_CANTEDIT, * argv);
  212. }
  213. if (_anyset (flags, SETPIB_CHANGED))
  214. {
  215. header.ImageChecksum = checksum32 (memory, extent, 0);
  216. if (write (fd, memory, extent) != extent)
  217. {
  218. error (1, errno, FILE_CANTSAVE, * argv);
  219. }
  220. if (lseek (fd, (off_t) (0) - extent, SEEK_CUR) == - 1)
  221. {
  222. error (1, errno, FILE_CANTHOME, * argv);
  223. }
  224. header.HeaderChecksum = checksum32 (& header, sizeof (header), header.HeaderChecksum);
  225. if (lseek (fd, (off_t) (0) - sizeof (header), SEEK_CUR) == - 1)
  226. {
  227. error (1, errno, FILE_CANTHOME, * argv);
  228. }
  229. if (write (fd, & header, sizeof (header)) != sizeof (header))
  230. {
  231. error (1, errno, FILE_CANTSAVE, * argv);
  232. }
  233. if (lseek (fd, (off_t) (0) - sizeof (header), SEEK_CUR) == - 1)
  234. {
  235. error (1, errno, FILE_CANTHOME, * argv);
  236. }
  237. }
  238. free (memory);
  239. }
  240. close (fd);
  241. return (0);
  242. }
  243. /*====================================================================*
  244. *
  245. * signed function (int argc, char const * argv [], unsigned window);
  246. *
  247. * call an appropriate parameter edit function based on the file
  248. * header;
  249. *
  250. * older parameter files are flat with their own header; newer ones
  251. * are image chains where one of image contains the parameter block;
  252. *
  253. *
  254. * Contributor(s):
  255. * Charles Maier <cmaier@qca.qualcomm.com>
  256. *
  257. *--------------------------------------------------------------------*/
  258. static signed function (int argc, char const * argv [], unsigned window)
  259. {
  260. uint32_t version;
  261. signed status;
  262. signed fd;
  263. if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1)
  264. {
  265. error (1, errno, FILE_CANTOPEN, * argv);
  266. }
  267. if (read (fd, & version, sizeof (version)) != sizeof (version))
  268. {
  269. error (1, errno, FILE_CANTREAD, * argv);
  270. }
  271. close (fd);
  272. if (LE32TOH (version) == 0x00010001)
  273. {
  274. status = pibimage2 (argc, argv, window);
  275. }
  276. else
  277. {
  278. status = pibimage1 (argc, argv, window);
  279. }
  280. return (status);
  281. }
  282. /*====================================================================*
  283. *
  284. * int main (int argc, char const * argv []);
  285. *
  286. *
  287. *--------------------------------------------------------------------*/
  288. int main (int argc, char const * argv [])
  289. {
  290. static char const * optv [] =
  291. {
  292. "qvw:x",
  293. "file base [type data] [type data] [...]\n\n\tstandard-length types are 'byte'|'word'|'long'|'huge'|'hfid'|'mac'|'key'\n\tvariable-length types are 'data'|'text'|'fill'|'skip'",
  294. "Qualcomm Atheros PIB File Editor",
  295. "q\tquiet mode",
  296. "v[v]\tverbose mode",
  297. "w n\twindow size is (n) [" LITERAL (SETPIB_WINDOW) "]",
  298. "x\trepair checksum",
  299. (char const *) (0)
  300. };
  301. unsigned window = SETPIB_WINDOW;
  302. signed c;
  303. optind = 1;
  304. opterr = 1;
  305. while (~ (c = getoptv (argc, argv, optv)))
  306. {
  307. switch (c)
  308. {
  309. case 'q':
  310. _setbits (flags, SETPIB_SILENCE);
  311. break;
  312. case 'v':
  313. if (_anyset (flags, SETPIB_VERBOSE))
  314. {
  315. _setbits (flags, SETPIB_HEADERS);
  316. }
  317. _setbits (flags, SETPIB_VERBOSE);
  318. break;
  319. case 'w':
  320. window = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  321. _setbits (flags, SETPIB_VERBOSE);
  322. break;
  323. case 'x':
  324. _setbits (flags, SETPIB_CHANGED);
  325. repairchksum = true;
  326. break;
  327. default:
  328. break;
  329. }
  330. }
  331. argc -= optind;
  332. argv += optind;
  333. if ((argc) && (* argv))
  334. {
  335. function (argc, argv, window);
  336. }
  337. return (0);
  338. }