[OpenWrt-Devel] [PATCH] [brcm47xx] Belkin F7DXXXX (BCM47XX based) support for Linux 3.18

2015-06-22 Thread Joseph East
From: Joseph East 

Patchset based on previous work by Cody Schafer and Luc Forcier to allow
TRX images with Belkin IDs to be compiled as part of the BRCM47XX platform
for Linux 3.18.

The patchset has been tested with a Belkin F7D4401 (PlayMax N600), other
devices part of the previous patch are untested but have been included,
however the Belkin QA TRX signature has been removed.

The otrx tool has been modified to accept a -m argument which allows
comparison against a user defined TRX signature, this works in tandem with
the changes in platform.sh to verify the different Belkin TRXs for
supported models.

Signed-off-by: Joseph East 
---
 package/system/mtd/src/trx.c   | 28 --
 package/utils/otrx/src/otrx.c  | 31 +++-
 .../brcm47xx/base-files/lib/upgrade/platform.sh| 39

 target/linux/brcm47xx/image/Makefile   | 19 ++
 .../brcm47xx/image/lzma-loader/src/decompress.c| 25 -
 ...nclude-Belkin-F7D4XXX-F7D3XXX-TRX-for-mtd.patch | 43
++
 6 files changed, 177 insertions(+), 8 deletions(-)
 create mode 100644
target/linux/brcm47xx/patches-3.18/032-11-MIPS-BCM47XX-Include-Belkin-F7D4XXX-F7D3XXX-TRX-for-mtd.patch

diff --git a/package/system/mtd/src/trx.c b/package/system/mtd/src/trx.c
index 245ee76..8e4980c 100644
--- a/package/system/mtd/src/trx.c
+++ b/package/system/mtd/src/trx.c
@@ -35,7 +35,12 @@
 #include "mtd.h"
 #include "crc32.h"

