Security personalizer rewritten (#794)
* Removed ability to lock data section * Introduced "guided mode" to simplify process * Refactored the sketch to be less monolithic * Print details and instructions in tables * Use device unique ID as serial when * Calculate and store personalization checksum * Print serial in a copy+paste friendly manner (fixes #804) * Clarify meaning of UART confirmation (fixes #805)
This commit is contained in:
@@ -14,7 +14,6 @@ static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer);
|
||||
static uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer);
|
||||
static uint8_t swi_send_byte(uint8_t value);
|
||||
static uint8_t sha204p_receive_response(uint8_t size, uint8_t *response);
|
||||
static uint8_t sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address);
|
||||
static uint8_t sha204c_resync(uint8_t size, uint8_t *response);
|
||||
static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer,
|
||||
uint8_t execution_delay, uint8_t execution_timeout);
|
||||
@@ -373,27 +372,6 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
|
||||
/* Marshaling functions */
|
||||
|
||||
static uint8_t sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address)
|
||||
{
|
||||
uint8_t rx_size;
|
||||
|
||||
address >>= 2;
|
||||
|
||||
tx_buffer[SHA204_COUNT_IDX] = READ_COUNT;
|
||||
tx_buffer[SHA204_OPCODE_IDX] = SHA204_READ;
|
||||
tx_buffer[READ_ZONE_IDX] = zone;
|
||||
tx_buffer[READ_ADDR_IDX] = (uint8_t) (address & SHA204_ADDRESS_MASK);
|
||||
tx_buffer[READ_ADDR_IDX + 1] = 0;
|
||||
|
||||
rx_size = (zone & SHA204_ZONE_COUNT_FLAG) ? READ_32_RSP_SIZE : READ_4_RSP_SIZE;
|
||||
|
||||
return sha204c_send_and_receive(&tx_buffer[0], rx_size, &rx_buffer[0], READ_DELAY,
|
||||
READ_EXEC_MAX - READ_DELAY);
|
||||
}
|
||||
|
||||
/* CRC Calculator and Checker */
|
||||
|
||||
static void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc)
|
||||
@@ -574,14 +552,14 @@ uint8_t atsha204_getSerialNumber(uint8_t * response)
|
||||
uint8_t readResponse[READ_4_RSP_SIZE];
|
||||
|
||||
/* read from bytes 0->3 of config zone */
|
||||
uint8_t returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03);
|
||||
uint8_t returnCode = atsha204_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03);
|
||||
if (!returnCode) {
|
||||
for (int i=0; i<4; i++) {// store bytes 0-3 into respones array
|
||||
response[i] = readResponse[SHA204_BUFFER_POS_DATA+i];
|
||||
}
|
||||
|
||||
/* read from bytes 8->11 of config zone */
|
||||
returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47);
|
||||
returnCode = atsha204_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47);
|
||||
|
||||
for (int i=4; i<8; i++) {// store bytes 4-7 of SN into response array
|
||||
response[i] = readResponse[SHA204_BUFFER_POS_DATA+(i-4)];
|
||||
@@ -589,10 +567,28 @@ uint8_t atsha204_getSerialNumber(uint8_t * response)
|
||||
|
||||
if (!returnCode) {
|
||||
/* Finally if last two reads were successful, read byte 8 of the SN */
|
||||
returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN8);
|
||||
returnCode = atsha204_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN8);
|
||||
response[8] = readResponse[SHA204_BUFFER_POS_DATA]; // Byte 8 of SN should always be 0xEE
|
||||
}
|
||||
}
|
||||
|
||||
return returnCode;
|
||||
}
|
||||
|
||||
uint8_t atsha204_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address)
|
||||
{
|
||||
uint8_t rx_size;
|
||||
|
||||
address >>= 2;
|
||||
|
||||
tx_buffer[SHA204_COUNT_IDX] = READ_COUNT;
|
||||
tx_buffer[SHA204_OPCODE_IDX] = SHA204_READ;
|
||||
tx_buffer[READ_ZONE_IDX] = zone;
|
||||
tx_buffer[READ_ADDR_IDX] = (uint8_t) (address & SHA204_ADDRESS_MASK);
|
||||
tx_buffer[READ_ADDR_IDX + 1] = 0;
|
||||
|
||||
rx_size = (zone & SHA204_ZONE_COUNT_FLAG) ? READ_32_RSP_SIZE : READ_4_RSP_SIZE;
|
||||
|
||||
return sha204c_send_and_receive(&tx_buffer[0], rx_size, &rx_buffer[0], READ_DELAY,
|
||||
READ_EXEC_MAX - READ_DELAY);
|
||||
}
|
||||
|
||||
@@ -247,6 +247,7 @@ uint8_t atsha204_execute(uint8_t op_code, uint8_t param1, uint16_t param2,
|
||||
uint8_t datalen1, uint8_t *data1, uint8_t tx_size,
|
||||
uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer);
|
||||
uint8_t atsha204_getSerialNumber(uint8_t *response);
|
||||
uint8_t atsha204_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -290,11 +290,10 @@ void SPIFlash::writeBytes(uint32_t addr, const void* buf, uint16_t len)
|
||||
command(SPIFLASH_WRITEDISABLE); //end AAI programming
|
||||
unselect();
|
||||
#else
|
||||
uint16_t n;
|
||||
uint16_t maxBytes = 256-(addr%256); // force the first set of bytes to stay within the first page
|
||||
uint16_t offset = 0;
|
||||
while (len>0) {
|
||||
n = (len<=maxBytes) ? len : maxBytes;
|
||||
uint16_t n = (len<=maxBytes) ? len : maxBytes;
|
||||
command(SPIFLASH_BYTEPAGEPROGRAM, true); // Byte/Page Program
|
||||
SPI.transfer(addr >> 16);
|
||||
SPI.transfer(addr >> 8);
|
||||
|
||||
@@ -122,16 +122,16 @@ byte extEEPROM::begin(twiClockFreq_t twiFreq)
|
||||
//from the Arduino Wire library is passed back through to the caller.
|
||||
byte extEEPROM::write(unsigned long addr, byte *values, unsigned int nBytes)
|
||||
{
|
||||
uint8_t ctrlByte; //control byte (I2C device address & chip/block select bits)
|
||||
uint8_t txStatus = 0; //transmit status
|
||||
uint16_t nWrite; //number of bytes to write
|
||||
uint16_t nPage; //number of bytes remaining on current page, starting at addr
|
||||
|
||||
if (addr + nBytes > _totalCapacity) { //will this write go past the top of the EEPROM?
|
||||
return EEPROM_ADDR_ERR; //yes, tell the caller
|
||||
}
|
||||
|
||||
while (nBytes > 0) {
|
||||
uint8_t ctrlByte; //control byte (I2C device address & chip/block select bits)
|
||||
uint16_t nWrite; //number of bytes to write
|
||||
uint16_t nPage; //number of bytes remaining on current page, starting at addr
|
||||
nPage = _pageSize - ( addr & (_pageSize - 1) );
|
||||
//find min(nBytes, nPage, BUFFER_LENGTH) -- BUFFER_LENGTH is defined in the Wire library.
|
||||
nWrite = nBytes < nPage ? nBytes : nPage;
|
||||
@@ -178,16 +178,15 @@ byte extEEPROM::write(unsigned long addr, byte *values, unsigned int nBytes)
|
||||
//from the Arduino Wire library is passed back through to the caller.
|
||||
byte extEEPROM::read(unsigned long addr, byte *values, unsigned int nBytes)
|
||||
{
|
||||
byte ctrlByte;
|
||||
byte rxStatus;
|
||||
uint16_t nRead; //number of bytes to read
|
||||
uint16_t nPage; //number of bytes remaining on current page, starting at addr
|
||||
|
||||
if (addr + nBytes > _totalCapacity) { //will this read take us past the top of the EEPROM?
|
||||
return EEPROM_ADDR_ERR; //yes, tell the caller
|
||||
}
|
||||
|
||||
while (nBytes > 0) {
|
||||
byte ctrlByte;
|
||||
byte rxStatus;
|
||||
uint16_t nRead; //number of bytes to read
|
||||
uint16_t nPage; //number of bytes remaining on current page, starting at addr
|
||||
nPage = _pageSize - ( addr & (_pageSize - 1) );
|
||||
nRead = nBytes < nPage ? nBytes : nPage;
|
||||
nRead = BUFFER_LENGTH < nRead ? BUFFER_LENGTH : nRead;
|
||||
|
||||
@@ -92,7 +92,7 @@ public:
|
||||
* @param pageSize
|
||||
* @param eepromAddr
|
||||
*/
|
||||
extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize,
|
||||
extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize, //!< extEEPROM()
|
||||
byte eepromAddr = 0x50);
|
||||
byte begin(twiClockFreq_t twiFreq = twiClock100kHz); //!< begin()
|
||||
byte write(unsigned long addr, byte *values, unsigned int nBytes); //!< write()
|
||||
|
||||
Reference in New Issue
Block a user