From patchwork Mon Sep 28 16:37:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Todor Colov X-Patchwork-Id: 1372829 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=abv.bg Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=xIaOA7/m; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=abv.bg header.i=@abv.bg header.a=rsa-sha256 header.s=smtp-out header.b=iH5o2h7m; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4C0Vhs3j9pz9sTv for ; Tue, 29 Sep 2020 04:02:03 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ukxQzQ04v9GgHFHrCV78pLdIm3fGGEoMu1xoUNrT9OU=; b=xIaOA7/mQoWX36/nRY4P+Mp86 4ChB2p61zTrPpzOC+F/FcnmluCt1puiJySV8z/t7v/h/8LmD4d2b001Vzg4JEyBxGcgjhPsxU60os U4awTkk/fWbCqJF4XDVUAgcmCDYWkofHOe2wvha+7GlsIMMeaja7JzPq8JIjhx4bjLeDUmbMk8Cf+ GuAaKKvttZWieVJIuvxgQ55hvAbqPKwtudPQDpXDdwjPWjtMoJnN5ipnXJeGrrlB4UcjSe0unI6Wq E2anZ5eHlSeaJw0HCfBOiiX8PxvgEnFaVC8uTuHbOEcGEYxwYjgzEGFOLXcCso5R0KaDCB+kgKULD 1EsJiqgtw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kMxRx-00036e-5P; Mon, 28 Sep 2020 18:00:33 +0000 Received: from pop31.abv.bg ([194.153.145.221]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kMwAO-0001ed-Tk for openwrt-devel@lists.openwrt.org; Mon, 28 Sep 2020 16:38:23 +0000 Received: from smtp.abv.bg (localhost [127.0.0.1]) by pop31.abv.bg (Postfix) with ESMTP id 4AA3B1805C61; Mon, 28 Sep 2020 19:38:18 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=abv.bg; s=smtp-out; t=1601311098; bh=g/i/TtH4NwCwsVA06fptZ6aSSkpo1u1tmDsnh7P8i+4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iH5o2h7mg77ZIRHUacmxXMtd6z+TQqo/YVcF9Rdjw1g6gbWXlE+evMHslH/p1mYFq rQW+ECW1vx+z2D4nlOwZKMFBJEeaSN8NLyDXvr33Xh2R0Le0Oos+Y0tm0nrcZ7fOhB PXCNTUUdczVjE7YxrpHJONDLNgjcK6SHsIfChW5k= X-HELO: tchome.lan Authentication-Results: smtp.abv.bg; auth=pass (plain) smtp.auth=todorcolov@abv.bg Received: from host.48.137.bol-bg.com (HELO tchome.lan) (78.128.48.137) by smtp.abv.bg (qpsmtpd/0.96) with ESMTPSA (ECDHE-RSA-AES256-GCM-SHA384 encrypted); Mon, 28 Sep 2020 19:38:18 +0300 From: Todor Colov To: openwrt-devel@lists.openwrt.org Subject: [PATCH v4 4/4] tools: addfwhrd Linksys E8350v1 FW header genarator Date: Mon, 28 Sep 2020 19:37:38 +0300 Message-Id: <20200928163738.2348371-5-todorcolov@abv.bg> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928163738.2348371-1-todorcolov@abv.bg> References: <20200928163738.2348371-1-todorcolov@abv.bg> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200928_123821_389409_DCB14AEF X-CRM114-Status: GOOD ( 28.56 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [todorcolov[at]abv.bg] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Todor Colov Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Signed-off-by: Todor Colov --- tools/firmware-utils/Makefile | 1 + tools/firmware-utils/src/addfwhdr.c | 233 ++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+) create mode 100644 tools/firmware-utils/src/addfwhdr.c diff --git a/tools/firmware-utils/Makefile b/tools/firmware-utils/Makefile index 39c3503886..b83acc7652 100644 --- a/tools/firmware-utils/Makefile +++ b/tools/firmware-utils/Makefile @@ -25,6 +25,7 @@ endef define Host/Compile mkdir -p $(HOST_BUILD_DIR)/bin $(call cc,add_header) + $(call cc,addfwhdr cyg_crc32) $(call cc,addpattern) $(call cc,asustrx) $(call cc,buffalo-enc buffalo-lib,-Wall) diff --git a/tools/firmware-utils/src/addfwhdr.c b/tools/firmware-utils/src/addfwhdr.c new file mode 100644 index 0000000000..ada56e6abf --- /dev/null +++ b/tools/firmware-utils/src/addfwhdr.c @@ -0,0 +1,233 @@ +/* + Linksys e8350 v1 firmware header generator +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cyg_crc.h" +#define AC2350 20 +#define USA 1 + +#if LOCALE == USA + #define CYBERTAN_VERSION "v1.0.03" + #define SERIAL_NUMBER "003" + #define MINOR_VERSION "" + #define BUILD_KEYWORD " B" + #define BUILD_NUMBER SERIAL_NUMBER + #define BETA_VERSION " " + #define CYBERTAN_UBOOT_VERSION "v1.0" +#else // ETSI + #define CYBERTAN_VERSION "v1.0.00" + #define SERIAL_NUMBER "01" + #define MINOR_VERSION "" + #define BUILD_KEYWORD " B" + #define BUILD_NUMBER SERIAL_NUMBER + #define BETA_VERSION " " + #define CYBERTAN_UBOOT_VERSION "v1.0" +#endif + +//add for AC2350 F/W header +#define FWHDR_MAGIC_STR "CHDR" +#define FWHDR_MAGIC 0X52444843 +struct cbt_fw_header +{ + unsigned int magic; /* "CHDR" */ + unsigned int len; /* Length of file including header */ + unsigned int crc32; /* 32-bit CRC */ + unsigned int res; +}; + +#define MAX_BUF 1024 +#define CRC32_INIT_VALUE 0xffffffff /* Initial CRC32 checksum value */ + +#ifndef TYPEDEF_UINT8 +typedef unsigned char uint8; +#endif + +#ifndef TYPEDEF_UINT16 +typedef unsigned short uint16; +#endif + +#ifndef TYPEDEF_UINT32 +typedef unsigned int uint32; +#endif + +typedef uint32_t cyg_uint32; +typedef uint16_t cyg_uint16; + +extern cyg_uint32 cyg_crc32_accumulate(cyg_uint32 crc32val, unsigned char *s, int len); +//extern uint32 crc32(uint8 *pdata, uint nbytes, uint32 crc); + +int fd, fd_w; + +void die(const char * str, ...) +{ + va_list args; + va_start(args, str); + vfprintf(stderr, str, args); + fputc('\n', stderr); + exit(1); +} + +int fill_null0(int size) +{ + unsigned char buf[1]; + int i; + + fprintf(stderr,"Fill null\n"); + + buf[0] = 0xff; + for (i=0 ; i< size; i++ ) + if (write(fd_w, buf, 1) != 1) + return 0; + + return 1; +} + +long file_open(const char *name) +{ + struct stat sb; + if ((fd = open(name, O_RDONLY, 0)) < 0){ + die("Unable to open `%s' : %m", name); + } + + if (fstat (fd, &sb)) + die("Unable to stat `%s' : %m", name); + + return sb.st_size; +} + +void usage(void) +{ + die("Usage: addfwhdr [-i|--input] sysupgrade.o [-o|--output] code.bin\n"); +} + +int main(int argc, char ** argv) +{ + uint input_size,c; + char *input_file=NULL, *output_file=NULL; + int opt; + int option_index=0; + int garbage = 0; + char *buf = NULL; + extern char *optarg; + extern int optind, opterr, optopt; + + struct cbt_fw_header *fwhdr; + uint32 crc; + + static struct option long_options[] = + { + {"input", 1, 0, 'i'}, + {"output", 1, 0, 'o'}, + {"garbage", 0, 0, 'g'}, + {0, 0, 0, 0} + }; + + printf("\n---------- add fw header --------\n"); + + fwhdr = malloc(sizeof(struct cbt_fw_header)); + memset(fwhdr, 0, sizeof(struct cbt_fw_header)); + + while(1){ + opt = getopt_long(argc, argv, "i:o:g",long_options, &option_index); + if(opt == -1) + break; + switch(opt){ + case 'h' : + usage(); break; + case 'i' : + input_file = optarg; + printf("input file is [%s]\n",input_file); break; + case 'o' : + output_file = optarg; + printf("output file is [%s]\n",output_file); break; + case 'g' : + garbage = 1; break; + default : + usage(); + } + } + + if(!input_file || !output_file) + { + printf("You must specify the input and output file!\n"); + usage(); + } + + + unlink(output_file); + if ((fd_w = open(output_file,O_RDWR|O_CREAT, S_IREAD | S_IWRITE)) < 0){ + die("Unable to open `%s' : %m", output_file); + } + + fwhdr = malloc(sizeof(struct cbt_fw_header)); + memset(fwhdr, 0, sizeof(struct cbt_fw_header)); + + memcpy((char *)&fwhdr->magic, FWHDR_MAGIC_STR, sizeof(fwhdr->magic)); + + input_size = file_open(input_file); + if(!(buf = malloc(input_size))) + { + perror("malloc"); + goto fail; + } + c = read(fd, buf, input_size); + fwhdr->len = input_size + sizeof(struct cbt_fw_header); + + fwhdr->res = fwhdr->res | 0x1; + + //junzhao add fw build date in cbt_fw_header for calculate MD5 + #if 0 + { + static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + int year, day, mon=1; + char temp[10]={0}; + unsigned char *p_date = (unsigned char *)&fwhdr->res; + int j; + + sscanf(__DATE__, "%s %d %d", temp, &day, &year); + + for(j=0;j<12;j++) { + if(!strcmp(temp, months[j])) { + mon = j+1; + break; + } + } + p_date[0]= year-2000; + p_date[1]= mon; + p_date[2]= day; + + printf("%s: Build date(20%d-%d-%d)\n", __FUNCTION__, p_date[0], p_date[1], p_date[2]); + + } + #endif + //end + + crc = cyg_crc32_accumulate(CRC32_INIT_VALUE,(uint8 *)&fwhdr->res, 4); + crc = cyg_crc32_accumulate(crc,(uint8 *)&buf[0], input_size); + + fwhdr->crc32 = crc; + + /* write code pattern header */ + write(fd_w, fwhdr, sizeof(struct cbt_fw_header)); + + if(write(fd_w, buf, c)!=c) + die("Write call failed!\n"); + +fail: + if(buf) + free(buf); + close (fd); + close (fd_w); + + return 0; +}