2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2017 Kevin Redon <kingkevin@cuvoodoo.info>
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode as srd
22 # Dictionary of FUNCTION commands and their names.
24 0x0f: 'Write scratchpad',
25 0xaa: 'Read scratchpad',
26 0x55: 'Copy scratchpad',
28 0x5a: 'Load first secret',
29 0x33: 'Compute next secret',
30 0xa5: 'Read authenticated page',
33 # Maxim DS2432 family code, present at the end of the ROM code.
36 # Calculate the CRC-16 checksum.
37 # Initial value: 0x0000, xor-in: 0x0000, polynom 0x8005, xor-out: 0xffff.
38 def crc16(byte_array):
39 reverse = 0xa001 # Use the reverse polynom to make algo simpler.
40 crc = 0x0000 # Initial value.
41 # Reverse CRC calculation.
42 for byte in byte_array:
45 crc = (crc >> 1) ^ reverse
49 crc ^= 0xffff # Invert CRC.
52 class Decoder(srd.Decoder):
56 longname = 'Maxim DS2432 1-Wire 1k-Bit Protected EEPROM with SHA-1 Engine'
57 desc = '1-Wire 1k-Bit Protected EEPROM with SHA-1 Engine.'
59 inputs = ['onewire_network']
62 ('text', 'Human-readable text'),
66 # Bytes for function command.
70 self.out_ann = self.register(srd.OUTPUT_ANN)
73 self.put(self.ss, self.es, self.out_ann, data)
75 def decode(self, ss, es, data):
78 if code == 'RESET/PRESENCE':
79 self.ss, self.es = ss, es
80 self.putx([0, ['Reset/presence: %s'
81 % ('true' if val else 'false')]])
84 self.ss, self.es = ss, es
85 self.putx([0, ['ROM: 0x%016x (family code %s to 0x%02x)'
86 % (val, 'matches' if family_code == (val & 0xff)
87 else 'does not match', family_code),
88 'ROM: 0x%016x (family code %s)'
89 % (val, 'match' if family_code == (val & 0xff)
93 self.bytes.append(val)
94 if 1 == len(self.bytes):
95 self.ss, self.es = ss, es
96 if val not in commands:
97 self.putx([0, ['Unrecognized command: 0x%02x' % val]])
99 self.putx([0, ['Function command: %s (0x%02x)'
100 % (commands[val], val)]])
101 elif 0x0f == self.bytes[0]: # Write scratchpad
102 if 2 == len(self.bytes):
104 elif 3 == len(self.bytes):
106 self.putx([0, ['Target address: 0x%04x'
107 % ((self.bytes[2] << 8) + self.bytes[1])]])
108 elif 4 == len(self.bytes):
110 elif 11 == len(self.bytes):
112 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
113 for n in self.bytes[3:11]))]])
114 elif 12 == len(self.bytes):
116 elif 13 == len(self.bytes):
118 self.putx([0, ['CRC: '
119 + ('ok' if crc16(self.bytes[0:11]) == (self.bytes[11]
120 + (self.bytes[12] << 8)) else 'error')]])
121 elif 0xaa == self.bytes[0]: # Read scratchpad
122 if 2 == len(self.bytes):
124 elif 3 == len(self.bytes):
126 self.putx([0, ['Target address: 0x%04x'
127 % ((self.bytes[2] << 8) + self.bytes[1])]])
128 elif 4 == len(self.bytes):
129 self.ss, self.es = ss, es
130 self.putx([0, ['Data status (E/S): 0x%02x'
132 elif 5 == len(self.bytes):
134 elif 12 == len(self.bytes):
136 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
137 for n in self.bytes[4:12]))]])
138 elif 13 == len(self.bytes):
140 elif 14 == len(self.bytes):
142 self.putx([0, ['CRC: '
143 + ('ok' if crc16(self.bytes[0:12]) == (self.bytes[12]
144 + (self.bytes[13] << 8)) else 'error')]])
145 elif 0x5a == self.bytes[0]: # Load first secret
146 if 2 == len(self.bytes):
148 elif 4 == len(self.bytes):
150 self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
151 + (','.join(format(n, '#04x')
152 for n in self.bytes[1:4]))]])
153 elif 4 < len(self.bytes):
154 self.ss, self.es = ss, es
155 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
156 self.putx([0, ['End of operation']])
157 elif 0x33 == self.bytes[0]: # Compute next secret
158 if 2 == len(self.bytes):
160 elif 3 == len(self.bytes):
162 self.putx([0, ['Target address: 0x%04x'
163 % ((self.bytes[2] << 8) + self.bytes[1])]])
164 elif 3 < len(self.bytes):
165 self.ss, self.es = ss, es
166 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
167 self.putx([0, ['End of operation']])
168 elif 0x55 == self.bytes[0]: # Copy scratchpad
169 if 2 == len(self.bytes):
171 elif 4 == len(self.bytes):
173 self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
174 + (','.join(format(n, '#04x')
175 for n in self.bytes[1:4]))]])
176 elif 5 == len(self.bytes):
178 elif 24 == len(self.bytes):
180 self.putx([0, ['Message authentication code: '
181 + (','.join(format(n, '#04x')
182 for n in self.bytes[4:24])),
184 + (','.join(format(n, '#04x')
185 for n in self.bytes[4:24])),
187 elif 24 < len(self.bytes):
188 self.ss, self.es = ss, es
189 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
190 self.putx([0, ['Operation succeeded']])
191 elif (0 == self.bytes[-1]):
192 self.putx([0, ['Operation failed']])
193 elif 0xa5 == self.bytes[0]: # Read authenticated page
194 if 2 == len(self.bytes):
196 elif 3 == len(self.bytes):
198 self.putx([0, ['Target address: 0x%04x'
199 % ((self.bytes[2] << 8) + self.bytes[1])]])
200 elif 4 == len(self.bytes):
202 elif 35 == len(self.bytes):
204 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
205 for n in self.bytes[3:35]))]])
206 elif 36 == len(self.bytes):
207 self.ss, self.es = ss, es
208 self.putx([0, ['Padding: '
209 + ('ok' if 0xff == self.bytes[-1] else 'error')]])
210 elif 37 == len(self.bytes):
212 elif 38 == len(self.bytes):
214 self.putx([0, ['CRC: '
215 + ('ok' if crc16(self.bytes[0:36]) == (self.bytes[36]
216 + (self.bytes[37] << 8)) else 'error')]])
217 elif 39 == len(self.bytes):
219 elif 58 == len(self.bytes):
221 self.putx([0, ['Message authentication code: '
222 + (','.join(format(n, '#04x')
223 for n in self.bytes[38:58])),
225 + (','.join(format(n, '#04x')
226 for n in self.bytes[38:58])),
228 elif 59 == len(self.bytes):
230 elif 60 == len(self.bytes):
232 self.putx([0, ['MAC CRC: '
233 + ('ok' if crc16(self.bytes[38:58]) == (self.bytes[58]
234 + (self.bytes[59] << 8)) else 'error')]])
235 elif 60 < len(self.bytes):
236 self.ss, self.es = ss, es
237 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
238 self.putx([0, ['Operation completed']])
239 elif 0xf0 == self.bytes[0]: # Read memory
240 if 2 == len(self.bytes):
242 elif 3 == len(self.bytes):
244 self.putx([0, ['Target address: 0x%04x'
245 % ((self.bytes[2] << 8) + self.bytes[1])]])
246 elif 3 < len(self.bytes):
247 self.ss, self.es = ss, es
248 self.putx([0, ['Data: 0x%02x' % (self.bytes[-1])]])