int6kmod.c 11 KB

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