Skip to content

Commit 40b6240

Browse files
committed
ECC508: add support for reading and writing slots
1 parent 75b1aae commit 40b6240

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Diff for: src/utility/ECC508.cpp

+47-1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,44 @@ int ECC508Class::ecSign(int slot, const byte message[], byte signature[])
173173
return 1;
174174
}
175175

176+
int ECC508Class::readSlot(int slot, byte data[], int length)
177+
{
178+
if (slot < 0 || slot > 15) {
179+
return -1;
180+
}
181+
182+
if (length % 4 != 0) {
183+
return 0;
184+
}
185+
186+
for (int i = 0; i < length; i += 4) {
187+
if (!read(2, addressForSlotOffset(slot, i), &data[i], 4)) {
188+
return 0;
189+
}
190+
}
191+
192+
return 1;
193+
}
194+
195+
int ECC508Class::writeSlot(int slot, const byte data[], int length)
196+
{
197+
if (slot < 0 || slot > 15) {
198+
return -1;
199+
}
200+
201+
if (length % 4 != 0) {
202+
return 0;
203+
}
204+
205+
for (int i = 0; i < length; i += 4) {
206+
if (!write(2, addressForSlotOffset(slot, i), &data[i], 4)) {
207+
return 0;
208+
}
209+
}
210+
211+
return 1;
212+
}
213+
176214
int ECC508Class::locked()
177215
{
178216
byte config[4];
@@ -476,6 +514,14 @@ int ECC508Class::lock(int zone)
476514
return 1;
477515
}
478516

517+
int ECC508Class::addressForSlotOffset(int slot, int offset)
518+
{
519+
int block = offset / 32;
520+
offset = (offset % 32) / 4;
521+
522+
return (slot << 3) | (block << 8) | (offset);
523+
}
524+
479525
int ECC508Class::sendCommand(uint8_t opcode, uint8_t param1, uint16_t param2, const byte data[], size_t dataLength)
480526
{
481527
int commandLength = 8 + dataLength; // 1 for type, 1 for length, 1 for opcode, 1 for param1, 2 for param2, 2 for crc
@@ -502,7 +548,7 @@ int ECC508Class::sendCommand(uint8_t opcode, uint8_t param1, uint16_t param2, co
502548

503549
int ECC508Class::receiveResponse(void* response, size_t length)
504550
{
505-
int retries = 20;
551+
int retries = 25;
506552
int responseSize = length + 3; // 1 for length header, 2 for CRC
507553
byte responseBuffer[responseSize];
508554

Diff for: src/utility/ECC508.h

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class ECC508Class
2323
int ecdsaVerify(const byte message[], const byte signature[], const byte pubkey[]);
2424
int ecSign(int slot, const byte message[], byte signature[]);
2525

26+
int readSlot(int slot, byte data[], int length);
27+
int writeSlot(int slot, const byte data[], int length);
28+
2629
int locked();
2730
int writeConfiguration(const byte data[]);
2831
int readConfiguration(byte data[]);
@@ -42,6 +45,8 @@ class ECC508Class
4245
int write(int zone, int address, const byte buffer[], int length);
4346
int lock(int zone);
4447

48+
int addressForSlotOffset(int slot, int offset);
49+
4550
int sendCommand(uint8_t opcode, uint8_t param1, uint16_t param2, const byte data[] = NULL, size_t dataLength = 0);
4651
int receiveResponse(void* response, size_t length);
4752
uint16_t crc16(const byte data[], size_t length);

0 commit comments

Comments
 (0)