plcboot.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. * plcboot.c -
  44. *
  45. *
  46. *--------------------------------------------------------------------*/
  47. /*====================================================================*"
  48. * system header files;
  49. *--------------------------------------------------------------------*/
  50. #include <unistd.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <limits.h>
  54. #include <fcntl.h>
  55. #include <ctype.h>
  56. #include <sys/stat.h>
  57. /*====================================================================*
  58. * custom header files;
  59. *--------------------------------------------------------------------*/
  60. #include "../tools/getoptv.h"
  61. #include "../tools/putoptv.h"
  62. #include "../tools/memory.h"
  63. #include "../tools/symbol.h"
  64. #include "../tools/number.h"
  65. #include "../tools/types.h"
  66. #include "../tools/flags.h"
  67. #include "../tools/files.h"
  68. #include "../tools/error.h"
  69. #include "../ram/nvram.h"
  70. #include "../ram/sdram.h"
  71. #include "../plc/plc.h"
  72. #include "../nvm/nvm.h"
  73. #include "../pib/pib.h"
  74. #include "../mme/mme.h"
  75. /*====================================================================*
  76. * custom source files;
  77. *--------------------------------------------------------------------*/
  78. #ifndef MAKEFILE
  79. #include "../plc/BootDevice2.c"
  80. #include "../plc/BootFirmware2.c"
  81. #include "../plc/BootParameters2.c"
  82. #include "../plc/chipset.c"
  83. #include "../plc/Confirm.c"
  84. #include "../plc/Devices.c"
  85. #include "../plc/Display.c"
  86. #include "../plc/Failure.c"
  87. #include "../plc/FlashDevice2.c"
  88. #include "../plc/FlashNVM.c"
  89. #include "../plc/FlashSoftloader.c"
  90. #include "../plc/FlashParameters.c"
  91. #include "../plc/FlashFirmware.c"
  92. #include "../plc/InitDevice2.c"
  93. #include "../plc/ModuleCommit.c"
  94. #include "../plc/ModuleSession.c"
  95. #include "../plc/ModuleSpec.c"
  96. #include "../plc/ModuleWrite.c"
  97. #include "../plc/NVMSelect.c"
  98. #include "../plc/ReadMME.c"
  99. #include "../plc/Request.c"
  100. #include "../plc/ResetDevice.c"
  101. #include "../plc/SendMME.c"
  102. #include "../plc/WaitForReset.c"
  103. #include "../plc/WaitForStart.c"
  104. #include "../plc/WriteExecuteApplet2.c"
  105. #include "../plc/WriteExecuteFirmware2.c"
  106. #include "../plc/WriteExecuteParameters2.c"
  107. #include "../plc/WriteFirmware2.c"
  108. #include "../plc/WriteMEM.c"
  109. #include "../plc/WriteNVM.c"
  110. #include "../plc/WritePIB.c"
  111. #include "../plc/WriteParameters2.c"
  112. #endif
  113. #ifndef MAKEFILE
  114. #include "../tools/basespec.c"
  115. #include "../tools/checkfilename.c"
  116. #include "../tools/checksum32.c"
  117. #include "../tools/error.c"
  118. #include "../tools/fdchecksum32.c"
  119. #include "../tools/getoptv.c"
  120. #include "../tools/hexdecode.c"
  121. #include "../tools/hexstring.c"
  122. #include "../tools/hexdump.c"
  123. #include "../tools/putoptv.c"
  124. #include "../tools/strfbits.c"
  125. #include "../tools/todigit.c"
  126. #include "../tools/typename.c"
  127. #include "../tools/uintspec.c"
  128. #include "../tools/version.c"
  129. #endif
  130. #ifndef MAKEFILE
  131. #include "../ether/channel.c"
  132. #include "../ether/closechannel.c"
  133. #include "../ether/openchannel.c"
  134. #include "../ether/readpacket.c"
  135. #include "../ether/sendpacket.c"
  136. #endif
  137. #ifndef MAKEFILE
  138. #include "../nvm/nvmfile2.c"
  139. #endif
  140. #ifndef MAKEFILE
  141. #include "../pib/pibfile2.c"
  142. #endif
  143. #ifndef MAKEFILE
  144. #include "../mme/EthernetHeader.c"
  145. #include "../mme/QualcommHeader.c"
  146. #include "../mme/UnwantedMessage.c"
  147. #include "../mme/MMECode.c"
  148. #endif
  149. #ifndef MAKEFILE
  150. #include "../key/keys.c"
  151. #endif
  152. /*====================================================================*
  153. *
  154. * int main (int argc, char const * argv[]);
  155. *
  156. *
  157. *--------------------------------------------------------------------*/
  158. int main (int argc, char const * argv [])
  159. {
  160. extern struct channel channel;
  161. static char const * optv [] =
  162. {
  163. "eFi:N:p:P:qS:t:vx",
  164. "-N file -P file [device] [device] [...]",
  165. "Qualcomm Atheros Panther/Lynx Powerline Device Bootstrapper",
  166. "e\tredirect stderr to stdout",
  167. "F[F]\tFlash [Force] non-volatile memory after boot",
  168. #if defined (WINPCAP) || defined (LIBPCAP)
  169. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  170. #else
  171. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  172. #endif
  173. "N f\tfirmware file is (f)",
  174. "P f\tparameter file is (f)",
  175. "q\tquiet mode",
  176. "S f\tsoftloader file is (f)",
  177. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  178. "v\tverbose mode",
  179. "x\texit on error",
  180. (char const *) (0)
  181. };
  182. #include "../plc/plc.c"
  183. char firmware [PLC_VERSION_STRING];
  184. signed c;
  185. if (getenv (PLCDEVICE))
  186. {
  187. #if defined (WINPCAP) || defined (LIBPCAP)
  188. channel.ifindex = atoi (getenv (PLCDEVICE));
  189. #else
  190. channel.ifname = strdup (getenv (PLCDEVICE));
  191. #endif
  192. }
  193. optind = 1;
  194. while ((c = getoptv (argc, argv, optv)) != -1)
  195. {
  196. switch (c)
  197. {
  198. case 'e':
  199. dup2 (STDOUT_FILENO, STDERR_FILENO);
  200. break;
  201. case 'F':
  202. _setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
  203. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  204. {
  205. _setbits (plc.module, VS_MODULE_FORCE);
  206. }
  207. _setbits (plc.flags, PLC_FLASH_DEVICE);
  208. break;
  209. case 'i':
  210. #if defined (WINPCAP) || defined (LIBPCAP)
  211. channel.ifindex = atoi (optarg);
  212. #else
  213. channel.ifname = optarg;
  214. #endif
  215. break;
  216. case 'N':
  217. if (!checkfilename (optarg))
  218. {
  219. error (1, EINVAL, "%s", optarg);
  220. }
  221. if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1)
  222. {
  223. error (1, errno, "%s", plc.NVM.name);
  224. }
  225. if (nvmfile2 (&plc.NVM))
  226. {
  227. error (1, errno, "Bad firmware file: %s", plc.NVM.name);
  228. }
  229. _setbits (plc.flags, PLC_WRITE_MAC);
  230. break;
  231. case 'P':
  232. if (!checkfilename (optarg))
  233. {
  234. error (1, EINVAL, "%s", optarg);
  235. }
  236. if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1)
  237. {
  238. error (1, errno, "%s", plc.PIB.name);
  239. }
  240. if (pibfile2 (&plc.PIB))
  241. {
  242. error (1, errno, "Bad parameter file: %s", plc.PIB.name);
  243. }
  244. _setbits (plc.flags, PLC_WRITE_PIB);
  245. break;
  246. case 'q':
  247. _setbits (channel.flags, CHANNEL_SILENCE);
  248. _setbits (plc.flags, PLC_SILENCE);
  249. break;
  250. case 'S':
  251. if (!checkfilename (optarg))
  252. {
  253. error (1, EINVAL, "%s", optarg);
  254. }
  255. if ((plc.SFT.file = open (plc.SFT.name = optarg, O_BINARY|O_RDONLY)) == -1)
  256. {
  257. error (1, errno, "%s", plc.SFT.name);
  258. }
  259. if (nvmfile2 (&plc.SFT))
  260. {
  261. error (1, errno, "Bad softloader file: %s", plc.SFT.name);
  262. }
  263. break;
  264. case 't':
  265. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  266. break;
  267. case 'v':
  268. _setbits (channel.flags, CHANNEL_VERBOSE);
  269. _setbits (plc.flags, PLC_VERBOSE);
  270. break;
  271. case 'x':
  272. _setbits (plc.flags, PLC_BAILOUT);
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. argc -= optind;
  279. argv += optind;
  280. if (argc)
  281. {
  282. error (1, ENOTSUP, ERROR_TOOMANY);
  283. }
  284. openchannel (&channel);
  285. if (!(plc.message = malloc (sizeof (* plc.message))))
  286. {
  287. error (1, errno, PLC_NOMEMORY);
  288. }
  289. if (WaitForStart (&plc, firmware, sizeof (firmware)))
  290. {
  291. Failure (&plc, PLC_NODETECT);
  292. exit (1);
  293. }
  294. if (strcmp (firmware, "BootLoader"))
  295. {
  296. Failure (&plc, "Bootloader must be running");
  297. exit (1);
  298. }
  299. if (plc.hardwareID < CHIPSET_INT6400)
  300. {
  301. Failure (&plc, "Device must be %s or later; Use program int6kboot or ampboot instead.", chipsetname (CHIPSET_INT6400));
  302. exit (1);
  303. }
  304. if (plc.PIB.file == -1)
  305. {
  306. error (1, ECANCELED, "No Parameter file named");
  307. }
  308. if (plc.NVM.file == -1)
  309. {
  310. error (1, ECANCELED, "No Firmware file named");
  311. }
  312. if (plc.SFT.file == -1)
  313. {
  314. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  315. {
  316. error (1, ECANCELED, "No Softloader file named");
  317. }
  318. }
  319. if (!InitDevice2 (&plc))
  320. {
  321. if (!BootDevice2 (&plc))
  322. {
  323. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  324. {
  325. FlashDevice2 (& plc, (PLC_COMMIT_FORCE | PLC_COMMIT_NORESET | PLC_COMMIT_FACTPIB));
  326. }
  327. }
  328. }
  329. free (plc.message);
  330. closechannel (&channel);
  331. exit (0);
  332. }