int6kboot.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. * int6kboot.c - Qualcomm Atheros INT6x00 Powerline Device Bootstrapper
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. *
  49. *--------------------------------------------------------------------*/
  50. /*====================================================================*"
  51. * system header files;
  52. *--------------------------------------------------------------------*/
  53. #include <unistd.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <limits.h>
  57. #include <fcntl.h>
  58. #include <ctype.h>
  59. #include <sys/stat.h>
  60. /*====================================================================*
  61. * custom header files;
  62. *--------------------------------------------------------------------*/
  63. #include "../tools/getoptv.h"
  64. #include "../tools/putoptv.h"
  65. #include "../tools/memory.h"
  66. #include "../tools/symbol.h"
  67. #include "../tools/number.h"
  68. #include "../tools/types.h"
  69. #include "../tools/flags.h"
  70. #include "../tools/files.h"
  71. #include "../tools/error.h"
  72. #include "../plc/plc.h"
  73. #include "../ram/nvram.h"
  74. #include "../ram/sdram.h"
  75. #include "../nvm/nvm.h"
  76. #include "../pib/pib.h"
  77. /*====================================================================*
  78. * custom source files;
  79. *--------------------------------------------------------------------*/
  80. #ifndef MAKEFILE
  81. #include "../plc/chipset.c"
  82. #include "../plc/Confirm.c"
  83. #include "../plc/Devices.c"
  84. #include "../plc/Display.c"
  85. #include "../plc/Failure.c"
  86. #include "../plc/FlashNVM.c"
  87. #include "../plc/ReadMME.c"
  88. #include "../plc/Request.c"
  89. #include "../plc/SendMME.c"
  90. #include "../plc/WriteMEM.c"
  91. #include "../plc/WriteNVM.c"
  92. #include "../plc/WritePIB.c"
  93. #include "../plc/WaitForReset.c"
  94. #include "../plc/WaitForStart.c"
  95. #include "../plc/WaitForRestart.c"
  96. #include "../plc/InitDevice1.c"
  97. #include "../plc/BootDevice1.c"
  98. #include "../plc/BootFirmware1.c"
  99. #include "../plc/BootParameters1.c"
  100. #include "../plc/WriteFirmware1.c"
  101. #include "../plc/WriteExecuteFirmware1.c"
  102. #include "../plc/WriteExecutePIB.c"
  103. #include "../plc/StartFirmware1.c"
  104. #include "../plc/FlashDevice1.c"
  105. #endif
  106. #ifndef MAKEFILE
  107. #include "../tools/getoptv.c"
  108. #include "../tools/putoptv.c"
  109. #include "../tools/version.c"
  110. #include "../tools/uintspec.c"
  111. #include "../tools/checkfilename.c"
  112. #include "../tools/hexdecode.c"
  113. #include "../tools/hexstring.c"
  114. #include "../tools/todigit.c"
  115. #include "../tools/hexdump.c"
  116. #include "../tools/checksum32.c"
  117. #include "../tools/fdchecksum32.c"
  118. #include "../tools/error.c"
  119. #include "../tools/strfbits.c"
  120. #include "../tools/typename.c"
  121. #endif
  122. #ifndef MAKEFILE
  123. // #include "../ram/sdramfile.c"// #include "../ram/sdrampeek.c"
  124. #endif
  125. #ifndef MAKEFILE
  126. #include "../nvm/nvmfile1.c"
  127. #endif
  128. #ifndef MAKEFILE
  129. #include "../pib/pibfile1.c"
  130. #include "../pib/pibpeek1.c"
  131. #endif
  132. #ifndef MAKEFILE
  133. #include "../ether/openchannel.c"
  134. #include "../ether/closechannel.c"
  135. #include "../ether/readpacket.c"
  136. #include "../ether/sendpacket.c"
  137. #include "../ether/channel.c"
  138. #endif
  139. #ifndef MAKEFILE
  140. #include "../mme/MMECode.c"
  141. #include "../mme/EthernetHeader.c"
  142. #include "../mme/QualcommHeader.c"
  143. #include "../mme/UnwantedMessage.c"
  144. #endif
  145. #ifndef MAKEFILE
  146. #include "../key/keys.c"
  147. #endif
  148. /*====================================================================*
  149. *
  150. * int main (int argc, char const * argv[]);
  151. *
  152. * parse command line, populate plc structure and perform selected
  153. * operations; show help summary when asked; see getoptv and putoptv
  154. * to understand command line parsing and help summary display; see
  155. * plc.h for the definition of struct plc;
  156. *
  157. * the default interface is eth1 because most people use eth0 as
  158. * their principle network connection; you can specify another
  159. * interface with -i or define environment string PLC to make
  160. * that the default interface and save typing;
  161. *
  162. *
  163. *--------------------------------------------------------------------*/
  164. int main (int argc, char const * argv [])
  165. {
  166. extern struct channel channel;
  167. static char const * optv [] =
  168. {
  169. "ei:FN:p:P:qt:vx",
  170. "-N file -P file [device] [device] [...]",
  171. "Qualcomm Atheros INT6x00 Powerline Device Bootstrapper",
  172. "e\tredirect stderr to stdout",
  173. #if defined (WINPCAP) || defined (LIBPCAP)
  174. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  175. #else
  176. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  177. #endif
  178. "F[F]\tflash [force] NVRAM after firmware start using VS_MOD_NVM",
  179. "N f\twrite NVM file to device using VS_WR_MEM",
  180. "P f\twrite PIB file to device using VS_WR_MEM",
  181. "q\tquiet mode",
  182. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  183. "v\tverbose mode",
  184. "x\texit on error",
  185. (char const *) (0)
  186. };
  187. #include "../plc/plc.c"
  188. char firmware [PLC_VERSION_STRING];
  189. signed c;
  190. if (getenv (PLCDEVICE))
  191. {
  192. #if defined (WINPCAP) || defined (LIBPCAP)
  193. channel.ifindex = atoi (getenv (PLCDEVICE));
  194. #else
  195. channel.ifname = strdup (getenv (PLCDEVICE));
  196. #endif
  197. }
  198. optind = 1;
  199. while ((c = getoptv (argc, argv, optv)) != -1)
  200. {
  201. switch (c)
  202. {
  203. case 'i':
  204. #if defined (WINPCAP) || defined (LIBPCAP)
  205. channel.ifindex = atoi (optarg);
  206. #else
  207. channel.ifname = optarg;
  208. #endif
  209. break;
  210. case 'e':
  211. dup2 (STDOUT_FILENO, STDERR_FILENO);
  212. break;
  213. case 'F':
  214. _setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
  215. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  216. {
  217. _setbits (plc.module, VS_MODULE_FORCE);
  218. }
  219. _setbits (plc.flags, PLC_FLASH_DEVICE);
  220. break;
  221. case 'N':
  222. if (!checkfilename (optarg))
  223. {
  224. error (1, EINVAL, "%s", optarg);
  225. }
  226. if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1)
  227. {
  228. error (1, errno, "%s", plc.NVM.name);
  229. }
  230. if (nvmfile1 (&plc.NVM))
  231. {
  232. error (1, errno, "Bad firmware file: %s", plc.NVM.name);
  233. }
  234. _setbits (plc.flags, PLC_WRITE_MAC);
  235. break;
  236. case 'P':
  237. if (!checkfilename (optarg))
  238. {
  239. error (1, EINVAL, "%s", optarg);
  240. }
  241. if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1)
  242. {
  243. error (1, errno, "%s", plc.PIB.name);
  244. }
  245. if (pibfile1 (&plc.PIB))
  246. {
  247. error (1, errno, "Bad parameter file: %s", plc.PIB.name);
  248. }
  249. _setbits (plc.flags, PLC_WRITE_PIB);
  250. break;
  251. case 'q':
  252. _setbits (channel.flags, CHANNEL_SILENCE);
  253. _setbits (plc.flags, PLC_SILENCE);
  254. break;
  255. case 't':
  256. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  257. break;
  258. case 'v':
  259. _setbits (channel.flags, CHANNEL_VERBOSE);
  260. _setbits (plc.flags, PLC_VERBOSE);
  261. break;
  262. case 'x':
  263. _setbits (plc.flags, PLC_BAILOUT);
  264. break;
  265. default:
  266. break;
  267. }
  268. }
  269. argc -= optind;
  270. argv += optind;
  271. if (argc)
  272. {
  273. error (1, ENOTSUP, ERROR_TOOMANY);
  274. }
  275. if (plc.PIB.file == -1)
  276. {
  277. error (1, ECANCELED, "No PIB file specified");
  278. }
  279. if (plc.NVM.file == -1)
  280. {
  281. error (1, ECANCELED, "No NVM file specified");
  282. }
  283. openchannel (&channel);
  284. if (!(plc.message = malloc (sizeof (* plc.message))))
  285. {
  286. error (1, errno, PLC_NOMEMORY);
  287. }
  288. if (WaitForStart (&plc, firmware, sizeof (firmware)))
  289. {
  290. Failure (&plc, PLC_NODETECT);
  291. exit (1);
  292. }
  293. if (strcmp (firmware, "BootLoader"))
  294. {
  295. Failure (&plc, "Bootloader must be running");
  296. exit (1);
  297. }
  298. if (plc.hardwareID < CHIPSET_INT6300)
  299. {
  300. Failure (&plc, "Device must be %s or later. Try program int6kf instead.", chipsetname (CHIPSET_INT6300));
  301. exit (1);
  302. }
  303. if (plc.hardwareID >= CHIPSET_AR7400)
  304. {
  305. Failure (&plc, "Program does not support %s or later; try program ampboot instead.", chipsetname (CHIPSET_AR7400));
  306. exit (1);
  307. }
  308. if (!InitDevice1 (&plc))
  309. {
  310. if (!BootDevice1 (&plc))
  311. {
  312. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  313. {
  314. FlashDevice1 (&plc);
  315. }
  316. }
  317. }
  318. free (plc.message);
  319. closechannel (&channel);
  320. exit (0);
  321. }