blob: b33d36ea2ee228da20e7cf95e8f6f47dbe2d35bc [file] [log] [blame]
Manish V Badarkhe7a867922021-04-22 14:41:27 +01001/*
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 Badarkhea26bf352021-07-02 20:29:56 +010012#include <common/tf_crc32.h>
Manish V Badarkhe7a867922021-04-22 14:41:27 +010013
Manish V Badarkhea26bf352021-07-02 20:29:56 +010014/* compute CRC using Arm intrinsic function
Manish V Badarkhe7a867922021-04-22 14:41:27 +010015 *
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 Badarkhea26bf352021-07-02 20:29:56 +010027uint32_t tf_crc32(uint32_t crc, const unsigned char *buf, size_t size)
Manish V Badarkhe7a867922021-04-22 14:41:27 +010028{
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}