[PATCH] Belkin F7DXXXX (BCM47XX based) Support for Linux 3.18

Joseph East eastyjr at gmail.com
Wed Jun 10 05:49:26 EDT 2015


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 <eastyjr at gmail.com>
---
 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_MAGIC    0x20100322
+#define BELKIN_F7D3302_MAGIC    0x20090928
+#define BELKIN_F7D4302_MAGIC    0x20101006
+#define BELKIN_F7D4401_MAGIC    0x00018517
+
 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 <byteswap.h>
 #include <endian.h>
 #include <errno.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -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_magic(uint32_t magic) {
+	if(use_trx_override)
+	{
+		return magic == trx_override;
+	}
+	else
+	{
+		return magic == TRX_MAGIC;
+	}
+}
+
 static int otrx_check(int argc, char **argv) {
 	FILE *trx;
 	struct trx_header hdr;
@@ -186,7 +204,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 +566,7 @@ static void usage() {
 	printf("Checking TRX file:\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("\t-m signature\t\t\tCompare against input TRX signature\n");
 	printf("\n");
 	printf("Creating new TRX file:\n");
 	printf("\totrx create <file> [options] [partitions]\n");
diff --git a/target/linux/brcm47xx/base-files/lib/upgrade/platform.sh b/target/linux/brcm47xx/base-files/lib/upgrade/platform.sh
index cbadefb..2e193bb 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 "belkin 22031020 nogeneric"; return;;
+		"Belkin F7D3302")	echo "belkin 28090920 nogeneric"; return;;
+		"Belkin F7D4302")	echo "belkin 06101020 nogeneric"; return;;
+		"Belkin F7D4401")       echo "belkin 17850100 nogeneric"; return;;
 	esac
 }
 
@@ -67,6 +71,22 @@ brcm47xx_identify() {
 			echo "chk"
 			return
 			;;
+		"17850100")
+			echo "beltrx"
+                        return
+                        ;;
+                "06101020")
+			echo "beltrx"
+                        return
+                        ;;
+                "28090920")
+			echo "beltrx"
+                        return
+                        ;;
+                "22031020")
+                        echo "beltrx"
+                        return
+                        ;;
 	esac
 
 	magic=$(get_magic_long_at "$1" 14)
@@ -119,11 +139,33 @@ platform_check_image() {
 			fi
 		;;
 		"trx")
+			local flash_generic=$(platform_expected_image | cut -d ' ' -f 3)
+			[ -n "$flash_generic" -a "nogeneric" == "$flash_generic" ] && {
+				echo "Machine cannot use generic image, please flash machine specific image"
+				error=1
+			}
+
 			if ! otrx check "$1"; then
 				echo "Invalid (corrupted?) TRX firmware"
 				error=1
 			fi
 		;;
+		"beltrx")
+			local dev_board_id=$(platform_expected_image | cut -d ' ' -f 2)
+			local magic=$(get_magic_long "$1")
+			echo "Found TRX image with Belkin magic TRX"
+			[ -n "$dev_board_id" -a "$magic" != "$dev_board_id" ] &&
+			{
+				echo "Firmware pattern doesn't match device pattern"
+				error=1
+			}
+
+			local be_magic=${magic:6:2}${magic:4:2}${magic:2:2}${magic:0:2}
+			if ! otrx check "$1" -m "$be_magic"; then
+				echo "Not a valid Belkin TRX image"
+				error=1
+			fi
+		;;
 		*)
 			echo "Invalid image type. Please use only .trx files"
 			error=1
diff --git a/target/linux/brcm47xx/image/Makefile b/target/linux/brcm47xx/image/Makefile
index 6768820..45ed330 100644
--- a/target/linux/brcm47xx/image/Makefile
+++ b/target/linux/brcm47xx/image/Makefile
@@ -34,6 +34,17 @@ endif
 	$(call prepare_generic_squashfs,$(KDIR)/fs_mark)
 endef
 
+# Function for multiple Belkin Trx
+# 1: trx input
+# 2: bin output
+# 3: 4 magic bytes
+define Image/Build/Belkin_
+	echo -ne $(3) > $(2).head
+	dd if=$(1) of=/dev/stdout bs=4 skip=1 > $(2).tail
+	cat $(2).head $(2).tail > $(2)
+	rm -f $(2).head $(2).tail
+endef
+
 define Image/Build/wgt634u
 	dd if=$(KDIR)/loader.elf of=$(BIN_DIR)/$(IMG_PREFIX)-wgt634u-$(2).bin bs=131072 conv=sync
 	cat $(BIN_DIR)/$(IMG_PREFIX)-$(1).trx >> $(BIN_DIR)/$(IMG_PREFIX)-wgt634u-$(2).bin
@@ -71,6 +82,10 @@ define Image/Build/Edi
 	$(STAGING_DIR_HOST)/bin/trx2edips $(BIN_DIR)/$(IMG_PREFIX)-$(1).trx $(BIN_DIR)/$(IMG_PREFIX)-$(2)-$(3).bin
 endef
 
