2 * nmrpflash - Netgear Unbrick Utility
3 * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
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.
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.
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/>.
26 #define NMRPFLASH_SET_REGION
33 "Usage: nmrpflash [OPTIONS...]\n"
35 "Options (-a, -i and -f and/or -c are mandatory):\n"
36 " -a <ipaddr> IP address to assign to target device\n"
37 " -c <command> Command to run before (or instead of) TFTP upload\n"
38 " -f <firmware> Firmware file\n"
39 " -F <filename> Remote filename to use during TFTP upload\n"
40 " -i <interface> Network interface directly connected to device\n"
41 " -m <mac> MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
42 " -M <netmask> Subnet mask to assign to target device\n"
43 " -t <timeout> Timeout (in milliseconds) for regular messages\n"
44 " -T <timeout> Time (seconds) to wait after successfull TFTP upload\n"
45 " -p <port> Port to use for TFTP upload\n"
46 #ifdef NMRPFLASH_SET_REGION
47 " -R <region> Set device region (NA, WW, GR, PR, RU, BZ, IN, KO, JP)\n"
49 #ifdef NMRPFLASH_TFTP_TEST
50 " -U Test TFTP upload\n"
53 " -V Print version and exit\n"
54 " -L List network interfaces\n"
55 " -h Show this screen\n"
58 #ifndef NMRPFLASH_WINDOWS
64 #ifndef NMRPFLASH_WINDOWS
65 "# nmrpflash -i eth0 -a 192.168.1.254 -f firmware.bin\n"
67 "C:\\> nmrpflash.exe -i net0 -a 192.168.1.254 -f firmware.bin\n"
70 "nmrpflash %s, Copyright (C) 2016 Joseph C. Lehner\n"
71 "nmrpflash is free software, licensed under the GNU GPLv3.\n"
72 "Source code at https://github.com/jclehner/nmrpflash\n"
78 #ifdef NMRPFLASH_WINDOWS
81 SID_IDENTIFIER_AUTHORITY auth = SECURITY_NT_AUTHORITY;
82 PSID adminGroup = NULL;
83 BOOL success = AllocateAndInitializeSid(
84 &auth, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
85 0, 0, 0, 0, 0, 0, &adminGroup
89 if (CheckTokenMembership(NULL, adminGroup, &success)) {
91 fprintf(stderr, "Error: must be run as administrator");
100 fprintf(stderr, "Warning: failed to check administrator privileges");
106 fprintf(stderr, "Error: must be run as root");
112 int main(int argc, char **argv)
116 struct nmrpd_args args = {
118 .ul_timeout = 120000,
123 .ipmask = "255.255.255.0",
125 .mac = "ff:ff:ff:ff:ff:ff",
126 .op = NMRP_UPLOAD_FW,
130 #ifdef NMRPFLASH_WINDOWS
133 val = WSAStartup(MAKEWORD(2, 2), &wsa);
135 win_perror2("WSAStartup", val);
142 while ((c = getopt(argc, argv, "a:c:f:F:i:m:M:p:R:t:T:hLVvU")) != -1) {
146 args.ipaddr = optarg;
149 args.tftpcmd = optarg;
152 args.file_local = optarg;
155 args.file_remote = optarg;
164 args.ipmask = optarg;
166 #ifdef NMRPFLASH_SET_REGION
168 args.region = optarg;
179 if (val <= 0 || val > max) {
180 fprintf(stderr, "Invalid numeric value for -%c.\n", c);
186 } else if (c == 't') {
187 args.rx_timeout = val;
188 } else if (c == 'T') {
189 args.ul_timeout = val * 1000;
194 printf("nmrpflash %s\n", NMRPFLASH_VERSION);
208 #ifdef NMRPFLASH_TFTP_TEST
210 if (args.ipaddr && args.file_local) {
211 val = tftp_put(&args);
223 if (!list && ((!args.file_local && !args.tftpcmd) || !args.intf || !args.ipaddr)) {
229 val = !list ? nmrp_do(&args) : ethsock_list_all();
232 #ifdef NMRPFLASH_WINDOWS