diff options
Diffstat (limited to 'tools/telechips.c')
-rw-r--r-- | tools/telechips.c | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/tools/telechips.c b/tools/telechips.c index 8eefffcb98..acb15b8b14 100644 --- a/tools/telechips.c +++ b/tools/telechips.c | |||
@@ -31,6 +31,7 @@ | |||
31 | #include <fcntl.h> | 31 | #include <fcntl.h> |
32 | #include <stdint.h> | 32 | #include <stdint.h> |
33 | #include <stdlib.h> | 33 | #include <stdlib.h> |
34 | #include <string.h> | ||
34 | 35 | ||
35 | static uint32_t crctable[256]; | 36 | static uint32_t crctable[256]; |
36 | 37 | ||
@@ -57,11 +58,11 @@ static void gentable(uint32_t poly) | |||
57 | { | 58 | { |
58 | int i; | 59 | int i; |
59 | uint32_t r; | 60 | uint32_t r; |
60 | uint32_t index; | 61 | uint32_t idx; |
61 | 62 | ||
62 | for (index = 0; index < 256; index++) | 63 | for (idx = 0; idx < 256; idx++) |
63 | { | 64 | { |
64 | r = bitreverse(index,8) << 24; | 65 | r = bitreverse(idx,8) << 24; |
65 | for (i=0; i<8; i++) | 66 | for (i=0; i<8; i++) |
66 | { | 67 | { |
67 | if (r & (1 << 31)) | 68 | if (r & (1 << 31)) |
@@ -69,7 +70,7 @@ static void gentable(uint32_t poly) | |||
69 | else | 70 | else |
70 | r<<=1; | 71 | r<<=1; |
71 | } | 72 | } |
72 | crctable[index] = bitreverse(r,32); | 73 | crctable[idx] = bitreverse(r,32); |
73 | } | 74 | } |
74 | } | 75 | } |
75 | 76 | ||
@@ -156,3 +157,26 @@ void telechips_encode_crc(unsigned char* buf, int length) | |||
156 | put_uint32le(buf+0x10, crc1); | 157 | put_uint32le(buf+0x10, crc1); |
157 | } | 158 | } |
158 | } | 159 | } |
160 | |||
161 | int telechips_test_crc(unsigned char* buf, int length) | ||
162 | { | ||
163 | uint32_t crc1, crc2, test_crc1, test_crc2; | ||
164 | unsigned char *test_buf; | ||
165 | |||
166 | crc1 = get_uint32le(buf + 0x10); | ||
167 | crc2 = get_uint32le(buf + 0x18); | ||
168 | |||
169 | test_buf = malloc(length); | ||
170 | if (!test_buf) | ||
171 | return 1; | ||
172 | |||
173 | memcpy(test_buf, buf, length); | ||
174 | telechips_encode_crc(test_buf, length); | ||
175 | |||
176 | test_crc1 = get_uint32le(test_buf + 0x10); | ||
177 | test_crc2 = get_uint32le(test_buf + 0x18); | ||
178 | |||
179 | free(test_buf); | ||
180 | |||
181 | return (crc1 == test_crc1 && crc2 == test_crc2) ? 0 : 2; | ||
182 | } | ||