1+ #!/usr/bin/env python
2+ import hid
3+ import argparse
4+ from at_kit_base import *
5+ import base64
6+
7+ # Default configuration for ATECC508A
8+ atecc508_config = bytearray .fromhex (
9+ 'B0 00 55 00 8F 20 C4 44 87 20 87 20 8F 0F C4 36'
10+ '9F 0F 82 20 0F 0F C4 44 0F 0F 0F 0F 0F 0F 0F 0F'
11+ '0F 0F 0F 0F FF FF FF FF 00 00 00 00 FF FF FF FF'
12+ '00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF'
13+ 'FF FF FF FF 00 00 55 55 FF FF 00 00 00 00 00 00'
14+ '33 00 1C 00 13 00 13 00 7C 00 1C 00 3C 00 33 00'
15+ '3C 00 3C 00 3C 00 30 00 3C 00 3C 00 3C 00 30 00' )
16+
17+ # Default configuration for ATECC608A
18+ atecc608_config = bytearray .fromhex (
19+ 'B0 00 00 01 8F 20 C4 44 87 20 87 20 8F 0F C4 36'
20+ '9F 0F 82 20 0F 0F C4 44 0F 0F 0F 0F 0F 0F 0F 0F'
21+ '0F 0F 0F 0F FF FF FF FF 00 00 00 00 FF FF FF FF'
22+ '00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00'
23+ '00 00 00 00 00 00 55 55 FF FF 00 00 00 00 00 00'
24+ '33 00 1C 00 13 00 13 00 7C 00 1C 00 3C 00 33 00'
25+ '3C 00 3C 00 3C 00 30 00 3C 00 3C 00 3C 00 30 00' )
26+
27+ def configure_device (i2c_addr = 0xC0 ):
28+ # Initialize the Kit Instance
29+ dev = KitDevice (hid .device ())
30+ dev .open ()
31+
32+ # Get id of the first device
33+ id = dev .kit_list (0 )
34+
35+ # Select the device to communicate with
36+ dev .kit_select (id )
37+
38+ # Get the kit type
39+ print ('\n Getting the Kit Type' )
40+ kit_version = dev .kit_info (0 ).split (' ' )[0 ]
41+ print (' {} Found' .format (kit_version ))
42+
43+ # Check if this is the demo firmware - if not don't brick a CK590
44+ if ('GCP IoT Core Example' != dev .device .get_product_string ()) and ('AT88CK590' == kit_version ) and (0xC0 != i2c_addr ):
45+ print (' Kit only supports I2C Address 0xC0' )
46+ i2c_addr = 0xC0
47+
48+ # Get the device type
49+ print ('\n Getting the Device Info' )
50+ resp = dev .kit_command_resp (0x30 , 0 , 0 , timeout_ms = 5000 )
51+ dev_type = resp [2 ]
52+ print (' Device type is {}08' .format (dev_type >> 4 ))
53+
54+ # Get Current I2C Address
55+ print ('\n Getting the I2C Address' )
56+ resp = dev .kit_command_resp (0x02 , 0 , 0x04 , timeout_ms = 5000 )
57+ print (' Current Address: {:02X}' .format (resp [0 ]))
58+ print (' New Address: {:02X}' .format (i2c_addr ))
59+
60+ # Check the zone locks
61+ print ('\n Reading the Lock Status' )
62+ resp = dev .kit_command_resp (0x02 , 0 , 0x15 , timeout_ms = 5000 )
63+ data_zone_lock = (0x55 != resp [2 ])
64+ config_zone_lock = (0x55 != resp [3 ])
65+ print (' Config Zone: {}' .format ('Locked' if config_zone_lock else 'Unlocked' ))
66+ print (' Data Zone: {}' .format ('Locked' if data_zone_lock else 'Unlocked' ))
67+
68+ # Program the configuration zone
69+ print ('\n Program Configuration' )
70+ if not config_zone_lock :
71+ if 0x50 == dev_type :
72+ print (' Programming ATECC508A Configuration' )
73+ config = atecc508_config
74+ elif 0x60 == dev_type :
75+ print (' Programming ATECC608A Configuration' )
76+ config = atecc608_config
77+ else :
78+ print (' Unknown Device' )
79+ raise ValueError ('Unknown Device Type: {:02X}' .format (dev_type ))
80+
81+ # Update with the target I2C Address
82+ config [0 ] = i2c_addr
83+
84+ # Write configuration
85+ resp = dev .write_bytes (0 , 0 , 16 , config , timeout_ms = 5000 )
86+ print (' Success' )
87+
88+ # Verify Config Zone
89+ print (' Verifying Configuration' )
90+ resp = dev .read_bytes (0 , 0 , 16 , len (config ), timeout_ms = 5000 )
91+
92+ if resp != config :
93+ raise ValueError ('Configuration read from the device does not match' )
94+ print (' Success' )
95+
96+ print (' Locking Configuration' )
97+ resp = dev .kit_command_resp (0x17 , 0x80 , 0 , timeout_ms = 5000 )
98+ print (' Locked' )
99+ else :
100+ print (' Locked, skipping' )
101+
102+ # Check data zone lock
103+ print ('\n Activating Configuration' )
104+ if not data_zone_lock :
105+ # Lock the data zone
106+ resp = dev .kit_command_resp (0x17 , 0x81 , 0 , timeout_ms = 5000 )
107+ print (' Activated' )
108+ else :
109+ print (' Already Active' )
110+
111+ # Generate new keys
112+ print ('\n Generating New Keys' )
113+ resp = dev .kit_command_resp (0x40 , 4 , 0 , timeout_ms = 5000 )
114+ print (' Key 0 Success' )
115+
116+ resp = dev .kit_command_resp (0x40 , 4 , 2 , timeout_ms = 5000 )
117+ print (' Key 2 Success' )
118+
119+ resp = dev .kit_command_resp (0x40 , 4 , 3 , timeout_ms = 5000 )
120+ print (' Key 3 Success' )
121+
122+ resp = dev .kit_command_resp (0x40 , 4 , 7 , timeout_ms = 5000 )
123+ print (' Key 7 Success' )
124+
125+
126+ if __name__ == '__main__' :
127+ parser = argparse .ArgumentParser (description = 'GCP ECC Device Configuration Utility' )
128+ parser .add_argument ('--i2c' , default = '0xB0' , help = 'I2C Address (in hex)' )
129+ args = parser .parse_args ()
130+
131+ configure_device (int (args .i2c ,16 ))
132+ print ('\n Device Successfully Configured' )
133+
0 commit comments