EmulateHost64.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. * int EmulateHost64 (struct plc * plc);
  44. *
  45. * plc.h
  46. *
  47. * wait indefinitely for VS_HOST_ACTION messages; service the device
  48. * only; it will stop dead - like a bug! - on error;
  49. *
  50. *
  51. * Contributor(s):
  52. * Charles Maier
  53. *
  54. *--------------------------------------------------------------------*/
  55. #ifndef EMULATEHOST64_SOURCE
  56. #define EMULATEHOST64_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 EmulateHost64 (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. char const * NVM = plc->NVM.name;
  105. char const * PIB = plc->PIB.name;
  106. signed status;
  107. signed action;
  108. Request (plc, "Waiting for Host Action");
  109. while (1)
  110. {
  111. status = ReadMME (plc, 0, (VS_HOST_ACTION | MMTYPE_IND));
  112. if (status < 0)
  113. {
  114. break;
  115. }
  116. if (status > 0)
  117. {
  118. printf ("\n");
  119. if (indicate->MACTION < (sizeof (actions) / sizeof (char const *)))
  120. {
  121. Confirm (plc, "Host Action Request is (%d) %s.", indicate->MACTION, actions [indicate->MACTION]);
  122. }
  123. else
  124. {
  125. error (0, ENOTSUP, "Host Action 0x%0X", indicate->MACTION);
  126. continue;
  127. }
  128. action = indicate->MACTION;
  129. memcpy (channel->peer, indicate->ethernet.OSA, sizeof (channel->peer));
  130. if (HostActionResponse (plc))
  131. {
  132. return (-1);
  133. }
  134. if (action == 0x00)
  135. {
  136. if (BootDevice1 (plc))
  137. {
  138. return (-1);
  139. }
  140. if (_anyset (plc->flags, PLC_FLASH_DEVICE))
  141. {
  142. if (WriteNVM (plc))
  143. {
  144. return (-1);
  145. }
  146. if (WritePIB (plc))
  147. {
  148. return (-1);
  149. }
  150. if (FlashNVM (plc))
  151. {
  152. return (-1);
  153. }
  154. }
  155. continue;
  156. }
  157. if (action == 0x01)
  158. {
  159. close (plc->NVM.file);
  160. if (ReadFirmware1 (plc))
  161. {
  162. return (-1);
  163. }
  164. if ((plc->NVM.file = open (plc->NVM.name = plc->nvm.name, O_BINARY|O_RDONLY)) == -1)
  165. {
  166. error (1, errno, "%s", plc->NVM.name);
  167. }
  168. if (ResetDevice (plc))
  169. {
  170. return (-1);
  171. }
  172. continue;
  173. }
  174. if (action == 0x02)
  175. {
  176. close (plc->PIB.file);
  177. if (ReadParameters1 (plc))
  178. {
  179. return (-1);
  180. }
  181. if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
  182. {
  183. error (1, errno, "%s", plc->PIB.name);
  184. }
  185. if (ResetDevice (plc))
  186. {
  187. return (-1);
  188. }
  189. continue;
  190. }
  191. if (action == 0x03)
  192. {
  193. close (plc->PIB.file);
  194. if (ReadParameters1 (plc))
  195. {
  196. return (-1);
  197. }
  198. if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
  199. {
  200. error (1, errno, "%s", plc->PIB.name);
  201. }
  202. close (plc->NVM.file);
  203. if (ReadFirmware1 (plc))
  204. {
  205. return (-1);
  206. }
  207. if ((plc->NVM.file = open (plc->NVM.name = plc->nvm.name, O_BINARY|O_RDONLY)) == -1)
  208. {
  209. error (1, errno, "%s", plc->NVM.name);
  210. }
  211. if (ResetDevice (plc))
  212. {
  213. return (-1);
  214. }
  215. continue;
  216. }
  217. if (action == 0x04)
  218. {
  219. if (InitDevice1 (plc))
  220. {
  221. return (-1);
  222. }
  223. continue;
  224. }
  225. if (action == 0x05)
  226. {
  227. close (plc->NVM.file);
  228. if ((plc->NVM.file = open (plc->NVM.name = NVM, O_BINARY|O_RDONLY)) == -1)
  229. {
  230. error (1, errno, "%s", plc->NVM.name);
  231. }
  232. close (plc->PIB.file);
  233. if ((plc->PIB.file = open (plc->PIB.name = PIB, O_BINARY|O_RDONLY)) == -1)
  234. {
  235. error (1, errno, "%s", plc->PIB.name);
  236. }
  237. if (ResetDevice (plc))
  238. {
  239. return (-1);
  240. }
  241. continue;
  242. }
  243. if (action == 0x06)
  244. {
  245. close (plc->PIB.file);
  246. if (ReadParameters1 (plc))
  247. {
  248. return (-1);
  249. }
  250. if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
  251. {
  252. error (1, errno, "%s", plc->PIB.name);
  253. }
  254. continue;
  255. }
  256. error (0, ENOSYS, "Host Action 0x%02X", action);
  257. }
  258. }
  259. return (0);
  260. }
  261. #endif