ampboot.c 8.7 KB

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