int6kfp.c 9.0 KB

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