linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] crypto: atmel-sha204a - read out otp zone
@ 2024-04-27  0:14 Lothar Rubusch
  2024-04-27  0:14 ` [PATCH 1/5] crypto: atmel-i2 - add missing arg description Lothar Rubusch
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Lothar Rubusch @ 2024-04-27  0:14 UTC (permalink / raw)
  To: herbert, davem
  Cc: nicolas.ferre, alexandre.belloni, claudiu.beznea, linux-crypto,
	linux-arm-kernel, linux-kernel, l.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 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.

Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---

Lothar Rubusch (5):
  crypto: atmel-i2 - add missing arg description
  crypto: atmel-sha204a - remove unused includes
  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 | 77 ++++++++++++++++++++++++++++++----
 3 files changed, 102 insertions(+), 13 deletions(-)

-- 
2.39.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/5] crypto: atmel-i2 - add missing arg description
  2024-04-27  0:14 [PATCH 0/5] crypto: atmel-sha204a - read out otp zone Lothar Rubusch
@ 2024-04-27  0:14 ` Lothar Rubusch
  2024-04-27  0:14 ` [PATCH 2/5] crypto: atmel-sha204a - remove unused includes Lothar Rubusch
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Lothar Rubusch @ 2024-04-27  0:14 UTC (permalink / raw)
  To: herbert, davem
  Cc: nicolas.ferre, alexandre.belloni, claudiu.beznea, linux-crypto,
	linux-arm-kernel, linux-kernel, l.rubusch

Add missing description for argument hwrng.

Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
 drivers/crypto/atmel-i2c.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/crypto/atmel-i2c.h b/drivers/crypto/atmel-i2c.h
index c0bd429ee..a442b47a4 100644
--- a/drivers/crypto/atmel-i2c.h
+++ b/drivers/crypto/atmel-i2c.h
@@ -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
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/5] crypto: atmel-sha204a - remove unused includes
  2024-04-27  0:14 [PATCH 0/5] crypto: atmel-sha204a - read out otp zone Lothar Rubusch
  2024-04-27  0:14 ` [PATCH 1/5] crypto: atmel-i2 - add missing arg description Lothar Rubusch
@ 2024-04-27  0:14 ` Lothar Rubusch
  2024-05-03 10:42   ` Herbert Xu
  2024-04-27  0:14 ` [PATCH 3/5] crypto: atmel-i2c - rename read function Lothar Rubusch
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 7+ messages in thread
From: Lothar Rubusch @ 2024-04-27  0:14 UTC (permalink / raw)
  To: herbert, davem
  Cc: nicolas.ferre, alexandre.belloni, claudiu.beznea, linux-crypto,
	linux-arm-kernel, linux-kernel, l.rubusch

Remove unnecessarily included headers.

Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
 drivers/crypto/atmel-sha204a.c | 9 ---------
 1 file changed, 9 deletions(-)

diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index c77f482d2..5c3fef6e9 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -5,17 +5,8 @@
  * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
  */
 
-#include <linux/delay.h>
-#include <linux/device.h>
-#include <linux/err.h>
-#include <linux/errno.h>
 #include <linux/i2c.h>
-#include <linux/init.h>
 #include <linux/kernel.h>
-#include <linux/module.h>
-#include <linux/scatterlist.h>
-#include <linux/slab.h>
-#include <linux/workqueue.h>
 #include "atmel-i2c.h"
 
 static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/5] crypto: atmel-i2c - rename read function
  2024-04-27  0:14 [PATCH 0/5] crypto: atmel-sha204a - read out otp zone Lothar Rubusch
  2024-04-27  0:14 ` [PATCH 1/5] crypto: atmel-i2 - add missing arg description Lothar Rubusch
  2024-04-27  0:14 ` [PATCH 2/5] crypto: atmel-sha204a - remove unused includes Lothar Rubusch
@ 2024-04-27  0:14 ` Lothar Rubusch
  2024-04-27  0:14 ` [PATCH 4/5] crypto: atmel-sha204a - add reading from otp zone Lothar Rubusch
  2024-04-27  0:14 ` [PATCH 5/5] crypto: atmel-sha204a - provide the otp content Lothar Rubusch
  4 siblings, 0 replies; 7+ messages in thread
From: Lothar Rubusch @ 2024-04-27  0:14 UTC (permalink / raw)
  To: herbert, davem
  Cc: nicolas.ferre, alexandre.belloni, claudiu.beznea, linux-crypto,
	linux-arm-kernel, linux-kernel, l.rubusch

Make 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.

Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
 drivers/crypto/atmel-i2c.c | 6 +++---
 drivers/crypto/atmel-i2c.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/crypto/atmel-i2c.c b/drivers/crypto/atmel-i2c.c
index 83a9093ef..a0d0d4f2a 100644
--- a/drivers/crypto/atmel-i2c.c
+++ b/drivers/crypto/atmel-i2c.c
@@ -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)
diff --git a/drivers/crypto/atmel-i2c.h b/drivers/crypto/atmel-i2c.h
index a442b47a4..275297a82 100644
--- a/drivers/crypto/atmel-i2c.h
+++ b/drivers/crypto/atmel-i2c.h
@@ -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,
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 4/5] crypto: atmel-sha204a - add reading from otp zone
  2024-04-27  0:14 [PATCH 0/5] crypto: atmel-sha204a - read out otp zone Lothar Rubusch
                   ` (2 preceding siblings ...)
  2024-04-27  0:14 ` [PATCH 3/5] crypto: atmel-i2c - rename read function Lothar Rubusch
