main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #define NMRPFLASH_TFTP_TEST
  25. int verbosity = 0;
  26. void usage(FILE *fp)
  27. {
  28. fprintf(fp,
  29. "Usage: nmrpflash [OPTIONS...]\n"
  30. "\n"
  31. "Options (-a, -i and -f are mandatory):\n"
  32. " -a <ipaddr> IP address to assign to target device\n"
  33. " -f <firmware> Firmware file\n"
  34. " -i <interface> Network interface directly connected to device\n"
  35. " -m <mac> MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
  36. " -M <netmask> Subnet mask to assign to target device\n"
  37. " -t <timeout> Timeout (in milliseconds) for regular messages\n"
  38. " -T <timeout> Time to wait after successfull TFTP upload\n"
  39. " -p <port> Port to use for TFTP upload\n"
  40. #ifdef NMRPFLASH_TFTP_TEST
  41. " -U Test TFTP upload\n"
  42. #endif
  43. " -v Be verbose\n"
  44. " -V Print version and exit\n"
  45. " -L List network interfaces\n"
  46. " -h Show this screen\n"
  47. "\n"
  48. "Example:\n"
  49. "\n"
  50. #ifndef NMRPFLASH_WINDOWS
  51. "$ sudo nmrpflash -i eth0 -a 192.168.1.254 -f firmware.bin\n"
  52. #else
  53. "C:\\> nmrpflash.exe -i net0 -a 192.168.1.254 -f firmware.bin\n"
  54. #endif
  55. "\n"
  56. "nmrpflash %s, Copyright (C) 2016 Joseph C. Lehner\n"
  57. "nmrpflash is free software, licensed under the GNU GPLv3.\n"
  58. "Source code at https://github.com/jclehner/nmrpflash\n"
  59. "\n",
  60. NMRPFLASH_VERSION
  61. );
  62. }
  63. int main(int argc, char **argv)
  64. {
  65. int c, val, max;
  66. struct nmrpd_args args = {
  67. .rx_timeout = 200,
  68. .ul_timeout = 60000,
  69. .tftpcmd = NULL,
  70. .filename = NULL,
  71. .ipaddr = NULL,
  72. .ipmask = "255.255.255.0",
  73. .intf = NULL,
  74. .mac = "ff:ff:ff:ff:ff:ff",
  75. .op = NMRP_UPLOAD_FW,
  76. .port = 69,
  77. .force_root = 1
  78. };
  79. #ifdef NMRPFLASH_WINDOWS
  80. WSADATA wsa;
  81. val = WSAStartup(MAKEWORD(2, 2), &wsa);
  82. if (val != 0) {
  83. win_perror2("WSAStartup", val);
  84. return 1;
  85. }
  86. #endif
  87. opterr = 0;
  88. while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:hLVvU")) != -1) {
  89. max = 0x7fffffff;
  90. switch (c) {
  91. case 'a':
  92. args.ipaddr = optarg;
  93. break;
  94. case 'f':
  95. args.filename = optarg;
  96. break;
  97. case 'i':
  98. args.intf = optarg;
  99. break;
  100. case 'm':
  101. args.mac = optarg;
  102. break;
  103. case 'M':
  104. args.ipmask = optarg;
  105. break;
  106. case 'p':
  107. max = 0xffff;
  108. case 'T':
  109. case 't':
  110. val = atoi(optarg);
  111. if (val <= 0 || val > max) {
  112. fprintf(stderr, "Invalid numeric value for -%c.\n", c);
  113. return 1;
  114. }
  115. if (c == 'p') {
  116. args.port = val;
  117. } else if (c == 't') {
  118. args.rx_timeout = val;
  119. } else {
  120. args.ul_timeout = val;
  121. }
  122. break;
  123. case 'V':
  124. printf("nmrpflash %s\n", NMRPFLASH_VERSION);
  125. val = 0;
  126. goto out;
  127. case 'v':
  128. ++verbosity;
  129. break;
  130. case 'L':
  131. val = ethsock_list_all();
  132. goto out;
  133. case 'h':
  134. usage(stdout);
  135. val = 0;
  136. goto out;
  137. #ifdef NMRPFLASH_TFTP_TEST
  138. case 'U':
  139. if (args.ipaddr && args.filename) {
  140. val = tftp_put(&args);
  141. goto out;
  142. }
  143. /* fall through */
  144. #endif
  145. default:
  146. usage(stderr);
  147. val = 1;
  148. goto out;
  149. }
  150. }
  151. if (!args.filename || !args.intf || !args.ipaddr) {
  152. usage(stderr);
  153. return 1;
  154. }
  155. #ifndef NMRPFLASH_WINDOWS
  156. if (geteuid() != 0) {
  157. fprintf(stderr, "This program must be run as root!\n");
  158. return 1;
  159. }
  160. #endif
  161. val = nmrp_do(&args);
  162. out:
  163. #ifdef NMRPFLASH_WINDOWS
  164. WSACleanup();
  165. #endif
  166. return val;
  167. }