PLCHostBoot.c.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. PLCHostBoot.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='plcget.c.html' title=' plcget.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='plchost.c.html' title=' plchost.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. /*====================================================================*&quot;
  62. *
  63. * PLCHostBoot.c -
  64. *
  65. *
  66. * Contributor(s):
  67. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  68. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  69. *
  70. *--------------------------------------------------------------------*/
  71. #ifndef PLCHOSTBOOT_SOURCE
  72. #define PLCHOSTBOOT_SOURCE
  73. #include &lt;unistd.h&gt;
  74. #include &lt;errno.h&gt;
  75. #include &lt;sys/socket.h&gt;
  76. #include &lt;sys/un.h&gt;
  77. #include &quot;../tools/error.h&quot;
  78. #include &quot;../tools/files.h&quot;
  79. #include &quot;../tools/flags.h&quot;
  80. #include &quot;../plc/plc.h&quot;
  81. #define MESSAGE &quot;Starting PLC Host Server\n&quot;
  82. #define SOCKETNAME &quot;/tmp/socket&quot;
  83. static bool done = false;
  84. void terminate (signo_t signal)
  85. {
  86. done = true;
  87. return;
  88. }
  89. static signed opensocket (char const * socketname)
  90. {
  91. signed fd;
  92. struct sockaddr_un sockaddr_un =
  93. {
  94. #ifdef __OpenBSD__
  95. 0,
  96. #endif
  97. AF_UNIX,
  98. &quot;/tmp/socket&quot;
  99. };
  100. strncpy (sockaddr_un.sun_path, socketname, sizeof (sockaddr_un.sun_path));
  101. #ifdef __OpenBSD__
  102. sockaddr_un.sun_len = SUN_LEN (&amp;sockaddr_un);
  103. #endif
  104. if (unlink (sockaddr_un.sun_path))
  105. {
  106. if (errno != ENOENT)
  107. {
  108. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  109. }
  110. }
  111. if ((fd = socket (AF_UNIX, SOCK_DGRAM, 0)) == -1)
  112. {
  113. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  114. }
  115. if (bind (fd, (struct sockaddr *)(&amp;sockaddr_un), sizeof (sockaddr_un)) == -1)
  116. {
  117. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  118. }
  119. if (chmod (sockaddr_un.sun_path, 0666) == -1)
  120. {
  121. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  122. }
  123. return (fd);
  124. }
  125. /*====================================================================*
  126. *
  127. * signed PLCHostBoot (struct plc * plc, char const * socket, signed timer);
  128. *
  129. * int6k.h
  130. *
  131. * wait indefinitely for VS_HOST_ACTION messages; service the device
  132. * only; it will stop dead - like a bug! - on error;
  133. *
  134. *
  135. * Contributor(s):
  136. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  137. *
  138. *--------------------------------------------------------------------*/
  139. signed PLCHostBoot (struct plc * plc, char const * socket, unsigned timer)
  140. {
  141. byte buffer [3000];
  142. struct plctopology * plctopology = (struct plctopology *)(buffer);
  143. struct channel * channel = (struct channel *)(plc-&gt;channel);
  144. struct message * message = (struct message *)(plc-&gt;message);
  145. #ifndef __GNUC__
  146. #pragma pack (push,1)
  147. #endif
  148. struct __packed vs_host_action_ind
  149. {
  150. struct ethernet_hdr ethernet;
  151. struct qualcomm_hdr intellon;
  152. uint8_t MACTION;
  153. uint8_t MAJOR_VERSION;
  154. uint8_t MINOR_VERSION;
  155. }
  156. * indicate = (struct vs_host_action_ind *) (message);
  157. #ifndef __GNUC__
  158. #pragma pack (pop)
  159. #endif
  160. signed fd = opensocket (socket);
  161. char const * NVM = plc-&gt;NVM.name;
  162. char const * PIB = plc-&gt;PIB.name;
  163. unsigned action;
  164. signed status;
  165. memset (buffer, 0, sizeof (buffer));
  166. write (fd, MESSAGE, strlen (MESSAGE));
  167. while (!done)
  168. {
  169. status = ReadMME (plc, 0, (VS_HOST_ACTION | MMTYPE_IND));
  170. if (status &lt; 0)
  171. {
  172. break;
  173. }
  174. if (status &lt; 1)
  175. {
  176. PLCTopology (channel, message, plctopology);
  177. PLCTopologyPrint (plctopology);
  178. continue;
  179. }
  180. action = indicate-&gt;MACTION;
  181. memcpy (channel-&gt;peer, indicate-&gt;ethernet.OSA, sizeof (channel-&gt;peer));
  182. if (HostActionResponse (plc))
  183. {
  184. return (-1);
  185. }
  186. if (action == 0x00)
  187. {
  188. if (BootDevice1 (plc))
  189. {
  190. return (-1);
  191. }
  192. if (_anyset (plc-&gt;flags, PLC_FLASH_DEVICE))
  193. {
  194. FlashDevice1 (plc);
  195. }
  196. continue;
  197. }
  198. if (action == 0x01)
  199. {
  200. close (plc-&gt;NVM.file);
  201. if (ReadFirmware1 (plc))
  202. {
  203. return (-1);
  204. }
  205. if ((plc-&gt;NVM.file = open (plc-&gt;NVM.name = plc-&gt;nvm.name, O_BINARY|O_RDONLY)) == -1)
  206. {
  207. error (1, errno, &quot;%s&quot;, plc-&gt;NVM.name);
  208. }
  209. if (ResetDevice (plc))
  210. {
  211. return (-1);
  212. }
  213. continue;
  214. }
  215. if (action == 0x02)
  216. {
  217. close (plc-&gt;PIB.file);
  218. if (ReadParameters (plc))
  219. {
  220. return (-1);
  221. }
  222. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = plc-&gt;pib.name, O_BINARY|O_RDONLY)) == -1)
  223. {
  224. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  225. }
  226. if (ResetDevice (plc))
  227. {
  228. return (-1);
  229. }
  230. continue;
  231. }
  232. if (action == 0x03)
  233. {
  234. close (plc-&gt;PIB.file);
  235. if (ReadParameters (plc))
  236. {
  237. return (-1);
  238. }
  239. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = plc-&gt;pib.name, O_BINARY|O_RDONLY)) == -1)
  240. {
  241. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  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 (action == 0x04)
  259. {
  260. if (InitDevice (plc))
  261. {
  262. return (-1);
  263. }
  264. continue;
  265. }
  266. if (action == 0x05)
  267. {
  268. close (plc-&gt;NVM.file);
  269. if ((plc-&gt;NVM.file = open (plc-&gt;NVM.name = NVM, O_BINARY|O_RDONLY)) == -1)
  270. {
  271. error (1, errno, &quot;%s&quot;, plc-&gt;NVM.name);
  272. }
  273. close (plc-&gt;PIB.file);
  274. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = PIB, O_BINARY|O_RDONLY)) == -1)
  275. {
  276. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  277. }
  278. if (ResetDevice (plc))
  279. {
  280. return (-1);
  281. }
  282. continue;
  283. }
  284. if (action == 0x06)
  285. {
  286. close (plc-&gt;PIB.file);
  287. if (ReadParameters (plc))
  288. {
  289. return (-1);
  290. }
  291. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = plc-&gt;pib.name, O_BINARY|O_RDONLY)) == -1)
  292. {
  293. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  294. }
  295. continue;
  296. }
  297. error (0, ENOSYS, &quot;Host Action 0x%02X&quot;, action);
  298. }
  299. close (fd);
  300. return (0);
  301. }
  302. #endif
  303. </pre>
  304. <div class='footerlink'>
  305. [<a href='plcget.c.html' title=' plcget.c '>PREV</a>]
  306. [<a href='toolkit.html' title=' Index '>HOME</a>]
  307. [<a href='plchost.c.html' title=' plchost.c '>NEXT</a>]
  308. </div>
  309. </body>
  310. </html>