From patchwork Thu Apr 21 15:50:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Schiffer X-Patchwork-Id: 613170 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (caladan.dune.hu [78.24.191.180]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qrNYf2vYZz9t40 for ; Fri, 22 Apr 2016 01:52:38 +1000 (AEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 634C0B91598; Thu, 21 Apr 2016 17:50:54 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00,RDNS_NONE autolearn=no autolearn_force=no version=3.4.1 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP; Thu, 21 Apr 2016 17:50:54 +0200 (CEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 97D11B91579 for ; Thu, 21 Apr 2016 17:50:46 +0200 (CEST) X-policyd-weight: using cached result; rate:hard: -6.1 Received: from chaos.universe-factory.net (unknown [37.72.148.22]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Thu, 21 Apr 2016 17:50:46 +0200 (CEST) Received: from avalon.localdomain (unknown [IPv6:fd1b:c28a:2fd6::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by chaos.universe-factory.net (Postfix) with ESMTPSA id 5D4501836B0 for ; Thu, 21 Apr 2016 17:50:45 +0200 (CEST) From: Matthias Schiffer To: openwrt-devel@lists.openwrt.org Date: Thu, 21 Apr 2016 17:50:38 +0200 Message-Id: X-Mailer: git-send-email 2.8.0 In-Reply-To: <5a67fa4de36dffa7dc257e56297b2e0b40704484.1461253840.git.mschiffer@universe-factory.net> References: <5a67fa4de36dffa7dc257e56297b2e0b40704484.1461253840.git.mschiffer@universe-factory.net> In-Reply-To: <5a67fa4de36dffa7dc257e56297b2e0b40704484.1461253840.git.mschiffer@universe-factory.net> References: <5a67fa4de36dffa7dc257e56297b2e0b40704484.1461253840.git.mschiffer@universe-factory.net> Subject: [OpenWrt-Devel] [PATCH v2 2/4] tools: firmware-utils: add region code support to mktplinkfw X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" Signed-off-by: Matthias Schiffer --- tools/firmware-utils/src/mktplinkfw.c | 51 +++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/tools/firmware-utils/src/mktplinkfw.c b/tools/firmware-utils/src/mktplinkfw.c index 695d4f5..7127932 100644 --- a/tools/firmware-utils/src/mktplinkfw.c +++ b/tools/firmware-utils/src/mktplinkfw.c @@ -28,6 +28,7 @@ #include "md5.h" #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); }) +#define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0])) #define HEADER_VERSION_V1 0x01000000 #define HEADER_VERSION_V2 0x02000000 @@ -45,7 +46,7 @@ struct fw_header { char fw_version[36]; uint32_t hw_id; /* hardware id */ uint32_t hw_rev; /* hardware revision */ - uint32_t unk1; + uint32_t region; /* region code */ uint8_t md5sum1[MD5SUM_LEN]; uint32_t unk2; uint8_t md5sum2[MD5SUM_LEN]; @@ -90,6 +91,8 @@ static uint32_t hw_id; static char *opt_hw_rev; static uint32_t hw_rev; static uint32_t opt_hdr_ver = 1; +static char *country; +static uint32_t region; static int fw_ver_lo; static int fw_ver_mid; static int fw_ver_hi; @@ -170,6 +173,11 @@ static struct flash_layout layouts[] = { } }; +static const char *const regions[] = { + "UN", /* universal */ + "US", +}; + /* * Message macros */ @@ -206,6 +214,24 @@ static struct flash_layout *find_layout(const char *id) return ret; } +static uint32_t find_region(const char *country) { + uint32_t i; + + for (i = 0; i < ARRAY_SIZE(regions); i++) { + if (strcasecmp(regions[i], country) == 0) + return i; + } + + return -1; +} + +static const char * get_region_country(uint32_t region) { + if (region < ARRAY_SIZE(regions)) + return regions[region]; + else + return "unknown"; +} + static void usage(int status) { fprintf(stderr, "Usage: %s [OPTIONS...]\n", progname); @@ -217,6 +243,7 @@ static void usage(int status) " -L overwrite kernel load address with (hexval prefixed with 0x)\n" " -H use hardware id specified with \n" " -W use hardware revision specified with \n" +" -C set region code to \n" " -F use flash layout specified with \n" " -k read kernel image from the file \n" " -r read rootfs image from the file \n" @@ -324,6 +351,18 @@ static int check_options(void) else hw_rev = 1; + if (country) { + region = find_region(country); + if (region == (uint32_t)-1) { + char *end; + region = strtoul(country, &end, 0); + if (*end) { + ERR("unknown region code \"%s\"", country); + return -1; + } + } + } + layout = find_layout(layout_id); if (layout == NULL) { ERR("unknown flash layout \"%s\"", layout_id); @@ -437,6 +476,7 @@ static void fill_header(char *buf, int len) strncpy(hdr->fw_version, version, sizeof(hdr->fw_version)); hdr->hw_id = htonl(hw_id); hdr->hw_rev = htonl(hw_rev); + hdr->region = htonl(region); if (boot_info.file_size == 0) memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1)); @@ -645,9 +685,6 @@ static int inspect_fw(void) inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header)); - if (ntohl(hdr->unk1) != 0) - inspect_fw_phexdec("Unknown value 1", hdr->unk1); - memcpy(md5sum, hdr->md5sum1, sizeof(md5sum)); if (ntohl(hdr->boot_len) == 0) memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum)); @@ -674,6 +711,7 @@ static int inspect_fw(void) inspect_fw_pstr("Firmware version", hdr->fw_version); inspect_fw_phex("Hardware ID", ntohl(hdr->hw_id)); inspect_fw_phex("Hardware Revision", ntohl(hdr->hw_rev)); + inspect_fw_phexpost("Region code", ntohl(hdr->region), get_region_country(ntohl(hdr->region))); printf("\n"); @@ -748,7 +786,7 @@ int main(int argc, char *argv[]) while ( 1 ) { int c; - c = getopt(argc, argv, "a:H:E:F:L:m:V:N:W:ci:k:r:R:o:xX:hsSjv:"); + c = getopt(argc, argv, "a:H:E:F:L:m:V:N:W:C:ci:k:r:R:o:xX:hsSjv:"); if (c == -1) break; @@ -768,6 +806,9 @@ int main(int argc, char *argv[]) case 'W': opt_hw_rev = optarg; break; + case 'C': + country = optarg; + break; case 'L': sscanf(optarg, "0x%x", &kernel_la); break;