-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindJTAG.ino
202 lines (168 loc) · 4.25 KB
/
FindJTAG.ino
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
/*
JTAG IDCODE reader which tries all possible pin assignments.
*/
uint8_t TDI_PIN;
uint8_t TDO_PIN;
uint8_t TMS_PIN;
uint8_t TCK_PIN;
bool found;
/*
#define A2 8
#define A3 9
#define A4 10
#define A5 11
*/
// Uses pins A2-A5, but they can be changed to any other digital or analog pins.
uint8_t permutations[2*3*4][4] = {
{A2, A3, A4, A5},
{A2, A3, A5, A4},
{A2, A4, A3, A5},
{A2, A4, A5, A3},
{A2, A5, A3, A4},
{A2, A5, A4, A3},
{A3, A2, A4, A5},
{A3, A2, A5, A4},
{A3, A4, A2, A5},
{A3, A4, A5, A2},
{A3, A5, A2, A4},
{A3, A5, A4, A2},
{A4, A2, A3, A5},
{A4, A2, A5, A3},
{A4, A3, A2, A5},
{A4, A3, A5, A2},
{A4, A5, A2, A3},
{A4, A5, A3, A2},
{A5, A2, A3, A4},
{A5, A2, A4, A3},
{A5, A3, A2, A4},
{A5, A3, A4, A2},
{A5, A4, A2, A3},
{A5, A4, A3, A2},
};
enum signals {
// Change value to 0 to make signal inverted (may be needed for transistor-based connection schematics).
TDI_HIGH = 1,
TDO_HIGH = 1,
TCK_HIGH = 1,
TMS_HIGH = 1,
// low signals always have opposite value
TDI_LOW = !TDI_HIGH,
TDO_LOW = !TDO_HIGH,
TCK_LOW = !TCK_HIGH,
TMS_LOW = !TMS_HIGH,
};
void setup() {
Serial.begin(9600);
while (!Serial)
;
}
int clock(int tms, int tdi = TDI_LOW) {
// read last output
digitalWrite(TCK_PIN, TCK_LOW);
const int r = digitalRead(TDO_PIN) == TDO_HIGH;
// set new input
digitalWrite(TDI_PIN, tdi);
digitalWrite(TMS_PIN, tms);
digitalWrite(TCK_PIN, TCK_HIGH);
return r;
}
inline int TMS() { return clock(TMS_HIGH); }
inline int tms() { return clock(TMS_LOW); }
inline int TMS_TDI() { return clock(TMS_HIGH, TDI_HIGH); }
inline int tms_TDI() { return clock(TMS_LOW, TDI_HIGH); }
inline int tms_tdi() { return clock(TMS_LOW, TDI_LOW); }
bool detect_chain(unsigned &ilen, unsigned &dlen) {
const unsigned MAX_CHAIN_BITS = 1234;
unsigned i;
// reset and go to Shift-IR
TMS(); TMS(); TMS(); TMS(); TMS();
tms(); TMS(); TMS(); tms(); tms();
// fill IR with zeroes
for (i = 0; i < MAX_CHAIN_BITS; i++)
tms_tdi();
// send ones until we get one back
for (i = 0; i < MAX_CHAIN_BITS && !tms_TDI(); i++)
;
// process response
if (i >= MAX_CHAIN_BITS) {
Serial.println("Unable to determine chain length.");
return false;
}
ilen = i;
// go to Shift-DR sending one last TDI
TMS_TDI(); TMS(); TMS(); tms(); tms();
// fill DR with zeroes
for (i = 0; i < ilen; i++)
tms_tdi();
// send ones until we get one back
for (i = 0; i <= ilen && !tms_TDI(); i++)
;
// process response
if (i > ilen) {
Serial.println("Unable to determine number of devices.");
return false;
}
dlen = i;
return true;
}
uint32_t read(unsigned n) {
uint32_t r = 0;
for (unsigned i = 0; i < n; i++)
r |= uint32_t(tms_tdi()) << i;
return r;
}
void read_idcode() {
Serial.println("IDCODE:");
// read and output 32 bits of IDCODE
#if 1
Serial.print("\tone: ");
Serial.println(read(1), BIN);
Serial.print("\tmanufacturer: 0x");
Serial.println(read(11), HEX);
Serial.print("\tpart: 0x");
Serial.println(read(16), HEX);
Serial.print("\tver: 0x");
Serial.println(read(4), HEX);
#else
for (unsigned j = 0; j < 32; j++)
Serial.print(tms_tdi(), DEC);
Serial.println();
#endif
}
void permutation(unsigned n) {
unsigned ilen, dlen;
Serial.print("Starting JTAG chain detection at permutation ");
Serial.println(n, DEC);
// assign pins according to permutation number
TDI_PIN = permutations[n][0];
TDO_PIN = permutations[n][1];
TMS_PIN = permutations[n][2];
TCK_PIN = permutations[n][3];
pinMode(TDI_PIN, OUTPUT);
pinMode(TDO_PIN, INPUT);
pinMode(TCK_PIN, OUTPUT);
pinMode(TMS_PIN, OUTPUT);
digitalWrite(TCK_PIN, TCK_HIGH);
if (!detect_chain(ilen, dlen))
return;
Serial.print("Chain length: ");
Serial.println(ilen, DEC);
Serial.print("Number of devices: ");
Serial.println(dlen, DEC);
// reset (also loads IDCODE into DR)
TMS(); TMS(); TMS(); TMS(); TMS();
// go to Shift-DR
tms(); TMS(); tms(); tms();
for (unsigned i = 0; i < dlen; i++) {
read_idcode();
Serial.println("We have found a working permutation!");
found = true;
}
}
void loop() {
if (found) return;
for (unsigned n = 0; n < 2*3*4; n++)
permutation(n);
delay(3000);
Serial.println();
}