summaryrefslogtreecommitdiff
path: root/decoders/ds243x/pd.py
blob: 7f9f6660cc86ec2293c866b4ecf04d50e05a2d12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2017 Kevin Redon <kingkevin@cuvoodoo.info>
## Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##

import sigrokdecode as srd

# Dictionary of FUNCTION commands and their names.
commands_2432 = {
    0x0f: 'Write scratchpad',
    0xaa: 'Read scratchpad',
    0x55: 'Copy scratchpad',
    0xf0: 'Read memory',
    0x5a: 'Load first secret',
    0x33: 'Compute next secret',
    0xa5: 'Read authenticated page',
}

commands_2433 = {
    0x0f: 'Write scratchpad',
    0xaa: 'Read scratchpad',
    0x55: 'Copy scratchpad',
    0xf0: 'Read memory',
}

# Maxim DS243x family code, present at the end of the ROM code.
family_codes = {
    0x33: ('DS2432', commands_2432),
    0x23: ('DS2433', commands_2433),
}

# Calculate the CRC-16 checksum.
# Initial value: 0x0000, xor-in: 0x0000, polynom 0x8005, xor-out: 0xffff.
def crc16(byte_array):
    reverse = 0xa001 # Use the reverse polynom to make algo simpler.
    crc = 0x0000 # Initial value.
    # Reverse CRC calculation.
    for byte in byte_array:
        for bit in range(8):
            if (byte ^ crc) & 1:
                crc = (crc >> 1) ^ reverse
            else:
                crc >>= 1
            byte >>= 1
    crc ^= 0xffff # Invert CRC.
    return crc

