Support -v switch
[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                         "$ sudo nmrp-flash -a 192.168.1.254 -i eth0 -f firmware.bin\n"
50                         "\n"
51                         "nmrp-flash v%s, Copyright (C) 2016 Joseph C. Lehner\n"
52                         "nmrp-flash is free software, licensed under the GNU GPLv3.\n"
53                         "Source code at https://github.com/jclehner/nmrp-flash\n"
54                         "\n",
55                         NMRPD_VERSION
56           );
57 }
58
59 int main(int argc, char **argv)
60 {
61         int c, val, max;
62         struct nmrpd_args args = {
63                 .rx_timeout = 200,
64                 .ul_timeout = 60000,
65                 .tftpcmd = NULL,
66                 .filename = NULL,
67                 .ipaddr = NULL,
68                 .ipmask = "255.255.255.0",
69                 .intf = NULL,
70                 .mac = "ff:ff:ff:ff:ff:ff",
71                 .op = NMRP_UPLOAD_FW,
72                 .port = 69,
73                 .force_root = 1
74         };
75
76         opterr = 0;
77
78         while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:hLVv")) != -1) {
79                 max = 0x7fffffff;
80                 switch (c) {
81                         case 'a':
82                                 args.ipaddr = optarg;
83                                 break;
84                         case 'f':
85                                 args.filename = optarg;
86                                 break;
87                         case 'i':
88                                 args.intf = optarg;
89                                 break;
90                         case 'm':
91                                 args.mac = optarg;
92                                 break;
93                         case 'M':
94                                 args.ipmask = optarg;
95                                 break;
96                         case 'p':
97                                 max = 0xffff;
98                         case 'T':
99                         case 't':
100                                 val = atoi(optarg);
101                                 if (val <= 0 || val > max) {
102                                         fprintf(stderr, "Invalid numeric value for -%c.\n", c);
103                                         return 1;
104                                 }
105
106                                 if (c == 'p') {
107                                         args.port = val;
108                                 } else if (c == 't') {
109                                         args.rx_timeout = val;
110                                 } else {
111                                         args.ul_timeout = val;
112                                 }
113
114                                 break;
115                         case 'V':
116                                 printf("nmrp-flash v%s\n", NMRPD_VERSION);
117                                 return 0;
118                         case 'v':
119                                 ++verbosity;
120                                 break;
121                         case 'L':
122                                 return ethsock_list_all();
123                         case 'h':
124                                 usage(stdout);
125                                 return 0;
126                         default:
127                                 usage(stderr);
128                                 return 1;
129                 }
130         }
131
132         if (!args.filename || !args.intf || !args.ipaddr) {
133                 usage(stderr);
134                 return 1;
135         }
136
137 #ifndef NMRPFLASH_WINDOWS
138         if (geteuid() != 0) {
139                 fprintf(stderr, "This program must be run as root!\n");
140                 return 1;
141         }
142 #endif
143
144         return nmrp_do(&args);
145 }