[LEDE-DEV] [PATCH] ramips: build factory images for Buffalo WSR-1166DHP/WSR-1166DHP2

FUKAUMI Naoki naobsd at gmail.com
Wed Dec 7 06:36:34 PST 2016


factory image has to have custom trx magic, and trx file will be written
in the middle of the mtd3 partition (bootpartition=1 on u-boot).
therefore, sysupgrade.bin cannot be installed directly from stock Web UI.

with this patch, factory.bin will be built with initramfs image which
can be installed from stock Web UI, and can be used to install
sysupgrade.bin.

install procedure:
 1. install initramfs image (factory.bin) from stock Web UI
 2. ssh root at 192.168.1.1
 3. run "mtd erase firmware" (erase factory.bin in mtd3)
 4. install sysupgrade.bin by sysupgrade

Signed-off-by: FUKAUMI Naoki <naobsd at gmail.com>
---
 target/linux/ramips/image/mt7621.mk    | 34 ++++++++++++
 tools/firmware-utils/Makefile          |  1 +
 tools/firmware-utils/src/buffalo-tag.c | 29 ++++++++---
 tools/firmware-utils/src/mkwsrimg.c    | 94 ++++++++++++++++++++++++++++++++++
 tools/firmware-utils/src/trx.c         | 13 ++++-
 5 files changed, 162 insertions(+), 9 deletions(-)
 create mode 100644 tools/firmware-utils/src/mkwsrimg.c

diff --git a/target/linux/ramips/image/mt7621.mk b/target/linux/ramips/image/mt7621.mk
index 6d85511..4641deb 100644
--- a/target/linux/ramips/image/mt7621.mk
+++ b/target/linux/ramips/image/mt7621.mk
@@ -192,8 +192,42 @@ define Device/wndr3700v5
 endef
 TARGET_DEVICES += wndr3700v5
 
+define Build/wsr-factory-image
+	$(STAGING_DIR_HOST)/bin/trx \
+		-M $(2) \
+		-m $(IMAGE_SIZE) \
+		-f $(1) \
+		-o $(1).trx
+	$(STAGING_DIR_HOST)/bin/buffalo-enc \
+		-l -m 'start' \
+		-p $(3) -v '0.00' \
+		-i $(1).trx -o $(1).trx.enc
+	$(STAGING_DIR_HOST)/bin/buffalo-tag \
+		-a MTK \
+		-b $(3) -p $(3) \
+		-l JP -r JP -r JP \
+		-s \
+		-v '0.00' -m '0.00' \
+		-w 1 \
+		-W $(1).trx.enc -o $(1).tag
+	$(STAGING_DIR_HOST)/bin/buffalo-enc \
+		-l -m 'start' \
+		-p '' -v '' \
+		-i $(1).tag -o $(1).tag.enc
+	$(STAGING_DIR_HOST)/bin/mkwsrimg \
+		$@.tag.enc $@.trx.enc \
+		$(call imgname,initramfs,$(4))-factory.bin
+endef
+
+define Build/wsr-1166-factory-images
+	$(call Build/wsr-factory-image,$(1),0x746f435c,WSR-1166DHP,wsr-1166dhp)
+	$(call Build/wsr-factory-image,$(1),0x1fc384dd,WSR-1166DHP2,wsr-1166dhp2)
+	$(CP) $(1) $(BIN_DIR)/
+endef
+
 define Device/wsr-1166
   DTS := WSR-1166
+  KERNEL_INITRAMFS := $$(KERNEL) | wsr-1166-factory-images $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE)
   IMAGE/sysupgrade.bin := trx | pad-rootfs | append-metadata
   IMAGE_SIZE := $(ralink_default_fw_size_16M)
   DEVICE_TITLE := Buffalo WSR-1166
