nvmsplit.c.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. nvmsplit.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='NVMSelect.c.html' title=' NVMSelect.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='nvram.c.html' title=' nvram.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. * nvmsplit.c -
  64. *
  65. *
  66. * Contributor(s):
  67. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  68. *
  69. *--------------------------------------------------------------------*/
  70. /*====================================================================*
  71. * system header files;
  72. *--------------------------------------------------------------------*/
  73. #include &lt;stdio.h&gt;
  74. #include &lt;unistd.h&gt;
  75. #include &lt;string.h&gt;
  76. #include &lt;errno.h&gt;
  77. /*====================================================================*
  78. * custom header files;
  79. *--------------------------------------------------------------------*/
  80. #include &quot;../tools/getoptv.h&quot;
  81. #include &quot;../tools/memory.h&quot;
  82. #include &quot;../tools/flags.h&quot;
  83. #include &quot;../tools/files.h&quot;
  84. #include &quot;../tools/error.h&quot;
  85. #include &quot;../nvm/nvm.h&quot;
  86. /*====================================================================*
  87. * custom source files;
  88. *--------------------------------------------------------------------*/
  89. #ifndef MAKEFILE
  90. #include &quot;../tools/getoptv.c&quot;
  91. #include &quot;../tools/putoptv.c&quot;
  92. #include &quot;../tools/version.c&quot;
  93. #include &quot;../tools/checksum32.c&quot;
  94. #include &quot;../tools/error.c&quot;
  95. #endif
  96. /*====================================================================*
  97. *
  98. * void function1 (char const * filename, flag_t flags);
  99. *
  100. * open an Atheros Qualcomm PLC firmware image file and write each
  101. * component as an independent image file;
  102. *
  103. *
  104. * Contributor(s):
  105. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  106. *
  107. *--------------------------------------------------------------------*/
  108. static void function1 (char const * filename, flag_t flags)
  109. {
  110. struct nvm_header1 nvm_header;
  111. char savename [FILENAME_MAX];
  112. byte buffer [BUFSIZ];
  113. signed image = 0;
  114. signed offset = 0;
  115. signed ifd;
  116. signed ofd;
  117. char * sp;
  118. char * cp;
  119. memset (&amp;nvm_header, 0, sizeof (nvm_header));
  120. for (sp = cp = strcpy (savename, filename); *cp; cp++)
  121. {
  122. if (*cp == FILE_C_EXTENDER)
  123. {
  124. sp = cp;
  125. }
  126. }
  127. if (sp == savename)
  128. {
  129. sp = cp;
  130. }
  131. if ((ifd = open (filename, O_BINARY|O_RDONLY)) == -1)
  132. {
  133. error (1, errno, FILE_CANTOPEN, filename);
  134. }
  135. do
  136. {
  137. signed extent;
  138. signed length;
  139. if (lseek (ifd, 0, SEEK_CUR) != offset)
  140. {
  141. error (1, errno, NVM_HDR_LINK, filename, image);
  142. }
  143. if (read (ifd, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  144. {
  145. error (1, errno, NVM_HDR_CANTREAD, filename, image);
  146. }
  147. if (LE32TOH (nvm_header.HEADERVERSION) != 0x60000000)
  148. {
  149. error (1, 0, NVM_HDR_VERSION, filename, image);
  150. }
  151. if (checksum32 (&amp;nvm_header, sizeof (nvm_header), 0))
  152. {
  153. error (1, 0, NVM_HDR_CHECKSUM, filename, image);
  154. }
  155. #if defined (WIN32)
  156. sprintf (sp, &quot;-%02d.nvm&quot;, image);
  157. #else
  158. snprintf (sp, savename + sizeof (savename) - sp, &quot;-%02d.nvm&quot;, image);
  159. #endif
  160. if ((ofd = open (savename, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  161. {
  162. error (1, errno, FILE_CANTOPEN, savename);
  163. }
  164. if (_anyset (flags, NVM_VERBOSE))
  165. {
  166. error (0, 0, &quot;%s&quot;, savename);
  167. }
  168. offset = LE32TOH (nvm_header.NEXTHEADER);
  169. nvm_header.NEXTHEADER = 0;
  170. nvm_header.HEADERCHECKSUM = 0;
  171. nvm_header.HEADERCHECKSUM = checksum32 (&amp;nvm_header, sizeof (nvm_header), 0);
  172. if (write (ofd, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  173. {
  174. error (1, errno, FILE_CANTSAVE, savename);
  175. }
  176. extent = LE32TOH (nvm_header.IMAGELENGTH);
  177. length = sizeof (buffer);
  178. while (extent)
  179. {
  180. if (length &gt; extent)
  181. {
  182. length = extent;
  183. }
  184. if (read (ifd, buffer, length) &lt; length)
  185. {
  186. error (1, errno, NVM_IMG_CANTREAD, filename, image);
  187. }
  188. if (write (ofd, buffer, length) &lt; length)
  189. {
  190. error (1, errno, NVM_IMG_CANTSAVE, savename, image);
  191. }
  192. extent -= length;
  193. }
  194. close (ofd);
  195. image++;
  196. }
  197. while (offset);
  198. close (ifd);
  199. return;
  200. }
  201. /*====================================================================*
  202. *
  203. * void function2 (char const * filename, flag_t flags);
  204. *
  205. * open an Atheros Qualcomm PLC firmware image file and write each
  206. * component as an independent image file;
  207. *
  208. *
  209. * Contributor(s):
  210. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  211. *
  212. *--------------------------------------------------------------------*/
  213. static void function2 (char const * filename, flag_t flags)
  214. {
  215. struct nvm_header2 nvm_header;
  216. char savename [FILENAME_MAX];
  217. byte buffer [BUFSIZ];
  218. signed image = 0;
  219. signed offset = 0;
  220. signed ifd;
  221. signed ofd;
  222. char * sp;
  223. char * cp;
  224. memset (&amp;nvm_header, 0, sizeof (nvm_header));
  225. for (sp = cp = strcpy (savename, filename); *cp; cp++)
  226. {
  227. if (*cp == FILE_C_EXTENDER)
  228. {
  229. sp = cp;
  230. }
  231. }
  232. if (sp == savename)
  233. {
  234. sp = cp;
  235. }
  236. if ((ifd = open (filename, O_BINARY|O_RDONLY)) == -1)
  237. {
  238. error (1, errno, FILE_CANTOPEN, filename);
  239. }
  240. do
  241. {
  242. signed extent;
  243. signed length;
  244. if (lseek (ifd, 0, SEEK_CUR) != offset)
  245. {
  246. error (1, errno, NVM_HDR_LINK, filename, image);
  247. }
  248. if (read (ifd, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  249. {
  250. error (1, errno, NVM_HDR_CANTREAD, filename, image);
  251. }
  252. if (LE16TOH (nvm_header.MinorVersion) != 1)
  253. {
  254. error (1, 0, NVM_HDR_VERSION, filename, image);
  255. }
  256. if (LE16TOH (nvm_header.MajorVersion) != 1)
  257. {
  258. error (1, 0, NVM_HDR_VERSION, filename, image);
  259. }
  260. if (checksum32 (&amp;nvm_header, sizeof (nvm_header), 0))
  261. {
  262. error (1, 0, NVM_HDR_CHECKSUM, filename, image);
  263. }
  264. #if defined (WIN32)
  265. sprintf (sp, &quot;-%02d.nvm&quot;, image);
  266. #else
  267. snprintf (sp, savename + sizeof (savename) - sp, &quot;-%02d.nvm&quot;, image);
  268. #endif
  269. if ((ofd = open (savename, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, FILE_FILEMODE)) == -1)
  270. {
  271. error (1, errno, FILE_CANTOPEN, savename);
  272. }
  273. if (_anyset (flags, NVM_VERBOSE))
  274. {
  275. error (0, 0, &quot;%s --&gt; %s&quot;, filename, savename);
  276. }
  277. offset = LE32TOH (nvm_header.NextHeader);
  278. nvm_header.NextHeader = ~0;
  279. nvm_header.PrevHeader = ~0;
  280. nvm_header.HeaderChecksum = 0;
  281. nvm_header.HeaderChecksum = checksum32 (&amp;nvm_header, sizeof (nvm_header), 0);
  282. if (write (ofd, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  283. {
  284. error (1, errno, NVM_HDR_CANTSAVE, savename, image);
  285. }
  286. extent = LE32TOH (nvm_header.ImageLength);
  287. length = sizeof (buffer);
  288. while (extent)
  289. {
  290. if (length &gt; extent)
  291. {
  292. length = extent;
  293. }
  294. if (read (ifd, buffer, length) &lt; length)
  295. {
  296. error (1, errno, NVM_IMG_CANTREAD, filename, image);
  297. }
  298. if (write (ofd, buffer, length) &lt; length)
  299. {
  300. error (1, errno, NVM_IMG_CANTSAVE, savename, image);
  301. }
  302. extent -= length;
  303. }
  304. close (ofd);
  305. image++;
  306. }
  307. while (~offset);
  308. close (ifd);
  309. return;
  310. }
  311. /*====================================================================*
  312. *
  313. * void function (struct _file_ * file, flag_t flags);
  314. *
  315. * open an Atheros Qualcomm PLC firmware image file and write each
  316. * component as an independent, single-image firmware file;
  317. *
  318. *
  319. * Contributor(s):
  320. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  321. *
  322. *--------------------------------------------------------------------*/
  323. static void function (char const * filename, flag_t flags)
  324. {
  325. uint32_t version;
  326. signed ifd;
  327. if ((ifd = open (filename, O_BINARY|O_RDONLY)) == -1)
  328. {
  329. error (1, errno, FILE_CANTOPEN, filename);
  330. }
  331. if (read (ifd, &amp;version, sizeof (version)) != sizeof (version))
  332. {
  333. error (1, errno, FILE_CANTREAD, filename);
  334. }
  335. close (ifd);
  336. if (LE32TOH (version) == 0x60000000)
  337. {
  338. function1 (filename, flags);
  339. }
  340. else
  341. {
  342. function2 (filename, flags);
  343. }
  344. return;
  345. }
  346. /*====================================================================*
  347. *
  348. * int main (int argc, char const * argv []);
  349. *
  350. *
  351. * Contributor(s):
  352. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  353. *
  354. *--------------------------------------------------------------------*/
  355. int main (int argc, char const * argv [])
  356. {
  357. static char const * optv [] =
  358. {
  359. &quot;qv&quot;,
  360. &quot;file [file] [...]&quot;,
  361. &quot;Qualcomm Atheros PLC Firmware Image File Slicer&quot;,
  362. &quot;q\tsuppress messages&quot;,
  363. &quot;v\tverbose messages&quot;,
  364. (char const *) (0)
  365. };
  366. flag_t flags = (flag_t)(0);
  367. signed c;
  368. optind = 1;
  369. while ((c = getoptv (argc, argv, optv)) != -1)
  370. {
  371. switch (c)
  372. {
  373. case 'q':
  374. _setbits (flags, NVM_SILENCE);
  375. break;
  376. case 'v':
  377. _setbits (flags, NVM_VERBOSE);
  378. break;
  379. default:
  380. break;
  381. }
  382. }
  383. argc -= optind;
  384. argv += optind;
  385. while ((argc) &amp;&amp; (* argv))
  386. {
  387. function (* argv, flags);
  388. argc--;
  389. argv++;
  390. }
  391. return (0);
  392. }
  393. </pre>
  394. <div class='footerlink'>
  395. [<a href='NVMSelect.c.html' title=' NVMSelect.c '>PREV</a>]
  396. [<a href='toolkit.html' title=' Index '>HOME</a>]
  397. [<a href='nvram.c.html' title=' nvram.c '>NEXT</a>]
  398. </div>
  399. </body>
  400. </html>