Skip to contentSkip to conversation

mmc-utils: lsmmc: Fix Manufacturing Date decoding

PATCH In review [email protected] 2 participants 4 messages Dec 21, 2025 – Dec 30, 2025 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

cover mmc-utils: lsmmc: Fix Manufacturing Date decoding

From: Avri Altman <[email protected]>

This series fixes the decoding of the Manufacturing Date (MDT) field in the CID register within lsmmc.

The MDT year field is a 4-bit value relative to a base year. To determine the correct base year (1997, 2013, or 2029), the tool must consult the Extended CSD Revision (EXT_CSD_REV).

Previously, lsmmc processed registers in isolation and often lacked access to the EXT_CSD revision, leading to incorrect dates (e.g., interpreting a 2022 card as 2006).

Patch 1 updates the CID printing logic to select the correct base year based on the cached EXT_CSD revision.

Patch 2 ensures that lsmmc successfully retrieves and caches the EXT_CSD revision.

Avri Altman (2):
mmc-utils: lsmmc: fix CID manufacturing date decoding
mmc-utils: lsmmc: cache ext_csd revision for MMC devices

 lsmmc.c    | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 mmc_cmds.c |  2 +-
 mmc_cmds.h |  3 +++
 3 files changed, 62 insertions(+), 3 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 mmc-utils: lsmmc: fix CID manufacturing date decoding

From: Avri Altman <[email protected]>

The CID register contains a 1-byte Manufacturing Date (MDT) field, where 4 bits represent the month and 4 bits represent the year offset. Originally, the base year was 1997.

However, as the 4-bit year field wraps around every 16 years, newer MMC specifications defined new base years based on the Extended CSD Revision (EXT_CSD_REV):
- Rev <= 4: Base 1997
- Rev > 4: Base 2013
- Rev > 8: Base 2029 (with specific overlap handling for 2026-2028)

Previously, lsmmc hardcoded the base year to 1997, causing newer cards to report incorrect manufacturing years (e.g., reporting 2000 instead of 2016).

This patch adds logic to calculate the correct base year using the cached ext_csd_rev. It also adds a warning if the EXT_CSD revision is unavailable, as the date cannot be guaranteed accurate without it.

---
 lsmmc.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
lsmmc.c +22 -2
index dd3ab83..b7b9378 100644
@@ -67,6 +67,7 @@ struct config {
 
 	enum bus_type bus;
 	char *reg;
+	unsigned int ext_csd_rev;
 };
 
 enum REG_TYPE {
@@ -586,6 +587,7 @@ static void print_mmc_cid(struct config *config, char *cid)
 	unsigned int mdt_year;
 	unsigned int crc;
 	char *manufacturer = NULL;
+	int base_year = 1997;
 
 	parse_bin(cid, "8u6r2u8u48a4u4u32u4u4u7u1r",
 		&mid, &cbx, &oid, &pnm[0], &prv_major, &prv_minor, &psn,
@@ -595,6 +597,18 @@ static void print_mmc_cid(struct config *config, char *cid)
 
 	manufacturer = get_manufacturer(config, mid);
 
+	if (config->ext_csd_rev) {
+		/* Adjust base year according to ext_csd_rev */
+		if (config->ext_csd_rev > 8) {
+			base_year = 2029;
+			if (mdt_year >= 13)
+				base_year = 2013;
+		} else if (config->ext_csd_rev > 4) {
+			base_year = 2013;
+		}
+	}
+
+
 	if (config->verbose) {
 		printf("======MMC/CID======\n");
 
@@ -626,7 +640,10 @@ static void print_mmc_cid(struct config *config, char *cid)
 		printf("(%u.%u)\n", prv_major, prv_minor);
 		printf("\tPSN: 0x%08x\n", psn);
 		printf("\tMDT: 0x%01x%01x %u %s\n", mdt_month, mdt_year,
-		       1997 + mdt_year, months[mdt_month]);
+		       base_year + mdt_year, months[mdt_month]);
+		if (!config->ext_csd_rev)
+			printf("\tWarn: ext_csd_rev not provided, "
+			       "manufacturing date year may be wrong.\n");
 		printf("\tCRC: 0x%02x\n", crc);
 	} else {
 		if (manufacturer)
@@ -637,8 +654,11 @@ static void print_mmc_cid(struct config *config, char *cid)
 
 		printf("product: '%s' %u.%u\n", pnm, prv_major, prv_minor);
 		printf("serial: 0x%08x\n", psn);
-		printf("manufacturing date: %u %s\n", 1997 + mdt_year,
+		printf("manufacturing date: %u %s\n", base_year + mdt_year,
 		       months[mdt_month]);
+		if (!config->ext_csd_rev)
+			printf("Warn: ext_csd_rev not provided, "
+			       "manufacturing date year may be wrong.\n");
 	}
 }
 
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/2 mmc-utils: lsmmc: cache ext_csd revision for MMC devices

In reply to [email protected]

From: Avri Altman <[email protected]>

The Manufacturing Date (MDT) field in the CID register depends on the Extended CSD Revision (EXT_CSD_REV) to determine the correct base year (1997, 2013, or 2029).

Currently, lsmmc processes registers in isolation. To enable correct MDT decoding in the CID print function, we need to read and cache the EXT_CSD revision when processing MMC devices from sysfs.

This patch adds logic to process_reg_from_file to attempt reading the ext_csd binary file and storing the revision byte (index 192) in the config structure.

Note: Opening the block device node to issue ioctls requires root privileges (sudo), whereas reading sysfs files might not.

---
 lsmmc.c    | 36 ++++++++++++++++++++++++++++++++++++
 mmc_cmds.c |  2 +-
 mmc_cmds.h |  3 +++
 3 files changed, 40 insertions(+), 1 deletion(-)
lsmmc.c +36 -0
index b7b9378..e9df5e1 100644
@@ -45,6 +45,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <fcntl.h>
 
 #include "mmc.h"
 #include "mmc_cmds.h"
@@ -2201,11 +2202,46 @@ err:
 	return ret;
 }
 
