int6khost.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. * int6khost.c - Atheros Powerline Device Host Emulator
  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/types.h"
  65. #include "../tools/flags.h"
  66. #include "../tools/files.h"
  67. #include "../tools/error.h"
  68. #include "../plc/plc.h"
  69. #include "../ram/nvram.h"
  70. #include "../ram/sdram.h"
  71. #include "../nvm/nvm.h"
  72. #include "../pib/pib.h"
  73. /*====================================================================*
  74. * custom source files;
  75. *--------------------------------------------------------------------*/
  76. #ifndef MAKEFILE
  77. #include "../plc/chipset.c"
  78. #include "../plc/Confirm.c"
  79. #include "../plc/Devices.c"
  80. #include "../plc/Display.c"
  81. #include "../plc/EmulateHost.c"
  82. #include "../plc/Failure.c"
  83. #include "../plc/FactoryDefaults.c"
  84. #include "../plc/FlashNVM.c"
  85. #include "../plc/HostActionResponse.c"
  86. #include "../plc/ModuleRead.c"
  87. #include "../plc/Request.c"
  88. #include "../plc/ResetDevice.c"
  89. #include "../plc/ReadMME.c"
  90. #include "../plc/ReadFirmware1.c"
  91. #include "../plc/ReadParameters1.c"
  92. #include "../plc/SendMME.c"
  93. #include "../plc/StartFirmware1.c"
  94. #include "../plc/WriteFirmware1.c"
  95. #include "../plc/WaitForReset.c"
  96. #include "../plc/WaitForStart.c"
  97. #include "../plc/WriteMEM.c"
  98. #include "../plc/WriteNVM.c"
  99. #include "../plc/WritePIB.c"
  100. #include "../plc/WriteCFG.c"
  101. #endif
  102. #ifndef MAKEFILE
  103. #include "../tools/getoptv.c"
  104. #include "../tools/putoptv.c"
  105. #include "../tools/version.c"
  106. #include "../tools/uintspec.c"
  107. #include "../tools/checkfilename.c"
  108. #include "../tools/hexdecode.c"
  109. #include "../tools/hexstring.c"
  110. #include "../tools/todigit.c"
  111. #include "../tools/hexdump.c"
  112. #include "../tools/checksum32.c"
  113. #include "../tools/fdchecksum32.c"
  114. #include "../tools/error.c"
  115. #include "../tools/strfbits.c"
  116. #include "../tools/typename.c"
  117. #endif
  118. #ifndef MAKEFILE
  119. #include "../ether/openchannel.c"
  120. #include "../ether/closechannel.c"
  121. #include "../ether/readpacket.c"
  122. #include "../ether/sendpacket.c"
  123. #include "../ether/channel.c"
  124. #endif
  125. #ifndef MAKEFILE
  126. #include "../mme/MMECode.c"
  127. #include "../mme/EthernetHeader.c"
  128. #include "../mme/QualcommHeader.c"
  129. #include "../mme/UnwantedMessage.c"
  130. #endif
  131. #ifndef MAKEFILE
  132. #include "../key/keys.c"
  133. #endif
  134. #ifndef MAKEFILE
  135. #include "../ram/sdramfile.c"
  136. #include "../ram/sdrampeek.c"
  137. #endif
  138. #ifndef MAKEFILE
  139. #include "../nvm/nvmfile1.c"
  140. #endif
  141. #ifndef MAKEFILE
  142. #include "../pib/pibfile1.c"
  143. #include "../pib/pibpeek1.c"
  144. #endif
  145. /*====================================================================*
  146. *
  147. * int main (int argc, char const * argv[]);
  148. *
  149. * parse command line, populate plc structure and perform selected
  150. * operations; show help summary when asked; see getoptv and putoptv
  151. * to understand command line parsing and help summary display; see
  152. * plc.h for the definition of struct plc;
  153. *
  154. * the default interface is eth1 because most people use eth0 as
  155. * their principle network connection; you can specify another
  156. * interface with -i or define environment string PLC to make
  157. * that the default interface and save typing;
  158. *
  159. *
  160. *--------------------------------------------------------------------*/
  161. int main (int argc, char const * argv [])
  162. {
  163. extern struct channel channel;
  164. static char const * optv [] =
  165. {
  166. "C:eFi:n:N:p:P:qt:vx",
  167. "-N file -P file [-n file] [-p file]",
  168. "Qualcomm Atheros Powerline Device Host Emulator for INT6300",
  169. "C f\twrite NVM file to device using VS_SET_SDRAM",
  170. "e\tredirect stderr to stdout",
  171. "F[F]\tflash (force) NVRAM using VS_MOD_NVM",
  172. #if defined (WINPCAP) || defined (LIBPCAP)
  173. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  174. #else
  175. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  176. #endif
  177. "n f\tread NVM from device into file using VS_RD_MOD",
  178. "N f\twrite NVM file to device using VS_WR_MEM",
  179. "p f\tread PIB from device into file using VS_RD_MOD",
  180. "P f\twrite PIB file to device using VS_WR_MEM",
  181. "q\tquiet mode",
  182. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  183. "v\tverbose mode",
  184. "x\texit on error",
  185. (char const *) (0)
  186. };
  187. #include "../plc/plc.c"
  188. signed c;
  189. if (getenv (PLCDEVICE))
  190. {
  191. #if defined (WINPCAP) || defined (LIBPCAP)
  192. channel.ifindex = atoi (getenv (PLCDEVICE));
  193. #else
  194. channel.ifname = strdup (getenv (PLCDEVICE));
  195. #endif
  196. }
  197. optind = 1;
  198. while ((c = getoptv (argc, argv, optv)) != -1)
  199. {
  200. switch (c)
  201. {
  202. case 'C':
  203. if (!checkfilename (optarg))
  204. {
  205. error (1, EINVAL, "%s", optarg);
  206. }
  207. if ((plc.CFG.file = open (plc.CFG.name = optarg, O_BINARY|O_RDONLY)) == -1)
  208. {
  209. error (1, errno, "%s", plc.CFG.name);
  210. }
  211. if (sdramfile (plc.CFG.file, optarg, plc.flags))
  212. {
  213. error (1, ECANCELED, "CFG file %s is corrupt", optarg);
  214. }
  215. _setbits (plc.flags, PLC_SDRAM_CONFIG);
  216. break;
  217. case 'e':
  218. dup2 (STDOUT_FILENO, STDERR_FILENO);
  219. break;
  220. case 'F':
  221. _setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
  222. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  223. {
  224. _setbits (plc.module, VS_MODULE_FORCE);
  225. }
  226. _setbits (plc.flags, PLC_FLASH_DEVICE);
  227. break;
  228. case 'i':
  229. #if defined (WINPCAP) || defined (LIBPCAP)
  230. channel.ifindex = atoi (optarg);
  231. #else
  232. channel.ifname = optarg;
  233. #endif
  234. break;
  235. case 'N':
  236. if (!checkfilename (optarg))
  237. {
  238. error (1, EINVAL, "%s", optarg);
  239. }
  240. if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1)
  241. {
  242. error (1, errno, "%s", plc.NVM.name);
  243. }
  244. if (nvmfile1 (&plc.NVM))
  245. {
  246. error (1, errno, "Bad firmware file: %s", plc.NVM.name);
  247. }
  248. _setbits (plc.flags, PLC_WRITE_MAC);
  249. break;
  250. case 'n':
  251. if (!checkfilename (optarg))
  252. {
  253. error (1, EINVAL, "%s", optarg);
  254. }
  255. if ((plc.nvm.file = open (plc.nvm.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  256. {
  257. error (1, errno, "%s", plc.nvm.name);
  258. }
  259. break;
  260. case 'P':
  261. if (!checkfilename (optarg))
  262. {
  263. error (1, EINVAL, "%s", optarg);
  264. }
  265. if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1)
  266. {
  267. error (1, errno, "%s", plc.PIB.name);
  268. }
  269. if (pibfile1 (&plc.PIB))
  270. {
  271. error (1, errno, "Bad parameter file: %s", plc.PIB.name);
  272. }
  273. _setbits (plc.flags, PLC_WRITE_PIB);
  274. break;
  275. case 'p':
  276. if (!checkfilename (optarg))
  277. {
  278. error (1, EINVAL, "%s", optarg);
  279. }
  280. if ((plc.pib.file = open (plc.pib.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  281. {
  282. error (1, errno, "%s", plc.pib.name);
  283. }
  284. break;
  285. case 'q':
  286. _setbits (channel.flags, CHANNEL_SILENCE);
  287. _setbits (plc.flags, PLC_SILENCE);
  288. break;
  289. case 't':
  290. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  291. break;
  292. case 'v':
  293. _setbits (channel.flags, CHANNEL_VERBOSE);
  294. _setbits (plc.flags, PLC_VERBOSE);
  295. break;
  296. case 'x':
  297. _setbits (plc.flags, PLC_BAILOUT);
  298. break;
  299. default:
  300. break;
  301. }
  302. }
  303. argc -= optind;
  304. argv += optind;
  305. /*
  306. * cancel operation if the user omitted a file or entered to extra argments
  307. * on the command line;
  308. */
  309. if (argc)
  310. {
  311. error (1, ENOTSUP, ERROR_TOOMANY);
  312. }
  313. if (plc.CFG.file == -1)
  314. {
  315. error (1, ECANCELED, "No host CFG file named");
  316. }
  317. if (plc.NVM.file == -1)
  318. {
  319. error (1, ECANCELED, "No host NVM file named");
  320. }
  321. if (plc.PIB.file == -1)
  322. {
  323. error (1, ECANCELED, "No host PIB file named");
  324. }
  325. if (plc.nvm.file == -1)
  326. {
  327. error (1, ECANCELED, "No user NVM file named");
  328. }
  329. if (plc.pib.file == -1)
  330. {
  331. error (1, ECANCELED, "No user PIB file named");
  332. }
  333. openchannel (&channel);
  334. if (!(plc.message = malloc (sizeof (* plc.message))))
  335. {
  336. error (1, errno, PLC_NOMEMORY);
  337. }
  338. EmulateHost (&plc);
  339. free (plc.message);
  340. closechannel (&channel);
  341. exit (0);
  342. }