Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Xiang Wang | 4e39c82 | 2019-11-05 12:00:39 +0800 | [diff] [blame] | 3 | |
| 4 | #include <crc_byte.h> |
| 5 | |
| 6 | uint8_t crc7_byte(uint8_t prev_crc, uint8_t data) |
| 7 | { |
| 8 | const uint8_t g = 0x89; |
| 9 | prev_crc ^= data; |
| 10 | for (int i = 0; i < 8; i++) { |
| 11 | if (prev_crc & 0x80) |
| 12 | prev_crc ^= g; |
| 13 | prev_crc <<= 1; |
| 14 | } |
| 15 | return prev_crc; |
| 16 | } |
| 17 | |
| 18 | uint16_t crc16_byte(uint16_t prev_crc, uint8_t data) |
| 19 | { |
| 20 | prev_crc = (uint8_t)(prev_crc >> 8)|(prev_crc << 8); |
| 21 | prev_crc ^= data; |
| 22 | prev_crc ^= (uint8_t)(prev_crc & 0xff) >> 4; |
| 23 | prev_crc ^= (prev_crc << 8) << 4; |
| 24 | prev_crc ^= ((prev_crc & 0xff) << 4) << 1; |
| 25 | return prev_crc; |
| 26 | } |
Patrick Rudolph | cc0b6f1 | 2019-12-15 18:03:36 +0100 | [diff] [blame] | 27 | |
| 28 | uint32_t crc32_byte(uint32_t prev_crc, uint8_t data) |
| 29 | { |
| 30 | prev_crc ^= (uint32_t)data << 24; |
| 31 | |
| 32 | for (int i = 0; i < 8; i++) { |
| 33 | if ((prev_crc & 0x80000000UL) != 0) |
| 34 | prev_crc = ((prev_crc << 1) ^ 0x04C11DB7UL); |
| 35 | else |
| 36 | prev_crc <<= 1; |
| 37 | } |
| 38 | |
| 39 | return prev_crc; |
| 40 | } |