nvmmerge.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*"
  42. *
  43. * nvmmerge.c -
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. * Nathaniel Houghton
  49. *
  50. *--------------------------------------------------------------------*/
  51. /*====================================================================*
  52. * system header files;
  53. *--------------------------------------------------------------------*/
  54. #include <stdio.h>
  55. #include <unistd.h>
  56. #include <string.h>
  57. #include <errno.h>
  58. /*====================================================================*
  59. * custom header files;
  60. *--------------------------------------------------------------------*/
  61. #include "../tools/getoptv.h"
  62. #include "../tools/memory.h"
  63. #include "../tools/flags.h"
  64. #include "../tools/files.h"
  65. #include "../tools/error.h"
  66. #include "../nvm/nvm.h"
  67. /*====================================================================*
  68. * custom source files;
  69. *--------------------------------------------------------------------*/
  70. #ifndef MAKEFILE
  71. #include "../tools/getoptv.c"
  72. #include "../tools/putoptv.c"
  73. #include "../tools/version.c"
  74. #include "../tools/checksum32.c"
  75. #include "../tools/error.c"
  76. #endif
  77. /*====================================================================*
  78. *
  79. * void copyimage (struct _file_ * file, signed extent, signed image)
  80. *
  81. *
  82. *
  83. * Contributor(s):
  84. * Charles Maier
  85. *
  86. *--------------------------------------------------------------------*/
  87. static void copyimage (struct _file_ * file, signed extent, signed image)
  88. {
  89. char buffer [1024];
  90. signed length = sizeof (buffer);
  91. while (extent)
  92. {
  93. if (length > extent)
  94. {
  95. length = extent;
  96. }
  97. if (read (file->file, buffer, length) < length)
  98. {
  99. error (1, errno, NVM_IMG_CANTREAD, file->name, image);
  100. }
  101. if (write (STDOUT_FILENO, buffer, length) < length)
  102. {
  103. error (1, errno, NVM_IMG_CANTSAVE, file->name, image);
  104. }
  105. extent -= length;
  106. }
  107. return;
  108. }
  109. /*====================================================================*
  110. *
  111. * void function1 (char const * filename, signed index, flag_t flags);
  112. *
  113. * concatenate Atheros image files having the older header format;
  114. *
  115. * the major and minor header version must be 0x6000 and 0x0000
  116. * and both header links must be 0x00000000;
  117. *
  118. *
  119. * Contributor(s):
  120. * Charles Maier
  121. *
  122. *--------------------------------------------------------------------*/
  123. static void function1 (struct _file_ * file, signed index, flag_t flags)
  124. {
  125. static signed image = 0;
  126. static uint32_t offset = 0;
  127. struct nvm_header1 nvm_header;
  128. if (read (file->file, &nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  129. {
  130. error (1, errno, NVM_HDR_CANTREAD, file->name, image);
  131. }
  132. if (LE32TOH (nvm_header.HEADERVERSION) != 0x60000000)
  133. {
  134. error (1, 0, NVM_HDR_VERSION, file->name, image);
  135. }
  136. if (checksum32 (&nvm_header, sizeof (nvm_header), 0))
  137. {
  138. error (1, 0, NVM_HDR_CHECKSUM, file->name, image);
  139. }
  140. if (nvm_header.NEXTHEADER)
  141. {
  142. error (1, 0, NVM_HDR_LINK, file->name, image);
  143. }
  144. if (index)
  145. {
  146. offset += sizeof (nvm_header);
  147. offset += LE32TOH (nvm_header.IMAGELENGTH);
  148. nvm_header.NEXTHEADER = HTOLE32 (offset);
  149. }
  150. nvm_header.HEADERCHECKSUM = 0;
  151. nvm_header.HEADERCHECKSUM = checksum32 (&nvm_header, sizeof (nvm_header), 0);
  152. if (write (STDOUT_FILENO, &nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  153. {
  154. error (1, errno, NVM_HDR_CANTSAVE, file->name, image);
  155. }
  156. copyimage (file, LE32TOH (nvm_header.IMAGELENGTH), image);
  157. return;
  158. }
  159. /*====================================================================*
  160. *
  161. * void function2 (char const * filename, signed index, flag_t flags);
  162. *
  163. * concatenate Atheros image files having the newer header format;
  164. *
  165. * the manor and minor header version must be 0x0001 and 0x0001
  166. * and both header links must be 0xFFFFFFFF;
  167. *
  168. *
  169. * Contributor(s):
  170. * Charles Maier
  171. *
  172. *--------------------------------------------------------------------*/
  173. static void function2 (struct _file_ * file, signed index, flag_t flags)
  174. {
  175. static signed image = 0;
  176. static signed origin = ~0;
  177. static signed offset = 0;
  178. struct nvm_header2 nvm_header;
  179. if (read (file->file, &nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  180. {
  181. error (1, errno, NVM_HDR_CANTREAD, file->name, image);
  182. }
  183. if (LE16TOH (nvm_header.MajorVersion) != 1)
  184. {
  185. error (1, 0, NVM_HDR_VERSION, file->name, image);
  186. }
  187. if (LE16TOH (nvm_header.MinorVersion) != 1)
  188. {
  189. error (1, 0, NVM_HDR_VERSION, file->name, image);
  190. }
  191. if (checksum32 (&nvm_header, sizeof (nvm_header), 0))
  192. {
  193. error (1, 0, NVM_HDR_CHECKSUM, file->name, image);
  194. }
  195. if (~nvm_header.PrevHeader)
  196. {
  197. error (1, 0, NVM_HDR_LINK, file->name, image);
  198. }
  199. if (~nvm_header.NextHeader)
  200. {
  201. error (1, 0, NVM_HDR_LINK, file->name, image);
  202. }
  203. nvm_header.PrevHeader = HTOLE32 (origin);
  204. origin = offset;
  205. if (index)
  206. {
  207. offset += sizeof (nvm_header);
  208. offset += LE32TOH (nvm_header.ImageLength);
  209. nvm_header.NextHeader = HTOLE32 (offset);
  210. }
  211. nvm_header.HeaderChecksum = 0;
  212. nvm_header.HeaderChecksum = checksum32 (&nvm_header, sizeof (nvm_header), 0);
  213. if (write (STDOUT_FILENO, &nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  214. {
  215. error (1, errno, NVM_HDR_CANTSAVE, file->name, image);
  216. }
  217. copyimage (file, LE32TOH (nvm_header.ImageLength), image);
  218. image++;
  219. return;
  220. }
  221. /*====================================================================*
  222. *
  223. * void function (char const * filename, signed count, flag_t flags);
  224. *
  225. *
  226. *
  227. * Contributor(s):
  228. * Charles Maier
  229. *
  230. *--------------------------------------------------------------------*/
  231. static void function (char const * filename, signed index, flag_t flags)
  232. {
  233. uint32_t version;
  234. struct _file_ file =
  235. {
  236. -1,
  237. filename
  238. };
  239. if (_anyset (flags, NVM_VERBOSE))
  240. {
  241. error (0, 0, "%s", file.name);
  242. }
  243. if ((file.file = open (file.name, O_BINARY|O_RDONLY)) == -1)
  244. {
  245. error (1, errno, FILE_CANTOPEN, file.name);
  246. }
  247. if (read (file.file, &version, sizeof (version)) != sizeof (version))
  248. {
  249. error (1, errno, FILE_CANTREAD, file.name);
  250. }
  251. if (lseek (file.file, 0, SEEK_SET))
  252. {
  253. error (1, errno, FILE_CANTHOME, file.name);
  254. }
  255. if (LE32TOH (version) == 0x60000000)
  256. {
  257. function1 (&file, index, flags);
  258. }
  259. else
  260. {
  261. function2 (&file, index, flags);
  262. }
  263. close (file.file);
  264. return;
  265. }
  266. /*====================================================================*
  267. *
  268. * int main (int argc, char const * argv []);
  269. *
  270. *
  271. *
  272. * Contributor(s):
  273. * Charles Maier
  274. *
  275. *--------------------------------------------------------------------*/
  276. int main (int argc, char const * argv [])
  277. {
  278. static char const * optv [] =
  279. {
  280. "qv",
  281. "file [file] [...] [> file]",
  282. "Qualcomm Atheros PLC Firmware Image File Splicer",
  283. "q\tsuppress messages",
  284. "v\tverbose messages",
  285. (char const *) (0)
  286. };
  287. flag_t flags = (flag_t)(0);
  288. signed c;
  289. optind = 1;
  290. while ((c = getoptv (argc, argv, optv)) != -1)
  291. {
  292. switch ((char) (c))
  293. {
  294. case 'q':
  295. _setbits (flags, NVM_SILENCE);
  296. break;
  297. case 'v':
  298. _setbits (flags, NVM_VERBOSE);
  299. break;
  300. default:
  301. break;
  302. }
  303. }
  304. argc -= optind;
  305. argv += optind;
  306. #ifdef WIN32
  307. setmode (fileno (stdin), O_BINARY);
  308. setmode (fileno (stdout), O_BINARY);
  309. #endif
  310. while ((argc) && (* argv))
  311. {
  312. function (* argv, argc-1, flags);
  313. argv++;
  314. argc--;
  315. }
  316. return (0);
  317. }