int6ktest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. * int6ktest.c - Atheros Test Applet Loader;
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. * Matthieu Poullet
  49. *
  50. *--------------------------------------------------------------------*/
  51. /*====================================================================*"
  52. * system header files;
  53. *--------------------------------------------------------------------*/
  54. #include <unistd.h>
  55. #include <stdlib.h>
  56. #include <stdint.h>
  57. #include <limits.h>
  58. #include <errno.h>
  59. /*====================================================================*
  60. * custom header files;
  61. *--------------------------------------------------------------------*/
  62. #include "../tools/getoptv.h"
  63. #include "../tools/putoptv.h"
  64. #include "../tools/memory.h"
  65. #include "../tools/number.h"
  66. #include "../tools/types.h"
  67. #include "../tools/flags.h"
  68. #include "../tools/files.h"
  69. #include "../tools/error.h"
  70. #include "../ether/channel.h"
  71. #include "../ram/nvram.h"
  72. #include "../ram/sdram.h"
  73. #include "../nvm/nvm.h"
  74. #include "../pib/pib.h"
  75. #include "../plc/plc.h"
  76. /*====================================================================*
  77. * custom source files;
  78. *--------------------------------------------------------------------*/
  79. #ifndef MAKEFILE
  80. #include "../plc/chipset.c"
  81. #include "../plc/Confirm.c"
  82. #include "../plc/Devices.c"
  83. #include "../plc/Display.c"
  84. #include "../plc/Failure.c"
  85. #include "../plc/Request.c"
  86. #include "../plc/SendMME.c"
  87. #include "../plc/WriteFirmware1.c"
  88. #include "../plc/StartFirmware1.c"
  89. #include "../plc/WaitForStart.c"
  90. #endif
  91. #ifndef MAKEFILE
  92. #include "../tools/getoptv.c"
  93. #include "../tools/putoptv.c"
  94. #include "../tools/version.c"
  95. #include "../tools/uintspec.c"
  96. #include "../tools/checkfilename.c"
  97. #include "../tools/hexdecode.c"
  98. #include "../tools/todigit.c"
  99. #include "../tools/hexdump.c"
  100. #include "../tools/checksum32.c"
  101. #include "../tools/fdchecksum32.c"
  102. #include "../tools/error.c"
  103. #include "../tools/strfbits.c"
  104. #include "../tools/typename.c"
  105. #endif
  106. #ifndef MAKEFILE
  107. #include "../ether/openchannel.c"
  108. #include "../ether/closechannel.c"
  109. #include "../ether/readpacket.c"
  110. #include "../ether/sendpacket.c"
  111. #include "../ether/channel.c"
  112. #endif
  113. #ifndef MAKEFILE
  114. #include "../nvm/nvmfile1.c"
  115. #endif
  116. #ifndef MAKEFILE
  117. #include "../mme/MMECode.c"
  118. #include "../mme/EthernetHeader.c"
  119. #include "../mme/QualcommHeader.c"
  120. #include "../mme/UnwantedMessage.c"
  121. #endif
  122. /*====================================================================*
  123. *
  124. * signed ReadMME (struct plc * plc, uint8_t MMV, uint16_t MMTYPE);
  125. *
  126. * plc.h
  127. *
  128. * this is a custom version of ReadMME that intercepts VS_ARPC
  129. * messages and print them on the console in human readable format;
  130. *
  131. * this implementation was an early attempt the does not take full
  132. * advantage of the VS_ARPC structure and so messages are printed
  133. * in a format that is incompatible with the Windows Device Manager;
  134. * program amptest corrects this problem by calling ARPCPrint;
  135. *
  136. * this function is maintained for legacy purposes only and will be
  137. * deleted at some point in the future;
  138. *
  139. *
  140. * Contributor(s):
  141. * Charles Maier
  142. *
  143. *--------------------------------------------------------------------*/
  144. signed ReadMME (struct plc * plc, uint8_t MMV, uint16_t MMTYPE)
  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. static qualcomm_hdr header_arpc =
  152. {
  153. 0,
  154. VS_ARPC | MMTYPE_IND,
  155. {
  156. 0x00,
  157. 0xB0,
  158. 0x52
  159. }
  160. };
  161. struct __packed vs_arpc_ind
  162. {
  163. struct ethernet_hdr ethernet;
  164. struct qualcomm_hdr qualcomm;
  165. uint16_t RDATALENGTH;
  166. uint8_t RDATAOFFSET;
  167. uint8_t RDATA [1];
  168. }
  169. * indicate = (struct vs_arpc_ind *)(plc->message);
  170. #ifndef __GNUC__
  171. #pragma pack (pop)
  172. #endif
  173. while ((plc->packetsize = readpacket (channel, message, sizeof (* message))) > 0)
  174. {
  175. #if 1
  176. /*
  177. * Normally this block of code would be replaced by function FirmwareMessage () but due to an
  178. * ARPC implmenetation error on early diagnostic applets we need to use this code instead;
  179. */
  180. header_arpc.MMTYPE = HTOLE16 (header_arpc.MMTYPE);
  181. if (!memcmp (&indicate->qualcomm, &header_arpc, sizeof (header_arpc)))
  182. {
  183. indicate->RDATA [indicate->RDATALENGTH - indicate->RDATAOFFSET] = (char)(0);
  184. Display (plc, "0x%02X %s", indicate->RDATA [0], &indicate->RDATA [5]);
  185. continue;
  186. }
  187. #endif
  188. if (UnwantedMessage (message, plc->packetsize, 0, MMTYPE))
  189. {
  190. continue;
  191. }
  192. break;
  193. }
  194. return (plc->packetsize);
  195. }
  196. /*====================================================================*
  197. *
  198. * signed sequence (struct plc * plc);
  199. *
  200. * plc.h
  201. *
  202. * sequentially download and execute all modules found in a .nvm
  203. * file regardless of module type; users must ensure that modules
  204. * are executable or results may be unpredictable;
  205. *
  206. * DO NOT USE THIS FUNCTION TO DOWNBOOT AND EXECUTE RUNTIME FIRMWARE;
  207. *
  208. *
  209. * Contributor(s):
  210. * Charles Maier
  211. *
  212. *--------------------------------------------------------------------*/
  213. static signed sequence (struct plc * plc)
  214. {
  215. struct nvm_header1 nvm_header;
  216. unsigned module = 0;
  217. do
  218. {
  219. if (read (plc->NVM.file, &nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  220. {
  221. if (_allclr (plc->flags, PLC_SILENCE))
  222. {
  223. error (0, errno, NVM_HDR_CANTREAD, plc->NVM.name, module);
  224. }
  225. return (-1);
  226. }
  227. if (LE32TOH (nvm_header.HEADERVERSION) != 0x60000000)
  228. {
  229. if (_allclr (plc->flags, PLC_SILENCE))
  230. {
  231. error (0, 0, NVM_HDR_VERSION, plc->NVM.name, module);
  232. }
  233. return (-1);
  234. }
  235. if (checksum32 (&nvm_header, sizeof (nvm_header), 0))
  236. {
  237. if (_allclr (plc->flags, PLC_SILENCE))
  238. {
  239. error (0, 0, NVM_HDR_CHECKSUM, plc->NVM.name, module);
  240. }
  241. return (-1);
  242. }
  243. if (WriteFirmware1 (plc, module, &nvm_header))
  244. {
  245. return (-1);
  246. }
  247. if (StartFirmware1 (plc, module, &nvm_header))
  248. {
  249. return (-1);
  250. }
  251. if (_anyset (plc->flags, PLC_QUICK_FLASH))
  252. {
  253. break;
  254. }
  255. while (!ReadMME (plc, 0, (VS_HOST_ACTION | MMTYPE_IND)));
  256. module++;
  257. }
  258. while (nvm_header.NEXTHEADER);
  259. return (0);
  260. }
  261. /*====================================================================*
  262. *
  263. * void function (struct plc * plc, int argc, char const * argv);
  264. *
  265. * execute all applets in each file on the command line; return no
  266. * value; this function may be called repeatedly;
  267. *
  268. *
  269. *--------------------------------------------------------------------*/
  270. static void function (struct plc * plc, int argc, char const * argv [])
  271. {
  272. while ((argc) && (* argv))
  273. {
  274. if ((plc->NVM.file = open (plc->NVM.name = * argv, O_BINARY|O_RDONLY)) == -1)
  275. {
  276. error (0, errno, "%s", plc->NVM.name);
  277. }
  278. else if (nvmfile1 (&plc->NVM))
  279. {
  280. error (0, errno, "Won't load %s", plc->NVM.name);
  281. }
  282. else if (sequence (plc))
  283. {
  284. error (0, errno, "Abandoning %s", plc->NVM.name);
  285. }
  286. close (plc->NVM.file);
  287. argc--;
  288. argv++;
  289. }
  290. return;
  291. }
  292. /*====================================================================*
  293. *
  294. * int main (int argc, char const * argv[]);
  295. *
  296. * parse command line, populate plc structure and perform selected
  297. * operations; show help summary when asked; see getoptv and putoptv
  298. * to understand command line parsing and help summary display; see
  299. * plc.h for the definition of struct plc;
  300. *
  301. * the default interface is eth1 because most people use eth0 as
  302. * their principle network connection; you can specify another
  303. * interface with -i or define environment string PLC to make
  304. * that the default interface and save typing;
  305. *
  306. *
  307. *--------------------------------------------------------------------*/
  308. int main (int argc, char const * argv [])
  309. {
  310. extern struct channel channel;
  311. char firmware [PLC_VERSION_STRING];
  312. static char const * optv [] =
  313. {
  314. "c:ei:lp:qt:vx",
  315. "file [file] [...]",
  316. "Qualcomm Atheros Test Applet Loader",
  317. "e\tredirect stderr to stdout",
  318. #if defined (WINPCAP) || defined (LIBPCAP)
  319. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  320. #else
  321. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  322. #endif
  323. "l\tloop until program is terminated",
  324. "q\tquiet mode",
  325. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  326. "v\tverbose mode",
  327. "x\texit on error",
  328. (char const *) (0)
  329. };
  330. #include "../plc/plc.c"
  331. signed c;
  332. if (getenv (PLCDEVICE))
  333. {
  334. #if defined (WINPCAP) || defined (LIBPCAP)
  335. channel.ifindex = atoi (getenv (PLCDEVICE));
  336. #else
  337. channel.ifname = strdup (getenv (PLCDEVICE));
  338. #endif
  339. }
  340. optind = 1;
  341. while ((c = getoptv (argc, argv, optv)) != -1)
  342. {
  343. switch (c)
  344. {
  345. case 'e':
  346. dup2 (STDOUT_FILENO, STDERR_FILENO);
  347. break;
  348. case 'i':
  349. #if defined (WINPCAP) || defined (LIBPCAP)
  350. channel.ifindex = atoi (optarg);
  351. #else
  352. channel.ifname = optarg;
  353. #endif
  354. break;
  355. case 'l':
  356. _setbits (plc.flags, PLC_FOREVER);
  357. break;
  358. case 'q':
  359. _setbits (channel.flags, CHANNEL_SILENCE);
  360. _setbits (plc.flags, PLC_SILENCE);
  361. break;
  362. case 't':
  363. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  364. break;
  365. case 'v':
  366. _setbits (channel.flags, CHANNEL_VERBOSE);
  367. _setbits (plc.flags, PLC_VERBOSE);
  368. break;
  369. case 'x':
  370. _setbits (plc.flags, PLC_BAILOUT);
  371. break;
  372. default:
  373. break;
  374. }
  375. }
  376. argc -= optind;
  377. argv += optind;
  378. openchannel (&channel);
  379. if (!(plc.message = malloc (sizeof (* plc.message))))
  380. {
  381. error (1, errno, PLC_NOMEMORY);
  382. }
  383. if (WaitForStart (&plc, firmware, sizeof (firmware)))
  384. {
  385. error (1, ECANCELED, PLC_NODETECT);
  386. }
  387. if (strcmp (firmware, "BootLoader"))
  388. {
  389. error (1, ECANCELED, "BootLoader must be running");
  390. }
  391. do
  392. {
  393. function (&plc, argc, argv);
  394. }
  395. while (_anyset (plc.flags, PLC_FOREVER));
  396. free (plc.message);
  397. closechannel (&channel);
  398. return (plc.state);
  399. }