crc.h 3.5 KB

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