+define Image/Build/Belkin
+	$(call Image/Build/Belkin_,$(BIN_DIR)/$(IMG_PREFIX)-$(1).trx,$(BIN_DIR)/openwrt-$(2)-$(patsubst jffs2-%,jffs2,$(1)).bin,$(3))
+endef
+
 define Image/Build/Huawei
 	dd if=/dev/zero of=$(BIN_DIR)/$(IMG_PREFIX)-$(2)-$(3)-gz.bin bs=92 count=1
 	echo -ne 'HDR0\x08\x00\x00\x00' >> $(BIN_DIR)/$(IMG_PREFIX)-$(2)-$(3)-gz.bin
@@ -362,6 +377,10 @@ define Image/Build
 		-f $(KDIR)/vmlinux.gz \
 		$(call trxalign/$(1),$(1))
 	$(call Image/Build/$(SUBTARGET)/$(1),$(1))
+	$(call Image/Build/Belkin,$(1),f7d3301,'\x22\x03\x10\x20')
+	$(call Image/Build/Belkin,$(1),f7d3302,'\x28\x09\x09\x20')
+	$(call Image/Build/Belkin,$(1),f7d4302,'\x06\x10\x10\x20')
+	$(call Image/Build/Belkin,$(1),f7d4401,'\x17\x85\x01\x00')
 #	$(call Image/Build/Chk,$(1),wgr614_v9,U12H094T00_NETGEAR,2,$(patsubst jffs2-%,jffs2,$(1)))
 #	$(call Image/Build/Chk,$(1),wndr3400_vcna,U12H155T01_NETGEAR,2,$(patsubst jffs2-%,jffs2,$(1)))
 #	$(call Image/Build/Chk,$(1),wnr3500U,U12H136T00_NETGEAR,2,$(patsubst jffs2-%,jffs2,$(1)))
diff --git a/target/linux/brcm47xx/image/lzma-loader/src/decompress.c b/target/linux/brcm47xx/image/lzma-loader/src/decompress.c
index 05681b1..076bff0 100644
--- a/target/linux/brcm47xx/image/lzma-loader/src/decompress.c
+++ b/target/linux/brcm47xx/image/lzma-loader/src/decompress.c
@@ -89,6 +89,10 @@ struct trx_header {
 
 #define EDIMAX_PS_HEADER_MAGIC	0x36315350 /*  "PS16"  */
 #define EDIMAX_PS_HEADER_LEN	0xc /* 12 bytes long for edimax header */
+#define BELKIN_F7D3301_MAGIC	0x20100322 /* Belkin TRX */
+#define BELKIN_F7D3302_MAGIC	0x20090928
+#define BELKIN_F7D4302_MAGIC	0x20101006
+#define BELKIN_F7D4401_MAGIC	0x00018517
 
 /* beyound the image end, size not known in advance */
 extern unsigned char workspace[];
@@ -121,6 +125,24 @@ static __inline__ unsigned char get_byte(void)
 	return read_byte(0, &buffer, &fake), *buffer;
 }
 
+static int has_trx_magic(unsigned char *data)
+{
+    UInt32 magic = ((struct trx_header *)data)->magic;
+
+    switch (magic) {
+    case TRX_MAGIC:
+    case EDIMAX_PS_HEADER_MAGIC:
+    case BELKIN_F7D3301_MAGIC:
+    case BELKIN_F7D3302_MAGIC:
+    case BELKIN_F7D4302_MAGIC:
+    case BELKIN_F7D4401_MAGIC:
+        return 1;
+    default:
+        return 0;
+    }
+}
+
+
 /* should be the first function */
 void entry(unsigned long icache_size, unsigned long icache_lsize, 
 	unsigned long dcache_size, unsigned long dcache_lsize,
@@ -138,8 +160,7 @@ void entry(unsigned long icache_size, unsigned long icache_lsize,
 
 	/* look for trx header, 32-bit data access */
 	for (data = ((unsigned char *) KSEG1ADDR(BCM4710_FLASH));
-		((struct trx_header *)data)->magic != TRX_MAGIC &&
-		((struct trx_header *)data)->magic != EDIMAX_PS_HEADER_MAGIC;
+		 !has_trx_magic(data);
 		 data += 65536);
 
 	if (((struct trx_header *)data)->magic == EDIMAX_PS_HEADER_MAGIC)
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 0000000..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
+ 
+ struct trx_header {
+ 	uint32_t magic;
+@@ -55,6 +59,20 @@ struct trx_header {
+ 	uint32_t offset[3];
+ } __packed;
+ 
++static bool is_trx_magic(uint32_t 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;
++	}
++}
++
+ static void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
+ 				 u64 offset, uint32_t mask_flags)
+ {
+@@ -189,7 +207,7 @@ static int bcm47xxpart_parse(struct mtd_
+ 		}
+ 
+ 		/* TRX */
+-		if (buf[0x000 / 4] == TRX_MAGIC) {
++		if (is_trx_magic(buf[0x000 / 4])) {
+ 			if (BCM47XXPART_MAX_PARTS - curr_part < 4) {
+ 				pr_warn("Not enough partitions left to register trx, scanning stopped!\n");
+ 				break;
-- 
2.1.4


On 23/06/2015 11:06 PM, Joseph East wrote:
> 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 0000000..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 at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list