@ 2024-04-27  0:14 ` Lothar Rubusch
  2024-04-27  0:14 ` [PATCH 5/5] crypto: atmel-sha204a - provide the otp content Lothar Rubusch
  4 siblings, 0 replies; 7+ messages in thread
From: Lothar Rubusch @ 2024-04-27  0:14 UTC (permalink / raw)
  To: herbert, davem
  Cc: nicolas.ferre, alexandre.belloni, claudiu.beznea, linux-crypto,
	linux-arm-kernel, linux-kernel, l.rubusch

Provide 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.

Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
 drivers/crypto/atmel-i2c.c     | 24 ++++++++++++++++++++++++
 drivers/crypto/atmel-i2c.h     |  5 +++++
 drivers/crypto/atmel-sha204a.c | 23 +++++++++++++++++++++++
 3 files changed, 52 insertions(+)

diff --git a/drivers/crypto/atmel-i2c.c b/drivers/crypto/atmel-i2c.c
index a0d0d4f2a..a895e4289 100644
--- a/drivers/crypto/atmel-i2c.c
+++ b/drivers/crypto/atmel-i2c.c
@@ -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;
diff --git a/drivers/crypto/atmel-i2c.h b/drivers/crypto/atmel-i2c.h
index 275297a82..72f04c156 100644
--- a/drivers/crypto/atmel-i2c.h
+++ b/drivers/crypto/atmel-i2c.h
@@ -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,
diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index 5c3fef6e9..81b5ea722 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -82,6 +82,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;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 5/5] crypto: atmel-sha204a - provide the otp content
  2024-04-27  0:14 [PATCH 0/5] crypto: atmel-sha204a - read out otp zone Lothar Rubusch
                   ` (3 preceding siblings ...)
  2024-04-27  0:14 ` [PATCH 4/5] crypto: atmel-sha204a - add reading from otp zone Lothar Rubusch
@ 2024-04-27  0:14 ` Lothar Rubusch
  4 siblings, 0 replies; 7+ messages in thread
From: Lothar Rubusch @ 2024-04-27  0:14 UTC (permalink / raw)
  To: herbert, davem
  Cc: nicolas.ferre, alexandre.belloni, claudiu.beznea, linux-crypto,
	linux-arm-kernel, linux-kernel, l.rubusch

Set 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.

Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
 drivers/crypto/atmel-sha204a.c | 45 ++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index 81b5ea722..4a2027a60 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -105,6 +105,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;
@@ -125,6 +158,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;
 }
 
@@ -137,6 +180,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);
 }
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/5] crypto: atmel-sha204a - remove unused includes
  2024-04-27  0:14 ` [PATCH 2/5] crypto: atmel-sha204a - remove unused includes Lothar Rubusch
@ 2024-05-03 10:42   ` Herbert Xu
  0 siblings, 0 replies; 7+ messages in thread
From: Herbert Xu @ 2024-05-03 10:42 UTC (permalink / raw)
  To: Lothar Rubusch
  Cc: davem, nicolas.ferre, alexandre.belloni, claudiu.beznea,
	linux-crypto, linux-arm-kernel, linux-kernel

On Sat, Apr 27, 2024 at 12:14:36AM +0000, Lothar Rubusch wrote:
> Remove unnecessarily included headers.
> 
> Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
> ---
>  drivers/crypto/atmel-sha204a.c | 9 ---------
>  1 file changed, 9 deletions(-)
> 
> diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
> index c77f482d2..5c3fef6e9 100644
> --- a/drivers/crypto/atmel-sha204a.c
> +++ b/drivers/crypto/atmel-sha204a.c
> @@ -5,17 +5,8 @@
>   * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
>   */
>  
> -#include <linux/delay.h>
> -#include <linux/device.h>
> -#include <linux/err.h>
> -#include <linux/errno.h>
>  #include <linux/i2c.h>
> -#include <linux/init.h>
>  #include <linux/kernel.h>
> -#include <linux/module.h>
> -#include <linux/scatterlist.h>
> -#include <linux/slab.h>
> -#include <linux/workqueue.h>
>  #include "atmel-i2c.h"

These do not seem to be redundant.

Cheers,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-05-03 10:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-27  0:14 [PATCH 0/5] crypto: atmel-sha204a - read out otp zone Lothar Rubusch
2024-04-27  0:14 ` [PATCH 1/5] crypto: atmel-i2 - add missing arg description Lothar Rubusch
2024-04-27  0:14 ` [PATCH 2/5] crypto: atmel-sha204a - remove unused includes Lothar Rubusch
2024-05-03 10:42   ` Herbert Xu
2024-04-27  0:14 ` [PATCH 3/5] crypto: atmel-i2c - rename read function Lothar Rubusch
2024-04-27  0:14 ` [PATCH 4/5] crypto: atmel-sha204a - add reading from otp zone Lothar Rubusch
2024-04-27  0:14 ` [PATCH 5/5] crypto: atmel-sha204a - provide the otp content Lothar Rubusch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).