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 (-i, -f and/or -c are mandatory):\n"
36 " -a <ipaddr> IP address to assign to target device\n"
37 " -A <ipaddr> IP address to assign to seleted interface\n"
38 " -c <command> Command to run before (or instead of) TFTP upload\n"
39 " -f <firmware> Firmware file\n"
40 " -F <filename> Remote filename to use during TFTP upload\n"
41 " -i <interface> Network interface directly connected to device\n"
42 " -m <mac> MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
43 " -M <netmask> Subnet mask to assign to target device\n"
44 " -t <timeout> Timeout (in milliseconds) for regular messages\n"
45 " -T <timeout> Time (seconds) to wait after successfull TFTP upload\n"
46 " -p <port> Port to use for TFTP upload\n"
47 #ifdef NMRPFLASH_SET_REGION
48 " -R <region> Set device region (NA, WW, GR, PR, RU, BZ, IN, KO, JP)\n"
50 #ifdef NMRPFLASH_TFTP_TEST
51 " -U Test TFTP upload\n"
54 " -V Print version and exit\n"
55 " -L List network interfaces\n"
56 " -h Show this screen\n"
59 #ifndef NMRPFLASH_WINDOWS
65 #ifndef NMRPFLASH_WINDOWS
66 "# nmrpflash -i eth0 -f firmware.bin\n"
68 "C:\\> nmrpflash.exe -i net0 -f firmware.bin\n"
71 "When using -c, the environment variables IP, PORT, NETMASK\n"
72 "and MAC are set to the device IP address, TFTP port, subnet\n"
73 "mask and MAC address, respectively.\n"
75 "nmrpflash %s, Copyright (C) 2016 Joseph C. Lehner\n"
76 "nmrpflash is free software, licensed under the GNU GPLv3.\n"
77 "Source code at https://github.com/jclehner/nmrpflash\n"
83 #ifdef NMRPFLASH_WINDOWS
86 SID_IDENTIFIER_AUTHORITY auth = SECURITY_NT_AUTHORITY;
87 PSID adminGroup = NULL;
88 BOOL success = AllocateAndInitializeSid(
89 &auth, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
90 0, 0, 0, 0, 0, 0, &adminGroup
94 if (CheckTokenMembership(NULL, adminGroup, &success)) {
96 fprintf(stderr, "Error: must be run as administrator\n");
105 fprintf(stderr, "Warning: failed to check administrator privileges\n");
111 fprintf(stderr, "Error: must be run as root\n");
117 int main(int argc, char **argv)
121 struct nmrpd_args args = {
123 .ul_timeout = 5 * 60 * 1000,
129 .ipmask = "255.255.255.0",
131 .mac = "ff:ff:ff:ff:ff:ff",
132 .op = NMRP_UPLOAD_FW,
136 #ifdef NMRPFLASH_WINDOWS
137 char *newpath = NULL;
138 char *oldpath = NULL;
142 val = WSAStartup(MAKEWORD(2, 2), &wsa);
144 win_perror2("WSAStartup", val);
150 // This dirty hack works around the WOW64 file system redirector[1], which would prevent
151 // us from calling programs residing in %windir%\System32 when running on a 64bit system
152 // (since nmrpflash is currently shipped as 32bit only).
154 // [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
156 oldpath = getenv("PATH");
157 windir = getenv("WINDIR");
158 if (oldpath && windir) {
159 newpath = malloc(strlen(oldpath) + strlen(windir) + 32);
160 sprintf(newpath, "%s;%s\\Sysnative", oldpath, windir);
161 SetEnvironmentVariable("PATH", newpath);
169 while ((c = getopt(argc, argv, "a:A:c:f:F:i:m:M:p:R:t:T:hLVvU")) != -1) {
173 args.ipaddr = optarg;
176 args.ipaddr_intf = optarg;
179 args.tftpcmd = optarg;
182 args.file_local = optarg;
185 args.file_remote = optarg;
194 args.ipmask = optarg;
196 #ifdef NMRPFLASH_SET_REGION
198 args.region = optarg;
209 if (val <= 0 || val > max) {
210 fprintf(stderr, "Invalid numeric value for -%c.\n", c);
216 } else if (c == 't') {
217 args.rx_timeout = val;
218 } else if (c == 'T') {
219 args.ul_timeout = val * 1000;
224 printf("nmrpflash %s\n", NMRPFLASH_VERSION);
238 #ifdef NMRPFLASH_TFTP_TEST
240 if (args.ipaddr && args.file_local) {
241 val = tftp_put(&args);
253 if (args.ipaddr_intf && !args.ipaddr) {
254 fprintf(stderr, "Error: cannot use -A <ipaddr> without using -a <ipaddr>.\n");
258 #ifndef NMRPFLASH_FUZZ
259 if (!list && ((!args.file_local && !args.tftpcmd) || !args.intf /*|| !args.ipaddr*/)) {
266 val = !list ? nmrp_do(&args) : ethsock_list_all();
269 #ifdef NMRPFLASH_WINDOWS