mcp_dump_d74.py

Use the MCP protocol to get a dump of the settings memory - Angus Ainslie, 06/13/2020 10:23 am

Download (1.6 kB)

 
1
#!/usr/bin/python3 
2

    
3
import ast
4
import serial
5
import argparse
6
import socket
7
import select 
8
import string
9
import struct
10
from time import sleep
11

    
12
DEBUG=True
13

    
14
parser = argparse.ArgumentParser(description='MCP dump the radio memories')
15
parser.add_argument('device', nargs=1, help='radio serial port')
16

    
17
START_MCP=b'0M PROGRAM\r'
18
START_ACK=b'0M\r'
19
STOP_MCP=b'E'
20
STOP_ACK=b'\x06\r\x00'
21
MCP_SUCCESS=b'\x06'
22
MCP_FAIL=b'\x0f'
23

    
24
#name table offset
25
# 0x00010100
26
OFFSET_ADDR=0x00010100
27

    
28
args = parser.parse_args()
29

    
30
def wait_serial(ser):
31
    timeout = 20
32

    
33
    while timeout > 0 :
34
        sleep(0.1)
35
        nbytes = ser.in_waiting
36
        if nbytes:
37
            data = ser.read(nbytes)
38
            #if DEBUG:
39
            #    print("Bytes %s" % data)
40

    
41
            return data
42
        timeout = timeout - 1
43

    
44
    return None
45

    
46
ser = serial.Serial(str(args.device[0]), 9600)
47
dump_file = open( "dumpfile.txt", 'wb' )
48
ser.write(b'ID\r')
49
print("ID %s", wait_serial(ser))
50

    
51
ser.write(START_MCP)
52
data = wait_serial(ser)
53
print("Command %s data %s" % (START_MCP, data))
54

    
55
for i in range( 0, int(0xffffff / 256)):
56
    addr2 = ((i * 256) >> 16) & 0xff
57
    addr1 = ((i * 256) >> 8) & 0xff
58
    addr0 = (i * 256) & 0xff
59
    print( "reading address %s %s %s" % (hex(addr2), hex(addr1), hex(addr0)))
60
    cmd = struct.pack('>cBBBB', b'R', addr2, addr1, addr0, 0)
61
    ser.write(cmd)
62
    data = wait_serial(ser) # get data
63
    ser.write(MCP_SUCCESS)
64
    status = ser.read(1)
65
    print("Command %s data %s status %s" % (cmd, data, status))
66
    dump_file.write( data[5:] )
67

    
68
ser.write(STOP_MCP)
69
data = wait_serial(ser)
70
print("Command %s data %s" % (STOP_MCP, data))
71

    
72
dump_file.close()
73
ser.close()
74