getpib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /****************************************************************************
  2. Copyright (c) 2013, 2022 Qualcomm Technologies, Inc.
  3. All Rights Reserved.
  4. Confidential and Proprietary - Qualcomm Technologies, Inc.
  5. **********************************************************************
  6. 2013 Qualcomm Atheros, Inc.
  7. ****************************************************************************/
  8. /*====================================================================*
  9. *
  10. * getpib.c - PIB Data Extractor
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. /*====================================================================*
  17. * system header files;
  18. *--------------------------------------------------------------------*/
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <limits.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <inttypes.h>
  25. /*====================================================================*
  26. * custom header files;
  27. *--------------------------------------------------------------------*/
  28. #include "../tools/getoptv.h"
  29. #include "../tools/memory.h"
  30. #include "../tools/number.h"
  31. #include "../tools/error.h"
  32. #include "../tools/types.h"
  33. #include "../tools/flags.h"
  34. #include "../tools/files.h"
  35. #include "../pib/pib.h"
  36. #include "../nvm/nvm.h"
  37. /*====================================================================*
  38. * custom source files;
  39. *--------------------------------------------------------------------*/
  40. #ifndef MAKEFILE
  41. #include "../tools/getoptv.c"
  42. #include "../tools/putoptv.c"
  43. #include "../tools/version.c"
  44. #include "../tools/uintspec.c"
  45. #include "../tools/basespec.c"
  46. #include "../tools/todigit.c"
  47. #include "../tools/hexout.c"
  48. #include "../tools/error.c"
  49. #include "../tools/checksum32.c"
  50. #include "../tools/fdchecksum32.c"
  51. #endif
  52. #ifndef MAKEFILE
  53. #include "../nvm/panther_nvm_seek.c"
  54. #endif
  55. /*====================================================================*
  56. * constants;
  57. *--------------------------------------------------------------------*/
  58. #define GETPIB_COMMA ' '
  59. #define GETPIB_TOOBIG "object '%s' exceeds extent of " SIZE_T_SPEC " bytes"
  60. #define GETPIB_NOSIZE "object '%s' has no length"
  61. #define GETPIB_VERBOSE (1 << 0)
  62. #define GETPIB_SILENCE (1 << 1)
  63. #define GETPIB_NEWLINE (1 << 2)
  64. /*====================================================================*
  65. *
  66. * void getmemory (byte const * memory, size_t extent, char const * object, size_t length);
  67. *
  68. *--------------------------------------------------------------------*/
  69. static void getmemory (byte const * memory, size_t extent, char const * object, size_t length)
  70. {
  71. if (length > extent)
  72. {
  73. error (1, ECANCELED, GETPIB_TOOBIG, object, length);
  74. }
  75. hexout (memory, length, ':', '\0', stdout);
  76. return;
  77. }
  78. /*====================================================================*
  79. *
  80. * void getstring (byte const * memory, size_t extent, char const * object, size_t length);
  81. *
  82. *--------------------------------------------------------------------*/
  83. static void getstring (byte const * memory, size_t extent, char const * object, size_t length)
  84. {
  85. char const * string = (char const *) (memory);
  86. if (length > extent)
  87. {
  88. error (1, ECANCELED, GETPIB_TOOBIG, object, length);
  89. }
  90. while (isprint ((unsigned char)* string) && (length--))
  91. {
  92. putc (* string++, stdout);
  93. }
  94. return;
  95. }
  96. /*====================================================================*
  97. *
  98. * void snatch (int argc, char const * argv [], byte const * memory, size_t extent, char comma);
  99. *
  100. * extract and print the specified data objects from memory; comma
  101. * delimits consecutive objects on output;
  102. *
  103. *--------------------------------------------------------------------*/
  104. static void snatch (int argc, char const * argv [], byte const * memory, size_t extent, char comma)
  105. {
  106. size_t length = 0;
  107. size_t offset = 0;
  108. if (! (argc) || ! (* argv))
  109. {
  110. error (1, ECANCELED, "Need an offset");
  111. }
  112. offset = (size_t) (basespec (* argv, 16, sizeof (uint32_t)));
  113. if (offset > extent)
  114. {
  115. error (1, ECANCELED, "offset " SIZE_T_SPEC " exceeds extent of " SIZE_T_SPEC " bytes", offset, extent);
  116. }
  117. memory += offset;
  118. extent -= offset;
  119. argc--;
  120. argv++;
  121. while ((argc) && (* argv))
  122. {
  123. char const * object = * argv;
  124. argc--;
  125. argv++;
  126. if (! strcmp (object, "byte"))
  127. {
  128. uint8_t * number = (uint8_t *) (memory);
  129. if (sizeof (* number) > extent)
  130. {
  131. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  132. }
  133. printf ("%u", * number);
  134. memory += sizeof (* number);
  135. extent -= sizeof (* number);
  136. }
  137. else if (! strcmp (object, "word"))
  138. {
  139. uint16_t * number = (uint16_t *) (memory);
  140. if (sizeof (* number) > extent)
  141. {
  142. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  143. }
  144. printf ("%u", LE16TOH (* number));
  145. memory += sizeof (* number);
  146. extent -= sizeof (* number);
  147. }
  148. else if (! strcmp (object, "long"))
  149. {
  150. uint32_t * number = (uint32_t *) (memory);
  151. if (sizeof (* number) > extent)
  152. {
  153. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  154. }
  155. printf ("%u", LE32TOH (* number));
  156. memory += sizeof (* number);
  157. extent -= sizeof (* number);
  158. }
  159. else if (! strcmp (object, "huge"))
  160. {
  161. uint64_t * number = (uint64_t *) (memory);
  162. if (sizeof (* number) > extent)
  163. {
  164. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  165. }
  166. printf ("%" PRIu64, LE64TOH (* number));
  167. memory += sizeof (* number);
  168. extent -= sizeof (* number);
  169. }
  170. #if 1
  171. else if (! strcmp (object, "xbyte"))
  172. {
  173. uint8_t * number = (uint8_t *) (memory);
  174. if (sizeof (* number) > extent)
  175. {
  176. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  177. }
  178. printf ("0x%02X", * number);
  179. memory += sizeof (* number);
  180. extent -= sizeof (* number);
  181. }
  182. else if (! strcmp (object, "xword"))
  183. {
  184. uint16_t * number = (uint16_t *) (memory);
  185. if (sizeof (* number) > extent)
  186. {
  187. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  188. }
  189. printf ("0x%04X", LE16TOH (* number));
  190. memory += sizeof (* number);
  191. extent -= sizeof (* number);
  192. }
  193. else if (! strcmp (object, "xlong"))
  194. {
  195. uint32_t * number = (uint32_t *) (memory);
  196. if (sizeof (* number) > extent)
  197. {
  198. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  199. }
  200. printf ("0x%08X", LE32TOH (* number));
  201. memory += sizeof (* number);
  202. extent -= sizeof (* number);
  203. }
  204. else if (! strcmp (object, "xhuge"))
  205. {
  206. uint64_t * number = (uint64_t *) (memory);
  207. if (sizeof (* number) > extent)
  208. {
  209. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  210. }
  211. printf ("0x%016" PRIX64, LE64TOH (* number));
  212. memory += sizeof (* number);
  213. extent -= sizeof (* number);
  214. }
  215. #endif
  216. else if (! strcmp (object, "mac"))
  217. {
  218. length = ETHER_ADDR_LEN;
  219. if (length > extent)
  220. {
  221. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  222. }
  223. getmemory (memory, extent, object, length);
  224. memory += length;
  225. extent -= length;
  226. }
  227. else if (! strcmp (object, "key"))
  228. {
  229. length = PIB_KEY_LEN;
  230. if (length > extent)
  231. {
  232. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  233. }
  234. getmemory (memory, extent, object, length);
  235. memory += length;
  236. extent -= length;
  237. }
  238. else if (! strcmp (object, "hfid"))
  239. {
  240. length = PIB_HFID_LEN;
  241. if (length > extent)
  242. {
  243. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  244. }
  245. getstring (memory, extent, object, length);
  246. memory += length;
  247. extent -= length;
  248. }
  249. #if 1
  250. else if (! strcmp (object, "adminusername") || ! strcmp (object, "adminpassword") || ! strcmp (object, "accessusername"))
  251. {
  252. length = PIB_NAME_LEN + 1;
  253. if (length > extent)
  254. {
  255. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  256. }
  257. getstring (memory, extent, object, length);
  258. memory += length;
  259. extent -= length;
  260. }
  261. else if (! strcmp (object, "accesspassword"))
  262. {
  263. length = PIB_HFID_LEN + 1;
  264. if (length > extent)
  265. {
  266. error (1, ECANCELED, GETPIB_TOOBIG, object, extent);
  267. }
  268. getstring (memory, extent, object, length);
  269. memory += length;
  270. extent -= length;
  271. }
  272. else if (! strcmp (object, "username") || ! strcmp (object, "password") || ! strcmp (object, "url"))
  273. {
  274. length = PIB_TEXT_LEN + 1;
  275. getstring (memory, extent, object, length);
  276. memory += length;
  277. extent -= length;
  278. }
  279. #endif
  280. else if (! strcmp (object, "data"))
  281. {
  282. if (! * argv)
  283. {
  284. error (1, EINVAL, GETPIB_NOSIZE, object);
  285. }
  286. length = (unsigned) (uintspec (* argv, 1, extent));
  287. hexout (memory, length, 0, 0, stdout);
  288. memory += length;
  289. extent -= length;
  290. argc--;
  291. argv++;
  292. }
  293. else if (! strcmp (object, "text"))
  294. {
  295. if (! * argv)
  296. {
  297. error (1, EINVAL, GETPIB_NOSIZE, object);
  298. }
  299. length = (unsigned) (uintspec (* argv, 1, extent));
  300. getstring (memory, extent, object, length);
  301. memory += length;
  302. extent -= length;
  303. argc--;
  304. argv++;
  305. }
  306. else if (! strcmp (object, "skip"))
  307. {
  308. if (! * argv)
  309. {
  310. error (1, EINVAL, GETPIB_NOSIZE, object);
  311. }
  312. length = (unsigned) (uintspec (* argv, 1, extent));
  313. memory += length;
  314. extent -= length;
  315. argc--;
  316. argv++;
  317. continue;
  318. }
  319. else
  320. {
  321. error (1, ENOTSUP, "%s", object);
  322. }
  323. if ((argc) && (* argv))
  324. {
  325. putc (comma, stdout);
  326. }
  327. }
  328. return;
  329. }
  330. /*====================================================================*
  331. *
  332. * signed pibimage1 (int argc, char const * argv [], char comma);
  333. *
  334. * read an entire flat parameter file into memory, edit it, save
  335. * it and display it;
  336. *
  337. * Contributor(s):
  338. * Charles Maier <cmaier@qca.qualcomm.com>
  339. *
  340. *--------------------------------------------------------------------*/
  341. static signed pibimage1 (int argc, char const * argv [], char comma)
  342. {
  343. signed fd;
  344. off_t extent;
  345. byte * memory;
  346. if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1)
  347. {
  348. error (1, errno, FILE_CANTOPEN, * argv);
  349. }
  350. if ((extent = lseek (fd, 0, SEEK_END)) == - 1)
  351. {
  352. error (1, errno, FILE_CANTSIZE, * argv);
  353. }
  354. if (lseek (fd, 0, SEEK_SET))
  355. {
  356. error (1, errno, FILE_CANTHOME, * argv);
  357. }
  358. if (! (memory = malloc (extent)))
  359. {
  360. error (1, errno, FILE_CANTLOAD, * argv);
  361. }
  362. if (read (fd, memory, extent) != extent)
  363. {
  364. error (1, errno, FILE_CANTREAD, * argv);
  365. }
  366. close (fd);
  367. snatch (argc - 1, argv + 1, memory, extent, comma);
  368. free (memory);
  369. return (0);
  370. }
  371. /*====================================================================*
  372. *
  373. * signed pibimage2 (int argc, char const * argv [], char comma);
  374. *
  375. * read an entire flat parameter file into memory, edit it, save
  376. * it and display it;
  377. *
  378. * Contributor(s):
  379. * Charles Maier <cmaier@qca.qualcomm.com>
  380. *
  381. *--------------------------------------------------------------------*/
  382. static signed pibimage2 (int argc, char const * argv [], char comma)
  383. {
  384. struct panther_nvm_header header;
  385. signed fd;
  386. off_t extent;
  387. byte * memory;
  388. if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1)
  389. {
  390. error (1, errno, FILE_CANTOPEN, * argv);
  391. }
  392. if (panther_nvm_seek (fd, * argv, & header, NVM_IMAGE_PIB))
  393. {
  394. error (1, errno, "Can't find PIB image in %s", * argv);
  395. }
  396. extent = LE32TOH (header.ImageLength);
  397. if (! (memory = malloc (extent)))
  398. {
  399. error (1, errno, FILE_CANTLOAD, * argv);
  400. }
  401. if (read (fd, memory, extent) != extent)
  402. {
  403. error (1, errno, FILE_CANTREAD, * argv);
  404. }
  405. close (fd);
  406. snatch (argc - 1, argv + 1, memory, extent, comma);
  407. free (memory);
  408. return (0);
  409. }
  410. /*====================================================================*
  411. *
  412. * signed function (int argc, char const * argv [], char comma);
  413. *
  414. * call an appropriate parameter edit function based on the file
  415. * header;
  416. *
  417. * older parameter files are flat with their own header; newer ones
  418. * are image chains where one of image contains the parameter block;
  419. *
  420. *
  421. * Contributor(s):
  422. * Charles Maier <cmaier@qca.qualcomm.com>
  423. *
  424. *--------------------------------------------------------------------*/
  425. static signed function (int argc, char const * argv [], char comma)
  426. {
  427. uint32_t version;
  428. signed status;
  429. signed fd;
  430. if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1)
  431. {
  432. error (1, errno, FILE_CANTOPEN, * argv);
  433. }
  434. if (read (fd, & version, sizeof (version)) != sizeof (version))
  435. {
  436. error (1, errno, FILE_CANTREAD, * argv);
  437. }
  438. close (fd);
  439. if (LE32TOH (version) == 0x00010001)
  440. {
  441. status = pibimage2 (argc, argv, comma);
  442. }
  443. else
  444. {
  445. status = pibimage1 (argc, argv, comma);
  446. }
  447. return (status);
  448. }
  449. /*====================================================================*
  450. *
  451. * int main (int argc, char const * argv []);
  452. *
  453. *
  454. *--------------------------------------------------------------------*/
  455. int main (int argc, char const * argv [])
  456. {
  457. static char const * optv [] =
  458. {
  459. "c:qvn",
  460. "file offset type [size]\n\n\tstandard-length types are 'byte'|'word'|'long'|'huge'|'hfid'|'mac'|'key'\n\tvariable-length types are 'data'|'text'|'skip' and need a size",
  461. "PIB Data Extractor",
  462. "c c\tobject separator is (c) [" LITERAL (GETPIB_COMMA) "]",
  463. "n\tappend newline",
  464. "q\tquiet mode",
  465. "v\tverbose mode",
  466. (char const *) (0)
  467. };
  468. flag_t flags = (flag_t) (0);
  469. char comma = GETPIB_COMMA;
  470. signed c;
  471. optind = 1;
  472. opterr = 1;
  473. while (~ (c = getoptv (argc, argv, optv)))
  474. {
  475. switch (c)
  476. {
  477. case 'c':
  478. comma = * optarg;
  479. break;
  480. case 'n':
  481. _setbits (flags, GETPIB_NEWLINE);
  482. break;
  483. case 'q':
  484. _setbits (flags, GETPIB_SILENCE);
  485. break;
  486. case 'v':
  487. _setbits (flags, GETPIB_VERBOSE);
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. argc -= optind;
  494. argv += optind;
  495. if ((argc) && (* argv))
  496. {
  497. function (argc, argv, comma);
  498. if (_anyset (flags, GETPIB_NEWLINE))
  499. {
  500. putc ('\n', stdout);
  501. }
  502. }
  503. return (0);
  504. }