tf_crc32.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2021, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdarg.h>
  7. #include <assert.h>
  8. #include <arm_acle.h>
  9. #include <common/debug.h>
  10. #include <common/tf_crc32.h>
  11. /* compute CRC using Arm intrinsic function
  12. *
  13. * This function is useful for the platforms with the CPU ARMv8.0
  14. * (with CRC instructions supported), and onwards.
  15. * Platforms with CPU ARMv8.0 should make sure to add a compile switch
  16. * '-march=armv8-a+crc" for successful compilation of this file.
  17. *
  18. * @crc: previous accumulated CRC
  19. * @buf: buffer base address
  20. * @size: the size of the buffer
  21. *
  22. * Return calculated CRC value
  23. */
  24. uint32_t tf_crc32(uint32_t crc, const unsigned char *buf, size_t size)
  25. {
  26. assert(buf != NULL);
  27. uint32_t calc_crc = ~crc;
  28. const unsigned char *local_buf = buf;
  29. size_t local_size = size;
  30. /*
  31. * calculate CRC over byte data
  32. */
  33. while (local_size != 0UL) {
  34. calc_crc = __crc32b(calc_crc, *local_buf);
  35. local_buf++;
  36. local_size--;
  37. }
  38. return ~calc_crc;
  39. }