plchostd.c.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. plchostd.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='plchostd2.c.html' title=' plchostd2.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='plcID.c.html' title=' plcID.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. * plchostd.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. /*====================================================================*&quot;
  72. * system header files;
  73. *--------------------------------------------------------------------*/
  74. #include &lt;unistd.h&gt;
  75. #include &lt;stdlib.h&gt;
  76. #include &lt;limits.h&gt;
  77. #include &lt;string.h&gt;
  78. #include &lt;ctype.h&gt;
  79. #include &lt;memory.h&gt;
  80. #include &lt;signal.h&gt;
  81. #include &lt;errno.h&gt;
  82. #include &lt;sys/socket.h&gt;
  83. #include &lt;sys/un.h&gt;
  84. /*====================================================================*
  85. * custom header files;
  86. *--------------------------------------------------------------------*/
  87. #include &quot;../ether/channel.h&quot;
  88. #include &quot;../tools/getoptv.h&quot;
  89. #include &quot;../tools/putoptv.h&quot;
  90. #include &quot;../tools/memory.h&quot;
  91. #include &quot;../tools/number.h&quot;
  92. #include &quot;../tools/types.h&quot;
  93. #include &quot;../tools/flags.h&quot;
  94. #include &quot;../tools/files.h&quot;
  95. #include &quot;../tools/error.h&quot;
  96. #include &quot;../ram/nvram.h&quot;
  97. #include &quot;../ram/sdram.h&quot;
  98. #include &quot;../nvm/nvm.h&quot;
  99. #include &quot;../pib/pib.h&quot;
  100. #include &quot;../plc/plc.h&quot;
  101. /*====================================================================*
  102. * custom source files;
  103. *--------------------------------------------------------------------*/
  104. #ifndef MAKEFILE
  105. #include &quot;../plc/BootDevice1.c&quot;
  106. #include &quot;../plc/BootDevice2.c&quot;
  107. #include &quot;../plc/BootFirmware1.c&quot;
  108. #include &quot;../plc/BootFirmware2.c&quot;
  109. #include &quot;../plc/BootParameters1.c&quot;
  110. #include &quot;../plc/BootParameters2.c&quot;
  111. #include &quot;../plc/chipset.c&quot;
  112. #include &quot;../plc/Confirm.c&quot;
  113. #include &quot;../plc/Devices.c&quot;
  114. #include &quot;../plc/ExecuteApplets.c&quot;
  115. #include &quot;../plc/ExecuteApplets1.c&quot;
  116. #include &quot;../plc/ExecuteApplets2.c&quot;
  117. #include &quot;../plc/Failure.c&quot;
  118. #include &quot;../plc/FlashDevice2.c&quot;
  119. #include &quot;../plc/FlashSoftloader.c&quot;
  120. #include &quot;../plc/FlashFirmware.c&quot;
  121. #include &quot;../plc/FlashParameters.c&quot;
  122. #include &quot;../plc/FlashNVM.c&quot;
  123. #include &quot;../plc/HostActionResponse.c&quot;
  124. #include &quot;../plc/InitDevice.c&quot;
  125. #include &quot;../plc/InitDevice1.c&quot;
  126. #include &quot;../plc/InitDevice2.c&quot;
  127. #include &quot;../plc/ModuleSpec.c&quot;
  128. #include &quot;../plc/ModuleSession.c&quot;
  129. #include &quot;../plc/ModuleWrite.c&quot;
  130. #include &quot;../plc/ModuleCommit.c&quot;
  131. #include &quot;../plc/NVMSelect.c&quot;
  132. #include &quot;../plc/Request.c&quot;
  133. #include &quot;../plc/ResetDevice.c&quot;
  134. #include &quot;../plc/ReadMME.c&quot;
  135. #include &quot;../plc/SendMME.c&quot;
  136. #include &quot;../plc/ReadFirmware1.c&quot;
  137. #include &quot;../plc/ReadParameters.c&quot;
  138. #include &quot;../plc/ReadParameters1.c&quot;
  139. #include &quot;../plc/ReadParameters2.c&quot;
  140. #include &quot;../plc/LocalDevices.c&quot;
  141. #include &quot;../plc/WaitForReset.c&quot;
  142. #include &quot;../plc/WaitForStart.c&quot;
  143. #include &quot;../plc/WaitForRestart.c&quot;
  144. #include &quot;../plc/WriteMEM.c&quot;
  145. #include &quot;../plc/WriteNVM.c&quot;
  146. #include &quot;../plc/WritePIB.c&quot;
  147. #include &quot;../plc/WriteExecuteApplet2.c&quot;
  148. #include &quot;../plc/WriteExecuteFirmware.c&quot;
  149. #include &quot;../plc/WriteExecuteFirmware1.c&quot;
  150. #include &quot;../plc/WriteExecuteFirmware2.c&quot;
  151. #include &quot;../plc/WriteExecuteParameters.c&quot;
  152. #include &quot;../plc/WriteExecuteParameters1.c&quot;
  153. #include &quot;../plc/WriteExecuteParameters2.c&quot;
  154. #include &quot;../plc/WriteExecutePIB.c&quot;
  155. #include &quot;../plc/Platform.c&quot;
  156. #include &quot;../plc/PLCTopology.c&quot;
  157. #include &quot;../plc/PLCTopologyPrint.c&quot;
  158. #include &quot;../plc/WriteFirmware1.c&quot;
  159. #include &quot;../plc/WriteFirmware2.c&quot;
  160. #include &quot;../plc/StartFirmware1.c&quot;
  161. #include &quot;../plc/StartFirmware2.c&quot;
  162. #include &quot;../plc/PLCSelect.c&quot;
  163. #include &quot;../plc/ModuleRead.c&quot;
  164. #endif
  165. #ifndef MAKEFILE
  166. #include &quot;../tools/getoptv.c&quot;
  167. #include &quot;../tools/putoptv.c&quot;
  168. #include &quot;../tools/version.c&quot;
  169. #include &quot;../tools/uintspec.c&quot;
  170. #include &quot;../tools/checkfilename.c&quot;
  171. #include &quot;../tools/hexdecode.c&quot;
  172. #include &quot;../tools/hexstring.c&quot;
  173. #include &quot;../tools/todigit.c&quot;
  174. #include &quot;../tools/hexdump.c&quot;
  175. #include &quot;../tools/checksum32.c&quot;
  176. #include &quot;../tools/fdchecksum32.c&quot;
  177. #include &quot;../tools/error.c&quot;
  178. #include &quot;../tools/strfbits.c&quot;
  179. #include &quot;../tools/typename.c&quot;
  180. #endif
  181. #ifndef MAKEFILE
  182. #include &quot;../ether/openchannel.c&quot;
  183. #include &quot;../ether/closechannel.c&quot;
  184. #include &quot;../ether/readpacket.c&quot;
  185. #include &quot;../ether/sendpacket.c&quot;
  186. #include &quot;../ether/channel.c&quot;
  187. #endif
  188. #ifndef MAKEFILE
  189. #include &quot;../mme/EthernetHeader.c&quot;
  190. #include &quot;../mme/QualcommHeader.c&quot;
  191. #include &quot;../mme/UnwantedMessage.c&quot;
  192. #include &quot;../mme/MMECode.c&quot;
  193. #ifdef AR7x00
  194. #include &quot;../mme/QualcommHeader1.c&quot;
  195. #endif
  196. #endif
  197. #ifndef MAKEFILE
  198. #include &quot;../key/keys.c&quot;
  199. #endif
  200. #ifndef MAKEFILE
  201. #include &quot;../nvm/nvmfile.c&quot;
  202. #include &quot;../nvm/nvmfile1.c&quot;
  203. #include &quot;../nvm/nvmfile2.c&quot;
  204. #endif
  205. #ifndef MAKEFILE
  206. #include &quot;../pib/pibfile.c&quot;
  207. #include &quot;../pib/pibfile1.c&quot;
  208. #include &quot;../pib/pibfile2.c&quot;
  209. #include &quot;../pib/pibpeek1.c&quot;
  210. #endif
  211. /*====================================================================*
  212. * program constants;
  213. *--------------------------------------------------------------------*/
  214. #define MESSAGE &quot;Starting PLC Host Server\n&quot;
  215. #define SOCKETNAME &quot;/tmp/socket&quot;
  216. /*====================================================================*
  217. * program variables;
  218. *--------------------------------------------------------------------*/
  219. static bool done = false;
  220. /*====================================================================*
  221. *
  222. * void terminate (signo_t signal);
  223. *
  224. *
  225. *
  226. * Contributor(s):
  227. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  228. *
  229. *--------------------------------------------------------------------*/
  230. static void terminate (signo_t signal)
  231. {
  232. done = true;
  233. return;
  234. }
  235. /*====================================================================*
  236. *
  237. * signed opensocket (char const * socketname);
  238. *
  239. *
  240. *
  241. * Contributor(s):
  242. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  243. *
  244. *--------------------------------------------------------------------*/
  245. static signed opensocket (char const * socketname)
  246. {
  247. signed fd;
  248. struct sockaddr_un sockaddr_un =
  249. {
  250. #if defined (__OpenBSD__) || defined (__APPLE__)
  251. 0,
  252. #endif
  253. AF_UNIX,
  254. &quot;/tmp/socket&quot;
  255. };
  256. strncpy (sockaddr_un.sun_path, socketname, sizeof (sockaddr_un.sun_path));
  257. #if defined (__OpenBSD__) || defined (__APPLE__)
  258. sockaddr_un.sun_len = SUN_LEN (&amp;sockaddr_un);
  259. #endif
  260. if (unlink (sockaddr_un.sun_path))
  261. {
  262. if (errno != ENOENT)
  263. {
  264. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  265. }
  266. }
  267. if ((fd = socket (AF_UNIX, SOCK_DGRAM, 0)) == -1)
  268. {
  269. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  270. }
  271. if (bind (fd, (struct sockaddr *)(&amp;sockaddr_un), sizeof (sockaddr_un)) == -1)
  272. {
  273. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  274. }
  275. if (chmod (sockaddr_un.sun_path, 0666) == -1)
  276. {
  277. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  278. }
  279. return (fd);
  280. }
  281. /*====================================================================*
  282. *
  283. * signed function (struct plc * plc, char const * socket);
  284. *
  285. * wait indefinitely for VS_HOST_ACTION messages; service the device
  286. * only; it will stop dead - like a bug! - on error;
  287. *
  288. *
  289. * Contributor(s):
  290. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  291. *
  292. *--------------------------------------------------------------------*/
  293. signed function (struct plc * plc, char const * socket)
  294. {
  295. struct channel * channel = (struct channel *)(plc-&gt;channel);
  296. struct message * message = (struct message *)(plc-&gt;message);
  297. #ifndef __GNUC__
  298. #pragma pack (push,1)
  299. #endif
  300. struct __packed vs_host_action_ind
  301. {
  302. struct ethernet_hdr ethernet;
  303. struct qualcomm_hdr qualcomm;
  304. uint8_t MACTION;
  305. uint8_t MAJOR_VERSION;
  306. uint8_t MINOR_VERSION;
  307. }
  308. * indicate = (struct vs_host_action_ind *) (message);
  309. #ifndef __GNUC__
  310. #pragma pack (pop)
  311. #endif
  312. byte buffer [3000];
  313. struct plctopology * plctopology = (struct plctopology *)(buffer);
  314. signed fd = opensocket (socket);
  315. char const * FactoryNVM = plc-&gt;NVM.name;
  316. char const * FactoryPIB = plc-&gt;PIB.name;
  317. signed action;
  318. signed status;
  319. memset (buffer, 0, sizeof (buffer));
  320. write (fd, MESSAGE, strlen (MESSAGE));
  321. while (!done)
  322. {
  323. status = ReadMME (plc, 0, (VS_HOST_ACTION | MMTYPE_IND));
  324. if (status &lt; 0)
  325. {
  326. break;
  327. }
  328. if (status &lt; 1)
  329. {
  330. PLCTopology (channel, message, plctopology);
  331. PLCTopologyPrint (plctopology);
  332. continue;
  333. }
  334. action = indicate-&gt;MACTION;
  335. memcpy (channel-&gt;peer, indicate-&gt;ethernet.OSA, sizeof (channel-&gt;peer));
  336. if (HostActionResponse (plc))
  337. {
  338. return (-1);
  339. }
  340. if (action == 0x00)
  341. {
  342. if (BootDevice2 (plc))
  343. {
  344. return (-1);
  345. }
  346. if (_anyset (plc-&gt;flags, PLC_FLASH_DEVICE))
  347. {
  348. FlashDevice2 (plc, (PLC_COMMIT_FORCE | PLC_COMMIT_NORESET | PLC_COMMIT_FACTPIB));
  349. }
  350. continue;
  351. }
  352. if (action == 0x01)
  353. {
  354. close (plc-&gt;NVM.file);
  355. if (ReadFirmware1 (plc))
  356. {
  357. return (-1);
  358. }
  359. if ((plc-&gt;NVM.file = open (plc-&gt;NVM.name = plc-&gt;nvm.name, O_BINARY|O_RDONLY)) == -1)
  360. {
  361. error (1, errno, &quot;%s&quot;, plc-&gt;NVM.name);
  362. }
  363. if (ResetDevice (plc))
  364. {
  365. return (-1);
  366. }
  367. continue;
  368. }
  369. if (action == 0x02)
  370. {
  371. close (plc-&gt;PIB.file);
  372. if (ReadParameters (plc))
  373. {
  374. return (-1);
  375. }
  376. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = plc-&gt;pib.name, O_BINARY|O_RDONLY)) == -1)
  377. {
  378. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  379. }
  380. if (ResetDevice (plc))
  381. {
  382. return (-1);
  383. }
  384. continue;
  385. }
  386. if (action == 0x03)
  387. {
  388. close (plc-&gt;PIB.file);
  389. if (ReadParameters (plc))
  390. {
  391. return (-1);
  392. }
  393. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = plc-&gt;pib.name, O_BINARY|O_RDONLY)) == -1)
  394. {
  395. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  396. }
  397. close (plc-&gt;NVM.file);
  398. if (ReadFirmware1 (plc))
  399. {
  400. return (-1);
  401. }
  402. if ((plc-&gt;NVM.file = open (plc-&gt;NVM.name = plc-&gt;nvm.name, O_BINARY|O_RDONLY)) == -1)
  403. {
  404. error (1, errno, &quot;%s&quot;, plc-&gt;NVM.name);
  405. }
  406. if (ResetDevice (plc))
  407. {
  408. return (-1);
  409. }
  410. continue;
  411. }
  412. if (action == 0x04)
  413. {
  414. if (InitDevice (plc))
  415. {
  416. return (-1);
  417. }
  418. continue;
  419. }
  420. if (action == 0x05)
  421. {
  422. close (plc-&gt;NVM.file);
  423. if ((plc-&gt;NVM.file = open (plc-&gt;NVM.name = FactoryNVM, O_BINARY|O_RDONLY)) == -1)
  424. {
  425. error (1, errno, &quot;%s&quot;, plc-&gt;NVM.name);
  426. }
  427. close (plc-&gt;PIB.file);
  428. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = FactoryPIB, O_BINARY|O_RDONLY)) == -1)
  429. {
  430. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  431. }
  432. if (ResetDevice (plc))
  433. {
  434. return (-1);
  435. }
  436. continue;
  437. }
  438. if (action == 0x06)
  439. {
  440. close (plc-&gt;PIB.file);
  441. if (ReadParameters (plc))
  442. {
  443. return (-1);
  444. }
  445. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = plc-&gt;pib.name, O_BINARY|O_RDONLY)) == -1)
  446. {
  447. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  448. }
  449. continue;
  450. }
  451. error (0, ENOSYS, &quot;Host Action 0x%02X&quot;, action);
  452. }
  453. close (fd);
  454. return (0);
  455. }
  456. /*====================================================================*
  457. *
  458. * int main (int argc, char const * argv[]);
  459. *
  460. * parse command line, populate plc structure and perform selected
  461. * operations; show help summary when asked; see getoptv and putoptv
  462. * to understand command line parsing and help summary display; see
  463. * plc.h for the definition of struct plc;
  464. *
  465. *
  466. *--------------------------------------------------------------------*/
  467. int main (int argc, char const * argv [])
  468. {
  469. extern struct channel channel;
  470. extern void terminate (signo_t);
  471. static char const * optv [] =
  472. {
  473. &quot;dFi:n:N:p:P:qs:S:t:vw:x&quot;,
  474. &quot;-N file -P file [-S file] [-n file] [-p file]&quot;,
  475. &quot;Qualcomm Atheros PLC Host Daemon&quot;,
  476. &quot;d\texecute in background as daemon&quot;,
  477. #if defined (WINPCAP) || defined (LIBPCAP)
  478. &quot;i n\thost interface is (n) [&quot; LITERAL (CHANNEL_ETHNUMBER) &quot;]&quot;,
  479. #else
  480. &quot;i s\thost interface is (s) [&quot; LITERAL (CHANNEL_ETHDEVICE) &quot;]&quot;,
  481. #endif
  482. &quot;n f\tread firmware from device into file (f)&quot;,
  483. &quot;N f\tfirmware file is (f)&quot;,
  484. &quot;p f\tread parameters from device into file (f)&quot;,
  485. &quot;P f\tparameter file is (f)&quot;,
  486. &quot;q\tquiet mode&quot;,
  487. &quot;s f\tbroadcast event status on socket (f) [&quot; SOCKETNAME &quot;]&quot;,
  488. &quot;S f\tsoftloader file is (f)&quot;,
  489. &quot;t n\tread timeout is (n) milliseconds [&quot; LITERAL (CHANNEL_TIMEOUT) &quot;]&quot;,
  490. &quot;v\tverbose mode&quot;,
  491. &quot;w n\twakeup every (n) seconds [&quot; LITERAL (PLC_LONGTIME) &quot;]&quot;,
  492. &quot;x\texit on error&quot;,
  493. (char const *) (0)
  494. };
  495. #include &quot;../plc/plc.c&quot;
  496. struct sigaction sa;
  497. char const * socketname = SOCKETNAME;
  498. signed c;
  499. if (getenv (PLCDEVICE))
  500. {
  501. #if defined (WINPCAP) || defined (LIBPCAP)
  502. channel.ifindex = atoi (getenv (PLCDEVICE));
  503. #else
  504. channel.ifname = strdup (getenv (PLCDEVICE));
  505. #endif
  506. }
  507. optind = 1;
  508. plc.timer = PLC_LONGTIME;
  509. while ((c = getoptv (argc, argv, optv)) != -1)
  510. {
  511. switch (c)
  512. {
  513. case 'd':
  514. _setbits (plc.flags, PLC_DAEMON);
  515. break;
  516. case 'i':
  517. #if defined (WINPCAP) || defined (LIBPCAP)
  518. channel.ifindex = atoi (optarg);
  519. #else
  520. channel.ifname = optarg;
  521. #endif
  522. break;
  523. case 'N':
  524. if (!checkfilename (optarg))
  525. {
  526. error (1, EINVAL, &quot;%s&quot;, optarg);
  527. }
  528. if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1)
  529. {
  530. error (1, errno, &quot;%s&quot;, plc.NVM.name);
  531. }
  532. if (nvmfile (&amp;plc.NVM))
  533. {
  534. error (1, errno, &quot;Bad firmware file: %s&quot;, plc.NVM.name);
  535. }
  536. _setbits (plc.flags, PLC_WRITE_MAC);
  537. break;
  538. case 'n':
  539. if (!checkfilename (optarg))
  540. {
  541. error (1, EINVAL, &quot;%s&quot;, optarg);
  542. }
  543. if ((plc.nvm.file = open (plc.nvm.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  544. {
  545. error (1, errno, &quot;%s&quot;, plc.nvm.name);
  546. }
  547. break;
  548. case 'P':
  549. if (!checkfilename (optarg))
  550. {
  551. error (1, EINVAL, &quot;%s&quot;, optarg);
  552. }
  553. if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1)
  554. {
  555. error (1, errno, &quot;%s&quot;, plc.PIB.name);
  556. }
  557. if (pibfile (&amp;plc.PIB))
  558. {
  559. error (1, errno, &quot;Bad parameter file: %s&quot;, plc.PIB.name);
  560. }
  561. _setbits (plc.flags, PLC_WRITE_PIB);
  562. break;
  563. case 'p':
  564. if (!checkfilename (optarg))
  565. {
  566. error (1, EINVAL, &quot;%s&quot;, optarg);
  567. }
  568. if ((plc.pib.file = open (plc.pib.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  569. {
  570. error (1, errno, &quot;%s&quot;, plc.pib.name);
  571. }
  572. break;
  573. case 'q':
  574. _setbits (channel.flags, CHANNEL_SILENCE);
  575. _setbits (plc.flags, PLC_SILENCE);
  576. break;
  577. case 's':
  578. break;
  579. case 'S':
  580. if (!checkfilename (optarg))
  581. {
  582. error (1, EINVAL, &quot;%s&quot;, optarg);
  583. }
  584. if ((plc.CFG.file = open (plc.CFG.name = optarg, O_BINARY|O_RDONLY)) == -1)
  585. {
  586. error (1, errno, &quot;%s&quot;, plc.CFG.name);
  587. }
  588. if (nvmfile (&amp;plc.CFG))
  589. {
  590. error (1, errno, &quot;Bad firmware file: %s&quot;, plc.CFG.name);
  591. }
  592. _setbits (plc.flags, PLC_FLASH_DEVICE);
  593. break;
  594. case 't':
  595. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  596. break;
  597. case 'v':
  598. _setbits (channel.flags, CHANNEL_VERBOSE);
  599. _setbits (plc.flags, PLC_VERBOSE);
  600. break;
  601. case 'w':
  602. plc.timer = (unsigned)(uintspec (optarg, 1, 3600));
  603. break;
  604. case 'x':
  605. _setbits (plc.flags, PLC_BAILOUT);
  606. break;
  607. default:
  608. break;
  609. }
  610. }
  611. argc -= optind;
  612. argv += optind;
  613. if (argc)
  614. {
  615. error (1, ENOTSUP, ERROR_TOOMANY);
  616. }
  617. if (plc.NVM.file == -1)
  618. {
  619. error (1, ECANCELED, &quot;No host NVM file named&quot;);
  620. }
  621. if (plc.PIB.file == -1)
  622. {
  623. error (1, ECANCELED, &quot;No host PIB file named&quot;);
  624. }
  625. if (plc.nvm.file == -1)
  626. {
  627. error (1, ECANCELED, &quot;No user NVM file named&quot;);
  628. }
  629. if (plc.pib.file == -1)
  630. {
  631. error (1, ECANCELED, &quot;No user PIB file named&quot;);
  632. }
  633. if (plc.CFG.file == -1)
  634. {
  635. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  636. {
  637. error (1, ECANCELED, &quot;No Softloader file named&quot;);
  638. }
  639. }
  640. #ifndef WIN32
  641. if (_anyset (plc.flags, PLC_DAEMON))
  642. {
  643. pid_t pid = fork ();
  644. if (pid &lt; 0)
  645. {
  646. error (1, errno, &quot;Can't start daemon&quot;);
  647. }
  648. if (pid &gt; 0)
  649. {
  650. exit (0);
  651. }
  652. }
  653. memset (&amp;sa, 0, sizeof (struct sigaction));
  654. sa.sa_handler = terminate;
  655. sigaction (SIGTERM, &amp;sa, (struct sigaction *)(0));
  656. sigaction (SIGQUIT, &amp;sa, (struct sigaction *)(0));
  657. sigaction (SIGTSTP, &amp;sa, (struct sigaction *)(0));
  658. sigaction (SIGINT, &amp;sa, (struct sigaction *)(0));
  659. sigaction (SIGHUP, &amp;sa, (struct sigaction *)(0));
  660. #endif
  661. openchannel (&amp;channel);
  662. if (!(plc.message = malloc (sizeof (* plc.message))))
  663. {
  664. error (1, errno, PLC_NOMEMORY);
  665. }
  666. function (&amp;plc, socketname);
  667. free (plc.message);
  668. closechannel (&amp;channel);
  669. exit (0);
  670. }
  671. </pre>
  672. <div class='footerlink'>
  673. [<a href='plchostd2.c.html' title=' plchostd2.c '>PREV</a>]
  674. [<a href='toolkit.html' title=' Index '>HOME</a>]
  675. [<a href='plcID.c.html' title=' plcID.c '>NEXT</a>]
  676. </div>
  677. </body>
  678. </html>