+static void read_mmc_extcsd_rev(struct config *config)
+{
+	DIR *dir;
+	struct dirent *dent;
+
+	if (config->bus != MMC)
+		return;
+
+	dir = opendir("block");
+	if (!dir)
+		return;
+
+	while ((dent = readdir(dir)) != NULL) {
+		if (strncmp(dent->d_name, "mmcblk", 6) == 0) {
+			char devpath[512];
+			int fd;
+			__u8 ext_csd[512];
+
+			snprintf(devpath, sizeof(devpath), "/dev/%s", dent->d_name);
+			fd = open(devpath, O_RDONLY);
+			if (fd >= 0) {
+				if (read_extcsd(fd, ext_csd) == 0)
+					config->ext_csd_rev = ext_csd[EXT_CSD_REV];
+				close(fd);
+			}
+
+			break;
+		}
+	}
+
+	closedir(dir);
+}
+
 static int process_reg_from_file(struct config *config, enum REG_TYPE reg)
 {
 	char *reg_content = NULL;
 	int ret = 0;
 
+	read_mmc_extcsd_rev(config);
+
 	switch (reg) {
 	case CID:
 		reg_content = read_file("cid");
mmc_cmds.c +1 -1
index 5990502..4c8c127 100644
@@ -74,7 +74,7 @@ static inline __u32 per_byte_htole32(__u8 *arr)
 	return arr[0] | arr[1] << 8 | arr[2] << 16 | arr[3] << 24;
 }
 
-static int read_extcsd(int fd, __u8 *ext_csd)
+int read_extcsd(int fd, __u8 *ext_csd)
 {
 	int ret = 0;
 	struct mmc_ioc_cmd idata = {};
mmc_cmds.h +3 -0
index f767deb..9d5f944 100644
@@ -17,12 +17,15 @@
  * those modifications are Copyright (c) 2016 SanDisk Corp.
  */
 
+#include <linux/types.h>
+
 typedef int (*CommandFunction)(int argc, char **argv);
 
 void print_usage(CommandFunction func);
 
 /* mmc_cmds.c */
 int do_read_extcsd(int nargs, char **argv);
+int read_extcsd(int fd, __u8 *ext_csd);
 int do_write_extcsd(int nargs, char **argv);
 int do_writeprotect_boot_get(int nargs, char **argv);
 int do_writeprotect_boot_set(int nargs, char **argv);
Signature · 1 line
2.34.1
#3 lore raw
In reply to [email protected]
Quoted 24 lines · On Sun, 21 Dec 2025 at 09:00, <[email protected]> wrote:

From: Avri Altman <[email protected]>

This series fixes the decoding of the Manufacturing Date (MDT) field in the CID register within lsmmc.

The MDT year field is a 4-bit value relative to a base year. To determine the correct base year (1997, 2013, or 2029), the tool must consult the Extended CSD Revision (EXT_CSD_REV).

Previously, lsmmc processed registers in isolation and often lacked access to the EXT_CSD revision, leading to incorrect dates (e.g., interpreting a 2022 card as 2006).

Patch 1 updates the CID printing logic to select the correct base year based on the cached EXT_CSD revision.

Patch 2 ensures that lsmmc successfully retrieves and caches the EXT_CSD revision.

Avri Altman (2):
mmc-utils: lsmmc: fix CID manufacturing date decoding
mmc-utils: lsmmc: cache ext_csd revision for MMC devices

Quoted 8 lines
 lsmmc.c    | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 mmc_cmds.c |  2 +-
 mmc_cmds.h |  3 +++
 3 files changed, 62 insertions(+), 3 deletions(-)

--
2.34.1

The series applied for mmc-utils master, thanks!

Kind regards
Uffe