setpib.c 11 KB

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