nvmsplit.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * nvmsplit.c -
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. /*====================================================================*
  17. * system header files;
  18. *--------------------------------------------------------------------*/
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. /*====================================================================*
  24. * custom header files;
  25. *--------------------------------------------------------------------*/
  26. #include "../tools/getoptv.h"
  27. #include "../tools/memory.h"
  28. #include "../tools/flags.h"
  29. #include "../tools/files.h"
  30. #include "../tools/error.h"
  31. #include "../nvm/nvm.h"
  32. /*====================================================================*
  33. * custom source files;
  34. *--------------------------------------------------------------------*/
  35. #ifndef MAKEFILE
  36. #include "../tools/getoptv.c"
  37. #include "../tools/putoptv.c"
  38. #include "../tools/version.c"
  39. #include "../tools/checksum32.c"
  40. #include "../tools/error.c"
  41. #endif
  42. /*====================================================================*
  43. *
  44. * void function1 (char const * filename, flag_t flags);
  45. *
  46. * open an Atheros Qualcomm PLC firmware image file and write each
  47. * component as an independent image file;
  48. *
  49. *
  50. * Contributor(s):
  51. * Charles Maier <cmaier@qca.qualcomm.com>
  52. *
  53. *--------------------------------------------------------------------*/
  54. static void function1 (char const * filename, flag_t flags)
  55. {
  56. struct lightning_nvm_header nvm_header;
  57. char savename [FILENAME_MAX];
  58. byte buffer [BUFSIZ];
  59. signed image = 0;
  60. signed offset = 0;
  61. signed ifd;
  62. signed ofd;
  63. char * sp;
  64. char * cp;
  65. memset (& nvm_header, 0, sizeof (nvm_header));
  66. for (sp = cp = strcpy (savename, filename); * cp; cp++)
  67. {
  68. if (* cp == FILE_C_EXTENDER)
  69. {
  70. sp = cp;
  71. }
  72. }
  73. if (sp == savename)
  74. {
  75. sp = cp;
  76. }
  77. if ((ifd = open (filename, O_BINARY | O_RDONLY)) == - 1)
  78. {
  79. error (1, errno, FILE_CANTOPEN, filename);
  80. }
  81. do
  82. {
  83. signed extent;
  84. signed length;
  85. if (lseek (ifd, 0, SEEK_CUR) != offset)
  86. {
  87. error (1, errno, NVM_HDR_LINK, filename, image);
  88. }
  89. if (read (ifd, & nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  90. {
  91. error (1, errno, NVM_HDR_CANTREAD, filename, image);
  92. }
  93. if (LE32TOH (nvm_header.HEADERVERSION) != 0x60000000)
  94. {
  95. error (1, 0, NVM_HDR_VERSION, filename, image);
  96. }
  97. if (checksum32 (& nvm_header, sizeof (nvm_header), 0))
  98. {
  99. error (1, 0, NVM_HDR_CHECKSUM, filename, image);
  100. }
  101. #if defined (WIN32)
  102. sprintf (sp, "-%02d.nvm", image);
  103. #else
  104. snprintf (sp, savename + sizeof (savename) - sp, "-%02d.nvm", image);
  105. #endif
  106. if ((ofd = open (savename, O_BINARY | O_CREAT | O_RDWR | O_TRUNC, FILE_FILEMODE)) == - 1)
  107. {
  108. error (1, errno, FILE_CANTOPEN, savename);
  109. }
  110. if (_anyset (flags, NVM_VERBOSE))
  111. {
  112. error (0, 0, "%s", savename);
  113. }
  114. offset = LE32TOH (nvm_header.NEXTHEADER);
  115. nvm_header.NEXTHEADER = 0;
  116. nvm_header.HEADERCHECKSUM = 0;
  117. nvm_header.HEADERCHECKSUM = checksum32 (& nvm_header, sizeof (nvm_header), 0);
  118. if (write (ofd, & nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  119. {
  120. error (1, errno, FILE_CANTSAVE, savename);
  121. }
  122. extent = LE32TOH (nvm_header.IMAGELENGTH);
  123. length = sizeof (buffer);
  124. while (extent)
  125. {
  126. if (length > extent)
  127. {
  128. length = extent;
  129. }
  130. if (read (ifd, buffer, length) < length)
  131. {
  132. error (1, errno, NVM_IMG_CANTREAD, filename, image);
  133. }
  134. if (write (ofd, buffer, length) < length)
  135. {
  136. error (1, errno, NVM_IMG_CANTSAVE, savename, image);
  137. }
  138. extent -= length;
  139. }
  140. close (ofd);
  141. image++;
  142. }
  143. while (offset);
  144. close (ifd);
  145. return;
  146. }
  147. /*====================================================================*
  148. *
  149. * void function2 (char const * filename, flag_t flags);
  150. *
  151. * open an Atheros Qualcomm PLC firmware image file and write each
  152. * component as an independent image file;
  153. *
  154. *
  155. * Contributor(s):
  156. * Charles Maier <cmaier@qca.qualcomm.com>
  157. *
  158. *--------------------------------------------------------------------*/
  159. static void function2 (char const * filename, flag_t flags)
  160. {
  161. struct panther_nvm_header nvm_header;
  162. char savename [FILENAME_MAX];
  163. byte buffer [BUFSIZ];
  164. signed image = 0;
  165. signed offset = 0;
  166. signed ifd;
  167. signed ofd;
  168. char * sp;
  169. char * cp;
  170. memset (& nvm_header, 0, sizeof (nvm_header));
  171. for (sp = cp = strcpy (savename, filename); * cp; cp++)
  172. {
  173. if (* cp == FILE_C_EXTENDER)
  174. {
  175. sp = cp;
  176. }
  177. }
  178. if (sp == savename)
  179. {
  180. sp = cp;
  181. }
  182. if ((ifd = open (filename, O_BINARY | O_RDONLY)) == - 1)
  183. {
  184. error (1, errno, FILE_CANTOPEN, filename);
  185. }
  186. do
  187. {
  188. signed extent;
  189. signed length;
  190. if (lseek (ifd, 0, SEEK_CUR) != offset)
  191. {
  192. error (1, errno, NVM_HDR_LINK, filename, image);
  193. }
  194. if (read (ifd, & nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  195. {
  196. error (1, errno, NVM_HDR_CANTREAD, filename, image);
  197. }
  198. if (LE16TOH (nvm_header.MinorVersion) != 1)
  199. {
  200. error (1, 0, NVM_HDR_VERSION, filename, image);
  201. }
  202. if (LE16TOH (nvm_header.MajorVersion) != 1)
  203. {
  204. error (1, 0, NVM_HDR_VERSION, filename, image);
  205. }
  206. if (checksum32 (& nvm_header, sizeof (nvm_header), 0))
  207. {
  208. error (1, 0, NVM_HDR_CHECKSUM, filename, image);
  209. }
  210. #if defined (WIN32)
  211. sprintf (sp, "-%02d.nvm", image);
  212. #else
  213. snprintf (sp, savename + sizeof (savename) - sp, "-%02d.nvm", image);
  214. #endif
  215. if ((ofd = open (savename, O_BINARY | O_WRONLY | O_CREAT | O_TRUNC, FILE_FILEMODE)) == - 1)
  216. {
  217. error (1, errno, FILE_CANTOPEN, savename);
  218. }
  219. if (_anyset (flags, NVM_VERBOSE))
  220. {
  221. error (0, 0, "%s --> %s", filename, savename);
  222. }
  223. offset = LE32TOH (nvm_header.NextHeader);
  224. nvm_header.NextHeader = ~ 0;
  225. nvm_header.PrevHeader = ~ 0;
  226. nvm_header.HeaderChecksum = 0;
  227. nvm_header.HeaderChecksum = checksum32 (& nvm_header, sizeof (nvm_header), 0);
  228. if (write (ofd, & nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  229. {
  230. error (1, errno, NVM_HDR_CANTSAVE, savename, image);
  231. }
  232. extent = LE32TOH (nvm_header.ImageLength);
  233. length = sizeof (buffer);
  234. while (extent)
  235. {
  236. if (length > extent)
  237. {
  238. length = extent;
  239. }
  240. if (read (ifd, buffer, length) < length)
  241. {
  242. error (1, errno, NVM_IMG_CANTREAD, filename, image);
  243. }
  244. if (write (ofd, buffer, length) < length)
  245. {
  246. error (1, errno, NVM_IMG_CANTSAVE, savename, image);
  247. }
  248. extent -= length;
  249. }
  250. close (ofd);
  251. image++;
  252. }
  253. while (~ offset);
  254. close (ifd);
  255. return;
  256. }
  257. /*====================================================================*
  258. *
  259. * void function (struct _file_ * file, flag_t flags);
  260. *
  261. * open an Atheros Qualcomm PLC firmware image file and write each
  262. * component as an independent, single-image firmware file;
  263. *
  264. *
  265. * Contributor(s):
  266. * Charles Maier <cmaier@qca.qualcomm.com>
  267. *
  268. *--------------------------------------------------------------------*/
  269. static void function (char const * filename, flag_t flags)
  270. {
  271. uint32_t version;
  272. signed ifd;
  273. if ((ifd = open (filename, O_BINARY | O_RDONLY)) == - 1)
  274. {
  275. error (1, errno, FILE_CANTOPEN, filename);
  276. }
  277. if (read (ifd, & version, sizeof (version)) != sizeof (version))
  278. {
  279. error (1, errno, FILE_CANTREAD, filename);
  280. }
  281. close (ifd);
  282. if (LE32TOH (version) == 0x60000000)
  283. {
  284. function1 (filename, flags);
  285. }
  286. else
  287. {
  288. function2 (filename, flags);
  289. }
  290. return;
  291. }
  292. /*====================================================================*
  293. *
  294. * int main (int argc, char const * argv []);
  295. *
  296. * Contributor(s):
  297. * Charles Maier <cmaier@qca.qualcomm.com>
  298. *
  299. *--------------------------------------------------------------------*/
  300. int main (int argc, char const * argv [])
  301. {
  302. static char const * optv [] =
  303. {
  304. "qv",
  305. "file [file] [...]",
  306. "Qualcomm Atheros PLC Firmware Image File Slicer",
  307. "q\tsuppress messages",
  308. "v\tverbose messages",
  309. (char const *) (0)
  310. };
  311. flag_t flags = (flag_t) (0);
  312. signed c;
  313. optind = 1;
  314. while (~ (c = getoptv (argc, argv, optv)))
  315. {
  316. switch (c)
  317. {
  318. case 'q':
  319. _setbits (flags, NVM_SILENCE);
  320. break;
  321. case 'v':
  322. _setbits (flags, NVM_VERBOSE);
  323. break;
  324. default:
  325. break;
  326. }
  327. }
  328. argc -= optind;
  329. argv += optind;
  330. while ((argc) && (* argv))
  331. {
  332. function (* argv, flags);
  333. argc--;
  334. argv++;
  335. }
  336. return (0);
  337. }