Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 60d18d3 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
Simon Glass | 60d18d3 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | c3a4d1c | 2019-11-14 12:57:14 -0700 | [diff] [blame^] | 6 | #ifdef USE_HOSTCC |
| 7 | #include <arpa/inet.h> |
| 8 | #else |
| 9 | #include <common.h> |
| 10 | #endif |
| 11 | #include <u-boot/crc.h> |
Simon Glass | 60d18d3 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 12 | |
Stefan Roese | 456ecd0 | 2016-04-08 15:56:29 +0200 | [diff] [blame] | 13 | #define POLY (0x1070U << 3) |
Simon Glass | 60d18d3 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 14 | |
Stefan Roese | 456ecd0 | 2016-04-08 15:56:29 +0200 | [diff] [blame] | 15 | static unsigned char _crc8(unsigned short data) |
| 16 | { |
| 17 | int i; |
| 18 | |
| 19 | for (i = 0; i < 8; i++) { |
| 20 | if (data & 0x8000) |
| 21 | data = data ^ POLY; |
| 22 | data = data << 1; |
Simon Glass | 60d18d3 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Stefan Roese | 456ecd0 | 2016-04-08 15:56:29 +0200 | [diff] [blame] | 25 | return (unsigned char)(data >> 8); |
| 26 | } |
| 27 | |
| 28 | unsigned int crc8(unsigned int crc, const unsigned char *vptr, int len) |
| 29 | { |
| 30 | int i; |
| 31 | |
| 32 | for (i = 0; i < len; i++) |
| 33 | crc = _crc8((crc ^ vptr[i]) << 8); |
| 34 | |
| 35 | return crc; |
Simon Glass | 60d18d3 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 36 | } |