plcotst.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. * plcotst.c - Atheros PLC One-Time Self-Test Manager;
  44. *
  45. * Use VS_SELFTEST_ONETIME_CONFIG and VS_SELFTEST_RESULTS message
  46. * type to configure the Atheros PLC One-Time Self-Test then read
  47. * and display stored selftest results;
  48. *
  49. * Contributor(s):
  50. * Charles Maier
  51. *
  52. *--------------------------------------------------------------------*/
  53. /*====================================================================*"
  54. * system header files;
  55. *--------------------------------------------------------------------*/
  56. #include <unistd.h>
  57. #include <stdlib.h>
  58. #include <stdint.h>
  59. #include <limits.h>
  60. /*====================================================================*
  61. * custom header files;
  62. *--------------------------------------------------------------------*/
  63. #include "../tools/getoptv.h"
  64. #include "../tools/putoptv.h"
  65. #include "../tools/memory.h"
  66. #include "../tools/number.h"
  67. #include "../tools/symbol.h"
  68. #include "../tools/types.h"
  69. #include "../tools/flags.h"
  70. #include "../tools/files.h"
  71. #include "../tools/error.h"
  72. #include "../plc/plc.h"
  73. /*====================================================================*
  74. * custom source files;
  75. *--------------------------------------------------------------------*/
  76. #ifndef MAKEFILE
  77. #include "../plc/Confirm.c"
  78. #include "../plc/Display.c"
  79. #include "../plc/Failure.c"
  80. #include "../plc/Request.c"
  81. #include "../plc/ReadMME.c"
  82. #include "../plc/SendMME.c"
  83. #include "../mme/UnwantedMessage.c"
  84. #include "../plc/Devices.c"
  85. #endif
  86. #ifndef MAKEFILE
  87. #include "../tools/getoptv.c"
  88. #include "../tools/putoptv.c"
  89. #include "../tools/version.c"
  90. #include "../tools/uintspec.c"
  91. #include "../tools/hexdump.c"
  92. #include "../tools/hexencode.c"
  93. #include "../tools/hexdecode.c"
  94. #include "../tools/todigit.c"
  95. #include "../tools/synonym.c"
  96. #include "../tools/error.c"
  97. #endif
  98. #ifndef MAKEFILE
  99. #include "../ether/openchannel.c"
  100. #include "../ether/closechannel.c"
  101. #include "../ether/readpacket.c"
  102. #include "../ether/sendpacket.c"
  103. #include "../ether/channel.c"
  104. #endif
  105. #ifndef MAKEFILE
  106. #include "../mme/MMECode.c"
  107. #include "../mme/EthernetHeader.c"
  108. #include "../mme/QualcommHeader.c"
  109. #endif
  110. /*====================================================================*
  111. * program constants;
  112. *--------------------------------------------------------------------*/
  113. #define PLCOTST_MVERSION 0
  114. #define PLCOTST_RUNAFTERRESET 1
  115. #define PLCOTST_DELAYTORUN 0
  116. #define PLCOTST_MEMRUNS 0
  117. #define PLCOTST_FLASHRUNS 0
  118. #define PLCOTST_RESETONDONE 0
  119. /*====================================================================*
  120. * program variables;
  121. *--------------------------------------------------------------------*/
  122. #ifndef __GNUC__
  123. #pragma pack (push,1)
  124. #endif
  125. typedef struct __packed selftest
  126. {
  127. uint8_t MVERSION;
  128. uint8_t RUNAFTERRESET;
  129. uint32_t DELAYTORUN;
  130. uint32_t MEMRUNS;
  131. uint32_t FLASHRUNS;
  132. uint8_t RESETONDONE;
  133. }
  134. selftest;
  135. #ifndef __GNUC__
  136. #pragma pack (pop)
  137. #endif
  138. /*====================================================================*
  139. *
  140. * signed configure (struct plc * plc, struct selftest * selftest);
  141. *
  142. *
  143. *--------------------------------------------------------------------*/
  144. static signed configure (struct plc * plc, struct selftest * selftest)
  145. {
  146. struct channel * channel = (struct channel *)(plc->channel);
  147. struct message * message = (struct message *)(plc->message);
  148. #ifndef __GNUC__
  149. #pragma pack (push,1)
  150. #endif
  151. struct __packed vs_selftest_onetime_config_request
  152. {
  153. struct ethernet_hdr ethernet;
  154. struct qualcomm_hdr qualcomm;
  155. uint8_t MVERSION;
  156. uint8_t RUNAFTERRESET;
  157. uint32_t DELAYTORUN;
  158. uint32_t MEMRUNS;
  159. uint32_t FLASHRUNS;
  160. uint8_t RESETONDONE;
  161. }
  162. * request = (struct vs_selftest_onetime_config_request *) (message);
  163. struct __packed vs_selftest_onetime_config_confirm
  164. {
  165. struct ethernet_hdr ethernet;
  166. struct qualcomm_hdr qualcomm;
  167. uint8_t MSTATUS;
  168. }
  169. * confirm = (struct vs_selftest_onetime_config_confirm *) (message);
  170. #ifndef __GNUC__
  171. #pragma pack (pop)
  172. #endif
  173. Request (plc, "Configure One-Time Self-Test");
  174. memset (message, 0, sizeof (* message));
  175. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  176. QualcommHeader (&request->qualcomm, 0, (VS_SELFTEST_ONETIME_CONFIG | MMTYPE_REQ));
  177. request->MVERSION = selftest->MVERSION;
  178. request->RUNAFTERRESET = selftest->RUNAFTERRESET;
  179. request->DELAYTORUN = HTOLE32 (selftest->DELAYTORUN);
  180. request->MEMRUNS = HTOLE32 (selftest->MEMRUNS);
  181. request->FLASHRUNS = HTOLE32 (selftest->FLASHRUNS);
  182. request->RESETONDONE = selftest->RESETONDONE;
  183. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  184. if (SendMME (plc) <= 0)
  185. {
  186. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  187. return (-1);
  188. }
  189. while (ReadMME (plc, 0, (VS_SELFTEST_ONETIME_CONFIG | MMTYPE_CNF)) > 0)
  190. {
  191. if (confirm->MSTATUS)
  192. {
  193. Failure (plc, PLC_WONTDOIT);
  194. continue;
  195. }
  196. Display (plc, "%s", "Configured");
  197. }
  198. return (0);
  199. }
  200. /*====================================================================*
  201. *
  202. * signed retrieve (struct plc * plc, struct selftest * selftest);
  203. *
  204. *
  205. *--------------------------------------------------------------------*/
  206. static signed retrieve (struct plc * plc, struct selftest * selftest)
  207. {
  208. struct channel * channel = (struct channel *)(plc->channel);
  209. struct message * message = (struct message *)(plc->message);
  210. #ifndef __GNUC__
  211. #pragma pack (push,1)
  212. #endif
  213. struct __packed vs_selftest_results_request
  214. {
  215. struct ethernet_hdr ethernet;
  216. struct qualcomm_hdr qualcomm;
  217. uint8_t MVERSION;
  218. uint8_t MACTION;
  219. }
  220. * request = (struct vs_selftest_results_request *) (message);
  221. struct __packed vs_selftest_results_confirm
  222. {
  223. struct ethernet_hdr ethernet;
  224. struct qualcomm_hdr qualcomm;
  225. uint8_t MVERSION;
  226. uint8_t MSTATUS;
  227. uint32_t NUMBER [6];
  228. }
  229. * confirm = (struct vs_selftest_results_confirm *) (message);
  230. #ifndef __GNUC__
  231. #pragma pack (pop)
  232. #endif
  233. Request (plc, "Retrieve Self-Test Results");
  234. memset (message, 0, sizeof (* message));
  235. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  236. QualcommHeader (&request->qualcomm, 0, (VS_SELFTEST_RESULTS | MMTYPE_REQ));
  237. request->MVERSION = selftest->MVERSION;
  238. request->MACTION = plc->action;
  239. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  240. if (SendMME (plc) <= 0)
  241. {
  242. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  243. return (-1);
  244. }
  245. while (ReadMME (plc, 0, (VS_SELFTEST_RESULTS | MMTYPE_CNF)) > 0)
  246. {
  247. if ((confirm->MSTATUS == 2) || (confirm->MSTATUS == 4) || (confirm->MSTATUS == 6))
  248. {
  249. Display (plc, "Memory test %d Passed %d Failed %d Flash test %d Passed %d Failed %d", LE32TOH (confirm->NUMBER [0]), LE32TOH (confirm->NUMBER [1]), LE32TOH (confirm->NUMBER [2]), LE32TOH (confirm->NUMBER [3]), LE32TOH (confirm->NUMBER [4]), LE32TOH (confirm->NUMBER [5]));
  250. continue;
  251. }
  252. if (confirm->MSTATUS == 3)
  253. {
  254. Failure (plc, "%d seconds remaining", LE32TOH (confirm->NUMBER [0]));
  255. continue;
  256. }
  257. if (confirm->MSTATUS)
  258. {
  259. Failure (plc, PLC_WONTDOIT);
  260. continue;
  261. }
  262. Confirm (plc, "Cleared Results");
  263. }
  264. return (0);
  265. }
  266. /*====================================================================*
  267. *
  268. * signed manager (struct plc * plc, struct selftest * selftest);
  269. *
  270. *
  271. *--------------------------------------------------------------------*/
  272. static signed manager (struct plc * plc, struct selftest * selftest)
  273. {
  274. if (_anyset (plc->flags, PLC_CONFIGURE))
  275. {
  276. configure (plc, selftest);
  277. }
  278. if (_anyset (plc->flags, PLC_RESULTS))
  279. {
  280. retrieve (plc, selftest);
  281. }
  282. return (0);
  283. }
  284. /*====================================================================*
  285. *
  286. * int main (int argc, char const * argv[]);
  287. *
  288. *
  289. *--------------------------------------------------------------------*/
  290. int main (int argc, char const * argv [])
  291. {
  292. extern struct channel channel;
  293. static char const * optv [] =
  294. {
  295. "c:Cd:ef:i:m:qr:R:t:vx",
  296. "device [device] [...] [> stdout]",
  297. "Qualcomm Atheros Powerline One-Time Self-Test Manager",
  298. "c n\tconfiguration version",
  299. "C\tclear selftest results",
  300. "d n\tdelay selftest start by (n) seconds [" LITERAL (PLCOTST_DELAYTORUN) "]",
  301. "e\tredirect stderr to stdout",
  302. "f n\trun flash selftest (n) times [" LITERAL (PLCOTST_FLASHRUNS) "]",
  303. #if defined (WINPCAP) || defined (LIBPCAP)
  304. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  305. #else
  306. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  307. #endif
  308. "m n\trun memory selftest (n) times [" LITERAL (PLCOTST_MEMRUNS) "]",
  309. "q\tquiet mode",
  310. "r n\trun selftest after reset [" LITERAL (PLCOTST_RUNAFTERRESET) "]",
  311. "R n\treset after selftest completes [" LITERAL (PLCOTST_RESETONDONE) "]",
  312. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  313. "v\tverbose mode",
  314. "x\texit on error",
  315. (char const *) (0)
  316. };
  317. #include "../plc/plc.c"
  318. struct selftest selftest =
  319. {
  320. PLCOTST_MVERSION,
  321. PLCOTST_RUNAFTERRESET,
  322. PLCOTST_DELAYTORUN,
  323. PLCOTST_MEMRUNS,
  324. PLCOTST_FLASHRUNS,
  325. PLCOTST_RESETONDONE
  326. };
  327. signed c;
  328. if (getenv (PLCDEVICE))
  329. {
  330. channel.ifname = strdup (getenv (PLCDEVICE));
  331. }
  332. optind = 1;
  333. while ((c = getoptv (argc, argv, optv)) != -1)
  334. {
  335. switch (c)
  336. {
  337. case 'c':
  338. selftest.MVERSION = (uint8_t)(uintspec (optarg, 0, UCHAR_MAX));
  339. break;
  340. case 'C':
  341. _setbits (plc.flags, PLC_RESULTS);
  342. plc.action = 1;
  343. break;
  344. case 'd':
  345. _setbits (plc.flags, PLC_CONFIGURE);
  346. selftest.DELAYTORUN = (uint32_t)(uintspec (optarg, 0, UINT_MAX));
  347. break;
  348. case 'e':
  349. dup2 (STDOUT_FILENO, STDERR_FILENO);
  350. break;
  351. case 'f':
  352. _setbits (plc.flags, PLC_CONFIGURE);
  353. selftest.FLASHRUNS = (uint32_t)(uintspec (optarg, 0, UINT_MAX));
  354. break;
  355. case 'i':
  356. #if defined (WINPCAP) || defined (LIBPCAP)
  357. channel.ifindex = atoi (optarg);
  358. #else
  359. channel.ifname = optarg;
  360. #endif
  361. break;
  362. case 'm':
  363. _setbits (plc.flags, PLC_CONFIGURE);
  364. selftest.MEMRUNS = (uint32_t)(uintspec (optarg, 0, UINT_MAX));
  365. break;
  366. case 'q':
  367. _setbits (channel.flags, CHANNEL_SILENCE);
  368. _setbits (plc.flags, PLC_SILENCE);
  369. break;
  370. case 'r':
  371. _setbits (plc.flags, PLC_CONFIGURE);
  372. selftest.RUNAFTERRESET = (uint8_t)(uintspec (optarg, false, true));
  373. break;
  374. case 'R':
  375. _setbits (plc.flags, PLC_CONFIGURE);
  376. selftest.RESETONDONE = (uint8_t)(uintspec (optarg, false, true));
  377. break;
  378. case 't':
  379. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  380. break;
  381. case 'v':
  382. _setbits (channel.flags, CHANNEL_VERBOSE);
  383. _setbits (plc.flags, PLC_VERBOSE);
  384. break;
  385. case 'x':
  386. _setbits (plc.flags, PLC_BAILOUT);
  387. break;
  388. default:
  389. break;
  390. }
  391. }
  392. argc -= optind;
  393. argv += optind;
  394. if (_allclr (plc.flags, (PLC_CONFIGURE | PLC_RESULTS)))
  395. {
  396. _setbits (plc.flags, PLC_RESULTS);
  397. }
  398. openchannel (&channel);
  399. if (!(plc.message = malloc (sizeof (* plc.message))))
  400. {
  401. error (1, errno, PLC_NOMEMORY);
  402. }
  403. if (!argc)
  404. {
  405. manager (&plc, &selftest);
  406. }
  407. while ((argc) && (* argv))
  408. {
  409. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  410. {
  411. error (1, errno, PLC_BAD_MAC, * argv);
  412. }
  413. manager (&plc, &selftest);
  414. argv++;
  415. argc--;
  416. }
  417. free (plc.message);
  418. closechannel (&channel);
  419. return (0);
  420. }