plchostd2.c.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. plchostd2.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='plchost.c.html' title=' plchost.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='plchostd.c.html' title=' plchostd.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. * Copyright (c) 2014 I2SE GmbH
  25. *
  26. * All rights reserved.
  27. *
  28. * Redistribution and use in source and binary forms, with or
  29. * without modification, are permitted (subject to the limitations
  30. * in the disclaimer below) provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. *
  36. * * Redistributions in binary form must reproduce the above
  37. * copyright notice, this list of conditions and the following
  38. * disclaimer in the documentation and/or other materials
  39. * provided with the distribution.
  40. *
  41. * * Neither the name of Qualcomm Atheros nor the names of
  42. * its contributors may be used to endorse or promote products
  43. * derived from this software without specific prior written
  44. * permission.
  45. *
  46. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  47. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  48. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  49. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  50. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  52. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  54. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  55. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  56. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  57. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  58. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  59. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. *
  61. *--------------------------------------------------------------------*/
  62. /*====================================================================*&quot;
  63. *
  64. * plchostd2.c -
  65. *
  66. *
  67. * Contributor(s):
  68. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  69. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  70. * Stefan Wahren &lt;stefan.wahren@i2se.com&gt;
  71. *
  72. *--------------------------------------------------------------------*/
  73. /*====================================================================*&quot;
  74. * system header files;
  75. *--------------------------------------------------------------------*/
  76. #include &lt;unistd.h&gt;
  77. #include &lt;stdlib.h&gt;
  78. #include &lt;limits.h&gt;
  79. #include &lt;string.h&gt;
  80. #include &lt;ctype.h&gt;
  81. #include &lt;memory.h&gt;
  82. #include &lt;signal.h&gt;
  83. #include &lt;errno.h&gt;
  84. #include &lt;sys/socket.h&gt;
  85. #include &lt;sys/un.h&gt;
  86. /*====================================================================*
  87. * custom header files;
  88. *--------------------------------------------------------------------*/
  89. #include &quot;../ether/channel.h&quot;
  90. #include &quot;../tools/getoptv.h&quot;
  91. #include &quot;../tools/putoptv.h&quot;
  92. #include &quot;../tools/memory.h&quot;
  93. #include &quot;../tools/number.h&quot;
  94. #include &quot;../tools/types.h&quot;
  95. #include &quot;../tools/flags.h&quot;
  96. #include &quot;../tools/files.h&quot;
  97. #include &quot;../tools/error.h&quot;
  98. #include &quot;../ram/nvram.h&quot;
  99. #include &quot;../ram/sdram.h&quot;
  100. #include &quot;../nvm/nvm.h&quot;
  101. #include &quot;../pib/pib.h&quot;
  102. #include &quot;../plc/plc.h&quot;
  103. /*====================================================================*
  104. * custom source files;
  105. *--------------------------------------------------------------------*/
  106. #ifndef MAKEFILE
  107. #include &quot;../plc/BootDevice2.c&quot;
  108. #include &quot;../plc/BootFirmware2.c&quot;
  109. #include &quot;../plc/BootParameters2.c&quot;
  110. #include &quot;../plc/chipset.c&quot;
  111. #include &quot;../plc/Confirm.c&quot;
  112. #include &quot;../plc/Devices.c&quot;
  113. #include &quot;../plc/Failure.c&quot;
  114. #include &quot;../plc/FlashDevice2.c&quot;
  115. #include &quot;../plc/FlashSoftloader.c&quot;
  116. #include &quot;../plc/FlashFirmware.c&quot;
  117. #include &quot;../plc/FlashParameters.c&quot;
  118. #include &quot;../plc/FlashNVM.c&quot;
  119. #include &quot;../plc/HostActionResponse.c&quot;
  120. #include &quot;../plc/InitDevice2.c&quot;
  121. #include &quot;../plc/ModuleRead.c&quot;
  122. #include &quot;../plc/ModuleSpec.c&quot;
  123. #include &quot;../plc/ModuleSession.c&quot;
  124. #include &quot;../plc/ModuleWrite.c&quot;
  125. #include &quot;../plc/ModuleCommit.c&quot;
  126. #include &quot;../plc/LocalDevices.c&quot;
  127. #include &quot;../plc/NVMSelect.c&quot;
  128. #include &quot;../plc/ReadFirmware2.c&quot;
  129. #include &quot;../plc/ReadParameters2.c&quot;
  130. #include &quot;../plc/Request.c&quot;
  131. #include &quot;../plc/ResetDevice.c&quot;
  132. #include &quot;../plc/ReadMME.c&quot;
  133. #include &quot;../plc/SendMME.c&quot;
  134. #include &quot;../plc/WaitForReset.c&quot;
  135. #include &quot;../plc/WaitForStart.c&quot;
  136. #include &quot;../plc/WriteMEM.c&quot;
  137. #include &quot;../plc/WriteNVM.c&quot;
  138. #include &quot;../plc/WritePIB.c&quot;
  139. #include &quot;../plc/WriteExecuteFirmware2.c&quot;
  140. #include &quot;../plc/WriteExecuteParameters2.c&quot;
  141. #include &quot;../plc/WriteExecuteApplet2.c&quot;
  142. #include &quot;../plc/Platform.c&quot;
  143. #include &quot;../plc/WriteFirmware2.c&quot;
  144. #include &quot;../plc/PLCSelect.c&quot;
  145. #endif
  146. #ifndef MAKEFILE
  147. #include &quot;../tools/getoptv.c&quot;
  148. #include &quot;../tools/putoptv.c&quot;
  149. #include &quot;../tools/version.c&quot;
  150. #include &quot;../tools/uintspec.c&quot;
  151. #include &quot;../tools/checkfilename.c&quot;
  152. #include &quot;../tools/hexdecode.c&quot;
  153. #include &quot;../tools/hexstring.c&quot;
  154. #include &quot;../tools/todigit.c&quot;
  155. #include &quot;../tools/hexdump.c&quot;
  156. #include &quot;../tools/checksum32.c&quot;
  157. #include &quot;../tools/fdchecksum32.c&quot;
  158. #include &quot;../tools/error.c&quot;
  159. #include &quot;../tools/strfbits.c&quot;
  160. #include &quot;../tools/typename.c&quot;
  161. #endif
  162. #ifndef MAKEFILE
  163. #include &quot;../ether/openchannel.c&quot;
  164. #include &quot;../ether/closechannel.c&quot;
  165. #include &quot;../ether/readpacket.c&quot;
  166. #include &quot;../ether/sendpacket.c&quot;
  167. #include &quot;../ether/channel.c&quot;
  168. #endif
  169. #ifndef MAKEFILE
  170. #include &quot;../mme/EthernetHeader.c&quot;
  171. #include &quot;../mme/QualcommHeader.c&quot;
  172. #include &quot;../mme/UnwantedMessage.c&quot;
  173. #include &quot;../mme/MMECode.c&quot;
  174. #ifdef AR7x00
  175. #include &quot;../mme/QualcommHeader1.c&quot;
  176. #endif
  177. #endif
  178. #ifndef MAKEFILE
  179. #include &quot;../key/keys.c&quot;
  180. #endif
  181. #ifndef MAKEFILE
  182. #include &quot;../nvm/nvmfile2.c&quot;
  183. #endif
  184. #ifndef MAKEFILE
  185. #include &quot;../pib/pibfile2.c&quot;
  186. #include &quot;../pib/pibpeek2.c&quot;
  187. #endif
  188. /*====================================================================*
  189. * program constants;
  190. *--------------------------------------------------------------------*/
  191. #define MESSAGE &quot;Starting PLC Host Server\n&quot;
  192. #define SOCKETNAME &quot;/tmp/socket&quot;
  193. #define NEWPIB &quot;user.pib&quot;
  194. #define NEWNVM &quot;user.nvm&quot;
  195. /*====================================================================*
  196. * program variables;
  197. *--------------------------------------------------------------------*/
  198. static bool done = false;
  199. /*====================================================================*
  200. *
  201. * void terminate (signo_t signal);
  202. *
  203. *
  204. *
  205. * Contributor(s):
  206. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  207. *
  208. *--------------------------------------------------------------------*/
  209. static void terminate (signo_t signal)
  210. {
  211. done = true;
  212. return;
  213. }
  214. /*====================================================================*
  215. *
  216. * signed opensocket (char const * socketname);
  217. *
  218. *
  219. *
  220. * Contributor(s):
  221. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  222. *
  223. *--------------------------------------------------------------------*/
  224. static signed opensocket (char const * socketname)
  225. {
  226. signed fd;
  227. struct sockaddr_un sockaddr_un =
  228. {
  229. #if defined (__OpenBSD__) || defined (__APPLE__)
  230. 0,
  231. #endif
  232. AF_UNIX,
  233. &quot;/tmp/socket&quot;
  234. };
  235. strncpy (sockaddr_un.sun_path, socketname, sizeof (sockaddr_un.sun_path));
  236. #if defined (__OpenBSD__) || defined (__APPLE__)
  237. sockaddr_un.sun_len = SUN_LEN (&amp;sockaddr_un);
  238. #endif
  239. if (unlink (sockaddr_un.sun_path))
  240. {
  241. if (errno != ENOENT)
  242. {
  243. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  244. }
  245. }
  246. if ((fd = socket (AF_UNIX, SOCK_DGRAM, 0)) == -1)
  247. {
  248. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  249. }
  250. if (bind (fd, (struct sockaddr *)(&amp;sockaddr_un), sizeof (sockaddr_un)) == -1)
  251. {
  252. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  253. }
  254. if (chmod (sockaddr_un.sun_path, 0666) == -1)
  255. {
  256. error (1, errno, &quot;%s&quot;, sockaddr_un.sun_path);
  257. }
  258. return (fd);
  259. }
  260. /*====================================================================*
  261. *
  262. * signed function (struct plc * plc, char const * socket);
  263. *
  264. * wait indefinitely for VS_HOST_ACTION messages; service the device
  265. * only; it will stop dead - like a bug! - on error;
  266. *
  267. *
  268. * Contributor(s):
  269. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  270. *
  271. *--------------------------------------------------------------------*/
  272. signed function (struct plc * plc, char const * socket)
  273. {
  274. struct channel * channel = (struct channel *)(plc-&gt;channel);
  275. struct message * message = (struct message *)(plc-&gt;message);
  276. static char const * actions [] =
  277. {
  278. &quot;start device&quot;,
  279. &quot;store firmware&quot;,
  280. &quot;store parameters&quot;,
  281. &quot;update host&quot;,
  282. &quot;config memory&quot;,
  283. &quot;restore defaults&quot;,
  284. &quot;unknown&quot;
  285. };
  286. #ifndef __GNUC__
  287. #pragma pack (push,1)
  288. #endif
  289. struct __packed vs_host_action_ind
  290. {
  291. struct ethernet_hdr ethernet;
  292. struct qualcomm_hdr qualcomm;
  293. uint8_t MACTION;
  294. uint8_t MAJOR_VERSION;
  295. uint8_t MINOR_VERSION;
  296. }
  297. * indicate = (struct vs_host_action_ind *) (message);
  298. #ifndef __GNUC__
  299. #pragma pack (pop)
  300. #endif
  301. signed fd = opensocket (socket);
  302. char firmware [PLC_VERSION_STRING];
  303. char const * FactoryNVM = plc-&gt;NVM.name;
  304. char const * FactoryPIB = plc-&gt;PIB.name;
  305. signed action;
  306. signed status;
  307. write (fd, MESSAGE, strlen (MESSAGE));
  308. while (!done)
  309. {
  310. status = ReadMME (plc, 0, (VS_HOST_ACTION | MMTYPE_IND));
  311. if (status &lt; 0)
  312. {
  313. break;
  314. }
  315. if (status &lt; 1)
  316. {
  317. continue;
  318. }
  319. action = indicate-&gt;MACTION;
  320. if (action &lt; SIZEOF (actions))
  321. {
  322. fprintf(stderr, &quot;plchostd2: action = %s\n&quot;, actions[action]);
  323. }
  324. else
  325. {
  326. fprintf(stderr, &quot;plchostd2: action = %d\n&quot;, action);
  327. }
  328. memcpy (channel-&gt;peer, indicate-&gt;ethernet.OSA, sizeof (channel-&gt;peer));
  329. if (HostActionResponse (plc))
  330. {
  331. return (-1);
  332. }
  333. if (action == 0x00)
  334. {
  335. if (BootDevice2 (plc))
  336. {
  337. return (-1);
  338. }
  339. if (WaitForStart (plc, firmware, sizeof (firmware)))
  340. {
  341. return (-1);
  342. }
  343. if (_anyset (plc-&gt;flags, PLC_FLASH_DEVICE))
  344. {
  345. FlashDevice2 (plc, (PLC_COMMIT_FORCE | PLC_COMMIT_NORESET | PLC_COMMIT_FACTPIB));
  346. }
  347. continue;
  348. }
  349. if (action == 0x01)
  350. {
  351. close (plc-&gt;NVM.file);
  352. if (ReadFirmware2 (plc))
  353. {
  354. return (-1);
  355. }
  356. if ((plc-&gt;NVM.file = open (plc-&gt;NVM.name = plc-&gt;nvm.name, O_BINARY|O_RDONLY)) == -1)
  357. {
  358. error (1, errno, &quot;%s&quot;, plc-&gt;NVM.name);
  359. }
  360. continue;
  361. }
  362. if (action == 0x02)
  363. {
  364. close (plc-&gt;PIB.file);
  365. if (ReadParameters2 (plc))
  366. {
  367. return (-1);
  368. }
  369. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = plc-&gt;pib.name, O_BINARY|O_RDONLY)) == -1)
  370. {
  371. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  372. }
  373. if (ResetDevice (plc))
  374. {
  375. return (-1);
  376. }
  377. continue;
  378. }
  379. if (action == 0x03)
  380. {
  381. close (plc-&gt;PIB.file);
  382. if (ReadParameters2 (plc))
  383. {
  384. return (-1);
  385. }
  386. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = plc-&gt;pib.name, O_BINARY|O_RDONLY)) == -1)
  387. {
  388. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  389. }
  390. close (plc-&gt;NVM.file);
  391. if (ReadFirmware2 (plc))
  392. {
  393. return (-1);
  394. }
  395. if ((plc-&gt;NVM.file = open (plc-&gt;NVM.name = plc-&gt;nvm.name, O_BINARY|O_RDONLY)) == -1)
  396. {
  397. error (1, errno, &quot;%s&quot;, plc-&gt;NVM.name);
  398. }
  399. if (ResetDevice (plc))
  400. {
  401. return (-1);
  402. }
  403. continue;
  404. }
  405. if (action == 0x04)
  406. {
  407. if (InitDevice2 (plc))
  408. {
  409. return (-1);
  410. }
  411. continue;
  412. }
  413. if (action == 0x05)
  414. {
  415. close (plc-&gt;NVM.file);
  416. if ((plc-&gt;NVM.file = open (plc-&gt;NVM.name = FactoryNVM, O_BINARY|O_RDONLY)) == -1)
  417. {
  418. error (1, errno, &quot;%s&quot;, plc-&gt;NVM.name);
  419. }
  420. close (plc-&gt;PIB.file);
  421. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = FactoryPIB, O_BINARY|O_RDONLY)) == -1)
  422. {
  423. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  424. }
  425. if (ResetDevice (plc))
  426. {
  427. return (-1);
  428. }
  429. continue;
  430. }
  431. if (action == 0x06)
  432. {
  433. close (plc-&gt;PIB.file);
  434. if (ReadParameters2 (plc))
  435. {
  436. return (-1);
  437. }
  438. if ((plc-&gt;PIB.file = open (plc-&gt;PIB.name = plc-&gt;pib.name, O_BINARY|O_RDONLY)) == -1)
  439. {
  440. error (1, errno, &quot;%s&quot;, plc-&gt;PIB.name);
  441. }
  442. continue;
  443. }
  444. error (0, ENOSYS, &quot;Host Action 0x%02X&quot;, action);
  445. }
  446. close (fd);
  447. return (0);
  448. }
  449. /*====================================================================*
  450. *
  451. * int main (int argc, char const * argv[]);
  452. *
  453. * parse command line, populate plc structure and perform selected
  454. * operations; show help summary when asked; see getoptv and putoptv
  455. * to understand command line parsing and help summary display; see
  456. * plc.h for the definition of struct plc;
  457. *
  458. *
  459. *--------------------------------------------------------------------*/
  460. int main (int argc, char const * argv [])
  461. {
  462. extern struct channel channel;
  463. extern void terminate (signo_t);
  464. static char const * optv [] =
  465. {
  466. &quot;dFi:n:N:p:P:qS:t:vw:x&quot;,
  467. &quot;-N file -P file [-S file]&quot;,
  468. &quot;Qualcomm Atheros Panther/Lynx PLC Host Daemon&quot;,
  469. &quot;d\texecute in background as daemon&quot;,
  470. &quot;F [F]\tflash [force] non-volatile memory&quot;,
  471. #if defined (WINPCAP) || defined (LIBPCAP)
  472. &quot;i n\thost interface is (n) [&quot; LITERAL (CHANNEL_ETHNUMBER) &quot;]&quot;,
  473. #else
  474. &quot;i s\thost interface is (s) [&quot; LITERAL (CHANNEL_ETHDEVICE) &quot;]&quot;,
  475. #endif
  476. &quot;n f\tread firmware from device into file (f)&quot;,
  477. &quot;N f\tfirmware file is (f)&quot;,
  478. &quot;p f\tread parameters from device into file (f)&quot;,
  479. &quot;P f\tparameter file is (f)&quot;,
  480. &quot;q\tquiet mode&quot;,
  481. &quot;S f\tsoftloader file is (f)&quot;,
  482. &quot;t n\tread timeout is (n) milliseconds [&quot; LITERAL (CHANNEL_TIMEOUT) &quot;]&quot;,
  483. &quot;v\tverbose mode&quot;,
  484. &quot;w n\twakeup every (n) seconds [&quot; LITERAL (PLC_LONGTIME) &quot;]&quot;,
  485. &quot;x\texit on error&quot;,
  486. (char const *) (0)
  487. };
  488. #include &quot;../plc/plc.c&quot;
  489. struct sigaction sa;
  490. char const * socketname = SOCKETNAME;
  491. signed c;
  492. if (getenv (PLCDEVICE))
  493. {
  494. #if defined (WINPCAP) || defined (LIBPCAP)
  495. channel.ifindex = atoi (getenv (PLCDEVICE));
  496. #else
  497. channel.ifname = strdup (getenv (PLCDEVICE));
  498. #endif
  499. }
  500. optind = 1;
  501. plc.timer = PLC_LONGTIME;
  502. while ((c = getoptv (argc, argv, optv)) != -1)
  503. {
  504. switch (c)
  505. {
  506. case 'd':
  507. _setbits (plc.flags, PLC_DAEMON);
  508. break;
  509. case 'F':
  510. _setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
  511. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  512. {
  513. _setbits (plc.module, VS_MODULE_FORCE);
  514. }
  515. _setbits (plc.flags, PLC_FLASH_DEVICE);
  516. break;
  517. case 'i':
  518. #if defined (WINPCAP) || defined (LIBPCAP)
  519. channel.ifindex = atoi (optarg);
  520. #else
  521. channel.ifname = optarg;
  522. #endif
  523. break;
  524. case 'N':
  525. if (!checkfilename (optarg))
  526. {
  527. error (1, EINVAL, &quot;%s&quot;, optarg);
  528. }
  529. if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1)
  530. {
  531. error (1, errno, &quot;%s&quot;, plc.NVM.name);
  532. }
  533. if (nvmfile2 (&amp;plc.NVM))
  534. {
  535. error (1, errno, &quot;Bad firmware file: %s&quot;, plc.NVM.name);
  536. }
  537. _setbits (plc.flags, PLC_WRITE_MAC);
  538. break;
  539. case 'n':
  540. if (!checkfilename (optarg))
  541. {
  542. error (1, EINVAL, &quot;%s&quot;, optarg);
  543. }
  544. if ((plc.nvm.file = open (plc.nvm.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  545. {
  546. error (1, errno, &quot;%s&quot;, plc.nvm.name);
  547. }
  548. break;
  549. case 'P':
  550. if (!checkfilename (optarg))
  551. {
  552. error (1, EINVAL, &quot;%s&quot;, optarg);
  553. }
  554. if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1)
  555. {
  556. error (1, errno, &quot;%s&quot;, plc.PIB.name);
  557. }
  558. if (pibfile2 (&amp;plc.PIB))
  559. {
  560. error (1, errno, &quot;Bad parameter file: %s&quot;, plc.PIB.name);
  561. }
  562. _setbits (plc.flags, PLC_WRITE_PIB);
  563. break;
  564. case 'p':
  565. if (!checkfilename (optarg))
  566. {
  567. error (1, EINVAL, &quot;%s&quot;, optarg);
  568. }
  569. if ((plc.pib.file = open (plc.pib.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  570. {
  571. error (1, errno, &quot;%s&quot;, plc.pib.name);
  572. }
  573. break;
  574. case 'q':
  575. _setbits (channel.flags, CHANNEL_SILENCE);
  576. _setbits (plc.flags, PLC_SILENCE);
  577. break;
  578. case 'S':
  579. if (!checkfilename (optarg))
  580. {
  581. error (1, EINVAL, &quot;%s&quot;, optarg);
  582. }
  583. if ((plc.CFG.file = open (plc.CFG.name = optarg, O_BINARY|O_RDONLY)) == -1)
  584. {
  585. error (1, errno, &quot;%s&quot;, plc.CFG.name);
  586. }
  587. if (nvmfile2 (&amp;plc.CFG))
  588. {
  589. error (1, errno, &quot;Bad firmware file: %s&quot;, plc.CFG.name);
  590. }
  591. break;
  592. case 't':
  593. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  594. break;
  595. case 'v':
  596. _setbits (channel.flags, CHANNEL_VERBOSE);
  597. _setbits (plc.flags, PLC_VERBOSE);
  598. break;
  599. case 'w':
  600. plc.timer = (unsigned)(uintspec (optarg, 1, 3600));
  601. break;
  602. case 'x':
  603. _setbits (plc.flags, PLC_BAILOUT);
  604. break;
  605. default:
  606. break;
  607. }
  608. }
  609. argc -= optind;
  610. argv += optind;
  611. if (argc)
  612. {
  613. error (1, ENOTSUP, ERROR_TOOMANY);
  614. }
  615. if (plc.NVM.file == -1)
  616. {
  617. error (1, ECANCELED, &quot;No host NVM file named&quot;);
  618. }
  619. if (plc.PIB.file == -1)
  620. {
  621. error (1, ECANCELED, &quot;No host PIB file named&quot;);
  622. }
  623. if (plc.CFG.file == -1)
  624. {
  625. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  626. {
  627. error (1, ECANCELED, &quot;No Softloader file named&quot;);
  628. }
  629. }
  630. if (plc.nvm.file == -1)
  631. {
  632. if ((plc.nvm.file = open (plc.nvm.name = NEWNVM, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  633. {
  634. error (1, errno, &quot;%s&quot;, plc.nvm.name);
  635. }
  636. }
  637. if (plc.pib.file == -1)
  638. {
  639. if ((plc.pib.file = open (plc.pib.name = NEWPIB, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  640. {
  641. error (1, errno, &quot;%s&quot;, plc.pib.name);
  642. }
  643. }
  644. #ifndef WIN32
  645. if (_anyset (plc.flags, PLC_DAEMON))
  646. {
  647. pid_t pid = fork ();
  648. if (pid &lt; 0)
  649. {
  650. error (1, errno, &quot;Can't start daemon&quot;);
  651. }
  652. if (pid &gt; 0)
  653. {
  654. exit (0);
  655. }
  656. }
  657. memset (&amp;sa, 0, sizeof (struct sigaction));
  658. sa.sa_handler = terminate;
  659. sigaction (SIGTERM, &amp;sa, (struct sigaction *)(0));
  660. sigaction (SIGQUIT, &amp;sa, (struct sigaction *)(0));
  661. sigaction (SIGTSTP, &amp;sa, (struct sigaction *)(0));
  662. sigaction (SIGINT, &amp;sa, (struct sigaction *)(0));
  663. sigaction (SIGHUP, &amp;sa, (struct sigaction *)(0));
  664. #endif
  665. openchannel (&amp;channel);
  666. if (!(plc.message = malloc (sizeof (* plc.message))))
  667. {
  668. error (1, errno, PLC_NOMEMORY);
  669. }
  670. function (&amp;plc, socketname);
  671. free (plc.message);
  672. closechannel (&amp;channel);
  673. exit (0);
  674. }
  675. </pre>
  676. <div class='footerlink'>
  677. [<a href='plchost.c.html' title=' plchost.c '>PREV</a>]
  678. [<a href='toolkit.html' title=' Index '>HOME</a>]
  679. [<a href='plchostd.c.html' title=' plchostd.c '>NEXT</a>]
  680. </div>
  681. </body>
  682. </html>