Commit 60d18d3fe9d1a5db663711063cd0d5c915af377a

Authored by Simon Glass
1 parent e7be18225f

Add crc8 routine

Add an implementation of the CRC8 algorithm. This is required by the TPM
emulation, but is probably useful to U-Boot in general.

Signed-off-by: Simon Glass <sjg@chromium.org>

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

Showing 3 changed files with 49 additions and 0 deletions Side-by-side Diff

include/linux/crc8.h
  1 +/*
  2 + * Copyright (c) 2013 Google, Inc
  3 + *
  4 + * SPDX-License-Identifier: GPL-2.0+
  5 + */
  6 +
  7 +
  8 +#ifndef __linux_crc8_h
  9 +#define __linux_crc8_h
  10 +
  11 +/**
  12 + * crc8() - Calculate and return CRC-8 of the data
  13 + *
  14 + * This uses an x^8 + x^2 + x + 1 polynomial. A table-based algorithm would
  15 + * be faster, but for only a few bytes it isn't worth the code size
  16 + *
  17 + * @vptr: Buffer to checksum
  18 + * @len: Length of buffer in bytes
  19 + * @return CRC8 checksum
  20 + */
  21 +unsigned int crc8(const unsigned char *vptr, int len);
  22 +
  23 +#endif
... ... @@ -21,6 +21,7 @@
21 21 obj-$(CONFIG_BZIP2) += bzlib_huffman.o
22 22 obj-$(CONFIG_USB_TTY) += circbuf.o
23 23 obj-y += crc7.o
  24 +obj-y += crc8.o
24 25 obj-y += crc16.o
25 26 obj-$(CONFIG_OF_CONTROL) += fdtdec.o
26 27 obj-$(CONFIG_TEST_FDTDEC) += fdtdec_test.o
  1 +/*
  2 + * Copyright (c) 2013 Google, Inc
  3 + *
  4 + * SPDX-License-Identifier: GPL-2.0+
  5 + */
  6 +
  7 +#include "linux/crc8.h"
  8 +
  9 +unsigned int crc8(const unsigned char *vptr, int len)
  10 +{
  11 + const unsigned char *data = vptr;
  12 + unsigned int crc = 0;
  13 + int i, j;
  14 +
  15 + for (j = len; j; j--, data++) {
  16 + crc ^= (*data << 8);
  17 + for (i = 8; i; i--) {
  18 + if (crc & 0x8000)
  19 + crc ^= (0x1070 << 3);
  20 + crc <<= 1;
  21 + }
  22 + }
  23 +
  24 + return (crc >> 8) & 0xff;
  25 +}