crypto: atmel-sha204a - read out otp zone
v2 cover crypto: atmel-sha204a - read out otp zone
Read out otp memory zone of this secure eeprom and rng chip. The chip has an otp zone and data zone, both still not accessed. The otp zone can be used to store persistently serial numbers or similar, if externally pre-configured. In any way the Atmel SHA204a needs to be preconfgured and fuse locked in order to be useful also for the already implemented RNG functionality. Placing data into the otp zone is optional. If empty, the chip returns 0xff on all field. The implementation passes the content to a new sysfs handle to userspace. If the chip is locked or not accessible no sysfs handle is set.
---
v1 -> v2
- fix typo in commit title
- drop patch: remove unused includes
Lothar Rubusch (4):
crypto: atmel-i2c - add missing arg description
crypto: atmel-i2c - rename read function
crypto: atmel-sha204a - add reading from otp zone
crypto: atmel-sha204a - provide the otp content
drivers/crypto/atmel-i2c.c | 30 +++++++++++++-- drivers/crypto/atmel-i2c.h | 8 +++- drivers/crypto/atmel-sha204a.c | 68 ++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 4 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/4 crypto: atmel-i2c - add missing arg description
Add missing description for argument hwrng.
--- drivers/crypto/atmel-i2c.h | 1 + 1 file changed, 1 insertion(+)
drivers/crypto/atmel-i2c.h +1 -0
@@ -124,6 +124,7 @@ struct atmel_ecc_driver_data {* @wake_token : wake token array of zeros * @wake_token_sz : size in bytes of the wake_token * @tfm_count : number of active crypto transformations on i2c client +* @hwrng : hold the hardware generated rng * * Reads and writes from/to the i2c client are sequential. The first byte * transmitted to the device is treated as the byte size. Any attempt to send
Signature · 1 line
2.25.1
PATCH v2 2/4 crypto: atmel-i2c - rename read function
In reply to Lothar RubuschMake the memory read function name more specific to the read memory zone. The Atmel SHA204 chips provide config, otp and data zone. The implemented read function in fact only reads some fields in zone config. The function renaming allows for a uniform naming scheme when reading from other memory zones.
--- drivers/crypto/atmel-i2c.c | 6 +++--- drivers/crypto/atmel-i2c.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/crypto/atmel-i2c.c +3 -3
@@ -51,7 +51,7 @@ static void atmel_i2c_checksum(struct atmel_i2c_cmd *cmd) *__crc16 =cpu_to_le16 (bitrev16 (crc16 (0 , data, len))); } -void atmel_i2c_init_read_cmd (struct atmel_i2c_cmd *cmd) +void atmel_i2c_init_read_config_cmd (struct atmel_i2c_cmd *cmd) { cmd->word_addr =COMMAND ; cmd->opcode =OPCODE_READ ;
@@ -68,7 +68,7 @@ void atmel_i2c_init_read_cmd(struct atmel_i2c_cmd *cmd) cmd->msecs =MAX_EXEC_TIME_READ ; cmd->rxsize =READ_RSP_SIZE ; } -EXPORT_SYMBOL (atmel_i2c_init_read_cmd); +EXPORT_SYMBOL (atmel_i2c_init_read_config_cmd);void atmel_i2c_init_random_cmd (struct atmel_i2c_cmd *cmd) {
@@ -301,7 +301,7 @@ static int device_sanity_check(struct i2c_client *client)if (!cmd)return -ENOMEM ; -atmel_i2c_init_read_cmd (cmd); +atmel_i2c_init_read_config_cmd (cmd); ret =atmel_i2c_send_receive (client, cmd);if (ret)
drivers/crypto/atmel-i2c.h +1 -1
@@ -178,7 +178,7 @@ void atmel_i2c_flush_queue(void);int atmel_i2c_send_receive (struct i2c_client *client,struct atmel_i2c_cmd *cmd); -void atmel_i2c_init_read_cmd (struct atmel_i2c_cmd *cmd); +void atmel_i2c_init_read_config_cmd (struct atmel_i2c_cmd *cmd);void atmel_i2c_init_random_cmd (struct atmel_i2c_cmd *cmd);void atmel_i2c_init_genkey_cmd (struct atmel_i2c_cmd *cmd,u16 keyid); int atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd *cmd,
Signature · 1 line
2.25.1
PATCH v2 3/4 crypto: atmel-sha204a - add reading from otp zone
In reply to Lothar RubuschProvide a read function reading the otp zone. The otp zone can be used for storing serial numbers. The otp zone, as also data zone, are only accessible if the chip was locked before. Locking the chip is a post production customization and has to be done manually i.e. not by this driver. Without this step the chip is pretty much not usable, where putting or not putting data into the otp zone is optional.
--- drivers/crypto/atmel-i2c.c | 24 ++++++++++++++++++++++++ drivers/crypto/atmel-i2c.h | 5 +++++ drivers/crypto/atmel-sha204a.c | 23 +++++++++++++++++++++++ 3 files changed, 52 insertions(+)
drivers/crypto/atmel-i2c.c +24 -0
@@ -70,6 +70,30 @@ void atmel_i2c_init_read_config_cmd(struct atmel_i2c_cmd *cmd) }EXPORT_SYMBOL (atmel_i2c_init_read_config_cmd); +int atmel_i2c_init_read_otp_cmd (struct atmel_i2c_cmd *cmd,u16 addr) +{ +if (addr <0 || addr >OTP_ZONE_SIZE ) +return -1 ; + + cmd->word_addr =COMMAND ; + cmd->opcode =OPCODE_READ ; +/* +* Read the word from OTP zone that may contain e.g. serial +* numbers or similar if persistently pre-initialized and locked +*/ + cmd->param1 =OTP_ZONE ; + cmd->param2 =cpu_to_le16 (addr); + cmd->count =READ_COUNT ; + +atmel_i2c_checksum (cmd); + + cmd->msecs =MAX_EXEC_TIME_READ ; + cmd->rxsize =READ_RSP_SIZE ; + +return 0 ; +} +EXPORT_SYMBOL (atmel_i2c_init_read_otp_cmd); +void atmel_i2c_init_random_cmd (struct atmel_i2c_cmd *cmd) { cmd->word_addr =COMMAND ;
drivers/crypto/atmel-i2c.h +5 -0
@@ -64,6 +64,10 @@ struct atmel_i2c_cmd {/* Definitions for eeprom organization */ #define CONFIGURATION_ZONE 0 +#define OTP_ZONE 1 + +/* Definitions for eeprom zone sizes */ +#define OTP_ZONE_SIZE 64/* Definitions for Indexes common to all commands */ #define RSP_DATA_IDX 1/* buffer index of data in response */
@@ -179,6 +183,7 @@ void atmel_i2c_flush_queue(void);int atmel_i2c_send_receive (struct i2c_client *client,struct atmel_i2c_cmd *cmd);void atmel_i2c_init_read_config_cmd (struct atmel_i2c_cmd *cmd); +int atmel_i2c_init_read_otp_cmd (struct atmel_i2c_cmd *cmd,u16 addr);void atmel_i2c_init_random_cmd (struct atmel_i2c_cmd *cmd);void atmel_i2c_init_genkey_cmd (struct atmel_i2c_cmd *cmd,u16 keyid); int atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd *cmd,
drivers/crypto/atmel-sha204a.c +23 -0
@@ -91,6 +91,29 @@ static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,return max; } +static int atmel_sha204a_otp_read (struct i2c_client *client,u16 addr,u8 *otp) +{ +struct atmel_i2c_cmd cmd; +int ret =-1 ; + +if (atmel_i2c_init_read_otp_cmd (&cmd, addr) <0 ) { +dev_err (&client->dev,"failed, invalid otp address %04X\n" , + addr); +return ret; + } + + ret =atmel_i2c_send_receive (client, &cmd); + +if (cmd.data[0 ] ==0xff ) { +dev_err (&client->dev,"failed, device not ready\n" ); +return -ret; + } + +memcpy (otp, cmd.data+1 ,4 ); + +return ret; +} +static int atmel_sha204a_probe (struct i2c_client *client) {struct atmel_i2c_client_priv *i2c_priv;
Signature · 1 line
2.25.1
PATCH v2 4/4 crypto: atmel-sha204a - provide the otp content
In reply to Lothar RubuschSet up sysfs for the Atmel SHA204a. Provide the content of the otp zone as an attribute field on the sysfs entry. Thereby make sure that if the chip is locked, not connected or trouble with the i2c bus, the sysfs device is not set up. This is mostly already handled in atmel-i2c.
--- drivers/crypto/atmel-sha204a.c | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
drivers/crypto/atmel-sha204a.c +45 -0
@@ -114,6 +114,39 @@ static int atmel_sha204a_otp_read(struct i2c_client *client, u16 addr, u8 *otp)return ret; } +static ssize_t otp_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +u16 addr; +u8 otp[OTP_ZONE_SIZE ]; +char *str = buf; +struct i2c_client *client =to_i2c_client (dev); +int i; + +for (addr =0 ; addr <OTP_ZONE_SIZE /4 ; addr++) { +if (atmel_sha204a_otp_read (client, addr, otp + addr *4 ) <0 ) { +dev_err (dev,"failed to read otp zone\n" ); +break ; + } + } + +for (i =0 ; i < addr*2 ; i++) + str +=sprintf (str,"%02X" , otp[i]); + str +=sprintf (str,"\n" ); +return str - buf; +} +static DEVICE_ATTR_RO (otp); + +static struct attribute *atmel_sha204a_attrs[] = { + &dev_attr_otp.attr, +NULL +}; + +static const struct attribute_group atmel_sha204a_groups = { + .name ="atsha204a" , + .attrs = atmel_sha204a_attrs, +}; +static int atmel_sha204a_probe (struct i2c_client *client) {struct atmel_i2c_client_priv *i2c_priv;
@@ -134,6 +167,16 @@ static int atmel_sha204a_probe(struct i2c_client *client)if (ret)dev_warn (&client->dev,"failed to register RNG (%d)\n" , ret); +/* otp read out */ +if (!i2c_check_functionality (client->adapter,I2C_FUNC_I2C )) +return -ENODEV ; + + ret =sysfs_create_group (&client->dev.kobj, &atmel_sha204a_groups); +if (ret) { +dev_err (&client->dev,"failed to register sysfs entry\n" ); +return ret; + } +return ret; }
@@ -146,6 +189,8 @@ static void atmel_sha204a_remove(struct i2c_client *client)return ; } +sysfs_remove_group (&client->dev.kobj, &atmel_sha204a_groups); +kfree ((void *)i2c_priv->hwrng.priv); }
Signature · 1 line
2.25.1
Herbert Xu
maintainerQuoted 22 lines · Lothar Rubusch
Read out otp memory zone of this secure eeprom and rng chip. The chip has an otp zone and data zone, both still not accessed. The otp zone can be used to store persistently serial numbers or similar, if externally pre-configured. In any way the Atmel SHA204a needs to be preconfgured and fuse locked in order to be useful also for the already implemented RNG functionality. Placing data into the otp zone is optional. If empty, the chip returns
0xffon all field. The implementation passes the content to a new sysfs handle to userspace. If the chip is locked or not accessible no sysfs handle is set.Signed-off-by Lothar Rubusch <[email protected]>---
v1 -> v2
- fix typo in commit title
- drop patch: remove unused includesLothar Rubusch (4):
crypto: atmel-i2c - add missing arg description
crypto: atmel-i2c - rename read function
crypto: atmel-sha204a - add reading from otp zone
crypto: atmel-sha204a - provide the otp content
Quoted 7 lines
drivers/crypto/atmel-i2c.c | 30 +++++++++++++-- drivers/crypto/atmel-i2c.h | 8 +++- drivers/crypto/atmel-sha204a.c | 68 ++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 4 deletions(-)Signature · 1 line
2.25.1
All applied. Thanks.
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt