mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Johannes Zink <j.zink@pengutronix.de>
To: patchwork-jzi@pengutronix.de
Cc: Johannes Zink <j.zink@pengutronix.de>,
	 Barebox Mailing List <barebox@lists.infradead.org>
Subject: [PATCH v2] crypto: crc32: make crc32 available in PBL
Date: Tue, 29 Aug 2023 16:38:32 +0200	[thread overview]
Message-ID: <20230829-crc32_in_pbl-v2-1-8a0a575b360a@pengutronix.de> (raw)

crc32 may be required in PBL for checking data integrity. Add it to PBL
when CONFIG_CRC32 is enabled.

To save some memory use a slower-but-smaller variant of the crc32 algorithm
in the PBL.

Signed-off-by: Johannes Zink <j.zink@pengutronix.de>
---
To: Barebox Mailing List <barebox@lists.infradead.org>
Cc: Johannes Zink <j.zink@pengutronix.de>
Cc: patchwork-jzi@pengutronix.de
---

Changes:

v1->v2: Worked in Ahmad's review (thank you for reviewing my patch):
  - instead of using CRC32_EARLY, always add crc32.o to obj and pbl if
    CONFIG_CRC is set and rely on LTO to remove unused symbols instead
  - use a CRC32 implementation without a prepopulated Lookup Table,
    which trades in speed for code size in the PBL, analogously to what
    Sascha implemented in [1]
  - add hints on some of the #endifs for better readability of nested
    ifdef blocks
  - reword commit message to reflect on the changes made

    [1] 2d13b856604b ("crc: Add PBL variant for crc_itu_t()")
---
 crypto/Makefile |  2 +-
 crypto/crc32.c  | 34 ++++++++++++++++++++++++++++------
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/crypto/Makefile b/crypto/Makefile
index 22035d4f69ee..4a1c7e9615b8 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
-obj-$(CONFIG_CRC32)	+= crc32.o
+obj-pbl-$(CONFIG_CRC32)	+= crc32.o
 obj-pbl-$(CONFIG_CRC_ITU_T)	+= crc-itu-t.o
 obj-$(CONFIG_CRC7)	+= crc7.o
 obj-$(CONFIG_DIGEST)	+= digest.o
diff --git a/crypto/crc32.c b/crypto/crc32.c
index 95cb2212db2b..2cf13144c1d2 100644
--- a/crypto/crc32.c
+++ b/crypto/crc32.c
@@ -22,7 +22,7 @@
 #define STATIC static inline
 #endif
 
-#ifdef CONFIG_DYNAMIC_CRC_TABLE
+#if defined(CONFIG_DYNAMIC_CRC_TABLE) && !defined(__PBL__)
 
 static uint32_t *crc_table;
 
@@ -73,7 +73,7 @@ static void make_crc_table(void)
     crc_table[n] = c;
   }
 }
-#else
+#elif !defined(__PBL__)
 /* ========================================================================
  * Table of CRC-32's of all single-byte values (made by make_crc_table)
  */
@@ -131,9 +131,30 @@ static const uint32_t crc_table[256] = {
   0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
   0x2d02ef8dL
 };
-#endif
+#endif /* defined(CONFIG_DYNAMIC_CRC_TABLE) && !defined(__PBL__) */
+
+
+
+#ifdef __PBL__
+#define CRC32_POLY 0xEDB88320L
+STATIC uint32_t crc32(uint32_t crc, const void *_buf, unsigned int len)
+{
+	int i, j;
+	const unsigned char *buf = _buf;
 
+	crc = crc ^ 0xffffffffL;
 
+	for (i=0; i< len; i++) {
+		crc ^= buf[i];
+		for (j = 0; j < 8; j++) {
+			crc = (crc >> 1) ^ ((crc & 1) ? CRC32_POLY : 0);
+		}
+	}
+
+	crc = crc ^ 0xffffffffL;
+	return crc;
+}
+#else
 /* ========================================================================= */
 #define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
 #define DO2(buf)  DO1(buf); DO1(buf);
@@ -148,7 +169,7 @@ STATIC uint32_t crc32(uint32_t crc, const void *_buf, unsigned int len)
 #ifdef CONFIG_DYNAMIC_CRC_TABLE
 	if (!crc_table)
 		make_crc_table();
-#endif
+#endif /* CONFIG_DYNAMIC_CRC_TABLE */
     crc = crc ^ 0xffffffffL;
     while (len >= 8)
     {
@@ -162,7 +183,7 @@ STATIC uint32_t crc32(uint32_t crc, const void *_buf, unsigned int len)
 }
 #ifdef __BAREBOX__
 EXPORT_SYMBOL(crc32);
-#endif
+#endif /* __BAREBOX__ */
 
 /* No ones complement version. JFFS2 (and other things ?)
  * don't use ones compliment in their CRC calculations.
@@ -174,7 +195,7 @@ STATIC uint32_t crc32_no_comp(uint32_t crc, const void *_buf, unsigned int len)
 #ifdef CONFIG_DYNAMIC_CRC_TABLE
 	if (!crc_table)
 		make_crc_table();
-#endif
+#endif /* CONFIG_DYNAMIC_CRC_TABLE */
     while (len >= 8)
     {
       DO8(buf);
@@ -186,6 +207,7 @@ STATIC uint32_t crc32_no_comp(uint32_t crc, const void *_buf, unsigned int len)
 
     return crc;
 }
+#endif /* __PBL__ */
 
 STATIC uint32_t crc32_be(uint32_t crc, const void *_buf, unsigned int len)
 {

---
base-commit: bef38b18eeb5d2f1fac334fb8b831e47261e099c
change-id: 20230829-crc32_in_pbl-4d824629d4e2

Best regards,
-- 
Johannes Zink <j.zink@pengutronix.de>




             reply	other threads:[~2023-08-29 14:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-29 14:38 Johannes Zink [this message]
2023-08-29 15:00 ` Ahmad Fatoum
2023-09-04  8:19 ` Sascha Hauer
2023-09-05  7:57   ` Johannes Zink
2023-09-05 12:26     ` Sascha Hauer
2023-09-05 12:27       ` Johannes Zink

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230829-crc32_in_pbl-v2-1-8a0a575b360a@pengutronix.de \
    --to=j.zink@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=patchwork-jzi@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox