Skip to contentSkip to conversation

mtd: add OTP (one-time-programmable) erase ioctl

RFC PATCH In review Michael Walle 3 participants 7 messages Mar 2, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 mtd: add OTP (one-time-programmable) erase ioctl

This may sound like a contradiction but some SPI-NOR flashes really support erasing their OTP region until it is finally locked. Having the possibility to erase an OTP region might come in handy during development.

The ioctl argument follows the OTPLOCK style.

---
OTP support for SPI-NOR flashes may be merged soon:
https://lore.kernel.org/linux-mtd/[email protected]/

Tudor suggested to add support for the OTP erase operation most SPI-NOR flashes have:
https://lore.kernel.org/linux-mtd/[email protected]/

Therefore, this is an RFC to get some feedback on the MTD side, once this is finished, I can post a patch for mtd-utils. Then we'll have a foundation to add the support to SPI-NOR.

 drivers/mtd/mtdchar.c      |  7 ++++++-
 drivers/mtd/mtdcore.c      | 12 ++++++++++++
 include/linux/mtd/mtd.h    |  3 +++
 include/uapi/mtd/mtd-abi.h |  2 ++
 4 files changed, 23 insertions(+), 1 deletion(-)
drivers/mtd/mtdchar.c +6 -1
index 323035d4f2d0..da423dd031ae 100644
@@ -661,6 +661,7 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 	case OTPGETREGIONCOUNT:
 	case OTPGETREGIONINFO:
 	case OTPLOCK:
+	case OTPERASE:
 	case ECCGETLAYOUT:
 	case ECCGETSTATS:
 	case MTDFILEMODE:
@@ -938,6 +939,7 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 	}
 
 	case OTPLOCK:
+	case OTPERASE:
 	{
 		struct otp_info oinfo;
 
@@ -945,7 +947,10 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 			return -EINVAL;
 		if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
 			return -EFAULT;
-		ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
+		if (cmd == OTPLOCK)
+			ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
+		else
+			ret = mtd_erase_user_prot_reg(mtd, oinfo.start, oinfo.length);
 		break;
 	}
 
drivers/mtd/mtdcore.c +12 -0
index 2d6423d89a17..d844d718ef77 100644
@@ -1918,6 +1918,18 @@ int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
 }
 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
 
+int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
+{
+	struct mtd_info *master = mtd_get_master(mtd);
+
+	if (!master->_erase_user_prot_reg)
+		return -EOPNOTSUPP;
+	if (!len)
+		return 0;
+	return master->_erase_user_prot_reg(master, from, len);
+}
+EXPORT_SYMBOL_GPL(mtd_erase_user_prot_reg);
+
 /* Chip-supported device locking */
 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
 {
include/linux/mtd/mtd.h +3 -0
index 157357ec1441..734ad7a8c353 100644
@@ -336,6 +336,8 @@ struct mtd_info {
 				     size_t len, size_t *retlen, u_char *buf);
 	int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
 				    size_t len);
+	int (*_erase_user_prot_reg) (struct mtd_info *mtd, loff_t from,
+				     size_t len);
 	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
 			unsigned long count, loff_t to, size_t *retlen);
 	void (*_sync) (struct mtd_info *mtd);
@@ -517,6 +519,7 @@ int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
 			    size_t *retlen, u_char *buf);
 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
+int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
 
 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
 	       unsigned long count, loff_t to, size_t *retlen);
