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