2
2
#include "scsiglue.h"
3
3
#include "transport.h"
4
/* #include "stdlib.h" */
5
/* #include "EUCR6SK.h" */
6
6
#include "smcommon.h"
14
//#include "EMCRIOS.h"
9
/* #include <stdio.h> */
10
/* #include <stdlib.h> */
11
/* #include <string.h> */
12
/* #include <dos.h> */
13
/* #include "EMCRIOS.h" */
15
/* CP0-CP5 code table */
17
16
static BYTE ecctable[256] = {
18
0x00,0x55,0x56,0x03,0x59,0x0C,0x0F,0x5A,0x5A,0x0F,0x0C,0x59,0x03,0x56,0x55,0x00,
19
0x65,0x30,0x33,0x66,0x3C,0x69,0x6A,0x3F,0x3F,0x6A,0x69,0x3C,0x66,0x33,0x30,0x65,
20
0x66,0x33,0x30,0x65,0x3F,0x6A,0x69,0x3C,0x3C,0x69,0x6A,0x3F,0x65,0x30,0x33,0x66,
21
0x03,0x56,0x55,0x00,0x5A,0x0F,0x0C,0x59,0x59,0x0C,0x0F,0x5A,0x00,0x55,0x56,0x03,
22
0x69,0x3C,0x3F,0x6A,0x30,0x65,0x66,0x33,0x33,0x66,0x65,0x30,0x6A,0x3F,0x3C,0x69,
23
0x0C,0x59,0x5A,0x0F,0x55,0x00,0x03,0x56,0x56,0x03,0x00,0x55,0x0F,0x5A,0x59,0x0C,
24
0x0F,0x5A,0x59,0x0C,0x56,0x03,0x00,0x55,0x55,0x00,0x03,0x56,0x0C,0x59,0x5A,0x0F,
25
0x6A,0x3F,0x3C,0x69,0x33,0x66,0x65,0x30,0x30,0x65,0x66,0x33,0x69,0x3C,0x3F,0x6A,
26
0x6A,0x3F,0x3C,0x69,0x33,0x66,0x65,0x30,0x30,0x65,0x66,0x33,0x69,0x3C,0x3F,0x6A,
27
0x0F,0x5A,0x59,0x0C,0x56,0x03,0x00,0x55,0x55,0x00,0x03,0x56,0x0C,0x59,0x5A,0x0F,
28
0x0C,0x59,0x5A,0x0F,0x55,0x00,0x03,0x56,0x56,0x03,0x00,0x55,0x0F,0x5A,0x59,0x0C,
29
0x69,0x3C,0x3F,0x6A,0x30,0x65,0x66,0x33,0x33,0x66,0x65,0x30,0x6A,0x3F,0x3C,0x69,
30
0x03,0x56,0x55,0x00,0x5A,0x0F,0x0C,0x59,0x59,0x0C,0x0F,0x5A,0x00,0x55,0x56,0x03,
31
0x66,0x33,0x30,0x65,0x3F,0x6A,0x69,0x3C,0x3C,0x69,0x6A,0x3F,0x65,0x30,0x33,0x66,
32
0x65,0x30,0x33,0x66,0x3C,0x69,0x6A,0x3F,0x3F,0x6A,0x69,0x3C,0x66,0x33,0x30,0x65,
33
0x00,0x55,0x56,0x03,0x59,0x0C,0x0F,0x5A,0x5A,0x0F,0x0C,0x59,0x03,0x56,0x55,0x00
17
0x00, 0x55, 0x56, 0x03, 0x59, 0x0C, 0x0F, 0x5A, 0x5A, 0x0F, 0x0C, 0x59, 0x03,
18
0x56, 0x55, 0x00, 0x65, 0x30, 0x33, 0x66, 0x3C, 0x69, 0x6A, 0x3F, 0x3F, 0x6A,
19
0x69, 0x3C, 0x66, 0x33, 0x30, 0x65, 0x66, 0x33, 0x30, 0x65, 0x3F, 0x6A, 0x69,
20
0x3C, 0x3C, 0x69, 0x6A, 0x3F, 0x65, 0x30, 0x33, 0x66, 0x03, 0x56, 0x55, 0x00,
21
0x5A, 0x0F, 0x0C, 0x59, 0x59, 0x0C, 0x0F, 0x5A, 0x00, 0x55, 0x56, 0x03, 0x69,
22
0x3C, 0x3F, 0x6A, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6A, 0x3F,
23
0x3C, 0x69, 0x0C, 0x59, 0x5A, 0x0F, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00,
24
0x55, 0x0F, 0x5A, 0x59, 0x0C, 0x0F, 0x5A, 0x59, 0x0C, 0x56, 0x03, 0x00, 0x55,
25
0x55, 0x00, 0x03, 0x56, 0x0C, 0x59, 0x5A, 0x0F, 0x6A, 0x3F, 0x3C, 0x69, 0x33,
26
0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3C, 0x3F, 0x6A, 0x6A, 0x3F,
27
0x3C, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3C, 0x3F,
28
0x6A, 0x0F, 0x5A, 0x59, 0x0C, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56,
29
0x0C, 0x59, 0x5A, 0x0F, 0x0C, 0x59, 0x5A, 0x0F, 0x55, 0x00, 0x03, 0x56, 0x56,
30
0x03, 0x00, 0x55, 0x0F, 0x5A, 0x59, 0x0C, 0x69, 0x3C, 0x3F, 0x6A, 0x30, 0x65,
31
0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6A, 0x3F, 0x3C, 0x69, 0x03, 0x56, 0x55,
32
0x00, 0x5A, 0x0F, 0x0C, 0x59, 0x59, 0x0C, 0x0F, 0x5A, 0x00, 0x55, 0x56, 0x03,
33
0x66, 0x33, 0x30, 0x65, 0x3F, 0x6A, 0x69, 0x3C, 0x3C, 0x69, 0x6A, 0x3F, 0x65,
34
0x30, 0x33, 0x66, 0x65, 0x30, 0x33, 0x66, 0x3C, 0x69, 0x6A, 0x3F, 0x3F, 0x6A,
35
0x69, 0x3C, 0x66, 0x33, 0x30, 0x65, 0x00, 0x55, 0x56, 0x03, 0x59, 0x0C, 0x0F,
36
0x5A, 0x5A, 0x0F, 0x0C, 0x59, 0x03, 0x56, 0x55, 0x00
36
static void trans_result (BYTE, BYTE, BYTE *, BYTE *);
39
static void trans_result(BYTE, BYTE, BYTE *, BYTE *);
48
51
#define MASK_CPS 0x3f
49
52
#define CORRECTABLE 0x00555554L
51
static void trans_result(reg2,reg3,ecc1,ecc2)
52
BYTE reg2; // LP14,LP12,LP10,...
53
BYTE reg3; // LP15,LP13,LP11,...
54
BYTE *ecc1; // LP15,LP14,LP13,...
55
BYTE *ecc2; // LP07,LP06,LP05,...
57
BYTE a; // Working for reg2,reg3
58
BYTE b; // Working for ecc1,ecc2
59
BYTE i; // For counting
61
a=BIT7; b=BIT7; // 80h=10000000b
62
*ecc1=*ecc2=0; // Clear ecc1,ecc2
65
*ecc1|=b; // LP15,13,11,9 -> ecc1
66
b=b>>1; // Right shift
68
*ecc1|=b; // LP14,12,10,8 -> ecc1
69
b=b>>1; // Right shift
70
a=a>>1; // Right shift
73
b=BIT7; // 80h=10000000b
76
*ecc2|=b; // LP7,5,3,1 -> ecc2
77
b=b>>1; // Right shift
79
*ecc2|=b; // LP6,4,2,0 -> ecc2
80
b=b>>1; // Right shift
81
a=a>>1; // Right shift
85
//static void calculate_ecc(table,data,ecc1,ecc2,ecc3)
86
void calculate_ecc(table,data,ecc1,ecc2,ecc3)
87
BYTE *table; // CP0-CP5 code table
89
BYTE *ecc1; // LP15,LP14,LP13,...
90
BYTE *ecc2; // LP07,LP06,LP05,...
91
BYTE *ecc3; // CP5,CP4,CP3,...,"1","1"
93
DWORD i; // For counting
94
BYTE a; // Working for table
95
BYTE reg1; // D-all,CP5,CP4,CP3,...
96
BYTE reg2; // LP14,LP12,L10,...
97
BYTE reg3; // LP15,LP13,L11,...
99
reg1=reg2=reg3=0; // Clear parameter
100
for(i=0; i<256; ++i) {
101
a=table[data[i]]; // Get CP0-CP5 code from table
102
reg1^=(a&MASK_CPS); // XOR with a
104
{ // If D_all(all bit XOR) = 1
105
reg3^=(BYTE)i; // XOR with counter
106
reg2^=~((BYTE)i); // XOR with inv. of counter
110
// Trans LP14,12,10,... & LP15,13,11,... -> LP15,14,13,... & LP7,6,5,..
111
trans_result(reg2,reg3,ecc1,ecc2);
112
*ecc1=~(*ecc1); *ecc2=~(*ecc2); // Inv. ecc2 & ecc3
113
*ecc3=((~reg1)<<2)|BIT1BIT0; // Make TEL format
116
BYTE correct_data(data,eccdata,ecc1,ecc2,ecc3)
118
BYTE *eccdata; // ECC DATA
119
BYTE ecc1; // LP15,LP14,LP13,...
120
BYTE ecc2; // LP07,LP06,LP05,...
121
BYTE ecc3; // CP5,CP4,CP3,...,"1","1"
123
DWORD l; // Working to check d
124
DWORD d; // Result of comparison
125
DWORD i; // For counting
126
BYTE d1,d2,d3; // Result of comparison
127
BYTE a; // Working for add
128
BYTE add; // Byte address of cor. DATA
129
BYTE b; // Working for bit
130
BYTE bit; // Bit address of cor. DATA
132
d1=ecc1^eccdata[1]; d2=ecc2^eccdata[0]; // Compare LP's
133
d3=ecc3^eccdata[2]; // Comapre CP's
134
d=((DWORD)d1<<16) // Result of comparison
138
if (d==0) return(0); // If No error, return
140
if (((d^(d>>1))&CORRECTABLE)==CORRECTABLE)
143
add=0; // Clear parameter
146
for(i=0; i<8; ++i) { // Checking 8 bit
147
if ((d&l)!=0) add|=a; // Make byte address from LP's
148
l>>=2; a>>=1; // Right Shift
151
bit=0; // Clear parameter
153
for(i=0; i<3; ++i) { // Checking 3 bit
154
if ((d&l)!=0) bit|=b; // Make bit address from CP's
155
l>>=2; b>>=1; // Right shift
159
data[add]^=(b<<bit); // Put corrected data
164
d&=0x00ffffffL; // Masking
166
while(d) { // If d=0 finish counting
167
if (d&BIT0) ++i; // Count number of 1 bit
168
d>>=1; // Right shift
173
eccdata[1]=ecc1; eccdata[0]=ecc2; // Put right ECC code
177
return(3); // Uncorrectable error
180
int _Correct_D_SwECC(buf,redundant_ecc,calculate_ecc)
55
* reg2; * LP14,LP12,LP10,...
56
* reg3; * LP15,LP13,LP11,...
57
* *ecc1; * LP15,LP14,LP13,...
58
* *ecc2; * LP07,LP06,LP05,...
60
static void trans_result(BYTE reg2, BYTE reg3, BYTE *ecc1, BYTE *ecc2)
62
BYTE a; /* Working for reg2,reg3 */
63
BYTE b; /* Working for ecc1,ecc2 */
64
BYTE i; /* For counting */
66
a = BIT7; b = BIT7; /* 80h=10000000b */
67
*ecc1 = *ecc2 = 0; /* Clear ecc1,ecc2 */
68
for (i = 0; i < 4; ++i) {
70
*ecc1 |= b; /* LP15,13,11,9 -> ecc1 */
71
b = b>>1; /* Right shift */
73
*ecc1 |= b; /* LP14,12,10,8 -> ecc1 */
74
b = b>>1; /* Right shift */
75
a = a>>1; /* Right shift */
78
b = BIT7; /* 80h=10000000b */
79
for (i = 0; i < 4; ++i) {
81
*ecc2 |= b; /* LP7,5,3,1 -> ecc2 */
82
b = b>>1; /* Right shift */
84
*ecc2 |= b; /* LP6,4,2,0 -> ecc2 */
85
b = b>>1; /* Right shift */
86
a = a>>1; /* Right shift */
90
/*static void calculate_ecc(table,data,ecc1,ecc2,ecc3) */
92
* *table; * CP0-CP5 code table
94
* *ecc1; * LP15,LP14,LP13,...
95
* *ecc2; * LP07,LP06,LP05,...
96
* *ecc3; * CP5,CP4,CP3,...,"1","1"
98
void calculate_ecc(BYTE *table, BYTE *data, BYTE *ecc1, BYTE *ecc2, BYTE *ecc3)
100
DWORD i; /* For counting */
101
BYTE a; /* Working for table */
102
BYTE reg1; /* D-all,CP5,CP4,CP3,... */
103
BYTE reg2; /* LP14,LP12,L10,... */
104
BYTE reg3; /* LP15,LP13,L11,... */
106
reg1 = reg2 = reg3 = 0; /* Clear parameter */
107
for (i = 0; i < 256; ++i) {
108
a = table[data[i]]; /* Get CP0-CP5 code from table */
109
reg1 ^= (a&MASK_CPS); /* XOR with a */
110
if ((a&BIT6) != 0) { /* If D_all(all bit XOR) = 1 */
111
reg3 ^= (BYTE)i; /* XOR with counter */
112
reg2 ^= ~((BYTE)i); /* XOR with inv. of counter */
116
/* Trans LP14,12,10,... & LP15,13,11,... ->
117
LP15,14,13,... & LP7,6,5,.. */
118
trans_result(reg2, reg3, ecc1, ecc2);
119
*ecc1 = ~(*ecc1); *ecc2 = ~(*ecc2); /* Inv. ecc2 & ecc3 */
120
*ecc3 = ((~reg1)<<2)|BIT1BIT0; /* Make TEL format */
125
* *eccdata; * ECC DATA
126
* ecc1; * LP15,LP14,LP13,...
127
* ecc2; * LP07,LP06,LP05,...
128
* ecc3; * CP5,CP4,CP3,...,"1","1"
130
BYTE correct_data(BYTE *data, BYTE *eccdata, BYTE ecc1, BYTE ecc2, BYTE ecc3)
132
DWORD l; /* Working to check d */
133
DWORD d; /* Result of comparison */
134
DWORD i; /* For counting */
135
BYTE d1, d2, d3; /* Result of comparison */
136
BYTE a; /* Working for add */
137
BYTE add; /* Byte address of cor. DATA */
138
BYTE b; /* Working for bit */
139
BYTE bit; /* Bit address of cor. DATA */
141
d1 = ecc1^eccdata[1]; d2 = ecc2^eccdata[0]; /* Compare LP's */
142
d3 = ecc3^eccdata[2]; /* Comapre CP's */
143
d = ((DWORD)d1<<16) /* Result of comparison */
148
return 0; /* If No error, return */
150
if (((d^(d>>1))&CORRECTABLE) == CORRECTABLE) { /* If correctable */
152
add = 0; /* Clear parameter */
155
for (i = 0; i < 8; ++i) { /* Checking 8 bit */
157
add |= a; /* Make byte address from LP's */
158
l >>= 2; a >>= 1; /* Right Shift */
161
bit = 0; /* Clear parameter */
163
for (i = 0; i < 3; ++i) { /* Checking 3 bit */
165
bit |= b; /* Make bit address from CP's */
166
l >>= 2; b >>= 1; /* Right shift */
170
data[add] ^= (b<<bit); /* Put corrected data */
174
i = 0; /* Clear count */
175
d &= 0x00ffffffL; /* Masking */
177
while (d) { /* If d=0 finish counting */
179
++i; /* Count number of 1 bit */
180
d >>= 1; /* Right shift */
183
if (i == 1) { /* If ECC error */
184
eccdata[1] = ecc1; eccdata[0] = ecc2; /* Put right ECC code */
188
return 3; /* Uncorrectable error */
191
int _Correct_D_SwECC(BYTE *buf, BYTE *redundant_ecc, BYTE *calculate_ecc)