diff --git a/tools/firmware-utils/Makefile b/tools/firmware-utils/Makefile
index 456b7a3..e7654ec 100644
--- a/tools/firmware-utils/Makefile
+++ b/tools/firmware-utils/Makefile
@@ -79,6 +79,7 @@ define Host/Compile
 	$(call cc,jcgimage, -lz -Wall)
 	$(call cc,mkbuffaloimg, -Wall)
 	$(call cc,zyimage, -Wall)
+	$(call cc,mkwsrimg buffalo-lib, -Wall)
 endef
 
 define Host/Install
diff --git a/tools/firmware-utils/src/buffalo-tag.c b/tools/firmware-utils/src/buffalo-tag.c
index b5db72e..f255bb9 100644
--- a/tools/firmware-utils/src/buffalo-tag.c
+++ b/tools/firmware-utils/src/buffalo-tag.c
@@ -7,6 +7,7 @@
  *
  */
 
+#include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
@@ -48,6 +49,7 @@ static uint32_t base2;
 static char *region_code;
 static uint32_t region_mask;
 static int num_regions;
+static int wsr;
 
 void usage(int status)
 {
@@ -63,6 +65,7 @@ void usage(int status)
 "  -d <base2>\n"
 "  -f <flag>       set flag to <flag>\n"
 "  -i <file>       read input from the file <file>\n"
+"  -W <file>       read input from the file <file> for Buffalo WSR\n"
 "  -l <language>   set language to <language>\n"
 "  -m <version>    set minor version to <version>\n"
 "  -o <file>       write output to the file <file>\n"
@@ -177,11 +180,17 @@ static void fixup_tag(unsigned char *buf, ssize_t buflen)
 		memcpy(tag->region_code, region_code, 2);
 	}
 
-	tag->len = htonl(buflen);
-	tag->data_len = htonl(fsize[0]);
-	tag->base1 = htonl(base1);
-	tag->base2 = htonl(base2);
-	tag->flag = flag;
+	if (wsr) {
+		tag->len = htonl(buflen + 200);
+		tag->base1 = htonl(buflen);
+		tag->base2 = htonl(0x01000000);
+	} else {
+		tag->len = htonl(buflen);
+		tag->data_len = htonl(fsize[0]);
+		tag->base1 = htonl(base1);
+		tag->base2 = htonl(base2);
+		tag->flag = flag;
+	}
 
 	if (hwver) {
 		memcpy(tag->hwv, "hwv", 3);
@@ -270,7 +279,10 @@ static int tag_file(void)
 		offset += fsize[i];
 	}
 
-	if (num_files == 1)
+	if (wsr) {
+		buflen = offsetof(struct buffalo_tag, data_len);
+		fixup_tag(buf, fsize[0]);
+	} else if (num_files == 1)
 		fixup_tag(buf, buflen);
 	else
 		fixup_tag2(buf, buflen);
