EmulateHost.c.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. EmulateHost.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='EmulateHost64.c.html' title=' EmulateHost64.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='error.c.html' title=' error.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. *
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted (subject to the limitations
  29. * in the disclaimer below) provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above
  36. * copyright notice, this list of conditions and the following
  37. * disclaimer in the documentation and/or other materials
  38. * provided with the distribution.
  39. *
  40. * * Neither the name of Qualcomm Atheros nor the names of
  41. * its contributors may be used to endorse or promote products
  42. * derived from this software without specific prior written
  43. * permission.
  44. *
  45. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  46. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  47. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  48. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  51. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  57. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*
  62. *
  63. * signed EmulateHost (struct plc * plc);
  64. *
  65. * plc.h
  66. *
  67. * wait indefinitely for VS_HOST_ACTION messages; service requests
  68. * only; it stops dead - like a bug! - on error;
  69. *
  70. *
  71. * Contributor(s):
  72. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  73. *
  74. *--------------------------------------------------------------------*/
  75. #ifndef EMULATEHOST_SOURCE
  76. #define EMULATEHOST_SOURCE
  77. #include &lt;unistd.h&gt;
  78. #include &lt;memory.h&gt;
  79. #include &quot;../plc/plc.h&quot;
  80. #include &quot;../ether/channel.h&quot;
  81. #include &quot;../tools/error.h&quot;
  82. #include &quot;../tools/flags.h&quot;
  83. #include &quot;../tools/files.h&quot;
  84. #include &quot;../nvm/nvm.h&quot;
  85. #include &quot;../pib/pib.h&quot;
  86. signed EmulateHost (struct plc * plc)
  87. {
  88. struct channel * channel = (struct channel *)(plc-&gt;channel);
  89. struct message * message = (struct message *)(plc-&gt;message);
  90. static char const * actions [] =
  91. {
  92. &quot;start device&quot;,
  93. &quot;store firmware&quot;,
  94. &quot;store parameters&quot;,
  95. &quot;update host&quot;,
  96. &quot;config memory&quot;,
  97. &quot;restore defaults&quot;,
  98. &quot;unknown&quot;
  99. };
  100. #ifndef __GNUC__
  101. #pragma pack (push,1)
  102. #endif
  103. struct __packed vs_host_action_ind
  104. {
  105. struct ethernet_hdr ethernet;
  106. struct qualcomm_hdr qualcomm;
  107. uint8_t MACTION;
  108. uint8_t MAJOR_VERSION;
  109. uint8_t MINOR_VERSION;
  110. }
  111. * indicate = (struct vs_host_action_ind *) (message);
  112. #if 0
  113. struct __packed vs_host_action_rsp
  114. {
  115. struct ethernet_hdr ethernet;
  116. struct qualcomm_hdr qualcomm;
  117. uint8_t MSTATUS;
  118. }
  119. * response = (struct vs_host_action_rsp *) (message);
  120. #endif
  121. #ifndef __GNUC__
  122. #pragma pack (pop)
  123. #endif
  124. struct nvm_header1 nvm_header;
  125. struct pib_header pib_header;
  126. uint32_t offset;
  127. char const * PIB = plc-&gt;PIB.name;
  128. char const * NVM = plc-&gt;NVM.name;
  129. signed timer = channel-&gt;timeout;
  130. signed status = 0;
  131. Request (plc, &quot;Waiting for Host Action&quot;);
  132. while (1)
  133. {
  134. channel-&gt;timeout = plc-&gt;timer;
  135. status = ReadMME (plc, 0, (VS_HOST_ACTION | MMTYPE_IND));
  136. channel-&gt;timeout = timer;
  137. if (status &lt; 0)
  138. {
  139. break;
  140. }
  141. if (status &gt; 0)
  142. {
  143. printf (&quot;\n&quot;);
  144. if (indicate-&gt;MACTION &lt; (sizeof (actions) / sizeof (char const *)))
  145. {
  146. Confirm (plc, &quot;Host Action Request is (%d) %s.&quot;, indicate-&gt;MACTION, actions [indicate-&gt;MACTION]);
  147. }
  148. else
  149. {
  150. error (0, ENOTSUP, &quot;Host Action 0x%0X&quot;, indicate-&gt;MACTION);
  151. continue;
  152. }
  153. memcpy (channel-&gt;peer, indicate-&gt;ethernet.OSA, sizeof (channel-&gt;peer));
  154. channel-&gt;timeout = timer;
  155. if (indicate-&gt;MACTION == 0x00)
  156. {
  157. unsigned module = 0;
  158. char firmware [PLC_VERSION_STRING];
  159. if (HostActionResponse (plc))
  160. {
  161. return (-1);
  162. }
  163. if (lseek (plc-&gt;PIB.file, 0, SEEK_SET))
  164. {
  165. error (1, errno, FILE_CANTHOME, plc-&gt;PIB.name);
  166. }
  167. if (read (plc-&gt;PIB.file, &amp;pib_header, sizeof (pib_header)) != sizeof (pib_header))
  168. {
  169. error (1, errno, FILE_CANTREAD, plc-&gt;PIB.name);
  170. }
  171. if (lseek (plc-&gt;PIB.file, 0, SEEK_SET))
  172. {
  173. error (1, errno, FILE_CANTHOME, plc-&gt;PIB.name);
  174. }
  175. if (BE16TOH (*(uint16_t *)(&amp;pib_header)) &lt; 0x0305)
  176. {
  177. offset = LEGACY_PIBOFFSET;
  178. }
  179. else if (BE16TOH (*(uint16_t *)(&amp;pib_header)) &lt; 0x0500)
  180. {
  181. offset = INT6x00_PIBOFFSET;
  182. }
  183. else
  184. {
  185. offset = AR7x00_PIBOFFSET;
  186. }
  187. if (WriteMEM (plc, &amp;plc-&gt;PIB, 0, offset, LE16TOH (pib_header.PIBLENGTH)))
  188. {
  189. return (-1);
  190. }
  191. if (lseek (plc-&gt;NVM.file, 0, SEEK_SET))
  192. {
  193. error (1, errno, FILE_CANTHOME, plc-&gt;NVM.name);
  194. }
  195. if (read (plc-&gt;NVM.file, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  196. {
  197. error (1, errno, FILE_CANTREAD, plc-&gt;NVM.name);
  198. }
  199. while (nvm_header.NEXTHEADER)
  200. {
  201. lseek (plc-&gt;NVM.file, LE32TOH (nvm_header.NEXTHEADER), SEEK_SET);
  202. if (read (plc-&gt;NVM.file, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  203. {
  204. error (1, errno, FILE_CANTREAD, plc-&gt;NVM.name);
  205. }
  206. module++;
  207. }
  208. if (WriteFirmware1 (plc, module, &amp;nvm_header))
  209. {
  210. return (-1);
  211. }
  212. if (StartFirmware1 (plc, module, &amp;nvm_header))
  213. {
  214. return (-1);
  215. }
  216. if (WaitForStart (plc, firmware, sizeof (firmware)))
  217. {
  218. return (-1);
  219. }
  220. if (_anyset (plc-&gt;flags, PLC_FLASH_DEVICE))
  221. {
  222. if (WriteNVM (plc))
  223. {
  224. return (-1);
  225. }
  226. if (WritePIB (plc))
  227. {
  228. return (-1);
  229. }
  230. if (FlashNVM (plc))
  231. {
  232. return (-1);
  233. }
  234. }
  235. continue;
  236. }
  237. if (indicate-&gt;MACTION == 0x01)
  238. {
  239. if (HostActionResponse (plc))
  240. {
  241. return (-1);
  242. }
  243. close (plc-&gt;NVM.file);
  244. if (ReadFirmware1 (plc))
  245. {
  246. return (-1);
  247. }
  248. if ((plc-&gt;NVM.file = open (plc-&gt;NVM.name = plc-&gt;nvm.name, O_BINARY|O_RDONLY)) == -1)
  249. {
  250. error (1, errno, &quot;%s&quot;, plc-&gt;NVM.name);
  251. }
  252. if (ResetDevice (plc))
  253. {
  254. return (-1);
  255. }
  256. continue;
  257. }
  258. if (indicate-&gt;MACTION == 0x02)
  259. {
  260. if (HostActionResponse (plc))
  261. {
  262. return (-1);
  263. }
  264. close (plc-&gt;PIB.file);
  265. if (ReadParameters1 (plc))
  266. {
  267. return (-1);
  268. }
  269. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = plc-&gt;pib.name, O_BINARY|O_RDONLY)) == -1)
  270. {
  271. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  272. }
  273. if (ResetDevice (plc))
  274. {
  275. return (-1);
  276. }
  277. continue;
  278. }
  279. if (indicate-&gt;MACTION == 0x03)
  280. {
  281. if (HostActionResponse (plc))
  282. {
  283. return (-1);
  284. }
  285. close (plc-&gt;NVM.file);
  286. if (ReadFirmware1 (plc))
  287. {
  288. return (-1);
  289. }
  290. if ((plc-&gt;NVM.file = open (plc-&gt;NVM.name = plc-&gt;nvm.name, O_BINARY|O_RDONLY)) == -1)
  291. {
  292. error (1, errno, &quot;%s&quot;, plc-&gt;NVM.name);
  293. }
  294. close (plc-&gt;PIB.file);
  295. if (ReadParameters1 (plc))
  296. {
  297. return (-1);
  298. }
  299. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = plc-&gt;pib.name, O_BINARY|O_RDONLY)) == -1)
  300. {
  301. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  302. }
  303. if (ResetDevice (plc))
  304. {
  305. return (-1);
  306. }
  307. continue;
  308. }
  309. if (indicate-&gt;MACTION == 0x04)
  310. {
  311. #if 0
  312. /*
  313. * Due to an omission in the INT6300 BootLoader, responding to this VS_HOST_ACTION
  314. * indication will suppress subsequent VS_HOST_ACTION messages and the device will
  315. * not request firmware and parameters; this may be corrected on the INT6400;
  316. */
  317. if (HostActionResponse (plc))
  318. {
  319. return (-1);
  320. }
  321. #endif
  322. if (WriteCFG (plc))
  323. {
  324. return (-1);
  325. }
  326. /*
  327. * At this point, one could download firmware and parameters without waiting for
  328. * further requests from the device; however, we elect to wait for them since it
  329. * is 'good form'; a device should send code 0x00 within 10 seconds of this one;
  330. */
  331. continue;
  332. }
  333. if (indicate-&gt;MACTION == 0x05)
  334. {
  335. if (HostActionResponse (plc))
  336. {
  337. return (-1);
  338. }
  339. close (plc-&gt;NVM.file);
  340. if ((plc-&gt;NVM.file = open (plc-&gt;NVM.name = NVM, O_BINARY|O_RDONLY)) == -1)
  341. {
  342. error (1, errno, &quot;%s&quot;, plc-&gt;NVM.name);
  343. }
  344. close (plc-&gt;PIB.file);
  345. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = PIB, O_BINARY|O_RDONLY)) == -1)
  346. {
  347. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  348. }
  349. if (ResetDevice (plc))
  350. {
  351. return (-1);
  352. }
  353. continue;
  354. }
  355. error (0, ENOSYS, &quot;Host Action 0x%0X&quot;, indicate-&gt;MACTION);
  356. }
  357. }
  358. return (0);
  359. }
  360. #endif
  361. </pre>
  362. <div class='footerlink'>
  363. [<a href='EmulateHost64.c.html' title=' EmulateHost64.c '>PREV</a>]
  364. [<a href='toolkit.html' title=' Index '>HOME</a>]
  365. [<a href='error.c.html' title=' error.c '>NEXT</a>]
  366. </div>
  367. </body>
  368. </html>