include/uapi/mtd/mtd-abi.h +2 -0
index 65b9db936557..242015f60d10 100644
@@ -205,6 +205,8 @@ struct otp_info {
  * without OOB, e.g., NOR flash.
  */
 #define MEMWRITE		_IOWR('M', 24, struct mtd_write_req)
+/* Erase a given range of user data (must be in mode %MTD_FILE_MODE_OTP_USER) */
+#define OTPERASE		_IOR('M', 25, struct otp_info)
 
 /*
  * Obsolete legacy interface. Keep it in order not to break userspace
Signature · 1 line
2.20.1
maintainer
#1 lore raw

Hi Michael,

Michael Walle <[email protected]> wrote on Tue, 2 Mar 2021 12:09:27 +0100:

Quoted 64 lines

This may sound like a contradiction but some SPI-NOR flashes really support erasing their OTP region until it is finally locked. Having the possibility to erase an OTP region might come in handy during development.

The ioctl argument follows the OTPLOCK style.

---
OTP support for SPI-NOR flashes may be merged soon:
https://lore.kernel.org/linux-mtd/[email protected]/

Tudor suggested to add support for the OTP erase operation most SPI-NOR flashes have:
https://lore.kernel.org/linux-mtd/[email protected]/

Therefore, this is an RFC to get some feedback on the MTD side, once this is finished, I can post a patch for mtd-utils. Then we'll have a foundation to add the support to SPI-NOR.

 drivers/mtd/mtdchar.c      |  7 ++++++-
 drivers/mtd/mtdcore.c      | 12 ++++++++++++
 include/linux/mtd/mtd.h    |  3 +++
 include/uapi/mtd/mtd-abi.h |  2 ++
 4 files changed, 23 insertions(+), 1 deletion(-)
drivers/mtd/mtdchar.c +6 -1
index 323035d4f2d0..da423dd031ae 100644
@@ -661,6 +661,7 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 	case OTPGETREGIONCOUNT:
 	case OTPGETREGIONINFO:
 	case OTPLOCK:
+	case OTPERASE:
 	case ECCGETLAYOUT:
 	case ECCGETSTATS:
 	case MTDFILEMODE:
@@ -938,6 +939,7 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 	}
 
 	case OTPLOCK:
+	case OTPERASE:
 	{
 		struct otp_info oinfo;
 
@@ -945,7 +947,10 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 			return -EINVAL;
 		if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
 			return -EFAULT;
-		ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
+		if (cmd == OTPLOCK)
+			ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
+		else
+			ret = mtd_erase_user_prot_reg(mtd, oinfo.start, oinfo.length);
 		break;
 	}
 
drivers/mtd/mtdcore.c +0 -0
index 2d6423d89a17..d844d718ef77 100644
@@ -1918,6 +1918,18 @@ int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
 }

drivers/mtd/mtdcore.c int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) · line 1928

Quoted 10 lines
drivers/mtd/mtdcore.c +8 -0
 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
 
+int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
+{
+	struct mtd_info *master = mtd_get_master(mtd);
+
+	if (!master->_erase_user_prot_reg)
+		return -EOPNOTSUPP;
+	if (!len)
+		return 0;

Should we add a sanity check enforcing that we don't try to access beyond the end of the OTP area?

Quoted 33 lines
drivers/mtd/mtdcore.c +4 -0
+	return master->_erase_user_prot_reg(master, from, len);
+}
+EXPORT_SYMBOL_GPL(mtd_erase_user_prot_reg);
+
 /* Chip-supported device locking */
 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
 {
include/linux/mtd/mtd.h +3 -0
index 157357ec1441..734ad7a8c353 100644
@@ -336,6 +336,8 @@ struct mtd_info {
 				     size_t len, size_t *retlen, u_char *buf);
 	int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
 				    size_t len);
+	int (*_erase_user_prot_reg) (struct mtd_info *mtd, loff_t from,
+				     size_t len);
 	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
 			unsigned long count, loff_t to, size_t *retlen);
 	void (*_sync) (struct mtd_info *mtd);
@@ -517,6 +519,7 @@ int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
 			    size_t *retlen, u_char *buf);
 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
+int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
 
 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
 	       unsigned long count, loff_t to, size_t *retlen);
include/uapi/mtd/mtd-abi.h +0 -0
index 65b9db936557..242015f60d10 100644