-#define TRX_MAGIC   0x30524448  /* "HDR0" */
+#define TRX_MAGIC   0x30524448  /* "HDR0" */
+#define BELKIN_F7D3301_MAGIC0x20100322
+#define BELKIN_F7D3302_MAGIC0x20090928
+#define BELKIN_F7D4302_MAGIC0x20101006
+#define BELKIN_F7D4401_MAGIC0x00018517
+
 struct trx_header {
  uint32_t magic;  /* "HDR0" */
  uint32_t len;  /* Length of file including header */
@@ -55,6 +60,21 @@ struct trx_header {
 ssize_t pread(int fd, void *buf, size_t count, off_t offset);
 ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);

+static bool is_trx_magic(uint32_t magic)
+{
+magic = STORE32_LE(magic);
+switch (magic) {
+case TRX_MAGIC:
+case BELKIN_F7D3301_MAGIC:
+case BELKIN_F7D3302_MAGIC:
+case BELKIN_F7D4302_MAGIC:
+case BELKIN_F7D4401_MAGIC:
+return true;
+default:
+return false;
+}
+}
+
 int
 trx_fixup(int fd, const char *name)
 {
@@ -83,7 +103,7 @@ trx_fixup(int fd, const char *name)
  }

  trx = ptr;
- if (trx->magic != TRX_MAGIC) {
+ if (!is_trx_magic(trx->magic)) {
   fprintf(stderr, "TRX header not found\n");
   goto err;
  }
@@ -118,7 +138,7 @@ trx_check(int imagefd, const char *mtd, char *buf, int
*len)
   return 0;
  }

- if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
+ if (!is_trx_magic(trx->magic) || trx->len < sizeof(struct trx_header)) {
   if (quiet < 2) {
fprintf(stderr, "Bad trx header\n");
fprintf(stderr, "This is not the correct file format; refusing to
flash.\n"
@@ -184,7 +204,7 @@ mtd_fixtrx(const char *mtd, size_t offset)
  }

  trx = (struct trx_header *) (buf + offset);
- if (trx->magic != STORE32_LE(0x30524448)) {
+ if (!is_trx_magic(trx->magic)) {
   fprintf(stderr, "No trx magic found\n");
   exit(1);
  }
diff --git a/package/utils/otrx/src/otrx.c b/package/utils/otrx/src/otrx.c
index 101a310..36ebe62 100644
--- a/package/utils/otrx/src/otrx.c
+++ b/package/utils/otrx/src/otrx.c
@@ -47,6 +47,8 @@ struct trx_header {

 char *trx_path;
 size_t trx_offset = 0;
+uint8_t use_trx_override = 0;
+uint32_t trx_override = 0;
 char *partition[TRX_MAX_PARTS] = {};

 static inline size_t otrx_min(size_t x, size_t y) {
@@ -143,11 +145,35 @@ uint32_t otrx_crc32(uint8_t *buf, size_t len) {
 static void otrx_check_parse_options(int argc, char **argv) {
  int c;

- while ((c = getopt(argc, argv, "o:")) != -1) {
+ while ((c = getopt(argc, argv, "m:o:")) != -1) {
   switch (c) {
   case 'o':
trx_offset = atoi(optarg);
break;
+  case 'm':
+   trx_override = strtol(optarg, NULL, 16);
+   use_trx_override = 1;
+   break;
+  }
+ }
+}
+
+static int is_trx_magic(uint32_t magic) {
+magic = cpu_to_le32(magic);
+ if(use_trx_override) {
+ if(magic == trx_override) {
+ return 1;
+ }
+ else {
+ return 0;
+ }
+ }
+ else {
+  if(magic == TRX_MAGIC) {
+   return 1;
+  }
+  else {
+   return 0;
   }
  }
 }
@@ -186,7 +212,7 @@ static int otrx_check(int argc, char **argv) {
   goto err_close;
  }

- if (le32_to_cpu(hdr.magic) != TRX_MAGIC) {
+ if (!is_trx_magic(le32_to_cpu(hdr.magic))) {
   fprintf(stderr, "Invalid TRX magic: 0x%08x\n", le32_to_cpu(hdr.magic));
   err =  -EINVAL;
   goto err_close;
@@ -548,6 +574,7 @@ static void usage() {
  printf("Checking TRX fi

Re: [OpenWrt-Devel] [PATCH] [brcm47xx] Belkin F7DXXXX (BCM47XX based) support for Linux 3.18

2015-06-22 Thread Joseph East

Patch didn't seem to mail properly, trying again.

From: Joseph East 

Patchset based on previous work by Cody Schafer and Luc Forcier to allow 
TRX images with Belkin IDs to be compiled as part of the BRCM47XX 
platform for Linux 3.18.


The patchset has been tested with a Belkin F7D4401 (PlayMax N600), other 
devices part of the previous patch are untested but have been included, 
however the Belkin QA TRX signature has been removed.


The otrx tool has been modified to accept a -m argument which allows 
comparison against a user defined TRX signature, this works in tandem 
with the changes in platform.sh to verify the different Belkin TRXs for 
supported models.


Signed-off-by: Joseph East 
---
 package/system/mtd/src/trx.c   | 28 --
 package/utils/otrx/src/otrx.c  | 31 +++-
 .../brcm47xx/base-files/lib/upgrade/platform.sh| 39 


 target/linux/brcm47xx/image/Makefile   | 19 ++
 .../brcm47xx/image/lzma-loader/src/decompress.c| 25 -
 ...nclude-Belkin-F7D4XXX-F7D3XXX-TRX-for-mtd.patch | 43 
++

 6 files changed, 177 insertions(+), 8 deletions(-)
 create mode 100644 
target/linux/brcm47xx/patches-3.18/032-11-MIPS-BCM47XX-Include-Belkin-F7D4XXX-F7D3XXX-TRX-for-mtd.patch


diff --git a/package/system/mtd/src/trx.c b/package/system/mtd/src/trx.c
index 245ee76..8e4980c 100644
--- a/package/system/mtd/src/trx.c
+++ b/package/system/mtd/src/trx.c
@@ -35,7 +35,12 @@
 #include "mtd.h"
 #include "crc32.h"

-#define TRX_MAGIC   0x30524448  /* "HDR0" */
+#define TRX_MAGIC   0x30524448  /* "HDR0" */
+#define BELKIN_F7D3301_MAGIC0x20100322
+#define BELKIN_F7D3302_MAGIC0x20090928
+#define BELKIN_F7D4302_MAGIC0x20101006
+#define BELKIN_F7D4401_MAGIC0x00018517
+
 struct trx_header {
 uint32_t magic;/* "HDR0" */
 uint32_t len;/* Length of file including header */
@@ -55,6 +60,21 @@ struct trx_header {
 ssize_t pread(int fd, void *buf, size_t count, off_t offset);
 ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);

+static bool is_trx_magic(uint32_t magic)
+{
+magic = STORE32_LE(magic);
+switch (magic) {
+case TRX_MAGIC:
+case BELKIN_F7D3301_MAGIC:
+case BELKIN_F7D3302_MAGIC:
+case BELKIN_F7D4302_MAGIC:
+case BELKIN_F7D4401_MAGIC:
+return true;
+default:
+return false;
+}
+}
+
 int
 trx_fixup(int fd, const char *name)
 {
@@ -83,7 +103,7 @@ trx_fixup(int fd, const char *name)
 }

 trx = ptr;
-if (trx->magic != TRX_MAGIC) {
+if (!is_trx_magic(trx->magic)) {
 fprintf(stderr, "TRX header not found\n");
 goto err;
 }
@@ -118,7 +138,7 @@ trx_check(int imagefd, const char *mtd, char *buf, 
int *len)

 return 0;
 }

-if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
+if (!is_trx_magic(trx->magic) || trx->len < sizeof(struct 
trx_header)) {

 if (quiet < 2) {
 fprintf(stderr, "Bad trx header\n");
 fprintf(stderr, "This is not the correct file format; 
refusing to flash.\n"

@@ -184,7 +204,7 @@ mtd_fixtrx(const char *mtd, size_t offset)
 }

 trx = (struct trx_header *) (buf + offset);
-if (trx->magic != STORE32_LE(0x30524448)) {
+if (!is_trx_magic(trx->magic)) {
 fprintf(stderr, "No trx magic found\n");
 exit(1);
 }
diff --git a/package/utils/otrx/src/otrx.c b/package/utils/otrx/src/otrx.c
index 101a310..36ebe62 100644
--- a/package/utils/otrx/src/otrx.c
+++ b/package/utils/otrx/src/otrx.c
@@ -47,6 +47,8 @@ struct trx_header {

 char *trx_path;
 size_t trx_offset = 0;
+uint8_t use_trx_override = 0;
+uint32_t trx_override = 0;
 char *partition[TRX_MAX_PARTS] = {};

 static inline size_t otrx_min(size_t x, size_t y) {
@@ -143,11 +145,35 @@ uint32_t otrx_crc32(uint8_t *buf, size_t len) {
 static void otrx_check_parse_options(int argc, char **argv) {
 int c;

-while ((c = getopt(argc, argv, "o:")) != -1) {
+while ((c = getopt(argc, argv, "m:o:")) != -1) {
 switch (c) {
 case 'o':
 trx_offset = atoi(optarg);
 break;
+case 'm':
+trx_override = strtol(optarg, NULL, 16);
+use_trx_override = 1;
+break;
+}
+}
+}
+
+static int is_trx_magic(uint32_t magic) {
+magic = cpu_to_le32(magic);
+if(use_trx_override) {
+if(magic == trx_override) {
+return 1;
+}
+else {
+return 0;
+}
+}
+else {
+if(magic == TRX_MAGIC) {
+return 1;
+}
+else {
+return 0;
 }
 }
 }
@@ -186,7 +212,7 @@ stat

Re: [OpenWrt-Devel] [PATCH] [brcm47xx] Belkin F7DXXXX (BCM47XX based) support for Linux 3.18

2015-06-23 Thread Joseph East
Thanks for the feedback, will have another attempt at submitting the patch. 

> @@ -184,7 +204,7 @@ mtd_fixtrx(const char *mtd, size_t offset)
>   }
>
>   trx = (struct trx_header *) (buf + offset);
> - if (trx->magic != STORE32_LE(0x30524448)) {
> + if (!is_trx_magic(trx->magic)) {
> Is dropping STORE32_LE safe? Should this be sth like
> le32_to_cpu(trx->magic)
> ?
I think this is a misunderstanding, both otrx.c and trx.c were modified
to have a function named is_trx_magic(). trx.c uses the STORE32_LE macro
whereas otrx uses the cpu_to_le32() / le32_to_cpu() methods, the two
files and is_trx_magic() implementations shouldn't be related otherwise. 

>> diff --git a/target/linux/brcm47xx/base-files/lib/upgrade/platform.sh
>> b/target/linux/brcm47xx/base-files/lib/upgrade/platform.sh
>> index cbadefb..9f12715 100644
>> --- a/target/linux/brcm47xx/base-files/lib/upgrade/platform.sh
>> +++ b/target/linux/brcm47xx/base-files/lib/upgrade/platform.sh
>> @@ -51,6 +51,10 @@ platform_expected_image() {
>>"Linksys WRT310N V2") echo "cybertan 310N"; return;;
>>"Linksys WRT610N V1") echo "cybertan 610N"; return;;
>>"Linksys WRT610N V2") echo "cybertan 610N"; return;;
>> +  "Belkin F7D3301") echo "22031020"; return;;
>> +  "Belkin F7D3302") echo "28090920"; return;;
>> +  "Belkin F7D4302") echo "06101020"; return;;
>> +  "Belkin F7D4401")   echo "17850100"; return;;
> I just think if we should follow "type id" logic like "beltrx
> 22031020". I did that for other formats to make sure there won't be
> collisions like two different formats using the same magic. In case of
> Belkin it's a bit tricky because their every ID could be treated as
> separated format.
Have updated to follow this convention, but also realised that the
F7D4401 won't take the generic squashfs trx image (CFE indicates bad
boot block when flashed). Have made an additional change to platform.sh
to check that if a generic trx is detected, verify that the machine
isn't on a 'generic blacklist'. This is indicated by a third field in
platform_expected_image() which should read 'nogeneric'

>> @@ -124,6 +144,25 @@ platform_check_image() {
>>  error=1
>> fi
>>;;
>> +  "beltrx")
>> +   local dev_board_id=$(platform_expected_image)
>> +   local machine=$(platform_machine)
>> +   local magic=$(get_magic_long "$1")
>> +   echo "Found TRX image with Belkin Magic TRX"
>> +   echo "Anticipating a $machine, checking..."
> You don't really need this $machine. And if you think we should print
> it for whatever reason, propose it as global change for all formats.
> I'm not sure if that's needed however.
Removed echoed comment
>> diff --git
>> a/target/linux/brcm47xx/patches-3.18/032-11-MIPS-BCM47XX-Include-Belkin-F7D4XXX-F7D3XXX-TRX-for-mtd.patch
>> b/target/linux/brcm47xx/patches-3.18/032-11-MIPS-BCM47XX-Include-Belkin-F7D4XXX-F7D3XXX-TRX-for-mtd.patch
>> new file mode 100644
>> index 000..8e2c1df
>> --- /dev/null
>> +++
>> b/target/linux/brcm47xx/patches-3.18/032-11-MIPS-BCM47XX-Include-Belkin-F7D4XXX-F7D3XXX-TRX-for-mtd.patch
>> @@ -0,0 +1,43 @@
>> +--- a/drivers/mtd/bcm47xxpart.c
>>  b/drivers/mtd/bcm47xxpart.c
>> +@@ -45,6 +45,10 @@
>> + #define TRX_MAGIC   0x30524448
>> + #define SHSQ_MAGIC   0x71736873 /* shsq (weird ZTE H218N endianness) */
>> + #define UBI_EC_MAGIC   0x23494255 /* UBI# */
>> ++#define BELKIN_F7D3301_MAGIC  0x20100322 /* Belkin TRX */
>> ++#define BELKIN_F7D3302_MAGIC  0x20090928
>> ++#define BELKIN_F7D4302_MAGIC  0x20101006
>> ++#define BELKIN_F7D4401_MAGIC  0x00018517
> Please send this patch for mainline inclusion, base it on top of:
> http://git.infradead.org/l2-mtd.git
> I don't want to have more brcm47xx patches noone will ever upstream ;)
Will try and do so, but would this stop this patch from going through if
it isn't in mainline?

For Hauke's comment -

> +static int is_trx_magic(uint32_t magic) {
> +magic = cpu_to_le32(magic);
> +if(use_trx_override) {
> +if(magic == trx_override) {
> +return 1;
> +}
> +else {
> +return 0;
> +}
> +}
> +else {
> +if(magic == TRX_MAGIC) {
> +return 1;
> +}
> +else {
> +return 0;
>  
> Make this function return bool and then you can simplify it like this:
> if(use_trx_override)
>   return magic == trx_override;
> else
>   return magic == TRX_MAGIC;
>
I was initially working within the constraints of defined libraries,
otrx.c now includes stdbool.h to allow boolean evaluation and this
simplified version. 
___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel


Re: [OpenWrt-Devel] [PATCH] [brcm47xx] Belkin F7DXXXX (BCM47XX based) support for Linux 3.18

2015-06-23 Thread Joseph East
>From d38c8c72b99908d3d9a39bd858d1c76e9864faac Mon Sep 17 00:00:00 2001
From: Joseph East 
Date: Wed, 10 Jun 2015 19:19:26 +0930
Subject: [PATCH] Belkin F7D (BCM47XX based) Support for Linux 3.18

Patchset based on previous work by Cody Schafer and Luc Forcier to allow TRX 
images with Belkin IDs to be compiled as part of the BRCM47XX platform for 
Linux 3.18.

The patchset has been tested with a Belkin F7D4401 (PlayMax N600), other 
devices part of the previous patch are untested but have been included, however 
the Belkin QA TRX signature has been removed.

The otrx tool has been modified to accept a -m argument which allows comparison 
against a user defined TRX signature, this works in tandem with the changes in 
platform.sh to verify the different Belkin TRXs for supported models.

Signed-off-by: Joseph East 
---
 package/system/mtd/src/trx.c   | 28 --
 package/utils/otrx/src/otrx.c  | 23 +++-
 .../brcm47xx/base-files/lib/upgrade/platform.sh| 42 +
 target/linux/brcm47xx/image/Makefile   | 19 ++
 .../brcm47xx/image/lzma-loader/src/decompress.c| 25 -
 ...nclude-Belkin-F7D4XXX-F7D3XXX-TRX-for-mtd.patch | 43 ++
 6 files changed, 172 insertions(+), 8 deletions(-)
 create mode 100644 
target/linux/brcm47xx/patches-3.18/032-11-MIPS-BCM47XX-Include-Belkin-F7D4XXX-F7D3XXX-TRX-for-mtd.patch

diff --git a/package/system/mtd/src/trx.c b/package/system/mtd/src/trx.c
index 245ee76..8e4980c 100644
--- a/package/system/mtd/src/trx.c
+++ b/package/system/mtd/src/trx.c
@@ -35,7 +35,12 @@
 #include "mtd.h"
 #include "crc32.h"
 
-#define TRX_MAGIC   0x30524448  /* "HDR0" */
+#define TRX_MAGIC   0x30524448  /* "HDR0" */
+#define BELKIN_F7D3301_MAGIC0x20100322
+#define BELKIN_F7D3302_MAGIC0x20090928
+#define BELKIN_F7D4302_MAGIC0x20101006
+#define BELKIN_F7D4401_MAGIC0x00018517
+
 struct trx_header {
uint32_t magic; /* "HDR0" */
uint32_t len;   /* Length of file including header */
@@ -55,6 +60,21 @@ struct trx_header {
 ssize_t pread(int fd, void *buf, size_t count, off_t offset);
 ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
 
+static bool is_trx_magic(uint32_t magic)
+{
+magic = STORE32_LE(magic);
+switch (magic) {
+case TRX_MAGIC:
+case BELKIN_F7D3301_MAGIC:
+case BELKIN_F7D3302_MAGIC:
+case BELKIN_F7D4302_MAGIC:
+case BELKIN_F7D4401_MAGIC:
+return true;
+default:
+return false;
+}
+}
+
 int
 trx_fixup(int fd, const char *name)
 {
@@ -83,7 +103,7 @@ trx_fixup(int fd, const char *name)
}
 
trx = ptr;
-   if (trx->magic != TRX_MAGIC) {
+   if (!is_trx_magic(trx->magic)) {
fprintf(stderr, "TRX header not found\n");
goto err;
}
@@ -118,7 +138,7 @@ trx_check(int imagefd, const char *mtd, char *buf, int *len)
return 0;
}
 
-   if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
+   if (!is_trx_magic(trx->magic) || trx->len < sizeof(struct trx_header)) {
if (quiet < 2) {
fprintf(stderr, "Bad trx header\n");
fprintf(stderr, "This is not the correct file format; 
refusing to flash.\n"
@@ -184,7 +204,7 @@ mtd_fixtrx(const char *mtd, size_t offset)
}
 
trx = (struct trx_header *) (buf + offset);
-   if (trx->magic != STORE32_LE(0x30524448)) {
+   if (!is_trx_magic(trx->magic)) {
fprintf(stderr, "No trx magic found\n");
exit(1);
}
diff --git a/package/utils/otrx/src/otrx.c b/package/utils/otrx/src/otrx.c
index 101a310..4e74d00 100644
--- a/package/utils/otrx/src/otrx.c
+++ b/package/utils/otrx/src/otrx.c
@@ -12,6 +12,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -47,6 +48,8 @@ struct trx_header {
 
 char *trx_path;
 size_t trx_offset = 0;
+uint8_t use_trx_override = 0;
+uint32_t trx_override = 0;
 char *partition[TRX_MAX_PARTS] = {};
 
 static inline size_t otrx_min(size_t x, size_t y) {
@@ -143,15 +146,30 @@ uint32_t otrx_crc32(uint8_t *buf, size_t len) {
 static void otrx_check_parse_options(int argc, char **argv) {
int c;
 
-   while ((c = getopt(argc, argv, "o:")) != -1) {
+   while ((c = getopt(argc, argv, "m:o:")) != -1) {
switch (c) {
case 'o':
trx_offset = atoi(optarg);
break;
+   case 'm':
+   trx_override = strtol(optarg, NULL, 16);
+   use_trx_override = 1;
+   break;
}
}
 }
 
+static bool is_trx_magi