Discussion:
[U-Boot] [PATCH 1/2] lib: merge CRC16-CCITT into u-boot/crc.h
Philipp Tomsich
2018-11-25 18:22:18 UTC
Permalink
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 <***@theobroma-systems.com>
---

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 <asm/arch/emac_defs.h>
#include <asm/gpio.h>
#include <i2c.h>
-#include <crc.h>
+#include <u-boot/crc.h>
#include <asm/mach-types.h>
#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 <asm/arch/gpio.h>
#include <asm/gpio.h>
#include <linux/errno.h>
+#include <u-boot/crc.h>
#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 <asm/gpio.h>
#include <asm/io.h>
-#include <crc.h>
+#include <u-boot/crc.h>
#include <environment.h>
#include <linux/libfdt.h>
#include <nand.h>
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 <common.h>
#include <xyzModem.h>
#include <stdarg.h>
-#include <crc.h>
+#include <u-boot/crc.h>

/* 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 <part.h>
#include <mmc.h>
#include <spi.h>
-#include <crc.h>
+#include <u-boot/crc.h>
#include <linux/crc7.h>
#include <asm/byteorder.h>

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 <common.h>
#include <dm.h>
#include <phy.h>
-#include <crc.h>
+#include <u-boot/crc.h>
#include <malloc.h>
#include <asm/byteorder.h>
#include <fs.h>
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 <linux/types.h>
-
-#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 <u-boot/crc.h>

/* Table of CRC constants - implements x^16+x^12+x^5+1 */
static const uint16_t crc16_tab[] = {
--
2.1.4
Philipp Tomsich
2018-11-25 18:22:19 UTC
Permalink
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 <***@theobroma-systems.com>
---

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 <arpa/inet.h>
+#else
+#include <common.h>
+#endif
#include <u-boot/crc.h>

/* 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 \
--
2.1.4
Tom Rini
2018-12-09 01:19:46 UTC
Permalink
Post by Philipp Tomsich
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.
Applied to u-boot/master, thanks!
--
Tom
Tom Rini
2018-12-09 01:19:38 UTC
Permalink
Post by Philipp Tomsich
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.
Applied to u-boot/master, thanks!
--
Tom
Loading...