From 4e39c824e07f192c35afa88dcceee863528dbd32 Mon Sep 17 00:00:00 2001 From: Xiang Wang Date: Tue, 5 Nov 2019 12:00:39 +0800 Subject: lib: add calculate crc byte by byte Change-Id: I5cab1f90452b08a464ad7a2d7e75d97187452992 Signed-off-by: Xiang Wang Reviewed-on: https://review.coreboot.org/c/coreboot/+/36624 Tested-by: build bot (Jenkins) Reviewed-by: Julius Werner Reviewed-by: Patrick Georgi --- src/include/crc_byte.h | 40 ++++++++++++++++++++++++++++++++++++++++ src/lib/Makefile.inc | 8 ++++++++ src/lib/crc_byte.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/include/crc_byte.h create mode 100644 src/lib/crc_byte.c (limited to 'src') diff --git a/src/include/crc_byte.h b/src/include/crc_byte.h new file mode 100644 index 0000000000..9315277d23 --- /dev/null +++ b/src/include/crc_byte.h @@ -0,0 +1,40 @@ +/* + * This file is part of the coreboot project. + * + * Copyright (C) 2019 HardenedLinux + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef CRC_BYTE_H +#define CRC_BYTE_H + +#include + +/* This function is used to calculate crc7 byte by byte, with polynomial + * x^7 + x^3 + 1. + * + * prev_crc: old crc result (0 for first) + * data: new byte + * return value: new crc result + */ +uint8_t crc7_byte(uint8_t prev_crc, uint8_t data); + +/* This function is used to calculate crc16 byte by byte, with polynomial + * x^16 + x^12 + x^5 + 1. + * + * prev_crc: old crc result (0 for first) + * data: new byte + * return value: new crc result + */ +uint16_t crc16_byte(uint16_t prev_crc, uint8_t data); + + +#endif /* CRC_BYTE_H */ diff --git a/src/lib/Makefile.inc b/src/lib/Makefile.inc index da7b4bbdbc..3b7d57c76a 100644 --- a/src/lib/Makefile.inc +++ b/src/lib/Makefile.inc @@ -249,6 +249,14 @@ postcar-y += string.c ramstage-y += string.c smm-y += string.c +decompressor-y += crc_byte.c +bootblock-y += crc_byte.c +verstage-y += crc_byte.c +romstage-y += crc_byte.c +postcar-y += crc_byte.c +ramstage-y += crc_byte.c +smm-y += crc_byte.c + postcar-y += bootmode.c postcar-y += boot_device.c postcar-y += cbfs.c diff --git a/src/lib/crc_byte.c b/src/lib/crc_byte.c new file mode 100644 index 0000000000..0ac006363a --- /dev/null +++ b/src/lib/crc_byte.c @@ -0,0 +1,38 @@ +/* + * This file is part of the coreboot project. + * + * Copyright (C) 2019 HardenedLinux + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include + +uint8_t crc7_byte(uint8_t prev_crc, uint8_t data) +{ + const uint8_t g = 0x89; + prev_crc ^= data; + for (int i = 0; i < 8; i++) { + if (prev_crc & 0x80) + prev_crc ^= g; + prev_crc <<= 1; + } + return prev_crc; +} + +uint16_t crc16_byte(uint16_t prev_crc, uint8_t data) +{ + prev_crc = (uint8_t)(prev_crc >> 8)|(prev_crc << 8); + prev_crc ^= data; + prev_crc ^= (uint8_t)(prev_crc & 0xff) >> 4; + prev_crc ^= (prev_crc << 8) << 4; + prev_crc ^= ((prev_crc & 0xff) << 4) << 1; + return prev_crc; +} -- cgit v1.2.3