All of lore.kernel.org
 help / color / mirror / Atom feed
From: Charles-Antoine Couret <charles-antoine.couret@essensium.com>
To: linux-mtd@lists.infradead.org
Cc: Charles-Antoine Couret <charles-antoine.couret@essensium.com>
Subject: [PATCH] mtdcore: add writeable attribute in sysfs
Date: Thu, 12 Apr 2018 15:42:32 +0200	[thread overview]
Message-ID: <20180412134232.14006-1-charles-antoine.couret@essensium.com> (raw)

MTD devices read-write or read-only property could be set by the bootloader with
the mtdparts argument but there is no way to change this definition at runtime
on Linux. The flags can be read but can not be changed.

This patch adds the possibility to force a MTD device to be read-only or read-write
even if the bootloader requested something else. The purpose is to offer to
the user of a device to eventually change something that is not allowed
like updating the bootloader or change its environement.

Or to protect the device without using mtdro virtual device to read the content.
---
 drivers/mtd/mtdcore.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 807d17d863b3..3a9a7055ee53 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -158,6 +158,39 @@ static ssize_t mtd_flags_show(struct device *dev,
 }
 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
 
+static ssize_t mtd_writeable_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct mtd_info *mtd = dev_get_drvdata(dev);
+	unsigned int writeable = mtd->flags & MTD_WRITEABLE ? 1 : 0;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n", writeable);
+
+}
+
+static ssize_t mtd_writeable_store(struct device *dev,
+					   struct device_attribute *attr,
+					   const char *buf, size_t count)
+{
+	struct mtd_info *mtd = dev_get_drvdata(dev);
+	unsigned int writeable;
+	int retval;
+
+
+	retval = kstrtouint(buf, 0, &writeable);
+	if (retval)
+		return retval;
+
+	if (writeable)
+		mtd->flags |= MTD_WRITEABLE;
+	else
+		mtd->flags &= ~MTD_WRITEABLE;
+
+	return count;
+}
+static DEVICE_ATTR(writeable, S_IRUGO | S_IWUSR, mtd_writeable_show,
+	mtd_writeable_store);
+
 static ssize_t mtd_size_show(struct device *dev,
 		struct device_attribute *attr, char *buf)
 {
@@ -322,6 +355,7 @@ static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL);
 static struct attribute *mtd_attrs[] = {
 	&dev_attr_type.attr,
 	&dev_attr_flags.attr,
+	&dev_attr_writeable.attr,
 	&dev_attr_size.attr,
 	&dev_attr_erasesize.attr,
 	&dev_attr_writesize.attr,
-- 
2.17.0

             reply	other threads:[~2018-04-12 13:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-12 13:42 Charles-Antoine Couret [this message]
2018-04-20 19:43 ` [PATCH] mtdcore: add writeable attribute in sysfs Boris Brezillon
2018-04-23  9:54   ` Couret Charles-Antoine

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=20180412134232.14006-1-charles-antoine.couret@essensium.com \
    --to=charles-antoine.couret@essensium.com \
    --cc=linux-mtd@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.