forked from sonovice/sle4442
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
341 lines (314 loc) · 8.33 KB
/
main.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// cpu: ATMega8
// speed: 8 MHz
//#define DEBUG
#define BACKDOOR
#define F_CPU 8000000UL
#include "sle4442.h"
#include "memory.h"
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
volatile unsigned char is_start = 0;
volatile unsigned char is_stop = 0;
volatile unsigned char data_out = 0;
volatile unsigned char data_in = 0;
volatile unsigned char is_falling=0;
volatile unsigned char is_rising=0;
volatile unsigned char exec_reset=0;
volatile unsigned char count=0;
volatile unsigned char control=0;
volatile unsigned int addr=0;
volatile unsigned char data=0;
volatile unsigned char cmd=0;
volatile unsigned char fail=0;
volatile unsigned char byteProt=0;
volatile unsigned char bitProt=0;
volatile unsigned char *memPtr;
void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE));
/* Set up address and data registers */
EEAR = uiAddress;EEDR = ucData;
/* Write logical one to EEMWE */
EECR |= (1<<EEMWE);
/* Start eeprom write by setting EEWE */
EECR |= (1<<EEWE);
}
void store_psc()
{
cli();
EEPROM_write(0x00,memorySecurity[1]);
EEPROM_write(0x01,memorySecurity[2]);
EEPROM_write(0x02,memorySecurity[3]);
sei();
}
static inline void falling_clock()
{
data_out = (memPtr[addr] >> pointerBit) & 0x01;
if (control==0xFF) //Fake "control" value for put at the output the ATR
{
if (addr==4 && pointerBit==1)
{
setInput();
mode = MODE_WAIT_START;
count = 0;
}
}
else if (control==0x30)
{
if (addr==256 && pointerBit==1)
{
setInput();
mode = MODE_WAIT_START;
count = 0;
}
}
else if (control==0x34)
{
if (addr==4 && pointerBit==1)
{
setInput();
mode = MODE_WAIT_START;
count = 0;
}
}
else if (control==0x31)
{
if (addr==4 && pointerBit==1)
{
setInput();
mode = MODE_WAIT_START;
count = 0;
}
}
else if (fail)
{
if (addr==0 && pointerBit==3)
{
fail = 0;
setInput();
mode = MODE_WAIT_START;
count = 0;
}
}
else if ((control==0x38 || control==0x3C) && fail==0)
{
if (addr==15 && pointerBit==5) //15*8 + 5 = 125
{
setInput();
mode = MODE_WAIT_START;
count = 0;
}
}
pointerBit++;
if (pointerBit==8)
{
pointerBit = 0;
addr++;
}
}
static inline void rising_clock()
{
cmd = (cmd << 1) | data_in;
if (count==7) //control ready
control = lut[cmd];
else if (count==15) //address ready
{
addr = lut[cmd];
//Put as output all zero, if i found a read command i change the pointer value
memPtr = memoryZero;
if (control==0x30) //Read Main Memory
memPtr = memoryMain;
else if (control==0x34) //Read Protection Memory
{
addr = 0;
memPtr = memoryProtected;
}
else if (control==0x31) //Read Security Memory
{
addr = 0;
memoryOut[0] = memorySecurity[0];
if (unlocked == 3)
memPtr = memorySecurity;
else
memPtr = memoryOut;
}
byteProt = (addr >> 3) & 0x03;
bitProt = addr & 0x07;
}
else if (count==16) //Use this extra counter value to execute some instructions
{
fail = 1;
if (control==0x38 && unlocked==3) //Update main memory
{
if (addr>31)
fail = 0;
else
fail = !((memoryProtected[byteProt] >> bitProt) & 0x01);
}
else if (control==0x3C && unlocked==3) //Write protection memory
{
if ((memoryProtected[byteProt] >> bitProt) & 0x01)
fail = 0;
addr = 0;
}
}
else if (count==23) //data ready
{
mode = MODE_WAIT_STOP;
pointerBit = 0;
data = lut[cmd];
if (control==0x38) //Update main memory
{
if (!fail)
memoryMain[addr] = data;
addr = 0;
}
else if (control==0x3C) //Write protection memory
{
if (!fail)
memoryProtected[byteProt] &= ~(1 << bitProt);
addr = 0;
}
else if (control==0x39) //Update security memory
{
if (unlocked == 3)
{
memorySecurity[addr] = data;
memorySecurity[0x00] &= 0x07;
fail = 0;
}
else if (addr == 0x00)
{
if (data <= memorySecurity[0x00])
{
memorySecurity[0x00] = data;
fail = 0;
}
else
fail = 1;
}
addr = 0;
}
else if (control==0x33) //Compare verification data
{
#ifdef BACKDOOR
if (addr != 0)
{
unlocked++;
memorySecurity[addr] = data;
if (unlocked >= 3)
{
unlocked = 3;
memorySecurity[0x00] = 0x07;
}
}
addr = 0;
#else
if ((memorySecurity[addr] == data) & (memorySecurity[0x00] != 0x00))
{
unlocked++;
if (unlocked >= 3)
{
unlocked = 3;
memorySecurity[0x00] = 0x07;
}
}
else
unlocked = 0;
addr = 0;
#endif
}
data_out = (unsigned char)(memPtr[addr]) & 0x01;
pointerBit++;
}
count++;
}
// Clock interrupt
ISR(INT0_vect)
{
//SET_DBG
data_in = (READ_IO) >> PIN_IO;
if (!(PIN & (1 << PIN_CLK)))
{
setIO(data_out);
if (is_start && !data_in)
{
pointerBit = 0;
setMode(MODE_CMD);
is_start = 0;
}
else if (is_stop && data_in)
{
setMode(MODE_OUT);
setOutput();
is_stop=0;
}
if (mode==MODE_OUT)
falling_clock();
}
else
{
if (PIN & (1 << PIN_RST))
{
setOutput();
setMode(MODE_OUT);
}
else if (data_in && mode==MODE_WAIT_START)
is_start = 1;
else if (!data_in && mode==MODE_WAIT_STOP)
is_stop = 1;
if (mode==MODE_CMD)
rising_clock();
}
//RESET_DBG
}
// Reset interrupt
ISR(INT1_vect)
{
exec_reset = 1;
}
int main(void) {
memPtr = memoryMain;
unlocked = 0;
// Activate interrupts
MCUCR |= (1 << ISC00) | (1 << ISC11) | (1 << ISC10);
GICR |= (1 << INT0) | (1 << INT1);
sei();
// Initialize port
PORT = 0x00;
DDR = 0x00;
PIN = 0x00;
DDR |= (1 << PIN_DBG);
RESET_DBG
setInput();
setMode(MODE_WAIT_START);
pointerBit = 0;
count = 0;
#ifdef DEBUG
initializeDebug();
#endif
while(1)
{
//=============================================================
// RESET ROUTINE
//=============================================================
if (exec_reset)
{
setInput();
setMode(MODE_WAIT_START);
pointerBit = 0;
is_rising = 0;
is_falling = 0;
count = 0;
addr = 0;
control = 0xFF; //Fake "control" for ATR
data_out = memoryMain[0] & 0x01;
pointerBit++;
exec_reset = 0;
}
}
}