Update readme
[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
25 void usage(FILE *fp)
26 {
27         fprintf(fp,
28                         "Usage: nmrp-flash [OPTIONS...]\n"
29                         "\n"
30                         "Options (-a, -i and -f are mandatory):\n"
31                         " -a <ipaddr>     IP address to assign to target device\n"
32                         " -f <firmware>   Firmware file\n"
33                         " -i <interface>  Network interface directly connected to device\n"
34                         " -m <mac>        MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
35                         " -M <netmask>    Subnet mask to assign to target device\n"
36                         " -t <timeout>    Timeout (in milliseconds) for regular messages\n"
37                         " -T <timeout>    Time to wait after successfull TFTP upload\n"
38                         " -p <port>       Port to use for TFTP upload\n"
39                         " -V              Print version and exit\n"
40                         " -h              Show this screen\n"
41                         "\n"
42                         "Example:\n"
43                         "\n"
44                         "$ sudo nmrp-flash -a 192.168.1.254 -i eth0 -f firmware.bin\n"
45                         "\n"
46                         "nmrp-flash v%s, Copyright (C) 2016 Joseph C. Lehner\n"
47                         "nmrp-flash is free software, licensed under the GNU GPLv3.\n"
48                         "Source code at https://github.com/jclehner/nmrp-flash\n"
49                         "\n",
50                         NMRPD_VERSION
51           );
52 }
53
54 int main(int argc, char **argv)
55 {
56         int c, val, max;
57         struct nmrpd_args args = {
58                 .rx_timeout = 200,
59                 .ul_timeout = 60000,
60                 .filename = NULL,
61                 .ipaddr = NULL,
62                 .ipmask = "255.255.255.0",
63                 .intf = NULL,
64                 .mac = "ff:ff:ff:ff:ff:ff",
65                 .op = NMRP_UPLOAD_FW,
66                 .port = 69,
67                 .force_root = 1
68         };
69
70         opterr = 0;
71
72         while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:hV")) != -1) {
73                 max = 0xffffffff;
74                 switch (c) {
75                         case 'a':
76                                 args.ipaddr = optarg;
77                                 break;
78                         case 'f':
79                                 args.filename = optarg;
80                                 break;
81                         case 'i':
82                                 args.intf = optarg;
83                                 break;
84                         case 'm':
85                                 args.mac = optarg;
86                                 break;
87                         case 'M':
88                                 args.ipmask = optarg;
89                                 break;
90                         case 'p':
91                                 max = 0xffff;
92                         case 'T':
93                         case 't':
94                                 val = atoi(optarg);
95                                 if (val <= 0 || val > max) {
96                                         fprintf(stderr, "Invalid numeric value for -%c.\n", c);
97                                         return 1;
98                                 }
99
100                                 if (c == 'p') {
101                                         args.port = val;
102                                 } else if (c == 't') {
103                                         args.rx_timeout = val;
104                                 } else {
105                                         args.ul_timeout = val;
106                                 }
107
108                                 break;
109                         case 'V':
110                                 printf("nmrp-flash v%s\n", NMRPD_VERSION);
111                                 return 0;
112                         case 'h':
113                                 usage(stdout);
114                                 return 0;
115                         default:
116                                 usage(stderr);
117                                 return 1;
118                 }
119         }
120
121         if (!args.filename || !args.intf || !args.ipaddr) {
122                 usage(stderr);
123                 return 1;
124         }
125
126         if (geteuid() != 0) {
127                 fprintf(stderr, "This program must be run as root!\n");
128                 return 1;
129         }
130
131         return nmrp_do(&args);
132 }