aboutsummaryrefslogtreecommitdiffstats
path: root/sim/simcard.ino
blob: 0bb01d67138acf405b3b4875eb6dcef21b61d6cb (plain)
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
#define RST  8
#define CLK  9
#define DATA 10

#define PSC1 0xff
#define PSC2 0xff

uint8_t card_data[] = {

0xff, 0xf7, 0x5c, 0xff, 0xff, 0xff, 0xff, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x38, 
0x78, 0x28, 0x07, 0x8c, 0xc1, 0x03, 0xfe, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00,

};

void setup()
{
  digitalWrite(RST, LOW);
  pinMode(RST, OUTPUT);
  digitalWrite(CLK, LOW);
  pinMode(CLK, OUTPUT);
  pinMode(DATA, INPUT_PULLUP);
  digitalWrite(DATA, HIGH);

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.readStringUntil('\m');
}

void loop()
{
again:
  Serial.println("Press 'a' for ATR, 'r' to read card, 'w' to program card, 'u' to unlock card");
  Serial.println("-> You must read card ('r') before you are able to write to card.");
  while (42) {
    if (Serial.available() == 0)
      continue;
    char inChar = Serial.read();
    if (inChar == 'a') {
      Serial.println("ATR...");
      card_atr(52);
      goto again;
    } else if (inChar == 'r') {
      Serial.println("reading...");
      card_read(52, 0);
      goto again;
    } else if (inChar == 'w') {
      Serial.println("writing...");
      card_write(card_data, sizeof(card_data), 0);
      goto again;
    } else if (inChar == 'u') {
      Serial.println("unlocking...");
      card_unlock(PSC1, PSC2);
      goto again;
    } else {
      goto again;
    }
  }
}

void card_atr(int num)
{
  uint8_t data[num];
  
  digitalWrite(RST, HIGH);
  delayMicroseconds(100);
  digitalWrite(CLK, HIGH);
  delayMicroseconds(100);
  digitalWrite(CLK, LOW);
  delayMicroseconds(100);
  digitalWrite(RST, LOW);
  delayMicroseconds(100);
  card_read_bytes(data, num);
}

void card_read(int num, int address)
{
  uint8_t data[num];
  
  card_command(((address >> 2) & 0xc0) | 0x0e, address & 0xff, 0);
  digitalWrite(CLK, HIGH);
  delayMicroseconds(100);
  digitalWrite(CLK, LOW);
  delayMicroseconds(100);
  card_read_bytes(data, num);
}

void card_write(uint8_t *data, int num, int address)
{
  int i, cnt;
  
  for (i = 0; i < num; i++) {
    card_command(((address >> 2) & 0xc0) | 0x33, address & 0xff, *data);
    cnt = card_erase_and_write();
    if (cnt <= 2) {
      Serial.println("write failed!");
//      break;
    }
    address++;
    data++;
  }
}

void card_unlock(uint8_t psc1, uint8_t psc2)
{
  int i;
  uint8_t data[3];
  
  // read error counter
  card_command(0xce, 253, 0);
  digitalWrite(CLK, HIGH);
  delayMicroseconds(100);
  digitalWrite(CLK, LOW);
  delayMicroseconds(100);
  Serial.println("3 bytes: error counter mask, first PSC code, second psc code");
  card_read_bytes(data, 3);
  // check bit to erase
  for (i = 0; i < 8; i++) {
     if ((data[0] & (1 << i)))
       break;
  }
  if (i == 8) {
    Serial.println("SORRY NO MORE BITS TO ERASE TO UNLOCK, YOUR CARD IS BRICKED!");
    return;
  }
  data[0] = data[0] - (1 << i);
  // ease bit to unlock
  card_command(0xf2, 253, data[0]);
  card_erase_and_write(); 
  // unlock
  Serial.println("unlock bit has been erased, sending PSC code");
  card_command(0xcd, 254, psc1);
  card_erase_and_write(); 
  card_command(0xcd, 255, psc2);
  card_erase_and_write(); 
  // read error counter mask
  card_command(0xce, 253, 0);
  digitalWrite(CLK, HIGH);
  delayMicroseconds(100);
  digitalWrite(CLK, LOW);
  delayMicroseconds(100);
  Serial.println("checking error counter mask, one bit shall be erased...");
  card_read_bytes(data, 1);
  // reset unlock mask
  Serial.println("PSC code has been sent, resetting error counter mask");
  card_command(0xf3, 253, 0xff);
  card_erase_and_write();
  // read error counter mask
  card_command(0xce, 253, 0);
  digitalWrite(CLK, HIGH);
  delayMicroseconds(100);
  digitalWrite(CLK, LOW);
  delayMicroseconds(100);
  Serial.println("reading: error counter mask, all bits should be reset");
  card_read_bytes(data, 1);
}

void card_command(uint8_t c1, uint8_t c2, uint8_t c3)
{
  int i, j;
  uint8_t c[3];

  c[0] = c1;
  c[1] = c2;
  c[2] = c3;
  Serial.println("card command:");
  Serial.println(c1);
  Serial.println(c2);
  Serial.println(c3);
  
  digitalWrite(RST, HIGH);
  delayMicroseconds(100);
  pinMode(DATA, OUTPUT);
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 8; j++) {
      digitalWrite(DATA, ((c[i] >> j) & 1) ? HIGH : LOW);
      delayMicroseconds(100);
      digitalWrite(CLK, HIGH);
      delayMicroseconds(100);
      digitalWrite(CLK, LOW);
      delayMicroseconds(100);
    }
  }
  pinMode(DATA, INPUT_PULLUP);
  digitalWrite(DATA, HIGH);
  digitalWrite(RST, LOW);
  delayMicroseconds(100);
}

void card_read_bytes(uint8_t *data, int num) {
  int i, j;
  
  for (i = 0; i < num; i++) {
    for (j = 0; j < 8; j++) {
      data[i] = (data[i] >> 1) | ((digitalRead(DATA) != LOW) ? 128 : 0);
      Serial.print((digitalRead(DATA) != LOW) ? '1' : '0');
      digitalWrite(CLK, HIGH);
      delayMicroseconds(100);
      digitalWrite(CLK, LOW);
      delayMicroseconds(100);
    }
    Serial.print(" 0x");
    Serial.println(data[i], HEX);
  }
}

int card_erase_and_write(void)
{
  int i;

  for (i = 0; i < 203; i++) {
    digitalWrite(CLK, HIGH);
    delayMicroseconds(100);
    digitalWrite(CLK, LOW);
    delayMicroseconds(100);
if (0)    if (digitalRead(DATA) == LOW) {
      Serial.print(" -> write pulses:");
      Serial.println(i);
      break;
    }
  }
  return i;
}