amphost.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. * amphost.c -
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. * Nathaniel Houghton
  49. *
  50. *--------------------------------------------------------------------*/
  51. /*====================================================================*"
  52. * system header files;
  53. *--------------------------------------------------------------------*/
  54. #include <unistd.h>
  55. #include <stdlib.h>
  56. #include <limits.h>
  57. #include <string.h>
  58. #include <ctype.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 "../ram/nvram.h"
  71. #include "../ram/sdram.h"
  72. #include "../plc/plc.h"
  73. #include "../nvm/nvm.h"
  74. #include "../pib/pib.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/Failure.c"
  83. #include "../plc/FactoryDefaults.c"
  84. #include "../plc/FlashDevice1.c"
  85. #include "../plc/FlashNVM.c"
  86. #include "../plc/HostActionResponse.c"
  87. #include "../plc/Request.c"
  88. #include "../plc/ResetDevice.c"
  89. #include "../plc/ReadMME.c"
  90. #include "../plc/SendMME.c"
  91. #include "../plc/WaitForReset.c"
  92. #include "../plc/WaitForStart.c"
  93. #include "../plc/WaitForRestart.c"
  94. #include "../plc/WriteExecuteFirmware1.c"
  95. #include "../plc/WriteExecutePIB.c"
  96. #include "../plc/WriteMEM.c"
  97. #include "../plc/WriteNVM.c"
  98. #include "../plc/WritePIB.c"
  99. #include "../plc/ReadFirmware1.c"
  100. #include "../plc/ReadParameters1.c"
  101. #include "../plc/InitDevice1.c"
  102. #include "../plc/BootDevice1.c"
  103. #include "../plc/BootFirmware1.c"
  104. #include "../plc/BootParameters1.c"
  105. #include "../plc/WriteFirmware1.c"
  106. #include "../plc/StartFirmware1.c"
  107. #include "../plc/ModuleRead.c"
  108. #endif
  109. #ifndef MAKEFILE
  110. #include "../tools/getoptv.c"
  111. #include "../tools/putoptv.c"
  112. #include "../tools/version.c"
  113. #include "../tools/uintspec.c"
  114. #include "../tools/checkfilename.c"
  115. #include "../tools/hexdecode.c"
  116. #include "../tools/hexstring.c"
  117. #include "../tools/todigit.c"
  118. #include "../tools/hexdump.c"
  119. #include "../tools/checksum32.c"
  120. #include "../tools/fdchecksum32.c"
  121. #include "../tools/error.c"
  122. #include "../tools/strfbits.c"
  123. #include "../tools/typename.c"
  124. #endif
  125. #ifndef MAKEFILE
  126. #include "../ether/openchannel.c"
  127. #include "../ether/closechannel.c"
  128. #include "../ether/readpacket.c"
  129. #include "../ether/sendpacket.c"
  130. #include "../ether/channel.c"
  131. #endif
  132. #ifndef MAKEFILE
  133. #include "../mme/EthernetHeader.c"
  134. #include "../mme/QualcommHeader.c"
  135. #include "../mme/UnwantedMessage.c"
  136. #include "../mme/MMECode.c"
  137. #endif
  138. #ifndef MAKEFILE
  139. #include "../ram/sdramfile.c"
  140. #include "../ram/sdrampeek.c"
  141. #endif
  142. #ifndef MAKEFILE
  143. #include "../nvm/nvmfile1.c"
  144. #endif
  145. #ifndef MAKEFILE
  146. #include "../pib/pibpeek1.c"
  147. #include "../pib/pibfile1.c"
  148. #endif
  149. #ifndef MAKEFILE
  150. #include "../key/keys.c"
  151. #endif
  152. /*====================================================================*
  153. * program constants;
  154. *--------------------------------------------------------------------*/
  155. #define NEWPIB "user.pib"
  156. #define NEWNVM "user.nvm"
  157. /*====================================================================*
  158. *
  159. * signed function (struct plc * plc);
  160. *
  161. * plc.h
  162. *
  163. * wait indefinitely for VS_HOST_ACTION messages; service the device
  164. * only; it will stop dead - like a bug! - on error;
  165. *
  166. *
  167. * Contributor(s):
  168. * Charles Maier
  169. *
  170. *--------------------------------------------------------------------*/
  171. signed function (struct plc * plc)
  172. {
  173. struct channel * channel = (struct channel *)(plc->channel);
  174. struct message * message = (struct message *)(plc->message);
  175. static char const * actions [] =
  176. {
  177. "start device",
  178. "store firmware",
  179. "store parameters",
  180. "update host",
  181. "config memory",
  182. "restore defaults",
  183. "unknown"
  184. };
  185. #ifndef __GNUC__
  186. #pragma pack (push,1)
  187. #endif
  188. struct __packed vs_host_action_ind
  189. {
  190. struct ethernet_hdr ethernet;
  191. struct qualcomm_hdr qualcomm;
  192. uint8_t MACTION;
  193. uint8_t MAJOR_VERSION;
  194. uint8_t MINOR_VERSION;
  195. }
  196. * indicate = (struct vs_host_action_ind *) (message);
  197. #ifndef __GNUC__
  198. #pragma pack (pop)
  199. #endif
  200. char const * FactoryNVM = plc->NVM.name;
  201. char const * FactoryPIB = plc->PIB.name;
  202. signed status;
  203. signed action;
  204. Request (plc, "Waiting for Host Action");
  205. while (1)
  206. {
  207. status = ReadMME (plc, 0, (VS_HOST_ACTION | MMTYPE_IND));
  208. if (status < 0)
  209. {
  210. break;
  211. }
  212. if (status > 0)
  213. {
  214. printf ("\n");
  215. if (indicate->MACTION < SIZEOF (actions))
  216. {
  217. Confirm (plc, "Host Action Request is (%d) %s.", indicate->MACTION, actions [indicate->MACTION]);
  218. }
  219. else
  220. {
  221. error (0, ENOTSUP, "Host Action 0x%0X", indicate->MACTION);
  222. continue;
  223. }
  224. action = indicate->MACTION;
  225. memcpy (channel->peer, indicate->ethernet.OSA, sizeof (channel->peer));
  226. if (HostActionResponse (plc))
  227. {
  228. return (-1);
  229. }
  230. if (action == 0x00)
  231. {
  232. if (BootDevice1 (plc))
  233. {
  234. return (-1);
  235. }
  236. if (_anyset (plc->flags, PLC_FLASH_DEVICE))
  237. {
  238. FlashDevice1 (plc);
  239. }
  240. continue;
  241. }
  242. if (action == 0x01)
  243. {
  244. close (plc->NVM.file);
  245. if (ReadFirmware1 (plc))
  246. {
  247. return (-1);
  248. }
  249. if ((plc->NVM.file = open (plc->NVM.name = plc->nvm.name, O_BINARY|O_RDONLY)) == -1)
  250. {
  251. error (1, errno, "%s", plc->NVM.name);
  252. }
  253. if (ResetDevice (plc))
  254. {
  255. return (-1);
  256. }
  257. continue;
  258. }
  259. if (action == 0x02)
  260. {
  261. close (plc->PIB.file);
  262. if (ReadParameters1 (plc))
  263. {
  264. return (-1);
  265. }
  266. if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
  267. {
  268. error (1, errno, "%s", plc->PIB.name);
  269. }
  270. if (ResetDevice (plc))
  271. {
  272. return (-1);
  273. }
  274. continue;
  275. }
  276. if (action == 0x03)
  277. {
  278. close (plc->PIB.file);
  279. if (ReadParameters1 (plc))
  280. {
  281. return (-1);
  282. }
  283. if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
  284. {
  285. error (1, errno, "%s", plc->PIB.name);
  286. }
  287. close (plc->NVM.file);
  288. if (ReadFirmware1 (plc))
  289. {
  290. return (-1);
  291. }
  292. if ((plc->NVM.file = open (plc->NVM.name = plc->nvm.name, O_BINARY|O_RDONLY)) == -1)
  293. {
  294. error (1, errno, "%s", plc->NVM.name);
  295. }
  296. if (ResetDevice (plc))
  297. {
  298. return (-1);
  299. }
  300. continue;
  301. }
  302. if (action == 0x04)
  303. {
  304. if (InitDevice1 (plc))
  305. {
  306. return (-1);
  307. }
  308. continue;
  309. }
  310. if (action == 0x05)
  311. {
  312. close (plc->PIB.file);
  313. if ((plc->PIB.file = open (plc->PIB.name = FactoryPIB, O_BINARY|O_RDONLY)) == -1)
  314. {
  315. error (1, errno, "%s", plc->PIB.name);
  316. }
  317. close (plc->NVM.file);
  318. if ((plc->NVM.file = open (plc->NVM.name = FactoryNVM, O_BINARY|O_RDONLY)) == -1)
  319. {
  320. error (1, errno, "%s", plc->NVM.name);
  321. }
  322. if (ResetDevice (plc))
  323. {
  324. return (-1);
  325. }
  326. continue;
  327. }
  328. if (action == 0x06)
  329. {
  330. close (plc->PIB.file);
  331. if (ReadParameters1 (plc))
  332. {
  333. return (-1);
  334. }
  335. if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
  336. {
  337. error (1, errno, "%s", plc->PIB.name);
  338. }
  339. continue;
  340. }
  341. error (0, ENOSYS, "Host Action 0x%02X", action);
  342. }
  343. }
  344. return (0);
  345. }
  346. /*====================================================================*
  347. *
  348. * int main (int argc, char const * argv[]);
  349. *
  350. * parse command line, populate plc structure and perform selected
  351. * operations; show help summary when asked; see getoptv and putoptv
  352. * to understand command line parsing and help summary display; see
  353. * plc.h for the definition of struct plc;
  354. *
  355. * the default interface is eth1 because most people use eth0 as
  356. * their principle network connection; you can specify another
  357. * interface with -i or define environment string PLC to make
  358. * that the default interface and save typing;
  359. *
  360. *
  361. *--------------------------------------------------------------------*/
  362. int main (int argc, char const * argv [])
  363. {
  364. extern struct channel channel;
  365. static char const * optv [] =
  366. {
  367. "Fi:n:N:p:P:qt:vx",
  368. "-N file -P file [-n file] [-p file]",
  369. "Qualcomm Atheros INT6400 Host Emulator",
  370. "F[F]\tflash (force) NVRAM using VS_MOD_NVM",
  371. #if defined (WINPCAP) || defined (LIBPCAP)
  372. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  373. #else
  374. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  375. #endif
  376. "n f\tread NVM from device into file using VS_RD_MOD",
  377. "N f\twrite NVM file to device using VS_WR_MEM",
  378. "p f\tread PIB from device into file using VS_RD_MOD",
  379. "P f\twrite PIB file to device using VS_WR_MEM",
  380. "q\tquiet mode",
  381. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  382. "v\tverbose mode",
  383. "x\texit on error",
  384. (char const *) (0)
  385. };
  386. #include "../plc/plc.c"
  387. signed c;
  388. if (getenv (PLCDEVICE))
  389. {
  390. #if defined (WINPCAP) || defined (LIBPCAP)
  391. channel.ifindex = atoi (getenv (PLCDEVICE));
  392. #else
  393. channel.ifname = strdup (getenv (PLCDEVICE));
  394. #endif
  395. }
  396. optind = 1;
  397. while ((c = getoptv (argc, argv, optv)) != -1)
  398. {
  399. switch (c)
  400. {
  401. case 'F':
  402. _setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
  403. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  404. {
  405. _setbits (plc.module, VS_MODULE_FORCE);
  406. }
  407. _setbits (plc.flags, PLC_FLASH_DEVICE);
  408. break;
  409. case 'i':
  410. #if defined (WINPCAP) || defined (LIBPCAP)
  411. channel.ifindex = atoi (optarg);
  412. #else
  413. channel.ifname = optarg;
  414. #endif
  415. break;
  416. case 'N':
  417. if (!checkfilename (optarg))
  418. {
  419. error (1, EINVAL, "%s", optarg);
  420. }
  421. if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1)
  422. {
  423. error (1, errno, "%s", plc.NVM.name);
  424. }
  425. if (nvmfile1 (&plc.NVM))
  426. {
  427. error (1, errno, "Bad firmware file: %s", plc.NVM.name);
  428. }
  429. _setbits (plc.flags, PLC_WRITE_MAC);
  430. break;
  431. case 'n':
  432. if (!checkfilename (optarg))
  433. {
  434. error (1, EINVAL, "%s", optarg);
  435. }
  436. if ((plc.nvm.file = open (plc.nvm.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  437. {
  438. error (1, errno, "%s", plc.nvm.name);
  439. }
  440. break;
  441. case 'P':
  442. if (!checkfilename (optarg))
  443. {
  444. error (1, EINVAL, "%s", optarg);
  445. }
  446. if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1)
  447. {
  448. error (1, errno, "%s", plc.PIB.name);
  449. }
  450. if (pibfile1 (&plc.PIB))
  451. {
  452. error (1, errno, "Bad parameter file: %s", plc.PIB.name);
  453. }
  454. _setbits (plc.flags, PLC_WRITE_PIB);
  455. break;
  456. case 'p':
  457. if (!checkfilename (optarg))
  458. {
  459. error (1, EINVAL, "%s", optarg);
  460. }
  461. if ((plc.pib.file = open (plc.pib.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  462. {
  463. error (1, errno, "%s", plc.pib.name);
  464. }
  465. break;
  466. case 'q':
  467. _setbits (channel.flags, CHANNEL_SILENCE);
  468. _setbits (plc.flags, PLC_SILENCE);
  469. break;
  470. case 't':
  471. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  472. break;
  473. case 'v':
  474. _setbits (channel.flags, CHANNEL_VERBOSE);
  475. _setbits (plc.flags, PLC_VERBOSE);
  476. break;
  477. case 'x':
  478. _setbits (plc.flags, PLC_BAILOUT);
  479. break;
  480. default:
  481. break;
  482. }
  483. }
  484. argc -= optind;
  485. argv += optind;
  486. if (argc)
  487. {
  488. error (1, ENOTSUP, ERROR_TOOMANY);
  489. }
  490. if (plc.NVM.file == -1)
  491. {
  492. error (1, ECANCELED, "No host NVM file named");
  493. }
  494. if (plc.PIB.file == -1)
  495. {
  496. error (1, ECANCELED, "No host PIB file named");
  497. }
  498. if (plc.nvm.file == -1)
  499. {
  500. if ((plc.nvm.file = open (plc.nvm.name = NEWNVM, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  501. {
  502. error (1, errno, "%s", plc.nvm.name);
  503. }
  504. }
  505. if (plc.pib.file == -1)
  506. {
  507. if ((plc.pib.file = open (plc.pib.name = NEWPIB, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  508. {
  509. error (1, errno, "%s", plc.pib.name);
  510. }
  511. }
  512. openchannel (&channel);
  513. if (!(plc.message = malloc (sizeof (* plc.message))))
  514. {
  515. error (1, errno, PLC_NOMEMORY);
  516. }
  517. function (&plc);
  518. free (plc.message);
  519. closechannel (&channel);
  520. exit (0);
  521. }