@@ -299,7 +311,7 @@ int main(int argc, char *argv[])
 	while ( 1 ) {
 		int c;
 
-		c = getopt(argc, argv, "a:b:c:d:f:hi:l:m:o:p:r:sv:w:");
+		c = getopt(argc, argv, "a:b:c:d:f:hi:l:m:o:p:r:sv:w:W:");
 		if (c == -1)
 			break;
 
@@ -319,6 +331,9 @@ int main(int argc, char *argv[])
 		case 'f':
 			flag = strtoul(optarg, NULL, 2);
 			break;
+		case 'W':
+			wsr = 1;
+			/* FALLTHROUGH */
 		case 'i':
 			err = process_ifname(optarg);
 			if (err)
diff --git a/tools/firmware-utils/src/mkwsrimg.c b/tools/firmware-utils/src/mkwsrimg.c
new file mode 100644
index 0000000..48de2d4
--- /dev/null
+++ b/tools/firmware-utils/src/mkwsrimg.c
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2016 FUKAUMI Naoki <naobsd at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ *
+ */
+
+#include <sys/stat.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "buffalo-lib.h"
+
+#define WSR_HEADER_SIZE	20
+
+static char *progname;
+
+static void
+usage(void)
+{
+
+	fprintf(stderr, "usage: %s <tag> <trx> <out>\n", progname);
+	exit(EXIT_FAILURE);
+}
+
+int
+main(int argc, char *argv[])
+{
+	struct stat tag_st, trx_st;
+	size_t size;
+	uint32_t crc;
+	int tag, trx, out;
+	uint8_t *buf;
+
+	progname = argv[0];
+
+	if (argc != 4)
+		usage();
+
+	if ((tag = open(argv[1], O_RDONLY)) == -1)
+		err(EXIT_FAILURE, "%s", argv[1]);
+
+	if (fstat(tag, &tag_st) == -1)
+		err(EXIT_FAILURE, "%s", argv[1]);
+
+	if ((trx = open(argv[2], O_RDONLY)) == -1)
+		err(EXIT_FAILURE, "%s", argv[2]);
+
+	if (fstat(trx, &trx_st) == -1)
+		err(EXIT_FAILURE, "%s", argv[2]);
+
+	size = WSR_HEADER_SIZE + tag_st.st_size + trx_st.st_size;
+
+	if ((buf = malloc(size)) == NULL)
+		err(EXIT_FAILURE, "malloc");
+
+	memset(buf, 0, WSR_HEADER_SIZE);
+	buf[0x0] = 0x62;
+	buf[0x1] = 0x67;
+	buf[0x2] = 0x6e;
+	buf[0xb] = 0xb1;
+	buf[0xc] = (size >> 24) & 0xff;
+	buf[0xd] = (size >> 16) & 0xff;
+	buf[0xe] = (size >> 8) & 0xff;
+	buf[0xf] = size & 0xff;
+
+	read(tag, &buf[WSR_HEADER_SIZE], tag_st.st_size);
+	close(tag);
+
+	read(trx, &buf[WSR_HEADER_SIZE + tag_st.st_size], trx_st.st_size);
+	close(trx);
+
+	crc = buffalo_crc(buf, size);
+	buf[0x10] = (crc >> 24) & 0xff;
+	buf[0x11] = (crc >> 16) & 0xff;
+	buf[0x12] = (crc >> 8) & 0xff;
+	buf[0x13] = crc & 0xff;
+
+	if ((out = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)
+		err(EXIT_FAILURE, "%s", argv[3]);
+	write(out, buf, size);
+	close(out);
+
+	free(buf);
+
+	return EXIT_SUCCESS;
+}
diff --git a/tools/firmware-utils/src/trx.c b/tools/firmware-utils/src/trx.c
index aa1f5be..dc5bb67 100644
--- a/tools/firmware-utils/src/trx.c
+++ b/tools/firmware-utils/src/trx.c
@@ -100,7 +100,7 @@ int main(int argc, char **argv)
 	int c, i, append = 0;
 	size_t n;
 	ssize_t n2;
-	uint32_t cur_len, fsmark=0;
+	uint32_t cur_len, fsmark=0, magic;
 	unsigned long maxlen = TRX_MAX_LEN;
 	struct trx_header *p;
 	char trx_version = 1;
@@ -121,7 +121,7 @@ int main(int argc, char **argv)
 	in = NULL;
 	i = 0;
 
-	while ((c = getopt(argc, argv, "-:2o:m:a:x:b:f:A:F:")) != -1) {
+	while ((c = getopt(argc, argv, "-:2o:m:a:x:b:f:A:F:M:")) != -1) {
 		switch (c) {
 			case '2':
 				/* take care that nothing was written to buf so far */
@@ -243,6 +243,15 @@ int main(int argc, char **argv)
 				}
 
 				break;
+			case 'M':
+				errno = 0;
+				magic = strtoul(optarg, &e, 0);
+				if (errno || (e == optarg) || *e) {
+					fprintf(stderr, "illegal numeric string\n");
+					usage();
+				}
+				p->magic = STORE32_LE(magic);
+				break;
 			default:
 				usage();
 		}
-- 
2.7.4




More information about the Lede-dev mailing list