pbl_crc32.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #include <u-boot/crc.h>
  9. static uint32_t crc_table[256];
  10. static int crc_table_valid;
  11. static void make_crc_table(void)
  12. {
  13. uint32_t mask;
  14. int i, j;
  15. uint32_t poly; /* polynomial exclusive-or pattern */
  16. if (crc_table_valid)
  17. return;
  18. /*
  19. * the polynomial used by PBL is 1 + x1 + x2 + x4 + x5 + x7 + x8 + x10
  20. * + x11 + x12 + x16 + x22 + x23 + x26 + x32.
  21. */
  22. poly = 0x04c11db7;
  23. for (i = 0; i < 256; i++) {
  24. mask = i << 24;
  25. for (j = 0; j < 8; j++) {
  26. if (mask & 0x80000000)
  27. mask = (mask << 1) ^ poly;
  28. else
  29. mask <<= 1;
  30. }
  31. crc_table[i] = mask;
  32. }
  33. crc_table_valid = 1;
  34. }
  35. uint32_t pbl_crc32(uint32_t in_crc, const char *buf, uint32_t len)
  36. {
  37. uint32_t crc32_val;
  38. int i;
  39. make_crc_table();
  40. crc32_val = ~in_crc;
  41. for (i = 0; i < len; i++)
  42. crc32_val = (crc32_val << 8) ^
  43. crc_table[(crc32_val >> 24) ^ (*buf++ & 0xff)];
  44. return crc32_val;
  45. }