Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import serial, struct, sys
import serial.tools.list_ports

def get_version(com):
com.write(b"\x00" * 5)
reply = com.read(4)
return struct.unpack("<I", reply)[0]

def get_config(com):
com.write(b"\x01" + b"\x00" * 4)
reply = com.read(4)
return struct.unpack("<I", reply)[0]

def stop_smc(com):
com.write(b"\xC1" + b"\x00" * 4)

def start_smc(com):
com.write(b"\xC0" + b"\x00" * 4)

def read_flash(com, block):
com.write(struct.pack("<BI", 2, block))
reply = com.read(4)
code = struct.unpack("<I", reply)[0]
if code != 0:
print("read error 0x%x" % code)
return None
return com.read(0x210)

def write_flash(com, block, data):
if len(data) != 0x210:
return False
com.write(struct.pack("<BI", 3, block) + data)
reply = com.read(4)
code = struct.unpack("<I", reply)[0]
if code != 0:
print("write error 0x%x" % code)
return code == 0

def write_flash_full(com, block, data):
for i in range(block, block + len(data) // 0x210):
if not write_flash(com, i, data[i*0x210:(i+1)*0x210]):
print("error at writing block %x" % i)
return False
return True

def read_flash_full(com, start, size):
res = b""
for i in range(start, start + size):
print("reading %d of %d" % (i, size))
data = read_flash(com, i)
if data is None:
print("error at reading block %x" % i)
return None
res += data
return res

def get_rp_port():
for port in serial.tools.list_ports.comports():
if "2E8A" in port.hwid:
return port
return None

if __name__ == "__main__":
port = get_rp_port()
if not port:
print("no raspberry TTY found")
sys.exit(0)
com = serial.Serial(port.device)
stop_smc(com)
config = get_config(com)
print("360 fconfig: 0x%x" % get_config(com))
if len(sys.argv) < 2 or sys.argv[1] == "read":
rdata = read_flash_full(com, 0, 0x1080000 // 0x210)
name = "nanddump.bin"
if len(sys.argv) > 2:
name = sys.argv[2]
open("dump.bin", "wb").write(rdata)
elif sys.argv[1] == "write" and len(sys.argv) > 2:
wdata = open(sys.argv[2], "rb").read()
write_flash_full(com, 0, wdata)
start_smc(com)
com.close()