main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * nmrpflash - Netgear Unbrick Utility
  3. * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
  4. *
  5. * nmrpflash is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * nmrpflash is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with nmrpflash. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <unistd.h>
  20. #include <getopt.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include "nmrpd.h"
  24. void usage(FILE *fp)
  25. {
  26. fprintf(fp,
  27. "Usage: nmrpflash [OPTIONS...]\n"
  28. "\n"
  29. "Options (-i, and -f or -c are mandatory):\n"
  30. " -a <ipaddr> IP address to assign to target device [%s]\n"
  31. " -A <ipaddr> IP address to assign to selected interface [%s]\n"
  32. " -B Blind mode (don't wait for response packets)\n"
  33. " -c <command> Command to run before (or instead of) TFTP upload\n"
  34. " -f <firmware> Firmware file\n"
  35. " -F <filename> Remote filename to use during TFTP upload\n"
  36. " -i <interface> Network interface directly connected to device\n"
  37. " -m <mac> MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
  38. " -M <netmask> Subnet mask to assign to target device [%s]\n"
  39. " -t <timeout> Timeout (in milliseconds) for NMRP packets [%d ms]\n"
  40. " -T <timeout> Time (seconds) to wait after successfull TFTP upload [%d s]\n"
  41. " -p <port> Port to use for TFTP upload [%d]\n"
  42. #ifdef NMRPFLASH_SET_REGION
  43. " -R <region> Set device region (NA, WW, GR, PR, RU, BZ, IN, KO, JP)\n"
  44. #endif
  45. " -S <n> Skip <n> bytes of the firmware file\n"
  46. #ifdef NMRPFLASH_TFTP_TEST
  47. " -U Test TFTP upload\n"
  48. #endif
  49. " -v Be verbose\n"
  50. " -V Print version and exit\n"
  51. " -L List network interfaces\n"
  52. " -h Show this screen\n"
  53. "\n"
  54. "Example: (run as "
  55. #ifndef NMRPFLASH_WINDOWS
  56. "root"
  57. #else
  58. "administrator"
  59. #endif
  60. ")\n\n"
  61. #ifndef NMRPFLASH_WINDOWS
  62. "# nmrpflash -i eth0 -f firmware.bin\n"
  63. #else
  64. "C:\\> nmrpflash.exe -i net0 -f firmware.bin\n"
  65. #endif
  66. "\n"
  67. "When using -c, the environment variables IP, PORT, NETMASK\n"
  68. "and MAC are set to the device IP address, TFTP port, subnet\n"
  69. "mask and MAC address, respectively.\n"
  70. "\n"
  71. "nmrpflash %s, Copyright (C) 2016-2022 Joseph C. Lehner\n"
  72. "nmrpflash is free software, licensed under the GNU GPLv3.\n"
  73. "Source code at https://github.com/jclehner/nmrpflash\n"
  74. "\n",
  75. NMRP_DEFAULT_IP_REMOTE,
  76. NMRP_DEFAULT_IP_LOCAL,
  77. NMRP_DEFAULT_SUBNET,
  78. NMRP_DEFAULT_RX_TIMEOUT_MS,
  79. NMRP_DEFAULT_UL_TIMEOUT_S,
  80. NMRP_DEFAULT_TFTP_PORT,
  81. NMRPFLASH_VERSION
  82. );
  83. }
  84. #ifdef NMRPFLASH_WINDOWS
  85. void require_admin()
  86. {
  87. SID_IDENTIFIER_AUTHORITY auth = { SECURITY_NT_AUTHORITY };
  88. PSID group = NULL;
  89. BOOL admin, success = AllocateAndInitializeSid(
  90. &auth, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
  91. 0, 0, 0, 0, 0, 0, &group
  92. );
  93. if (success) {
  94. success = CheckTokenMembership(NULL, group, &admin);
  95. FreeSid(group);
  96. if (success) {
  97. if (!admin) {
  98. fprintf(stderr, "Error: must be run as administrator\n");
  99. exit(1);
  100. } else {
  101. return;
  102. }
  103. }
  104. }
  105. fprintf(stderr, "Warning: failed to check administrator privileges\n");
  106. }
  107. void show_exit_prompt()
  108. {
  109. DWORD pid;
  110. HWND win = GetConsoleWindow();
  111. if (!win || !GetWindowThreadProcessId(win, &pid)) {
  112. return;
  113. }
  114. if (GetCurrentProcessId() == pid) {
  115. printf("Press any key to exit\n");
  116. getch();
  117. }
  118. }
  119. #else
  120. void require_admin()
  121. {
  122. if (getuid() != 0) {
  123. fprintf(stderr, "Error: must be run as root\n");
  124. exit(1);
  125. }
  126. }
  127. #endif
  128. int main(int argc, char **argv)
  129. {
  130. int c, val, max;
  131. bool list = false, have_dest_mac = false;
  132. struct nmrpd_args args = {
  133. .rx_timeout = NMRP_DEFAULT_RX_TIMEOUT_MS,
  134. .ul_timeout = NMRP_DEFAULT_UL_TIMEOUT_S * 1000,
  135. .tftpcmd = NULL,
  136. .file_local = NULL,
  137. .file_remote = NULL,
  138. .ipaddr_intf = NULL,
  139. .ipaddr = NULL,
  140. .ipmask = NMRP_DEFAULT_SUBNET,
  141. .intf = NULL,
  142. .mac = "ff:ff:ff:ff:ff:ff",
  143. .op = NMRP_UPLOAD_FW,
  144. .port = NMRP_DEFAULT_TFTP_PORT,
  145. .region = NULL,
  146. .blind = false,
  147. .offset = 0,
  148. };
  149. #ifdef NMRPFLASH_WINDOWS
  150. char *newpath = NULL;
  151. char *oldpath = NULL;
  152. char *windir = NULL;
  153. WSADATA wsa;
  154. atexit(&show_exit_prompt);
  155. val = WSAStartup(MAKEWORD(2, 2), &wsa);
  156. if (val != 0) {
  157. win_perror2("WSAStartup", val);
  158. return 1;
  159. }
  160. #ifndef _WIN64
  161. // This dirty hack works around the WOW64 file system redirector[1], which would prevent
  162. // us from calling programs residing in %windir%\System32 when running on a 64bit system
  163. // (since nmrpflash is currently shipped as 32bit only).
  164. //
  165. // [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
  166. oldpath = getenv("PATH");
  167. windir = getenv("WINDIR");
  168. if (oldpath && windir) {
  169. newpath = malloc(strlen(oldpath) + strlen(windir) + 32);
  170. sprintf(newpath, "%s;%s\\Sysnative", oldpath, windir);
  171. SetEnvironmentVariable("PATH", newpath);
  172. free(newpath);
  173. }
  174. #endif
  175. #endif
  176. opterr = 0;
  177. while ((c = getopt(argc, argv, "a:A:Bc:f:F:i:m:M:p:R:S:t:T:hLVvU")) != -1) {
  178. max = 0x7fffffff;
  179. switch (c) {
  180. case 'a':
  181. args.ipaddr = optarg;
  182. break;
  183. case 'A':
  184. args.ipaddr_intf = optarg;
  185. break;
  186. case 'B':
  187. args.blind = true;
  188. break;
  189. case 'c':
  190. args.tftpcmd = optarg;
  191. break;
  192. case 'f':
  193. args.file_local = optarg;
  194. break;
  195. case 'F':
  196. args.file_remote = optarg;
  197. break;
  198. case 'i':
  199. args.intf = optarg;
  200. break;
  201. case 'm':
  202. args.mac = optarg;
  203. have_dest_mac = true;
  204. break;
  205. case 'M':
  206. args.ipmask = optarg;
  207. break;
  208. #ifdef NMRPFLASH_SET_REGION
  209. case 'R':
  210. args.region = optarg;
  211. break;
  212. #endif
  213. case 'p':
  214. case 'S':
  215. case 'T':
  216. case 't':
  217. if (c == 'p') {
  218. max = 0xffff;
  219. }
  220. val = atoi(optarg);
  221. if (val <= 0 || val > max) {
  222. fprintf(stderr, "Invalid numeric value for -%c.\n", c);
  223. return 1;
  224. }
  225. if (c == 'p') {
  226. args.port = val;
  227. } else if (c == 't') {
  228. args.rx_timeout = val;
  229. } else if (c == 'T') {
  230. args.ul_timeout = val * 1000;
  231. } else if (c == 'S') {
  232. args.offset = val;
  233. }
  234. break;
  235. case 'V':
  236. printf("nmrpflash %s\n", NMRPFLASH_VERSION);
  237. val = 0;
  238. goto out;
  239. case 'v':
  240. ++verbosity;
  241. break;
  242. case 'L':
  243. list = true;
  244. break;
  245. case 'h':
  246. usage(stdout);
  247. val = 0;
  248. goto out;
  249. #ifdef NMRPFLASH_TFTP_TEST
  250. case 'U':
  251. if (args.ipaddr && args.file_local) {
  252. val = tftp_put(&args);
  253. goto out;
  254. }
  255. /* fall through */
  256. #endif
  257. default:
  258. usage(stderr);
  259. val = 1;
  260. goto out;
  261. }
  262. }
  263. if (args.ipaddr_intf && !args.ipaddr) {
  264. fprintf(stderr, "Error: cannot use -A <ipaddr> without using -a <ipaddr>.\n");
  265. return 1;
  266. }
  267. if (args.blind && !have_dest_mac) {
  268. fprintf(stderr, "Error: use of -B requires -m <mac>.\n");
  269. return 1;
  270. }
  271. #ifndef NMRPFLASH_FUZZ
  272. if (!list && ((!args.file_local && !args.tftpcmd) || !args.intf)) {
  273. usage(stderr);
  274. return 1;
  275. }
  276. if (!list) {
  277. require_admin();
  278. }
  279. #endif
  280. val = !list ? nmrp_do(&args) : ethsock_list_all();
  281. out:
  282. #ifdef NMRPFLASH_WINDOWS
  283. WSACleanup();
  284. #endif
  285. return val;
  286. }