From 9d7be234e1c516448e411320b96cbd219424d60f Mon Sep 17 00:00:00 2001
From: Nicolas Schichan <nschichan@freebox.fr>
Date: Wed, 23 Nov 2022 19:39:20 +0100
Subject: [PATCH 4/7] mmc-utils: allow to permanently disable password
 function.

---
 mmc.c      |  5 +++++
 mmc_cmds.c | 37 +++++++++++++++++++++++++++++++++++++
 mmc_cmds.h |  1 +
 3 files changed, 43 insertions(+)

diff --git a/mmc.c b/mmc.c
index f01118d..ff6a7e4 100644
--- a/mmc.c
+++ b/mmc.c
@@ -66,6 +66,11 @@ static struct Command commands[] = {
 	  "permanently disable permanent write protection on boot partitions",
 	  NULL,
 	},
+	{ do_pswd_perm_dis, -1,
+	  "pswd perm dis", "<device>\n"
+	  "permanently disable password configuration for <device>.",
+	  NULL,
+	},
 	{ do_writeprotect_device_perm_dis, -1,
 	  "writeprotect device perm dis", "<device>\n"
 	  "permanently disable permanent write protection on all partitions",
diff --git a/mmc_cmds.c b/mmc_cmds.c
index b51f813..8404b9f 100644
--- a/mmc_cmds.c
+++ b/mmc_cmds.c
@@ -465,6 +465,43 @@ int do_writeprotect_user_perm_dis(int nargs, char **argv)
 	return  ret;
 }
 
+int do_pswd_perm_dis(int nargs, char **argv)
+{
+	__u8 ext_csd[512], value;
+	const char *device = argv[1];
+	int fd, ret = 0;
+
+	if (nargs != 2) {
+		fprintf(stderr, "Usage: mmc pswd perm dis <path/to/mmcblkX>\n");
+		exit(1);
+	}
+
+	fd = open(device, O_RDWR);
+	if (fd < 0) {
+		perror("open");
+		exit(1);
+	}
+
+	ret = read_extcsd(fd, ext_csd);
+	if (ret) {
+		fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
+		exit(1);
+	}
+
+	value = ext_csd[EXT_CSD_USER_WP];
+	value |= USER_WP_PERM_PSWD_DIS;
+
+	ret = write_extcsd_value(fd, EXT_CSD_USER_WP, value, 0);
+	if (ret) {
+		fprintf(stderr, "Could not write 0x%02x to "
+			"EXT_CSD[%d] in %s\n",
+			value, EXT_CSD_USER_WP, device);
+		exit(1);
+	}
+
+	return  ret;
+}
+
 static char *prot_desc[] = {
 	"No",
 	"Temporary",
diff --git a/mmc_cmds.h b/mmc_cmds.h
index 4cf522c..f11d193 100644
--- a/mmc_cmds.h
+++ b/mmc_cmds.h
@@ -24,6 +24,7 @@ int do_writeprotect_boot_get(int nargs, char **argv);
 int do_writeprotect_boot_set(int nargs, char **argv);
 int do_writeprotect_boot_perm_dis(int nargs, char **argv);
 int do_writeprotect_device_perm_dis(int nargs, char **argv);
+int do_pswd_perm_dis(int nargs, char **argv);
 int do_writeprotect_user_perm_dis(int nargs, char **argv);
 int do_writeprotect_user_get(int nargs, char **argv);
 int do_writeprotect_user_set(int nargs, char **argv);
-- 
2.34.1

