1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

tools: refresh mem_* debug tools, blackify

This commit is contained in:
matejcik 2019-04-15 16:15:28 +02:00
parent 832c47a278
commit 2813522b05
3 changed files with 55 additions and 59 deletions

View File

@ -1,47 +1,49 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from trezorlib.debuglink import DebugLink from trezorlib.debuglink import DebugLink
from trezorlib.client import TrezorClient
from trezorlib.transport import enumerate_devices from trezorlib.transport import enumerate_devices
import sys import sys
# fmt: off
sectoraddrs = [0x8000000, 0x8004000, 0x8008000, 0x800c000, sectoraddrs = [0x8000000, 0x8004000, 0x8008000, 0x800c000,
0x8010000, 0x8020000, 0x8040000, 0x8060000, 0x8010000, 0x8020000, 0x8040000, 0x8060000,
0x8080000, 0x80a0000, 0x80c0000, 0x80f0000] 0x8080000, 0x80a0000, 0x80c0000, 0x80f0000]
sectorlens = [0x4000, 0x4000, 0x4000, 0x4000, sectorlens = [0x4000, 0x4000, 0x4000, 0x4000,
0x8000, 0x10000, 0x10000, 0x10000, 0x8000, 0x10000, 0x10000, 0x10000,
0x10000, 0x10000, 0x10000, 0x10000] 0x10000, 0x10000, 0x10000, 0x10000]
# fmt: on
def find_debug():
for device in enumerate_devices():
try:
debug_transport = device.find_debug()
debug = DebugLink(debug_transport, auto_interact=False)
debug.open()
return debug
except Exception:
continue
else:
print("No suitable Trezor device found")
sys.exit(1)
def main(): def main():
# List all debuggable TREZORs debug = find_debug()
devices = [device for device in enumerate_devices() if hasattr(device, 'find_debug')]
# Check whether we found any
if len(devices) == 0:
print('No TREZOR found')
return
# Use first connected device
transport = devices[0]
debug_transport = devices[0].find_debug()
# Creates object for manipulating TREZOR
client = TrezorClient(transport)
debug = DebugLink(debug_transport)
sector = int(sys.argv[1]) sector = int(sys.argv[1])
f = open(sys.argv[2], "rb") f = open(sys.argv[2], "rb")
content = f.read(sectorlens[sector]) content = f.read(sectorlens[sector])
if (len(content) != sectorlens[sector]): if len(content) != sectorlens[sector]:
print("Not enough bytes in file") print("Not enough bytes in file")
return return
debug.flash_erase(sector) debug.flash_erase(sector)
step = 0x400 step = 0x400
for offset in range(0, sectorlens[sector], step): for offset in range(0, sectorlens[sector], step):
debug.memory_write(sectoraddrs[sector] + offset, content[offset:offset + step], flash=True) debug.memory_write(
client.close() sectoraddrs[sector] + offset, content[offset : offset + step], flash=True
)
if __name__ == '__main__': if __name__ == "__main__":
main() main()

View File

@ -1,6 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from trezorlib.debuglink import DebugLink from trezorlib.debuglink import DebugLink
from trezorlib.client import TrezorClient
from trezorlib.transport import enumerate_devices from trezorlib.transport import enumerate_devices
import sys import sys
@ -13,28 +12,28 @@ import sys
# be running a firmware that was built with debug link enabled # be running a firmware that was built with debug link enabled
def find_debug():
for device in enumerate_devices():
try:
debug_transport = device.find_debug()
debug = DebugLink(debug_transport, auto_interact=False)
debug.open()
return debug
except Exception:
continue
else:
print("No suitable Trezor device found")
sys.exit(1)
def main(): def main():
# List all debuggable TREZORs debug = find_debug()
devices = [device for device in enumerate_devices() if hasattr(device, 'find_debug')]
# Check whether we found any
if len(devices) == 0:
print('No TREZOR found')
return
# Use first connected device
transport = devices[0]
debug_transport = devices[0].find_debug()
# Creates object for manipulating TREZOR
client = TrezorClient(transport)
debug = DebugLink(debug_transport)
arg1 = int(sys.argv[1], 16) arg1 = int(sys.argv[1], 16)
arg2 = int(sys.argv[2], 16) arg2 = int(sys.argv[2], 16)
step = 0x400 if arg2 >= 0x400 else arg2 step = 0x400 if arg2 >= 0x400 else arg2
f = open('memory.dat', 'wb') f = open("memory.dat", "wb")
for addr in range(arg1, arg1 + arg2, step): for addr in range(arg1, arg1 + arg2, step):
mem = debug.memory_read(addr, step) mem = debug.memory_read(addr, step)
@ -42,8 +41,6 @@ def main():
f.close() f.close()
client.close()
if __name__ == "__main__":
if __name__ == '__main__':
main() main()

View File

@ -1,30 +1,27 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from trezorlib.debuglink import DebugLink from trezorlib.debuglink import DebugLink
from trezorlib.client import TrezorClient
from trezorlib.transport import enumerate_devices from trezorlib.transport import enumerate_devices
import sys import sys
def find_debug():
for device in enumerate_devices():
try:
debug_transport = device.find_debug()
debug = DebugLink(debug_transport, auto_interact=False)
debug.open()
return debug
except Exception:
continue
else:
print("No suitable Trezor device found")
sys.exit(1)
def main(): def main():
# List all debuggable TREZORs debug = find_debug()
devices = [device for device in enumerate_devices() if hasattr(device, 'find_debug')]
# Check whether we found any
if len(devices) == 0:
print('No TREZOR found')
return
# Use first connected device
transport = devices[0]
debug_transport = devices[0].find_debug()
# Creates object for manipulating TREZOR
client = TrezorClient(transport)
debug = DebugLink(debug_transport)
debug.memory_write(int(sys.argv[1], 16), bytes.fromhex(sys.argv[2]), flash=True) debug.memory_write(int(sys.argv[1], 16), bytes.fromhex(sys.argv[2]), flash=True)
client.close()
if __name__ == '__main__': if __name__ == "__main__":
main() main()