Author: rmilecki
Date: 2015-04-14 22:50:46 +0200 (Tue, 14 Apr 2015)
New Revision: 45443

Modified:
   trunk/package/utils/otrx/src/Makefile
   trunk/package/utils/otrx/src/otrx.c
   trunk/target/linux/bcm53xx/base-files/lib/upgrade/platform.sh
   trunk/target/linux/brcm47xx/base-files/lib/upgrade/platform.sh
Log:
otrx: change command line API to start with a mode

This will allow adding more modes without options conflict.

Signed-off-by: Rafa?\197?\130 Mi?\197?\130ecki <[email protected]>

Modified: trunk/package/utils/otrx/src/Makefile
===================================================================
--- trunk/package/utils/otrx/src/Makefile       2015-04-14 19:01:29 UTC (rev 
45442)
+++ trunk/package/utils/otrx/src/Makefile       2015-04-14 20:50:46 UTC (rev 
45443)
@@ -1,7 +1,7 @@
 all: otrx
 
 otrx:
-       $(CC) $(CFLAGS) -o $@ otrx.c
+       $(CC) $(CFLAGS) -o $@ otrx.c -Wall
 
 clean:
        rm -f otrx

Modified: trunk/package/utils/otrx/src/otrx.c
===================================================================
--- trunk/package/utils/otrx/src/otrx.c 2015-04-14 19:01:29 UTC (rev 45442)
+++ trunk/package/utils/otrx/src/otrx.c 2015-04-14 20:50:46 UTC (rev 45443)
@@ -14,6 +14,7 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
 #if __BYTE_ORDER == __BIG_ENDIAN
@@ -39,14 +40,6 @@
        uint32_t offset[3];
 };
 
-enum mode {
-       MODE_UNKNOWN,
-       MODE_CHECK,
-       MODE_EXTRACT,
-};
-
-enum mode mode = MODE_UNKNOWN;
-
 char *trx_path;
 size_t trx_offset = 0;
 char *partition[TRX_MAX_PARTS] = {};
@@ -137,7 +130,19 @@
  * Check
  **************************************************/
 
