int6kf.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. * int6kf.c - Atheros Powerline Device Flash Utility;
  44. *
  45. * this program sends and receives raw ethernet frames and so needs
  46. * root priviledges; if you install it using "chmod 555" and "chown
  47. * root:root" then you must login as root to run it; otherwise, you
  48. * can install it using "chmod 4555" and "chown root:root" so that
  49. * anyone can run it; the program will refuse to run until you get
  50. * things right;
  51. *
  52. * Contributor(s):
  53. * Charles Maier
  54. *
  55. *--------------------------------------------------------------------*/
  56. /*====================================================================*"
  57. * system header files;
  58. *--------------------------------------------------------------------*/
  59. #include <unistd.h>
  60. #include <stdlib.h>
  61. #include <limits.h>
  62. #include <string.h>
  63. #include <ctype.h>
  64. /*====================================================================*
  65. * custom header files;
  66. *--------------------------------------------------------------------*/
  67. #include "../tools/getoptv.h"
  68. #include "../tools/putoptv.h"
  69. #include "../tools/memory.h"
  70. #include "../tools/number.h"
  71. #include "../tools/types.h"
  72. #include "../tools/flags.h"
  73. #include "../tools/files.h"
  74. #include "../tools/error.h"
  75. #include "../ram/nvram.h"
  76. #include "../ram/sdram.h"
  77. #include "../nvm/nvm.h"
  78. #include "../plc/plc.h"
  79. #include "../pib/pib.h"
  80. /*====================================================================*
  81. * custom source files;
  82. *--------------------------------------------------------------------*/
  83. #ifndef MAKEFILE
  84. #include "../plc/chipset.c"
  85. #include "../plc/Confirm.c"
  86. #include "../plc/Devices.c"
  87. #include "../plc/Display.c"
  88. #include "../plc/Failure.c"
  89. #include "../plc/FlashNVM.c"
  90. #include "../plc/ReadMME.c"
  91. #include "../plc/Request.c"
  92. #include "../plc/SendMME.c"
  93. #include "../plc/StartDevice1.c"
  94. #include "../plc/FlashDevice1.c"
  95. #include "../plc/UpgradeDevice1.c"
  96. #include "../plc/StartFirmware1.c"
  97. #include "../plc/WriteFirmware1.c"
  98. #include "../plc/WriteCFG.c"
  99. #include "../plc/WriteMEM.c"
  100. #include "../plc/WriteNVM.c"
  101. #include "../plc/WritePIB.c"
  102. #include "../plc/WaitForReset.c"
  103. #include "../plc/WaitForStart.c"
  104. #include "../plc/WaitForRestart.c"
  105. #endif
  106. #ifndef MAKEFILE
  107. #include "../tools/getoptv.c"
  108. #include "../tools/putoptv.c"
  109. #include "../tools/version.c"
  110. #include "../tools/uintspec.c"
  111. #include "../tools/checkfilename.c"
  112. #include "../tools/hexdecode.c"
  113. #include "../tools/hexstring.c"
  114. #include "../tools/todigit.c"
  115. #include "../tools/hexdump.c"
  116. #include "../tools/checksum32.c"
  117. #include "../tools/fdchecksum32.c"
  118. #include "../tools/typename.c"
  119. #include "../tools/strfbits.c"
  120. #include "../tools/error.c"
  121. #endif
  122. #ifndef MAKEFILE
  123. #include "../ram/sdramfile.c"
  124. #include "../ram/sdrampeek.c"
  125. #endif
  126. #ifndef MAKEFILE
  127. #include "../nvm/nvm.c"
  128. #include "../nvm/nvmfile1.c"
  129. #include "../nvm/nvmpeek1.c"
  130. #endif
  131. #ifndef MAKEFILE
  132. #include "../pib/pibfile1.c"
  133. #include "../pib/pibpeek1.c"
  134. #endif
  135. #ifndef MAKEFILE
  136. #include "../ether/openchannel.c"
  137. #include "../ether/closechannel.c"
  138. #include "../ether/readpacket.c"
  139. #include "../ether/sendpacket.c"
  140. #include "../ether/channel.c"
  141. #endif
  142. #ifndef MAKEFILE
  143. #include "../mme/EthernetHeader.c"
  144. #include "../mme/HomePlugHeader.c"
  145. #include "../mme/QualcommHeader.c"
  146. #include "../mme/UnwantedMessage.c"
  147. #include "../mme/MMECode.c"
  148. #endif
  149. #ifndef MAKEFILE
  150. #include "../key/keys.c"
  151. #endif
  152. /*====================================================================*
  153. *
  154. * int main (int argc, const char * argv[]);
  155. *
  156. * parse command line, populate int6k structure and perform selected
  157. * operations; show help summary when asked; see getoptv and putoptv
  158. * to understand command line parsing and help summary display; see
  159. * int6k.h for the definition of struct int6k;
  160. *
  161. * the default interface is eth1 because most people use eth0 as
  162. * their principle network connection; you can specify another
  163. * interface with -i or define environment string PLC to make
  164. * that the default interface and save typing;
  165. *
  166. *--------------------------------------------------------------------*/
  167. int main (int argc, const char * argv [])
  168. {
  169. extern struct channel channel;
  170. static const char *optv [] =
  171. {
  172. "C:i:eFN:p:P:qt:vx",
  173. "-C file -P file -N file",
  174. "Atheros Powerline Device Flash Utility for INT6300",
  175. "C f\twrite CFG file to device using VS_SET_SDRAM",
  176. "e\tredirect stderr messages to stdout",
  177. #if defined (WINPCAP) || defined (LIBPCAP)
  178. "i n\thost interface number [2]",
  179. #else
  180. "i s\thost interface name [" CHANNEL_ETHDEVICE "]",
  181. #endif
  182. "F[F]\tflash [force] NVRAM after firmware start using VS_MOD_NVM",
  183. "N f\twrite NVM file to device using VS_WR_MEM",
  184. "P f\twrite PIB file to device using VS_WR_MEM",
  185. "q\tquiet mode",
  186. #if defined (WINPCAP) || defined (LIBPCAP)
  187. "t n\tread capture time is (n) milliseconds [50]",
  188. #else
  189. "t n\tread timeout is (n) milliseconds [50]",
  190. #endif
  191. "v\tverbose mode",
  192. "x\texit on error",
  193. (const char *) (0)
  194. };
  195. #include "../plc/plc.c"
  196. char firmware [PLC_VERSION_STRING];
  197. signed c;
  198. if (getenv (PLCDEVICE))
  199. {
  200. #if defined (WINPCAP) || defined (LIBPCAP)
  201. channel.ifindex = atoi (getenv (PLCDEVICE));
  202. #else
  203. channel.ifname = strdup (getenv (PLCDEVICE));
  204. #endif
  205. }
  206. optind = 1;
  207. opterr = 1;
  208. while ((c = getoptv (argc, argv, optv)) != -1)
  209. {
  210. switch ((char) (c))
  211. {
  212. case 'C':
  213. if (!checkfilename (optarg))
  214. {
  215. error (1, EINVAL, "%s", optarg);
  216. }
  217. if ((plc.CFG.file = open (optarg, O_BINARY|O_RDONLY)) == -1)
  218. {
  219. error (1, errno, "%s", optarg);
  220. }
  221. if (sdramfile (plc.CFG.file, optarg, plc.flags))
  222. {
  223. error (1, ECANCELED, "CFG file %s is corrupt", optarg);
  224. }
  225. _setbits (plc.flags, PLC_SDRAM_CONFIG);
  226. plc.CFG.name = optarg;
  227. break;
  228. case 'e':
  229. dup2 (STDOUT_FILENO, STDERR_FILENO);
  230. break;
  231. case 'i':
  232. #if defined (WINPCAP) || defined (LIBPCAP)
  233. channel.ifindex = atoi (optarg);
  234. #else
  235. channel.ifname = optarg;
  236. #endif
  237. break;
  238. case 'F':
  239. _setbits (plc.module, PLC_MODULE_NVM_PIB);
  240. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  241. {
  242. _setbits (plc.module, VS_MODULE_FORCE);
  243. }
  244. _setbits (plc.flags, PLC_FLASH_DEVICE);
  245. break;
  246. case 'N':
  247. if (!checkfilename (optarg))
  248. {
  249. error (1, EINVAL, "%s", optarg);
  250. }
  251. if ((plc.NVM.file = open (optarg, O_BINARY|O_RDONLY)) == -1)
  252. {
  253. error (1, errno, "%s", optarg);
  254. }
  255. plc.NVM.name = optarg;
  256. if (nvmfile1 (&plc.NVM))
  257. {
  258. error (1, errno, "Bad firmware file: %s", plc.NVM.name);
  259. }
  260. _setbits (plc.flags, PLC_WRITE_MAC);
  261. break;
  262. case 'P':
  263. if (!checkfilename (optarg))
  264. {
  265. error (1, EINVAL, "%s", optarg);
  266. }
  267. if ((plc.PIB.file = open (optarg, O_BINARY|O_RDONLY)) == -1)
  268. {
  269. error (1, errno, "%s", optarg);
  270. }
  271. plc.PIB.name = optarg;
  272. if (pibfile1 (&plc.PIB))
  273. {
  274. error (1, errno, "Bad parameter file: %s", plc.PIB.name);
  275. }
  276. _setbits (plc.flags, PLC_WRITE_PIB);
  277. break;
  278. case 'q':
  279. _setbits (channel.flags, CHANNEL_SILENCE);
  280. _setbits (plc.flags, PLC_SILENCE);
  281. break;
  282. case 't':
  283. channel.timeout = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  284. break;
  285. case 'v':
  286. _setbits (channel.flags, CHANNEL_VERBOSE);
  287. _setbits (plc.flags, PLC_VERBOSE);
  288. break;
  289. case 'x':
  290. _setbits (plc.flags, PLC_BAILOUT);
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. argc -= optind;
  297. argv += optind;
  298. if (argc)
  299. {
  300. error (1, ECANCELED, "Too many arguments");
  301. }
  302. if (plc.CFG.file == -1)
  303. {
  304. error (1, ECANCELED, "No CFG file specified");
  305. }
  306. if (plc.PIB.file == -1)
  307. {
  308. error (1, ECANCELED, "No PIB file specified");
  309. }
  310. if (plc.NVM.file == -1)
  311. {
  312. error (1, ECANCELED, "No NVM file specified");
  313. }
  314. openchannel (&channel);
  315. if (!(plc.message = malloc (sizeof (struct message))))
  316. {
  317. error (1, errno, PLC_NOMEMORY);
  318. }
  319. if (WaitForStart (&plc, firmware, sizeof (firmware)))
  320. {
  321. Failure (&plc, "Device must be connected");
  322. return (-1);
  323. }
  324. if (plc.hardwareID > CHIPSET_INT6300)
  325. {
  326. Failure (&plc, "Device must be %s or earlier; try using int6kboot.", chipsetname (CHIPSET_INT6300));
  327. return (-1);
  328. }
  329. if (strcmp (firmware, "BootLoader"))
  330. {
  331. Failure (&plc, "Bootloader must be running");
  332. return (-1);
  333. }
  334. if (!StartDevice1 (&plc))
  335. {
  336. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  337. {
  338. UpgradeDevice1 (&plc);
  339. }
  340. }
  341. free (plc.message);
  342. closechannel (&channel);
  343. exit (0);
  344. }