crc.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2009
  4. * Marvell Semiconductor <www.marvell.com>
  5. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  6. */
  7. #ifndef _UBOOT_CRC_H
  8. #define _UBOOT_CRC_H
  9. #include <compiler.h> /* 'uint*' definitions */
  10. /**
  11. * crc8() - Calculate and return CRC-8 of the data
  12. *
  13. * This uses an x^8 + x^2 + x + 1 polynomial. A table-based algorithm would
  14. * be faster, but for only a few bytes it isn't worth the code size
  15. *
  16. * lib/crc8.c
  17. *
  18. * @crc_start: CRC8 start value
  19. * @vptr: Buffer to checksum
  20. * @len: Length of buffer in bytes
  21. * @return CRC8 checksum
  22. */
  23. unsigned int crc8(unsigned int crc_start, const unsigned char *vptr, int len);
  24. /* lib/crc16.c - 16 bit CRC with polynomial x^16+x^12+x^5+1 (CRC-CCITT) */
  25. uint16_t crc16_ccitt(uint16_t crc_start, const unsigned char *s, int len);
  26. /**
  27. * crc16_ccitt_wd_buf - Perform CRC16-CCIT on an input buffer and return the
  28. * 16-bit result (network byte-order) in an output buffer
  29. *
  30. * @in: input buffer
  31. * @len: input buffer length
  32. * @out: output buffer (at least 2 bytes)
  33. * @chunk_sz: ignored
  34. */
  35. void crc16_ccitt_wd_buf(const uint8_t *in, uint len,
  36. uint8_t *out, uint chunk_sz);
  37. /* lib/crc32.c */
  38. /**
  39. * crc32 - Calculate the CRC32 for a block of data
  40. *
  41. * @crc: Input crc to chain from a previous calculution (use 0 to start a new
  42. * calculation)
  43. * @buf: Bytes to checksum
  44. * @len: Number of bytes to checksum
  45. * @return checksum value
  46. */
  47. uint32_t crc32(uint32_t crc, const unsigned char *buf, uint len);
  48. /**
  49. * crc32_wd - Calculate the CRC32 for a block of data (watchdog version)
  50. *
  51. * This checksums the data @chunk_sz bytes at a time, calling WATCHDOG_RESET()
  52. * after each chunk, to prevent the watchdog from firing.
  53. *
  54. * @crc: Input crc to chain from a previous calculution (use 0 to start a new
  55. * calculation)
  56. * @buf: Bytes to checksum
  57. * @len: Number of bytes to checksum
  58. * @chunk_sz: Chunk size to use between watchdog resets
  59. * @return checksum
  60. */
  61. uint32_t crc32_wd(uint32_t crc, const unsigned char *buf, uint len,
  62. uint chunk_sz);
  63. /**
  64. * crc32_no_comp - Calculate the CRC32 for a block of data (no one's compliment)
  65. *
  66. * This version uses a different algorithm which doesn't use one's compliment.
  67. * JFFS2 (and other things?) use this.
  68. *
  69. * @crc: Input crc to chain from a previous calculution (use 0 to start a new
  70. * calculation)
  71. * @buf: Bytes to checksum
  72. * @len: Number of bytes to checksum
  73. * @return checksum value
  74. */
  75. uint32_t crc32_no_comp(uint32_t crc, const unsigned char *buf, uint len);
  76. /**
  77. * crc32_wd_buf - Perform CRC32 on a buffer and return result in buffer
  78. *
  79. * @input: Input buffer
  80. * @ilen: Input buffer length
  81. * @output: Place to put checksum result (4 bytes)
  82. * @chunk_sz: Trigger watchdog after processing this many bytes
  83. */
  84. void crc32_wd_buf(const uint8_t *input, uint ilen, uint8_t *output,
  85. uint chunk_sz);
  86. /* lib/crc32c.c */
  87. /**
  88. * crc32c_init() - Set up a the CRC32 table
  89. *
  90. * This sets up 256-item table to aid in CRC32 calculation
  91. *
  92. * @crc32c_table: Place to put table
  93. * @pol: polynomial to use
  94. */
  95. void crc32c_init(uint32_t *crc32c_table, uint32_t pol);
  96. /**
  97. * crc32c_cal() - Perform CRC32 on a buffer given a table
  98. *
  99. * This algorithm uses the table (set up by crc32c_init() to speed up
  100. * processing.
  101. *
  102. * @crc: Previous crc (use 0 at start)
  103. * @data: Data bytes to checksum
  104. * @length: Number of bytes to process
  105. * @crc32c_table:: CRC table
  106. * @return checksum value
  107. */
  108. uint32_t crc32c_cal(uint32_t crc, const char *data, int length,
  109. uint32_t *crc32c_table);
  110. #endif /* _UBOOT_CRC_H */