lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20251007224049.2798233-1-jayxu1990@gmail.com>
Date: Wed,  8 Oct 2025 06:40:49 +0800
From: jayxu1990@...il.com
To: Miquel Raynal <miquel.raynal@...tlin.com>,
	Richard Weinberger <richard@....at>,
	Vignesh Raghavendra <vigneshr@...com>
Cc: linux-mtd@...ts.infradead.org,
	linux-kernel@...r.kernel.org,
	avnerkhan@...xas.edu,
	rdlee.upstream@...il.com,
	Jay Xu <jayxu1990@...il.com>
Subject: [PATCH] mtd: core: Add nand_id sysfs attribute for NAND devices

From: Jay Xu <jayxu1990@...il.com>

[Problem]
Currently, NAND devices do not expose their NAND ID through sysfs,
making it difficult for userspace applications to identify the specific
NAND flash chip in use. For supply management reasons, electronics
products are typically manufactured with multiple storage device
suppliers, creating a need to identify which storage device is used
on a particular product. The NAND ID is a semi-unique identifier that can
be used to determine chip-specific characteristics such as maximum P/E
cycles, which is essential for NAND health monitoring and wear leveling
algorithms.

[Solution]
This patch adds a new 'nand_id' sysfs attribute that:

1. Exposes the full NAND ID (typically 5-8 bytes) in hexadecimal format
2. Only appears on physical NAND devices (MTD_NANDFLASH/MTD_MLCNANDFLASH)
3. Is hidden on virtual MTD devices
4. Reads from the master device to ensure consistent ID across partitions
5. Handles on-demand ID reading if not already populated during probe

The implementation uses a separate attribute group with visibility control
to avoid affecting existing MTD sysfs attributes. All NAND partitions
from the same physical chip will show the same ID, as expected.

This enables userspace tools to reliably identify NAND chips for
health monitoring, bad block management, and device-specific
optimizations.

Signed-off-by: Jay Xu <jayxu1990@...il.com>
---
 drivers/mtd/mtdcore.c | 60 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 59 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 5ba9a741f5ac..9290dbe04093 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -34,6 +34,7 @@
 
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/partitions.h>
+#include <linux/mtd/rawnand.h>
 
 #include "mtdcore.h"
 
@@ -339,6 +340,54 @@ static ssize_t mtd_bbt_blocks_show(struct device *dev,
 }
 MTD_DEVICE_ATTR_RO(bbt_blocks);
 
+static ssize_t mtd_nand_id_show(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	struct mtd_info *mtd = dev_get_drvdata(dev);
+	struct mtd_info *master = mtd_get_master(mtd);
+	struct nand_chip *chip;
+	int ret;
+
+	/* Ensure this is actually a NAND device */
+	if (master->type != MTD_NANDFLASH && master->type != MTD_MLCNANDFLASH)
+		return -ENODEV;
+
+	chip = mtd_to_nand(master);
+
+	/* If ID not populated, try to read it now */
+	if (!chip->id.len) {
+		ret = nand_readid_op(chip, 0, chip->id.data, NAND_MAX_ID_LEN);
+		if (ret)
+			return sysfs_emit(buf, "read-error\n");
+		chip->id.len = strnlen(chip->id.data, NAND_MAX_ID_LEN);
+	}
+
+	return sysfs_emit(buf, "%*phN\n", chip->id.len, chip->id.data);
+}
+MTD_DEVICE_ATTR_RO(nand_id);
+
+static umode_t mtd_nand_id_visible(struct kobject *kobj, struct attribute *attr, int n)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct mtd_info *mtd = dev_get_drvdata(dev);
+
+	/* Only show on NAND devices (excludes UBI volumes which have type 'ubi') */
+	if (mtd->type != MTD_NANDFLASH && mtd->type != MTD_MLCNANDFLASH)
+		return 0;
+
+	return attr->mode;
+}
+
+static struct attribute *mtd_nand_attrs[] = {
+	&dev_attr_nand_id.attr,
+	NULL,
+};
+
+static const struct attribute_group mtd_nand_group = {
+	.attrs = mtd_nand_attrs,
+	.is_visible = mtd_nand_id_visible,
+};
+
 static struct attribute *mtd_attrs[] = {
 	&dev_attr_type.attr,
 	&dev_attr_flags.attr,
@@ -359,7 +408,16 @@ static struct attribute *mtd_attrs[] = {
 	&dev_attr_bitflip_threshold.attr,
 	NULL,
 };
-ATTRIBUTE_GROUPS(mtd);
+
+static const struct attribute_group mtd_group = {
+	.attrs = mtd_attrs,
+};
+
+static const struct attribute_group *mtd_groups[] = {
+	&mtd_group,
+	&mtd_nand_group,
+	NULL,
+};
 
 static const struct device_type mtd_devtype = {
 	.name		= "mtd",
-- 
2.47.3


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