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@xxxxxxxxxxxxxx> --- To: Barebox Mailing List <barebox@xxxxxxxxxxxxxxxxxxx> Cc: Johannes Zink <j.zink@xxxxxxxxxxxxxx> Cc: patchwork-jzi@xxxxxxxxxxxxxx --- 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@xxxxxxxxxxxxxx>