forked from zAxis/PSGrade
-
Notifications
You must be signed in to change notification settings - Fork 3
/
psgrade.c
435 lines (367 loc) · 9.8 KB
/
psgrade.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
PSGrade
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <stdio.h>
#include <LUFA/Version.h>
#include <LUFA/Drivers/Board/LEDs.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Drivers/USB/Class/CDC.h>
#include "key.h"
#include "hmac.h"
#define RED (LEDS_LED1)
#define GREEN (LEDS_LED2)
#define BOTH (RED|GREEN)
#define NONE (LEDS_NO_LEDS)
#define LED(x) LEDs_SetAllLEDs(x)
#define PORT_EMPTY 0x0100 /* powered only */
#define PORT_FULL 0x0103 /* connected, enabled, powered, full-speed */
#define C_PORT_CONN 0x0001 /* connection */
#define C_PORT_RESET 0x0010 /* reset */
#define C_PORT_NONE 0x0000 /* no change */
#define CHALLENGE_INDEX 7
uint16_t port_status[6] = { PORT_EMPTY, PORT_EMPTY, PORT_EMPTY, PORT_EMPTY, PORT_EMPTY, PORT_EMPTY };
uint16_t port_change[6] = { C_PORT_NONE, C_PORT_NONE, C_PORT_NONE, C_PORT_NONE, C_PORT_NONE, C_PORT_NONE };
enum {
init,
wait_hub_ready,
hub_ready,
p5_wait_reset,
p5_wait_enumerate,
p5_challenged,
p5_responded,
p5_wait_disconnect,
p5_disconnected,
done,
} state = init;
uint8_t hub_int_response = 0x00;
uint8_t hub_int_force_data0 = 0;
int last_port_conn_clear = 0;
int last_port_reset_clear = 0;
int8_t port_addr[7] = { -1, -1, -1, -1, -1, -1, -1 };
int8_t port_cur = -1;
void USB_Device_SetDeviceAddress(uint8_t Address)
{
port_addr[port_cur] = Address & 0x7f;
UDADDR = Address & 0x7f;
UDADDR |= (1 << ADDEN);
}
void switch_port(int8_t port)
{
if (port_cur == port) return;
port_cur = port;
if (port_addr[port] < 0)
port_addr[port] = 0;
UDADDR = port_addr[port] & 0x7f;
UDADDR |= (1 << ADDEN);
}
volatile uint8_t expire = 0; /* counts down every 10 milliseconds */
ISR(TIMER1_OVF_vect)
{
uint16_t rate = (uint16_t) -(F_CPU / 64 / 100);
TCNT1H = rate >> 8;
TCNT1L = rate & 0xff;
if (expire > 0)
expire--;
}
void panic(int led1, int led2)
{
for(;;) {
_delay_ms(100);
LED(led1);
_delay_ms(100);
LED(led2);
}
}
void HUB_Task(void)
{
Endpoint_SelectEndpoint(1);
if (Endpoint_IsReadWriteAllowed())
{
if (hub_int_response) {
if (hub_int_force_data0) {
Endpoint_ResetDataToggle();
hub_int_force_data0 = 0;
}
Endpoint_Write_Byte(hub_int_response);
Endpoint_ClearIN();
hub_int_response = 0x00;
}
}
}
void JIG_Task(void)
{
static int bytes_out = 0, bytes_in = 0;
Endpoint_SelectEndpoint(2);
if (Endpoint_IsReadWriteAllowed())
{
Endpoint_Read_Stream_LE (&jig_challenge_res[bytes_out], 8, NO_STREAM_CALLBACK) ;
Endpoint_ClearOUT();
bytes_out += 8;
if (bytes_out >= 64) {
//prepare the response
jig_challenge_res[1]--;
jig_challenge_res[3]++;
jig_challenge_res[6]++;
HMACBlock(&jig_challenge_res[CHALLENGE_INDEX],20);
USB_USBTask(); //just in case
HMACDone();
jig_challenge_res[7] = jig_id[0];
jig_challenge_res[8] = jig_id[1];
int i;
for( i = 0; i < 20; i++)
jig_challenge_res[9+i] = hmacdigest[i];
state = p5_challenged;
expire = 50; // was 90
}
}
Endpoint_SelectEndpoint(1);
if (Endpoint_IsReadWriteAllowed() && state == p5_challenged && expire == 0)
{
if ( bytes_in < 64) {
Endpoint_Write_Stream_LE(&jig_challenge_res[bytes_in], 8, NO_STREAM_CALLBACK);
Endpoint_ClearIN();
bytes_in += 8;
if ( bytes_in >= 64) {
state = p5_responded;
expire = 50;
}
}
}
}
void connect_port(int port)
{
last_port_reset_clear = 0;
hub_int_response = (1 << port);
port_status[port - 1] = PORT_FULL;
port_change[port - 1] = C_PORT_CONN;
}
void disconnect_port(int port)
{
last_port_conn_clear = 0;
hub_int_response = (1 << port);
port_status[port - 1] = PORT_EMPTY;
port_change[port - 1] = C_PORT_CONN;
}
void SetupHardware(void)
{
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR &= ~(1 << WDRF);
wdt_disable();
/* Disable clock division */
clock_prescale_set(clock_div_1);
/* Setup timer */
TCCR1B = 0x03; /* timer rate clk/64 */
TIMSK1 = 0x01;
/* Hardware Initialization */
LEDs_Init();
USB_Init();
sei();
}
int main(void)
{
SetupHardware();
HMACInit(jig_key,20);
LED(~GREEN);
state = init;
switch_port(0);
for (;;)
{
if (port_cur == 0)
HUB_Task();
if (port_cur == 5)
JIG_Task();
USB_USBTask();
//connect 5
if (state == hub_ready && expire == 0)
{
connect_port(5);
state = p5_wait_reset;
}
if (state == p5_wait_reset && last_port_reset_clear == 5)
{
switch_port(5);
state = p5_wait_enumerate;
}
// disconnect 5
if (state == p5_responded && expire == 0)
{
switch_port(0);
disconnect_port(5);
state = p5_wait_disconnect;
}
if (state == p5_wait_disconnect && last_port_conn_clear == 5)
{
state = p5_disconnected;
expire = 20;
}
// done
if (state == p5_disconnected && expire == 0)
{
LED(GREEN);
break;
}
}
}
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
const uint8_t wIndex,
const void** const DescriptorAddress)
{
const uint8_t DescriptorType = (wValue >> 8);
void* Address = NULL;
uint16_t Size = NO_DESCRIPTOR;
switch (DescriptorType)
{
case DTYPE_Device:
switch (port_cur) {
case 0:
Address = (void *) HUB_Device_Descriptor;
Size = sizeof(HUB_Device_Descriptor);
break;
case 5:
Address = (void *) port5_device_descriptor;
Size = sizeof(port5_device_descriptor);
break;
}
break;
case DTYPE_Configuration:
switch (port_cur) {
case 0:
Address = (void *) HUB_Config_Descriptor;
Size = sizeof(HUB_Config_Descriptor);
break;
case 5:
// 1 config
Address = (void *) port5_config_descriptor;
Size = sizeof(port5_config_descriptor);
break;
}
break;
case 0x29: // HUB descriptor (always to port 0 we'll assume)
switch (port_cur) {
case 0:
Address = (void *) HUB_Hub_Descriptor;
Size = sizeof(HUB_Hub_Descriptor);
break;
}
break;
}
*DescriptorAddress = Address;
return Size;
}
void EVENT_USB_Device_Connect(void) { }
void EVENT_USB_Device_Disconnect(void) { }
void EVENT_USB_Device_UnhandledControlRequest(void)
{
if (port_cur == 5 && USB_ControlRequest.bRequest == REQ_SetInterface)
{
/* can ignore this */
Endpoint_ClearSETUP();
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
return;
}
if (port_cur == 0 &&
USB_ControlRequest.bmRequestType == 0xA0 &&
USB_ControlRequest.bRequest == 0x00 && // GET HUB STATUS
USB_ControlRequest.wValue == 0x00 &&
USB_ControlRequest.wIndex == 0x00 &&
USB_ControlRequest.wLength == 0x04) {
Endpoint_ClearSETUP();
Endpoint_Write_Word_LE(0x0000); // wHubStatus
Endpoint_Write_Word_LE(0x0000); // wHubChange
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
return;
}
if (port_cur == 0 &&
USB_ControlRequest.bmRequestType == 0xA3 &&
USB_ControlRequest.bRequest == 0x00 && // GET PORT STATUS
USB_ControlRequest.wValue == 0x00 &&
USB_ControlRequest.wLength == 0x04) {
uint8_t p = USB_ControlRequest.wIndex;
if (p < 1 || p > 6) return;
Endpoint_ClearSETUP();
Endpoint_Write_Word_LE(port_status[p - 1]); // wHubStatus
Endpoint_Write_Word_LE(port_change[p - 1]); // wHubChange
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
return;
}
if (port_cur == 0 &&
USB_ControlRequest.bmRequestType == 0x23 &&
USB_ControlRequest.bRequest == 0x03 && // SET_FEATURE
USB_ControlRequest.wLength == 0x00) {
uint8_t p = USB_ControlRequest.wIndex;
if (p < 1 || p > 6) return;
Endpoint_ClearSETUP();
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
switch(USB_ControlRequest.wValue) {
case 0x0008: // PORT_POWER
if (p == 6 && state == init) {
/* after the 6th port is powered, wait a bit and continue */
state = hub_ready;
expire = 15;
}
break;
case 0x0004: // PORT_RESET
hub_int_response = (1 << p);
port_change[p - 1] |= C_PORT_RESET;
break;
}
return;
}
if (port_cur == 0 &&
USB_ControlRequest.bmRequestType == 0x23 &&
USB_ControlRequest.bRequest == 0x01 && // CLEAR_FEATURE
USB_ControlRequest.wLength == 0x00) {
uint8_t p = USB_ControlRequest.wIndex;
if (p < 1 || p > 6) return;
Endpoint_ClearSETUP();
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
switch(USB_ControlRequest.wValue) {
case 0x0010: // C_PORT_CONNECTION
last_port_conn_clear = p;
port_change[p - 1] &= ~C_PORT_CONN;
break;
case 0x0014: // C_PORT_RESET
last_port_reset_clear = p;
port_change[p - 1] &= ~C_PORT_RESET;
break;
}
return;
}
panic(RED, GREEN);
}
void EVENT_USB_Device_ConfigurationChanged(void)
{
/* careful with endpoints: we don't reconfigure when "switching ports"
so we need the same configuration on all of them */
if (!Endpoint_ConfigureEndpoint(1, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, 8, ENDPOINT_BANK_SINGLE))
panic(GREEN, BOTH);
if (!Endpoint_ConfigureEndpoint(2, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, 8, ENDPOINT_BANK_SINGLE))
panic(GREEN, BOTH);
}
void EVENT_USB_Device_Suspend(void) { }
void EVENT_USB_Device_WakeUp(void) { }
void EVENT_USB_Device_Reset(void) { }
void EVENT_USB_Device_StartOfFrame(void) { }
void EVENT_USB_InitFailure(const uint8_t ErrorCode) { }
void EVENT_USB_UIDChange(void) {}