RAFlasher.py: signature request info
[renesas-ra-flasher] / src / RAPacker.py
1 # Copyright (C) Robin Krens - 2024
2
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 #
17
18 import struct
19
20 # Commands send to boot firmware
21 INQ_CMD = 0x00
22 ERA_CMD = 0x12
23 WRI_CMD = 0x13
24 REA_CMD = 0x15
25 IDA_CMD = 0x30
26 BAU_CMD = 0x34
27 SIG_CMD = 0x3A
28 ARE_CMD = 0x3B
29
30 # These are combined with send command, for example
31 # STATUS_OK | ERA_CMD == 0x12
32 # STATUS_ERR | ERA_CMD = 0x92
33 STATUS_OK = 0x00
34 STATUS_ERR = 0x80
35
36 # Error codes
37 error_codes = {
38     0xC: "ERR_UNSU",
39     0xC1: "ERR_PCKT",
40     0xC2: "ERR_CHKS",
41     0xC3: "ERR_FLOW",
42     0xD0: "ERR_ADDR",
43     0xD4: "ERR_BAUD",
44     0xDA: "ERR_PROT",
45     0xDB: "ERR_ID",
46     0xDC: "ERR_SERI",
47     0xE1: "ERR_ERA",
48     0xE2: "ERR_WRI",
49     0xE7: "ERR_SEQ"
50 }
51
52 # used for init sequence
53 LOW_PULSE = 0x00
54 GENERIC_CODE = 0x55
55 BOOT_CODE = 0xC3
56
57 TESTID = [
58     "0xF0", "0xF1", "0xF2", "0xF3",
59     "0xE4", "0xE5", "0xE6", "0xE7",
60     "0xD8", "0xD9", "0xDA", "0xDB",
61     "0xCC", "0xCD", "0xCE", "0xCF"
62 ]
63
64 def calc_sum(cmd, data):
65     data_len = len(data)
66     lnh = (data_len + 1 & 0xFF00) >> 8
67     lnl = data_len + 1 & 0x00FF
68     res = lnh + lnl + cmd
69     for i in range(data_len):
70             if isinstance(data[i], str):
71                 res += int(data[i], 16)
72             elif isinstance(data[i], int):
73                 res += data[i]
74             else:
75                 res += ord(data[i])
76     res = ~(res - 1) & 0xFF # two's complement
77     return (lnh, lnl, res)
78
79
80 # format of data packet is [SOD|LNH|LNL|COM|byte_data|SUM|ETX]
81 def pack_command(cmd, data):
82     SOD = 0x01
83     COM = cmd
84
85     if isinstance(data, str):
86         byte_data = bytes(data.encode('utf-8'))
87     else:
88         byte_data = bytes([int(x, 16) for x in data])
89     
90     LNH, LNL, SUM = calc_sum(int(cmd), data)
91     ETX = 0x03
92     fmt_header = '<BBBB'
93     fmt_footer = 'BB'
94     fmt = fmt_header + str(len(data)) + 's' + fmt_footer
95     pack = struct.pack(fmt, SOD, LNH, LNL, COM, byte_data, SUM, ETX)
96     print(fmt, pack, len(pack))
97     return fmt
98
99 # format of data packet is [SOD|LNH|LNL|RES|DAT|SUM|ETX]
100 def pack_pkt(res, data):
101     SOD = 0x01 # TODO: check if 0x81 header needed
102     if (len(data) > 1024):
103         raise Exception(f'Data packet too large, data length is {DATA_LEN} (>1024)')
104     LNH, LNL, SUM = calc_sum(int(res), data)
105     if not isinstance(data, bytes):
106         DAT = bytes([int(x, 16) for x in data])
107     else:
108         DAT = data
109     RES = res
110     ETX = 0x03
111     fmt_header = '<BBBB'
112     fmt_footer = 'BB'
113     fmt = fmt_header + str(len(data)) + 's' + fmt_footer
114     pack = struct.pack(fmt, SOD, LNH, LNL, RES, DAT, SUM, ETX)
115     return pack
116
117 # packet received from mcu 
118 def unpack_pkt(data):
119     header = data[0:4]
120     fmt_header = '<BBBB'
121     SOD, LNH, LNL, RES = struct.unpack(fmt_header, header)
122     if (SOD != 0x81):
123         raise Exception(f'Wrong start of packet data received')
124     pkt_len = (LNH << 0x8 | LNL) - 1
125     fmt_message = '<' + str(pkt_len) + 's'
126     raw = struct.unpack_from(fmt_message, data, 4)[0]
127     message = ['0x{:02X}'.format(byte) for byte in raw]
128     if (RES & 0x80):
129         raise ValueError(f'MCU encountered error {message[0]}')
130     fmt_footer = '<BB'
131     SUM, ETX = struct.unpack_from(fmt_footer, data, 4 + pkt_len)
132     lnh, lnl, local_sum = calc_sum(RES, message)
133     if (SUM != local_sum):
134         raise Exception(f'Sum calculation mismatch, read {SUM} instead of {local_sum}')
135     if (ETX != 0x03):
136         raise Exception(f'Packet ETX error')
137     return message
138     
139
140 #cmd = pack_command(INQ_CMD, "")
141 #cmd = pack_command(BAU_CMD, ['0x00','0x1E'])
142 #cmd = pack_command(IDA_CMD, TESTID)
143