From: Oleksij Rempel <o.rempel@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Oleksij Rempel <o.rempel@pengutronix.de>
Subject: [PATCH v2 2/9] nvmem: rmem: add write and protect support
Date: Thu, 5 Jun 2025 09:47:06 +0200 [thread overview]
Message-ID: <20250605074713.4170334-3-o.rempel@pengutronix.de> (raw)
In-Reply-To: <20250605074713.4170334-1-o.rempel@pengutronix.de>
Add reg_write and reg_protect operations to the rmem NVMEM driver.
This makes rmem devices writable (they were previously read-only via the
NVMEM interface due to lacking reg_write) and allows specific regions
to be marked read-only.
The primary motivation is to improve testing of NVMEM consumer code that
handles write protection, by enabling rmem to emulate such hardware,
particularly in sandbox environments.
Key changes:
- reg_write implemented: Enables writes. Writes to protected regions
return -EROFS.
- reg_protect implemented:
- PROTECT_DISABLE_WRITE: Marks range read-only.
- PROTECT_ENABLE_WRITE: Makes range writable.
- Probe function updated for new ops and list initialization.
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
changes v2:
- use a bitmap instead of lists of ranges.
---
drivers/nvmem/rmem.c | 195 +++++++++++++++++++++++++++++++++++++++++++
include/driver.h | 3 +
2 files changed, 198 insertions(+)
diff --git a/drivers/nvmem/rmem.c b/drivers/nvmem/rmem.c
index afa0dd78c8f4..fd7389cd39e3 100644
--- a/drivers/nvmem/rmem.c
+++ b/drivers/nvmem/rmem.c
@@ -11,6 +11,10 @@
struct rmem {
struct device *dev;
const struct resource *mem;
+ size_t total_size;
+ /* Bitmap: 1 bit per byte of rmem. 1=protected, 0=writable. */
+ unsigned char *protection_bitmap;
+ size_t protection_bitmap_bytes;
};
static int rmem_read(void *context, unsigned int offset,
@@ -21,6 +25,190 @@ static int rmem_read(void *context, unsigned int offset,
bytes, offset, 0);
}
+/**
+ * rmem_is_byte_protected() - Check if a specific byte is write-protected.
+ * @rmem: Pointer to the rmem private data.
+ * @offset: Offset of the byte to check.
+ *
+ * Helper function to consult the protection bitmap.
+ *
+ * Return: True if the byte at @offset is protected, false otherwise.
+ */
+static inline bool rmem_is_byte_protected(const struct rmem *rmem,
+ unsigned int offset)
+{
+ unsigned char bit_mask;
+ unsigned int byte_idx;
+
+ byte_idx = offset / 8;
+ bit_mask = 1U << (offset % 8);
+ return (rmem->protection_bitmap[byte_idx] & bit_mask) != 0;
+}
+
+/**
+ * rmem_do_protect_byte() - Mark a specific byte as write-protected.
+ * @rmem: Pointer to the rmem private data.
+ * @offset: Offset of the byte to protect.
+ *
+ * Helper function to set a bit in the protection bitmap. Assumes @offset
+ * is valid and rmem->protection_bitmap is allocated.
+ */
+static inline void rmem_do_protect_byte(struct rmem *rmem, unsigned int offset)
+{
+ unsigned char bit_mask = 1U << (offset % 8);
+ unsigned int byte_idx = offset / 8;
+
+ rmem->protection_bitmap[byte_idx] |= bit_mask;
+}
+
+/**
+ * rmem_do_unprotect_byte() - Mark a specific byte as writable.
+ * @rmem: Pointer to the rmem private data.
+ * @offset: Offset of the byte to unprotect.
+ *
+ * Helper function to clear a bit in the protection bitmap. Assumes @offset
+ * is valid and rmem->protection_bitmap is allocated.
+ */
+static inline void rmem_do_unprotect_byte(struct rmem *rmem,
+ unsigned int offset)
+{
+ unsigned int byte_idx = offset / 8;
+ unsigned char bit_mask = 1U << (offset % 8);
+
+ rmem->protection_bitmap[byte_idx] &= ~bit_mask;
+}
+
+/**
+ * rmem_write() - Write data to the NVMEM device.
+ * @context: Pointer to the rmem private data (struct rmem).
+ * @offset: Offset within the NVMEM device to write to.
+ * @val: Buffer containing the data to write.
+ * @bytes: Number of bytes to write.
+ *
+ * This function is called by the NVMEM core to write data to the
+ * reserved memory region. It first checks the protection bitmap to ensure
+ * the target range is not write-protected. If writable, it uses the
+ * custom 'mem_copy' function.
+ * Specific error codes include -EROFS if protected or -EINVAL for bad params.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+static int rmem_write(void *context, unsigned int offset, const void *val,
+ size_t bytes)
+{
+ struct rmem *rmem = context;
+ unsigned int i;
+
+ for (i = 0; i < bytes; ++i) {
+ if (rmem_is_byte_protected(rmem, offset + i)) {
+ dev_warn(rmem->dev,
+ "Write [0x%x, len %zu] denied at 0x%x (protected)\n",
+ offset, bytes, offset + i);
+ return -EROFS;
+ }
+ }
+
+ /*
+ * The last two arguments to mem_copy (0, 0) are specific to
+ * the custom mem_copy implementation.
+ */
+ return mem_copy(rmem->dev, (void *)rmem->mem->start + offset, val,
+ bytes, 0, 0);
+}
+
+/**
+ * rmem_set_protection_bits() - Mark a memory range as read-only.
+ * @rmem: Pointer to the rmem private data.
+ * @offset: Starting offset of the range to protect.
+ * @bytes: Length of the range to protect in bytes.
+ *
+ * Sets the corresponding bits in the protection_bitmap to mark the
+ * specified memory range as write-protected (read-only).
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+static int rmem_set_protection_bits(struct rmem *rmem, unsigned int offset,
+ size_t bytes)
+{
+ unsigned int end_offset = offset + bytes;
+ unsigned int i;
+
+ for (i = offset; i < end_offset; ++i)
+ rmem_do_protect_byte(rmem, i);
+
+ dev_info(rmem->dev, "Protected range [0x%x, len %zu]\n", offset,
+ bytes);
+ return 0;
+}
+
+/**
+ * rmem_clear_protection_bits() - Mark a memory range as writable.
+ * @rmem: Pointer to the rmem private data.
+ * @offset: Starting offset of the range to unprotect.
+ * @bytes: Length of the range to unprotect in bytes.
+ *
+ * Clears the corresponding bits in the protection_bitmap to mark the
+ * specified memory range as writable.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+static int rmem_clear_protection_bits(struct rmem *rmem, unsigned int offset,
+ size_t bytes)
+{
+ unsigned int end_offset = offset + bytes;
+ unsigned int i;
+
+ for (i = offset; i < end_offset; ++i)
+ rmem_do_unprotect_byte(rmem, i);
+
+ dev_info(rmem->dev, "Unprotected range [0x%x, len %zu]\n", offset,
+ bytes);
+ return 0;
+}
+
+/**
+ * rmem_protect() - NVMEM callback to change protection status of a range.
+ * @context: Pointer to the rmem private data (struct rmem).
+ * @offset: Starting offset of the range.
+ * @bytes: Length of the range in bytes.
+ * @prot_mode: Protection mode to apply (PROTECT_DISABLE_WRITE or
+ * PROTECT_ENABLE_WRITE).
+ *
+ * This function is called by the NVMEM core to enable or disable
+ * write protection for a specified memory range.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+static int rmem_protect(void *context, unsigned int offset, size_t bytes,
+ int prot_mode)
+{
+ struct rmem *rmem = context;
+ int ret;
+
+ switch (prot_mode) {
+ case PROTECT_DISABLE_WRITE: /* Make read-only */
+ ret = rmem_set_protection_bits(rmem, offset, bytes);
+ break;
+ case PROTECT_ENABLE_WRITE: /* Make writable */
+ ret = rmem_clear_protection_bits(rmem, offset, bytes);
+ break;
+ default:
+ dev_warn(rmem->dev, "%s: Invalid protection mode %d\n",
+ __func__, prot_mode);
+ ret = -EINVAL;
+ break;
+ }
+
+ if (ret)
+ return ret;
+
+ dev_dbg(rmem->dev,
+ "Protection op complete [0x%x, len %zu], mode %d\n", offset,
+ bytes, prot_mode);
+
+ return 0;
+}
+
static int rmem_probe(struct device *dev)
{
struct nvmem_config config = { };
@@ -41,7 +229,14 @@ static int rmem_probe(struct device *dev)
config.priv = priv;
config.name = "rmem";
config.size = resource_size(mem);
+ priv->total_size = config.size;
+
+ priv->protection_bitmap_bytes = DIV_ROUND_UP(priv->total_size, 8);
+ priv->protection_bitmap = xzalloc(priv->protection_bitmap_bytes);
+
config.reg_read = rmem_read;
+ config.reg_write = rmem_write;
+ config.reg_protect = rmem_protect;
return PTR_ERR_OR_ZERO(nvmem_register(&config));
}
diff --git a/include/driver.h b/include/driver.h
index e9a919f9bbb5..8e738f076c30 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -431,6 +431,9 @@ int platform_driver_register(struct driver *drv);
int platform_device_register(struct device *new_device);
+#define PROTECT_ENABLE_WRITE 0
+#define PROTECT_DISABLE_WRITE 1
+
struct cdev_operations {
/*! Called in response of reading from this device. Required */
ssize_t (*read)(struct cdev*, void* buf, size_t count, loff_t offset, ulong flags);
--
2.39.5
next prev parent reply other threads:[~2025-06-05 7:49 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-05 7:47 [PATCH v2 0/9] NVMEM: Introduce write protection support Oleksij Rempel
2025-06-05 7:47 ` [PATCH v2 1/9] nvmem: Add 'protect' operation to core framework Oleksij Rempel
2025-06-05 7:47 ` Oleksij Rempel [this message]
2025-06-05 7:47 ` [PATCH v2 3/9] commands: nvmem: Add support for creating dynamic rmem devices Oleksij Rempel
2025-06-05 7:47 ` [PATCH v2 4/9] regmap: Add reg_seal operation for hardware protection Oleksij Rempel
2025-06-05 7:47 ` [PATCH v2 5/9] nvmem: regmap: Implement protect operation using regmap_seal Oleksij Rempel
2025-06-05 7:47 ` [PATCH v2 6/9] nvmem: bsec: Implement NVMEM protect via regmap_seal for OTP locking Oleksij Rempel
2025-06-05 7:47 ` [PATCH v2 7/9] nvmem: rmem: generate unic device name Oleksij Rempel
2025-06-05 7:47 ` [PATCH v2 8/9] fs: Report errors for out-of-bounds protect operations Oleksij Rempel
2025-06-05 7:47 ` [PATCH v2 9/9] test: Add pytest suite for NVMEM framework Oleksij Rempel
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=20250605074713.4170334-3-o.rempel@pengutronix.de \
--to=o.rempel@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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