include/uapi/mtd/mtd-abi.h struct otp_info { · line 212

Quoted 8 lines
include/uapi/mtd/mtd-abi.h +2 -0
@@ -205,6 +205,8 @@ struct otp_info {
  * without OOB, e.g., NOR flash.
  */
 #define MEMWRITE		_IOWR('M', 24, struct mtd_write_req)
+/* Erase a given range of user data (must be in mode %MTD_FILE_MODE_OTP_USER) */
+#define OTPERASE		_IOR('M', 25, struct otp_info)
 
 /*
  * Obsolete legacy interface. Keep it in order not to break userspace

Thanks,
Miquèl

#2 lore raw

Hi,

Am 2021-03-02 13:46, schrieb Miquel Raynal:

Quoted 23 lines

Michael Walle <[email protected]> wrote on Tue, 2 Mar 2021 12:09:27 +0100:

Quoted 20 lines

This may sound like a contradiction but some SPI-NOR flashes really support erasing their OTP region until it is finally locked. Having the possibility to erase an OTP region might come in handy during development.

The ioctl argument follows the OTPLOCK style.

---
OTP support for SPI-NOR flashes may be merged soon:
https://lore.kernel.org/linux-mtd/[email protected]/

Tudor suggested to add support for the OTP erase operation most SPI-NOR flashes have:
https://lore.kernel.org/linux-mtd/[email protected]/

Therefore, this is an RFC to get some feedback on the MTD side, once this is finished, I can post a patch for mtd-utils. Then we'll have a foundation to add the support to SPI-NOR.

[..]

Quoted 4 lines
Quoted 4 lines

+int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)

drivers/mtd/mtdcore.c +2 -0
+{
+	struct mtd_info *master = mtd_get_master(mtd);
Quoted 8 lines

drivers/mtd/mtdcore.c int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) · line 1928

Quoted 5 lines
drivers/mtd/mtdcore.c +5 -0
+
+	if (!master->_erase_user_prot_reg)
+		return -EOPNOTSUPP;
+	if (!len)
+		return 0;

Should we add a sanity check enforcing that we don't try to access beyond the end of the OTP area?

This is checked in the op itself, as it is done for all the other OTP read/write/lock ops.

Right at the moment, I don't see how this could be achieved in an elegant way. Without additional changes, you'd have to call mtd_get_user_prot_info() and iterate over the returned values and figure out whether from and len are valid.

[..]

-michael

#3 lore raw
In reply to Michael Walle

Hi,

Quoted 31 lines · Michael Walle

This may sound like a contradiction but some SPI-NOR flashes really support erasing their OTP region until it is finally locked. Having the possibility to erase an OTP region might come in handy during development.

The ioctl argument follows the OTPLOCK style.

---
OTP support for SPI-NOR flashes may be merged soon:
https://lore.kernel.org/linux-mtd/[email protected]/

Tudor suggested to add support for the OTP erase operation most SPI-NOR flashes have:
https://lore.kernel.org/linux-mtd/[email protected]/

Therefore, this is an RFC to get some feedback on the MTD side, once this is finished, I can post a patch for mtd-utils. Then we'll have a foundation to add the support to SPI-NOR.

 drivers/mtd/mtdchar.c      |  7 ++++++-
 drivers/mtd/mtdcore.c      | 12 ++++++++++++
 include/linux/mtd/mtd.h    |  3 +++
 include/uapi/mtd/mtd-abi.h |  2 ++
 4 files changed, 23 insertions(+), 1 deletion(-)
drivers/mtd/mtdchar.c +0 -0
index 323035d4f2d0..da423dd031ae 100644

drivers/mtd/mtdchar.c static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg) · line 664

Quoted 4 lines
drivers/mtd/mtdchar.c +1 -0
@@ -661,6 +661,7 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 	case OTPGETREGIONCOUNT:
 	case OTPGETREGIONINFO:
 	case OTPLOCK:
+	case OTPERASE:

This is not a Safe IOCTL. We are destroying OTP data. Need to check for write permission before allowing the ioctl right?

Quoted 72 lines
drivers/mtd/mtdchar.c +5 -1
 	case ECCGETLAYOUT:
 	case ECCGETSTATS:
 	case MTDFILEMODE:
@@ -938,6 +939,7 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 	}
 
 	case OTPLOCK:
+	case OTPERASE:
 	{
 		struct otp_info oinfo;
 
@@ -945,7 +947,10 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 			return -EINVAL;
 		if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
 			return -EFAULT;
-		ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
+		if (cmd == OTPLOCK)
+			ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
+		else
+			ret = mtd_erase_user_prot_reg(mtd, oinfo.start, oinfo.length);
 		break;
 	}
 
drivers/mtd/mtdcore.c +12 -0
index 2d6423d89a17..d844d718ef77 100644
@@ -1918,6 +1918,18 @@ int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
 }
 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
 
+int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
+{
+	struct mtd_info *master = mtd_get_master(mtd);
+
+	if (!master->_erase_user_prot_reg)
+		return -EOPNOTSUPP;
+	if (!len)
+		return 0;
+	return master->_erase_user_prot_reg(master, from, len);
+}
+EXPORT_SYMBOL_GPL(mtd_erase_user_prot_reg);
+
 /* Chip-supported device locking */
 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
 {
include/linux/mtd/mtd.h +3 -0
index 157357ec1441..734ad7a8c353 100644
@@ -336,6 +336,8 @@ struct mtd_info {
 				     size_t len, size_t *retlen, u_char *buf);
 	int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
 				    size_t len);
+	int (*_erase_user_prot_reg) (struct mtd_info *mtd, loff_t from,
+				     size_t len);
 	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
 			unsigned long count, loff_t to, size_t *retlen);
 	void (*_sync) (struct mtd_info *mtd);
@@ -517,6 +519,7 @@ int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
 			    size_t *retlen, u_char *buf);
 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
