plctest.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. * plctest.c - Atheros Test Applet Loader;
  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. #include <errno.h>
  58. /*====================================================================*
  59. * custom header files;
  60. *--------------------------------------------------------------------*/
  61. #include "../tools/getoptv.h"
  62. #include "../tools/putoptv.h"
  63. #include "../tools/memory.h"
  64. #include "../tools/number.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 "../ram/nvram.h"
  71. #include "../ram/sdram.h"
  72. #include "../nvm/nvm.h"
  73. #include "../pib/pib.h"
  74. #include "../plc/plc.h"
  75. /*====================================================================*
  76. * custom source files;
  77. *--------------------------------------------------------------------*/
  78. #ifndef MAKEFILE
  79. #include "../plc/chipset.c"
  80. #include "../plc/Confirm.c"
  81. #include "../plc/Devices.c"
  82. #include "../plc/Display.c"
  83. #include "../plc/ExecuteApplets.c"
  84. #include "../plc/ExecuteApplets1.c"
  85. #include "../plc/ExecuteApplets2.c"
  86. #include "../plc/Failure.c"
  87. #include "../plc/NVMSelect.c"
  88. #include "../plc/Request.c"
  89. #include "../plc/SendMME.c"
  90. #include "../plc/StartFirmware1.c"
  91. #include "../plc/WaitForStart.c"
  92. #include "../plc/WriteExecuteApplet2.c"
  93. #include "../plc/WriteExecuteFirmware1.c"
  94. #include "../plc/WriteExecuteFirmware2.c"
  95. #include "../plc/WriteFirmware1.c"
  96. #include "../plc/WriteFirmware2.c"
  97. #endif
  98. #ifndef MAKEFILE
  99. #include "../tools/getoptv.c"
  100. #include "../tools/putoptv.c"
  101. #include "../tools/version.c"
  102. #include "../tools/uintspec.c"
  103. #include "../tools/checkfilename.c"
  104. #include "../tools/hexdecode.c"
  105. #include "../tools/todigit.c"
  106. #include "../tools/hexdump.c"
  107. #include "../tools/checksum32.c"
  108. #include "../tools/fdchecksum32.c"
  109. #include "../tools/error.c"
  110. #include "../tools/strfbits.c"
  111. #include "../tools/typename.c"
  112. #endif
  113. #ifndef MAKEFILE
  114. #include "../ether/openchannel.c"
  115. #include "../ether/closechannel.c"
  116. #include "../ether/readpacket.c"
  117. #include "../ether/sendpacket.c"
  118. #include "../ether/channel.c"
  119. #endif
  120. #ifndef MAKEFILE
  121. #include "../nvm/nvmfile.c"
  122. #include "../nvm/nvmfile1.c"
  123. #include "../nvm/nvmfile2.c"
  124. #endif
  125. #ifndef MAKEFILE
  126. #include "../mme/EthernetHeader.c"
  127. #include "../mme/QualcommHeader.c"
  128. #include "../mme/UnwantedMessage.c"
  129. #include "../mme/FirmwareMessage.c"
  130. #include "../mme/ARPCPrint.c"
  131. #include "../mme/MMECode.c"
  132. #endif
  133. /*====================================================================*
  134. *
  135. * signed ReadMME (struct plc * plc, uint8_t MMV, uint16_t MMTYPE);
  136. *
  137. * plc.h
  138. *
  139. * this version of ReadMME() calls FirmwareMessage() to intercept
  140. * VS_ARPC messages and print them on stdout in readable format;
  141. *
  142. *
  143. * Contributor(s):
  144. * Charles Maier
  145. * Matthieu Poullet
  146. *
  147. *--------------------------------------------------------------------*/
  148. signed ReadMME (struct plc * plc, uint8_t MMV, uint16_t MMTYPE)
  149. {
  150. struct channel * channel = (struct channel *)(plc->channel);
  151. struct message * message = (struct message *)(plc->message);
  152. while ((plc->packetsize = readpacket (channel, message, sizeof (* message))) > 0)
  153. {
  154. if (FirmwareMessage (message))
  155. {
  156. continue;
  157. }
  158. if (UnwantedMessage (message, plc->packetsize, MMV, MMTYPE))
  159. {
  160. continue;
  161. }
  162. break;
  163. }
  164. return (plc->packetsize);
  165. }
  166. /*====================================================================*
  167. *
  168. * void sequence (struct plc * plc, int argc, char const * argv);
  169. *
  170. * open each file on the command line and execute all applets in
  171. * each file; move on if errors occur;
  172. *
  173. *
  174. * Contributor(s):
  175. * Charles Maier
  176. *
  177. *--------------------------------------------------------------------*/
  178. static void sequence (struct plc * plc, int argc, char const * argv [])
  179. {
  180. while ((argc) && (* argv))
  181. {
  182. if ((plc->NVM.file = open (plc->NVM.name = * argv, O_BINARY|O_RDONLY)) == -1)
  183. {
  184. error (0, errno, FILE_CANTOPEN, plc->NVM.name);
  185. }
  186. else if (nvmfile (&plc->NVM))
  187. {
  188. error (0, errno, FILE_CANTLOAD, plc->NVM.name);
  189. }
  190. else
  191. {
  192. ExecuteApplets (plc);
  193. }
  194. close (plc->NVM.file);
  195. argc--;
  196. argv++;
  197. }
  198. return;
  199. }
  200. /*====================================================================*
  201. *
  202. * int main (int argc, char const * argv[]);
  203. *
  204. * parse command line, populate plc structure and perform selected
  205. * operations; show help summary when asked; see getoptv and putoptv
  206. * to understand command line parsing and help summary display; see
  207. * plc.h for the definition of struct plc;
  208. *
  209. * the default interface is eth1 because most people use eth0 as
  210. * their principle network connection; you can specify another
  211. * interface with -i or define environment string PLC to make
  212. * that the default interface and save typing;
  213. *
  214. *
  215. * Contributor(s):
  216. * Charles Maier
  217. *
  218. *--------------------------------------------------------------------*/
  219. int main (int argc, char const * argv [])
  220. {
  221. extern struct channel channel;
  222. char firmware [PLC_VERSION_STRING];
  223. static char const * optv [] =
  224. {
  225. "ei:lqt:vw:xX",
  226. "file [file] [...]",
  227. "Qualcomm Atheros PLC Test Applet Loader",
  228. "e\tredirect stderr to stdout",
  229. #if defined (WINPCAP) || defined (LIBPCAP)
  230. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  231. #else
  232. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  233. #endif
  234. "l\tloop until program is terminated",
  235. "q\tquiet mode",
  236. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  237. "v\tverbose mode",
  238. "w n\twait time is (n) seconds [" LITERAL (PLC_TIMER) "]",
  239. "x\texit on error",
  240. "X\texit on start",
  241. (char const *) (0)
  242. };
  243. #include "../plc/plc.c"
  244. signed loop = 0;
  245. signed c;
  246. if (getenv (PLCDEVICE))
  247. {
  248. #if defined (WINPCAP) || defined (LIBPCAP)
  249. channel.ifindex = atoi (getenv (PLCDEVICE));
  250. #else
  251. channel.ifname = strdup (getenv (PLCDEVICE));
  252. #endif
  253. }
  254. optind = 1;
  255. while ((c = getoptv (argc, argv, optv)) != -1)
  256. {
  257. switch (c)
  258. {
  259. case 'e':
  260. dup2 (STDOUT_FILENO, STDERR_FILENO);
  261. break;
  262. case 'i':
  263. #if defined (WINPCAP) || defined (LIBPCAP)
  264. channel.ifindex = atoi (optarg);
  265. #else
  266. channel.ifname = optarg;
  267. #endif
  268. break;
  269. case 'l':
  270. loop = 1;
  271. break;
  272. case 'q':
  273. _setbits (channel.flags, CHANNEL_SILENCE);
  274. _setbits (plc.flags, PLC_SILENCE);
  275. break;
  276. case 't':
  277. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  278. break;
  279. case 'v':
  280. _setbits (channel.flags, CHANNEL_VERBOSE);
  281. _setbits (plc.flags, PLC_VERBOSE);
  282. break;
  283. case 'w':
  284. plc.timer = (unsigned)(uintspec (optarg, 0, 3600));
  285. break;
  286. case 'x':
  287. _setbits (plc.flags, PLC_BAILOUT);
  288. break;
  289. case 'X':
  290. _setbits (plc.flags, PLC_QUICK_FLASH);
  291. break;
  292. }
  293. }
  294. argc -= optind;
  295. argv += optind;
  296. openchannel (&channel);
  297. if (!(plc.message = malloc (sizeof (* plc.message))))
  298. {
  299. error (1, errno, PLC_NOMEMORY);
  300. }
  301. if (WaitForStart (&plc, firmware, sizeof (firmware)))
  302. {
  303. error (1, ECANCELED, PLC_NODETECT);
  304. }
  305. if (strcmp (firmware, "BootLoader"))
  306. {
  307. error (1, ECANCELED, "BootLoader must be running");
  308. }
  309. do
  310. {
  311. sequence (&plc, argc, argv);
  312. }
  313. while (loop);
  314. free (plc.message);
  315. closechannel (&channel);
  316. return (plc.state);
  317. }