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