]> git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/crc8.c
bbb229c3892c23f99df9b9873c922409410fa87a
[thirdparty/u-boot.git] / lib / crc8.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2013 Google, Inc
4 */
5
6 #ifdef USE_HOSTCC
7 #include <arpa/inet.h>
8 #endif
9 #include <asm/sections.h>
10 #include <u-boot/crc.h>
11
12 #define POLY (0x1070U << 3)
13
14 __rcode static unsigned char _crc8(unsigned short data)
15 {
16 int i;
17
18 for (i = 0; i < 8; i++) {
19 if (data & 0x8000)
20 data = data ^ POLY;
21 data = data << 1;
22 }
23
24 return (unsigned char)(data >> 8);
25 }
26
27 __rcode unsigned int crc8(unsigned int crc, const unsigned char *vptr, int len)
28 {
29 int i;
30
31 for (i = 0; i < len; i++)
32 crc = _crc8((crc ^ vptr[i]) << 8);
33
34 return crc;
35 }
36
37 void crc8_wd_buf(const unsigned char *input, unsigned int len,
38 unsigned char output[1], unsigned int chunk_sz)
39 {
40 *output = crc8(0, input, len);
41 }