-static int otrx_check() {
+static void otrx_check_parse_options(int argc, char **argv) {
+       int c;
+
+       while ((c = getopt(argc, argv, "o:")) != -1) {
+               switch (c) {
+               case 'o':
+                       trx_offset = atoi(optarg);
+                       break;
+               }
+       }
+}
+
+static int otrx_check(int argc, char **argv) {
        FILE *trx;
        struct trx_header hdr;
        size_t bytes, length;
@@ -145,6 +150,16 @@
        uint32_t crc32;
        int err = 0;
 
+       if (argc < 3) {
+               fprintf(stderr, "No TRX file passed\n");
+               err = -EINVAL;
+               goto out;
+       }
+       trx_path = argv[2];
+
+       optind = 3;
+       otrx_check_parse_options(argc, argv);
+
        trx = fopen(trx_path, "r");
        if (!trx) {
                fprintf(stderr, "Couldn't open %s\n", trx_path);
@@ -209,6 +224,27 @@
  * Extract
  **************************************************/
 
+static void otrx_extract_parse_options(int argc, char **argv) {
+       int c;
+
+       while ((c = getopt(argc, argv, "c:e:o:1:2:3:")) != -1) {
+               switch (c) {
+               case 'o':
+                       trx_offset = atoi(optarg);
+                       break;
+               case '1':
+                       partition[0] = optarg;
+                       break;
+               case '2':
+                       partition[1] = optarg;
+                       break;
+               case '3':
+                       partition[2] = optarg;
+                       break;
+               }
+       }
+}
+
 static int otrx_extract_copy(FILE *trx, size_t offset, size_t length, char 
*out_path) {
        FILE *out;
        size_t bytes;
@@ -254,13 +290,23 @@
        return err;
 }
 
-static int otrx_extract() {
+static int otrx_extract(int argc, char **argv) {
        FILE *trx;
        struct trx_header hdr;
        size_t bytes;
        int i;
        int err = 0;
 
+       if (argc < 3) {
+               fprintf(stderr, "No TRX file passed\n");
+               err = -EINVAL;
+               goto out;
+       }
+       trx_path = argv[2];
+
+       optind = 3;
+       otrx_extract_parse_options(argc, argv);
+
        trx = fopen(trx_path, "r");
        if (!trx) {
                fprintf(stderr, "Couldn't open %s\n", trx_path);
@@ -310,61 +356,29 @@
  * Start
  **************************************************/
 
-static void parse_options(int argc, char **argv) {
-       int c;
-
-       while ((c = getopt(argc, argv, "c:e:o:1:2:3:")) != -1) {
-               switch (c) {
-               case 'c':
-                       mode = MODE_CHECK;
-                       trx_path = optarg;
-                       break;
-               case 'e':
-                       mode = MODE_EXTRACT;
-                       trx_path = optarg;
-                       break;
-               case 'o':
-                       trx_offset = atoi(optarg);
-                       break;
-               case '1':
-                       partition[0] = optarg;
-                       break;
-               case '2':
-                       partition[1] = optarg;
-                       break;
-               case '3':
-                       partition[2] = optarg;
-                       break;
-               }
-       }
-}
-
 static void usage() {
        printf("Usage:\n");
        printf("\n");
        printf("Checking TRX file:\n");
-       printf("\t-c file\t\tcheck if file is a valid TRX\n");
-       printf("\t-o offset\toffset of TRX data in file (default: 0)\n");
+       printf("\totrx check <file> [options]\tcheck if file is a valid TRX\n");
+       printf("\t-o offset\t\t\toffset of TRX data in file (default: 0)\n");
        printf("\n");
        printf("Extracting from TRX file:\n");
-       printf("\t-e file\t\tfile with TRX to extract from\n");
-       printf("\t-o offset\toffset of TRX data in file (default: 0)\n");
-       printf("\t-1 file\t\tfile to extract 1st partition to (optional)\n");
-       printf("\t-2 file\t\tfile to extract 2nd partition to (optional)\n");
-       printf("\t-3 file\t\tfile to extract 3rd partition to (optional)\n");
+       printf("\totrx extract <file> [options]\textract partitions from TRX 
file\n");
+       printf("\t-o offset\t\t\toffset of TRX data in file (default: 0)\n");
+       printf("\t-1 file\t\t\t\tfile to extract 1st partition to 
(optional)\n");
+       printf("\t-2 file\t\t\t\tfile to extract 2nd partition to 
(optional)\n");
+       printf("\t-3 file\t\t\t\tfile to extract 3rd partition to 
(optional)\n");
 }
 
 int main(int argc, char **argv) {
-       parse_options(argc, argv);
-
-       switch (mode) {
-       case MODE_CHECK:
-               return otrx_check();
-       case MODE_EXTRACT:
-               return otrx_extract();
-       default:
-               usage();
+       if (argc > 1) {
+               if (!strcmp(argv[1], "check"))
+                       return otrx_check(argc, argv);
+               else if (!strcmp(argv[1], "extract"))
+                       return otrx_extract(argc, argv);
        }
 
+       usage();
        return 0;
 }

Modified: trunk/target/linux/bcm53xx/base-files/lib/upgrade/platform.sh
===================================================================
--- trunk/target/linux/bcm53xx/base-files/lib/upgrade/platform.sh       
2015-04-14 19:01:29 UTC (rev 45442)
+++ trunk/target/linux/bcm53xx/base-files/lib/upgrade/platform.sh       
2015-04-14 20:50:46 UTC (rev 45443)
@@ -79,7 +79,7 @@
                                error=1
                        }
 
-                       if ! otrx -c "$1" -o "$header_len"; then
+                       if ! otrx check "$1" -o "$header_len"; then
                                echo "No valid TRX firmware in the CHK image"
                                error=1
                        fi
@@ -94,13 +94,13 @@
                                error=1
                        }
 
-                       if ! otrx -c "$1" -o 32; then
+                       if ! otrx check "$1" -o 32; then
                                echo "No valid TRX firmware in the CyberTAN 
image"
                                error=1
                        fi
                ;;
                "trx")
-                       if ! otrx -c "$1"; then
+                       if ! otrx check "$1"; then
                                echo "Invalid (corrupted?) TRX firmware"
                                error=1
                        fi
@@ -140,7 +140,7 @@
        # Extract partitions from trx
        rm -fR $dir
        mkdir -p $dir
-       otrx -e "$trx" \
+       otrx extract "$trx" \
                -1 $dir/kernel \
                -2 $dir/root
 

Modified: trunk/target/linux/brcm47xx/base-files/lib/upgrade/platform.sh
===================================================================
--- trunk/target/linux/brcm47xx/base-files/lib/upgrade/platform.sh      
2015-04-14 19:01:29 UTC (rev 45442)
+++ trunk/target/linux/brcm47xx/base-files/lib/upgrade/platform.sh      
2015-04-14 20:50:46 UTC (rev 45443)
@@ -98,7 +98,7 @@
                                error=1
                        }
 
-                       if ! otrx -c "$1" -o "$header_len"; then
+                       if ! otrx check "$1" -o "$header_len"; then
                                echo "No valid TRX firmware in the CHK image"
                                error=1
                        fi
@@ -113,13 +113,13 @@
                                error=1
                        }
 
-                       if ! otrx -c "$1" -o 32; then
+                       if ! otrx check "$1" -o 32; then
                                echo "No valid TRX firmware in the CyberTAN 
image"
                                error=1
                        fi
                ;;
                "trx")
-                       if ! otrx -c "$1"; then
+                       if ! otrx check "$1"; then
                                echo "Invalid (corrupted?) TRX firmware"
                                error=1
                        fi
_______________________________________________
openwrt-commits mailing list
[email protected]
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-commits

Reply via email to