1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/*
*
* $Id: ecc.c,v 1.2 2002/09/09 13:29:52 germeier Exp $
*
* Library for USB MPIO-*
*
* Markus Germeier (mager@tzi.de)
*
*
* 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; either version 2 of the
* License, or (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* */
#include "ecc.h"
#include "debug.h"
BYTE get_bit(BYTE, int);
/* TODO: fix correctable errors */
inline BYTE
get_bit(BYTE d, int offset)
{
return ((d >> offset) & 0x01 );
}
int
mpio_ecc_256_gen(BYTE *data, BYTE *ecc)
{
BYTE p1, p1_;
BYTE p2, p2_;
BYTE p4, p4_;
BYTE p08, p08_;
BYTE p16, p16_;
BYTE p32, p32_;
BYTE p64, p64_;
BYTE p0128, p0128_;
BYTE p0256, p0256_;
BYTE p0512, p0512_;
BYTE p1024, p1024_;
int i, j;
/* init */
p1=p1_=0;
p2=p2_=0;
p4=p4_=0;
p08=p08_=0;
p16=p16_=0;
p32=p32_=0;
p64=p64_=0;
p0128=p0128_=0;
p0256=p0256_=0;
p0512=p0512_=0;
p1024=p1024_=0;
/* vertical */
for (i=0; i<256; i++) {
/* p1, p1_ */
p1 ^= (get_bit(data[i], 7) ^
get_bit(data[i], 5) ^
get_bit(data[i], 3) ^
get_bit(data[i], 1));
p1_^= (get_bit(data[i], 6) ^
get_bit(data[i], 4) ^
get_bit(data[i], 2) ^
get_bit(data[i], 0));
/* p2, p2_ */
p2 ^= (get_bit(data[i], 7) ^
get_bit(data[i], 6) ^
get_bit(data[i], 3) ^
get_bit(data[i], 2));
p2_^= (get_bit(data[i], 5) ^
get_bit(data[i], 4) ^
get_bit(data[i], 1) ^
get_bit(data[i], 0));
/* p4, p4_ */
p4 ^= (get_bit(data[i], 7) ^
get_bit(data[i], 6) ^
get_bit(data[i], 5) ^
get_bit(data[i], 4));
p4_^= (get_bit(data[i], 3) ^
get_bit(data[i], 2) ^
get_bit(data[i], 1) ^
get_bit(data[i], 0));
}
/* horizontal */
for (i=0; i<8; i++) {
for (j=0; j<256; j++) {
/* p08, p08_ */
if ((j & 0x01) == 0)
p08_^= get_bit(data[j], i);
if ((j & 0x01) == 1)
p08 ^= get_bit(data[j], i);
/* p16, p16_ */
if (((j/2) & 0x01) == 0)
p16_^= get_bit(data[j], i);
if (((j/2) & 0x01) == 1)
p16^= get_bit(data[j], i);
/* p32, p32_ */
if (((j/4) & 0x01) == 0)
p32_^= get_bit(data[j], i);
if (((j/4) & 0x01) == 1)
p32^= get_bit(data[j], i);
/* p64, p64_ */
if (((j/8) & 0x01) == 0)
p64_^= get_bit(data[j], i);
if (((j/8) & 0x01) == 1)
p64^= get_bit(data[j], i);
/* p0128, p0128_ */
if (((j/16) & 0x01) == 0)
p0128_^= get_bit(data[j], i);
if (((j/16) & 0x01) == 1)
p0128 ^= get_bit(data[j], i);
/* p0256, p0256_ */
if (((j/32) & 0x01) == 0)
p0256_^= get_bit(data[j], i);
if (((j/32) & 0x01) == 1)
p0256^= get_bit(data[j], i);
/* p0512, p0512_ */
if (((j/64) & 0x01) == 0)
p0512_^= get_bit(data[j], i);
if (((j/64) & 0x01) == 1)
p0512^= get_bit(data[j], i);
/* p1024, p1024_ */
if (((j/128) & 0x01) == 0)
p1024_^= get_bit(data[j], i);
if (((j/128) & 0x01) == 1)
p1024^= get_bit(data[j], i);
}
}
ecc[0]=~((p64 << 7) | (p64_ << 6) |
(p32 << 5) | (p32_ << 4) |
(p16 << 3) | (p16_ << 2) |
(p08 << 1) | (p08_ << 0));
ecc[1]=~((p1024 << 7) | (p1024_ << 6) |
(p0512 << 5) | (p0512_ << 4) |
(p0256 << 3) | (p0256_ << 2) |
(p0128 << 1) | (p0128_ << 0));
ecc[2]=~((p4 << 7) | (p4_ << 6) |
(p2 << 5) | (p2_ << 4) |
(p1 << 3) | (p1_ << 2));
return 0;
}
int
mpio_ecc_256_check(BYTE *data, BYTE *ecc)
{
BYTE own_ecc[3];
mpio_ecc_256_gen(data, own_ecc);
if ((own_ecc[0]^ecc[0])|
(own_ecc[1]^ecc[1])|
(own_ecc[2]^ecc[2])) {
debug("ECC %2x %2x %2x vs. %2x %2x %2x\n",
ecc[0], ecc[1], ecc[2], own_ecc[0], own_ecc[1], own_ecc[2]);
debugn(2, "ECC Error detected\n");
debugn(2, "ECC Correction code not in place yet, Sorry\n");
debugn(3, "got ECC: %2x %2x %2x\n", ecc[0], ecc[1], ecc[2]);
debugn(3, "calc. ECC: %2x %2x %2x\n", own_ecc[0], own_ecc[1], own_ecc[2]);
return 1;
}
return 0;
}
|