class Decoder(srd.Decoder):
    api_version = 3
    id = 'ds243x'
    name = 'DS243x'
    longname = 'Maxim DS2432/3'
    desc = 'Maxim DS243x series 1-Wire EEPROM protocol.'
    license = 'gplv2+'
    inputs = ['onewire_network']
    outputs = []
    tags = ['IC', 'Memory']
    annotations = (
        ('text', 'Human-readable text'),
    )
    binary = (
        ('mem_read', 'Data read from memory'),
    )

    def __init__(self):
        self.reset()

    def reset(self):
        # Bytes for function command.
        self.bytes = []
        self.family_code = None
        self.family = ''
        self.commands = commands_2432 # Use max command set until we know better.

    def start(self):
        self.out_ann = self.register(srd.OUTPUT_ANN)
        self.out_binary = self.register(srd.OUTPUT_BINARY)

    def putx(self, data):
        self.put(self.ss, self.es, self.out_ann, data)

    def decode(self, ss, es, data):
        code, val = data

        if code == 'RESET/PRESENCE':
            self.ss, self.es = ss, es
            self.putx([0, ['Reset/presence: %s'
                           % ('true' if val else 'false')]])
            self.bytes = []
        elif code == 'ROM':
            self.ss, self.es = ss, es
            self.family_code = val & 0xff

            s = None
            if self.family_code in family_codes:
                self.family, self.commands = family_codes[val & 0xff]
                s = 'is 0x%02x, %s detected' % (self.family_code, self.family)
            else:
                s = '0x%02x unknown' % (self.family_code)

            self.putx([0, ['ROM: 0x%016x (%s)' % (val, 'family code ' + s),
                           'ROM: 0x%016x (%s)' % (val, self.family)]])
            self.bytes = []
        elif code == 'DATA':
            self.bytes.append(val)
            if 1 == len(self.bytes):
                self.ss, self.es = ss, es
                if val not in self.commands:
                    self.putx([0, ['Unrecognized command: 0x%02x' % val]])
                else:
                    self.putx([0, ['Function command: %s (0x%02x)'
                                   % (self.commands[val], val)]])
            elif 0x0f == self.bytes[0]: # Write scratchpad
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 3 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Target address: 0x%04x'
                                   % ((self.bytes[2] << 8) + self.bytes[1])]])
                elif 4 == len(self.bytes):
                    self.ss = ss
                elif 11 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
                                       for n in self.bytes[3:11]))]])
                elif 12 == len(self.bytes):
                    self.ss = ss
                elif 13 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['CRC: '
                        + ('ok' if crc16(self.bytes[0:11]) == (self.bytes[11]
                        + (self.bytes[12] << 8)) else 'error')]])
            elif 0xaa == self.bytes[0]: # Read scratchpad
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 3 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Target address: 0x%04x'
                                   % ((self.bytes[2] << 8) + self.bytes[1])]])
                elif 4 == len(self.bytes):
                    self.ss, self.es = ss, es
                    self.putx([0, ['Data status (E/S): 0x%02x'
                                   % (self.bytes[3])]])
                elif 5 == len(self.bytes):
                    self.ss = ss
                elif 12 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
                                       for n in self.bytes[4:12]))]])
                elif 13 == len(self.bytes):
                    self.ss = ss
                elif 14 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['CRC: '
                        + ('ok' if crc16(self.bytes[0:12]) == (self.bytes[12]
                        + (self.bytes[13] << 8)) else 'error')]])
            elif 0x5a == self.bytes[0]: # Load first secret
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 4 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
                        + (','.join(format(n, '#04x')
                            for n in self.bytes[1:4]))]])
                elif 4 < len(self.bytes):
                    self.ss, self.es = ss, es
                    if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
                        self.putx([0, ['End of operation']])
            elif 0x33 == self.bytes[0]: # Compute next secret
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 3 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Target address: 0x%04x'
                                   % ((self.bytes[2] << 8) + self.bytes[1])]])
                elif 3 < len(self.bytes):
                    self.ss, self.es = ss, es
                    if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
                        self.putx([0, ['End of operation']])
            elif 0x55 == self.bytes[0]: # Copy scratchpad
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 4 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
                        + (','.join(format(n, '#04x')
                            for n in self.bytes[1:4]))]])
                elif 5 == len(self.bytes):
                    self.ss = ss
                elif 24 == len(self.bytes):
                    self.es = es
                    mac = ','.join(format(n, '#04x') for n in self.bytes[4:24])
                    self.putx([0, ['Message authentication code: ' + mac,
                                   'MAC: ' + mac]])
                elif 24 < len(self.bytes):
                    self.ss, self.es = ss, es
                    if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
                        self.putx([0, ['Operation succeeded']])
                    elif (0 == self.bytes[-1]):
                        self.putx([0, ['Operation failed']])
            elif 0xa5 == self.bytes[0]: # Read authenticated page
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 3 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Target address: 0x%04x'
                                   % ((self.bytes[2] << 8) + self.bytes[1])]])
                elif 4 == len(self.bytes):
                    self.ss = ss
                elif 35 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
                                       for n in self.bytes[3:35]))]])
                elif 36 == len(self.bytes):
                    self.ss, self.es = ss, es
                    self.putx([0, ['Padding: '
                        + ('ok' if 0xff == self.bytes[-1] else 'error')]])
                elif 37 == len(self.bytes):
                    self.ss = ss
                elif 38 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['CRC: '
                        + ('ok' if crc16(self.bytes[0:36]) == (self.bytes[36]
                        + (self.bytes[37] << 8)) else 'error')]])
                elif 39 == len(self.bytes):
                    self.ss = ss
                elif 58 == len(self.bytes):
                    self.es = es
                    mac = ','.join(format(n, '#04x') for n in self.bytes[38:58])
                    self.putx([0, ['Message authentication code: ' + mac,
                                   'MAC: ' + mac]])
                elif 59 == len(self.bytes):
                    self.ss = ss
                elif 60 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['MAC CRC: '
                        + ('ok' if crc16(self.bytes[38:58]) == (self.bytes[58]
                        + (self.bytes[59] << 8)) else 'error')]])
                elif 60 < len(self.bytes):
                    self.ss, self.es = ss, es
                    if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
                        self.putx([0, ['Operation completed']])
            elif 0xf0 == self.bytes[0]: # Read memory
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 3 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Target address: 0x%04x'
                                   % ((self.bytes[2] << 8) + self.bytes[1])]])
                elif 3 < len(self.bytes):
                    self.ss, self.es = ss, es
                    self.putx([0, ['Data: 0x%02x' % (self.bytes[-1])]])

                    bdata = self.bytes[-1].to_bytes(1, byteorder='big')
                    self.put(ss, es, self.out_binary, [0, bdata])