forked from brother-yan/LGTISP
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathswd_lgt8fx8p.cpp
396 lines (336 loc) · 6.96 KB
/
swd_lgt8fx8p.cpp
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "swd_lgt8fx8p.h"
void SWD_init()
{
// set to output
SWDIF_DIR |= (SWDIF_CLK | SWDIF_DAT);
// clear output
SWD_SET();
SWC_SET();
}
void SWD_exit()
{
/* 禁止:reset后halt CPU,并lock flash */
/* Prohibition: Halt CPU after reset and lock flash */
SWD_WriteByte(1, 0xb1, 0);
SWD_WriteByte(0, 0x0d, 1);
SWD_Idle(2);
delayus(200);
/* software reset */
SWD_WriteByte(1, 0xb1, 0);
SWD_WriteByte(0, 0x0c, 1);
SWD_Idle(2);
SWD_Idle(40);
//SWDIF_DIR &= ~(SWDIF_CLK | SWDIF_DAT);
}
void SWD_WriteByte(uint8_t start, uint8_t data, uint8_t stop)
{
volatile uint8_t cnt;
if ( start )
{
SWC_CLR();
SWD_Delay();
SWD_CLR();
SWD_Delay();
SWC_SET();
SWD_Delay();
}
// send data
for( cnt = 0; cnt < 8; cnt++ )
{
SWC_CLR();
if ( data & 0x1 )
{
SWD_SET();
}
else
{
SWD_CLR();
}
SWD_Delay();
data >>= 1;
SWC_SET();
SWD_Delay();
}
SWC_CLR();
if ( stop )
{
SWD_SET();
}
else
{
SWD_CLR();
}
SWD_Delay();
SWC_SET();
SWD_Delay();
}
uint8_t SWD_ReadByte(uint8_t start, uint8_t stop)
{
volatile uint8_t cnt;
volatile uint8_t bRes = 0;
if ( start )
{
SWC_CLR();
SWD_Delay();
SWD_CLR();
SWD_Delay();
SWC_SET();
SWD_Delay();
}
SWD_IND();
//SWD_Delay();
for( cnt = 0; cnt < 8; cnt++ )
{
bRes >>= 1;
SWC_CLR();
SWD_Delay();
if ( SWDIF_PIN & SWDIF_DAT )
{
bRes |= 0x80;
}
SWC_SET();
SWD_Delay();
}
SWD_OUD();
SWC_CLR();
if( stop )
{
SWD_SET();
}
else
{
SWD_CLR();
}
SWD_Delay();
SWC_SET();
SWD_Delay();
return bRes;
}
void SWD_Idle( uint8_t cnt )
{
volatile uint8_t i;
SWD_SET();
for( i = 0; i < cnt; i++ )
{
SWC_CLR();
SWD_Delay();
SWC_SET();
SWD_Delay();
}
}
void SWD_ReadSWDID( char *pdata )
{
SWD_WriteByte(1, 0xae, 1);
SWD_Idle(4);
pdata[0] = SWD_ReadByte(1, 0);
pdata[1] = SWD_ReadByte(0, 0);
pdata[2] = SWD_ReadByte(0, 0);
pdata[3] = SWD_ReadByte(0, 1);
SWD_Idle(4);
}
void SWD_ReadGUID( char *guid )
{
SWD_Idle(10);
SWD_WriteByte(1, 0xa8, 1);
SWD_Idle(4);
guid[0] = SWD_ReadByte(1, 0);
guid[1] = SWD_ReadByte(0, 0);
guid[2] = SWD_ReadByte(0, 0);
guid[3] = SWD_ReadByte(0, 1);
SWD_Idle(4);
}
void SWD_SWDEN()
{
SWD_WriteByte(1, 0xd0, 0);
SWD_WriteByte(0, 0xaa, 0);
SWD_WriteByte(0, 0x55, 0);
SWD_WriteByte(0, 0xaa, 0);
SWD_WriteByte(0, 0x55, 1);
SWD_Idle(4);
}
void SWD_UnLock0()
{
SWD_WriteByte(1, 0xf0, 0);
SWD_WriteByte(0, 0x54, 0);
SWD_WriteByte(0, 0x51, 0);
SWD_WriteByte(0, 0x4a, 0);
SWD_WriteByte(0, 0x4c, 1);
SWD_Idle(4);
}
void SWD_UnLock1()
{
SWD_WriteByte(1, 0xf0, 0);
SWD_WriteByte(0, 0x00, 0);
SWD_WriteByte(0, 0x00, 0);
SWD_WriteByte(0, 0x00, 0);
SWD_WriteByte(0, 0x00, 1);
SWD_Idle(4);
}
void SWD_UnLock2()
{
SWD_WriteByte(1, 0xf0, 0);
SWD_WriteByte(0, 0x43, 0);
SWD_WriteByte(0, 0x40, 0);
SWD_WriteByte(0, 0x59, 0);
SWD_WriteByte(0, 0x5d, 1);
SWD_Idle(4);
}
void SWD_EEE_CSEQ(uint8_t ctrl, uint16_t addr)
{
SWD_WriteByte(1, 0xb2, 0);
SWD_WriteByte(0, (addr & 0xff), 0);
SWD_WriteByte(0, ((ctrl & 0x3) << 6) | ((addr >> 8) & 0x3f), 0);
SWD_WriteByte(0, (0xC0 | (ctrl >> 2)), 1);
SWD_Idle(4);
}
void SWD_EEE_DSEQ(uint32_t data)
{
SWD_WriteByte(1, 0xb2, 0);
SWD_WriteByte(0, ((uint8_t *)&data)[0], 0);
SWD_WriteByte(0, ((uint8_t *)&data)[1], 0);
SWD_WriteByte(0, ((uint8_t *)&data)[2], 0);
SWD_WriteByte(0, ((uint8_t *)&data)[3], 1);
SWD_Idle(4);
}
uint8_t SWD_EEE_GetBusy()
{
uint8_t res = 0;
SWD_WriteByte(1, 0xaa, 1);
SWD_Idle(8);
SWD_ReadByte(1, 0);
SWD_ReadByte(0, 0);
res = SWD_ReadByte(0, 1);
SWD_Idle(8);
return res & 0x1;
}
void SWD_ChipErase()
{
/* reset flash to 0xff */
// 我猜是操作FLASH访问控制寄存器 - EECR
// I guess it is to operate the FLASH access control register-EECR
SWD_EEE_CSEQ(0x00, 1);
SWD_EEE_CSEQ(0x98, 1);
SWD_EEE_CSEQ(0x9a, 1);
delay(200);
SWD_EEE_CSEQ(0x8a, 1);
delay(20);
SWD_EEE_CSEQ(0x88, 1);
SWD_EEE_CSEQ(0x00, 1);
}
uint8_t crack() // 破解读保护(目前只能读出除了前1k以外的flash,前1k会被擦除) // Crack the read protection (currently only the flash except the first 1k can be read, the first 1k will be erased)
{
SWD_EEE_CSEQ(0x00, 1);
SWD_EEE_CSEQ(0x98, 1);
SWD_EEE_CSEQ(0x92, 1); // 会擦除flash的第一页(1024 bytes) // Will erase the first page of flash (1024 bytes)
delay(200);
SWD_EEE_CSEQ(0x9e, 1); // 解锁 // unlock
delay(200);
SWD_EEE_CSEQ(0x8a, 1);
delay(20);
SWD_EEE_CSEQ(0x88, 1);
SWD_EEE_CSEQ(0x00, 1);
}
uint32_t SWD_EEE_Read(uint16_t addr)
{
uint32_t data;
SWD_EEE_CSEQ(0xc0, addr);
SWD_EEE_CSEQ(0xe0, addr);
SWD_WriteByte(1, 0xaa, 1);
((uint8_t *)&data)[0] = SWD_ReadByte(1, 0);
((uint8_t *)&data)[1] = SWD_ReadByte(0, 0);
((uint8_t *)&data)[2] = SWD_ReadByte(0, 0);
((uint8_t *)&data)[3] = SWD_ReadByte(0, 1);
SWD_Idle(4);
return data;
}
void SWD_EEE_Write(uint32_t data, uint16_t addr)
{
SWD_EEE_DSEQ(data);
SWD_EEE_CSEQ(0x86, addr);
SWD_EEE_CSEQ(0xc6, addr);
SWD_EEE_CSEQ(0x86, addr);
}
uint8_t SWD_read_lockbits()
{
char swdid[4];
SWD_ReadSWDID(swdid);
return swdid[0];
}
uint8_t SWD_UnLock(uint8_t chip_erase)
{
char swdid[4];
SWD_ReadSWDID(swdid);
// {0x3e, 0xa2, 0x50, 0xe9}表示这是第一次进行SWD操作,{0x3f, 0xa2, 0x50, 0xe9}表示之前进行过SWD解锁操作
// {0x3e, 0xa2, 0x50, 0xe9} indicates that this is the first SWD operation,
// {0x3f, 0xa2, 0x50, 0xe9} indicates that the SWD unlock operation has been performed before
SWD_SWDEN();
if ( ! (swdid[0] == 0x3e || swdid[0] == 0x3f) ) // invalid device
{
return 0;
}
if ( swdid[0] == 0x3f && chip_erase == 0 ) // 已经解锁,且不全片擦除 - // Has been unlocked, and not completely erased
{
return 1;
}
if ( swdid[0] == 0x3e ) // 第一次解锁 // unlock for the first time
{
SWD_UnLock0();
}
if ( chip_erase == 1 ) // full erase
{
SWD_ChipErase();
}
else
if ( chip_erase == 2 ) // partial erase
{
crack();
}
if ( swdid[0] == 0x3e ) // 第一次解锁 // unlock for the first time
{
SWD_UnLock1();
// 此时swdid[0] == 0x3f
// At this time swdid[0] == 0x3f
SWD_WriteByte(1, 0xb1, 0);
SWD_WriteByte(0, 0x3d, 0);
SWD_WriteByte(0, 0x60, 0);
SWD_WriteByte(0, 0x0c, 0);
SWD_WriteByte(0, 0x00, 0);
SWD_WriteByte(0, 0x0f, 1);
SWD_Idle(40);
SWD_UnLock2();
}
SWD_Idle(40);
SWD_WriteByte(1, 0xb1, 0);
SWD_WriteByte(0, 0x0c, 0);
SWD_WriteByte(0, 0x00, 0);
SWD_WriteByte(0, 0x17, 1);
SWD_Idle(40);
char flag[2];
SWD_WriteByte(1, 0xa9, 1);
SWD_Idle(4);
flag[0] = SWD_ReadByte(1, 0);
flag[1] = SWD_ReadByte(0, 1);
SWD_Idle(4);
if ( flag[1] == 0x20 )
{
SWD_WriteByte(1, 0xb1, 0);
SWD_WriteByte(0, 0x3d, 0);
SWD_WriteByte(0, 0x20, 0);
SWD_WriteByte(0, 0x0c, 0);
SWD_WriteByte(0, 0x00, 0);
SWD_WriteByte(0, 0x0f, 1);
SWD_Idle(40);
}
else
if ( flag[1] == 0x60 ) // 0x60没有这段命令 // 0x60 does not have this command
{
}
else
{
return 0; // 不接reset引脚会返回这个 // Do not connect the reset pin will return this
}
SWD_WriteByte(1, 0xb1, 0);
SWD_WriteByte(0, 0x0d, 1);
SWD_Idle(2);
return 1;
}