From patchwork Sat Jan 28 16:05:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUKAUMI Naoki X-Patchwork-Id: 721061 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3v9gXj35QDz9sD6 for ; Sun, 29 Jan 2017 03:07:33 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="mdFPUQJI"; dkim-atps=neutral Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cXVXH-0007Wy-2z; Sat, 28 Jan 2017 16:07:31 +0000 Received: from mail-pg0-f68.google.com ([74.125.83.68]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cXVXC-0007Ur-Jv for lede-dev@lists.infradead.org; Sat, 28 Jan 2017 16:07:29 +0000 Received: by mail-pg0-f68.google.com with SMTP id 3so9765124pgj.1 for ; Sat, 28 Jan 2017 08:07:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=c+BKoK3SVLSfITv+6zOhw+ON7ro0Dl3NTfFb8G9oNas=; b=mdFPUQJIzPxB8s21OrxlhY67DespY3ztxyKYnpKmnP+QYVO7n3SxUuVGJ5oPJb6FpN B3d/HFyYdB3VzNhtqMdstUH5HNmIH8GvFumok06BfiPvwdiLt1EWiJashsEJzHyAkj4I 0CAYe9teI5mA2iEAMBq04jg2XcpdCXVKkuj3B6DHI7TxDH0HZHYkkcC56LnB3yEdm7QI Vwlt2vWVITEPRw5VP32zrxC4EPByZQZKfP9yzbB7aTW5N5XVeehRBk9r4Ms+1YeDWFtf QW4wqZXcAptMTfh0AloGd+kikNcMoul2+iOyBquiCWGwv2UWjXKFzw5A/+S9eOjLFp/y UXJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=c+BKoK3SVLSfITv+6zOhw+ON7ro0Dl3NTfFb8G9oNas=; b=W3r2VZ6/EubfckRyKhHIZq51BaZPnnJiiD/dUlEZoQJGJe8mdSFfBbBkzu8q25nIHd NNrsfb5aaXaXhufsP+x1ookq1MJVgx4Ua0FviuB1h1G3jEy4VVxkK0x/Det2uKzCYMQQ KKnQnNB213toWJL5Jz9rXphEjoiOJDlvbeeaycQII4UQjZ3TAAREDUuD1W6saviMvLaB OBPEhM/9f/9PizjPhRdzjC2Hbt5vM3FMYA5Ur32Y2BlFcNYIC8gwyA5UrSypFxpb98oP j1RfdAps48w6WBSF9tZFAm/iApemaQNhbfiMqiVbR4zoAsfdR7QtEmLz6r7f1vybQ/QF ex4A== X-Gm-Message-State: AIkVDXLn2AHNsliZKPv6J84Bt+iBIL1FJ4e7tbpy0zM74dmRm9EvO19Vu8kwv316tD45Xg== X-Received: by 10.98.144.218 with SMTP id q87mr14833398pfk.51.1485619565577; Sat, 28 Jan 2017 08:06:05 -0800 (PST) Received: from localhost.localdomain (sakura.naobsd.org. [160.16.200.221]) by smtp.gmail.com with ESMTPSA id l22sm19951778pgc.43.2017.01.28.08.06.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 28 Jan 2017 08:06:05 -0800 (PST) From: FUKAUMI Naoki To: lede-dev@lists.infradead.org Date: Sun, 29 Jan 2017 01:05:54 +0900 Message-Id: <20170128160556.23071-1-naobsd@gmail.com> X-Mailer: git-send-email 2.11.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170128_080726_717533_36441720 X-CRM114-Status: GOOD ( 21.29 ) X-Spam-Score: -1.5 (-) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-1.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- 0.5 RCVD_IN_SORBS_SPAM RBL: SORBS: sender is a spam source [74.125.83.68 listed in dnsbl.sorbs.net] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (naobsd[at]gmail.com) -0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [74.125.83.68 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [74.125.83.68 listed in list.dnswl.org] -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.0 RCVD_IN_MSPIKE_WL Mailspike good senders Subject: [LEDE-DEV] [PATCH v4 1/3] firmware-utils: improve tools for Buffalo DHP series X-BeenThere: lede-dev@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: FUKAUMI Naoki MIME-Version: 1.0 Sender: "Lede-dev" Errors-To: lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org some of Buffalo DHP series use slightly different trx magic, buffalo-enc, buffalo-tag, and factory image begin with 'bgn'. this patch adds support for building those images. Signed-off-by: FUKAUMI Naoki --- Changes in v4: Changes in v3: - no change Changes in v2: - refresh everything to make rules simple include/image-commands.mk | 33 +++++++++++++ tools/firmware-utils/Makefile | 1 + tools/firmware-utils/src/buffalo-enc.c | 26 +++++++++-- tools/firmware-utils/src/buffalo-lib.h | 20 ++++++++ tools/firmware-utils/src/buffalo-tag.c | 46 ++++++++++++++++-- tools/firmware-utils/src/mkdhpimg.c | 85 ++++++++++++++++++++++++++++++++++ tools/firmware-utils/src/trx.c | 13 +++++- 7 files changed, 215 insertions(+), 9 deletions(-) create mode 100644 tools/firmware-utils/src/mkdhpimg.c diff --git a/include/image-commands.mk b/include/image-commands.mk index e0f2918..7d124ec 100644 --- a/include/image-commands.mk +++ b/include/image-commands.mk @@ -11,6 +11,39 @@ define Build/uImage @mv $@.new $@ endef +define Build/buffalo-enc + $(eval product=$(word 1,$(1))) + $(eval version=$(word 2,$(1))) + $(eval args=$(wordlist 3,$(words $(1)),$(1))) + $(STAGING_DIR_HOST)/bin/buffalo-enc \ + -p $(product) -v $(version) $(args) \ + -i $@ -o $@.new + mv $@.new $@ +endef + +define Build/buffalo-enc-tag + $(call Build/buffalo-enc,'' '' -S 152 $(1)) +endef + +define Build/buffalo-tag-dhp + $(eval product=$(word 1,$(1))) + $(eval region=$(word 2,$(1))) + $(eval language=$(word 3,$(1))) + $(STAGING_DIR_HOST)/bin/buffalo-tag \ + -d 0x01000000 -w 1 \ + -a $(BUFFALO_TAG_PLATFORM) \ + -v $(BUFFALO_TAG_VERSION) -m $(BUFFALO_TAG_MINOR) \ + -b $(product) -p $(product) \ + -r $(region) -r $(region) -l $(language) \ + -I $@ -o $@.new + mv $@.new $@ +endef + +define Build/buffalo-dhp-image + $(STAGING_DIR_HOST)/bin/mkdhpimg $@ $@.new + mv $@.new $@ +endef + define Build/netgear-chk $(STAGING_DIR_HOST)/bin/mkchkimg \ -o $@.new \ diff --git a/tools/firmware-utils/Makefile b/tools/firmware-utils/Makefile index 69cba69..2504678 100644 --- a/tools/firmware-utils/Makefile +++ b/tools/firmware-utils/Makefile @@ -80,6 +80,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-enc.c b/tools/firmware-utils/src/buffalo-enc.c index 794659e..08fad4e 100644 --- a/tools/firmware-utils/src/buffalo-enc.c +++ b/tools/firmware-utils/src/buffalo-enc.c @@ -35,6 +35,7 @@ static char *product; static char *version; static int do_decrypt; static int offset; +static int size; void usage(int status) { @@ -54,6 +55,7 @@ void usage(int status) " -v set version to \n" " -h show this screen\n" " -O Offset of encrypted data in file (decryption)\n" +" -S Size of unencrypted data in file (encryption)\n" ); exit(status); @@ -118,7 +120,7 @@ out: static int encrypt_file(void) { struct enc_param ep; - ssize_t src_len; + ssize_t src_len, tail_dst, tail_len, tail_src; unsigned char *buf; uint32_t hdrlen; ssize_t totlen = 0; @@ -131,8 +133,12 @@ static int encrypt_file(void) goto out; } - totlen = enc_compute_buf_len(product, version, src_len); - hdrlen = enc_compute_header_len(product, version); + if (size) { + tail_dst = enc_compute_buf_len(product, version, size); + tail_len = src_len - size; + totlen = tail_dst + tail_len; + } else + totlen = enc_compute_buf_len(product, version, src_len); buf = malloc(totlen); if (buf == NULL) { @@ -140,12 +146,21 @@ static int encrypt_file(void) goto out; } + hdrlen = enc_compute_header_len(product, version); + err = read_file_to_buf(ifname, &buf[hdrlen], src_len); if (err) { ERR("unable to read from file '%s'", ofname); goto free_buf; } + if (size) { + tail_src = hdrlen + size; + memmove(&buf[tail_dst], &buf[tail_src], tail_len); + memset(&buf[tail_src], 0, tail_dst - tail_src); + src_len = size; + } + memset(&ep, '\0', sizeof(ep)); ep.key = (unsigned char *) crypt_key; ep.seed = seed; @@ -241,7 +256,7 @@ int main(int argc, char *argv[]) while ( 1 ) { int c; - c = getopt(argc, argv, "adi:m:o:hlp:v:k:O:r:s:"); + c = getopt(argc, argv, "adi:m:o:hlp:v:k:O:r:s:S:"); if (c == -1) break; @@ -276,6 +291,9 @@ int main(int argc, char *argv[]) case 'O': offset = strtoul(optarg, NULL, 0); break; + case 'S': + size = strtoul(optarg, NULL, 0); + break; case 'h': usage(EXIT_SUCCESS); break; diff --git a/tools/firmware-utils/src/buffalo-lib.h b/tools/firmware-utils/src/buffalo-lib.h index ba8a508..7eb9bf5 100644 --- a/tools/firmware-utils/src/buffalo-lib.h +++ b/tools/firmware-utils/src/buffalo-lib.h @@ -69,6 +69,26 @@ struct buffalo_tag2 { uint8_t unknown2[3]; } __attribute ((packed)); +struct buffalo_tag3 { + unsigned char product[TAG_PRODUCT_LEN]; + unsigned char brand[TAG_BRAND_LEN]; + unsigned char ver_major[TAG_VERSION_LEN]; + unsigned char ver_minor[TAG_VERSION_LEN]; + unsigned char region_code[2]; + uint32_t region_mask; + unsigned char unknown0[2]; + unsigned char language[TAG_LANGUAGE_LEN]; + unsigned char platform[TAG_PLATFORM_LEN]; + unsigned char hwv[TAG_HWVER_LEN]; + unsigned char hwv_val[TAG_HWVER_VAL_LEN]; + uint8_t unknown1[24]; + + uint32_t total_len; + uint32_t crc; + uint32_t len1; + uint32_t base2; +} __attribute ((packed)); + #define ENC_PRODUCT_LEN 32 #define ENC_VERSION_LEN 8 #define ENC_MAGIC_LEN 6 diff --git a/tools/firmware-utils/src/buffalo-tag.c b/tools/firmware-utils/src/buffalo-tag.c index b5db72e..6d479f7 100644 --- a/tools/firmware-utils/src/buffalo-tag.c +++ b/tools/firmware-utils/src/buffalo-tag.c @@ -48,6 +48,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 +64,7 @@ void usage(int status) " -d \n" " -f set flag to \n" " -i read input from the file \n" +" -I read input from the file for DHP series\n" " -l set language to \n" " -m set minor version to \n" " -o write output to the file \n" @@ -227,6 +229,37 @@ static void fixup_tag2(unsigned char *buf, ssize_t buflen) tag->crc = htonl(buffalo_crc(buf, buflen)); } +static void fixup_tag3(unsigned char *buf, ssize_t totlen) +{ + struct buffalo_tag3 *tag = (struct buffalo_tag3 *) buf; + + memset(tag, '\0', sizeof(*tag)); + + memcpy(tag->brand, brand, strlen(brand)); + memcpy(tag->product, product, strlen(product)); + memcpy(tag->platform, platform, strlen(platform)); + memcpy(tag->ver_major, major, strlen(major)); + memcpy(tag->ver_minor, minor, strlen(minor)); + memcpy(tag->language, language, strlen(language)); + + if (num_regions > 1) { + tag->region_code[0] = 'M'; + tag->region_code[1] = '_'; + tag->region_mask = htonl(region_mask); + } else { + memcpy(tag->region_code, region_code, 2); + } + + tag->total_len = htonl(totlen); + tag->len1 = htonl(fsize[0]); + tag->base2 = htonl(base2); + + if (hwver) { + memcpy(tag->hwv, "hwv", 3); + memcpy(tag->hwv_val, hwver, strlen(hwver)); + } +} + static int tag_file(void) { unsigned char *buf; @@ -237,7 +270,9 @@ static int tag_file(void) int ret = -1; int i; - if (num_files == 1) + if (dhp) + hdrlen = sizeof(struct buffalo_tag3); + else if (num_files == 1) hdrlen = sizeof(struct buffalo_tag); else hdrlen = sizeof(struct buffalo_tag2); @@ -270,7 +305,9 @@ static int tag_file(void) offset += fsize[i]; } - if (num_files == 1) + if (dhp) + fixup_tag3(buf, fsize[0] + 200); + else if (num_files == 1) fixup_tag(buf, buflen); else fixup_tag2(buf, buflen); @@ -299,7 +336,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 +356,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..e61d042 --- /dev/null +++ b/tools/firmware-utils/src/mkdhpimg.c @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2016 FUKAUMI Naoki + * + * 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 +#include +#include +#include +#include +#include +#include +#include + +#include "buffalo-lib.h" + +#define DHP_HEADER_SIZE 20 + +static char *progname; + +static void +usage(void) +{ + + fprintf(stderr, "usage: %s \n", progname); + exit(EXIT_FAILURE); +} + +int +main(int argc, char *argv[]) +{ + struct stat in_st; + size_t size; + uint32_t crc; + int in, out; + uint8_t *buf; + + progname = argv[0]; + + if (argc != 3) + usage(); + + if ((in = open(argv[1], O_RDONLY)) == -1) + err(EXIT_FAILURE, "%s", argv[1]); + + if (fstat(in, &in_st) == -1) + err(EXIT_FAILURE, "%s", argv[1]); + + size = DHP_HEADER_SIZE + in_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(in, &buf[DHP_HEADER_SIZE], in_st.st_size); + close(in); + + 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[2], O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) + err(EXIT_FAILURE, "%s", argv[2]); + 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(); }