X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fatsha204a%2Fpd.py;h=950fec1da3b3bad33436b825dc4ebd0f3e5609ee;hp=c2289facab88eecb1536f3859533830cce741bcc;hb=8968f2fe6d561f48608a6f03e43045e10b99a689;hpb=f96a96022ca3ce7b98320db95608323d289c6dc1 diff --git a/decoders/atsha204a/pd.py b/decoders/atsha204a/pd.py index c2289fa..950fec1 100644 --- a/decoders/atsha204a/pd.py +++ b/decoders/atsha204a/pd.py @@ -116,122 +116,133 @@ class Decoder(srd.Decoder): self.out_ann = self.register(srd.OUTPUT_ANN) def output_tx_bytes(self): - if len(self.bytes) < 1: # Ignore wakeup. + b = self.bytes + if len(b) < 1: # Ignore wakeup. return - self.waddr = self.bytes[0][2] - self.display_waddr(self.bytes[0]) + self.waddr = b[0][2] + self.display_waddr(b[0]) if self.waddr == WORD_ADDR_COMMAND: - count = self.bytes[1][2] - self.display_count(self.bytes[1]) - if len(self.bytes) - 1 != count: - self.display_warning(self.bytes[0][0], self.bytes[-1][1], + count = b[1][2] + self.display_count(b[1]) + if len(b) - 1 != count: + self.display_warning(b[0][0], b[-1][1], 'Invalid frame length: Got {}, expecting {} '.format( - len(self.bytes) - 1, count)) + len(b) - 1, count)) return - self.opcode = self.bytes[2][2] - self.display_opcode(self.bytes[2]) - self.display_param1(self.bytes[3]) - self.display_param2([self.bytes[4], self.bytes[5]]) - self.display_data(self.bytes[6:-2]) - self.display_crc([self.bytes[-2], self.bytes[-1]]) + self.opcode = b[2][2] + self.display_opcode(b[2]) + self.display_param1(b[3]) + self.display_param2([b[4], b[5]]) + self.display_data(b[6:-2]) + self.display_crc([b[-2], b[-1]]) def output_rx_bytes(self): - count = self.bytes[0][2] - self.display_count(self.bytes[0]) + b = self.bytes + count = b[0][2] + self.display_count(b[0]) if self.waddr == WORD_ADDR_RESET: - self.display_data([self.bytes[1]]) - self.display_crc([self.bytes[2], self.bytes[3]]) - self.display_status(self.bytes[0][0], self.bytes[-1][1], self.bytes[1][2]) + self.display_data([b[1]]) + self.display_crc([b[2], b[3]]) + self.display_status(b[0][0], b[-1][1], b[1][2]) elif self.waddr == WORD_ADDR_COMMAND: if count == 4: # Status / Error. - self.display_data([self.bytes[1]]) - self.display_crc([self.bytes[2], self.bytes[3]]) - self.display_status(self.bytes[0][0], self.bytes[-1][1], self.bytes[1][2]) + self.display_data([b[1]]) + self.display_crc([b[2], b[3]]) + self.display_status(b[0][0], b[-1][1], b[1][2]) else: - self.display_data(self.bytes[1:-2]) - self.display_crc([self.bytes[-2], self.bytes[-1]]) + self.display_data(b[1:-2]) + self.display_crc([b[-2], b[-1]]) - def display_waddr(self, data): - self.put(data[0], data[1], self.out_ann, [0, ['Word addr: %s' % WORD_ADDR[data[2]]]]) + def putx(self, s, data): + self.put(s[0], s[1], self.out_ann, data) - def display_count(self, data): - self.put(data[0], data[1], self.out_ann, [1, ['Count: %s' % data[2]]]) + def puty(self, s, data): + self.put(s[0][0], s[1][1], self.out_ann, data) - def display_opcode(self, data): - self.put(data[0], data[1], self.out_ann, [2, ['Opcode: %s' % OPCODES[data[2]]]]) + def putz(self, ss, es, data): + self.put(ss, es, self.out_ann, data) - def display_param1(self, data): - if (self.opcode == OPCODE_CHECK_MAC) or (self.opcode == OPCODE_DEV_REV) or \ - (self.opcode == OPCODE_HMAC) or (self.opcode == OPCODE_MAC) or \ - (self.opcode == OPCODE_NONCE) or (self.opcode == OPCODE_RANDOM) or \ - (self.opcode == OPCODE_SHA): - self.put(data[0], data[1], self.out_ann, [3, ['Mode: %02X' % data[2]]]) - elif self.opcode == OPCODE_DERIVE_KEY: - self.put(data[0], data[1], self.out_ann, [3, ['Random: %s' % data[2]]]) - elif self.opcode == OPCODE_GEN_DIG: - self.put(data[0], data[1], self.out_ann, [3, ['Zone: %s' % ZONES[data[2]]]]) - elif self.opcode == OPCODE_LOCK: - self.put(data[0], data[1], self.out_ann, [3, ['Zone: {}, Summary: {}'.format( - 'DATA/OTP' if data[2] else 'CONFIG', - 'Ignored' if data[2] & 0x80 else 'Used')]]) - elif self.opcode == OPCODE_PAUSE: - self.put(data[0], data[1], self.out_ann, [3, ['Selector: %02X' % data[2]]]) - elif self.opcode == OPCODE_READ: - self.put(data[0], data[1], self.out_ann, [3, ['Zone: {}, Length: {}'.format(ZONES[data[2] & 0x03], - '32 bytes' if data[2] & 0x90 else '4 bytes')]]) - elif self.opcode == OPCODE_WRITE: - self.put(data[0], data[1], self.out_ann, [3, ['Zone: {}, Encrypted: {}, Length: {}'.format(ZONES[data[2] & 0x03], - 'Yes' if data[2] & 0x40 else 'No', '32 bytes' if data[2] & 0x90 else '4 bytes')]]) + def display_waddr(self, s): + self.putx(s, [0, ['Word addr: %s' % WORD_ADDR[s[2]]]]) + + def display_count(self, s): + self.putx(s, [1, ['Count: %s' % s[2]]]) + + def display_opcode(self, s): + self.putx(s, [2, ['Opcode: %s' % OPCODES[s[2]]]]) + + def display_param1(self, s): + op = self.opcode + if op in (OPCODE_CHECK_MAC, OPCODE_DEV_REV, OPCODE_HMAC, \ + OPCODE_MAC, OPCODE_NONCE, OPCODE_RANDOM, OPCODE_SHA): + self.putx(s, [3, ['Mode: %02X' % s[2]]]) + elif op == OPCODE_DERIVE_KEY: + self.putx(s, [3, ['Random: %s' % s[2]]]) + elif op == OPCODE_GEN_DIG: + self.putx(s, [3, ['Zone: %s' % ZONES[s[2]]]]) + elif op == OPCODE_LOCK: + self.putx(s, [3, ['Zone: {}, Summary: {}'.format( + 'DATA/OTP' if s[2] else 'CONFIG', + 'Ignored' if s[2] & 0x80 else 'Used')]]) + elif op == OPCODE_PAUSE: + self.putx(s, [3, ['Selector: %02X' % s[2]]]) + elif op == OPCODE_READ: + self.putx(s, [3, ['Zone: {}, Length: {}'.format(ZONES[s[2] & 0x03], + '32 bytes' if s[2] & 0x90 else '4 bytes')]]) + elif op == OPCODE_WRITE: + self.putx(s, [3, ['Zone: {}, Encrypted: {}, Length: {}'.format(ZONES[s[2] & 0x03], + 'Yes' if s[2] & 0x40 else 'No', '32 bytes' if s[2] & 0x90 else '4 bytes')]]) else: - self.put(data[0], data[1], self.out_ann, [3, ['Param1: %02X' % data[2]]]) + self.putx(s, [3, ['Param1: %02X' % s[2]]]) - def display_param2(self, data): - if self.opcode == OPCODE_DERIVE_KEY: - self.put(data[0][0], data[1][1], self.out_ann, [4, ['TargetKey: {:02x} {:02x}'.format(data[1][2], data[0][2])]]) - elif (self.opcode == OPCODE_NONCE) or (self.opcode == OPCODE_PAUSE) or (self.opcode == OPCODE_RANDOM): - self.put(data[0][0], data[1][1], self.out_ann, [4, ['Zero: {:02x} {:02x}'.format(data[1][2], data[0][2])]]) - elif (self.opcode == OPCODE_HMAC) or (self.opcode == OPCODE_MAC) or \ - (self.opcode == OPCODE_CHECK_MAC) or (self.opcode == OPCODE_GEN_DIG): - self.put(data[0][0], data[1][1], self.out_ann, [4, ['SlotID: {:02x} {:02x}'.format(data[1][2], data[0][2])]]) - elif self.opcode == OPCODE_LOCK: - self.put(data[0][0], data[1][1], self.out_ann, [4, ['Summary: {:02x} {:02x}'.format(data[1][2], data[0][2])]]) - elif (self.opcode == OPCODE_READ) or (self.opcode == OPCODE_WRITE): - self.put(data[0][0], data[1][1], self.out_ann, [4, ['Address: {:02x} {:02x}'.format(data[1][2], data[0][2])]]) - elif self.opcode == OPCODE_UPDATE_EXTRA: - self.put(data[0][0], data[1][1], self.out_ann, [4, ['NewValue: {:02x}'.format(data[0][2])]]) + def display_param2(self, s): + op = self.opcode + if op == OPCODE_DERIVE_KEY: + self.puty(s, [4, ['TargetKey: {:02x} {:02x}'.format(s[1][2], s[0][2])]]) + elif op in (OPCODE_NONCE, OPCODE_PAUSE, OPCODE_RANDOM): + self.puty(s, [4, ['Zero: {:02x} {:02x}'.format(s[1][2], s[0][2])]]) + elif op in (OPCODE_HMAC, OPCODE_MAC, OPCODE_CHECK_MAC, OPCODE_GEN_DIG): + self.puty(s, [4, ['SlotID: {:02x} {:02x}'.format(s[1][2], s[0][2])]]) + elif op == OPCODE_LOCK: + self.puty(s, [4, ['Summary: {:02x} {:02x}'.format(s[1][2], s[0][2])]]) + elif op in (OPCODE_READ, OPCODE_WRITE): + self.puty(s, [4, ['Address: {:02x} {:02x}'.format(s[1][2], s[0][2])]]) + elif op == OPCODE_UPDATE_EXTRA: + self.puty(s, [4, ['NewValue: {:02x}'.format(s[0][2])]]) else: - self.put(data[0][0], data[1][1], self.out_ann, [4, ['-']]) + self.puty(s, [4, ['-']]) - def display_data(self, data): - if len(data) == 0: + def display_data(self, s): + if len(s) == 0: return - if self.opcode == OPCODE_CHECK_MAC: - self.put(data[0][0], data[31][1], self.out_ann, [5, ['ClientChal: %s' % ' '.join(format(i[2], '02x') for i in data[0:31])]]) - self.put(data[32][0], data[63][1], self.out_ann, [5, ['ClientResp: %s' % ' '.join(format(i[2], '02x') for i in data[32:63])]]) - self.put(data[64][0], data[76][1], self.out_ann, [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in data[64:76])]]) - elif self.opcode == OPCODE_DERIVE_KEY: - self.put(data[0][0], data[31][1], self.out_ann, [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in data)]]) - elif self.opcode == OPCODE_GEN_DIG: - self.put(data[0][0], data[3][1], self.out_ann, [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in data)]]) - elif self.opcode == OPCODE_MAC: - self.put(data[0][0], data[31][1], self.out_ann, [5, ['Challenge: %s' % ' '.join(format(i[2], '02x') for i in data)]]) - elif self.opcode == OPCODE_WRITE: - if len(data) > 32: # Value + MAC. - self.put(data[0][0], data[-31][1], self.out_ann, [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in data)]]) - self.put(data[-32][0], data[-1][1], self.out_ann, [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in data)]]) + op = self.opcode + if op == OPCODE_CHECK_MAC: + self.putz(s[0][0], s[31][1], [5, ['ClientChal: %s' % ' '.join(format(i[2], '02x') for i in s[0:31])]]) + self.putz(s[32][0], s[63][1], [5, ['ClientResp: %s' % ' '.join(format(i[2], '02x') for i in s[32:63])]]) + self.putz(s[64][0], s[76][1], [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in s[64:76])]]) + elif op == OPCODE_DERIVE_KEY: + self.putz(s[0][0], s[31][1], [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in s)]]) + elif op == OPCODE_GEN_DIG: + self.putz(s[0][0], s[3][1], [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in s)]]) + elif op == OPCODE_MAC: + self.putz(s[0][0], s[31][1], [5, ['Challenge: %s' % ' '.join(format(i[2], '02x') for i in s)]]) + elif op == OPCODE_WRITE: + if len(s) > 32: # Value + MAC. + self.putz(s[0][0], s[-31][1], [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in s)]]) + self.putz(s[-32][0], s[-1][1], [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in s)]]) else: # Just value. - self.put(data[0][0], data[-1][1], self.out_ann, [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in data)]]) + self.putz(s[0][0], s[-1][1], [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in s)]]) else: - self.put(data[0][0], data[-1][1], self.out_ann, [5, ['Data: %s' % ' '.join(format(i[2], '02x') for i in data)]]) + self.putz(s[0][0], s[-1][1], [5, ['Data: %s' % ' '.join(format(i[2], '02x') for i in s)]]) - def display_crc(self, data): - self.put(data[0][0], data[1][1], self.out_ann, [6, ['CRC: {:02X} {:02X}'.format(data[0][2], data[1][2])]]) + def display_crc(self, s): + self.puty(s, [6, ['CRC: {:02X} {:02X}'.format(s[0][2], s[1][2])]]) - def display_status(self, start, end, status): - self.put(start, end, self.out_ann, [7, ['Status: %s' % STATUS[status]]]) + def display_status(self, ss, es, status): + self.putz(ss, es, [7, ['Status: %s' % STATUS[status]]]) - def display_warning(self, start, end, msg): - self.put(start, end, self.out_ann, [8, ['Warning: %s' % msg]]) + def display_warning(self, ss, es, msg): + self.putz(ss, es, [8, ['Warning: %s' % msg]]) def decode(self, ss, es, data): cmd, databyte = data