Skip to content

Instantly share code, notes, and snippets.

@Wh1terat
Created September 4, 2023 18:18
Show Gist options
  • Save Wh1terat/944cee824a2a02a9c28f39ed7c598ff9 to your computer and use it in GitHub Desktop.
Save Wh1terat/944cee824a2a02a9c28f39ed7c598ff9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import serial
import signal
DEVICE = '/dev/ttyUSB0'
BAUD = 38400
PROMPT = 'FW>'
BLOCK_SIZE = 0x1000
class KeyboardInterruptException(Exception):
pass
def signal_handler(signum, frame):
raise KeyboardInterruptException
signal.signal(signal.SIGINT, signal_handler)
def connect(device, baud):
try:
ser = serial.Serial(device, baud)
ser.reset_input_buffer()
ser.reset_output_buffer()
print('Waiting for console...', end="", flush=True)
ser.write(b'\r')
ser.read_until(PROMPT.encode())
print('Connected!')
return ser
except serial.SerialException:
print('Console Port Error!')
raise SystemExit()
def dump_memory(ser, start, length, chunk):
for chunk_start in range(start, start+length, chunk):
chunk_len = min(((start+length)-chunk_start), chunk)
print(f'Dumping chunk {chunk_start:#0{10}x} [{chunk_len:#0x}]...')
ser.write(f'mb {chunk_start:x} {chunk_len:x}\r'.encode())
buffer = ser.read_until(f'\n{PROMPT}'.encode())
for line in buffer.decode('ascii').splitlines()[1:]:
if 'The Address is converted' in line:
print(line)
else:
try:
yield bytes.fromhex(line[10:33] + line[36:60])
except ValueError:
print(line)
continue
def main():
blocks = [
#(0xA0000000, 0x00024000), # BL16XMNI.004
#(0xA0000000, 0x04000000),
(0xA010d720, 0x3EF28E0),
]
with connect(DEVICE, BAUD) as ser:
for (start, length) in blocks:
filename = f'{start:#0{10}x}.bin'
try:
with open(filename, 'wb') as fh:
print(f'Dumping {start:#0{10}x} [{length:#0x}]...')
for data in dump_memory(ser, start, length, BLOCK_SIZE):
fh.write(data)
fh.flush()
print(f'Done! - {filename}')
except KeyboardInterruptException:
print(f"Interrupted! - {filename}")
break
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment