diff mbox series

[U-Boot,1/9] lib: Add CRC32-C

Message ID 20170903150031.18179-2-marek.behun@nic.cz
State Accepted
Commit 85d8bf57131a21424b50e50884372e813345f09a
Delegated to: Tom Rini
Headers show
Series Add single-device read-only BTRFS support | expand

Commit Message

Marek Behún Sept. 3, 2017, 3 p.m. UTC
This is needed for BTRFS.

Signed-off-by: Marek Behun <marek.behun@nic.cz>

 create mode 100644 lib/crc32c.c

Comments

Lukasz Majewski Sept. 3, 2017, 3:47 p.m. UTC | #1
Hi Marek,

> This is needed for BTRFS.
> 
> Signed-off-by: Marek Behun <marek.behun@nic.cz>
> 
>   create mode 100644 lib/crc32c.c

Excuse me my ignorance, but in u-boot we already have:
./lib/crc32.c

is the crc32c algorithm a different one from venerable crc32?


> 
> diff --git a/include/u-boot/crc.h b/include/u-boot/crc.h
> index 6764d58bab..6d08f5df98 100644
> --- a/include/u-boot/crc.h
> +++ b/include/u-boot/crc.h
> @@ -28,4 +28,8 @@ uint32_t crc32_no_comp (uint32_t, const unsigned char *, uint);
>   void crc32_wd_buf(const unsigned char *input, uint ilen,
>   		    unsigned char *output, uint chunk_sz);
>   
> +/* lib/crc32c.c */
> +void crc32c_init(uint32_t *, uint32_t);
> +uint32_t crc32c_cal(uint32_t, const char *, int, uint32_t *);
> +
>   #endif /* _UBOOT_CRC_H */
> diff --git a/lib/Kconfig b/lib/Kconfig
> index fe337acaeb..29e55dbe1d 100644
> --- a/lib/Kconfig
> +++ b/lib/Kconfig
> @@ -146,6 +146,9 @@ config SHA_PROG_HW_ACCEL
>   config MD5
>   	bool
>   
> +config CRC32C
> +	bool
> +
>   endmenu
>   
>   menu "Compression Support"
> diff --git a/lib/Makefile b/lib/Makefile
> index 2eef1eb80e..a58ce0f815 100644
> --- a/lib/Makefile
> +++ b/lib/Makefile
> @@ -67,6 +67,7 @@ obj-y += display_options.o
>   CFLAGS_display_options.o := $(if $(BUILD_TAG),-DBUILD_TAG='"$(BUILD_TAG)"')
>   obj-$(CONFIG_BCH) += bch.o
>   obj-y += crc32.o
> +obj-$(CONFIG_CRC32C) += crc32c.o
>   obj-y += ctype.o
>   obj-y += div64.o
>   obj-y += hang.o
> diff --git a/lib/crc32c.c b/lib/crc32c.c
> new file mode 100644
> index 0000000000..322c08ff5d
> --- /dev/null
> +++ b/lib/crc32c.c
> @@ -0,0 +1,38 @@
> +/*
> + * Copied from Linux kernel crypto/crc32c.c
> + * Copyright (c) 2004 Cisco Systems, Inc.
> + * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU General Public License as published by the Free
> + * Software Foundation; either version 2 of the License, or (at your option)
> + * any later version.
> + * SPDX-License-Identifier:	GPL-2.0+
> + */
> +
> +#include <common.h>
> +#include <compiler.h>
> +
> +uint32_t crc32c_cal(uint32_t crc, const char *data, int length,
> +		    uint32_t *crc32c_table)
> +{
> +	while (length--)
> +		crc = crc32c_table[(u8)(crc ^ *data++)] ^ (crc >> 8);
> +
> +	return crc;
> +}
> +
> +void crc32c_init(uint32_t *crc32c_table, uint32_t pol)
> +{
> +	int i, j;
> +	uint32_t v;
> +	const uint32_t poly = pol; /* Bit-reflected CRC32C polynomial */
> +
> +	for (i = 0; i < 256; i++) {
> +		v = i;
> +		for (j = 0; j < 8; j++)
> +			v = (v >> 1) ^ ((v & 1) ? poly : 0);
> +
> +		crc32c_table[i] = v;
> +	}
> +}
>
Marek Behún Sept. 3, 2017, 7:25 p.m. UTC | #2
Hi Łukasz,

the Castagnoli CRC32 algorithm just uses different polynomial. Perhaps
I should generalize the original CRC32.

Marek

On Sun, 3 Sep 2017 17:47:28 +0200
Łukasz Majewski <lukma@denx.de> wrote:

> Hi Marek,
> 
> > This is needed for BTRFS.
> > 
> > Signed-off-by: Marek Behun <marek.behun@nic.cz>
> > 
> >   create mode 100644 lib/crc32c.c  
> 
> Excuse me my ignorance, but in u-boot we already have:
> ./lib/crc32.c
> 
> is the crc32c algorithm a different one from venerable crc32?
> 
> 
> > 
> > diff --git a/include/u-boot/crc.h b/include/u-boot/crc.h
> > index 6764d58bab..6d08f5df98 100644
> > --- a/include/u-boot/crc.h
> > +++ b/include/u-boot/crc.h
> > @@ -28,4 +28,8 @@ uint32_t crc32_no_comp (uint32_t, const unsigned
> > char *, uint); void crc32_wd_buf(const unsigned char *input, uint
> > ilen, unsigned char *output, uint chunk_sz);
> >   
> > +/* lib/crc32c.c */
> > +void crc32c_init(uint32_t *, uint32_t);
> > +uint32_t crc32c_cal(uint32_t, const char *, int, uint32_t *);
> > +
> >   #endif /* _UBOOT_CRC_H */
> > diff --git a/lib/Kconfig b/lib/Kconfig
> > index fe337acaeb..29e55dbe1d 100644
> > --- a/lib/Kconfig
> > +++ b/lib/Kconfig
> > @@ -146,6 +146,9 @@ config SHA_PROG_HW_ACCEL
> >   config MD5
> >   	bool
> >   
> > +config CRC32C
> > +	bool
> > +
> >   endmenu
> >   
> >   menu "Compression Support"
> > diff --git a/lib/Makefile b/lib/Makefile
> > index 2eef1eb80e..a58ce0f815 100644
> > --- a/lib/Makefile
> > +++ b/lib/Makefile
> > @@ -67,6 +67,7 @@ obj-y += display_options.o
> >   CFLAGS_display_options.o := $(if
> > $(BUILD_TAG),-DBUILD_TAG='"$(BUILD_TAG)"') obj-$(CONFIG_BCH) +=
> > bch.o obj-y += crc32.o
> > +obj-$(CONFIG_CRC32C) += crc32c.o
> >   obj-y += ctype.o
> >   obj-y += div64.o
> >   obj-y += hang.o
> > diff --git a/lib/crc32c.c b/lib/crc32c.c
> > new file mode 100644
> > index 0000000000..322c08ff5d
> > --- /dev/null
> > +++ b/lib/crc32c.c
> > @@ -0,0 +1,38 @@
> > +/*
> > + * Copied from Linux kernel crypto/crc32c.c
> > + * Copyright (c) 2004 Cisco Systems, Inc.
> > + * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
> > + *
> > + * This program is free software; you can redistribute it and/or
> > modify it
> > + * under the terms of the GNU General Public License as published
> > by the Free
> > + * Software Foundation; either version 2 of the License, or (at
> > your option)
> > + * any later version.
> > + * SPDX-License-Identifier:	GPL-2.0+
> > + */
> > +
> > +#include <common.h>
> > +#include <compiler.h>
> > +
> > +uint32_t crc32c_cal(uint32_t crc, const char *data, int length,
> > +		    uint32_t *crc32c_table)
> > +{
> > +	while (length--)
> > +		crc = crc32c_table[(u8)(crc ^ *data++)] ^ (crc >>
> > 8); +
> > +	return crc;
> > +}
> > +
> > +void crc32c_init(uint32_t *crc32c_table, uint32_t pol)
> > +{
> > +	int i, j;
> > +	uint32_t v;
> > +	const uint32_t poly = pol; /* Bit-reflected CRC32C
> > polynomial */ +
> > +	for (i = 0; i < 256; i++) {
> > +		v = i;
> > +		for (j = 0; j < 8; j++)
> > +			v = (v >> 1) ^ ((v & 1) ? poly : 0);
> > +
> > +		crc32c_table[i] = v;
> > +	}
> > +}
> >   
> 
>
Tom Rini Oct. 3, 2017, 12:51 p.m. UTC | #3
On Sun, Sep 03, 2017 at 05:00:23PM +0200, Marek Behún wrote:

> This is needed for BTRFS.
> 
> Signed-off-by: Marek Behun <marek.behun@nic.cz>
> 
>  create mode 100644 lib/crc32c.c
> 
> diff --git a/include/u-boot/crc.h b/include/u-boot/crc.h
> index 6764d58bab..6d08f5df98 100644

Note that in the Linux Kernel, crc32 and crc32c are still separate in
some cases, so I'm OK with starting out this way.  Applied to
u-boot/master, thanks!
diff mbox series

Patch

diff --git a/include/u-boot/crc.h b/include/u-boot/crc.h
index 6764d58bab..6d08f5df98 100644
--- a/include/u-boot/crc.h
+++ b/include/u-boot/crc.h
@@ -28,4 +28,8 @@  uint32_t crc32_no_comp (uint32_t, const unsigned char *, uint);
 void crc32_wd_buf(const unsigned char *input, uint ilen,
 		    unsigned char *output, uint chunk_sz);
 
+/* lib/crc32c.c */
+void crc32c_init(uint32_t *, uint32_t);
+uint32_t crc32c_cal(uint32_t, const char *, int, uint32_t *);
+
 #endif /* _UBOOT_CRC_H */
diff --git a/lib/Kconfig b/lib/Kconfig
index fe337acaeb..29e55dbe1d 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -146,6 +146,9 @@  config SHA_PROG_HW_ACCEL
 config MD5
 	bool
 
+config CRC32C
+	bool
+
 endmenu
 
 menu "Compression Support"
diff --git a/lib/Makefile b/lib/Makefile
index 2eef1eb80e..a58ce0f815 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -67,6 +67,7 @@  obj-y += display_options.o
 CFLAGS_display_options.o := $(if $(BUILD_TAG),-DBUILD_TAG='"$(BUILD_TAG)"')
 obj-$(CONFIG_BCH) += bch.o
 obj-y += crc32.o
+obj-$(CONFIG_CRC32C) += crc32c.o
 obj-y += ctype.o
 obj-y += div64.o
 obj-y += hang.o
diff --git a/lib/crc32c.c b/lib/crc32c.c
new file mode 100644
index 0000000000..322c08ff5d
--- /dev/null
+++ b/lib/crc32c.c
@@ -0,0 +1,38 @@ 
+/*
+ * Copied from Linux kernel crypto/crc32c.c
+ * Copyright (c) 2004 Cisco Systems, Inc.
+ * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <compiler.h>
+
+uint32_t crc32c_cal(uint32_t crc, const char *data, int length,
+		    uint32_t *crc32c_table)
+{
+	while (length--)
+		crc = crc32c_table[(u8)(crc ^ *data++)] ^ (crc >> 8);
+
+	return crc;
+}
+
+void crc32c_init(uint32_t *crc32c_table, uint32_t pol)
+{
+	int i, j;
+	uint32_t v;
+	const uint32_t poly = pol; /* Bit-reflected CRC32C polynomial */
+
+	for (i = 0; i < 256; i++) {
+		v = i;
+		for (j = 0; j < 8; j++)
+			v = (v >> 1) ^ ((v & 1) ? poly : 0);
+
+		crc32c_table[i] = v;
+	}
+}