diff mbox

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

Message ID 20161211011753.26526-1-naobsd@gmail.com
State Accepted
Headers show

Commit Message

FUKAUMI Naoki Dec. 11, 2016, 1:17 a.m. UTC
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. it can
be installed from stock Web UI, then sysupgrade.bin can be installed by
sysupgrade on initramfs image.

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

Signed-off-by: FUKAUMI Naoki <naobsd@gmail.com>
---
Changes in v3:
- use "DHP/dhp" instead of "WSR/wsr" for keyword for common things
- use "-I" instead of "-W" for buffalo-tag optarg
- make commit log message better

Changes in v2:
- cosmetic cleanup in mt7621.mk

 target/linux/ramips/image/mt7621.mk    | 33 ++++++++++++
 tools/firmware-utils/Makefile          |  1 +
 tools/firmware-utils/src/buffalo-tag.c | 29 ++++++++---
 tools/firmware-utils/src/mkdhpimg.c    | 94 ++++++++++++++++++++++++++++++++++
 tools/firmware-utils/src/trx.c         | 13 ++++-
 5 files changed, 161 insertions(+), 9 deletions(-)
 create mode 100644 tools/firmware-utils/src/mkdhpimg.c

Comments

Mathias Kresin Dec. 11, 2016, 1:53 p.m. UTC | #1
First of all, sorry for beeing late to the party and requesting changes 
to a V3.

11.12.2016 02:17, FUKAUMI Naoki:
> diff --git a/target/linux/ramips/image/mt7621.mk b/target/linux/ramips/image/mt7621.mk
> index 6d85511..23a79f5 100644
> --- a/target/linux/ramips/image/mt7621.mk
> +++ b/target/linux/ramips/image/mt7621.mk
> @@ -192,8 +192,41 @@ 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 \
> +		-I $(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/mkdhpimg \
> +		$(1).tag.enc $(1).trx.enc \
> +		$(call imgname,initramfs,$(4))-factory.bin
> +endef
> +
> +define Build/wsr-1166-factory-images
> +	$(call Build/wsr-factory-image,$@,0x746f435c,WSR-1166DHP,wsr-1166dhp)
> +	$(call Build/wsr-factory-image,$@,0x1fc384dd,WSR-1166DHP2,wsr-1166dhp2)
> +endef

This looks like the old image build code we just got rid of. Would you 
please add multiple factory images to "Device/wsr-1166" instead.

Have a look at the DGN3500 image build code in the lantiq target for an 
example.

Mathias
John Crispin Dec. 12, 2016, 7:08 a.m. UTC | #2
On 11/12/2016 14:53, Mathias Kresin wrote:
> First of all, sorry for beeing late to the party and requesting changes
> to a V3.
> 
> 11.12.2016 02:17, FUKAUMI Naoki:
>> diff --git a/target/linux/ramips/image/mt7621.mk
>> b/target/linux/ramips/image/mt7621.mk
>> index 6d85511..23a79f5 100644
>> --- a/target/linux/ramips/image/mt7621.mk
>> +++ b/target/linux/ramips/image/mt7621.mk
>> @@ -192,8 +192,41 @@ 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 \
>> +        -I $(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/mkdhpimg \
>> +        $(1).tag.enc $(1).trx.enc \
>> +        $(call imgname,initramfs,$(4))-factory.bin
>> +endef
>> +
>> +define Build/wsr-1166-factory-images
>> +    $(call
>> Build/wsr-factory-image,$@,0x746f435c,WSR-1166DHP,wsr-1166dhp)
>> +    $(call
>> Build/wsr-factory-image,$@,0x1fc384dd,WSR-1166DHP2,wsr-1166dhp2)
>> +endef
> 
> This looks like the old image build code we just got rid of. Would you
> please add multiple factory images to "Device/wsr-1166" instead.
> 
> Have a look at the DGN3500 image build code in the lantiq target for an
> example.
> 

i missed that, was too excited about the feature :)

this should be converted to the new style image code. i hvae dropped v3
from my tree.

	John
FUKAUMI Naoki Dec. 12, 2016, 1:09 p.m. UTC | #3
hi,

thanks for the comments.

On Mon, Dec 12, 2016 at 4:08 PM, John Crispin <john@phrozen.org> wrote:
>> This looks like the old image build code we just got rid of. Would you
>> please add multiple factory images to "Device/wsr-1166" instead.
>>
>> Have a look at the DGN3500 image build code in the lantiq target for an
>> example.
>>
>
> i missed that, was too excited about the feature :)
>
> this should be converted to the new style image code. i hvae dropped v3
> from my tree.

before sending v1 patch, I tried "IMAGE/factory.bin" method, but I
cannot build factory.bin _with initramfs uImage_ properly.

how can I write "build initramfs uImage before building factory.bin" rule?

I checked DGN3500 code, I guess it doesn't use initramfs image.

Regards,
FUKAUMI Naoki Dec. 14, 2016, 5:23 p.m. UTC | #4
hi,

I'll split my patch into 2 pieces, firmware-utils part and device specific part.
I confirmed factory image (squashfs based) for WZR-900DHP2 works, so
I'll send firmware-utils + WZR-900DHP2 patches at first.

for WSR-1166DHP, I'll send RFC patch.

Regards,
diff mbox

Patch

diff --git a/target/linux/ramips/image/mt7621.mk b/target/linux/ramips/image/mt7621.mk
index 6d85511..23a79f5 100644
--- a/target/linux/ramips/image/mt7621.mk
+++ b/target/linux/ramips/image/mt7621.mk
@@ -192,8 +192,41 @@  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 \
+		-I $(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/mkdhpimg \
+		$(1).tag.enc $(1).trx.enc \
+		$(call imgname,initramfs,$(4))-factory.bin
+endef
+
+define Build/wsr-1166-factory-images
+	$(call Build/wsr-factory-image,$@,0x746f435c,WSR-1166DHP,wsr-1166dhp)
+	$(call Build/wsr-factory-image,$@,0x1fc384dd,WSR-1166DHP2,wsr-1166dhp2)
+endef
+
 define Device/wsr-1166
   DTS := WSR-1166
+  KERNEL_INITRAMFS := $$(KERNEL) | wsr-1166-factory-images
   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..8a0d13f 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,mkdhpimg 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..63bc6d3 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 dhp;
 
 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"
+"  -I <file>       read input from the file <file> for DHP series\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 (dhp) {
+		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 (dhp) {
+		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:I:");
 		if (c == -1)
 			break;
 
@@ -319,6 +331,9 @@  int main(int argc, char *argv[])
 		case 'f':
 			flag = strtoul(optarg, NULL, 2);
 			break;
+		case 'I':
+			dhp = 1;
+			/* FALLTHROUGH */
 		case 'i':
 			err = process_ifname(optarg);
 			if (err)
diff --git a/tools/firmware-utils/src/mkdhpimg.c b/tools/firmware-utils/src/mkdhpimg.c
new file mode 100644
index 0000000..775e512
--- /dev/null
+++ b/tools/firmware-utils/src/mkdhpimg.c
@@ -0,0 +1,94 @@ 
+/*
+ * Copyright (c) 2016 FUKAUMI Naoki <naobsd@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 DHP_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 = DHP_HEADER_SIZE + tag_st.st_size + trx_st.st_size;
+
+	if ((buf = malloc(size)) == NULL)
+		err(EXIT_FAILURE, "malloc");
+
+	memset(buf, 0, DHP_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[DHP_HEADER_SIZE], tag_st.st_size);
+	close(tag);
+
+	read(trx, &buf[DHP_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();
 		}