+int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
 
 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
 	       unsigned long count, loff_t to, size_t *retlen);
include/uapi/mtd/mtd-abi.h +0 -0
index 65b9db936557..242015f60d10 100644

include/uapi/mtd/mtd-abi.h struct otp_info { · line 210

Quoted 6 lines
include/uapi/mtd/mtd-abi.h +2 -0
@@ -205,6 +205,8 @@ struct otp_info {
  * without OOB, e.g., NOR flash.
  */
 #define MEMWRITE		_IOWR('M', 24, struct mtd_write_req)
+/* Erase a given range of user data (must be in mode %MTD_FILE_MODE_OTP_USER) */
+#define OTPERASE		_IOR('M', 25, struct otp_info)
 

Hmm, shouldn't this be:

#define OTPERASE _IOW('M', 25, struct otp_info)

Userspace is writing struct otp_info to the driver. OTPLOCK should probably be _IOW() as well.

include/uapi/mtd/mtd-abi.h struct otp_info { · line 212

Quoted 3 lines
include/uapi/mtd/mtd-abi.h +0 -0
 /*
  * Obsolete legacy interface. Keep it in order not to break userspace

Regards
Vignesh

#4 lore raw

Am 2021-03-02 16:30, schrieb Vignesh Raghavendra:

Quoted 29 lines

Hi,

Quoted 26 lines · Michael Walle

This may sound like a contradiction but some SPI-NOR flashes really support erasing their OTP region until it is finally locked. Having the possibility to erase an OTP region might come in handy during development.

The ioctl argument follows the OTPLOCK style.

---
OTP support for SPI-NOR flashes may be merged soon:
https://lore.kernel.org/linux-mtd/[email protected]/

Tudor suggested to add support for the OTP erase operation most SPI-NOR flashes have:
https://lore.kernel.org/linux-mtd/[email protected]/

Therefore, this is an RFC to get some feedback on the MTD side, once this is finished, I can post a patch for mtd-utils. Then we'll have a foundation to add the support to SPI-NOR.

 drivers/mtd/mtdchar.c      |  7 ++++++-
 drivers/mtd/mtdcore.c      | 12 ++++++++++++
 include/linux/mtd/mtd.h    |  3 +++
 include/uapi/mtd/mtd-abi.h |  2 ++
 4 files changed, 23 insertions(+), 1 deletion(-)
Quoted 13 lines

drivers/mtd/mtdchar.c static int mtdchar_ioctl(struct file *file, u_int · line 665

Quoted 10 lines
drivers/mtd/mtdchar.c +1 -0
index 323035d4f2d0..da423dd031ae 100644
@@ -661,6 +661,7 @@ static int mtdchar_ioctl(struct file *file, u_int 
 cmd, u_long arg)
 	case OTPGETREGIONCOUNT:
 	case OTPGETREGIONINFO:
 	case OTPLOCK:
+	case OTPERASE:

This is not a Safe IOCTL. We are destroying OTP data. Need to check for write permission before allowing the ioctl right?

Ah yes, of course. But this makes me wonder why OTPLOCK is considered a safe command. As well as MEMLOCK and MEMUNLOCK. And MEMSETBADBLOCK. Shouldn't these also require write permissions?

Quoted 90 lines
Quoted 84 lines
drivers/mtd/mtdchar.c +0 -0
 	case ECCGETLAYOUT:
 	case ECCGETSTATS:
drivers/mtd/mtdchar.c +5 -1
 	case MTDFILEMODE:
@@ -938,6 +939,7 @@ static int mtdchar_ioctl(struct file *file, u_int 
 cmd, u_long arg)
 	}
 
 	case OTPLOCK:
+	case OTPERASE:
 	{
 		struct otp_info oinfo;
@@ -945,7 +947,10 @@ static int mtdchar_ioctl(struct file *file, u_int 
 cmd, u_long arg)
 			return -EINVAL;
 		if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
 			return -EFAULT;
-		ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
+		if (cmd == OTPLOCK)
+			ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
+		else
+			ret = mtd_erase_user_prot_reg(mtd, oinfo.start, oinfo.length);
 		break;
 	}
drivers/mtd/mtdcore.c +12 -0
index 2d6423d89a17..d844d718ef77 100644
@@ -1918,6 +1918,18 @@ int mtd_lock_user_prot_reg(struct mtd_info 
 *mtd, loff_t from, size_t len)
 }
 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
 
+int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t 
 len)
+{
+	struct mtd_info *master = mtd_get_master(mtd);
+
+	if (!master->_erase_user_prot_reg)
+		return -EOPNOTSUPP;
+	if (!len)
+		return 0;
+	return master->_erase_user_prot_reg(master, from, len);
+}
+EXPORT_SYMBOL_GPL(mtd_erase_user_prot_reg);
+
 /* Chip-supported device locking */

int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) {

include/linux/mtd/mtd.h +3 -0
index 157357ec1441..734ad7a8c353 100644
@@ -336,6 +336,8 @@ struct mtd_info {
 				     size_t len, size_t *retlen, u_char *buf);
 	int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
 				    size_t len);
+	int (*_erase_user_prot_reg) (struct mtd_info *mtd, loff_t from,
+				     size_t len);
 	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
 			unsigned long count, loff_t to, size_t *retlen);
 	void (*_sync) (struct mtd_info *mtd);
@@ -517,6 +519,7 @@ int mtd_read_user_prot_reg(struct mtd_info *mtd, 
 loff_t from, size_t len,
 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t 
 len,
 			    size_t *retlen, u_char *buf);
 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t 
 len);
