plcmod.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * plcmod.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 <unistd.h>
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. #include <limits.h>
  24. /*====================================================================*
  25. * custom header files;
  26. *--------------------------------------------------------------------*/
  27. #include "../tools/getoptv.h"
  28. #include "../tools/putoptv.h"
  29. #include "../tools/memory.h"
  30. #include "../tools/number.h"
  31. #include "../tools/symbol.h"
  32. #include "../tools/types.h"
  33. #include "../tools/flags.h"
  34. #include "../tools/files.h"
  35. #include "../tools/error.h"
  36. #include "../ether/channel.h"
  37. #include "../plc/plc.h"
  38. #include "../nda/nda.h"
  39. #include "../key/keys.h"
  40. /*====================================================================*
  41. * custom source files;
  42. *--------------------------------------------------------------------*/
  43. #ifndef MAKEFILE
  44. #include "../plc/Devices.c"
  45. #include "../plc/Confirm.c"
  46. #include "../plc/Display.c"
  47. #include "../plc/Request.c"
  48. #include "../plc/Failure.c"
  49. #include "../plc/ReadMME.c"
  50. #include "../plc/SendMME.c"
  51. #include "../plc/ModuleCommit.c"
  52. #include "../plc/ModuleRead.c"
  53. #include "../plc/ModuleDump.c"
  54. #include "../plc/ModuleSpec.c"
  55. #include "../plc/ModuleSession.c"
  56. #include "../plc/ModuleWrite.c"
  57. #endif
  58. #ifndef MAKEFILE
  59. #include "../tools/basespec.c"
  60. #include "../tools/getoptv.c"
  61. #include "../tools/putoptv.c"
  62. #include "../tools/version.c"
  63. #include "../tools/uintspec.c"
  64. #include "../tools/hexdump.c"
  65. #include "../tools/hexview.c"
  66. #include "../tools/hexencode.c"
  67. #include "../tools/hexdecode.c"
  68. #include "../tools/hexstring.c"
  69. #include "../tools/todigit.c"
  70. #include "../tools/checkfilename.c"
  71. #include "../tools/checksum32.c"
  72. #include "../tools/fdchecksum32.c"
  73. #include "../tools/strfbits.c"
  74. #include "../tools/synonym.c"
  75. #include "../tools/typename.c"
  76. #include "../tools/error.c"
  77. #endif
  78. #ifndef MAKEFILE
  79. #include "../ether/openchannel.c"
  80. #include "../ether/closechannel.c"
  81. #include "../ether/readpacket.c"
  82. #include "../ether/sendpacket.c"
  83. #include "../ether/channel.c"
  84. #endif
  85. #ifndef MAKEFILE
  86. #include "../mme/MMECode.c"
  87. #include "../mme/EthernetHeader.c"
  88. #include "../mme/QualcommHeader.c"
  89. #include "../mme/UnwantedMessage.c"
  90. #endif
  91. #ifndef MAKEFILE
  92. #include "../nda/AccessLevelPTS.c"
  93. #endif
  94. #ifndef MAKEFILE
  95. #include "../key/keys.c"
  96. #endif
  97. /*====================================================================*
  98. *
  99. *--------------------------------------------------------------------*/
  100. static struct _term_ const modules [] =
  101. {
  102. {
  103. "forward",
  104. "0x7000"
  105. },
  106. {
  107. "fw",
  108. "0x7000"
  109. },
  110. {
  111. "mdio",
  112. "0x1000"
  113. },
  114. {
  115. "parameters",
  116. "0x7002"
  117. },
  118. {
  119. "pib",
  120. "0x7002"
  121. },
  122. {
  123. "raw",
  124. "0x7004"
  125. },
  126. {
  127. "sl",
  128. "0x7003"
  129. },
  130. {
  131. "softloader",
  132. "0x7003"
  133. }
  134. };
  135. /*====================================================================*
  136. *
  137. * void Manager (struct plc * plc, uint16_t ModuleID, uint16_t ModuleSubID);
  138. *
  139. * read a custom modul from flash memory or write a suceom module
  140. * to flash memory using the VS_MODULE_OPERATION message; read and
  141. * save the old module before writing and commiting the new module;
  142. *
  143. *--------------------------------------------------------------------*/
  144. static void Manager (struct plc * plc, uint16_t ModuleID, uint16_t ModuleSubID)
  145. {
  146. if (_anyset (plc->flags, PLC_DUMP_MODULE))
  147. {
  148. if (AccessLevelPTS (plc) != -1)
  149. {
  150. ModuleDump (plc, PLC_MOD_OP_READ_FLASH, ModuleID, ModuleSubID);
  151. }
  152. }
  153. else if (_anyset (plc->flags, PLC_READ_MODULE))
  154. {
  155. if (AccessLevelPTS (plc) != -1)
  156. {
  157. ModuleRead (plc, & plc->nvm, PLC_MOD_OP_READ_FLASH, ModuleID, ModuleSubID);
  158. }
  159. }
  160. else if (_anyset (plc->flags, PLC_WRITE_MODULE))
  161. {
  162. struct vs_module_spec vs_module_spec;
  163. vs_module_spec.MODULE_ID = ModuleID;
  164. vs_module_spec.MODULE_SUB_ID = ModuleSubID;
  165. ModuleSpec (& plc->NVM, & vs_module_spec);
  166. ModuleSession (plc, 1, & vs_module_spec);
  167. ModuleWrite (plc, & plc->NVM, 0, & vs_module_spec);
  168. ModuleCommit (plc, (PLC_COMMIT_FORCE));
  169. return;
  170. }
  171. return;
  172. }
  173. /*====================================================================*
  174. *
  175. * int main (int argc, char const * argv[]);
  176. *
  177. * parse command line, populate plc structure and perform selected
  178. * operations; show help summary if asked; see getoptv and putoptv
  179. * to understand command line parsing and help summary display; see
  180. * plc.h for the definition of struct plc;
  181. *
  182. * the command line accepts multiple MAC addresses and the program
  183. * performs the specified operations on each address, in turn; the
  184. * address order is significant but the option order is not; the
  185. * default address is a local broadcast that causes all devices on
  186. * the local H1 interface to respond but not those at the remote
  187. * end of the powerline;
  188. *
  189. * the default address is 00:B0:52:00:00:01; omitting the address
  190. * will automatically address the local device; some options will
  191. * cancel themselves if this makes no sense;
  192. *
  193. * the default interface is eth1 because most people use eth0 as
  194. * their principle network connection; you can specify another
  195. * interface with -i or define environment string PLC to make
  196. * that the default interface and save typing;
  197. *
  198. *--------------------------------------------------------------------*/
  199. int main (int argc, char const * argv [])
  200. {
  201. extern struct channel channel;
  202. extern struct _term_ const daks [];
  203. static char const * optv [] =
  204. {
  205. "dD:ei:m:M:qs:S:t:vx",
  206. "device [device] [...]",
  207. "Qualcomm Atheros Powerline Device Module Manager",
  208. "d\tdump module in hexadecimal format",
  209. "D x\tDevice Access Key (DAK) is (x) [" DAK1 "]",
  210. "e\tredirect stderr to stdout",
  211. #if defined (WINPCAP) || defined (LIBPCAP)
  212. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  213. #else
  214. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  215. #endif
  216. "m f\tread module from NVRAM to file (f)",
  217. "M f\twrite module file (f) to flash memory",
  218. "s n\tmodule sub-ID [" LITERAL (PLC_SUBMODULEID) "]",
  219. "S n\tsession ID [" LITERAL (PLCSESSION) "]",
  220. "t n\tmodule ID [" LITERAL (PLC_MODULEID) "]",
  221. "q\tquiet mode",
  222. "v\tverbose mode",
  223. (char const *) (0)
  224. };
  225. #include "../plc/plc.c"
  226. struct vs_module_spec vs_module_spec;
  227. uint16_t ModuleID = 0;
  228. uint16_t ModuleSubID = 0;
  229. signed c;
  230. memset (& vs_module_spec, 0, sizeof (vs_module_spec));
  231. if (getenv (PLCDEVICE))
  232. {
  233. #if defined (WINPCAP) || defined (LIBPCAP)
  234. channel.ifindex = atoi (getenv (PLCDEVICE));
  235. #else
  236. channel.ifname = strdup (getenv (PLCDEVICE));
  237. #endif
  238. }
  239. optind = 1;
  240. while (~ (c = getoptv (argc, argv, optv)))
  241. {
  242. switch (c)
  243. {
  244. case 'e':
  245. dup2 (STDOUT_FILENO, STDERR_FILENO);
  246. break;
  247. case 'C':
  248. _setbits (plc.flags, PLC_COMMIT_MODULE);
  249. break;
  250. case 'd':
  251. if (_anyset (plc.flags, PLC_READ_MODULE))
  252. {
  253. error (1, EINVAL, "Options -d and -m are mutually exclusive");
  254. }
  255. _setbits (plc.flags, PLC_DUMP_MODULE);
  256. break;
  257. case 'D':
  258. if (! hexencode (plc.DAK, sizeof (plc.DAK), synonym (optarg, daks, SIZEOF (daks))))
  259. {
  260. error (1, errno, PLC_BAD_DAK, optarg);
  261. }
  262. break;
  263. case 'i':
  264. #if defined (WINPCAP) || defined (LIBPCAP)
  265. channel.ifindex = atoi (optarg);
  266. #else
  267. channel.ifname = optarg;
  268. #endif
  269. break;
  270. case 'M':
  271. _setbits (plc.flags, PLC_WRITE_MODULE);
  272. if (! checkfilename (optarg))
  273. {
  274. error (1, EINVAL, "%s", optarg);
  275. }
  276. if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY | O_RDONLY)) == -1)
  277. {
  278. error (1, errno, "%s", plc.NVM.name);
  279. }
  280. if (ModuleSpec (& plc.NVM, & vs_module_spec) == -1)
  281. {
  282. error (1, errno, "%s", optarg);
  283. }
  284. break;
  285. case 'm':
  286. if (_anyset (plc.flags, PLC_DUMP_MODULE))
  287. {
  288. error (1, EINVAL, "Options -d and -m are mutually exclusive");
  289. }
  290. _setbits (plc.flags, PLC_READ_MODULE);
  291. if (! checkfilename (optarg))
  292. {
  293. error (1, EINVAL, "%s", optarg);
  294. }
  295. if ((plc.nvm.file = open (plc.nvm.name = optarg, O_BINARY | O_CREAT | O_RDWR | O_TRUNC, FILE_FILEMODE)) == -1)
  296. {
  297. error (1, errno, "%s", plc.nvm.name);
  298. }
  299. #ifndef WIN32
  300. chown (optarg, getuid (), getgid ());
  301. #endif
  302. break;
  303. case 't':
  304. ModuleID = (uint16_t) (basespec (synonym (optarg, modules, SIZEOF (modules)), 16, sizeof (ModuleID)));
  305. break;
  306. case 's':
  307. ModuleSubID = (uint16_t) (basespec (optarg, 16, sizeof (ModuleSubID)));
  308. break;
  309. case 'S':
  310. plc.cookie = (uint32_t) (basespec (optarg, 16, sizeof (plc.cookie)));
  311. break;
  312. case 'q':
  313. _setbits (channel.flags, CHANNEL_SILENCE);
  314. _setbits (plc.flags, PLC_SILENCE);
  315. break;
  316. case 'v':
  317. _setbits (channel.flags, CHANNEL_VERBOSE);
  318. _setbits (plc.flags, PLC_VERBOSE);
  319. break;
  320. case 'x':
  321. _setbits (plc.flags, PLC_BAILOUT);
  322. break;
  323. default:
  324. break;
  325. }
  326. }
  327. argc -= optind;
  328. argv += optind;
  329. if (argc != 1)
  330. {
  331. if (plc.nvm.file != -1)
  332. {
  333. error (1, ECANCELED, PLC_NODEVICE);
  334. }
  335. }
  336. openchannel (& channel);
  337. if (! (plc.message = malloc (sizeof (* plc.message))))
  338. {
  339. error (1, errno, PLC_NOMEMORY);
  340. }
  341. if (! argc)
  342. {
  343. Manager (& plc, ModuleID, ModuleSubID);
  344. }
  345. while ((argc) && (* argv))
  346. {
  347. if (! hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  348. {
  349. error (1, errno, PLC_BAD_MAC, * argv);
  350. }
  351. Manager (& plc, ModuleID, ModuleSubID);
  352. argc--;
  353. argv++;
  354. }
  355. free (plc.message);
  356. closechannel (& channel);
  357. exit (0);
  358. }