[PATCH 3/5] mtd: Add option for TRX magic to fixtrx

Hauke Mehrtens hauke at hauke-m.de
Tue Mar 9 23:52:12 GMT 2021


From: INAGAKI Hiroshi <musashino.open at gmail.com>

Buffalo uses the TRX header with a different magic and even changes this
magic with different devices. This change allows to specify the header
to use as a command line argument.

This is needed for the Buffalo WSR-2533DHP2 based on mt7622.

Co-Developed-by: Hauke Mehrtens <hauke at hauke-m.de>
Signed-off-by: Hauke Mehrtens <hauke at hauke-m.de>
---
 package/system/mtd/src/mtd.c | 14 ++++++++++++--
 package/system/mtd/src/mtd.h |  1 +
 package/system/mtd/src/trx.c |  8 ++++----
 3 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/package/system/mtd/src/mtd.c b/package/system/mtd/src/mtd.c
index 9baed3fd5e99..fc7071d94083 100644
--- a/package/system/mtd/src/mtd.c
+++ b/package/system/mtd/src/mtd.c
@@ -94,6 +94,7 @@ int mtdsize = 0;
 int erasesize = 0;
 int jffs2_skip_bytes=0;
 int mtdtype = 0;
+uint32_t opt_trxmagic = TRX_MAGIC;
 
 int mtd_open(const char *mtd, bool block)
 {
@@ -205,7 +206,7 @@ image_check(int imagefd, const char *mtd)
 
 	magic = ((uint32_t *)buf)[0];
 
-	if (be32_to_cpu(magic) == TRX_MAGIC)
+	if (be32_to_cpu(magic) == opt_trxmagic)
 		imageformat = MTD_IMAGE_FORMAT_TRX;
 	else if (be32_to_cpu(magic) == SEAMA_MAGIC)
 		imageformat = MTD_IMAGE_FORMAT_SEAMA;
@@ -810,6 +811,7 @@ static void usage(void)
 	"        -l <length>             the length of data that we want to dump\n");
 	if (mtd_fixtrx) {
 	    fprintf(stderr,
+	"        -M <magic>              magic number of the image header in the partition (for fixtrx)\n"
 	"        -o offset               offset of the image header in the partition(for fixtrx)\n");
 	}
 	if (mtd_fixtrx || mtd_fixseama || mtd_fixwrg || mtd_fixwrgg) {
@@ -877,7 +879,7 @@ int main (int argc, char **argv)
 #ifdef FIS_SUPPORT
 			"F:"
 #endif
-			"frnqe:d:s:j:p:o:c:t:l:")) != -1)
+			"frnqe:d:s:j:p:o:c:t:l:M:")) != -1)
 		switch (ch) {
 			case 'f':
 				force = 1;
@@ -929,6 +931,14 @@ int main (int argc, char **argv)
 					usage();
 				}
 				break;
+			case 'M':
+				errno = 0;
+				opt_trxmagic = strtoul(optarg, 0, 0);
+				if (errno) {
+					fprintf(stderr, "-M: illegal numeric string\n");
+					usage();
+				}
+				break;
 			case 'o':
 				errno = 0;
 				offset = strtoul(optarg, 0, 0);
diff --git a/package/system/mtd/src/mtd.h b/package/system/mtd/src/mtd.h
index fe716b715090..d2facc8e3b94 100644
--- a/package/system/mtd/src/mtd.h
+++ b/package/system/mtd/src/mtd.h
@@ -12,6 +12,7 @@
 extern int quiet;
 extern int mtdsize;
 extern int erasesize;
+extern uint32_t opt_trxmagic;
 
 extern int mtd_open(const char *mtd, bool block);
 extern int mtd_check_open(const char *mtd);
diff --git a/package/system/mtd/src/trx.c b/package/system/mtd/src/trx.c
index 3e3b5d220c62..d7c5d832c42d 100644
--- a/package/system/mtd/src/trx.c
+++ b/package/system/mtd/src/trx.c
@@ -35,7 +35,6 @@
 #include "mtd.h"
 #include "crc32.h"
 
-#define TRX_MAGIC       0x30524448      /* "HDR0" */
 #define TRX_CRC32_DATA_OFFSET	12	/* First 12 bytes are not covered by CRC32 */
 #define TRX_CRC32_DATA_SIZE	16
 struct trx_header {
@@ -92,7 +91,7 @@ trx_fixup(int fd, const char *name)
 	}
 
 	trx = ptr;
-	if (trx->magic != TRX_MAGIC) {
+	if (ntohl(trx->magic) != opt_trxmagic) {
 		fprintf(stderr, "TRX header not found\n");
 		goto err;
 	}
@@ -127,7 +126,8 @@ trx_check(int imagefd, const char *mtd, char *buf, int *len)
 		}
 	}
 
-	if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
+	if (ntohl(trx->magic) != opt_trxmagic ||
+	    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"
@@ -200,7 +200,7 @@ mtd_fixtrx(const char *mtd, size_t offset, size_t data_size)
 	}
 
 	trx = (struct trx_header *)(first_block + offset);
-	if (trx->magic != STORE32_LE(0x30524448)) {
+	if (ntohl(trx->magic) != opt_trxmagic) {
 		fprintf(stderr, "No trx magic found\n");
 		exit(1);
 	}
-- 
2.30.1




More information about the openwrt-devel mailing list