ampinit.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. * system header files;
  10. *--------------------------------------------------------------------*/
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <stdint.h>
  14. #include <limits.h>
  15. /*====================================================================*
  16. * custom header files;
  17. *--------------------------------------------------------------------*/
  18. #include "../tools/getoptv.h"
  19. #include "../tools/putoptv.h"
  20. #include "../tools/memory.h"
  21. #include "../tools/number.h"
  22. #include "../tools/symbol.h"
  23. #include "../tools/types.h"
  24. #include "../tools/flags.h"
  25. #include "../tools/files.h"
  26. #include "../tools/error.h"
  27. #include "../key/HPAVKey.h"
  28. #include "../key/keys.h"
  29. #include "../ram/sdram.h"
  30. #include "../pib/pib.h"
  31. #include "../nvm/nvm.h"
  32. #include "../plc/plc.h"
  33. #include "../nda/nda.h"
  34. /*====================================================================*
  35. * custom source files;
  36. *--------------------------------------------------------------------*/
  37. #ifndef MAKEFILE
  38. #include "../plc/Attributes1.c"
  39. #include "../plc/chipset.c"
  40. #include "../plc/Confirm.c"
  41. #include "../plc/Devices.c"
  42. #include "../plc/Display.c"
  43. #include "../plc/FactoryDefaults.c"
  44. #include "../plc/Failure.c"
  45. #include "../plc/HostActionResponse.c"
  46. #include "../plc/Identity1.c"
  47. #include "../plc/NVRAMInfo.c"
  48. #include "../plc/PushButton.c"
  49. #include "../plc/ReadFirmware1.c"
  50. #include "../plc/ReadParameters1.c"
  51. #include "../plc/Request.c"
  52. #include "../plc/ReadMME.c"
  53. #include "../plc/ReadMFG.c"
  54. #include "../plc/RemoteHosts.c"
  55. #include "../plc/SendMME.c"
  56. #include "../plc/SetNMK.c"
  57. #include "../plc/SDRAMInfo.c"
  58. #include "../plc/StationRole.c"
  59. #include "../plc/WaitForReset.c"
  60. #include "../plc/WaitForRestart.c"
  61. #include "../plc/WaitForStart.c"
  62. #include "../plc/WriteCFG.c"
  63. #include "../plc/WriteNVM.c"
  64. #include "../plc/WritePIB.c"
  65. #include "../plc/WatchdogReport.c"
  66. #include "../plc/ResetDevice.c"
  67. #include "../plc/NetInfo2.c"
  68. #include "../plc/VersionInfo1.c"
  69. #endif
  70. #ifndef MAKEFILE
  71. #include "../nda/FlashDevice3.c"
  72. #include "../nda/FlashPTS.c"
  73. #include "../nda/EraseFlashMemory1.c"
  74. #include "../nda/ReadFlashFirmware.c"
  75. #include "../nda/ReadFlashParameters.c"
  76. #endif
  77. #ifndef MAKEFILE
  78. #include "../tools/getoptv.c"
  79. #include "../tools/putoptv.c"
  80. #include "../tools/version.c"
  81. #include "../tools/basespec.c"
  82. #include "../tools/uintspec.c"
  83. #include "../tools/hexdump.c"
  84. #include "../tools/hexencode.c"
  85. #include "../tools/hexdecode.c"
  86. #include "../tools/hexstring.c"
  87. #include "../tools/hexout.c"
  88. #include "../tools/todigit.c"
  89. #include "../tools/checkfilename.c"
  90. #include "../tools/checksum32.c"
  91. #include "../tools/fdchecksum32.c"
  92. #include "../tools/error.c"
  93. #include "../tools/strfbits.c"
  94. #include "../tools/synonym.c"
  95. #include "../tools/typename.c"
  96. #endif
  97. #ifndef MAKEFILE
  98. #include "../ether/openchannel.c"
  99. #include "../ether/closechannel.c"
  100. #include "../ether/readpacket.c"
  101. #include "../ether/sendpacket.c"
  102. #include "../ether/channel.c"
  103. #endif
  104. #ifndef MAKEFILE
  105. #include "../ram/nvram.c"
  106. #include "../ram/sdramfile.c"
  107. #include "../ram/sdrampeek.c"
  108. #endif
  109. #ifndef MAKEFILE
  110. #include "../nvm/lightning_nvm_file.c"
  111. #endif
  112. #ifndef MAKEFILE
  113. #include "../pib/lightning_pib_file.c"
  114. #include "../pib/lightning_pib_peek.c"
  115. #endif
  116. #ifndef MAKEFILE
  117. #include "../mme/EthernetHeader.c"
  118. #include "../mme/QualcommHeader.c"
  119. #include "../mme/QualcommHeader1.c"
  120. #include "../mme/UnwantedMessage.c"
  121. #include "../mme/MMECode.c"
  122. #endif
  123. #ifndef MAKEFILE
  124. #include "../key/keys.c"
  125. #endif
  126. /*====================================================================*
  127. * program constants;
  128. *--------------------------------------------------------------------*/
  129. #define DEFAULT_LOOP 1
  130. #define DEFAULT_WAIT 0
  131. /*====================================================================*
  132. * program variables;
  133. *--------------------------------------------------------------------*/
  134. static const struct _term_ modules [] =
  135. {
  136. {
  137. "nvm",
  138. "1"
  139. },
  140. {
  141. "pib",
  142. "2"
  143. },
  144. {
  145. "both",
  146. "3"
  147. }
  148. };
  149. static const struct _term_ buttons [] =
  150. {
  151. {
  152. "join",
  153. "1"
  154. },
  155. {
  156. "leave",
  157. "2"
  158. },
  159. {
  160. "reset",
  161. "4"
  162. },
  163. {
  164. "status",
  165. "3"
  166. }
  167. };
  168. /*====================================================================*
  169. *
  170. * void manager (struct plc * plc, signed loop, signed wait);
  171. *
  172. * perform operations in logical order despite any order specfied
  173. * on the command line; for example, read PIB before writing PIB;
  174. *
  175. * operation order is controlled by the order of "if" statements
  176. * shown here; the entire operation sequence can be repeated with
  177. * an optional pause between each iteration;
  178. *
  179. *--------------------------------------------------------------------*/
  180. static void manager (struct plc * plc, signed loop, signed wait)
  181. {
  182. while (loop-- > 0)
  183. {
  184. if (_anyset (plc->flags, PLC_VERSION))
  185. {
  186. VersionInfo1 (plc);
  187. }
  188. if (_anyset (plc->flags, PLC_ATTRIBUTES))
  189. {
  190. Attributes1 (plc);
  191. }
  192. if (_anyset (plc->flags, PLC_WATCHDOG_REPORT))
  193. {
  194. WatchdogReport (plc);
  195. }
  196. if (_anyset (plc->flags, PLC_NVRAM_INFO))
  197. {
  198. NVRAMInfo (plc);
  199. }
  200. if (_anyset (plc->flags, PLC_SDRAM_INFO))
  201. {
  202. SDRAMInfo (plc);
  203. }
  204. if (_anyset (plc->flags, PLC_READ_IDENTITY))
  205. {
  206. Identity1 (plc);
  207. }
  208. if (_anyset (plc->flags, PLC_REMOTEHOSTS))
  209. {
  210. RemoteHosts (plc);
  211. }
  212. if (_anyset (plc->flags, PLC_NETWORK))
  213. {
  214. NetInfo2 (plc);
  215. }
  216. if (_anyset (plc->flags, PLC_READ_MAC))
  217. {
  218. ReadFlashFirmware (plc);
  219. }
  220. if (_anyset (plc->flags, PLC_WRITE_MAC))
  221. {
  222. WriteNVM (plc);
  223. }
  224. if (_anyset (plc->flags, PLC_READ_PIB))
  225. {
  226. ReadFlashParameters (plc);
  227. }
  228. if (_anyset (plc->flags, PLC_WRITE_PIB))
  229. {
  230. WritePIB (plc);
  231. }
  232. if (_anyset (plc->flags, PLC_HOST_ACTION))
  233. {
  234. HostActionResponse (plc);
  235. }
  236. if (_anyset (plc->flags, PLC_PUSH_BUTTON))
  237. {
  238. PushButton (plc);
  239. }
  240. if (_anyset (plc->flags, (PLC_SETLOCALKEY | PLC_SETREMOTEKEY)))
  241. {
  242. SetNMK (plc);
  243. }
  244. if (_anyset (plc->flags, PLC_FACTORY_DEFAULTS))
  245. {
  246. FactoryDefaults (plc);
  247. }
  248. if (_anyset (plc->flags, PLC_ERASE_DEVICE))
  249. {
  250. EraseFlashMemory1 (plc);
  251. }
  252. if (_anyset (plc->flags, PLC_FLASH_DEVICE))
  253. {
  254. FlashDevice3 (plc);
  255. }
  256. if (_anyset (plc->flags, PLC_RESET_DEVICE))
  257. {
  258. ResetDevice (plc);
  259. }
  260. sleep (wait);
  261. }
  262. return;
  263. }
  264. /*====================================================================*
  265. *
  266. * int main (int argc, char const * argv[]);
  267. *
  268. * parse command line, populate int6k structure and perform selected
  269. * operations; show help summary if asked; see getoptv and putoptv
  270. * to understand command line parsing and help summary display; see
  271. * plc.h for the definition of struct plc;
  272. *
  273. * the command line accepts multiple MAC addresses and the program
  274. * performs the specified operations on each address, in turn; the
  275. * address order is significant but the option order is not; the
  276. * default address is a local broadcast that causes all devices on
  277. * the local H1 interface to respond but not those at the remote
  278. * end of the powerline;
  279. *
  280. * the default address is 00:B0:52:00:00:01; omitting the address
  281. * will automatically address the local device; some options will
  282. * cancel themselves if this makes no sense;
  283. *
  284. * the default interface is eth1 because most people use eth0 as
  285. * their principle network connection; you can specify another
  286. * interface with -i or define environment string PLC to make
  287. * that the default interface and save typing;
  288. *
  289. *--------------------------------------------------------------------*/
  290. int main (int argc, char const * argv [])
  291. {
  292. extern struct channel channel;
  293. extern struct _term_ const devices [];
  294. extern struct _term_ const daks [];
  295. extern struct _term_ const nmks [];
  296. static char const * optv [] =
  297. {
  298. "aAbB:C:d:D:eEfFHi:IJ:K:l:mMn:N:op:P:qQrRst:TvW:w:x12",
  299. "device [device] [...]",
  300. "Qualcomm Atheros AR7400 Powerline Device Manager",
  301. "a\tread Device Attributes using VS_OP_ATTRIBUTES",
  302. "b\tread device enumeration ID table using VS_EM_ID_TABLE",
  303. "B n\tperform pushbutton action (n) using MS_PB_ENC [1|2|3|4|'join'|'leave'|'status'|'reset']",
  304. "C n\tflash NVRAM with module (n) using VS_MOD_NVM [1|2|3|'nvm'|'pib'|'both']",
  305. "d f\tdump and clear watchdog report to file (f) using VS_WD_RPT",
  306. "D x\tset Device Access Key (DAK) to (x) for option -E, -F and -J [" DAK1 "]",
  307. "e\tredirect stderr messages to stdout",
  308. "E\terase NVRAM then reset using VS_PTS_NVM",
  309. "f\tread NVRAM Configuration using VS_GET_NVM",
  310. "F[F]\tflash [force] NVRAM with firmware and PIB using VS_PTS_NVM",
  311. #if defined (WINPCAP) || defined (LIBPCAP)
  312. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  313. #else
  314. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  315. #endif
  316. "H\tstop host action requests using VS_HOST_ACTION.IND",
  317. "I\tread device identity using VS_RD_MOD",
  318. "J\tchange Network Membership Key (NMK) on remote device using VS_SET_KEY (see -D and -K)",
  319. "K x\tset Network Membership Key (NMK) to (x) for options -J or -M [" NMK1 "]",
  320. "l n\tloop (n) times [" LITERAL (DEFAULT_LOOP) "]",
  321. "m\tread network membership information using VS_NW_INFO",
  322. "M\tchange Network Membership Key (NMK) on local device using VS_SET_KEY (see -K)",
  323. "n f\tread NVM from SDRAM to file (f) using VS_RD_MOD",
  324. "N f\twrite NVM file (f) to SDRAM using VS_WR_MOD",
  325. "o\tread flash memory using VS_MODULE_OPERATION",
  326. "p f\tread PIB from SDRAM to file (f) using VS_RD_MOD",
  327. "P f\twrite PIB file (f) to SDRAM using VS_WR_MOD",
  328. "q\tquiet mode",
  329. "Q\tquick flash",
  330. "r\tread hardware and firmware revision using VS_SW_VER",
  331. "R\treset device using VS_RS_DEV",
  332. "s\tread SDRAM Configuration using VS_RD_CBLOCK",
  333. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  334. "T\treset factory defaults using VS_FAC_DEFAULTS",
  335. "v\tverbose mode",
  336. "w n\tpause (n) seconds [" LITERAL (DEFAULT_WAIT) "]",
  337. "x\texit on error",
  338. "1\tsuppress confirm MME when resetting device [option -R only]",
  339. "2\tboot from host after reset [option -R only]",
  340. (char const *) (0)
  341. };
  342. #include "../plc/plc.c"
  343. signed loop = DEFAULT_LOOP;
  344. signed wait = DEFAULT_WAIT;
  345. signed c;
  346. if (getenv (PLCDEVICE))
  347. {
  348. #if defined (WINPCAP) || defined (LIBPCAP)
  349. channel.ifindex = atoi (getenv (PLCDEVICE));
  350. #else
  351. channel.ifname = strdup (getenv (PLCDEVICE));
  352. #endif
  353. }
  354. optind = 1;
  355. while (~ (c = getoptv (argc, argv, optv)))
  356. {
  357. switch (c)
  358. {
  359. case 'a':
  360. _setbits (plc.flags, PLC_ATTRIBUTES);
  361. break;
  362. case 'B':
  363. _setbits (plc.flags, PLC_PUSH_BUTTON);
  364. plc.pushbutton = (unsigned) (uintspec (synonym (optarg, buttons, SIZEOF (buttons)), 0, UCHAR_MAX));
  365. break;
  366. case 'b':
  367. _setbits (plc.flags, PLC_REMOTEHOSTS);
  368. break;
  369. case 'C':
  370. _setbits (plc.flags, PLC_FLASH_DEVICE);
  371. plc.module = (unsigned) (uintspec (synonym (optarg, modules, SIZEOF (modules)), 0, UCHAR_MAX));
  372. break;
  373. case 'd':
  374. _setbits (plc.flags, PLC_WATCHDOG_REPORT);
  375. if (! checkfilename (optarg))
  376. {
  377. error (1, EINVAL, "%s", optarg);
  378. }
  379. if ((plc.rpt.file = open (plc.rpt.name = optarg, O_BINARY | O_CREAT | O_RDWR | O_TRUNC, FILE_FILEMODE)) == -1)
  380. {
  381. error (1, errno, "%s", optarg);
  382. }
  383. #ifndef WIN32
  384. chown (optarg, getuid (), getgid ());
  385. #endif
  386. plc.readaction = 3;
  387. break;
  388. case 'D':
  389. if (! hexencode (plc.DAK, sizeof (plc.DAK), synonym (optarg, daks, SIZEOF (daks))))
  390. {
  391. error (1, errno, PLC_BAD_DAK, optarg);
  392. }
  393. break;
  394. case 'e':
  395. dup2 (STDOUT_FILENO, STDERR_FILENO);
  396. break;
  397. case 'E':
  398. _clrbits (plc.flags, PLC_FLASH_DEVICE);
  399. _setbits (plc.flags, PLC_ERASE_DEVICE);
  400. plc.module = 0x0C;
  401. break;
  402. case 'f':
  403. if (_anyset (plc.flags, PLC_NVRAM_INFO))
  404. {
  405. _setbits (plc.flags, PLC_READ_ALL);
  406. }
  407. _setbits (plc.flags, PLC_NVRAM_INFO);
  408. break;
  409. case 'F':
  410. _setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
  411. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  412. {
  413. _setbits (plc.module, VS_MODULE_FORCE);
  414. }
  415. _clrbits (plc.flags, PLC_ERASE_DEVICE);
  416. _setbits (plc.flags, PLC_FLASH_DEVICE);
  417. break;
  418. case 'H':
  419. _setbits (plc.flags, PLC_HOST_ACTION);
  420. break;
  421. case 'I':
  422. _setbits (plc.flags, PLC_READ_IDENTITY);
  423. break;
  424. case 'i':
  425. #if defined (WINPCAP) || defined (LIBPCAP)
  426. channel.ifindex = atoi (optarg);
  427. #else
  428. channel.ifname = optarg;
  429. #endif
  430. break;
  431. case 'J':
  432. if (! hexencode (plc.RDA, sizeof (plc.RDA), (char const *) (optarg)))
  433. {
  434. error (1, errno, PLC_BAD_MAC, optarg);
  435. }
  436. _setbits (plc.flags, PLC_SETREMOTEKEY);
  437. break;
  438. case 'K':
  439. if (! hexencode (plc.NMK, sizeof (plc.NMK), synonym (optarg, nmks, SIZEOF (nmks))))
  440. {
  441. error (1, errno, PLC_BAD_NMK, optarg);
  442. }
  443. break;
  444. case 'M':
  445. _setbits (plc.flags, PLC_SETLOCALKEY);
  446. break;
  447. case 'l':
  448. loop = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  449. break;
  450. case 'm':
  451. _setbits (plc.flags, PLC_NETWORK);
  452. break;
  453. case 'N':
  454. if (! checkfilename (optarg))
  455. {
  456. error (1, EINVAL, "%s", optarg);
  457. }
  458. if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY | O_RDONLY)) == -1)
  459. {
  460. error (1, errno, "%s", optarg);
  461. }
  462. if (lightning_nvm_file (& plc.NVM))
  463. {
  464. error (1, errno, "Bad lightning image file: %s", plc.NVM.name);
  465. }
  466. _setbits (plc.flags, PLC_WRITE_MAC);
  467. break;
  468. case 'n':
  469. if (! checkfilename (optarg))
  470. {
  471. error (1, EINVAL, "%s", optarg);
  472. }
  473. if ((plc.nvm.file = open (plc.nvm.name = optarg, O_BINARY | O_CREAT | O_RDWR | O_TRUNC, FILE_FILEMODE)) == -1)
  474. {
  475. error (1, errno, "%s", optarg);
  476. }
  477. #ifndef WIN32
  478. chown (optarg, getuid (), getgid ());
  479. #endif
  480. _setbits (plc.flags, PLC_READ_MAC);
  481. break;
  482. case 'P':
  483. if (! checkfilename (optarg))
  484. {
  485. error (1, EINVAL, "%s", optarg);
  486. }
  487. if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY | O_RDONLY)) == -1)
  488. {
  489. error (1, errno, "%s", optarg);
  490. }
  491. if (lightning_pib_file (& plc.PIB))
  492. {
  493. error (1, errno, "Bad lightning parameter file: %s", plc.PIB.name);
  494. }
  495. _setbits (plc.flags, PLC_WRITE_PIB);
  496. break;
  497. case 'p':
  498. if (! checkfilename (optarg))
  499. {
  500. error (1, EINVAL, "%s", optarg);
  501. }
  502. if ((plc.pib.file = open (plc.pib.name = optarg, O_BINARY | O_CREAT | O_RDWR | O_TRUNC, FILE_FILEMODE)) == -1)
  503. {
  504. error (1, errno, "%s", optarg);
  505. }
  506. #ifndef WIN32
  507. chown (optarg, getuid (), getgid ());
  508. #endif
  509. _setbits (plc.flags, PLC_READ_PIB);
  510. break;
  511. case 'Q':
  512. _setbits (plc.flags, PLC_QUICK_FLASH);
  513. break;
  514. case 'q':
  515. _setbits (channel.flags, CHANNEL_SILENCE);
  516. _setbits (plc.flags, PLC_SILENCE);
  517. break;
  518. case 'R':
  519. _setbits (plc.flags, PLC_RESET_DEVICE);
  520. break;
  521. case 'r':
  522. _setbits (plc.flags, PLC_VERSION);
  523. break;
  524. case 's':
  525. _setbits (plc.flags, PLC_SDRAM_INFO);
  526. break;
  527. case 't':
  528. channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX));
  529. break;
  530. case 'T':
  531. _setbits (plc.flags, PLC_FACTORY_DEFAULTS);
  532. break;
  533. case 'v':
  534. _setbits (channel.flags, CHANNEL_VERBOSE);
  535. _setbits (plc.flags, PLC_VERBOSE);
  536. break;
  537. case 'w':
  538. wait = (unsigned) (uintspec (optarg, 0, 3600));
  539. break;
  540. case 'x':
  541. _setbits (plc.flags, PLC_BAILOUT);
  542. break;
  543. case '1':
  544. _setbits (plc.flag2, PLC_FLAG2_CFM_ACT);
  545. break;
  546. case '2':
  547. _setbits (plc.flag2, PLC_FLAG2_BT_SRC);
  548. break;
  549. default:
  550. break;
  551. }
  552. }
  553. argc -= optind;
  554. argv += optind;
  555. if (argc != 1)
  556. {
  557. if (plc.nvm.file != -1)
  558. {
  559. error (1, ECANCELED, PLC_NODEVICE);
  560. }
  561. if (plc.pib.file != -1)
  562. {
  563. error (1, ECANCELED, PLC_NODEVICE);
  564. }
  565. if (plc.rpt.file != -1)
  566. {
  567. error (1, ECANCELED, PLC_NODEVICE);
  568. }
  569. }
  570. openchannel (& channel);
  571. if (! (plc.message = malloc (sizeof (* plc.message))))
  572. {
  573. error (1, errno, PLC_NOMEMORY);
  574. }
  575. if (! argc)
  576. {
  577. manager (& plc, loop, wait);
  578. }
  579. while ((argc) && (* argv))
  580. {
  581. if (! hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  582. {
  583. error (1, errno, PLC_BAD_MAC, * argv);
  584. }
  585. manager (& plc, loop, wait);
  586. argc--;
  587. argv++;
  588. }
  589. free (plc.message);
  590. closechannel (& channel);
  591. exit (0);
  592. }