int6kbootp.c 8.2 KB

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