mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
put the trng tester host-side helpe script inside the tools directory
This commit is contained in:
parent
ad72c0ee7d
commit
406aa9405d
1 changed files with 310 additions and 0 deletions
310
tools/trng_test.py
Executable file
310
tools/trng_test.py
Executable file
|
|
@ -0,0 +1,310 @@
|
|||
#! /usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
|
||||
import usb.core
|
||||
import usb.util
|
||||
import array
|
||||
import sys
|
||||
import hashlib
|
||||
import csv
|
||||
import time
|
||||
|
||||
from progressbar.bar import ProgressBar
|
||||
|
||||
class PrecursorUsb:
|
||||
def __init__(self, dev):
|
||||
self.dev = dev
|
||||
self.RDSR = 0x05
|
||||
self.RDSCUR = 0x2B
|
||||
self.RDID = 0x9F
|
||||
self.WREN = 0x06
|
||||
self.WRDI = 0x04
|
||||
self.SE4B = 0x21
|
||||
self.BE4B = 0xDC
|
||||
self.PP4B = 0x12
|
||||
self.registers = {}
|
||||
self.regions = {}
|
||||
self.gitrev = ''
|
||||
|
||||
def register(self, name):
|
||||
return int(self.registers[name], 0)
|
||||
|
||||
def peek(self, addr, display=False):
|
||||
_dummy_s = '\x00'.encode('utf-8')
|
||||
data = array.array('B', _dummy_s * 4)
|
||||
|
||||
numread = self.dev.ctrl_transfer(bmRequestType=(0x80 | 0x43), bRequest=0,
|
||||
wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff),
|
||||
data_or_wLength=data, timeout=500)
|
||||
|
||||
read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False)
|
||||
if display == True:
|
||||
print("0x{:08x}".format(read_data))
|
||||
return read_data
|
||||
|
||||
def poke(self, addr, wdata, check=False, display=False):
|
||||
if check == True:
|
||||
_dummy_s = '\x00'.encode('utf-8')
|
||||
data = array.array('B', _dummy_s * 4)
|
||||
|
||||
numread = self.dev.ctrl_transfer(bmRequestType=(0x80 | 0x43), bRequest=0,
|
||||
wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff),
|
||||
data_or_wLength=data, timeout=500)
|
||||
|
||||
read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False)
|
||||
print("before poke: 0x{:08x}".format(read_data))
|
||||
|
||||
data = array.array('B', wdata.to_bytes(4, 'little'))
|
||||
numwritten = self.dev.ctrl_transfer(bmRequestType=(0x00 | 0x43), bRequest=0,
|
||||
wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff),
|
||||
data_or_wLength=data, timeout=500)
|
||||
|
||||
if check == True:
|
||||
_dummy_s = '\x00'.encode('utf-8')
|
||||
data = array.array('B', _dummy_s * 4)
|
||||
|
||||
numread = self.dev.ctrl_transfer(bmRequestType=(0x80 | 0x43), bRequest=0,
|
||||
wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff),
|
||||
data_or_wLength=data, timeout=500)
|
||||
|
||||
read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False)
|
||||
print("after poke: 0x{:08x}".format(read_data))
|
||||
if display == True:
|
||||
print("wrote 0x{:08x} to 0x{:08x}".format(wdata, addr))
|
||||
|
||||
def burst_read(self, addr, len):
|
||||
_dummy_s = '\x00'.encode('utf-8')
|
||||
maxlen = 4096
|
||||
|
||||
ret = bytearray()
|
||||
packet_count = len // maxlen
|
||||
if (len % maxlen) != 0:
|
||||
packet_count += 1
|
||||
|
||||
time.sleep(0.2) # this improves system stability, somehow
|
||||
for pkt_num in range(packet_count):
|
||||
# print('.', end='')
|
||||
cur_addr = addr + pkt_num * maxlen
|
||||
if pkt_num == packet_count - 1:
|
||||
if len % maxlen != 0:
|
||||
bufsize = len % maxlen
|
||||
else:
|
||||
bufsize = maxlen
|
||||
else:
|
||||
bufsize = maxlen
|
||||
|
||||
data = array.array('B', _dummy_s * bufsize)
|
||||
for attempt in range(10):
|
||||
try:
|
||||
numread = self.dev.ctrl_transfer(bmRequestType=(0x80 | 0x43), bRequest=0,
|
||||
wValue=(cur_addr & 0xffff), wIndex=((cur_addr >> 16) & 0xffff),
|
||||
data_or_wLength=data, timeout=50000)
|
||||
except Exception as e:
|
||||
self.dev.reset()
|
||||
time.sleep(2)
|
||||
else:
|
||||
break
|
||||
else:
|
||||
sys.stderr.write("Burst read failed")
|
||||
exit(1)
|
||||
|
||||
if numread != bufsize:
|
||||
sys.stderr.write("Burst read error: {} bytes requested, {} bytes read at 0x{:08x}".format(bufsize, numread, cur_addr))
|
||||
else:
|
||||
ret = ret + data
|
||||
|
||||
return ret
|
||||
|
||||
def burst_write(self, addr, data):
|
||||
if len(data) == 0:
|
||||
return
|
||||
|
||||
maxlen = 4096
|
||||
packet_count = len(data) // maxlen
|
||||
if (len(data) % maxlen) != 0:
|
||||
packet_count += 1
|
||||
|
||||
for pkt_num in range(packet_count):
|
||||
cur_addr = addr + pkt_num * maxlen
|
||||
if pkt_num == packet_count - 1:
|
||||
if len(data) % maxlen != 0:
|
||||
bufsize = len(data) % maxlen
|
||||
else:
|
||||
bufsize = maxlen
|
||||
else:
|
||||
bufsize = maxlen
|
||||
|
||||
wdata = array.array('B', data[(pkt_num * maxlen):(pkt_num * maxlen) + bufsize])
|
||||
numwritten = self.dev.ctrl_transfer(bmRequestType=(0x00 | 0x43), bRequest=0,
|
||||
wValue=(cur_addr & 0xffff), wIndex=((cur_addr >> 16) & 0xffff),
|
||||
data_or_wLength=wdata, timeout=500)
|
||||
|
||||
if numwritten != bufsize:
|
||||
print("Burst write error: {} bytes requested, {} bytes written at 0x{:08x}".format(bufsize, numwritten, cur_addr))
|
||||
exit(1)
|
||||
|
||||
def ping_wdt(self):
|
||||
self.poke(self.register('wdt_watchdog'), 0x600d, display=False)
|
||||
self.poke(self.register('wdt_watchdog'), 0xc0de, display=False)
|
||||
|
||||
def load_csrs(self):
|
||||
LOC_CSRCSV = 0x20278000 # this address shouldn't change because it's how we figure out our version number
|
||||
|
||||
csr_data = self.burst_read(LOC_CSRCSV, 0x8000)
|
||||
hasher = hashlib.sha512()
|
||||
hasher.update(csr_data[:0x7FC0])
|
||||
digest = hasher.digest()
|
||||
if digest != csr_data[0x7fc0:]:
|
||||
print("Could not find a valid csr.csv descriptor on the device, aborting!")
|
||||
exit(1)
|
||||
|
||||
csr_len = int.from_bytes(csr_data[:4], 'little')
|
||||
csr_extracted = csr_data[4:4+csr_len]
|
||||
decoded = csr_extracted.decode('utf-8')
|
||||
# strip comments
|
||||
stripped = []
|
||||
for line in decoded.split('\n'):
|
||||
if line.startswith('#') == False:
|
||||
stripped.append(line)
|
||||
# create database
|
||||
csr_db = csv.reader(stripped)
|
||||
for row in csr_db:
|
||||
if len(row) > 1:
|
||||
if 'csr_register' in row[0]:
|
||||
self.registers[row[1]] = row[2]
|
||||
if 'memory_region' in row[0]:
|
||||
self.regions[row[1]] = [row[2], row[3]]
|
||||
if 'git_rev' in row[0]:
|
||||
self.gitrev = row[1]
|
||||
print("Using SoC {} registers".format(self.gitrev))
|
||||
|
||||
def auto_int(x):
|
||||
return int(x, 0)
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Pipe TRNG data out of a Xous 0.8/0.9 Precusor that is configured to run the test server")
|
||||
parser.add_argument(
|
||||
"--peek", required=False, help="Inspect an address", type=auto_int, metavar=('ADDR')
|
||||
)
|
||||
parser.add_argument(
|
||||
"--poke", required=False, help="Write to an address", type=auto_int, nargs=2, metavar=('ADDR', 'DATA')
|
||||
)
|
||||
parser.add_argument(
|
||||
"--check-poke", required=False, action='store_true', help="Read data before and after the poke"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--config", required=False, help="Print the descriptor", action='store_true'
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
dev = usb.core.find(idProduct=0x5bf0, idVendor=0x1209)
|
||||
|
||||
if dev is None:
|
||||
raise ValueError('Precursor device not found')
|
||||
|
||||
dev.set_configuration()
|
||||
if args.config:
|
||||
cfg = dev.get_active_configuration()
|
||||
print(cfg)
|
||||
|
||||
pc_usb = PrecursorUsb(dev)
|
||||
|
||||
if args.peek:
|
||||
pc_usb.peek(args.peek, display=True)
|
||||
# print(burst_read(dev, args.peek, 256).hex())
|
||||
exit(0)
|
||||
|
||||
if args.poke:
|
||||
addr, data = args.poke
|
||||
pc_usb.poke(addr, data, check=args.check_poke, display=True)
|
||||
# import os
|
||||
# d = bytearray(os.urandom(8000))
|
||||
# burst_write(dev, addr, d)
|
||||
# r = burst_read(dev, addr, 8000)
|
||||
# print(r.hex())
|
||||
# if d != r:
|
||||
# print("mismatch")
|
||||
# else:
|
||||
# print("match")
|
||||
exit(0)
|
||||
|
||||
pc_usb.load_csrs() # prime the CSR values
|
||||
if "v0.8" in pc_usb.gitrev:
|
||||
LOC_SOC = 0x00000000
|
||||
LOC_LOADER = 0x00500000
|
||||
LOC_KERNEL = 0x00980000
|
||||
LOC_WF200 = 0x07F80000
|
||||
LOC_EC = 0x07FCE000
|
||||
elif args.force == True:
|
||||
# try the v0.8 offsets
|
||||
LOC_SOC = 0x00000000
|
||||
LOC_LOADER = 0x00500000
|
||||
LOC_KERNEL = 0x00980000
|
||||
LOC_WF200 = 0x07F80000
|
||||
LOC_EC = 0x07FCE000
|
||||
else:
|
||||
print("SoC is from an unknow rev '{}', use --force to continue anyways with v0.8 firmware offsets".format(pc_usb.load_csrs()))
|
||||
exit(1)
|
||||
|
||||
vexdbg_addr = int(pc_usb.regions['vexriscv_debug'][0], 0)
|
||||
#pc_usb.ping_wdt()
|
||||
#print("Halting CPU.")
|
||||
#pc_usb.poke(vexdbg_addr, 0x00020000)
|
||||
|
||||
messible2_in = pc_usb.register('messible2_in')
|
||||
messible_out = pc_usb.register('messible_out')
|
||||
RAM_A = 0x4080_0000
|
||||
RAM_B = 0x4090_0000
|
||||
BURST_LEN = 512 * 1024
|
||||
TIMEOUT = 30.0
|
||||
|
||||
phase = 0
|
||||
blocks = 0
|
||||
while True:
|
||||
start_time = time.time()
|
||||
#sys.stderr.write("at phase, {} waiting for next buffer\n".format(phase))
|
||||
while True:
|
||||
remote_phase = pc_usb.peek(messible_out)
|
||||
if remote_phase > phase:
|
||||
break
|
||||
time.sleep(0.10)
|
||||
if time.time() > (start_time + TIMEOUT):
|
||||
sys.stderr.write("timeout\n")
|
||||
break
|
||||
|
||||
phase = remote_phase
|
||||
pc_usb.poke(messible2_in, phase)
|
||||
|
||||
if (phase % 2) == 1:
|
||||
#sys.stderr.write("phase {} fetching RAM_A\n".format(phase))
|
||||
pc_usb.poke(vexdbg_addr, 0x00020000)
|
||||
page = pc_usb.burst_read(RAM_A, BURST_LEN)
|
||||
pc_usb.poke(vexdbg_addr, 0x02000000)
|
||||
sys.stdout.buffer.write(page)
|
||||
#sys.stderr.write("got page A {}\n".format(len(page)))
|
||||
else:
|
||||
#sys.stderr.write("phase {} fetching RAM_B\n".format(phase))
|
||||
pc_usb.poke(vexdbg_addr, 0x00020000)
|
||||
page = pc_usb.burst_read(RAM_B, BURST_LEN)
|
||||
pc_usb.poke(vexdbg_addr, 0x02000000)
|
||||
sys.stdout.buffer.write(page)
|
||||
#sys.stderr.write("got page B {}\n".format(len(page)))
|
||||
|
||||
blocks += 1
|
||||
|
||||
|
||||
#print("Resuming CPU.")
|
||||
#pc_usb.poke(vexdbg_addr, 0x02000000)
|
||||
|
||||
#print("Resetting SOC...")
|
||||
#try:
|
||||
# pc_usb.poke(pc_usb.register('reboot_soc_reset'), 0xac, display=False)
|
||||
#except usb.core.USBError:
|
||||
# pass # we expect an error because we reset the SOC and that includes the USB core
|
||||
|
||||
# print("If you need to run more commands, please unplug and re-plug your device in, as the Precursor USB core was just reset")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
exit(0)
|
||||
Loading…
Reference in a new issue