pbl_crc32.c 1014 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2012 Freescale Semiconductor, Inc.
  4. *
  5. * Cleaned up and refactored by Charles Manning.
  6. */
  7. #include "pblimage.h"
  8. static uint32_t crc_table[256];
  9. static int crc_table_valid;
  10. static void make_crc_table(void)
  11. {
  12. uint32_t mask;
  13. int i, j;
  14. uint32_t poly; /* polynomial exclusive-or pattern */
  15. if (crc_table_valid)
  16. return;
  17. /*
  18. * the polynomial used by PBL is 1 + x1 + x2 + x4 + x5 + x7 + x8 + x10
  19. * + x11 + x12 + x16 + x22 + x23 + x26 + x32.
  20. */
  21. poly = 0x04c11db7;
  22. for (i = 0; i < 256; i++) {
  23. mask = i << 24;
  24. for (j = 0; j < 8; j++) {
  25. if (mask & 0x80000000)
  26. mask = (mask << 1) ^ poly;
  27. else
  28. mask <<= 1;
  29. }
  30. crc_table[i] = mask;
  31. }
  32. crc_table_valid = 1;
  33. }
  34. uint32_t pbl_crc32(uint32_t in_crc, const char *buf, uint32_t len)
  35. {
  36. uint32_t crc32_val;
  37. int i;
  38. make_crc_table();
  39. crc32_val = ~in_crc;
  40. for (i = 0; i < len; i++)
  41. crc32_val = (crc32_val << 8) ^
  42. crc_table[(crc32_val >> 24) ^ (*buf++ & 0xff)];
  43. return crc32_val;
  44. }