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