blob: 06da1d92ffdd69a1406e989c78ae1501e774ff07 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Charles Manningabbc67e2014-05-14 14:45:00 +12002/*
3 * Copyright 2012 Freescale Semiconductor, Inc.
4 *
5 * Cleaned up and refactored by Charles Manning.
Charles Manningabbc67e2014-05-14 14:45:00 +12006 */
7#include "pblimage.h"
8
9static uint32_t crc_table[256];
10static int crc_table_valid;
11
12static void make_crc_table(void)
13{
14 uint32_t mask;
15 int i, j;
16 uint32_t poly; /* polynomial exclusive-or pattern */
17
18 if (crc_table_valid)
19 return;
20
21 /*
22 * the polynomial used by PBL is 1 + x1 + x2 + x4 + x5 + x7 + x8 + x10
23 * + x11 + x12 + x16 + x22 + x23 + x26 + x32.
24 */
25 poly = 0x04c11db7;
26
27 for (i = 0; i < 256; i++) {
28 mask = i << 24;
29 for (j = 0; j < 8; j++) {
30 if (mask & 0x80000000)
31 mask = (mask << 1) ^ poly;
32 else
33 mask <<= 1;
34 }
35 crc_table[i] = mask;
36 }
37
38 crc_table_valid = 1;
39}
40
41uint32_t pbl_crc32(uint32_t in_crc, const char *buf, uint32_t len)
42{
43 uint32_t crc32_val;
44 int i;
45
46 make_crc_table();
47
48 crc32_val = ~in_crc;
49
50 for (i = 0; i < len; i++)
51 crc32_val = (crc32_val << 8) ^
52 crc_table[(crc32_val >> 24) ^ (*buf++ & 0xff)];
53
54 return crc32_val;
55}