nvmmerge.c.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. nvmmerge.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='nvmlock2.c.html' title=' nvmlock2.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='nvmpeek1.c.html' title=' nvmpeek1.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. * nvmmerge.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;unistd.h&gt;
  76. #include &lt;string.h&gt;
  77. #include &lt;errno.h&gt;
  78. /*====================================================================*
  79. * custom header files;
  80. *--------------------------------------------------------------------*/
  81. #include &quot;../tools/getoptv.h&quot;
  82. #include &quot;../tools/memory.h&quot;
  83. #include &quot;../tools/flags.h&quot;
  84. #include &quot;../tools/files.h&quot;
  85. #include &quot;../tools/error.h&quot;
  86. #include &quot;../nvm/nvm.h&quot;
  87. /*====================================================================*
  88. * custom source files;
  89. *--------------------------------------------------------------------*/
  90. #ifndef MAKEFILE
  91. #include &quot;../tools/getoptv.c&quot;
  92. #include &quot;../tools/putoptv.c&quot;
  93. #include &quot;../tools/version.c&quot;
  94. #include &quot;../tools/checksum32.c&quot;
  95. #include &quot;../tools/error.c&quot;
  96. #endif
  97. /*====================================================================*
  98. *
  99. * void copyimage (struct _file_ * file, signed extent, signed image)
  100. *
  101. *
  102. *
  103. * Contributor(s):
  104. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  105. *
  106. *--------------------------------------------------------------------*/
  107. static void copyimage (struct _file_ * file, signed extent, signed image)
  108. {
  109. char buffer [1024];
  110. signed length = sizeof (buffer);
  111. while (extent)
  112. {
  113. if (length &gt; extent)
  114. {
  115. length = extent;
  116. }
  117. if (read (file-&gt;file, buffer, length) &lt; length)
  118. {
  119. error (1, errno, NVM_IMG_CANTREAD, file-&gt;name, image);
  120. }
  121. if (write (STDOUT_FILENO, buffer, length) &lt; length)
  122. {
  123. error (1, errno, NVM_IMG_CANTSAVE, file-&gt;name, image);
  124. }
  125. extent -= length;
  126. }
  127. return;
  128. }
  129. /*====================================================================*
  130. *
  131. * void function1 (char const * filename, signed index, flag_t flags);
  132. *
  133. * concatenate Atheros image files having the older header format;
  134. *
  135. * the major and minor header version must be 0x6000 and 0x0000
  136. * and both header links must be 0x00000000;
  137. *
  138. *
  139. * Contributor(s):
  140. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  141. *
  142. *--------------------------------------------------------------------*/
  143. static void function1 (struct _file_ * file, signed index, flag_t flags)
  144. {
  145. static signed image = 0;
  146. static uint32_t offset = 0;
  147. struct nvm_header1 nvm_header;
  148. if (read (file-&gt;file, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  149. {
  150. error (1, errno, NVM_HDR_CANTREAD, file-&gt;name, image);
  151. }
  152. if (LE32TOH (nvm_header.HEADERVERSION) != 0x60000000)
  153. {
  154. error (1, 0, NVM_HDR_VERSION, file-&gt;name, image);
  155. }
  156. if (checksum32 (&amp;nvm_header, sizeof (nvm_header), 0))
  157. {
  158. error (1, 0, NVM_HDR_CHECKSUM, file-&gt;name, image);
  159. }
  160. if (nvm_header.NEXTHEADER)
  161. {
  162. error (1, 0, NVM_HDR_LINK, file-&gt;name, image);
  163. }
  164. if (index)
  165. {
  166. offset += sizeof (nvm_header);
  167. offset += LE32TOH (nvm_header.IMAGELENGTH);
  168. nvm_header.NEXTHEADER = HTOLE32 (offset);
  169. }
  170. nvm_header.HEADERCHECKSUM = 0;
  171. nvm_header.HEADERCHECKSUM = checksum32 (&amp;nvm_header, sizeof (nvm_header), 0);
  172. if (write (STDOUT_FILENO, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  173. {
  174. error (1, errno, NVM_HDR_CANTSAVE, file-&gt;name, image);
  175. }
  176. copyimage (file, LE32TOH (nvm_header.IMAGELENGTH), image);
  177. return;
  178. }
  179. /*====================================================================*
  180. *
  181. * void function2 (char const * filename, signed index, flag_t flags);
  182. *
  183. * concatenate Atheros image files having the newer header format;
  184. *
  185. * the manor and minor header version must be 0x0001 and 0x0001
  186. * and both header links must be 0xFFFFFFFF;
  187. *
  188. *
  189. * Contributor(s):
  190. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  191. *
  192. *--------------------------------------------------------------------*/
  193. static void function2 (struct _file_ * file, signed index, flag_t flags)
  194. {
  195. static signed image = 0;
  196. static signed origin = ~0;
  197. static signed offset = 0;
  198. struct nvm_header2 nvm_header;
  199. if (read (file-&gt;file, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  200. {
  201. error (1, errno, NVM_HDR_CANTREAD, file-&gt;name, image);
  202. }
  203. if (LE16TOH (nvm_header.MajorVersion) != 1)
  204. {
  205. error (1, 0, NVM_HDR_VERSION, file-&gt;name, image);
  206. }
  207. if (LE16TOH (nvm_header.MinorVersion) != 1)
  208. {
  209. error (1, 0, NVM_HDR_VERSION, file-&gt;name, image);
  210. }
  211. if (checksum32 (&amp;nvm_header, sizeof (nvm_header), 0))
  212. {
  213. error (1, 0, NVM_HDR_CHECKSUM, file-&gt;name, image);
  214. }
  215. if (~nvm_header.PrevHeader)
  216. {
  217. error (1, 0, NVM_HDR_LINK, file-&gt;name, image);
  218. }
  219. if (~nvm_header.NextHeader)
  220. {
  221. error (1, 0, NVM_HDR_LINK, file-&gt;name, image);
  222. }
  223. nvm_header.PrevHeader = HTOLE32 (origin);
  224. origin = offset;
  225. if (index)
  226. {
  227. offset += sizeof (nvm_header);
  228. offset += LE32TOH (nvm_header.ImageLength);
  229. nvm_header.NextHeader = HTOLE32 (offset);
  230. }
  231. nvm_header.HeaderChecksum = 0;
  232. nvm_header.HeaderChecksum = checksum32 (&amp;nvm_header, sizeof (nvm_header), 0);
  233. if (write (STDOUT_FILENO, &amp;nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  234. {
  235. error (1, errno, NVM_HDR_CANTSAVE, file-&gt;name, image);
  236. }
  237. copyimage (file, LE32TOH (nvm_header.ImageLength), image);
  238. image++;
  239. return;
  240. }
  241. /*====================================================================*
  242. *
  243. * void function (char const * filename, signed count, flag_t flags);
  244. *
  245. *
  246. *
  247. * Contributor(s):
  248. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  249. *
  250. *--------------------------------------------------------------------*/
  251. static void function (char const * filename, signed index, flag_t flags)
  252. {
  253. uint32_t version;
  254. struct _file_ file =
  255. {
  256. -1,
  257. filename
  258. };
  259. if (_anyset (flags, NVM_VERBOSE))
  260. {
  261. error (0, 0, &quot;%s&quot;, file.name);
  262. }
  263. if ((file.file = open (file.name, O_BINARY|O_RDONLY)) == -1)
  264. {
  265. error (1, errno, FILE_CANTOPEN, file.name);
  266. }
  267. if (read (file.file, &amp;version, sizeof (version)) != sizeof (version))
  268. {
  269. error (1, errno, FILE_CANTREAD, file.name);
  270. }
  271. if (lseek (file.file, 0, SEEK_SET))
  272. {
  273. error (1, errno, FILE_CANTHOME, file.name);
  274. }
  275. if (LE32TOH (version) == 0x60000000)
  276. {
  277. function1 (&amp;file, index, flags);
  278. }
  279. else
  280. {
  281. function2 (&amp;file, index, flags);
  282. }
  283. close (file.file);
  284. return;
  285. }
  286. /*====================================================================*
  287. *
  288. * int main (int argc, char const * argv []);
  289. *
  290. *
  291. *
  292. * Contributor(s):
  293. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  294. *
  295. *--------------------------------------------------------------------*/
  296. int main (int argc, char const * argv [])
  297. {
  298. static char const * optv [] =
  299. {
  300. &quot;qv&quot;,
  301. &quot;file [file] [...] [&gt; file]&quot;,
  302. &quot;Qualcomm Atheros PLC Firmware Image File Splicer&quot;,
  303. &quot;q\tsuppress messages&quot;,
  304. &quot;v\tverbose messages&quot;,
  305. (char const *) (0)
  306. };
  307. flag_t flags = (flag_t)(0);
  308. signed c;
  309. optind = 1;
  310. while ((c = getoptv (argc, argv, optv)) != -1)
  311. {
  312. switch ((char) (c))
  313. {
  314. case 'q':
  315. _setbits (flags, NVM_SILENCE);
  316. break;
  317. case 'v':
  318. _setbits (flags, NVM_VERBOSE);
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. argc -= optind;
  325. argv += optind;
  326. #ifdef WIN32
  327. setmode (fileno (stdin), O_BINARY);
  328. setmode (fileno (stdout), O_BINARY);
  329. #endif
  330. while ((argc) &amp;&amp; (* argv))
  331. {
  332. function (* argv, argc-1, flags);
  333. argv++;
  334. argc--;
  335. }
  336. return (0);
  337. }
  338. </pre>
  339. <div class='footerlink'>
  340. [<a href='nvmlock2.c.html' title=' nvmlock2.c '>PREV</a>]
  341. [<a href='toolkit.html' title=' Index '>HOME</a>]
  342. [<a href='nvmpeek1.c.html' title=' nvmpeek1.c '>NEXT</a>]
  343. </div>
  344. </body>
  345. </html>