modpib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * modpib.c -
  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 <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include <sys/stat.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. /*====================================================================*
  30. * custom header files;
  31. *--------------------------------------------------------------------*/
  32. #include "../tools/getoptv.h"
  33. #include "../tools/number.h"
  34. #include "../tools/symbol.h"
  35. #include "../tools/flags.h"
  36. #include "../tools/error.h"
  37. #include "../tools/files.h"
  38. #include "../tools/chars.h"
  39. #include "../plc/plc.h"
  40. #include "../key/HPAVKey.h"
  41. #include "../key/keys.h"
  42. #include "../pib/pib.h"
  43. /*====================================================================*
  44. * custom source files;
  45. *--------------------------------------------------------------------*/
  46. #ifndef MAKEFILE
  47. #include "../tools/getoptv.c"
  48. #include "../tools/putoptv.c"
  49. #include "../tools/version.c"
  50. #include "../tools/checksum32.c"
  51. #include "../tools/fdchecksum32.c"
  52. #include "../tools/uintspec.c"
  53. #include "../tools/hexencode.c"
  54. #include "../tools/hexdecode.c"
  55. #include "../tools/hexstring.c"
  56. #include "../tools/todigit.c"
  57. #include "../tools/memincr.c"
  58. #include "../tools/synonym.c"
  59. #include "../tools/error.c"
  60. #endif
  61. #ifndef MAKEFILE
  62. #include "../pib/pibfile.c"
  63. #include "../pib/lightning_pib_file.c"
  64. #include "../pib/lightning_pib_peek.c"
  65. #include "../pib/panther_pib_file.c"
  66. #include "../pib/panther_pib_peek.c"
  67. #endif
  68. #ifndef MAKEFILE
  69. #include "../key/HPAVKeyNID.c"
  70. #include "../key/SHA256Reset.c"
  71. #include "../key/SHA256Write.c"
  72. #include "../key/SHA256Block.c"
  73. #include "../key/SHA256Fetch.c"
  74. #include "../key/keys.c"
  75. #endif
  76. /*====================================================================*
  77. * constants;
  78. *--------------------------------------------------------------------*/
  79. #define MEMBERSHIP_OFFSET 0x00001E8C
  80. #define MEMBERSHIP_STATUS 0x00000000
  81. /*====================================================================*
  82. *
  83. * signed pibimage1 (struct _file_ * file, simple_pib *sample_pib, signed level, flag_t flags);
  84. *
  85. * modify selected PIB header values and compute a new checksum;
  86. * this function assumes that the file is open and positioned to
  87. * the start of the parameter block;
  88. *
  89. * Contributor(s):
  90. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  91. * Charles Maier <cmaier@qca.qualcomm.com>
  92. *
  93. *--------------------------------------------------------------------*/
  94. static signed pibimage1 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags)
  95. {
  96. struct simple_pib simple_pib;
  97. memset (& simple_pib, 0, sizeof (simple_pib));
  98. if (read (file->file, & simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  99. {
  100. error (1, errno, FILE_CANTREAD, file->name);
  101. }
  102. if (_anyset (flags, PIB_MAC))
  103. {
  104. memcpy (simple_pib.MAC, sample_pib->MAC, sizeof (simple_pib.MAC));
  105. }
  106. if (_anyset (flags, PIB_MACINC))
  107. {
  108. memincr (simple_pib.MAC, sizeof (simple_pib.MAC));
  109. }
  110. if (_anyset (flags, PIB_DAK))
  111. {
  112. memcpy (simple_pib.DAK, sample_pib->DAK, sizeof (simple_pib.DAK));
  113. }
  114. if (_anyset (flags, PIB_NMK))
  115. {
  116. memcpy (simple_pib.NMK, sample_pib->NMK, sizeof (simple_pib.NMK));
  117. }
  118. if (_anyset (flags, PIB_NETWORK))
  119. {
  120. memcpy (simple_pib.NET, sample_pib->NET, sizeof (simple_pib.NET));
  121. }
  122. if (_anyset (flags, PIB_MFGSTRING))
  123. {
  124. memcpy (simple_pib.MFG, sample_pib->MFG, sizeof (simple_pib.MFG));
  125. }
  126. if (_anyset (flags, PIB_USER))
  127. {
  128. memcpy (simple_pib.USR, sample_pib->USR, sizeof (simple_pib.USR));
  129. }
  130. if (_anyset (flags, PIB_CCO_MODE))
  131. {
  132. simple_pib.CCoSelection = sample_pib->CCoSelection;
  133. }
  134. if (_anyset (flags, PIB_NMK | PIB_NID))
  135. {
  136. #if 0
  137. /*
  138. * clear the AVLNMembership bit whenever the NMK or NID change; this is an important
  139. * step because it prevents false network association;
  140. */
  141. if (BE16TOH (simple_pib.PIBVERSION) > 0x0200)
  142. {
  143. uint32_t membership = MEMBERSHIP_STATUS;
  144. if (lseek (file->file, MEMBERSHIP_OFFSET, SEEK_SET) != MEMBERSHIP_OFFSET)
  145. {
  146. error (1, errno, FILE_CANTHOME, file->name);
  147. }
  148. if (write (file->file, & membership, sizeof (membership)) != sizeof (membership))
  149. {
  150. error (1, errno, FILE_CANTSAVE, file->name);
  151. }
  152. }
  153. #endif
  154. if (_allclr (flags, PIB_NID))
  155. {
  156. level = simple_pib.PreferredNID [HPAVKEY_NID_LEN - 1] >> 4;
  157. }
  158. HPAVKeyNID (simple_pib.PreferredNID, simple_pib.NMK, level & 1);
  159. }
  160. if (lseek (file->file, (off_t) (0) - sizeof (simple_pib), SEEK_CUR) == - 1)
  161. {
  162. error (1, errno, FILE_CANTHOME, file->name);
  163. }
  164. if (write (file->file, & simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  165. {
  166. error (1, errno, FILE_CANTSAVE, file->name);
  167. }
  168. if (lseek (file->file, (off_t) (0) - sizeof (simple_pib), SEEK_CUR) == - 1)
  169. {
  170. error (1, errno, FILE_CANTHOME, file->name);
  171. }
  172. simple_pib.CHECKSUM = fdchecksum32 (file->file, LE16TOH (simple_pib.PIBLENGTH), simple_pib.CHECKSUM);
  173. if (lseek (file->file, (off_t) (0) - LE16TOH (simple_pib.PIBLENGTH), SEEK_CUR) == - 1)
  174. {
  175. error (1, errno, FILE_CANTHOME, file->name);
  176. }
  177. if (write (file->file, & simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  178. {
  179. error (1, errno, FILE_CANTSAVE, file->name);
  180. }
  181. if (_anyset (flags, PIB_VERBOSE))
  182. {
  183. lightning_pib_peek (& simple_pib);
  184. }
  185. return (0);
  186. }
  187. /*====================================================================*
  188. *
  189. * signed pibimage2 (struct _file_ * file, simple_pib *sample_pib, signed level, flag_t flags);
  190. *
  191. * modify selected PIB header values but do not compute a checksum;
  192. *
  193. * Contributor(s):
  194. * Charles Maier <cmaier@qca.qualcomm.com>
  195. *
  196. *--------------------------------------------------------------------*/
  197. static signed pibimage2 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags)
  198. {
  199. struct simple_pib simple_pib;
  200. memset (& simple_pib, 0, sizeof (simple_pib));
  201. if (read (file->file, & simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  202. {
  203. error (1, errno, FILE_CANTREAD, file->name);
  204. }
  205. if (_anyset (flags, PIB_MAC))
  206. {
  207. memcpy (simple_pib.MAC, sample_pib->MAC, sizeof (simple_pib.MAC));
  208. }
  209. if (_anyset (flags, PIB_MACINC))
  210. {
  211. memincr (simple_pib.MAC, sizeof (simple_pib.MAC));
  212. }
  213. if (_anyset (flags, PIB_DAK))
  214. {
  215. memcpy (simple_pib.DAK, sample_pib->DAK, sizeof (simple_pib.DAK));
  216. }
  217. if (_anyset (flags, PIB_NMK))
  218. {
  219. memcpy (simple_pib.NMK, sample_pib->NMK, sizeof (simple_pib.NMK));
  220. }
  221. if (_anyset (flags, PIB_NETWORK))
  222. {
  223. memcpy (simple_pib.NET, sample_pib->NET, sizeof (simple_pib.NET));
  224. }
  225. if (_anyset (flags, PIB_MFGSTRING))
  226. {
  227. memcpy (simple_pib.MFG, sample_pib->MFG, sizeof (simple_pib.MFG));
  228. }
  229. if (_anyset (flags, PIB_USER))
  230. {
  231. memcpy (simple_pib.USR, sample_pib->USR, sizeof (simple_pib.USR));
  232. }
  233. if (_anyset (flags, PIB_CCO_MODE))
  234. {
  235. simple_pib.CCoSelection = sample_pib->CCoSelection;
  236. }
  237. if (_anyset (flags, PIB_NMK | PIB_NID))
  238. {
  239. #if 0
  240. /*
  241. * clear the AVLNMembership bit whenever the NMK or NID change; this is an important
  242. * step because it prevents false network association;
  243. */
  244. if (BE16TOH (simple_pib.PIBVERSION) > 0x0200)
  245. {
  246. uint32_t membership = MEMBERSHIP_STATUS;
  247. if (lseek (file->file, MEMBERSHIP_OFFSET, SEEK_SET) != MEMBERSHIP_OFFSET)
  248. {
  249. error (1, errno, FILE_CANTHOME, file->name);
  250. }
  251. if (write (file->file, & membership, sizeof (membership)) != sizeof (membership))
  252. {
  253. error (1, errno, FILE_CANTSAVE, file->name);
  254. }
  255. }
  256. #endif
  257. if (_allclr (flags, PIB_NID))
  258. {
  259. level = simple_pib.PreferredNID [HPAVKEY_NID_LEN - 1] >> 4;
  260. }
  261. HPAVKeyNID (simple_pib.PreferredNID, simple_pib.NMK, level & 1);
  262. }
  263. if (lseek (file->file, (off_t) (0) - sizeof (simple_pib), SEEK_CUR) == - 1)
  264. {
  265. error (1, errno, FILE_CANTHOME, file->name);
  266. }
  267. if (write (file->file, & simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  268. {
  269. error (1, errno, FILE_CANTSAVE, file->name);
  270. }
  271. if (lseek (file->file, (off_t) (0) - sizeof (simple_pib), SEEK_CUR) == - 1)
  272. {
  273. error (1, errno, FILE_CANTHOME, file->name);
  274. }
  275. if (_anyset (flags, PIB_VERBOSE))
  276. {
  277. panther_pib_peek (& simple_pib);
  278. }
  279. return (0);
  280. }
  281. /*====================================================================*
  282. *
  283. * signed pibchain2 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags);
  284. *
  285. * Contributor(s):
  286. * Charles Maier <cmaier@qca.qualcomm.com>
  287. *
  288. *--------------------------------------------------------------------*/
  289. static signed pibchain2 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags)
  290. {
  291. unsigned module = 0;
  292. struct panther_nvm_header header;
  293. uint32_t prev = ~ 0;
  294. uint32_t next = 0;
  295. if (lseek (file->file, 0, SEEK_SET))
  296. {
  297. error (1, errno, NVM_IMG_CHECKSUM, file->name, module);
  298. }
  299. do
  300. {
  301. if (read (file->file, & header, sizeof (header)) != sizeof (header))
  302. {
  303. error (1, errno, NVM_HDR_CANTREAD, file->name, module);
  304. }
  305. if (LE16TOH (header.MajorVersion) != 1)
  306. {
  307. error (1, errno, NVM_HDR_VERSION, file->name, module);
  308. }
  309. if (LE16TOH (header.MinorVersion) != 1)
  310. {
  311. error (1, errno, NVM_HDR_VERSION, file->name, module);
  312. }
  313. if (checksum32 (& header, sizeof (header), 0))
  314. {
  315. error (1, errno, NVM_HDR_CHECKSUM, file->name, module);
  316. }
  317. if (LE32TOH (header.PrevHeader) != prev)
  318. {
  319. error (1, errno, NVM_HDR_LINK, file->name, module);
  320. }
  321. if (LE32TOH (header.ImageType) == NVM_IMAGE_PIB)
  322. {
  323. pibimage2 (file, sample_pib, level, flags);
  324. header.ImageChecksum = fdchecksum32 (file->file, LE32TOH (header.ImageLength), 0);
  325. if (lseek (file->file, (off_t) (0) - LE32TOH (header.ImageLength), SEEK_CUR) == - 1)
  326. {
  327. error (1, errno, FILE_CANTHOME, file->name);
  328. }
  329. header.HeaderChecksum = checksum32 (& header, sizeof (header), header.HeaderChecksum);
  330. if (lseek (file->file, (off_t) (0) - sizeof (header), SEEK_CUR) == - 1)
  331. {
  332. error (1, errno, FILE_CANTHOME, file->name);
  333. }
  334. if (write (file->file, & header, sizeof (header)) != sizeof (header))
  335. {
  336. error (1, errno, FILE_CANTSAVE, file->name);
  337. }
  338. if (lseek (file->file, (off_t) (0) - sizeof (header), SEEK_CUR) == - 1)
  339. {
  340. error (1, errno, FILE_CANTHOME, file->name);
  341. }
  342. break;
  343. }
  344. if (fdchecksum32 (file->file, LE32TOH (header.ImageLength), header.ImageChecksum))
  345. {
  346. error (1, errno, NVM_IMG_CHECKSUM, file->name, module);
  347. }
  348. prev = next;
  349. next = LE32TOH (header.NextHeader);
  350. module++;
  351. }
  352. while (~ header.NextHeader);
  353. return (0);
  354. }
  355. /*====================================================================*
  356. *
  357. * signed function (char const * filename, struct simple_pib * simple_pib, unsigned level, flag_t flags);
  358. *
  359. * Contributor(s):
  360. * Charles Maier <cmaier@qca.qualcomm.com>
  361. *
  362. *--------------------------------------------------------------------*/
  363. static signed function (char const * filename, struct simple_pib * sample_pib, unsigned level, flag_t flags)
  364. {
  365. uint32_t version;
  366. signed status;
  367. struct _file_ file;
  368. file.name = filename;
  369. if ((file.file = open (file.name, O_BINARY | O_RDWR, FILE_FILEMODE)) == - 1)
  370. {
  371. if (_allclr (flags, PIB_SILENCE))
  372. {
  373. error (0, errno, FILE_CANTOPEN, file.name);
  374. }
  375. return (- 1);
  376. }
  377. if (read (file.file, & version, sizeof (version)) != sizeof (version))
  378. {
  379. if (_allclr (flags, PIB_SILENCE))
  380. {
  381. error (0, errno, FILE_CANTREAD, file.name);
  382. }
  383. return (- 1);
  384. }
  385. if (lseek (file.file, 0, SEEK_SET))
  386. {
  387. error (1, errno, FILE_CANTHOME, file.name);
  388. }
  389. if (LE32TOH (version) == 0x00010001)
  390. {
  391. status = pibchain2 (& file, sample_pib, level, flags);
  392. }
  393. else
  394. {
  395. status = pibimage1 (& file, sample_pib, level, flags);
  396. }
  397. close (file.file);
  398. return (status);
  399. }
  400. /*====================================================================*
  401. *
  402. * int main (int argc, char const * argv [])
  403. *
  404. *
  405. *--------------------------------------------------------------------*/
  406. int main (int argc, char const * argv [])
  407. {
  408. extern struct _term_ const daks [];
  409. extern struct _term_ const nmks [];
  410. static char const * optv [] =
  411. {
  412. "C:D:L:M:N:S:T:U:v",
  413. "file [file] [...]",
  414. "modify selected PIB parameters and update checksum",
  415. "C n\tCCo Selection is n",
  416. "D x\tDAK as xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",
  417. "L n\tsecurity level is n",
  418. "M x\tMAC as xx:xx:xx:xx:xx:xx",
  419. "N x\tNMK as xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",
  420. "S s\tMFG string is s",
  421. "T s\tNET string is s",
  422. "U s\tUSR string is s",
  423. "v\tverbose messages",
  424. (char const *) (0)
  425. };
  426. struct simple_pib sample_pib;
  427. signed level = 0;
  428. signed state = 0;
  429. flag_t flags = (flag_t) (0);
  430. signed c;
  431. optind = 1;
  432. memset (& sample_pib, 0, sizeof (sample_pib));
  433. while (~ (c = getoptv (argc, argv, optv)))
  434. {
  435. char const * sp;
  436. switch ((char) (c))
  437. {
  438. case 'C':
  439. _setbits (flags, PIB_CCO_MODE);
  440. sample_pib.CCoSelection = (uint8_t) (uintspec (optarg, 0, 4));
  441. break;
  442. case 'D':
  443. if (!hexencode (sample_pib.DAK, sizeof (sample_pib.DAK), synonym (optarg, daks, SIZEOF (daks))))
  444. {
  445. error (1, errno, PLC_BAD_DAK, optarg);
  446. }
  447. _setbits (flags, PIB_DAK);
  448. break;
  449. case 'L':
  450. level = (uint8_t) (uintspec (optarg, 0, 1));
  451. _setbits (flags, PIB_NID);
  452. break;
  453. case 'M':
  454. if (! strcmp (optarg, "auto"))
  455. {
  456. _setbits (flags, PIB_MACINC);
  457. break;
  458. }
  459. if (! strcmp (optarg, "next"))
  460. {
  461. _setbits (flags, PIB_MACINC);
  462. break;
  463. }
  464. if (! strcmp (optarg, "plus"))
  465. {
  466. _setbits (flags, PIB_MACINC);
  467. break;
  468. }
  469. if (! hexencode (sample_pib.MAC, sizeof (sample_pib.MAC), optarg))
  470. {
  471. error (1, errno, PLC_BAD_MAC, optarg);
  472. }
  473. _setbits (flags, PIB_MAC);
  474. break;
  475. case 'N':
  476. if (!hexencode (sample_pib.NMK, sizeof (sample_pib.NMK), synonym (optarg, nmks, SIZEOF (nmks))))
  477. {
  478. error (1, errno, PLC_BAD_NMK, optarg);
  479. }
  480. _setbits (flags, PIB_NMK);
  481. break;
  482. case 'S':
  483. for (sp = optarg; isprint ((unsigned char)* sp); ++ sp);
  484. if (* sp)
  485. {
  486. error (1, EINVAL, "NMK contains illegal characters");
  487. }
  488. if ((sp - optarg) > (signed) (sizeof (sample_pib.MFG) - 1))
  489. {
  490. error (1, 0, "Manufacturing string is at most %u chars", (unsigned) (sizeof (sample_pib.MFG) - 1));
  491. }
  492. memcpy (sample_pib.MFG, optarg, strlen (optarg));
  493. _setbits (flags, PIB_MFGSTRING);
  494. break;
  495. case 'T':
  496. for (sp = optarg; isprint ((unsigned char)* sp); ++ sp);
  497. if (* sp)
  498. {
  499. error (1, EINVAL, "NMK contains illegal characters");
  500. }
  501. if ((sp - optarg) > (signed) (sizeof (sample_pib.NET) - 1))
  502. {
  503. error (1, 0, "NET string is at most %u chars", (unsigned) (sizeof (sample_pib.NET) - 1));
  504. }
  505. memcpy (sample_pib.NET, optarg, strlen (optarg));
  506. _setbits (flags, PIB_NETWORK);
  507. break;
  508. case 'U':
  509. for (sp = optarg; isprint ((unsigned char)* sp); ++ sp);
  510. if (* sp)
  511. {
  512. error (1, EINVAL, "NMK contains illegal characters");
  513. }
  514. if ((sp - optarg) > (signed) (sizeof (sample_pib.USR) - 1))
  515. {
  516. error (1, 0, "USR string is at most %u chars", (unsigned) (sizeof (sample_pib.USR) - 1));
  517. }
  518. memcpy (sample_pib.USR, optarg, strlen (optarg));
  519. _setbits (flags, PIB_USER);
  520. break;
  521. case 'q':
  522. _setbits (flags, PIB_SILENCE);
  523. break;
  524. case 'v':
  525. _setbits (flags, PIB_VERBOSE);
  526. break;
  527. default:
  528. break;
  529. }
  530. }
  531. argc -= optind;
  532. argv += optind;
  533. while ((argc) && (* argv))
  534. {
  535. if (function (* argv, & sample_pib, level, flags))
  536. {
  537. state = 1;
  538. }
  539. argc--;
  540. argv++;
  541. }
  542. exit (state);
  543. }