2014-02-28 05:09:40 -06:00
|
|
|
#!/usr/bin/env python3
|
2022-08-30 10:25:24 -05:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2014-02-28 05:09:40 -06:00
|
|
|
"""
|
|
|
|
OpenOCD RPC example, covered by GNU GPLv3 or later
|
|
|
|
Copyright (C) 2014 Andreas Ortmann (ortmann@finf.uni-hannover.de)
|
|
|
|
|
|
|
|
|
|
|
|
Example output:
|
|
|
|
./ocd_rpc_example.py
|
|
|
|
echo says hi!
|
|
|
|
|
|
|
|
target state: halted
|
|
|
|
target halted due to debug-request, current mode: Thread
|
|
|
|
xPSR: 0x01000000 pc: 0x00000188 msp: 0x10000fd8
|
|
|
|
|
|
|
|
variable @ 0x10000000: 0x01c9c380
|
|
|
|
|
|
|
|
variable @ 0x10000000: 0xdeadc0de
|
|
|
|
|
|
|
|
memory (before): ['0xdeadc0de', '0x00000011', '0xaaaaaaaa', '0x00000023',
|
|
|
|
'0x00000042', '0x0000ffff']
|
|
|
|
|
|
|
|
memory (after): ['0x00000001', '0x00000000', '0xaaaaaaaa', '0x00000023',
|
|
|
|
'0x00000042', '0x0000ffff']
|
|
|
|
"""
|
|
|
|
|
|
|
|
import socket
|
|
|
|
import itertools
|
|
|
|
|
|
|
|
def strToHex(data):
|
|
|
|
return map(strToHex, data) if isinstance(data, list) else int(data, 16)
|
|
|
|
|
|
|
|
def hexify(data):
|
|
|
|
return "<None>" if data is None else ("0x%08x" % data)
|
|
|
|
|
|
|
|
def compareData(a, b):
|
|
|
|
for i, j, num in zip(a, b, itertools.count(0)):
|
|
|
|
if i != j:
|
|
|
|
print("difference at %d: %s != %s" % (num, hexify(i), hexify(j)))
|
|
|
|
|
|
|
|
|
|
|
|
class OpenOcd:
|
|
|
|
COMMAND_TOKEN = '\x1a'
|
|
|
|
def __init__(self, verbose=False):
|
|
|
|
self.verbose = verbose
|
|
|
|
self.tclRpcIp = "127.0.0.1"
|
|
|
|
self.tclRpcPort = 6666
|
|
|
|
self.bufferSize = 4096
|
|
|
|
|
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
|
|
|
def __enter__(self):
|
2019-05-20 11:14:46 -05:00
|
|
|
self.connect()
|
2014-02-28 05:09:40 -06:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
2019-05-20 11:14:46 -05:00
|
|
|
self.disconnect()
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
self.sock.connect((self.tclRpcIp, self.tclRpcPort))
|
|
|
|
|
|
|
|
def disconnect(self):
|
2014-02-28 05:09:40 -06:00
|
|
|
try:
|
|
|
|
self.send("exit")
|
|
|
|
finally:
|
|
|
|
self.sock.close()
|
|
|
|
|
|
|
|
def send(self, cmd):
|
|
|
|
"""Send a command string to TCL RPC. Return the result that was read."""
|
|
|
|
data = (cmd + OpenOcd.COMMAND_TOKEN).encode("utf-8")
|
|
|
|
if self.verbose:
|
|
|
|
print("<- ", data)
|
|
|
|
|
|
|
|
self.sock.send(data)
|
|
|
|
return self._recv()
|
|
|
|
|
|
|
|
def _recv(self):
|
|
|
|
"""Read from the stream until the token (\x1a) was received."""
|
|
|
|
data = bytes()
|
|
|
|
while True:
|
|
|
|
chunk = self.sock.recv(self.bufferSize)
|
|
|
|
data += chunk
|
|
|
|
if bytes(OpenOcd.COMMAND_TOKEN, encoding="utf-8") in chunk:
|
|
|
|
break
|
|
|
|
|
|
|
|
if self.verbose:
|
|
|
|
print("-> ", data)
|
|
|
|
|
|
|
|
data = data.decode("utf-8").strip()
|
|
|
|
data = data[:-1] # strip trailing \x1a
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def readVariable(self, address):
|
2019-05-31 06:42:28 -05:00
|
|
|
raw = self.send("mdw 0x%x" % address).split(": ")
|
2014-02-28 05:09:40 -06:00
|
|
|
return None if (len(raw) < 2) else strToHex(raw[1])
|
|
|
|
|
|
|
|
def readMemory(self, wordLen, address, n):
|
2022-02-25 08:44:58 -06:00
|
|
|
output = self.send("read_memory 0x%x %d %d" % (address, wordLen, n))
|
|
|
|
return [*map(lambda x: int(x, 16), output.split(" "))]
|
2014-02-28 05:09:40 -06:00
|
|
|
|
|
|
|
def writeVariable(self, address, value):
|
|
|
|
assert value is not None
|
|
|
|
self.send("mww 0x%x 0x%x" % (address, value))
|
|
|
|
|
2022-02-25 08:44:58 -06:00
|
|
|
def writeMemory(self, wordLen, address, data):
|
|
|
|
data = "{" + ' '.join(['0x%x' % x for x in data]) + "}"
|
|
|
|
self.send("write_memory 0x%x %d %s" % (address, wordLen, data))
|
2014-02-28 05:09:40 -06:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
def show(*args):
|
|
|
|
print(*args, end="\n\n")
|
|
|
|
|
|
|
|
with OpenOcd() as ocd:
|
|
|
|
ocd.send("reset")
|
|
|
|
|
2019-07-01 07:34:30 -05:00
|
|
|
show(ocd.send("capture { echo \"echo says hi!\" }")[:-1])
|
2019-05-31 06:42:28 -05:00
|
|
|
show(ocd.send("capture \"halt\"")[:-1])
|
2014-02-28 05:09:40 -06:00
|
|
|
|
2020-07-12 17:32:49 -05:00
|
|
|
# Read the first few words at the RAM region (put starting address of RAM
|
2014-02-28 05:09:40 -06:00
|
|
|
# region into 'addr')
|
|
|
|
addr = 0x10000000
|
|
|
|
|
|
|
|
value = ocd.readVariable(addr)
|
|
|
|
show("variable @ %s: %s" % (hexify(addr), hexify(value)))
|
|
|
|
|
|
|
|
ocd.writeVariable(addr, 0xdeadc0de)
|
|
|
|
show("variable @ %s: %s" % (hexify(addr), hexify(ocd.readVariable(addr))))
|
|
|
|
|
|
|
|
data = [1, 0, 0xaaaaaaaa, 0x23, 0x42, 0xffff]
|
|
|
|
wordlen = 32
|
|
|
|
n = len(data)
|
|
|
|
|
|
|
|
read = ocd.readMemory(wordlen, addr, n)
|
|
|
|
show("memory (before):", list(map(hexify, read)))
|
|
|
|
|
|
|
|
ocd.writeMemory(wordlen, addr, n, data)
|
|
|
|
|
|
|
|
read = ocd.readMemory(wordlen, addr, n)
|
|
|
|
show("memory (after):", list(map(hexify, read)))
|
|
|
|
|
|
|
|
compareData(read, data)
|
|
|
|
|
|
|
|
ocd.send("resume")
|