-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimtrace.py
More file actions
97 lines (81 loc) · 2.83 KB
/
simtrace.py
File metadata and controls
97 lines (81 loc) · 2.83 KB
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
'''
simtrace - main program for the host PC
(C) 2010-2011 by Dean Chester <dean.g.chester@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation
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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''
__author__ = 'Dean Chester'
__email__ = 'dean.g.chester@googlemail.com'
import usb
import sys
import usb.backend.libusb1 as libusb1
from types import *
idVendor = 0x16c0
idProduct = 0x0762
def readUSB():
try:
rawIn = dev.read(SIMTRACE_IN_EP, 10000,None, 10000)
rawIn = rawIn.tolist()
while(rawIn[1] & SIMTRACE_FLAG_WTIME_EXP):
tmpIn = dev.read(SIMTRACE_IN_EP, 10000,None, 10000)
tmpIn = tmpIn.tolist()
rawIn[0:4] = tmpIn[0:4]
rawIn += tmpIn[4:]
return rawIn
except:
return readUSB()
def printATR(atr):
hex = ' '.join('%02x' % b for b in atr)
print 'APDU ATR: (' + len(atr).__str__() + '): '+ hex
def readMoreData():
tmpIn = readUSB()
return tmpIn[4:]
def splitApdu(dataIn):
apdus = []
ins = dataIn[1]
numAPDU = dataIn[4] + 7
if(ins == dataIn[5]):
dataIn.pop(5)
apdus.append(dataIn[0:numAPDU])
while(numAPDU < len(dataIn)):
while((numAPDU + 4) > len(dataIn) | (numAPDU + dataIn[numAPDU + 4] + 7) > len(dataIn)):
dataIn += readMoreData()
nextAPDU = numAPDU + dataIn[numAPDU + 4] + 7
if(nextAPDU >= len(dataIn)):
dataIn += readMoreData()
ins = dataIn[numAPDU+1]
check = dataIn[numAPDU + 5]
if(ins == check):
dataIn.pop(numAPDU + 5)
apdus.append(dataIn[numAPDU:nextAPDU])
numAPDU = nextAPDU
return apdus
SIMTRACE_OUT_EP = 0x01
SIMTRACE_IN_EP = 0x82
SIMTRACE_INT_EP = 0x83
SIMTRACE_FLAG_ATR = 0x01 #/* ATR immediately after reset */
SIMTRACE_FLAG_WTIME_EXP = 0x04 #/* work waiting time expired */
SIMTRACE_FLAG_PPS_FIDI = 0x08 #/* Fi/Di values in res[2] */
dev = usb.core.find(idVendor=idVendor, idProduct=idProduct)
if dev is None:
raise ValueError('Device not found')
dev.set_configuration()
while(True):
rawIn = readUSB()
if(rawIn[1] & SIMTRACE_FLAG_ATR):
printATR(rawIn[4:])
else:
rawIn = rawIn[4:]
apdus = splitApdu(rawIn)
for i in apdus:
apdu = i
hex = ' '.join('%02x' % b for b in apdu)
print "APDU: (%d): %s" % (len(apdu), hex)