xref: /OK3568_Linux_fs/u-boot/drivers/mtd/ubi/crc32.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
3*4882a593Smuzhiyun  * Nicer crc32 functions/docs submitted by linux@horizon.com.  Thanks!
4*4882a593Smuzhiyun  * Code was from the public domain, copyright abandoned.  Code was
5*4882a593Smuzhiyun  * subsequently included in the kernel, thus was re-licensed under the
6*4882a593Smuzhiyun  * GNU GPL v2.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
9*4882a593Smuzhiyun  * Same crc32 function was used in 5 other places in the kernel.
10*4882a593Smuzhiyun  * I made one version, and deleted the others.
11*4882a593Smuzhiyun  * There are various incantations of crc32().  Some use a seed of 0 or ~0.
12*4882a593Smuzhiyun  * Some xor at the end with ~0.  The generic crc32() function takes
13*4882a593Smuzhiyun  * seed as an argument, and doesn't xor at the end.  Then individual
14*4882a593Smuzhiyun  * users can do whatever they need.
15*4882a593Smuzhiyun  *   drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
16*4882a593Smuzhiyun  *   fs/jffs2 uses seed 0, doesn't xor with ~0.
17*4882a593Smuzhiyun  *   fs/partitions/efi.c uses seed ~0, xor's with ~0.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * This source code is licensed under the GNU General Public License,
20*4882a593Smuzhiyun  * Version 2.  See the file COPYING for more details.
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #ifndef __UBOOT__
24*4882a593Smuzhiyun #include <linux/crc32.h>
25*4882a593Smuzhiyun #include <linux/kernel.h>
26*4882a593Smuzhiyun #include <linux/module.h>
27*4882a593Smuzhiyun #include <linux/compiler.h>
28*4882a593Smuzhiyun #endif
29*4882a593Smuzhiyun #include <linux/types.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <asm/byteorder.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #ifndef __UBOOT__
34*4882a593Smuzhiyun #include <linux/slab.h>
35*4882a593Smuzhiyun #include <linux/init.h>
36*4882a593Smuzhiyun #include <asm/atomic.h>
37*4882a593Smuzhiyun #endif
38*4882a593Smuzhiyun #include "crc32defs.h"
39*4882a593Smuzhiyun #define CRC_LE_BITS 8
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #if CRC_LE_BITS == 8
42*4882a593Smuzhiyun #define tole(x) cpu_to_le32(x)
43*4882a593Smuzhiyun #define tobe(x) cpu_to_be32(x)
44*4882a593Smuzhiyun #else
45*4882a593Smuzhiyun #define tole(x) (x)
46*4882a593Smuzhiyun #define tobe(x) (x)
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun #include "crc32table.h"
49*4882a593Smuzhiyun #ifndef __UBOOT__
50*4882a593Smuzhiyun MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
51*4882a593Smuzhiyun MODULE_DESCRIPTION("Ethernet CRC32 calculations");
52*4882a593Smuzhiyun MODULE_LICENSE("GPL");
53*4882a593Smuzhiyun #endif
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun  * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
56*4882a593Smuzhiyun  * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for
57*4882a593Smuzhiyun  *	other uses, or the previous crc32 value if computing incrementally.
58*4882a593Smuzhiyun  * @p: pointer to buffer over which CRC is run
59*4882a593Smuzhiyun  * @len: length of buffer @p
60*4882a593Smuzhiyun  */
61*4882a593Smuzhiyun u32  crc32_le(u32 crc, unsigned char const *p, size_t len);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #if CRC_LE_BITS == 1
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun  * In fact, the table-based code will work in this case, but it can be
66*4882a593Smuzhiyun  * simplified by inlining the table in ?: form.
67*4882a593Smuzhiyun  */
68*4882a593Smuzhiyun 
crc32_le(u32 crc,unsigned char const * p,size_t len)69*4882a593Smuzhiyun u32 crc32_le(u32 crc, unsigned char const *p, size_t len)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	int i;
72*4882a593Smuzhiyun 	while (len--) {
73*4882a593Smuzhiyun 		crc ^= *p++;
74*4882a593Smuzhiyun 		for (i = 0; i < 8; i++)
75*4882a593Smuzhiyun 			crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 	return crc;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun #else				/* Table-based approach */
80*4882a593Smuzhiyun 
crc32_le(u32 crc,unsigned char const * p,size_t len)81*4882a593Smuzhiyun u32 crc32_le(u32 crc, unsigned char const *p, size_t len)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun # if CRC_LE_BITS == 8
84*4882a593Smuzhiyun 	const u32      *b =(u32 *)p;
85*4882a593Smuzhiyun 	const u32      *tab = crc32table_le;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun # ifdef __LITTLE_ENDIAN
88*4882a593Smuzhiyun #  define DO_CRC(x) crc = tab[ (crc ^ (x)) & 255 ] ^ (crc>>8)
89*4882a593Smuzhiyun # else
90*4882a593Smuzhiyun #  define DO_CRC(x) crc = tab[ ((crc >> 24) ^ (x)) & 255] ^ (crc<<8)
91*4882a593Smuzhiyun # endif
92*4882a593Smuzhiyun 	/* printf("Crc32_le crc=%x\n",crc); */
93*4882a593Smuzhiyun 	crc = __cpu_to_le32(crc);
94*4882a593Smuzhiyun 	/* Align it */
95*4882a593Smuzhiyun 	if((((long)b)&3 && len)){
96*4882a593Smuzhiyun 		do {
97*4882a593Smuzhiyun 			u8 *p = (u8 *)b;
98*4882a593Smuzhiyun 			DO_CRC(*p++);
99*4882a593Smuzhiyun 			b = (void *)p;
100*4882a593Smuzhiyun 		} while ((--len) && ((long)b)&3 );
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 	if((len >= 4)){
103*4882a593Smuzhiyun 		/* load data 32 bits wide, xor data 32 bits wide. */
104*4882a593Smuzhiyun 		size_t save_len = len & 3;
105*4882a593Smuzhiyun 		len = len >> 2;
106*4882a593Smuzhiyun 		--b; /* use pre increment below(*++b) for speed */
107*4882a593Smuzhiyun 		do {
108*4882a593Smuzhiyun 			crc ^= *++b;
109*4882a593Smuzhiyun 			DO_CRC(0);
110*4882a593Smuzhiyun 			DO_CRC(0);
111*4882a593Smuzhiyun 			DO_CRC(0);
112*4882a593Smuzhiyun 			DO_CRC(0);
113*4882a593Smuzhiyun 		} while (--len);
114*4882a593Smuzhiyun 		b++; /* point to next byte(s) */
115*4882a593Smuzhiyun 		len = save_len;
116*4882a593Smuzhiyun 	}
117*4882a593Smuzhiyun 	/* And the last few bytes */
118*4882a593Smuzhiyun 	if(len){
119*4882a593Smuzhiyun 		do {
120*4882a593Smuzhiyun 			u8 *p = (u8 *)b;
121*4882a593Smuzhiyun 			DO_CRC(*p++);
122*4882a593Smuzhiyun 			b = (void *)p;
123*4882a593Smuzhiyun 		} while (--len);
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	return __le32_to_cpu(crc);
127*4882a593Smuzhiyun #undef ENDIAN_SHIFT
128*4882a593Smuzhiyun #undef DO_CRC
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun # elif CRC_LE_BITS == 4
131*4882a593Smuzhiyun 	while (len--) {
132*4882a593Smuzhiyun 		crc ^= *p++;
133*4882a593Smuzhiyun 		crc = (crc >> 4) ^ crc32table_le[crc & 15];
134*4882a593Smuzhiyun 		crc = (crc >> 4) ^ crc32table_le[crc & 15];
135*4882a593Smuzhiyun 	}
136*4882a593Smuzhiyun 	return crc;
137*4882a593Smuzhiyun # elif CRC_LE_BITS == 2
138*4882a593Smuzhiyun 	while (len--) {
139*4882a593Smuzhiyun 		crc ^= *p++;
140*4882a593Smuzhiyun 		crc = (crc >> 2) ^ crc32table_le[crc & 3];
141*4882a593Smuzhiyun 		crc = (crc >> 2) ^ crc32table_le[crc & 3];
142*4882a593Smuzhiyun 		crc = (crc >> 2) ^ crc32table_le[crc & 3];
143*4882a593Smuzhiyun 		crc = (crc >> 2) ^ crc32table_le[crc & 3];
144*4882a593Smuzhiyun 	}
145*4882a593Smuzhiyun 	return crc;
146*4882a593Smuzhiyun # endif
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun #endif
149*4882a593Smuzhiyun #ifndef __UBOOT__
150*4882a593Smuzhiyun /**
151*4882a593Smuzhiyun  * crc32_be() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
152*4882a593Smuzhiyun  * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for
153*4882a593Smuzhiyun  *	other uses, or the previous crc32 value if computing incrementally.
154*4882a593Smuzhiyun  * @p: pointer to buffer over which CRC is run
155*4882a593Smuzhiyun  * @len: length of buffer @p
156*4882a593Smuzhiyun  */
157*4882a593Smuzhiyun u32 __attribute_pure__ crc32_be(u32 crc, unsigned char const *p, size_t len);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun #if CRC_BE_BITS == 1
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun  * In fact, the table-based code will work in this case, but it can be
162*4882a593Smuzhiyun  * simplified by inlining the table in ?: form.
163*4882a593Smuzhiyun  */
164*4882a593Smuzhiyun 
crc32_be(u32 crc,unsigned char const * p,size_t len)165*4882a593Smuzhiyun u32 __attribute_pure__ crc32_be(u32 crc, unsigned char const *p, size_t len)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	int i;
168*4882a593Smuzhiyun 	while (len--) {
169*4882a593Smuzhiyun 		crc ^= *p++ << 24;
170*4882a593Smuzhiyun 		for (i = 0; i < 8; i++)
171*4882a593Smuzhiyun 			crc =
172*4882a593Smuzhiyun 			    (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE :
173*4882a593Smuzhiyun 					  0);
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun 	return crc;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun #else				/* Table-based approach */
crc32_be(u32 crc,unsigned char const * p,size_t len)179*4882a593Smuzhiyun u32 __attribute_pure__ crc32_be(u32 crc, unsigned char const *p, size_t len)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun # if CRC_BE_BITS == 8
182*4882a593Smuzhiyun 	const u32      *b =(u32 *)p;
183*4882a593Smuzhiyun 	const u32      *tab = crc32table_be;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun # ifdef __LITTLE_ENDIAN
186*4882a593Smuzhiyun #  define DO_CRC(x) crc = tab[ (crc ^ (x)) & 255 ] ^ (crc>>8)
187*4882a593Smuzhiyun # else
188*4882a593Smuzhiyun #  define DO_CRC(x) crc = tab[ ((crc >> 24) ^ (x)) & 255] ^ (crc<<8)
189*4882a593Smuzhiyun # endif
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	crc = __cpu_to_be32(crc);
192*4882a593Smuzhiyun 	/* Align it */
193*4882a593Smuzhiyun 	if(unlikely(((long)b)&3 && len)){
194*4882a593Smuzhiyun 		do {
195*4882a593Smuzhiyun 			u8 *p = (u8 *)b;
196*4882a593Smuzhiyun 			DO_CRC(*p++);
197*4882a593Smuzhiyun 			b = (u32 *)p;
198*4882a593Smuzhiyun 		} while ((--len) && ((long)b)&3 );
199*4882a593Smuzhiyun 	}
200*4882a593Smuzhiyun 	if(likely(len >= 4)){
201*4882a593Smuzhiyun 		/* load data 32 bits wide, xor data 32 bits wide. */
202*4882a593Smuzhiyun 		size_t save_len = len & 3;
203*4882a593Smuzhiyun 		len = len >> 2;
204*4882a593Smuzhiyun 		--b; /* use pre increment below(*++b) for speed */
205*4882a593Smuzhiyun 		do {
206*4882a593Smuzhiyun 			crc ^= *++b;
207*4882a593Smuzhiyun 			DO_CRC(0);
208*4882a593Smuzhiyun 			DO_CRC(0);
209*4882a593Smuzhiyun 			DO_CRC(0);
210*4882a593Smuzhiyun 			DO_CRC(0);
211*4882a593Smuzhiyun 		} while (--len);
212*4882a593Smuzhiyun 		b++; /* point to next byte(s) */
213*4882a593Smuzhiyun 		len = save_len;
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 	/* And the last few bytes */
216*4882a593Smuzhiyun 	if(len){
217*4882a593Smuzhiyun 		do {
218*4882a593Smuzhiyun 			u8 *p = (u8 *)b;
219*4882a593Smuzhiyun 			DO_CRC(*p++);
220*4882a593Smuzhiyun 			b = (void *)p;
221*4882a593Smuzhiyun 		} while (--len);
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 	return __be32_to_cpu(crc);
224*4882a593Smuzhiyun #undef ENDIAN_SHIFT
225*4882a593Smuzhiyun #undef DO_CRC
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun # elif CRC_BE_BITS == 4
228*4882a593Smuzhiyun 	while (len--) {
229*4882a593Smuzhiyun 		crc ^= *p++ << 24;
230*4882a593Smuzhiyun 		crc = (crc << 4) ^ crc32table_be[crc >> 28];
231*4882a593Smuzhiyun 		crc = (crc << 4) ^ crc32table_be[crc >> 28];
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun 	return crc;
234*4882a593Smuzhiyun # elif CRC_BE_BITS == 2
235*4882a593Smuzhiyun 	while (len--) {
236*4882a593Smuzhiyun 		crc ^= *p++ << 24;
237*4882a593Smuzhiyun 		crc = (crc << 2) ^ crc32table_be[crc >> 30];
238*4882a593Smuzhiyun 		crc = (crc << 2) ^ crc32table_be[crc >> 30];
239*4882a593Smuzhiyun 		crc = (crc << 2) ^ crc32table_be[crc >> 30];
240*4882a593Smuzhiyun 		crc = (crc << 2) ^ crc32table_be[crc >> 30];
241*4882a593Smuzhiyun 	}
242*4882a593Smuzhiyun 	return crc;
243*4882a593Smuzhiyun # endif
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun #endif
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun EXPORT_SYMBOL(crc32_le);
248*4882a593Smuzhiyun EXPORT_SYMBOL(crc32_be);
249*4882a593Smuzhiyun #endif
250*4882a593Smuzhiyun /*
251*4882a593Smuzhiyun  * A brief CRC tutorial.
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * A CRC is a long-division remainder.  You add the CRC to the message,
254*4882a593Smuzhiyun  * and the whole thing (message+CRC) is a multiple of the given
255*4882a593Smuzhiyun  * CRC polynomial.  To check the CRC, you can either check that the
256*4882a593Smuzhiyun  * CRC matches the recomputed value, *or* you can check that the
257*4882a593Smuzhiyun  * remainder computed on the message+CRC is 0.  This latter approach
258*4882a593Smuzhiyun  * is used by a lot of hardware implementations, and is why so many
259*4882a593Smuzhiyun  * protocols put the end-of-frame flag after the CRC.
260*4882a593Smuzhiyun  *
261*4882a593Smuzhiyun  * It's actually the same long division you learned in school, except that
262*4882a593Smuzhiyun  * - We're working in binary, so the digits are only 0 and 1, and
263*4882a593Smuzhiyun  * - When dividing polynomials, there are no carries.  Rather than add and
264*4882a593Smuzhiyun  *   subtract, we just xor.  Thus, we tend to get a bit sloppy about
265*4882a593Smuzhiyun  *   the difference between adding and subtracting.
266*4882a593Smuzhiyun  *
267*4882a593Smuzhiyun  * A 32-bit CRC polynomial is actually 33 bits long.  But since it's
268*4882a593Smuzhiyun  * 33 bits long, bit 32 is always going to be set, so usually the CRC
269*4882a593Smuzhiyun  * is written in hex with the most significant bit omitted.  (If you're
270*4882a593Smuzhiyun  * familiar with the IEEE 754 floating-point format, it's the same idea.)
271*4882a593Smuzhiyun  *
272*4882a593Smuzhiyun  * Note that a CRC is computed over a string of *bits*, so you have
273*4882a593Smuzhiyun  * to decide on the endianness of the bits within each byte.  To get
274*4882a593Smuzhiyun  * the best error-detecting properties, this should correspond to the
275*4882a593Smuzhiyun  * order they're actually sent.  For example, standard RS-232 serial is
276*4882a593Smuzhiyun  * little-endian; the most significant bit (sometimes used for parity)
277*4882a593Smuzhiyun  * is sent last.  And when appending a CRC word to a message, you should
278*4882a593Smuzhiyun  * do it in the right order, matching the endianness.
279*4882a593Smuzhiyun  *
280*4882a593Smuzhiyun  * Just like with ordinary division, the remainder is always smaller than
281*4882a593Smuzhiyun  * the divisor (the CRC polynomial) you're dividing by.  Each step of the
282*4882a593Smuzhiyun  * division, you take one more digit (bit) of the dividend and append it
283*4882a593Smuzhiyun  * to the current remainder.  Then you figure out the appropriate multiple
284*4882a593Smuzhiyun  * of the divisor to subtract to being the remainder back into range.
285*4882a593Smuzhiyun  * In binary, it's easy - it has to be either 0 or 1, and to make the
286*4882a593Smuzhiyun  * XOR cancel, it's just a copy of bit 32 of the remainder.
287*4882a593Smuzhiyun  *
288*4882a593Smuzhiyun  * When computing a CRC, we don't care about the quotient, so we can
289*4882a593Smuzhiyun  * throw the quotient bit away, but subtract the appropriate multiple of
290*4882a593Smuzhiyun  * the polynomial from the remainder and we're back to where we started,
291*4882a593Smuzhiyun  * ready to process the next bit.
292*4882a593Smuzhiyun  *
293*4882a593Smuzhiyun  * A big-endian CRC written this way would be coded like:
294*4882a593Smuzhiyun  * for (i = 0; i < input_bits; i++) {
295*4882a593Smuzhiyun  * 	multiple = remainder & 0x80000000 ? CRCPOLY : 0;
296*4882a593Smuzhiyun  * 	remainder = (remainder << 1 | next_input_bit()) ^ multiple;
297*4882a593Smuzhiyun  * }
298*4882a593Smuzhiyun  * Notice how, to get at bit 32 of the shifted remainder, we look
299*4882a593Smuzhiyun  * at bit 31 of the remainder *before* shifting it.
300*4882a593Smuzhiyun  *
301*4882a593Smuzhiyun  * But also notice how the next_input_bit() bits we're shifting into
302*4882a593Smuzhiyun  * the remainder don't actually affect any decision-making until
303*4882a593Smuzhiyun  * 32 bits later.  Thus, the first 32 cycles of this are pretty boring.
304*4882a593Smuzhiyun  * Also, to add the CRC to a message, we need a 32-bit-long hole for it at
305*4882a593Smuzhiyun  * the end, so we have to add 32 extra cycles shifting in zeros at the
306*4882a593Smuzhiyun  * end of every message,
307*4882a593Smuzhiyun  *
308*4882a593Smuzhiyun  * So the standard trick is to rearrage merging in the next_input_bit()
309*4882a593Smuzhiyun  * until the moment it's needed.  Then the first 32 cycles can be precomputed,
310*4882a593Smuzhiyun  * and merging in the final 32 zero bits to make room for the CRC can be
311*4882a593Smuzhiyun  * skipped entirely.
312*4882a593Smuzhiyun  * This changes the code to:
313*4882a593Smuzhiyun  * for (i = 0; i < input_bits; i++) {
314*4882a593Smuzhiyun  *      remainder ^= next_input_bit() << 31;
315*4882a593Smuzhiyun  * 	multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
316*4882a593Smuzhiyun  * 	remainder = (remainder << 1) ^ multiple;
317*4882a593Smuzhiyun  * }
318*4882a593Smuzhiyun  * With this optimization, the little-endian code is simpler:
319*4882a593Smuzhiyun  * for (i = 0; i < input_bits; i++) {
320*4882a593Smuzhiyun  *      remainder ^= next_input_bit();
321*4882a593Smuzhiyun  * 	multiple = (remainder & 1) ? CRCPOLY : 0;
322*4882a593Smuzhiyun  * 	remainder = (remainder >> 1) ^ multiple;
323*4882a593Smuzhiyun  * }
324*4882a593Smuzhiyun  *
325*4882a593Smuzhiyun  * Note that the other details of endianness have been hidden in CRCPOLY
326*4882a593Smuzhiyun  * (which must be bit-reversed) and next_input_bit().
327*4882a593Smuzhiyun  *
328*4882a593Smuzhiyun  * However, as long as next_input_bit is returning the bits in a sensible
329*4882a593Smuzhiyun  * order, we can actually do the merging 8 or more bits at a time rather
330*4882a593Smuzhiyun  * than one bit at a time:
331*4882a593Smuzhiyun  * for (i = 0; i < input_bytes; i++) {
332*4882a593Smuzhiyun  * 	remainder ^= next_input_byte() << 24;
333*4882a593Smuzhiyun  * 	for (j = 0; j < 8; j++) {
334*4882a593Smuzhiyun  * 		multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
335*4882a593Smuzhiyun  * 		remainder = (remainder << 1) ^ multiple;
336*4882a593Smuzhiyun  * 	}
337*4882a593Smuzhiyun  * }
338*4882a593Smuzhiyun  * Or in little-endian:
339*4882a593Smuzhiyun  * for (i = 0; i < input_bytes; i++) {
340*4882a593Smuzhiyun  * 	remainder ^= next_input_byte();
341*4882a593Smuzhiyun  * 	for (j = 0; j < 8; j++) {
342*4882a593Smuzhiyun  * 		multiple = (remainder & 1) ? CRCPOLY : 0;
343*4882a593Smuzhiyun  * 		remainder = (remainder << 1) ^ multiple;
344*4882a593Smuzhiyun  * 	}
345*4882a593Smuzhiyun  * }
346*4882a593Smuzhiyun  * If the input is a multiple of 32 bits, you can even XOR in a 32-bit
347*4882a593Smuzhiyun  * word at a time and increase the inner loop count to 32.
348*4882a593Smuzhiyun  *
349*4882a593Smuzhiyun  * You can also mix and match the two loop styles, for example doing the
350*4882a593Smuzhiyun  * bulk of a message byte-at-a-time and adding bit-at-a-time processing
351*4882a593Smuzhiyun  * for any fractional bytes at the end.
352*4882a593Smuzhiyun  *
353*4882a593Smuzhiyun  * The only remaining optimization is to the byte-at-a-time table method.
354*4882a593Smuzhiyun  * Here, rather than just shifting one bit of the remainder to decide
355*4882a593Smuzhiyun  * in the correct multiple to subtract, we can shift a byte at a time.
356*4882a593Smuzhiyun  * This produces a 40-bit (rather than a 33-bit) intermediate remainder,
357*4882a593Smuzhiyun  * but again the multiple of the polynomial to subtract depends only on
358*4882a593Smuzhiyun  * the high bits, the high 8 bits in this case.
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  * The multile we need in that case is the low 32 bits of a 40-bit
361*4882a593Smuzhiyun  * value whose high 8 bits are given, and which is a multiple of the
362*4882a593Smuzhiyun  * generator polynomial.  This is simply the CRC-32 of the given
363*4882a593Smuzhiyun  * one-byte message.
364*4882a593Smuzhiyun  *
365*4882a593Smuzhiyun  * Two more details: normally, appending zero bits to a message which
366*4882a593Smuzhiyun  * is already a multiple of a polynomial produces a larger multiple of that
367*4882a593Smuzhiyun  * polynomial.  To enable a CRC to detect this condition, it's common to
368*4882a593Smuzhiyun  * invert the CRC before appending it.  This makes the remainder of the
369*4882a593Smuzhiyun  * message+crc come out not as zero, but some fixed non-zero value.
370*4882a593Smuzhiyun  *
371*4882a593Smuzhiyun  * The same problem applies to zero bits prepended to the message, and
372*4882a593Smuzhiyun  * a similar solution is used.  Instead of starting with a remainder of
373*4882a593Smuzhiyun  * 0, an initial remainder of all ones is used.  As long as you start
374*4882a593Smuzhiyun  * the same way on decoding, it doesn't make a difference.
375*4882a593Smuzhiyun  */
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun #ifdef UNITTEST
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun #include <stdlib.h>
380*4882a593Smuzhiyun #include <stdio.h>
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun #ifndef __UBOOT__
383*4882a593Smuzhiyun static void
buf_dump(char const * prefix,unsigned char const * buf,size_t len)384*4882a593Smuzhiyun buf_dump(char const *prefix, unsigned char const *buf, size_t len)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	fputs(prefix, stdout);
387*4882a593Smuzhiyun 	while (len--)
388*4882a593Smuzhiyun 		printf(" %02x", *buf++);
389*4882a593Smuzhiyun 	putchar('\n');
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun #endif
393*4882a593Smuzhiyun 
bytereverse(unsigned char * buf,size_t len)394*4882a593Smuzhiyun static void bytereverse(unsigned char *buf, size_t len)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun 	while (len--) {
397*4882a593Smuzhiyun 		unsigned char x = bitrev8(*buf);
398*4882a593Smuzhiyun 		*buf++ = x;
399*4882a593Smuzhiyun 	}
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun 
random_garbage(unsigned char * buf,size_t len)402*4882a593Smuzhiyun static void random_garbage(unsigned char *buf, size_t len)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun 	while (len--)
405*4882a593Smuzhiyun 		*buf++ = (unsigned char) random();
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun #ifndef __UBOOT__
store_le(u32 x,unsigned char * buf)409*4882a593Smuzhiyun static void store_le(u32 x, unsigned char *buf)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	buf[0] = (unsigned char) x;
412*4882a593Smuzhiyun 	buf[1] = (unsigned char) (x >> 8);
413*4882a593Smuzhiyun 	buf[2] = (unsigned char) (x >> 16);
414*4882a593Smuzhiyun 	buf[3] = (unsigned char) (x >> 24);
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun #endif
417*4882a593Smuzhiyun 
store_be(u32 x,unsigned char * buf)418*4882a593Smuzhiyun static void store_be(u32 x, unsigned char *buf)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun 	buf[0] = (unsigned char) (x >> 24);
421*4882a593Smuzhiyun 	buf[1] = (unsigned char) (x >> 16);
422*4882a593Smuzhiyun 	buf[2] = (unsigned char) (x >> 8);
423*4882a593Smuzhiyun 	buf[3] = (unsigned char) x;
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun /*
427*4882a593Smuzhiyun  * This checks that CRC(buf + CRC(buf)) = 0, and that
428*4882a593Smuzhiyun  * CRC commutes with bit-reversal.  This has the side effect
429*4882a593Smuzhiyun  * of bytewise bit-reversing the input buffer, and returns
430*4882a593Smuzhiyun  * the CRC of the reversed buffer.
431*4882a593Smuzhiyun  */
test_step(u32 init,unsigned char * buf,size_t len)432*4882a593Smuzhiyun static u32 test_step(u32 init, unsigned char *buf, size_t len)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun 	u32 crc1, crc2;
435*4882a593Smuzhiyun 	size_t i;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	crc1 = crc32_be(init, buf, len);
438*4882a593Smuzhiyun 	store_be(crc1, buf + len);
439*4882a593Smuzhiyun 	crc2 = crc32_be(init, buf, len + 4);
440*4882a593Smuzhiyun 	if (crc2)
441*4882a593Smuzhiyun 		printf("\nCRC cancellation fail: 0x%08x should be 0\n",
442*4882a593Smuzhiyun 		       crc2);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	for (i = 0; i <= len + 4; i++) {
445*4882a593Smuzhiyun 		crc2 = crc32_be(init, buf, i);
446*4882a593Smuzhiyun 		crc2 = crc32_be(crc2, buf + i, len + 4 - i);
447*4882a593Smuzhiyun 		if (crc2)
448*4882a593Smuzhiyun 			printf("\nCRC split fail: 0x%08x\n", crc2);
449*4882a593Smuzhiyun 	}
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	/* Now swap it around for the other test */
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	bytereverse(buf, len + 4);
454*4882a593Smuzhiyun 	init = bitrev32(init);
455*4882a593Smuzhiyun 	crc2 = bitrev32(crc1);
456*4882a593Smuzhiyun 	if (crc1 != bitrev32(crc2))
457*4882a593Smuzhiyun 		printf("\nBit reversal fail: 0x%08x -> 0x%08x -> 0x%08x\n",
458*4882a593Smuzhiyun 		       crc1, crc2, bitrev32(crc2));
459*4882a593Smuzhiyun 	crc1 = crc32_le(init, buf, len);
460*4882a593Smuzhiyun 	if (crc1 != crc2)
461*4882a593Smuzhiyun 		printf("\nCRC endianness fail: 0x%08x != 0x%08x\n", crc1,
462*4882a593Smuzhiyun 		       crc2);
463*4882a593Smuzhiyun 	crc2 = crc32_le(init, buf, len + 4);
464*4882a593Smuzhiyun 	if (crc2)
465*4882a593Smuzhiyun 		printf("\nCRC cancellation fail: 0x%08x should be 0\n",
466*4882a593Smuzhiyun 		       crc2);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	for (i = 0; i <= len + 4; i++) {
469*4882a593Smuzhiyun 		crc2 = crc32_le(init, buf, i);
470*4882a593Smuzhiyun 		crc2 = crc32_le(crc2, buf + i, len + 4 - i);
471*4882a593Smuzhiyun 		if (crc2)
472*4882a593Smuzhiyun 			printf("\nCRC split fail: 0x%08x\n", crc2);
473*4882a593Smuzhiyun 	}
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	return crc1;
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun #define SIZE 64
479*4882a593Smuzhiyun #define INIT1 0
480*4882a593Smuzhiyun #define INIT2 0
481*4882a593Smuzhiyun 
main(void)482*4882a593Smuzhiyun int main(void)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun 	unsigned char buf1[SIZE + 4];
485*4882a593Smuzhiyun 	unsigned char buf2[SIZE + 4];
486*4882a593Smuzhiyun 	unsigned char buf3[SIZE + 4];
487*4882a593Smuzhiyun 	int i, j;
488*4882a593Smuzhiyun 	u32 crc1, crc2, crc3;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	for (i = 0; i <= SIZE; i++) {
491*4882a593Smuzhiyun 		printf("\rTesting length %d...", i);
492*4882a593Smuzhiyun 		fflush(stdout);
493*4882a593Smuzhiyun 		random_garbage(buf1, i);
494*4882a593Smuzhiyun 		random_garbage(buf2, i);
495*4882a593Smuzhiyun 		for (j = 0; j < i; j++)
496*4882a593Smuzhiyun 			buf3[j] = buf1[j] ^ buf2[j];
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 		crc1 = test_step(INIT1, buf1, i);
499*4882a593Smuzhiyun 		crc2 = test_step(INIT2, buf2, i);
500*4882a593Smuzhiyun 		/* Now check that CRC(buf1 ^ buf2) = CRC(buf1) ^ CRC(buf2) */
501*4882a593Smuzhiyun 		crc3 = test_step(INIT1 ^ INIT2, buf3, i);
502*4882a593Smuzhiyun 		if (crc3 != (crc1 ^ crc2))
503*4882a593Smuzhiyun 			printf("CRC XOR fail: 0x%08x != 0x%08x ^ 0x%08x\n",
504*4882a593Smuzhiyun 			       crc3, crc1, crc2);
505*4882a593Smuzhiyun 	}
506*4882a593Smuzhiyun 	printf("\nAll test complete.  No failures expected.\n");
507*4882a593Smuzhiyun 	return 0;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun #endif				/* UNITTEST */
511