2dfc1670f5778c0d447119032772a0613a697b15
[oweals/nmrpflash.git] / main.c
1 /**
2  * nmrp-flash - Netgear Unbrick Utility
3  * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
4  *
5  * nmrp-flash 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  * nmrp-flash 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 nmrp-flash.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19
20 #include <getopt.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "nmrpd.h"
24 #include "ethsock.h"
25
26 int verbosity = 0;
27
28 void usage(FILE *fp)
29 {
30         fprintf(fp,
31                         "Usage: nmrp-flash [OPTIONS...]\n"
32                         "\n"
33                         "Options (-a, -i and -f are mandatory):\n"
34                         " -a <ipaddr>     IP address to assign to target device\n"
35                         " -f <firmware>   Firmware file\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\n"
39                         " -t <timeout>    Timeout (in milliseconds) for regular messages\n"
40                         " -T <timeout>    Time to wait after successfull TFTP upload\n"
41                         " -p <port>       Port to use for TFTP upload\n"
42                         " -v              Be verbose\n"
43                         " -V              Print version and exit\n"
44                         " -L              List network interfaces\n"
45                         " -h              Show this screen\n"
46                         "\n"
47                         "Example:\n"
48                         "\n"
49 #ifndef NMRPFLASH_WINDOWS
50                         "$ sudo nmrp-flash -i eth0 -a 192.168.1.254 -f firmware.bin\n"
51 #else
52                         "C:\\> nmrp-flash.exe -i net0 -a 192.168.1.254 -f firmware.bin\n"
53 #endif
54                         "\n"
55                         "nmrp-flash v%s, Copyright (C) 2016 Joseph C. Lehner\n"
56                         "nmrp-flash is free software, licensed under the GNU GPLv3.\n"
57                         "Source code at https://github.com/jclehner/nmrp-flash\n"
58                         "\n",
59                         NMRPD_VERSION
60           );
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
82         val = WSAStartup(MAKEWORD(2, 2), &wsa);
83         if (val != 0) {
84                 win_perror2("WSAStartup", val);
85                 return 1;
86         }
87 #endif
88
89         opterr = 0;
90
91         while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:hLVv")) != -1) {
92                 max = 0x7fffffff;
93                 switch (c) {
94                         case 'a':
95                                 args.ipaddr = optarg;
96                                 break;
97                         case 'f':
98                                 args.filename = optarg;
99                                 break;
100                         case 'i':
101                                 args.intf = optarg;
102                                 break;
103                         case 'm':
104                                 args.mac = optarg;
105                                 break;
106                         case 'M':
107                                 args.ipmask = optarg;
108                                 break;
109                         case 'p':
110                                 max = 0xffff;
111                         case 'T':
112                         case 't':
113                                 val = atoi(optarg);
114                                 if (val <= 0 || val > max) {
115                                         fprintf(stderr, "Invalid numeric value for -%c.\n", c);
116                                         return 1;
117                                 }
118
119                                 if (c == 'p') {
120                                         args.port = val;
121                                 } else if (c == 't') {
122                                         args.rx_timeout = val;
123                                 } else {
124                                         args.ul_timeout = val;
125                                 }
126
127                                 break;
128                         case 'V':
129                                 printf("nmrp-flash v%s\n", NMRPD_VERSION);
130                                 return 0;
131                         case 'v':
132                                 ++verbosity;
133                                 break;
134                         case 'L':
135                                 return ethsock_list_all();
136                         case 'h':
137                                 usage(stdout);
138                                 return 0;
139                         default:
140                                 usage(stderr);
141                                 return 1;
142                 }
143         }
144
145         if (!args.filename || !args.intf || !args.ipaddr) {
146                 usage(stderr);
147                 return 1;
148         }
149
150 #ifndef NMRPFLASH_WINDOWS
151         if (geteuid() != 0) {
152                 fprintf(stderr, "This program must be run as root!\n");
153                 return 1;
154         }
155 #endif
156
157         val = nmrp_do(&args);
158 #ifdef NMRPFLASH_WINDOWS
159         WSACleanup();
160 #endif
161         return val;
162 }