Add -c switch to execute command before (or instead of) TFTP upload
authorJoseph C. Lehner <joseph.c.lehner@gmail.com>
Fri, 12 Feb 2016 11:22:44 +0000 (12:22 +0100)
committerJoseph C. Lehner <joseph.c.lehner@gmail.com>
Fri, 12 Feb 2016 11:24:38 +0000 (12:24 +0100)
main.c
nmrp.c

diff --git a/main.c b/main.c
index b1b080c5b84fd0933d72658d619c43f0204e08c3..36e0e4255c944f24ab7507226372ba6dceae03dd 100644 (file)
--- a/main.c
+++ b/main.c
@@ -30,8 +30,9 @@ void usage(FILE *fp)
        fprintf(fp,
                        "Usage: nmrpflash [OPTIONS...]\n"
                        "\n"
-                       "Options (-a, -i and -f are mandatory):\n"
+                       "Options (-a, -i and -f and/or -c are mandatory):\n"
                        " -a <ipaddr>     IP address to assign to target device\n"
+                       " -c <command>    Command to run before (or instead of) TFTP upload\n"
                        " -f <firmware>   Firmware file\n"
                        " -i <interface>  Network interface directly connected to device\n"
                        " -m <mac>        MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
@@ -91,12 +92,15 @@ int main(int argc, char **argv)
 
        opterr = 0;
 
-       while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:hLVvU")) != -1) {
+       while ((c = getopt(argc, argv, "a:c:f:i:m:M:p:t:T:hLVvU")) != -1) {
                max = 0x7fffffff;
                switch (c) {
                        case 'a':
                                args.ipaddr = optarg;
                                break;
+                       case 'c':
+                               args.tftpcmd = optarg;
+                               break;
                        case 'f':
                                args.filename = optarg;
                                break;
@@ -157,7 +161,7 @@ int main(int argc, char **argv)
                }
        }
 
-       if (!args.filename || !args.intf || !args.ipaddr) {
+       if ((!args.filename && !args.tftpcmd) || !args.intf || !args.ipaddr) {
                usage(stderr);
                return 1;
        }
diff --git a/nmrp.c b/nmrp.c
index f6a1e032c0fb6270f5113e57573c2dadc8c090d4..6b42c5e65cdc60dc1ce0c990e51b263225f6e630 100644 (file)
--- a/nmrp.c
+++ b/nmrp.c
@@ -417,14 +417,23 @@ int nmrp_do(struct nmrpd_args *args)
                                        break;
                                }
 
-                               if (!args->tftpcmd) {
+                               err = 0;
+
+                               if (args->tftpcmd) {
+                                       printf("Executing '%s' ... ", args->tftpcmd);
+                                       fflush(stdout);
+                                       err = system(args->tftpcmd);
+                                       if (!err) {
+                                               printf("OK\n");
+                                       } else {
+                                               printf("ERR\n");
+                                       }
+                               }
+
+                               if (!err && args->filename) {
                                        printf("Uploading %s ... ", args->filename);
                                        fflush(stdout);
                                        err = tftp_put(args);
-                               } else {
-                                       printf("Running %s ... ", args->tftpcmd);
-                                       fflush(stdout);
-                                       err = system(args->tftpcmd);
                                }
 
                                if (!err) {