EmulateHost.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. * signed EmulateHost (struct plc * plc);
  44. *
  45. * plc.h
  46. *
  47. * wait indefinitely for VS_HOST_ACTION messages; service requests
  48. * only; it stops dead - like a bug! - on error;
  49. *
  50. *
  51. * Contributor(s):
  52. * Charles Maier
  53. *
  54. *--------------------------------------------------------------------*/
  55. #ifndef EMULATEHOST_SOURCE
  56. #define EMULATEHOST_SOURCE
  57. #include <unistd.h>
  58. #include <memory.h>
  59. #include "../plc/plc.h"
  60. #include "../ether/channel.h"
  61. #include "../tools/error.h"
  62. #include "../tools/flags.h"
  63. #include "../tools/files.h"
  64. #include "../nvm/nvm.h"
  65. #include "../pib/pib.h"
  66. signed EmulateHost (struct plc * plc)
  67. {
  68. struct channel * channel = (struct channel *)(plc->channel);
  69. struct message * message = (struct message *)(plc->message);
  70. static char const * actions [] =
  71. {
  72. "start device",
  73. "store firmware",
  74. "store parameters",
  75. "update host",
  76. "config memory",
  77. "restore defaults",
  78. "unknown"
  79. };
  80. #ifndef __GNUC__
  81. #pragma pack (push,1)
  82. #endif
  83. struct __packed vs_host_action_ind
  84. {
  85. struct ethernet_hdr ethernet;
  86. struct qualcomm_hdr qualcomm;
  87. uint8_t MACTION;
  88. uint8_t MAJOR_VERSION;
  89. uint8_t MINOR_VERSION;
  90. }
  91. * indicate = (struct vs_host_action_ind *) (message);
  92. #if 0
  93. struct __packed vs_host_action_rsp
  94. {
  95. struct ethernet_hdr ethernet;
  96. struct qualcomm_hdr qualcomm;
  97. uint8_t MSTATUS;
  98. }
  99. * response = (struct vs_host_action_rsp *) (message);
  100. #endif
  101. #ifndef __GNUC__
  102. #pragma pack (pop)
  103. #endif
  104. struct nvm_header1 nvm_header;
  105. struct pib_header pib_header;
  106. uint32_t offset;
  107. char const * PIB = plc->PIB.name;
  108. char const * NVM = plc->NVM.name;
  109. signed timer = channel->timeout;
  110. signed status = 0;
  111. Request (plc, "Waiting for Host Action");
  112. while (1)
  113. {
  114. channel->timeout = plc->timer;
  115. status = ReadMME (plc, 0, (VS_HOST_ACTION | MMTYPE_IND));
  116. channel->timeout = timer;
  117. if (status < 0)
  118. {
  119. break;
  120. }
  121. if (status > 0)
  122. {
  123. printf ("\n");
  124. if (indicate->MACTION < (sizeof (actions) / sizeof (char const *)))
  125. {
  126. Confirm (plc, "Host Action Request is (%d) %s.", indicate->MACTION, actions [indicate->MACTION]);
  127. }
  128. else
  129. {
  130. error (0, ENOTSUP, "Host Action 0x%0X", indicate->MACTION);
  131. continue;
  132. }
  133. memcpy (channel->peer, indicate->ethernet.OSA, sizeof (channel->peer));
  134. channel->timeout = timer;
  135. if (indicate->MACTION == 0x00)
  136. {
  137. unsigned module = 0;
  138. char firmware [PLC_VERSION_STRING];
  139. if (HostActionResponse (plc))
  140. {
  141. return (-1);
  142. }
  143. if (lseek (plc->PIB.file, 0, SEEK_SET))
  144. {
  145. error (1, errno, FILE_CANTHOME, plc->PIB.name);
  146. }
  147. if (read (plc->PIB.file, &pib_header, sizeof (pib_header)) != sizeof (pib_header))
  148. {
  149. error (1, errno, FILE_CANTREAD, plc->PIB.name);
  150. }
  151. if (lseek (plc->PIB.file, 0, SEEK_SET))
  152. {
  153. error (1, errno, FILE_CANTHOME, plc->PIB.name);
  154. }
  155. if (BE16TOH (*(uint16_t *)(&pib_header)) < 0x0305)
  156. {
  157. offset = LEGACY_PIBOFFSET;
  158. }
  159. else if (BE16TOH (*(uint16_t *)(&pib_header)) < 0x0500)
  160. {
  161. offset = INT6x00_PIBOFFSET;
  162. }
  163. else
  164. {
  165. offset = AR7x00_PIBOFFSET;
  166. }
  167. if (WriteMEM (plc, &plc->PIB, 0, offset, LE16TOH (pib_header.PIBLENGTH)))
  168. {
  169. return (-1);
  170. }
  171. if (lseek (plc->NVM.file, 0, SEEK_SET))
  172. {
  173. error (1, errno, FILE_CANTHOME, plc->NVM.name);
  174. }
  175. if (read (plc->NVM.file, &nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  176. {
  177. error (1, errno, FILE_CANTREAD, plc->NVM.name);
  178. }
  179. while (nvm_header.NEXTHEADER)
  180. {
  181. lseek (plc->NVM.file, LE32TOH (nvm_header.NEXTHEADER), SEEK_SET);
  182. if (read (plc->NVM.file, &nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  183. {
  184. error (1, errno, FILE_CANTREAD, plc->NVM.name);
  185. }
  186. module++;
  187. }
  188. if (WriteFirmware1 (plc, module, &nvm_header))
  189. {
  190. return (-1);
  191. }
  192. if (StartFirmware1 (plc, module, &nvm_header))
  193. {
  194. return (-1);
  195. }
  196. if (WaitForStart (plc, firmware, sizeof (firmware)))
  197. {
  198. return (-1);
  199. }
  200. if (_anyset (plc->flags, PLC_FLASH_DEVICE))
  201. {
  202. if (WriteNVM (plc))
  203. {
  204. return (-1);
  205. }
  206. if (WritePIB (plc))
  207. {
  208. return (-1);
  209. }
  210. if (FlashNVM (plc))
  211. {
  212. return (-1);
  213. }
  214. }
  215. continue;
  216. }
  217. if (indicate->MACTION == 0x01)
  218. {
  219. if (HostActionResponse (plc))
  220. {
  221. return (-1);
  222. }
  223. close (plc->NVM.file);
  224. if (ReadFirmware1 (plc))
  225. {
  226. return (-1);
  227. }
  228. if ((plc->NVM.file = open (plc->NVM.name = plc->nvm.name, O_BINARY|O_RDONLY)) == -1)
  229. {
  230. error (1, errno, "%s", plc->NVM.name);
  231. }
  232. if (ResetDevice (plc))
  233. {
  234. return (-1);
  235. }
  236. continue;
  237. }
  238. if (indicate->MACTION == 0x02)
  239. {
  240. if (HostActionResponse (plc))
  241. {
  242. return (-1);
  243. }
  244. close (plc->PIB.file);
  245. if (ReadParameters1 (plc))
  246. {
  247. return (-1);
  248. }
  249. if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
  250. {
  251. error (1, errno, "%s", plc->PIB.name);
  252. }
  253. if (ResetDevice (plc))
  254. {
  255. return (-1);
  256. }
  257. continue;
  258. }
  259. if (indicate->MACTION == 0x03)
  260. {
  261. if (HostActionResponse (plc))
  262. {
  263. return (-1);
  264. }
  265. close (plc->NVM.file);
  266. if (ReadFirmware1 (plc))
  267. {
  268. return (-1);
  269. }
  270. if ((plc->NVM.file = open (plc->NVM.name = plc->nvm.name, O_BINARY|O_RDONLY)) == -1)
  271. {
  272. error (1, errno, "%s", plc->NVM.name);
  273. }
  274. close (plc->PIB.file);
  275. if (ReadParameters1 (plc))
  276. {
  277. return (-1);
  278. }
  279. if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
  280. {
  281. error (1, errno, "%s", plc->PIB.name);
  282. }
  283. if (ResetDevice (plc))
  284. {
  285. return (-1);
  286. }
  287. continue;
  288. }
  289. if (indicate->MACTION == 0x04)
  290. {
  291. #if 0
  292. /*
  293. * Due to an omission in the INT6300 BootLoader, responding to this VS_HOST_ACTION
  294. * indication will suppress subsequent VS_HOST_ACTION messages and the device will
  295. * not request firmware and parameters; this may be corrected on the INT6400;
  296. */
  297. if (HostActionResponse (plc))
  298. {
  299. return (-1);
  300. }
  301. #endif
  302. if (WriteCFG (plc))
  303. {
  304. return (-1);
  305. }
  306. /*
  307. * At this point, one could download firmware and parameters without waiting for
  308. * further requests from the device; however, we elect to wait for them since it
  309. * is 'good form'; a device should send code 0x00 within 10 seconds of this one;
  310. */
  311. continue;
  312. }
  313. if (indicate->MACTION == 0x05)
  314. {
  315. if (HostActionResponse (plc))
  316. {
  317. return (-1);
  318. }
  319. close (plc->NVM.file);
  320. if ((plc->NVM.file = open (plc->NVM.name = NVM, O_BINARY|O_RDONLY)) == -1)
  321. {
  322. error (1, errno, "%s", plc->NVM.name);
  323. }
  324. close (plc->PIB.file);
  325. if ((plc->PIB.file = open (plc->PIB.name = PIB, O_BINARY|O_RDONLY)) == -1)
  326. {
  327. error (1, errno, "%s", plc->PIB.name);
  328. }
  329. if (ResetDevice (plc))
  330. {
  331. return (-1);
  332. }
  333. continue;
  334. }
  335. error (0, ENOSYS, "Host Action 0x%0X", indicate->MACTION);
  336. }
  337. }
  338. return (0);
  339. }
  340. #endif