modpib.c.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. modpib.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='MMEPeek.c.html' title=' MMEPeek.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='ModuleCommit.c.html' title=' ModuleCommit.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. * modpib.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. /*====================================================================*
  72. * system header files;
  73. *--------------------------------------------------------------------*/
  74. #include &lt;stdio.h&gt;
  75. #include &lt;stdlib.h&gt;
  76. #include &lt;string.h&gt;
  77. #include &lt;unistd.h&gt;
  78. #include &lt;ctype.h&gt;
  79. #include &lt;errno.h&gt;
  80. #include &lt;sys/stat.h&gt;
  81. #include &lt;netinet/in.h&gt;
  82. #include &lt;arpa/inet.h&gt;
  83. /*====================================================================*
  84. * custom header files;
  85. *--------------------------------------------------------------------*/
  86. #include &quot;../tools/getoptv.h&quot;
  87. #include &quot;../tools/number.h&quot;
  88. #include &quot;../tools/symbol.h&quot;
  89. #include &quot;../tools/flags.h&quot;
  90. #include &quot;../tools/error.h&quot;
  91. #include &quot;../tools/files.h&quot;
  92. #include &quot;../tools/chars.h&quot;
  93. #include &quot;../plc/plc.h&quot;
  94. #include &quot;../key/HPAVKey.h&quot;
  95. #include &quot;../key/keys.h&quot;
  96. #include &quot;../pib/pib.h&quot;
  97. /*====================================================================*
  98. * custom source files;
  99. *--------------------------------------------------------------------*/
  100. #ifndef MAKEFILE
  101. #include &quot;../tools/getoptv.c&quot;
  102. #include &quot;../tools/putoptv.c&quot;
  103. #include &quot;../tools/version.c&quot;
  104. #include &quot;../tools/checksum32.c&quot;
  105. #include &quot;../tools/fdchecksum32.c&quot;
  106. #include &quot;../tools/uintspec.c&quot;
  107. #include &quot;../tools/hexencode.c&quot;
  108. #include &quot;../tools/hexdecode.c&quot;
  109. #include &quot;../tools/hexstring.c&quot;
  110. #include &quot;../tools/todigit.c&quot;
  111. #include &quot;../tools/memincr.c&quot;
  112. #include &quot;../tools/synonym.c&quot;
  113. #include &quot;../tools/error.c&quot;
  114. #endif
  115. #ifndef MAKEFILE
  116. #include &quot;../pib/pibfile.c&quot;
  117. #include &quot;../pib/pibfile1.c&quot;
  118. #include &quot;../pib/pibfile2.c&quot;
  119. #include &quot;../pib/pibpeek1.c&quot;
  120. #include &quot;../pib/pibpeek2.c&quot;
  121. #endif
  122. #ifndef MAKEFILE
  123. #include &quot;../key/HPAVKeyNID.c&quot;
  124. #include &quot;../key/SHA256Reset.c&quot;
  125. #include &quot;../key/SHA256Write.c&quot;
  126. #include &quot;../key/SHA256Block.c&quot;
  127. #include &quot;../key/SHA256Fetch.c&quot;
  128. #include &quot;../key/keys.c&quot;
  129. #endif
  130. /*====================================================================*
  131. * constants;
  132. *--------------------------------------------------------------------*/
  133. #define MEMBERSHIP_OFFSET 0x00001E8C
  134. #define MEMBERSHIP_STATUS 0x00000000
  135. /*====================================================================*
  136. *
  137. * signed pibimage1 (struct _file_ * file, simple_pib *sample_pib, signed level, flag_t flags);
  138. *
  139. * modify selected PIB header values and compute a new checksum;
  140. * this function assumes that the file is open and positioned to
  141. * the start of the parameter block;
  142. *
  143. *
  144. * Contributor(s):
  145. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  146. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  147. *
  148. *--------------------------------------------------------------------*/
  149. static signed pibimage1 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags)
  150. {
  151. struct simple_pib simple_pib;
  152. memset (&amp;simple_pib, 0, sizeof (simple_pib));
  153. if (read (file-&gt;file, &amp;simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  154. {
  155. error (1, errno, FILE_CANTREAD, file-&gt;name);
  156. }
  157. if (_anyset (flags, PIB_MAC))
  158. {
  159. memcpy (simple_pib.MAC, sample_pib-&gt;MAC, sizeof (simple_pib.MAC));
  160. }
  161. if (_anyset (flags, PIB_MACINC))
  162. {
  163. memincr (simple_pib.MAC, sizeof (simple_pib.MAC));
  164. }
  165. if (_anyset (flags, PIB_DAK))
  166. {
  167. memcpy (simple_pib.DAK, sample_pib-&gt;DAK, sizeof (simple_pib.DAK));
  168. }
  169. if (_anyset (flags, PIB_NMK))
  170. {
  171. memcpy (simple_pib.NMK, sample_pib-&gt;NMK, sizeof (simple_pib.NMK));
  172. }
  173. if (_anyset (flags, PIB_NETWORK))
  174. {
  175. memcpy (simple_pib.NET, sample_pib-&gt;NET, sizeof (simple_pib.NET));
  176. }
  177. if (_anyset (flags, PIB_MFGSTRING))
  178. {
  179. memcpy (simple_pib.MFG, sample_pib-&gt;MFG, sizeof (simple_pib.MFG));
  180. }
  181. if (_anyset (flags, PIB_USER))
  182. {
  183. memcpy (simple_pib.USR, sample_pib-&gt;USR, sizeof (simple_pib.USR));
  184. }
  185. if (_anyset (flags, PIB_CCO_MODE))
  186. {
  187. simple_pib.CCoSelection = sample_pib-&gt;CCoSelection;
  188. }
  189. if (_anyset (flags, PIB_NMK | PIB_NID))
  190. {
  191. #if 0
  192. /*
  193. * clear the AVLNMembership bit whenever the NMK or NID change; this is an important
  194. * step because it prevents false network association;
  195. */
  196. if (BE16TOH (simple_pib.PIBVERSION) &gt; 0x0200)
  197. {
  198. uint32_t membership = MEMBERSHIP_STATUS;
  199. if (lseek (file-&gt;file, MEMBERSHIP_OFFSET, SEEK_SET) != MEMBERSHIP_OFFSET)
  200. {
  201. error (1, errno, FILE_CANTHOME, file-&gt;name);
  202. }
  203. if (write (file-&gt;file, &amp;membership, sizeof (membership)) != sizeof (membership))
  204. {
  205. error (1, errno, FILE_CANTSAVE, file-&gt;name);
  206. }
  207. }
  208. #endif
  209. if (_allclr (flags, PIB_NID))
  210. {
  211. level = simple_pib.PreferredNID [HPAVKEY_NID_LEN-1] &gt;&gt; 4;
  212. }
  213. HPAVKeyNID (simple_pib.PreferredNID, simple_pib.NMK, level &amp; 1);
  214. }
  215. if (lseek (file-&gt;file, (off_t)(0) - sizeof (simple_pib), SEEK_CUR) == -1)
  216. {
  217. error (1, errno, FILE_CANTHOME, file-&gt;name);
  218. }
  219. if (write (file-&gt;file, &amp;simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  220. {
  221. error (1, errno, FILE_CANTSAVE, file-&gt;name);
  222. }
  223. if (lseek (file-&gt;file, (off_t)(0) - sizeof (simple_pib), SEEK_CUR) == -1)
  224. {
  225. error (1, errno, FILE_CANTHOME, file-&gt;name);
  226. }
  227. simple_pib.CHECKSUM = fdchecksum32 (file-&gt;file, LE16TOH (simple_pib.PIBLENGTH), simple_pib.CHECKSUM);
  228. if (lseek (file-&gt;file, (off_t)(0) - LE16TOH (simple_pib.PIBLENGTH), SEEK_CUR) == -1)
  229. {
  230. error (1, errno, FILE_CANTHOME, file-&gt;name);
  231. }
  232. if (write (file-&gt;file, &amp;simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  233. {
  234. error (1, errno, FILE_CANTSAVE, file-&gt;name);
  235. }
  236. if (_anyset (flags, PIB_VERBOSE))
  237. {
  238. pibpeek1 (&amp;simple_pib);
  239. }
  240. return (0);
  241. }
  242. /*====================================================================*
  243. *
  244. * signed pibimage2 (struct _file_ * file, simple_pib *sample_pib, signed level, flag_t flags);
  245. *
  246. * modify selected PIB header values but do not compute a checksum;
  247. *
  248. *
  249. * Contributor(s):
  250. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  251. *
  252. *--------------------------------------------------------------------*/
  253. static signed pibimage2 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags)
  254. {
  255. struct simple_pib simple_pib;
  256. memset (&amp;simple_pib, 0, sizeof (simple_pib));
  257. if (read (file-&gt;file, &amp;simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  258. {
  259. error (1, errno, FILE_CANTREAD, file-&gt;name);
  260. }
  261. if (_anyset (flags, PIB_MAC))
  262. {
  263. memcpy (simple_pib.MAC, sample_pib-&gt;MAC, sizeof (simple_pib.MAC));
  264. }
  265. if (_anyset (flags, PIB_MACINC))
  266. {
  267. memincr (simple_pib.MAC, sizeof (simple_pib.MAC));
  268. }
  269. if (_anyset (flags, PIB_DAK))
  270. {
  271. memcpy (simple_pib.DAK, sample_pib-&gt;DAK, sizeof (simple_pib.DAK));
  272. }
  273. if (_anyset (flags, PIB_NMK))
  274. {
  275. memcpy (simple_pib.NMK, sample_pib-&gt;NMK, sizeof (simple_pib.NMK));
  276. }
  277. if (_anyset (flags, PIB_NETWORK))
  278. {
  279. memcpy (simple_pib.NET, sample_pib-&gt;NET, sizeof (simple_pib.NET));
  280. }
  281. if (_anyset (flags, PIB_MFGSTRING))
  282. {
  283. memcpy (simple_pib.MFG, sample_pib-&gt;MFG, sizeof (simple_pib.MFG));
  284. }
  285. if (_anyset (flags, PIB_USER))
  286. {
  287. memcpy (simple_pib.USR, sample_pib-&gt;USR, sizeof (simple_pib.USR));
  288. }
  289. if (_anyset (flags, PIB_CCO_MODE))
  290. {
  291. simple_pib.CCoSelection = sample_pib-&gt;CCoSelection;
  292. }
  293. if (_anyset (flags, PIB_NMK | PIB_NID))
  294. {
  295. #if 0
  296. /*
  297. * clear the AVLNMembership bit whenever the NMK or NID change; this is an important
  298. * step because it prevents false network association;
  299. */
  300. if (BE16TOH (simple_pib.PIBVERSION) &gt; 0x0200)
  301. {
  302. uint32_t membership = MEMBERSHIP_STATUS;
  303. if (lseek (file-&gt;file, MEMBERSHIP_OFFSET, SEEK_SET) != MEMBERSHIP_OFFSET)
  304. {
  305. error (1, errno, FILE_CANTHOME, file-&gt;name);
  306. }
  307. if (write (file-&gt;file, &amp;membership, sizeof (membership)) != sizeof (membership))
  308. {
  309. error (1, errno, FILE_CANTSAVE, file-&gt;name);
  310. }
  311. }
  312. #endif
  313. if (_allclr (flags, PIB_NID))
  314. {
  315. level = simple_pib.PreferredNID [HPAVKEY_NID_LEN-1] &gt;&gt; 4;
  316. }
  317. HPAVKeyNID (simple_pib.PreferredNID, simple_pib.NMK, level &amp; 1);
  318. }
  319. if (lseek (file-&gt;file, (off_t)(0) - sizeof (simple_pib), SEEK_CUR) == -1)
  320. {
  321. error (1, errno, FILE_CANTHOME, file-&gt;name);
  322. }
  323. if (write (file-&gt;file, &amp;simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  324. {
  325. error (1, errno, FILE_CANTSAVE, file-&gt;name);
  326. }
  327. if (lseek (file-&gt;file, (off_t)(0) - sizeof (simple_pib), SEEK_CUR) == -1)
  328. {
  329. error (1, errno, FILE_CANTHOME, file-&gt;name);
  330. }
  331. if (_anyset (flags, PIB_VERBOSE))
  332. {
  333. pibpeek2 (&amp;simple_pib);
  334. }
  335. return (0);
  336. }
  337. /*====================================================================*
  338. *
  339. * signed pibchain2 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags);
  340. *
  341. *
  342. *
  343. * Contributor(s):
  344. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  345. *
  346. *--------------------------------------------------------------------*/
  347. static signed pibchain2 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags)
  348. {
  349. unsigned module = 0;
  350. struct nvm_header2 nvm_header;
  351. uint32_t prev = ~0;
  352. uint32_t next = 0;
  353. if (lseek (file-&gt;file, 0, SEEK_SET))
  354. {
  355. error (1, errno, NVM_IMG_CHECKSUM, file-&gt;name, module);
  356. }
  357. do
  358. {
  359. if (read (file-&gt;file, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  360. {
  361. error (1, errno, NVM_HDR_CANTREAD, file-&gt;name, module);
  362. }
  363. if (LE16TOH (nvm_header.MajorVersion) != 1)
  364. {
  365. error (1, errno, NVM_HDR_VERSION, file-&gt;name, module);
  366. }
  367. if (LE16TOH (nvm_header.MinorVersion) != 1)
  368. {
  369. error (1, errno, NVM_HDR_VERSION, file-&gt;name, module);
  370. }
  371. if (checksum32 (&amp;nvm_header, sizeof (nvm_header), 0))
  372. {
  373. error (1, errno, NVM_HDR_CHECKSUM, file-&gt;name, module);
  374. }
  375. if (LE32TOH (nvm_header.PrevHeader) != prev)
  376. {
  377. error (1, errno, NVM_HDR_LINK, file-&gt;name, module);
  378. }
  379. if (LE32TOH (nvm_header.ImageType) == NVM_IMAGE_PIB)
  380. {
  381. pibimage2 (file, sample_pib, level, flags);
  382. nvm_header.ImageChecksum = fdchecksum32 (file-&gt;file, LE32TOH (nvm_header.ImageLength), 0);
  383. if (lseek (file-&gt;file, (off_t)(0) - LE32TOH (nvm_header.ImageLength), SEEK_CUR) == -1)
  384. {
  385. error (1, errno, FILE_CANTHOME, file-&gt;name);
  386. }
  387. nvm_header.HeaderChecksum = checksum32 (&amp;nvm_header, sizeof (nvm_header), nvm_header.HeaderChecksum);
  388. if (lseek (file-&gt;file, (off_t)(0) - sizeof (nvm_header), SEEK_CUR) == -1)
  389. {
  390. error (1, errno, FILE_CANTHOME, file-&gt;name);
  391. }
  392. if (write (file-&gt;file, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  393. {
  394. error (1, errno, FILE_CANTSAVE, file-&gt;name);
  395. }
  396. if (lseek (file-&gt;file, (off_t)(0) - sizeof (nvm_header), SEEK_CUR) == -1)
  397. {
  398. error (1, errno, FILE_CANTHOME, file-&gt;name);
  399. }
  400. break;
  401. }
  402. if (fdchecksum32 (file-&gt;file, LE32TOH (nvm_header.ImageLength), nvm_header.ImageChecksum))
  403. {
  404. error (1, errno, NVM_IMG_CHECKSUM, file-&gt;name, module);
  405. }
  406. prev = next;
  407. next = LE32TOH (nvm_header.NextHeader);
  408. module++;
  409. }
  410. while (~nvm_header.NextHeader);
  411. return (0);
  412. }
  413. /*====================================================================*
  414. *
  415. * signed function (char const * filename, struct simple_pib * simple_pib, unsigned level, flag_t flags);
  416. *
  417. *
  418. *
  419. * Contributor(s):
  420. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  421. *
  422. *--------------------------------------------------------------------*/
  423. static signed function (char const * filename, struct simple_pib * sample_pib, unsigned level, flag_t flags)
  424. {
  425. uint32_t version;
  426. signed status;
  427. struct _file_ file;
  428. file.name = filename;
  429. if ((file.file = open (file.name, O_BINARY|O_RDWR, FILE_FILEMODE)) == -1)
  430. {
  431. if (_allclr (flags, PIB_SILENCE))
  432. {
  433. error (0, errno, FILE_CANTOPEN, file.name);
  434. }
  435. return (-1);
  436. }
  437. if (read (file.file, &amp;version, sizeof (version)) != sizeof (version))
  438. {
  439. if (_allclr (flags, PIB_SILENCE))
  440. {
  441. error (0, errno, FILE_CANTREAD, file.name);
  442. }
  443. return (-1);
  444. }
  445. if (lseek (file.file, 0, SEEK_SET))
  446. {
  447. error (1, errno, FILE_CANTHOME, file.name);
  448. }
  449. if (LE32TOH (version) == 0x00010001)
  450. {
  451. status = pibchain2 (&amp;file, sample_pib, level, flags);
  452. }
  453. else
  454. {
  455. status = pibimage1 (&amp;file, sample_pib, level, flags);
  456. }
  457. close (file.file);
  458. return (status);
  459. }
  460. /*====================================================================*
  461. *
  462. * int main (int argc, char const * argv [])
  463. *
  464. *
  465. *--------------------------------------------------------------------*/
  466. int main (int argc, char const * argv [])
  467. {
  468. extern struct _term_ const daks [];
  469. extern struct _term_ const nmks [];
  470. static char const * optv [] =
  471. {
  472. &quot;C:D:L:M:N:S:T:U:v&quot;,
  473. &quot;file [file] [...]&quot;,
  474. &quot;modify selected PIB parameters and update checksum&quot;,
  475. &quot;C n\tCCo Selection is n&quot;,
  476. &quot;D x\tDAK as xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx&quot;,
  477. &quot;L n\tsecurity level is n&quot;,
  478. &quot;M x\tMAC as xx:xx:xx:xx:xx:xx&quot;,
  479. &quot;N x\tNMK as xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx&quot;,
  480. &quot;S s\tMFG string is s&quot;,
  481. &quot;T s\tNET string is s&quot;,
  482. &quot;U s\tUSR string is s&quot;,
  483. &quot;v\tverbose messages&quot;,
  484. (char const *) (0)
  485. };
  486. struct simple_pib sample_pib;
  487. signed level = 0;
  488. signed state = 0;
  489. flag_t flags = (flag_t)(0);
  490. signed c;
  491. optind = 1;
  492. memset (&amp;sample_pib, 0, sizeof (sample_pib));
  493. while ((c = getoptv (argc, argv, optv)) != -1)
  494. {
  495. char const * sp;
  496. switch ((char) (c))
  497. {
  498. case 'C':
  499. _setbits (flags, PIB_CCO_MODE);
  500. sample_pib.CCoSelection = (uint8_t)(uintspec (optarg, 0, 4));
  501. break;
  502. case 'D':
  503. if (!hexencode (sample_pib.DAK, sizeof (sample_pib.DAK), synonym (optarg, daks, SIZEOF (daks))))
  504. {
  505. error (1, errno, PLC_BAD_DAK, optarg);
  506. }
  507. _setbits (flags, PIB_DAK);
  508. break;
  509. case 'L':
  510. level = (uint8_t)(uintspec (optarg, 0, 1));
  511. _setbits (flags, PIB_NID);
  512. break;
  513. case 'M':
  514. if (!strcmp (optarg, &quot;auto&quot;))
  515. {
  516. _setbits (flags, PIB_MACINC);
  517. break;
  518. }
  519. if (!strcmp (optarg, &quot;next&quot;))
  520. {
  521. _setbits (flags, PIB_MACINC);
  522. break;
  523. }
  524. if (!strcmp (optarg, &quot;plus&quot;))
  525. {
  526. _setbits (flags, PIB_MACINC);
  527. break;
  528. }
  529. if (!hexencode (sample_pib.MAC, sizeof (sample_pib.MAC), optarg))
  530. {
  531. error (1, errno, PLC_BAD_MAC, optarg);
  532. }
  533. _setbits (flags, PIB_MAC);
  534. break;
  535. case 'N':
  536. if (!hexencode (sample_pib.NMK, sizeof (sample_pib.NMK), synonym (optarg, nmks, SIZEOF (nmks))))
  537. {
  538. error (1, errno, PLC_BAD_NMK, optarg);
  539. }
  540. _setbits (flags, PIB_NMK);
  541. break;
  542. case 'S':
  543. for (sp = optarg; isprint (*sp); ++sp);
  544. if (*sp)
  545. {
  546. error (1, EINVAL, &quot;NMK contains illegal characters&quot;);
  547. }
  548. if ((sp - optarg) &gt; (signed)(sizeof (sample_pib.MFG) - 1))
  549. {
  550. error (1, 0, &quot;Manufacturing string is at most %u chars&quot;, (unsigned)(sizeof (sample_pib.MFG) - 1));
  551. }
  552. memcpy (sample_pib.MFG, optarg, strlen (optarg));
  553. _setbits (flags, PIB_MFGSTRING);
  554. break;
  555. case 'T':
  556. for (sp = optarg; isprint (*sp); ++sp);
  557. if (*sp)
  558. {
  559. error (1, EINVAL, &quot;NMK contains illegal characters&quot;);
  560. }
  561. if ((sp - optarg) &gt; (signed)(sizeof (sample_pib.NET) - 1))
  562. {
  563. error (1, 0, &quot;NET string is at most %u chars&quot;, (unsigned)(sizeof (sample_pib.NET) - 1));
  564. }
  565. memcpy (sample_pib.NET, optarg, strlen (optarg));
  566. _setbits (flags, PIB_NETWORK);
  567. break;
  568. case 'U':
  569. for (sp = optarg; isprint (*sp); ++sp);
  570. if (*sp)
  571. {
  572. error (1, EINVAL, &quot;NMK contains illegal characters&quot;);
  573. }
  574. if ((sp - optarg) &gt; (signed)(sizeof (sample_pib.USR) - 1))
  575. {
  576. error (1, 0, &quot;USR string is at most %u chars&quot;, (unsigned)(sizeof (sample_pib.USR) - 1));
  577. }
  578. memcpy (sample_pib.USR, optarg, strlen (optarg));
  579. _setbits (flags, PIB_USER);
  580. break;
  581. case 'q':
  582. _setbits (flags, PIB_SILENCE);
  583. break;
  584. case 'v':
  585. _setbits (flags, PIB_VERBOSE);
  586. break;
  587. default:
  588. break;
  589. }
  590. }
  591. argc -= optind;
  592. argv += optind;
  593. while ((argc) &amp;&amp; (* argv))
  594. {
  595. if (function (* argv, &amp;sample_pib, level, flags))
  596. {
  597. state = 1;
  598. }
  599. argc--;
  600. argv++;
  601. }
  602. exit (state);
  603. }
  604. </pre>
  605. <div class='footerlink'>
  606. [<a href='MMEPeek.c.html' title=' MMEPeek.c '>PREV</a>]
  607. [<a href='toolkit.html' title=' Index '>HOME</a>]
  608. [<a href='ModuleCommit.c.html' title=' ModuleCommit.c '>NEXT</a>]
  609. </div>
  610. </body>
  611. </html>