Skip to contentSkip to conversation

lib: merge CRC16-CCITT into u-boot/crc.h

PATCH New Philipp Tomsich 2 participants 4 messages Nov 25, 2018 – Dec 9, 2018

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 lib: merge CRC16-CCITT into u-boot/crc.h

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.

---

 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
board/8dtech/eco5pk/eco5pk.c +1 -1
index e05928f..dcbd483 100644
@@ -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"
 
board/armadeus/apf27/apf27.c +1 -1
index 0f0c8a4..bf2586d 100644
@@ -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;
board/sunxi/board.c +1 -1
index 64ccbc7..a2254fd 100644
@@ -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>
common/xyzModem.c +1 -1
index 830fca8..e5c65b4 100644
@@ -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 */
 
drivers/mmc/mmc_spi.c +1 -1
index a9d95fb..4f57990 100644
@@ -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>
 
drivers/net/phy/aquantia.c +1 -1
index 37749e0..a0abb23 100644
@@ -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>
include/crc.h deleted +0 -43

43 lines not on this page · raw message

include/u-boot/crc.h +3 -0
index e98cb46..111b22c 100644
@@ -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);
lib/crc16.c +1 -1
index 763ae33..25bdfd8 100644
@@ -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[] = {
Signature · 1 line
2.1.4
#1 lore raw

PATCH 2/2 Roll CRC16-CCITT into the hash infrastructure

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.

---

 common/hash.c        | 36 ++++++++++++++++++++++++++++++++++++
 include/u-boot/crc.h | 11 +++++++++++
 lib/crc16.c          | 23 +++++++++++++++++------
 tools/Makefile       |  1 +
 4 files changed, 65 insertions(+), 6 deletions(-)
common/hash.c +36 -0
index ef14651..413a5bf 100644
@@ -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,
include/u-boot/crc.h +11 -0
index 111b22c..788ef29 100644
@@ -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);
lib/crc16.c +17 -6
index 25bdfd8..f46ba72 100644
@@ -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));
+}
tools/Makefile +1 -0
index 3c0521f..a338705 100644
@@ -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 \
Signature · 1 line
2.1.4
#2 lore raw

PATCH 2/2 Roll CRC16-CCITT into the hash infrastructure

Quoted 7 lines · 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.

Quoted 5 lines

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!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20181208/02411367/attachment.sig>
#3 lore raw

PATCH 1/2 lib: merge CRC16-CCITT into u-boot/crc.h

In reply to Philipp Tomsich
Quoted 6 lines · 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!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20181208/f06c124e/attachment.sig>