What's new? | Help | Directory | Sign in
Google
             
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
/* Simple ECC verification code, originally by Segher */

#include <stdio.h>
#include <string.h>
#include "amoxiflash.h"

static u8 parity(u8 x)
{
u8 y = 0;

while (x) {
y ^= (x & 1);
x >>= 1;
}

return y;
}

static void calc_ecc(u8 *data, u8 *ecc)
{
u8 a[12][2];
int i, j;
u32 a0, a1;
u8 x;

memset(a, 0, sizeof a);
for (i = 0; i < 512; i++) {
x = data[i];
for (j = 0; j < 9; j++)
a[3+j][(i >> j) & 1] ^= x;
}

x = a[3][0] ^ a[3][1];
a[0][0] = x & 0x55;
a[0][1] = x & 0xaa;
a[1][0] = x & 0x33;
a[1][1] = x & 0xcc;
a[2][0] = x & 0x0f;
a[2][1] = x & 0xf0;

for (j = 0; j < 12; j++) {
a[j][0] = parity(a[j][0]);
a[j][1] = parity(a[j][1]);
}

a0 = a1 = 0;
for (j = 0; j < 12; j++) {
a0 |= a[j][0] << j;
a1 |= a[j][1] << j;
}

ecc[0] = a0;
ecc[1] = a0 >> 8;
ecc[2] = a1;
ecc[3] = a1 >> 8;
}

u8 * calc_page_ecc(u8 *data)
{
static u8 ecc[16];

calc_ecc(data, ecc);
calc_ecc(data + 512, ecc + 4);
calc_ecc(data + 1024, ecc + 8);
calc_ecc(data + 1536, ecc + 12);
return ecc;
}


int check_ecc(u8 *page) {
u8 *stored_ecc = page + 2048 + 48;
if (page[2048]!=0xFF) return ECC_INVALID;
if (stored_ecc[0] == 0xFF && stored_ecc[1] == 0xFF) return ECC_BLANK;

if (memcmp(stored_ecc, calc_page_ecc(page), 16)) return ECC_WRONG;
return ECC_OK;
}
Show details Hide details

Change log

r21 by bushing on May 06, 2008   Diff
changes to support mingw compilation
Go to: 
Project members, sign in to write a code review

Older revisions

r17 by bushing on May 04, 2008   Diff
more cleanup and error checking
r16 by bushing on May 04, 2008   Diff
added ECC-checking code
All revisions of this file

File info

Size: 1289 bytes, 77 lines