Create zip files when release-building
[oweals/nmrpflash.git] / main.c
diff --git a/main.c b/main.c
index 39f327c32e3efc720fe8cd5a7c1e651b79912df0..31531d3d6099c5f3473d503efb751af2917664b5 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,14 +1,38 @@
+/**
+ * nmrpflash - Netgear Unbrick Utility
+ * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
+ *
+ * nmrpflash is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * nmrpflash is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with nmrpflash.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <unistd.h>
 #include <getopt.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include "nmrpd.h"
 
+#define NMRPFLASH_TFTP_TEST
+
+int verbosity = 0;
+
 void usage(FILE *fp)
 {
        fprintf(fp,
-                       "Usage: nmrpd [OPTIONS...]\n"
+                       "Usage: nmrpflash [OPTIONS...]\n"
                        "\n"
-                       "Options:\n"
+                       "Options (-a, -i and -f are mandatory):\n"
                        " -a <ipaddr>     IP address to assign to target device\n"
                        " -f <firmware>   Firmware file\n"
                        " -i <interface>  Network interface directly connected to device\n"
@@ -17,10 +41,27 @@ void usage(FILE *fp)
                        " -t <timeout>    Timeout (in milliseconds) for regular messages\n"
                        " -T <timeout>    Time to wait after successfull TFTP upload\n"
                        " -p <port>       Port to use for TFTP upload\n"
+#ifdef NMRPFLASH_TFTP_TEST
+                       " -U              Test TFTP upload\n"
+#endif
+                       " -v              Be verbose\n"
+                       " -V              Print version and exit\n"
+                       " -L              List network interfaces\n"
                        " -h              Show this screen\n"
                        "\n"
-                       "Options -a, -i and -f are mandatory!\n"
+                       "Example:\n"
+                       "\n"
+#ifndef NMRPFLASH_WINDOWS
+                       "$ sudo nmrpflash -i eth0 -a 192.168.1.254 -f firmware.bin\n"
+#else
+                       "C:\\> nmrpflash.exe -i net0 -a 192.168.1.254 -f firmware.bin\n"
+#endif
                        "\n"
+                       "nmrpflash %s, Copyright (C) 2016 Joseph C. Lehner\n"
+                       "nmrpflash is free software, licensed under the GNU GPLv3.\n"
+                       "Source code at https://github.com/jclehner/nmrpflash\n"
+                       "\n",
+                       NMRPFLASH_VERSION
          );
 }
 
@@ -30,6 +71,7 @@ int main(int argc, char **argv)
        struct nmrpd_args args = {
                .rx_timeout = 200,
                .ul_timeout = 60000,
+               .tftpcmd = NULL,
                .filename = NULL,
                .ipaddr = NULL,
                .ipmask = "255.255.255.0",
@@ -39,11 +81,20 @@ int main(int argc, char **argv)
                .port = 69,
                .force_root = 1
        };
+#ifdef NMRPFLASH_WINDOWS
+       WSADATA wsa;
+
+       val = WSAStartup(MAKEWORD(2, 2), &wsa);
+       if (val != 0) {
+               win_perror2("WSAStartup", val);
+               return 1;
+       }
+#endif
 
        opterr = 0;
 
-       while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:")) != -1) {
-               max = 0xffffffff;
+       while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:hLVvU")) != -1) {
+               max = 0x7fffffff;
                switch (c) {
                        case 'a':
                                args.ipaddr = optarg;
@@ -79,12 +130,32 @@ int main(int argc, char **argv)
                                }
 
                                break;
+                       case 'V':
+                               printf("nmrpflash %s\n", NMRPFLASH_VERSION);
+                               val = 0;
+                               goto out;
+                       case 'v':
+                               ++verbosity;
+                               break;
+                       case 'L':
+                               val = ethsock_list_all();
+                               goto out;
                        case 'h':
                                usage(stdout);
-                               return 0;
+                               val = 0;
+                               goto out;
+#ifdef NMRPFLASH_TFTP_TEST
+                       case 'U':
+                               if (args.ipaddr && args.filename) {
+                                       val = tftp_put(&args);
+                                       goto out;
+                               }
+                               /* fall through */
+#endif
                        default:
                                usage(stderr);
-                               return 1;
+                               val = 1;
+                               goto out;
                }
        }
 
@@ -93,5 +164,18 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       return nmrp_do(&args);
+#ifndef NMRPFLASH_WINDOWS
+       if (geteuid() != 0) {
+               fprintf(stderr, "This program must be run as root!\n");
+               return 1;
+       }
+#endif
+
+       val = nmrp_do(&args);
+
+out:
+#ifdef NMRPFLASH_WINDOWS
+       WSACleanup();
+#endif
+       return val;
 }