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