-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxcp_util.py
More file actions
188 lines (149 loc) · 6.16 KB
/
xcp_util.py
File metadata and controls
188 lines (149 loc) · 6.16 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
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
#!/usr/bin/env python3
# Utility for interfacing with CAN XCP.
# Use it for dumping / poking memory locations
# Example usages:
# xcp_util.py dump -s 0x0 -e 0x1FFFFF
# xcp_util.py poke -a 0x4000ecd6 1
#
# requirements: pip install python-can
import argparse
import threading
import struct
import can
import time
import sys
import os
running = True
def can_testerPresent_thread(args, bus: can.Bus):
print("Entering extended diagnostic session via UDS")
# Enter extended diagnostic session
msg = can.Message(arbitration_id=args.udsid, data=[0x02, 0x10, 0x03, 0, 0, 0, 0, 0], is_extended_id=False)
bus.send(msg)
time.sleep(0.5)
bus.send(msg) # send twice to make sure device is awake
while running:
time.sleep(1)
# Tester present
msg = can.Message(arbitration_id=args.udsid, data=[0x01, 0x3E, 0, 0, 0, 0, 0, 0], is_extended_id=False)
pass
def hex_int(x):
return int(x, 16)
def main():
parser = argparse.ArgumentParser(description="XCP Util")
parser.add_argument('--txid', type=hex_int, default=0x18EF6201, help="XCP Transmit Arbitration CAN ID")
parser.add_argument('--rxid', type=hex_int, default=0x18EF6202, help="XCP Receive Arbitration CAN ID")
parser.add_argument('--udsid', type=hex_int, default=0x7E6, help="UDS TX CAN ID (needed to enter diagnostic session and keep ECU awake)")
parser.add_argument('--interface', type=str, default='pcan', help="Interface Name, see: https://python-can.readthedocs.io/en/stable/configuration.html#interface-names")
parser.add_argument('--channel', type=str, default='PCAN_USBBUS1', help="Interface Channel, see the respective interface documentation")
parser.add_argument('--bitrate', type=int, default=500000)
subparsers = parser.add_subparsers(
title='commands',
# description='Available commands',
# help='Use one of the following commands:',
dest='command')
cmd_dump = subparsers.add_parser(
'dump',
help='Dumps memory region'
)
cmd_dump.add_argument('--start', '-s', '-a', type=hex_int, default=0, help='Start address', required=True)
cmd_dump.add_argument('--end', '-e', type=hex_int, default=0, help='End address, inclusive')
cmd_dump.add_argument('--length', '-n', type=hex_int, default=4, help='Number of bytes to dump')
cmd_dump.add_argument('--name', '-O', type=str, default='dump.bin', help='Output filename')
cmd_poke = subparsers.add_parser(
'poke',
help='Write bytes to arbitrary location'
)
cmd_poke.add_argument('--address', '-a', type=hex_int, help='Adress to write to', required=True)
cmd_poke.add_argument('data', type=hex_int, help='Data, hex (currently only supports 1/2/4 bytes)')
cmd_poke.add_argument('len', type=int, default=1, nargs='?', help='Length of data, hex (optional, assumed 1-4 bytes depending on input)')
global args
args = parser.parse_args()
try:
bus = can.interface.Bus(interface=args.interface, channel=args.channel, bitrate=args.bitrate)
thr_keepalive = threading.Thread(target=can_testerPresent_thread, args=(args, bus,))
thr_keepalive.daemon = True
thr_keepalive.start()
listener = threading.Thread(target=can_listener, args=(bus,))
listener.daemon = True
listener.start()
time.sleep(1) # make sure ECU is in diagnostic session
# Implement the functionality based on subcommand
if args.command == 'dump':
dump(args, bus)
elif args.command == 'poke':
poke(args, bus)
else:
parser.print_help()
except KeyboardInterrupt:
global running
running = False
sys.exit(1)
time.sleep(3)
os._exit(1)
cb_msg = None
event = threading.Event()
def blocking_cb(msg):
global cb_msg, event
if msg.arbitration_id != args.rxid:
return
if msg.data[0] == 0xfe:
print("Error:", msg)
cb_msg = None
else:
cb_msg = msg
event.set()
def send_blocking(bus, arb_id, data, is_extended = True):
global cb_msg
cb_msg = None
msg = can.Message(arbitration_id=arb_id, data=data, is_extended_id=is_extended)
bus.send(msg)
event.wait(5)
event.clear()
return cb_msg
def can_listener(bus):
while True:
msg = bus.recv()
if msg:
blocking_cb(msg)
def dump(args, bus: can.Bus):
if args.end == 0: # (not default)
args.end = args.start + args.length - 1
print(f"Reading from {args.start:08X} to {args.end:08X}.")
with open(args.name, 'wb') as bin_file:
send_blocking(bus, args.txid, [0xFF]) # CONNECT
addr = struct.pack('>L', args.start)
send_blocking(bus, args.txid, [0xf6, 0x00, 0x00, 0x00] + list(addr)) # SET_MTA (seek)
for i in range(args.start, args.end + 1, 0x7):
num_bytes = min(0x07, args.end + 1 - i)
response = send_blocking(bus, args.txid, [0xf5, num_bytes, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) # Upload
if response is None:
print(f"Timeout or error at address {i:08X}")
continue
data = response.data
# print hex
for j in range(i, i + num_bytes):
if j % 16 == 0:
print(f'\n{j:08X}: ', end='')
print(f'{data[1 + j - i]:02X} ', end='')
# write to file
bin_file.write(data[1:1+num_bytes])
def poke(args, bus: can.Bus):
print(send_blocking(bus, args.txid, [0xFF])) # CONNECT
addr = struct.pack('>L', args.address)
print(send_blocking(bus, args.txid, [0xf6, 0x00, 0x00, 0x00] + list(addr))) # SET_MTA (seek)
data = struct.pack('>L', args.data)
# set correct data length
if args.len == 1:
if (args.data > 0xFFFF):
args.len = 4
elif (args.data > 0xFF):
args.len = 2
data = struct.pack('>H', args.data)
else:
data = struct.pack('>B', args.data)
response = send_blocking(bus, args.txid, [0xf0, args.len] + list(data) + [0x00, 0x00]) # Download
if response is None:
print(f"Timeout or error")
print(response)
if __name__ == "__main__":
main()