+int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t 

len);

int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,

include/linux/mtd/mtd.h +0 -0
 	       unsigned long count, loff_t to, size_t *retlen);
include/uapi/mtd/mtd-abi.h +1 -0
index 65b9db936557..242015f60d10 100644
@@ -205,6 +205,8 @@ struct otp_info {
  * without OOB, e.g., NOR flash.
  */
 #define MEMWRITE		_IOWR('M', 24, struct mtd_write_req)
+/* Erase a given range of user data (must be in mode 
Quoted 3 lines

%MTD_FILE_MODE_OTP_USER) */
+#define OTPERASE _IOR('M', 25, struct otp_info)

Hmm, shouldn't this be:

Quoted 4 lines

#define OTPERASE _IOW('M', 25, struct otp_info)

Userspace is writing struct otp_info to the driver. OTPLOCK should probably be _IOW() as well.

You're right.

NB. most OTP commands have a wrong direction flag.

Quoted 3 lines
Quoted 3 lines

/*
* Obsolete legacy interface. Keep it in order not to break userspace

-michael

#5 lore raw
Quoted 48 lines · Michael Walle

Am 2021-03-02 16:30, schrieb Vignesh Raghavendra:

Quoted 33 lines

Hi,

Quoted 30 lines · Michael Walle

This may sound like a contradiction but some SPI-NOR flashes really support erasing their OTP region until it is finally locked. Having the possibility to erase an OTP region might come in handy during development.

The ioctl argument follows the OTPLOCK style.

---
OTP support for SPI-NOR flashes may be merged soon:
https://lore.kernel.org/linux-mtd/[email protected]/

Tudor suggested to add support for the OTP erase operation most SPI-NOR flashes have:
https://lore.kernel.org/linux-mtd/[email protected]/

Therefore, this is an RFC to get some feedback on the MTD side, once this
is finished, I can post a patch for mtd-utils. Then we'll have a foundation
to add the support to SPI-NOR.

 drivers/mtd/mtdchar.c      |  7 ++++++-
 drivers/mtd/mtdcore.c      | 12 ++++++++++++
 include/linux/mtd/mtd.h    |  3 +++
 include/uapi/mtd/mtd-abi.h |  2 ++

 4 files changed, 23 insertions(+), 1 deletion(-)
Quoted 13 lines
Quoted 10 lines
drivers/mtd/mtdchar.c +0 -0
index 323035d4f2d0..da423dd031ae 100644
@@ -661,6 +661,7 @@ static int mtdchar_ioctl(struct file *file, u_int
 cmd, u_long arg)

     case OTPGETREGIONCOUNT:
     case OTPGETREGIONINFO:
     case OTPLOCK:
+    case OTPERASE:

This is not a Safe IOCTL. We are destroying OTP data. Need to check for write permission before allowing the ioctl right?

Quoted 5 lines

Ah yes, of course. But this makes me wonder why OTPLOCK is considered a safe command. As well as MEMLOCK and MEMUNLOCK. And MEMSETBADBLOCK. Shouldn't these also require write permissions?

Well, one argument would be that LOCK/UNLOCK in itself won't modify data and thus does not need write permission.. Although can brick a flash from ever being writable again and change content of flash registers.

I am fine with moving these to require write permissions as well (probably OTPLOCK as well).

[...]

Quoted 15 lines
Quoted 15 lines
Quoted 9 lines
include/uapi/mtd/mtd-abi.h +1 -0
index 65b9db936557..242015f60d10 100644
@@ -205,6 +205,8 @@ struct otp_info {
   * without OOB, e.g., NOR flash.
   */
  #define MEMWRITE        _IOWR('M', 24, struct mtd_write_req)
+/* Erase a given range of user data (must be in mode
Quoted 3 lines

%MTD_FILE_MODE_OTP_USER) */
+#define OTPERASE        _IOR('M', 25, struct otp_info)

Hmm, shouldn't this be:

Quoted 9 lines
Quoted 4 lines

#define OTPERASE        _IOW('M', 25, struct otp_info)

Userspace is writing struct otp_info to the driver. OTPLOCK should probably be _IOW() as well.

You're right.

NB. most OTP commands have a wrong direction flag.

Unfortunately, yes :(

Regards
Vignesh

#6 lore raw

Am 2021-03-02 17:33, schrieb Vignesh Raghavendra:

Quoted 56 lines · Michael Walle
Quoted 49 lines

Am 2021-03-02 16:30, schrieb Vignesh Raghavendra:

Quoted 33 lines

Hi,

Quoted 30 lines · Michael Walle

This may sound like a contradiction but some SPI-NOR flashes really support erasing their OTP region until it is finally locked. Having the possibility to erase an OTP region might come in handy during development.

The ioctl argument follows the OTPLOCK style.

---
OTP support for SPI-NOR flashes may be merged soon:
https://lore.kernel.org/linux-mtd/[email protected]/

Tudor suggested to add support for the OTP erase operation most SPI-NOR flashes have:
https://lore.kernel.org/linux-mtd/[email protected]/

Therefore, this is an RFC to get some feedback on the MTD side, once this
is finished, I can post a patch for mtd-utils. Then we'll have a foundation
to add the support to SPI-NOR.

 drivers/mtd/mtdchar.c      |  7 ++++++-
 drivers/mtd/mtdcore.c      | 12 ++++++++++++
 include/linux/mtd/mtd.h    |  3 +++
 include/uapi/mtd/mtd-abi.h |  2 ++

 4 files changed, 23 insertions(+), 1 deletion(-)
Quoted 14 lines
Quoted 11 lines
drivers/mtd/mtdchar.c +0 -0
index 323035d4f2d0..da423dd031ae 100644
@@ -661,6 +661,7 @@ static int mtdchar_ioctl(struct file *file, 

u_int
cmd, u_long arg)
     case OTPGETREGIONCOUNT:
     case OTPGETREGIONINFO:
     case OTPLOCK:
+    case OTPERASE:

This is not a Safe IOCTL. We are destroying OTP data. Need to check for write permission before allowing the ioctl right?

Quoted 5 lines

Ah yes, of course. But this makes me wonder why OTPLOCK is considered a safe command. As well as MEMLOCK and MEMUNLOCK. And MEMSETBADBLOCK. Shouldn't these also require write permissions?

Quoted 3 lines

Well, one argument would be that LOCK/UNLOCK in itself won't modify data and thus does not need write permission.. Although can brick a flash from ever being writable again and change content of flash registers.

Whether not you can brick a device (I agree with you), it is writing to the protection bits. But what is more imporant is that OTPLOCK is actually write-once.

Quoted 2 lines

I am fine with moving these to require write permissions as well (probably OTPLOCK as well).

Ok, I'm unsure about MEMSETBADBLOCK, though.

-michael