xref: /rk3399_ARM-atf/common/tf_crc32.c (revision fb0c409889d0066a90247075f1abeb0b02c1c6fe)
1 /*
2  * Copyright (c) 2021-2025, 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>
12 #include <common/tf_crc32.h>
13 
14 /* compute CRC using Arm intrinsic function
15  *
16  * This function is useful for platforms with FEAT_CRC32 (mandatory from v8.1)
17  * Platforms with CPU ARMv8.0 should make sure to add a make switch
18  * `ARM_ARCH_FEATURE := crc` for successful compilation of this file.
19  *
20  * @crc: previous accumulated CRC
21  * @buf: buffer base address
22  * @size: the size of the buffer
23  *
24  * Return calculated CRC value
25  */
26 uint32_t tf_crc32(uint32_t crc, const unsigned char *buf, size_t size)
27 {
28 	assert(buf != NULL);
29 
30 	uint32_t calc_crc = ~crc;
31 	const unsigned char *local_buf = buf;
32 	size_t local_size = size;
33 
34 	/*
35 	 * calculate CRC over byte data
36 	 */
37 	while (local_size != 0UL) {
38 		calc_crc = __crc32b(calc_crc, *local_buf);
39 		local_buf++;
40 		local_size--;
41 	}
42 
43 	return ~calc_crc;
44 }
45