From patchwork Sun Nov 25 18:22:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 1002880 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=theobroma-systems.com Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 432z194ZsYz9s29 for ; Mon, 26 Nov 2018 05:22:37 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 5D983C22079; Sun, 25 Nov 2018 18:22:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_DNSWL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 33AE5C22024; Sun, 25 Nov 2018 18:22:31 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 39084C22024; Sun, 25 Nov 2018 18:22:30 +0000 (UTC) Received: from mail.theobroma-systems.com (vegas.theobroma-systems.com [144.76.126.164]) by lists.denx.de (Postfix) with ESMTPS id E0BB9C2200E for ; Sun, 25 Nov 2018 18:22:29 +0000 (UTC) Received: from [86.59.122.178] (port=55888 helo=android.lan) by mail.theobroma-systems.com with esmtpsa (TLS1.2:RSA_AES_128_CBC_SHA256:128) (Exim 4.80) (envelope-from ) id 1gQz34-0002Vm-RV; Sun, 25 Nov 2018 19:22:27 +0100 From: Philipp Tomsich To: u-boot@lists.denx.de Date: Sun, 25 Nov 2018 19:22:18 +0100 Message-Id: <1543170139-37629-1-git-send-email-philipp.tomsich@theobroma-systems.com> X-Mailer: git-send-email 2.1.4 Cc: Joe Hershberger , Maxime Ripard , Philippe Reynes , Eric Jarrige , Raphael Assenat , Hans de Goede , Jeremy Gebben , Heinrich Schuchardt , Jagan Teki Subject: [U-Boot] [PATCH 1/2] lib: merge CRC16-CCITT into u-boot/crc.h X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" This merges the CRC16-CCITT headers into u-boot/crc.h to prepare for rolling CRC16 into the hash infrastructure. Given that CRC8, CRC32 and CRC32-C already have their prototypes in a single header file, it seems a good idea to also include CRC16-CCITT in the same. Signed-off-by: Philipp Tomsich --- board/8dtech/eco5pk/eco5pk.c | 2 +- board/armadeus/apf27/apf27.c | 2 +- board/sunxi/board.c | 2 +- common/xyzModem.c | 2 +- drivers/mmc/mmc_spi.c | 2 +- drivers/net/phy/aquantia.c | 2 +- include/crc.h | 43 ------------------------------------------- include/u-boot/crc.h | 3 +++ lib/crc16.c | 2 +- 9 files changed, 10 insertions(+), 50 deletions(-) delete mode 100644 include/crc.h diff --git a/board/8dtech/eco5pk/eco5pk.c b/board/8dtech/eco5pk/eco5pk.c index e05928f..dcbd483 100644 --- a/board/8dtech/eco5pk/eco5pk.c +++ b/board/8dtech/eco5pk/eco5pk.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include "eco5pk.h" diff --git a/board/armadeus/apf27/apf27.c b/board/armadeus/apf27/apf27.c index 0f0c8a4..bf2586d 100644 --- a/board/armadeus/apf27/apf27.c +++ b/board/armadeus/apf27/apf27.c @@ -16,8 +16,8 @@ #include #include #include +#include #include "apf27.h" -#include "crc.h" #include "fpga.h" DECLARE_GLOBAL_DATA_PTR; diff --git a/board/sunxi/board.c b/board/sunxi/board.c index 64ccbc7..a2254fd 100644 --- a/board/sunxi/board.c +++ b/board/sunxi/board.c @@ -28,7 +28,7 @@ #endif #include #include -#include +#include #include #include #include diff --git a/common/xyzModem.c b/common/xyzModem.c index 830fca8..e5c65b4 100644 --- a/common/xyzModem.c +++ b/common/xyzModem.c @@ -24,7 +24,7 @@ #include #include #include -#include +#include /* Assumption - run xyzModem protocol over the console port */ diff --git a/drivers/mmc/mmc_spi.c b/drivers/mmc/mmc_spi.c index a9d95fb..4f57990 100644 --- a/drivers/mmc/mmc_spi.c +++ b/drivers/mmc/mmc_spi.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include diff --git a/drivers/net/phy/aquantia.c b/drivers/net/phy/aquantia.c index 37749e0..a0abb23 100644 --- a/drivers/net/phy/aquantia.c +++ b/drivers/net/phy/aquantia.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/crc.h b/include/crc.h deleted file mode 100644 index 2a00af5..0000000 --- a/include/crc.h +++ /dev/null @@ -1,43 +0,0 @@ -/* SPDX-License-Identifier: eCos-2.0 */ -/* - *========================================================================== - * - * crc.h - * - * Interface for the CRC algorithms. - * - *========================================================================== - *========================================================================== - *#####DESCRIPTIONBEGIN#### - * - * Author(s): Andrew Lunn - * Contributors: Andrew Lunn - * Date: 2002-08-06 - * Purpose: - * Description: - * - * This code is part of eCos (tm). - * - *####DESCRIPTIONEND#### - * - *========================================================================== - */ - -#ifndef _SERVICES_CRC_CRC_H_ -#define _SERVICES_CRC_CRC_H_ - -#include - -#ifndef __externC -# ifdef __cplusplus -# define __externC extern "C" -# else -# define __externC extern -# endif -#endif - -/* 16 bit CRC with polynomial x^16+x^12+x^5+1 (CRC-CCITT) */ - -uint16_t crc16_ccitt(uint16_t crc_start, unsigned char *s, int len); - -#endif /* _SERVICES_CRC_CRC_H_ */ diff --git a/include/u-boot/crc.h b/include/u-boot/crc.h index e98cb46..111b22c 100644 --- a/include/u-boot/crc.h +++ b/include/u-boot/crc.h @@ -11,6 +11,9 @@ /* lib/crc8.c */ unsigned int crc8(unsigned int crc_start, const unsigned char *vptr, int len); +/* lib/crc16.c - 16 bit CRC with polynomial x^16+x^12+x^5+1 (CRC-CCITT) */ +uint16_t crc16_ccitt(uint16_t crc_start, const unsigned char *s, int len); + /* lib/crc32.c */ uint32_t crc32 (uint32_t, const unsigned char *, uint); uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint); diff --git a/lib/crc16.c b/lib/crc16.c index 763ae33..25bdfd8 100644 --- a/lib/crc16.c +++ b/lib/crc16.c @@ -22,7 +22,7 @@ *========================================================================== */ -#include "crc.h" +#include /* Table of CRC constants - implements x^16+x^12+x^5+1 */ static const uint16_t crc16_tab[] = { From patchwork Sun Nov 25 18:22:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 1002881 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=theobroma-systems.com Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 432z1x68phz9s29 for ; Mon, 26 Nov 2018 05:23:17 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 751DEC2200E; Sun, 25 Nov 2018 18:23:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_DNSWL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id E014AC2204A; Sun, 25 Nov 2018 18:22:44 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 2889BC22066; Sun, 25 Nov 2018 18:22:39 +0000 (UTC) Received: from mail.theobroma-systems.com (vegas.theobroma-systems.com [144.76.126.164]) by lists.denx.de (Postfix) with ESMTPS id E581FC2204A for ; Sun, 25 Nov 2018 18:22:35 +0000 (UTC) Received: from [86.59.122.178] (port=55888 helo=android.lan) by mail.theobroma-systems.com with esmtpsa (TLS1.2:RSA_AES_128_CBC_SHA256:128) (Exim 4.80) (envelope-from ) id 1gQz37-0002Vm-Hp; Sun, 25 Nov 2018 19:22:29 +0100 From: Philipp Tomsich To: u-boot@lists.denx.de Date: Sun, 25 Nov 2018 19:22:19 +0100 Message-Id: <1543170139-37629-2-git-send-email-philipp.tomsich@theobroma-systems.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1543170139-37629-1-git-send-email-philipp.tomsich@theobroma-systems.com> References: <1543170139-37629-1-git-send-email-philipp.tomsich@theobroma-systems.com> Cc: Alexander Graf , Breno Lima , Heinrich Schuchardt Subject: [U-Boot] [PATCH 2/2] Roll CRC16-CCITT into the hash infrastructure X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" The CRC16-CCITT checksum function is useful for space-constrained applications (such as obtaining a checksum across a 2KBit or 4KBit EEPROM) in boot applications. It has not been accessible from boot scripts until now (due to not having a dedicated command and not being supported by the hash infrstructure) limiting its applicability outside of custom commands. This adds the CRC16-CCITT (poly 0x1021, init 0x0) algorithm to the list of available hashes and adds a new crc16_ccitt_wd_buf() to make this possible. Signed-off-by: Philipp Tomsich --- common/hash.c | 36 ++++++++++++++++++++++++++++++++++++ include/u-boot/crc.h | 11 +++++++++++ lib/crc16.c | 23 +++++++++++++++++------ tools/Makefile | 1 + 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/common/hash.c b/common/hash.c index ef14651..413a5bf 100644 --- a/common/hash.c +++ b/common/hash.c @@ -85,6 +85,33 @@ static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void } #endif +static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp) +{ + uint16_t *ctx = malloc(sizeof(uint16_t)); + *ctx = 0; + *ctxp = ctx; + return 0; +} + +static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx, + const void *buf, unsigned int size, + int is_last) +{ + *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size); + return 0; +} + +static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx, + void *dest_buf, int size) +{ + if (size < algo->digest_size) + return -1; + + *((uint16_t *)dest_buf) = *((uint16_t *)ctx); + free(ctx); + return 0; +} + static int hash_init_crc32(struct hash_algo *algo, void **ctxp) { uint32_t *ctx = malloc(sizeof(uint32_t)); @@ -160,6 +187,15 @@ static struct hash_algo hash_algo[] = { }, #endif { + .name = "crc16-ccitt", + .digest_size = 2, + .chunk_size = CHUNKSZ, + .hash_func_ws = crc16_ccitt_wd_buf, + .hash_init = hash_init_crc16_ccitt, + .hash_update = hash_update_crc16_ccitt, + .hash_finish = hash_finish_crc16_ccitt, + }, + { .name = "crc32", .digest_size = 4, .chunk_size = CHUNKSZ_CRC32, diff --git a/include/u-boot/crc.h b/include/u-boot/crc.h index 111b22c..788ef29 100644 --- a/include/u-boot/crc.h +++ b/include/u-boot/crc.h @@ -13,6 +13,17 @@ unsigned int crc8(unsigned int crc_start, const unsigned char *vptr, int len); /* lib/crc16.c - 16 bit CRC with polynomial x^16+x^12+x^5+1 (CRC-CCITT) */ uint16_t crc16_ccitt(uint16_t crc_start, const unsigned char *s, int len); +/** + * crc16_ccitt_wd_buf - Perform CRC16-CCIT on an input buffer and return the + * 16-bit result (network byte-order) in an output buffer + * + * @in: input buffer + * @len: input buffer length + * @out: output buffer (at least 2 bytes) + * @chunk_sz: ignored + */ +void crc16_ccitt_wd_buf(const uint8_t *in, uint len, + uint8_t *out, uint chunk_sz); /* lib/crc32.c */ uint32_t crc32 (uint32_t, const unsigned char *, uint); diff --git a/lib/crc16.c b/lib/crc16.c index 25bdfd8..f46ba72 100644 --- a/lib/crc16.c +++ b/lib/crc16.c @@ -22,6 +22,11 @@ *========================================================================== */ +#ifdef USE_HOSTCC +#include +#else +#include +#endif #include /* Table of CRC constants - implements x^16+x^12+x^5+1 */ @@ -60,14 +65,20 @@ static const uint16_t crc16_tab[] = { 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0, }; -uint16_t crc16_ccitt(uint16_t crc_start, unsigned char *buf, int len) +uint16_t crc16_ccitt(uint16_t cksum, const unsigned char *buf, int len) { - int i; - uint16_t cksum; - - cksum = crc_start; - for (i = 0; i < len; i++) + for (int i = 0; i < len; i++) cksum = crc16_tab[((cksum>>8) ^ *buf++) & 0xff] ^ (cksum << 8); return cksum; } + +void crc16_ccitt_wd_buf(const uint8_t *in, uint len, + uint8_t *out, uint chunk_sz) +{ + uint16_t crc; + + crc = crc16_ccitt(0, in, len); + crc = htons(crc); + memcpy(out, &crc, sizeof(crc)); +} diff --git a/tools/Makefile b/tools/Makefile index 3c0521f..a338705 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -106,6 +106,7 @@ dumpimage-mkimage-objs := aisimage.o \ stm32image.o \ $(ROCKCHIP_OBS) \ socfpgaimage.o \ + lib/crc16.o \ lib/sha1.o \ lib/sha256.o \ common/hash.o \