int64host.c 9.5 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. * int64host.c - Atheros INT6400 Host Emulator
  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 <limits.h>
  56. #include <string.h>
  57. #include <ctype.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 "../plc/plc.h"
  70. #include "../ram/nvram.h"
  71. #include "../ram/sdram.h"
  72. #include "../nvm/nvm.h"
  73. #include "../pib/pib.h"
  74. /*====================================================================*
  75. * custom source files;
  76. *--------------------------------------------------------------------*/
  77. #ifndef MAKEFILE
  78. #include "../plc/BootDevice1.c"
  79. #include "../plc/BootFirmware1.c"
  80. #include "../plc/BootParameters1.c"
  81. #include "../plc/chipset.c"
  82. #include "../plc/Confirm.c"
  83. #include "../plc/Devices.c"
  84. #include "../plc/Failure.c"
  85. #include "../plc/FlashNVM.c"
  86. #include "../plc/HostActionResponse.c"
  87. #include "../plc/InitDevice1.c"
  88. #include "../plc/EmulateHost64.c"
  89. #include "../plc/Request.c"
  90. #include "../plc/ResetDevice.c"
  91. #include "../plc/ReadMME.c"
  92. #include "../plc/SendMME.c"
  93. #include "../plc/WaitForReset.c"
  94. #include "../plc/WaitForStart.c"
  95. #include "../plc/WriteExecuteFirmware1.c"
  96. #include "../plc/WriteExecutePIB.c"
  97. #include "../plc/StartFirmware1.c"
  98. #include "../plc/WriteFirmware1.c"
  99. #include "../plc/WriteMEM.c"
  100. #include "../plc/WriteNVM.c"
  101. #include "../plc/WritePIB.c"
  102. #include "../plc/WriteCFG.c"
  103. #include "../plc/ReadFirmware1.c"
  104. #include "../plc/ReadParameters1.c"
  105. #include "../plc/ModuleRead.c"
  106. #endif
  107. #ifndef MAKEFILE
  108. #include "../tools/getoptv.c"
  109. #include "../tools/putoptv.c"
  110. #include "../tools/version.c"
  111. #include "../tools/uintspec.c"
  112. #include "../tools/checkfilename.c"
  113. #include "../tools/hexdecode.c"
  114. #include "../tools/hexstring.c"
  115. #include "../tools/todigit.c"
  116. #include "../tools/hexdump.c"
  117. #include "../tools/checksum32.c"
  118. #include "../tools/fdchecksum32.c"
  119. #include "../tools/error.c"
  120. #include "../tools/strfbits.c"
  121. #include "../tools/typename.c"
  122. #endif
  123. #ifndef MAKEFILE
  124. #include "../ether/openchannel.c"
  125. #include "../ether/closechannel.c"
  126. #include "../ether/readpacket.c"
  127. #include "../ether/sendpacket.c"
  128. #include "../ether/channel.c"
  129. #endif
  130. #ifndef MAKEFILE
  131. #include "../mme/MMECode.c"
  132. #include "../mme/EthernetHeader.c"
  133. #include "../mme/QualcommHeader.c"
  134. #include "../mme/UnwantedMessage.c"
  135. #endif
  136. #ifndef MAKEFILE
  137. #include "../key/keys.c"
  138. #endif
  139. #ifndef MAKEFILE
  140. #include "../nvm/nvmfile1.c"
  141. #endif
  142. #ifndef MAKEFILE
  143. #include "../pib/pibfile1.c"
  144. #include "../pib/pibpeek1.c"
  145. #endif
  146. /*====================================================================*
  147. *
  148. * int main (int argc, char const * argv[]);
  149. *
  150. * parse command line, populate plc structure and perform selected
  151. * operations; show help summary when asked; see getoptv and putoptv
  152. * to understand command line parsing and help summary display; see
  153. * plc.h for the definition of struct plc;
  154. *
  155. * the default interface is eth1 because most people use eth0 as
  156. * their principle network connection; you can specify another
  157. * interface with -i or define environment string PLC to make
  158. * that the default interface and save typing;
  159. *
  160. *
  161. *--------------------------------------------------------------------*/
  162. int main (int argc, char const * argv [])
  163. {
  164. extern struct channel channel;
  165. static char const * optv [] =
  166. {
  167. "Fi:n:N:p:P:qt:vx",
  168. "-N file -P file [-n file] [-p file]",
  169. "Qualcomm Atheros INT6400 Host Emulator",
  170. "F[F]\tflash (force) NVRAM using VS_MOD_NVM",
  171. #if defined (WINPCAP) || defined (LIBPCAP)
  172. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  173. #else
  174. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  175. #endif
  176. "n f\tread NVM from device into file using VS_RD_MOD",
  177. "N f\twrite NVM file to device using VS_WR_MEM",
  178. "p f\tread PIB from device into file using VS_RD_MOD",
  179. "P f\twrite PIB file to device using VS_WR_MEM",
  180. "q\tquiet mode",
  181. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  182. "v\tverbose mode",
  183. "x\texit on error",
  184. (char const *) (0)
  185. };
  186. #include "../plc/plc.c"
  187. signed c;
  188. plc.timer = 10000;
  189. if (getenv (PLCDEVICE))
  190. {
  191. #if defined (WINPCAP) || defined (LIBPCAP)
  192. channel.ifindex = atoi (getenv (PLCDEVICE));
  193. #else
  194. channel.ifname = strdup (getenv (PLCDEVICE));
  195. #endif
  196. }
  197. optind = 1;
  198. while ((c = getoptv (argc, argv, optv)) != -1)
  199. {
  200. switch (c)
  201. {
  202. case 'F':
  203. _setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
  204. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  205. {
  206. _setbits (plc.module, VS_MODULE_FORCE);
  207. }
  208. _setbits (plc.flags, PLC_FLASH_DEVICE);
  209. break;
  210. case 'i':
  211. #if defined (WINPCAP) || defined (LIBPCAP)
  212. channel.ifindex = atoi (optarg);
  213. #else
  214. channel.ifname = optarg;
  215. #endif
  216. break;
  217. case 'N':
  218. if (!checkfilename (optarg))
  219. {
  220. error (1, EINVAL, "%s", optarg);
  221. }
  222. if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1)
  223. {
  224. error (1, errno, "%s", plc.NVM.name);
  225. }
  226. if (nvmfile1 (&plc.NVM))
  227. {
  228. error (1, errno, "Bad firmware file: %s", plc.NVM.name);
  229. }
  230. _setbits (plc.flags, PLC_WRITE_MAC);
  231. break;
  232. case 'n':
  233. if (!checkfilename (optarg))
  234. {
  235. error (1, EINVAL, "%s", optarg);
  236. }
  237. if ((plc.nvm.file = open (plc.nvm.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  238. {
  239. error (1, errno, "%s", plc.nvm.name);
  240. }
  241. break;
  242. case 'P':
  243. if (!checkfilename (optarg))
  244. {
  245. error (1, EINVAL, "%s", optarg);
  246. }
  247. if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1)
  248. {
  249. error (1, errno, "%s", plc.PIB.name);
  250. }
  251. if (pibfile1 (&plc.PIB))
  252. {
  253. error (1, errno, "Bad parameter file: %s", plc.PIB.name);
  254. }
  255. _setbits (plc.flags, PLC_WRITE_PIB);
  256. break;
  257. case 'p':
  258. if (!checkfilename (optarg))
  259. {
  260. error (1, EINVAL, "%s", optarg);
  261. }
  262. if ((plc.pib.file = open (plc.pib.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  263. {
  264. error (1, errno, "%s", plc.pib.name);
  265. }
  266. break;
  267. case 'q':
  268. _setbits (channel.flags, CHANNEL_SILENCE);
  269. _setbits (plc.flags, PLC_SILENCE);
  270. break;
  271. case 't':
  272. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  273. break;
  274. case 'v':
  275. _setbits (channel.flags, CHANNEL_VERBOSE);
  276. _setbits (plc.flags, PLC_VERBOSE);
  277. break;
  278. case 'x':
  279. _setbits (plc.flags, PLC_BAILOUT);
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. argc -= optind;
  286. argv += optind;
  287. /*
  288. * cancel operation if the user omitted a file or entered to extra argments
  289. * on the command line;
  290. */
  291. if (argc)
  292. {
  293. error (1, ENOTSUP, ERROR_TOOMANY);
  294. }
  295. if (plc.NVM.file == -1)
  296. {
  297. error (1, ECANCELED, "No host NVM file named");
  298. }
  299. if (plc.PIB.file == -1)
  300. {
  301. error (1, ECANCELED, "No host PIB file named");
  302. }
  303. if (plc.nvm.file == -1)
  304. {
  305. error (1, ECANCELED, "No user NVM file named");
  306. }
  307. if (plc.pib.file == -1)
  308. {
  309. error (1, ECANCELED, "No user PIB file named");
  310. }
  311. openchannel (&channel);
  312. if (!(plc.message = malloc (sizeof (* plc.message))))
  313. {
  314. error (1, errno, PLC_NOMEMORY);
  315. }
  316. EmulateHost64 (&plc);
  317. free (plc.message);
  318. closechannel (&channel);
  319. exit (0);
  320. }