From: Oleksij Rempel <o.rempel@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Robin van der Gracht <robin@protonic.nl>,
Oleksij Rempel <o.rempel@pengutronix.de>
Subject: [PATCH v1 1/2] nvmem: bsec: Add support for OTP permanent write lock
Date: Thu, 30 Jan 2025 13:08:13 +0100 [thread overview]
Message-ID: <20250130120814.1053382-1-o.rempel@pengutronix.de> (raw)
From: Robin van der Gracht <robin@protonic.nl>
Introduce a mechanism to permanently lock OTP eFuses after programming by
adding a new `writelock` parameter. When `writelock` is enabled, the
driver:
- Programs the OTP fuse using `BSEC_SMC_PROG_OTP`.
- If successful, triggers `BSEC_SMC_WRLOCK_OTP` (OP-TEE:
`STM32_SIP_SVC_BSEC_WRLOCK_OTP`) to permanently disable further
modifications to the OTP word.
Security Concern:
Without this lock mechanism, an OTP word can still be altered by OR-ing
additional bits onto the existing value, as STM32 BSEC OTP fuses only
allow one-way bit transitions from 0 to 1. This is a potential security
risk when dealing with keys or sensitive configuration values, as an
attacker could modify certain OTP bits without fully replacing the
original value.
Warning! Write lock is enabled globally per BSEC device:
- While `writelock=1`, all writes via the BSEC device will be
permanently locked.
- The user must avoid writing unintended values during this period,
as they will become irrevocable.
Example Use Case:
To program and permanently lock an OTP word:
bsec0.permanent_write_enable=1
bsec0.writelock=1
mw -l -d /dev/stm32-bsec 0x00000170+4 $some_data
bsec0.permanent_write_enable=0
bsec0.writelock=0
After execution, the OTP at address `0x170` will be permanently
write-locked.
Signed-off-by: Robin van der Gracht <robin@protonic.nl>
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
drivers/nvmem/bsec.c | 14 +++++++++++---
include/mach/stm32mp/bsec.h | 1 +
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/drivers/nvmem/bsec.c b/drivers/nvmem/bsec.c
index b92d925956ee..c0ca0a2ab6a4 100644
--- a/drivers/nvmem/bsec.c
+++ b/drivers/nvmem/bsec.c
@@ -27,6 +27,7 @@ struct bsec_priv {
int permanent_write_enable;
u8 lower;
struct tee_context *ctx;
+ int writelock;
};
struct stm32_bsec_data {
@@ -67,11 +68,16 @@ static int stm32_bsec_read_shadow(void *ctx, unsigned reg, unsigned *val)
static int stm32_bsec_reg_write(void *ctx, unsigned reg, unsigned val)
{
struct bsec_priv *priv = ctx;
+ int ret;
- if (priv->permanent_write_enable)
- return bsec_smc(BSEC_SMC_PROG_OTP, reg, val, NULL);
- else
+ if (!priv->permanent_write_enable)
return bsec_smc(BSEC_SMC_WRITE_SHADOW, reg, val, NULL);
+
+ ret = bsec_smc(BSEC_SMC_PROG_OTP, reg, val, NULL);
+ if (!ret && priv->writelock)
+ ret = bsec_smc(BSEC_SMC_WRLOCK_OTP, reg, 0, NULL);
+
+ return ret;
}
static struct regmap_bus stm32_bsec_regmap_bus = {
@@ -245,6 +251,8 @@ static int stm32_bsec_probe(struct device *dev)
if (IS_ENABLED(CONFIG_STM32_BSEC_WRITE)) {
dev_add_param_bool(&priv->dev, "permanent_write_enable",
NULL, NULL, &priv->permanent_write_enable, NULL);
+ dev_add_param_bool(&priv->dev, "writelock",
+ NULL, NULL, &priv->writelock, NULL);
}
nvmem = nvmem_regmap_register(map, "stm32-bsec");
diff --git a/include/mach/stm32mp/bsec.h b/include/mach/stm32mp/bsec.h
index 45eb0a3f4523..be8cec536a40 100644
--- a/include/mach/stm32mp/bsec.h
+++ b/include/mach/stm32mp/bsec.h
@@ -26,6 +26,7 @@ enum bsec_op {
BSEC_SMC_READ_OTP = 4,
BSEC_SMC_READ_ALL = 5,
BSEC_SMC_WRITE_ALL = 6,
+ BSEC_SMC_WRLOCK_OTP = 7,
};
static inline enum bsec_smc bsec_read_field(unsigned field, unsigned *val)
--
2.39.5
next reply other threads:[~2025-01-30 12:08 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-30 12:08 Oleksij Rempel [this message]
2025-01-30 12:08 ` [PATCH v1 2/2] Documentation: user: Add OTP support and parameter descriptions Oleksij Rempel
2025-01-31 13:03 ` Sascha Hauer
2025-01-31 13:07 ` [PATCH v1 1/2] nvmem: bsec: Add support for OTP permanent write lock Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250130120814.1053382-1-o.rempel@pengutronix.de \
--to=o.rempel@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=robin@protonic.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox