tf_crc32.c 1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * Copyright (c) 2021, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdarg.h>
#include <assert.h>

#include <arm_acle.h>
#include <common/debug.h>
12
#include <common/tf_crc32.h>
13

14
/* compute CRC using Arm intrinsic function
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * This function is useful for the platforms with the CPU ARMv8.0
 * (with CRC instructions supported), and onwards.
 * Platforms with CPU ARMv8.0 should make sure to add a compile switch
 * '-march=armv8-a+crc" for successful compilation of this file.
 *
 * @crc: previous accumulated CRC
 * @buf: buffer base address
 * @size: the size of the buffer
 *
 * Return calculated CRC value
 */
27
uint32_t tf_crc32(uint32_t crc, const unsigned char *buf, size_t size)
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{
	assert(buf != NULL);

	uint32_t calc_crc = ~crc;
	const unsigned char *local_buf = buf;
	size_t local_size = size;

	/*
	 * calculate CRC over byte data
	 */
	while (local_size != 0UL) {
		calc_crc = __crc32b(calc_crc, *local_buf);
		local_buf++;
		local_size--;
	}

	return ~calc_crc;
}